#!/bin/bash
set -e
set -o pipefail
+
+cpus=$(
+ perl -we '
+ use strict;
+ require Sys::CPU;
+ my $ncpus = Sys::CPU::cpu_count();
+ $ncpus *= 1.5;
+ print join " ", (0 .. $ncpus-1) or die $!;
+ print "\n" or die $!;
+ ' ||
+ echo 0 1 2
+)
+
+uid=`id -u`
+cd /var/run/user/$uid
+mkdir -p version-charset-test
+cd version-charset-test
+
rm -rf stunt
mkdir stunt
cd stunt
-git init
-git commit --allow-empty -m 'Test object'
-head=`git rev-parse HEAD`
-perl -ne 'chomp; print "create refs/tags/$_ '$head'\n" or die $!' \
-| tee /dev/tty \
-| git update-ref --stdin
+
+for cpu in $cpus; do
+ mkdir c$cpu
+ (cd c$cpu
+ git init)
+done
+
+#exec 3<&0 </dev/null
+
+#git commit --allow-empty -m 'Test object'
+#head=`git rev-parse HEAD`
+
+#exec <&3 3<&-
+
+perl -we '
+ use strict;
+
+ our $file;
+ our $count;
+ our $batch=0;
+ our @tasks;
+
+ my @freecpus = qw('"$cpus"');
+
+ print STDERR "check using cpus @freecpus\n";
+
+ sub await_task () {
+ my ($pid, $cpu, $oldbatch) = @{ shift @tasks };
+ waitpid $pid, 0 == $pid or die $!;
+ die "$oldbatch $?" if $?;
+ unlink "b$oldbatch" or die "$oldbatch $!";
+ print STDERR "check completed $oldbatch [$pid] #$cpu\n";
+ push @freecpus, $cpu;
+ }
+
+ sub complete_batch () {
+ return unless $file;
+
+ close $file or die $!;
+
+ print STDERR "check closing $batch ($count)\n";
+
+ await_task unless @freecpus;
+
+ my $cpu = shift @freecpus;
+
+ my $pid = fork // die $!;
+ if (!$pid) {
+ close STDIN;
+# seek STDIN,0,1;
+# flush STDIN;
+ open STDIN, "<", "b$batch" or die $batch;
+ chdir "c$cpu" or die "$cpu $!";
+ exec qw(eatmydata git update-ref --stdin);
+ die $!;
+ }
+ print STDERR "check spawned $batch ($count) [$pid] #$cpu\n";
+ push @tasks, [ $pid, $cpu, $batch ];
+ $batch++;
+ $file = undef;
+ $count = 0;
+ }
+
+ while (<>) {
+#print STDERR ">$_<\n";
+ $file ||= new IO::File "b$batch", ">" or die $!;
+ chomp;
+ print $file "delete refs/tags/$_\n" or die $!;
+ $count++ < 100000 or complete_batch;
+ }
+#print STDERR "FOO\n";
+ STDIN->error and die $!;
+ complete_batch();
+ await_task while @tasks;
+'