blib/lib/Proc/Forkmap.pm

filter: Statementstmt 59.0% 75 / 127 Branchbran 29.4% 20 / 68 Conditioncond 33.3% 5 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 70.0% 7 / 10 Podpod 100% 2 / 2 totaltotal 48.2% 112 / 232 CC 41 SCAR 53.7
CC
41
Cov
48%
forkmap
286.9
forkmap_settings
30.0
__ANON__
2.0
CRAP 215.5
↑ Summary
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
BranchTrueFalseTotal
if exists $opts{"MAX_PROCS"} 0 0 0
26 0 ▶ $TIMEOUT = $opts{TIMEOUT} if exists $opts{TIMEOUT};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"TIMEOUT"} 0 0 0
27 0 ▶ $IPC = $opts{IPC} if exists $opts{IPC};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"IPC"} 0 0 0
28 0 ▶ $TEMPFILE_DIR = $opts{TEMPFILE_DIR} if exists $opts{TEMPFILE_DIR};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"TEMPFILE_DIR"} 0 0 0
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
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 4 4
34 4 my @args = @_;
35  
36 4 ▶ unless ($IPC) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IPC) 0 4 4
37 0 my $procs = 0;
38 0 my $i = 0;
39  
40 0 ▶ while ($i <= $#args || $procs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#args or $procs > 0) 0 0 0
Condition0%0/3
$i <= $#args or $procs > 0
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
41 0 ▶ while ($procs < $MAX_PROCS && $i <= $#args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($procs < $MAX_PROCS and $i <= $#args) 0 0 0
Condition0%0/3
$procs < $MAX_PROCS and $i <= $#args
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
42 0 my $idx = $i;
43 0 my $pid = fork();
44 0 ▶ die "bad fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
45 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
46 0 ▶ if ($TIMEOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TIMEOUT) { } 0 0 0
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
BranchTrueFalseTotal
if ($@) 0 0 0
55 0 die "$@";
56 }
57 } else {
58 0 local $_ = $args[$idx];
59 0 $fn->($_);
60 }
61 0 ▶ $cb->($idx) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
62 0 exit 0;
63 }
64 0 $procs++;
65 0 $i++;
66 }
67  
68 0 ▶ while (waitpid(-1, WNOHANG) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid(-1, 1) > 0) 0 0 0
69 0 $procs--;
70 }
71 0 ▶ select undef, undef, undef, 0.1 if $procs >= $MAX_PROCS || $i > $#args;
Branch: 1 branch
BranchTrueFalseTotal
if $procs >= $MAX_PROCS or $i > $#args 0 0 0
Condition0%0/3
$procs >= $MAX_PROCS or $i > $#args
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($i <= $#args or $procs > 0) 38122 1 38123
Condition100%3/3
$i <= $#args or $procs > 0
l!l&&r!l&&!r
4381181
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
ABresult
1X1
011
000
82 38122 ▶ while ($procs < $MAX_PROCS && $i <= $#args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($procs < $MAX_PROCS and $i <= $#args) 9 38119 38128
Condition66%2/3
$procs < $MAX_PROCS and $i <= $#args
!ll&&!rl&&r
0381199
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
ABresult
0X0
100
111
83 9 ▶ pipe(my $r, my $w) or die "bad pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $r, my $w or die "bad pipe: $!" 9 0 9
84 9 my $idx = $i;
85 9 my $pid = fork();
86 9 ▶ die "bad fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 9 9
87 9 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 3 6 9
88 3 close $r;
89 3 my ($fh, $fname) = tempfile(DIR => $TEMPFILE_DIR, UNLINK => 0);
90 3 ▶ if ($TIMEOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TIMEOUT) { } 0 3 3
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
BranchTrueFalseTotal
if defined $t 0 0 0
97 0 alarm 0;
98 };
99 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
100 0 ▶ close $fh if defined fileno $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $fh 0 0 0
101 0 ▶ unlink $fname if defined $fname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fname 0 0 0
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
BranchTrueFalseTotal
if defined $t 3 0 3
109 };
110 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
111 0 ▶ close $fh if defined fileno $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $fh 0 0 0
112 0 ▶ unlink $fname if defined $fname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fname 0 0 0
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
BranchTrueFalseTotal
if (defined $line) { } 3 3 6
130 3 chomp $line;
131 3 my ($idx, $fname) = split /:/, $line, 2;
132 3 ▶ open my $f, "<", $fname or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $f, "<", $fname or do { warn "bad file [$fname]: $!"; $res[$idx] = undef; next } 3 0 3
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
BranchTrueFalseTotal
if ($cb) { } 0 3 3
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
BranchTrueFalseTotal
if (waitpid(-1, 1) > 0) 3 38119 38122
152 3 $procs--;
153 }
154 }
155  
156 1 ▶ return $cb ? undef : @res;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 1 1
157 }
158  
159 5 1;
160  
161 __END__
↑ Summary