hm60x: Use binary search in button driver.
[maemo-rb.git] / tools / multigcc.pl
blobc272ebae08f6bad44c80c8917b3177c268380b0c
1 #!/usr/bin/perl
2 use List::Util 'shuffle'; # standard from Perl 5.8 and later
4 my $tempfile = "multigcc.out";
5 my @params;
6 my @files;
7 my $list = \@params;
9 # parse command line arguments
10 for my $a (@ARGV) {
11 if ($a eq "--") {
12 $list = \@files;
13 next;
16 push @{$list}, $a;
19 exit if (not @files);
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
27 my $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`);
31 $cores = 1 if ($?);
33 elsif ($^O eq 'solaris') {
34 $cores = scalar grep /on-line/i, `psrinfo`;
35 $cores = 1 if ($?);
37 else {
38 if (open CPUINFO, "</proc/cpuinfo") {
39 $cores = scalar grep /^processor/i, <CPUINFO>;
40 close CPUINFO;
42 else {
43 $cores = 1;
47 # fork children
48 my @pids;
49 my $slice = int((scalar @files + $cores) / $cores);
51 # reset $cores to 0 so we can count the number of actually used cores
52 $cores=0;
54 for (my $i=0;$i<scalar @files;$i += $slice)
56 my $pid = fork;
57 if ($pid)
59 # mother
60 $pids[$cores++] = $pid;
62 else
64 # get my slice of the files
65 my @list = @files[$i .. $i + $slice - 1];
67 # run command
68 system("$command @list > $tempfile.$$");
70 exit;
74 for my $i (0 .. $cores - 1)
76 # wait for child to complete
77 waitpid $pids[$i], 0;
79 # read & print result
80 if (open F, "<$tempfile.$pids[$i]")
82 print <F>;
83 close F;
84 unlink "$tempfile.$pids[$i]";