2 use List
::Util
'shuffle'; # standard from Perl 5.8 and later
4 my $tempfile = "multigcc.out";
9 # parse command line arguments
21 my $command = join " ", @params;
23 # shuffle the file list to spread the load as evenly as we can
24 @files = shuffle
(@files);
26 # count number of cores
28 # Don't use given/when here - it's not compatible with old perl versions
29 if ($^O
eq 'darwin') {
30 chomp($cores = `sysctl -n hw.ncpu`);
33 elsif ($^O
eq 'solaris') {
34 $cores = scalar grep /on-line/i, `psrinfo`;
38 if (open CPUINFO
, "</proc/cpuinfo") {
39 $cores = scalar grep /^processor/i, <CPUINFO
>;
49 my $slice = int((scalar @files + $cores) / $cores);
51 # reset $cores to 0 so we can count the number of actually used cores
54 for (my $i=0;$i<scalar @files;$i += $slice)
60 $pids[$cores++] = $pid;
64 # get my slice of the files
65 my @list = @files[$i .. $i + $slice - 1];
68 system("$command @list > $tempfile.$$");
74 for my $i (0 .. $cores - 1)
76 # wait for child to complete
80 if (open F
, "<$tempfile.$pids[$i]")
84 unlink "$tempfile.$pids[$i]";