| 1 | package Proc::Forkmap; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use POSIX ":sys_wait_h"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | require Exporter; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 5 | our @EXPORT = qw/ | |||||||||||||||||||||||||||
| 12 | forkmap_settings | ||||||||||||||||||||||||||||
| 13 | forkmap | ||||||||||||||||||||||||||||
| 14 | /; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | our $VERSION = '0.2305'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | my $MAX_PROCS = 4; | |||||||||||||||||||||||||||
| 19 | 5 | my $TIMEOUT = 0; | |||||||||||||||||||||||||||
| 20 | 5 | my $IPC = 1; | |||||||||||||||||||||||||||
| 21 | 5 | my $TEMPFILE_DIR; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub forkmap_settings { | ||||||||||||||||||||||||||
|
Subroutine
forkmap_settings: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | my %opts = @_; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | $MAX_PROCS = $opts{MAX_PROCS} if exists $opts{MAX_PROCS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $TIMEOUT = $opts{TIMEOUT} if exists $opts{TIMEOUT}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $IPC = $opts{IPC} if exists $opts{IPC}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $TEMPFILE_DIR = $opts{TEMPFILE_DIR} if exists $opts{TEMPFILE_DIR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub forkmap (&@) { | ||||||||||||||||||||||||||
|
Subroutine
forkmap: called CC=37 SCAR=56.6
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 4 | my $fn = shift; | |||||||||||||||||||||||||||
| 33 | 4 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 4 | my @args = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | unless ($IPC) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | my $procs = 0; | |||||||||||||||||||||||||||
| 38 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | while ($i <= $#args || $procs > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i <= $#args or $procs > 0
MC/DC0%0/2
$i <= $#args or $procs > 0
$i <= $#args
$procs > 0
Truth table0%0/3
A or B
A= $i <= $#argsB= $procs > 0
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | while ($procs < $MAX_PROCS && $i <= $#args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$procs < $MAX_PROCS and $i <= $#args
MC/DC0%0/2
$procs < $MAX_PROCS and $i <= $#args
$procs < $MAX_PROCS
$i <= $#args
Truth table0%0/3
A and B
A= $procs < $MAX_PROCSB= $i <= $#args
| |||||||||||||||||||||||||||||
| 42 | 0 | my $idx = $i; | |||||||||||||||||||||||||||
| 43 | 0 | my $pid = fork(); | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | die "bad fork: $!" unless defined $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($TIMEOUT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | local $SIG{ALRM} = sub { die "timeout: pid [$$]" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | alarm $TIMEOUT; | |||||||||||||||||||||||||||
| 49 | 0 | eval { | |||||||||||||||||||||||||||
| 50 | 0 | local $_ = $args[$idx]; | |||||||||||||||||||||||||||
| 51 | 0 | $fn->($_); | |||||||||||||||||||||||||||
| 52 | 0 | alarm 0; | |||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | die "$@"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | 0 | local $_ = $args[$idx]; | |||||||||||||||||||||||||||
| 59 | 0 | $fn->($_); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $cb->($idx) if $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | exit 0; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | $procs++; | |||||||||||||||||||||||||||
| 65 | 0 | $i++; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | while (waitpid(-1, WNOHANG) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $procs--; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | select undef, undef, undef, 0.1 if $procs >= $MAX_PROCS || $i > $#args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$procs >= $MAX_PROCS or $i > $#args
MC/DC0%0/2
$procs >= $MAX_PROCS or $i > $#args
$procs >= $MAX_PROCS
$i > $#args
Truth table0%0/3
A or B
A= $procs >= $MAX_PROCSB= $i > $#args
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | return; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | my $sel = IO::Select->new(); | |||||||||||||||||||||||||||
| 77 | 4 | my @res; | |||||||||||||||||||||||||||
| 78 | 4 | my $procs = 0; | |||||||||||||||||||||||||||
| 79 | 4 | my $i = 0; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | while ($i <= $#args || $procs > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i <= $#args or $procs > 0
MC/DC100%2/2
$i <= $#args or $procs > 0
$i <= $#args
$procs > 0
Truth table100%3/3
A or B
A= $i <= $#argsB= $procs > 0
| |||||||||||||||||||||||||||||
| 82 | 38122 | ▶ | while ($procs < $MAX_PROCS && $i <= $#args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$procs < $MAX_PROCS and $i <= $#args
MC/DC50%1/2
$procs < $MAX_PROCS and $i <= $#args
$procs < $MAX_PROCS
$i <= $#args
Truth table66%2/3
A and B
A= $procs < $MAX_PROCSB= $i <= $#args
| |||||||||||||||||||||||||||||
| 83 | 9 | ▶ | pipe(my $r, my $w) or die "bad pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 9 | my $idx = $i; | |||||||||||||||||||||||||||
| 85 | 9 | my $pid = fork(); | |||||||||||||||||||||||||||
| 86 | 9 | ▶ | die "bad fork: $!" unless defined $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 9 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 3 | close $r; | |||||||||||||||||||||||||||
| 89 | 3 | my ($fh, $fname) = tempfile(DIR => $TEMPFILE_DIR, UNLINK => 0); | |||||||||||||||||||||||||||
| 90 | 3 | ▶ | if ($TIMEOUT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | local $SIG{ALRM} = sub { die "timeout: pid [$$] index [$idx]" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 92 | 0 | alarm $TIMEOUT; | |||||||||||||||||||||||||||
| 93 | 0 | eval { | |||||||||||||||||||||||||||
| 94 | 0 | local $_ = $args[$idx]; | |||||||||||||||||||||||||||
| 95 | 0 | my $t = $fn->($_); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | print $fh $t if defined $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | alarm 0; | |||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | close $fh if defined fileno $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unlink $fname if defined $fname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | die "$@"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||
| 105 | 3 | eval { | |||||||||||||||||||||||||||
| 106 | 3 | local $_ = $args[$idx]; | |||||||||||||||||||||||||||
| 107 | 3 | my $t = $fn->($_); | |||||||||||||||||||||||||||
| 108 | 3 | ▶ | print $fh $t if defined $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | close $fh if defined fileno $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | unlink $fname if defined $fname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | die "$@"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 3 | close $fh; | |||||||||||||||||||||||||||
| 117 | 3 | print $w "$idx:$fname\n"; | |||||||||||||||||||||||||||
| 118 | 3 | close $w; | |||||||||||||||||||||||||||
| 119 | 3 | exit 0; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 6 | close $w; | |||||||||||||||||||||||||||
| 122 | 6 | $sel->add($r); | |||||||||||||||||||||||||||
| 123 | 6 | $procs++; | |||||||||||||||||||||||||||
| 124 | 6 | $i++; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 38119 | for my $fh ($sel->can_read(0.1)) { | |||||||||||||||||||||||||||
| 128 | 6 | my $line = <$fh>; | |||||||||||||||||||||||||||
| 129 | 6 | ▶ | if (defined $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 3 | chomp $line; | |||||||||||||||||||||||||||
| 131 | 3 | my ($idx, $fname) = split /:/, $line, 2; | |||||||||||||||||||||||||||
| 132 | 3 | ▶ | open my $f, "<", $fname or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | warn "bad file [$fname]: $!"; | |||||||||||||||||||||||||||
| 134 | 0 | $res[$idx] = undef; | |||||||||||||||||||||||||||
| 135 | 0 | next; | |||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||
| 137 | 3 | my $d = do {local $/; <$f>}; | |||||||||||||||||||||||||||
| 138 | 3 | close $f; | |||||||||||||||||||||||||||
| 139 | 3 | unlink $fname; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | ▶ | if ($cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | $cb->($idx, $d); | |||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||
| 144 | 3 | $res[$idx] = $d; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 3 | $sel->remove($fh); | |||||||||||||||||||||||||||
| 148 | 3 | close $fh; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 38119 | ▶ | while (waitpid(-1, WNOHANG) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 3 | $procs--; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | return $cb ? undef : @res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 5 | 1; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||