summaryrefslogtreecommitdiffstats
path: root/tools/multigcc.pl
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2009-07-24 21:53:32 +0000
committerBjörn Stenberg <bjorn@haxx.se>2009-07-24 21:53:32 +0000
commit4fc00222cb5851e28e83d367f173038c7d026b03 (patch)
tree01dc3cc97e5c09b405249686be586f5c3e1776b9 /tools/multigcc.pl
parenteb0061411d6fa08ab540107cdbd2906e18e516d7 (diff)
downloadrockbox-4fc00222cb5851e28e83d367f173038c7d026b03.tar.gz
rockbox-4fc00222cb5851e28e83d367f173038c7d026b03.zip
Dependency generation now uses all cores on multi-core machines.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22021 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'tools/multigcc.pl')
-rwxr-xr-xtools/multigcc.pl72
1 files changed, 72 insertions, 0 deletions
diff --git a/tools/multigcc.pl b/tools/multigcc.pl
new file mode 100755
index 0000000000..db544355ca
--- /dev/null
+++ b/tools/multigcc.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+use List::Util 'shuffle'; # standard from Perl 5.8 and later
+
+my $tempfile = "multigcc.out";
+my @params;
+my @files;
+my $list = \@params;
+
+# parse command line arguments
+for my $a (@ARGV) {
+ if ($a eq "--") {
+ $list = \@files;
+ next;
+ }
+
+ push @{$list}, $a;
+}
+
+my $command = join " ", @params;
+
+# shuffle the file list to spread the load as evenly as we can
+@files = shuffle(@files);
+
+# count number of cores
+my $cores = 1;
+if (open CPUINFO, "</proc/cpuinfo") {
+ $cores = scalar grep /^processor/i, <CPUINFO>;
+ close CPUINFO;
+}
+
+# don't run empty children
+if (scalar @files < $cores)
+{
+ $cores = 1;
+}
+
+# fork children
+my @pids;
+my $slice = int((scalar @files / $cores) + 0.5);
+for my $i (0 .. $cores-1)
+{
+ my $pid = fork;
+ if ($pid)
+ {
+ # mother
+ $pids[$i] = $pid;
+ }
+ else
+ {
+ # get my slice of the files
+ my @list = @files[$i * $slice .. $i * $slice + $slice - 1];
+
+ # run command
+ system("$command @list > $tempfile.$$");
+
+ exit;
+ }
+}
+
+for my $i (0 .. $cores - 1)
+{
+ # wait for child to complete
+ waitpid $pids[$i], 0;
+
+ # read & print result
+ if (open F, "<$tempfile.$pids[$i]")
+ {
+ print <F>;
+ close F;
+ unlink "$tempfile.$pids[$i]";
+ }
+}