blib/lib/Parallel/Queue.pm

filter: Statementstmt 87.8% 101 / 115 Branchbran 69.1% 65 / 94 Conditioncond 78.5% 11 / 14 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 6 totaltotal 77.5% 193 / 249 CC 56 SCAR 25.8
CC
56
Cov
79%
fork_queue
21.3
import
16.6
next_job
14.3
CRAP 13.2
↑ Summary
1 ########################################################################
2 # housekeeping
3 ########################################################################
4  
5 package Parallel::Queue;
6 90 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 90 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 90 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 90 ▶use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 90 ▶use Symbol qw( qualify_to_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ########################################################################
15 # package variables
16 ########################################################################
17  
18 90 our $VERSION = '3.6';
19 90 $VERSION = eval $VERSION;
20  
21 # defaults.
22  
23 90 my $fork = '';
24 90 my $verbose = '';
25 90 my $finish = '';
26  
27 ########################################################################
28 # execution handlers
29 ########################################################################
30  
31 sub next_job
32 {
33 3505 ▶ state $next = '';
Subroutine next_job: called CC=14 SCAR=26.6
Pod undocumented
34 3505 my $job = '';
35  
36 JOB:
37 3505 for(;;)
38 {
39 3517 ▶ if( $next )
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 48 3469 3517
40 {
41 48 ▶ $job = $next->( $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
$job = &$next($_[0]) and last 42 6 48
42 and last;
43  
44 6 ▶ say STDERR "Completed iterator: '$_[0]'"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 6 6
45 if $verbose;
46  
47 6 $next = '';
48 6 shift;
49 }
50  
51 3475 ▶ @_ or last;
Branch: 1 branch
BranchTrueFalseTotal
@_ or last 11 3464 3475
52  
53 3464 for my $item ( $_[0] )
54 {
55 3464 ▶ if
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($item) and $next = $item->can('next_job')) { } 6 3458 3464
elsif (ref $item) { } 3452 6 3458
elsif ($item) { } 6 0 6
Condition66%2/3
builtin::blessed($item) and $next = $item->can("next_job")
!ll&&!rl&&r
345806
MC/DC50%1/2
builtin::blessed($item) and $next = $item->can("next_job")
builtin::blessed($item) $next = $item->can("next_job")
Truth table66%2/3
A and B
A= builtin::blessed($item)B= $next = $item->can("next_job")
ABresult
0X0
100
111
56 (
57 blessed $item
58 and
59 $next = $item->can( 'next_job' )
60 )
61 {
62 6 ▶ say STDERR "New iterator: '$next' ($_[0])"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 6 6
63 if $verbose;
64  
65 next JOB
66 6 }
67 elsif( ref $item )
68 {
69 3452 $job = shift;
70 }
71 elsif( $item )
72 {
73 # these might end up being processing directives
74 # later on, for now ignore them.
75  
76 6 say STDERR "Discarding non-queue '$item'";
77  
78 shift
79 6 }
80 else
81 {
82 # silently ignore empty filler.
83  
84 0 shift;
85 }
86 }
87  
88 3458 ▶ last if $job;
Branch: 1 branch
BranchTrueFalseTotal
if $job 3452 6 3458
89 }
90  
91 3505 ▶ say STDERR "Next job: '$job'"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 126 3379 3505
92 if $verbose;
93  
94 3505 ▶ $job
Branch: 1 branch
BranchTrueFalseTotal
$job or return 11 3494 3505
95 or return
96 }
97  
98 sub run_nofork
99 {
100 # discard the count, iterate the queue without forking.
101  
102 13 ▶ shift;
Subroutine run_nofork: called CC=6 SCAR=18.1
Pod undocumented
103  
104 13 ▶ say STDERR "Non-forking queue"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 6 7 13
105 if $verbose;
106  
107 13 ▶ while( my $sub = &next_job )
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = &next_job) 174 11 185
108 {
109 # these should all exit zero.
110  
111 174 my $result = eval { $sub->() };
112  
113 174 ▶ say STDERR "Complete: '$result' ($@)"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 120 54 174
114 if $verbose;
115  
116 174 ▶ if( $result )
Branch: 2 branches
BranchTrueFalseTotal
if ($result) { } 2 172 174
elsif ($@) { } 0 172 172
117 {
118 2 say STDERR "Non-zero exit: $result, aborting queue";
119  
120 last
121 2 }
122 elsif( $@ )
123 {
124 0 say STDERR "Error in job: $@";
125  
126 last
127 0 }
128 }
129  
130 return
131 13 }
132  
133 sub fork_job
134 {
135 @_
136 or do
137 3320 ▶ {
Subroutine fork_job: called CC=9 SCAR=26.3
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ or do { say STDERR "Queue empty." if $verbose; return } 0 3320 3320
138 0 ▶ say STDERR "Queue empty."
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
139 if $verbose;
140  
141 return
142 0 };
143  
144 3320 ▶ my $job = &next_job
Branch: 1 branch
BranchTrueFalseTotal
my $job = &next_job or return 0 3320 3320
145 or return;
146  
147 3320 ▶ if( ( my $pid = fork ) > 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ((my $pid = fork) > 0) { } 3240 80 3320
elsif (defined $pid) { } 80 0 80
148 {
149 3240 ▶ say STDERR "fork: $pid"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3240 3240
150 if $verbose;
151  
152 # nothing useful to hand back.
153  
154 return
155 3240 }
156 elsif( defined $pid )
157 {
158 # child passes the exit status of the perl sub call
159 # to the caller as our exit status. the O/S will deal
160 # with signal values.
161 #
162 # aside: failing to exit here will cause runaway
163 # phorkatosis.
164  
165 80 ▶ say STDERR "\tExecuting: '$job'"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 80 80
166 if $verbose;
167  
168 80 my $exitval = eval { $job->() };
169  
170 80 ▶ $@
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 80 80
171 ? die $@
172 : exit $exitval
173 }
174 else
175 {
176 # pass back the fork failure for the caller to deal with.
177  
178 0 die "Phorkafobia: $!";
179 }
180 };
181  
182 sub fork_queue
183 {
184 # count was validated in runqueue.
185  
186 204 ▶ my $count = shift;
Subroutine fork_queue: called CC=10 SCAR=30.6
Pod undocumented
187  
188 # what's left on the stack are the jobs to run.
189 # which may be none.
190 # if so, we're done.
191  
192 204 ▶ say STDERR "Forking initial $count jobs"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 204 204
193 if $verbose;
194  
195 204 &fork_job for 1 .. $count;
196  
197 194 ▶ while( (my $pid = wait) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pid = wait) > 0) 3132 124 3256
198 {
199 3132 ▶ say STDERR "Complete: $pid ($?)"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3132 3132
200 if $verbose;
201  
202 # this assumes normal *NIX 16-bit exit values,
203 # with a status in the high byte and signum
204 # in the lower. notice that $status is not
205 # masked to 8 bits, however. this allows us to
206 # deal with non-zero exits on > 16-bit systems.
207 #
208 # caller can trap the signals.
209  
210 3132 ▶ if( $? )
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 3132 3132
211 {
212 # bad news, boss...
213  
214 my $message
215 = do
216 0 {
217 0 ▶ if( my $exit = $? >> 8 )
Branch: 2 branches
BranchTrueFalseTotal
if (my $exit = $? >> 8) { } 0 0 0
elsif (my $signal = $? & 255) { } 0 0 0
218 {
219 0 "exit( $exit ) by $pid"
220 }
221 elsif( my $signal = $? & 0xFF )
222 {
223 0 "kill SIG-$signal on $pid"
224 }
225 };
226  
227 0 ▶ $finish
Branch: 1 branch
BranchTrueFalseTotal
$finish ? : 0 0 0
228 ? warn $message
229 : die $message
230 ;
231 }
232  
233 # kick off another job if the queue is not empty.
234  
235 3132 ▶ @_ and &fork_job;
Branch: 1 branch
BranchTrueFalseTotal
@_ and &fork_job 2942 190 3132
236 }
237  
238 return
239 124 };
240  
241 # debug or zero count run the jobs without forking,
242 # simplifies most debugging issues.
243  
244 sub runqueue
245 {
246 217 ▶ my ( $count ) = @_;
Subroutine runqueue: called CC=5 SCAR=16.2
Pod undocumented
247  
248 217 ▶ looks_like_number $count
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($count) or croak("Bogus runqueue: '${count}' non-numeric") 0 217 217
249 or croak "Bogus runqueue: '$count' non-numeric";
250  
251 217 ▶ $count < 0
Branch: 1 branch
BranchTrueFalseTotal
$count < 0 and croak("Bogus runqueue: negative count ($count)") 0 217 217
252 and croak "Bogus runqueue: negative count ($count)";
253  
254 $fork && $count
255 204 ? eval { &fork_queue }
256 217 ▶ : eval { &run_nofork }
Branch: 1 branch
BranchTrueFalseTotal
$fork && $count ? : 204 13 217
Condition100%3/3
$fork and $count
!ll&&!rl&&r
112204
MC/DC100%2/2
$fork and $count
$fork $count
Truth table100%3/3
A and B
A= $forkB= $count
ABresult
0X0
100
111
257 ;
258  
259 # return the unused portion.
260 # this includes any incomplete iterators.
261  
262 @_
263 137 }
264  
265 sub import
266 {
267 # discard the current package name and deal
268 # with the args. empty arg for 'export'
269 # indicates that runqueue needs to be exported.
270  
271 90 ▶ my $caller = caller;
Subroutine import: called CC=16 SCAR=28.1
272  
273 90 shift;
274  
275 90 ▶ @_ or unshift @_, qw( export );
Branch: 1 branch
BranchTrueFalseTotal
@_ or unshift @_, "export" 2 88 90
276  
277 90 my $export = 1;
278 90 my $subname = 'runqueue';
279  
280 90 $fork = ! $^P;
281 90 $verbose = '';
282 90 $finish = '';
283  
284 90 for my $arg ( @_ )
285 {
286 90 my( $name, $value ) = split /=/, $arg;
287  
288 90 ▶ $value //= 1;
Condition100%2/2
$value //= 1
l!l
189
MC/DC100%1/1
$value //= 1
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
289  
290 90 ▶ $value = ! $value
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ s/^no//u 2 88 90
291 if $name =~ s/^no//;
292  
293 90 ▶ if( 'fork' eq $name )
Branch: 5 branches
BranchTrueFalseTotal
if ('fork' eq $name) { } 85 5 90
elsif ('verbose' eq $name) { } 0 5 5
elsif ('finish' eq $name) { } 1 4 5
elsif ('debug' eq $name) { } 1 3 4
elsif ('export' eq $name) { } 3 0 3
294 {
295 85 $fork = $value;
296 }
297 elsif( 'verbose' eq $name )
298 {
299 0 $verbose = $value;
300 }
301 elsif( 'finish' eq $name )
302 {
303 1 $finish = $value;
304 }
305 elsif( 'debug' eq $name )
306 {
307 1 ▶ if( $value )
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 1 0 1
308 {
309 1 $fork = '';
310 1 $verbose = 1;
311 }
312 }
313 elsif( 'export' eq $name )
314 {
315 3 $export = !! $value;
316  
317 3 ▶ looks_like_number $value
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($value) or $subname = $value 1 2 3
318 or $subname = $value;
319 }
320 else
321 {
322 0 warn "Unknown argument: '$arg' ignored";
323 }
324 }
325  
326 90 ▶ if( $fork && $^P && ! $DB::fork_TTY )
Branch: 1 branch
BranchTrueFalseTotal
if ($fork and $^P and not $DB::fork_TTY) 83 7 90
Condition66%4/6
$fork and $^P and not $DB::fork_TTY
!ll&&!rl&&r
7083
$fork and $^P
!ll&&!rl&&r
7083
MC/DC33%1/3
$fork and $^P and not $DB::fork_TTY
$fork $^P not $DB::fork_TTY
Truth table50%2/4
A and B and not(C)
A= $forkB= $^PC= not $DB::fork_TTY
ABCresult
0XX0
10X0
1100
1111
327 {
328 83 say STDERR
329 'Debugger forking without $DB::fork_TTY; expect problems';
330 }
331  
332 90 ▶ if( $export )
Branch: 1 branch
BranchTrueFalseTotal
if ($export) 90 0 90
333 {
334 90 my $ref = qualify_to_ref $subname, $caller;
335  
336 90 undef &{ *$ref };
337  
338 90 *$ref = \&runqueue
339 }
340  
341 return
342 90 }
343  
344 sub configure
345 {
346 0 ▶ @_ and import @_, qw( noexport );
Subroutine configure: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and import(@_, "noexport") 0 0 0
347 }
348  
349 # keep require happy
350  
351 90 1
352  
353 __END__
↑ Summary