summaryrefslogtreecommitdiffstats
path: root/tools/multigcc.pl
blob: 9be9978bd4897870d83a1a839c09c4c75fe31d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl
use Switch;
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;
}

exit if (not @files);

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;
switch($^O) {
    case "darwin" {
        chomp($cores = `sysctl -n hw.ncpu`);
        $cores = 1 if ($?);
    }
    case "solaris" {
        $cores = scalar grep /on-line/i, `psrinfo`;
        $cores = 1 if ($?);
    }
    else {
        if (open CPUINFO, "</proc/cpuinfo") {
            $cores = scalar grep /^processor/i, <CPUINFO>;
            close CPUINFO;
        }
        else {
            $cores = 1;
        }
    }
}

# fork children
my @pids;
my $slice = int((scalar @files + $cores) / $cores);

# reset $cores to 0 so we can count the number of actually used cores
$cores=0;

for (my $i=0;$i<scalar @files;$i += $slice)
{
    my $pid = fork;
    if ($pid)
    {
        # mother
        $pids[$cores++] = $pid;
    }
    else
    {
        # get my slice of the files
        my @list = @files[$i .. $i + $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]";
    }
}