Queue.pm

filter: Statementstmt 94.5% 121 / 128 Branchbran 80.3% 53 / 66 Conditioncond 83.3% 15 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 87.9% 211 / 240 CC 43 SCAR 20.7
CC
43
Cov
89%
configure
14.1
__ANON__
10.3
__ANON__
8.3
CRAP 7.9
↑ Summary Queue/Manager.pm »
1 ########################################################################
2 # housekeeping
3 ########################################################################
4  
5 package Parallel::Queue v4.0.2;
6 249 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 249 ▶use mro qw( c3 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 249 ▶use mro::EVERY;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 249 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 249 ▶use Symbol qw( qualify_to_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use Scalar::Util
15 249 ▶qw
Subroutine BEGIN: called CC=1 SCAR=0.0
16 249 (
17 blessed
18 reftype
19 looks_like_number
20 249 );
21  
22 ########################################################################
23 # package variables
24 ########################################################################
25  
26 249 our @CARP_NOT = ( __PACKAGE__, qw( mro mro::EVERY ) );
27  
28 # config state
29  
30 249 my %defaultz =
31 (
32 fork => ! $^P
33 , qw
34 (
35 export runqueue
36 finish 0
37 debug 0
38 verbose 0
39 )
40 );
41  
42 249 my %argz = %defaultz;
43  
44 ########################################################################
45 # utility subs
46 ########################################################################
47  
48 my $format
49 = sub
50 2029 ▶{
Subroutine __ANON__: called CC=2 SCAR=6.9
51 state $dumper
52 = do
53 2029 {
54 55 require Data::Dumper;
55 55 Data::Dumper->can( 'Dumper' )
56 };
57  
58 2029 local $Data::Dumper::Terse = 1;
59 2029 local $Data::Dumper::Indent = 1;
60 2029 local $Data::Dumper::Sortkeys = 1;
61  
62 2029 local $Data::Dumper::Purity = 0;
63 2029 local $Data::Dumper::Deepcopy = 0;
64 2029 local $Data::Dumper::Quotekeys = 0;
65  
66 2029 my $head = shift;
67  
68 say join "\n" =>
69 map
70 {
71 2029 ▶ ref $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 316 2029 2345
72 ? $_->$dumper
73 : $_
74 }
75 ( "($$) $head" => @_ );
76  
77 2029 return;
78 249 };
79  
80 249 ▶my $stub = sub{};
Subroutine __ANON__: called CC=1 SCAR=0.0
81 249 my $debug = $stub;
82  
83 ########################################################################
84 # execution handlers
85 ########################################################################
86  
87 my $next_job
88 = sub
89 25468 ▶{
Subroutine __ANON__: called CC=9 SCAR=23.3
90 25468 state $redo = __SUB__;
91 25468 state $object = '';
92  
93 25468 my $job = '';
94  
95 25468 ▶ if( $object )
Branch: 1 branch
BranchTrueFalseTotal
if ($object) 20417 5051 25468
96 {
97 # objects may have their own internal stack.
98 # if there aren't any arguments then pass
99 # nothing rather than undef from shift.
100  
101 20417 ▶ if( my $job = $object->next_job )
Branch: 1 branch
BranchTrueFalseTotal
if (my $job = $object->next_job) { } 19513 904 20417
102 {
103 19513 return $job;
104 }
105 else
106 {
107 904 $object = '';
108 }
109 }
110  
111 5955 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 4679 1276 5955
112
113 # silently ignore empty slots.
114  
115 4679 ▶ my $next = shift
Branch: 1 branch
BranchTrueFalseTotal
my $next = shift() or goto *$redo 4679 0 4679
116 or goto *$redo;
117  
118 4679 my $class = blessed $next;
119  
120 4679 ▶ if( $class && $class->can( 'next_job' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($class and $class->can('next_job')) { } 1079 3600 4679
elsif ('CODE' eq builtin::reftype($next)) { } 3600 0 3600
Condition66%2/3
$class and $class->can("next_job")
!ll&&!rl&&r
360001079
MC/DC50%1/2
$class and $class->can("next_job")
$class $class->can("next_job")
Truth table66%2/3
A and B
A= $classB= $class->can("next_job")
ABresult
0X0
100
111
121 {
122 1079 $debug->( "New iterator: '$class'" );
123  
124 1079 $object = $next;
125 1079 goto &$redo
126 }
127 elsif( 'CODE' eq reftype $next )
128 {
129 3600 $next
130 }
131 else
132 {
133 0 my $nastygram
134 = $format->( 'Bothched queue: un-blessed, non-coderef', $_[0] );
135  
136 $argz{ finish }
137 0 ▶ or croak $nastygram;
Branch: 1 branch
BranchTrueFalseTotal
$argz{"finish"} or croak($nastygram) 0 0 0
138  
139 0 say STDERR $nastygram;
140 0 goto &$redo
141 }
142 249 };
143  
144 my $run_nofork
145 = sub
146 977 ▶{
Subroutine __ANON__: called CC=4 SCAR=13.9
147 # discard the count, iterate the queue without forking.
148 977 shift;
149  
150 977 $debug->( 'Non-forking queue' );
151  
152 977 ▶ while( my $sub = &$next_job )
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = &$next_job) 16602 928 17530
153 {
154 # these should all exit zero.
155  
156 my $exit
157 = eval
158 16602 {
159 16602 ▶ $sub->()
Condition100%2/2
&$sub() || (next)
l!l
9416508
MC/DC100%1/1
&$sub() || (next)
&$sub()
Truth table100%2/2
A
A= &$sub()
Aresult
11
00
160 or next
161 };
162  
163 94 say STDERR "\nNon-zero exit: $exit, $@\n";
164  
165 94 ▶ if( $argz{ finish } )
Branch: 1 branch
BranchTrueFalseTotal
if ($argz{'finish'}) { } 45 49 94
166 {
167 45 say 'Non-zero exit: Continuing queue.';
168 }
169 else
170 {
171 49 say 'Non-zero exit: Aborting queue.';
172 49 last;
173 }
174 }
175  
176 return
177 977 };
178  
179 my $fork_job
180 = sub
181 6859 ▶{
Subroutine __ANON__: called CC=6 SCAR=18.2
182 # don't check @_: next_job may have an object
183 # returning next jobs w/ an empty queue.
184  
185 6859 ▶ my $job = &$next_job
Branch: 1 branch
BranchTrueFalseTotal
my $job = &$next_job or return 6511 348 6859
186 or return;
187  
188 6511 ▶ if( ( my $pid = fork ) > 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ((my $pid = fork) > 0) { } 6277 234 6511
elsif (defined $pid) { } 234 0 234
189 {
190 6277 $debug->( "fork: $pid" );
191 return
192 6277 }
193 elsif( defined $pid )
194 {
195 # child passes the exit status of the perl sub call
196 # to the caller as our exit status. the O/S will deal
197 # with signal values.
198 #
199 # aside: failing to exit here will cause runaway
200 # phorkatosis.
201  
202 234 $debug->( "\tExecuting: '$job'" );
203  
204 234 ▶ my $exitval = eval { $job->() } || 0;
Condition100%2/2
eval { do { &$job() } } || 0
l!l
4230
MC/DC100%1/1
eval { do { &$job() } } || 0
eval { do { &$job() } }
Truth table100%2/2
A
A= eval { do { &$job() } }
Aresult
11
00
205  
206 # either way, this process needs to exit.
207  
208 234 ▶ $@
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 234 234
209 ? die
210 : exit $exitval
211 }
212 else
213 {
214 # pass back the fork failure for the caller to deal with.
215  
216 0 die "Phorkafobia: $!";
217 }
218 249 };
219  
220 my $fork_queue
221 = sub
222 489 ▶{
Subroutine __ANON__: called CC=8 SCAR=21.2
223 # count was validated in runqueue.
224  
225 489 my $count = shift;
226  
227 # what's on the stack?
228 # the jobs to run!
229 # which may be none.
230 # if so, we're done.
231  
232 489 $debug->( "Forking initial $count jobs." );
233  
234 489 &$fork_job for 1 .. $count;
235  
236 441 $debug->( "Processing remainder of queue." );
237  
238 441 my $reap_only = '';
239  
240 441 ▶ while( ( my $pid = wait ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pid = wait) > 0) 6012 255 6267
241 {
242 6012 $debug->( "Complete: $pid ($?)" );
243  
244 6012 ▶ if( $? )
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 18 5994 6012
245 {
246 # this assumes normal *NIX 16-bit exit values,
247 # with a status in the high byte and signum
248 # in the lower. notice that $status is not
249 # masked to 8 bits, however. this allows us to
250 # deal with non-zero exits on > 16-bit systems.
251 #
252 # caller can trap the signals.
253  
254 my $failure
255 = do
256 18 {
257 18 ▶ if( my $exit = $? >> 8 )
Branch: 2 branches
BranchTrueFalseTotal
if (my $exit = $? >> 8) { } 18 0 18
elsif (my $signal = $? & 255) { } 0 0 0
258 {
259 18 "exit( $exit ) by $pid"
260 }
261 elsif( my $signal = $? & 0xFF )
262 {
263 0 "kill SIG-$signal on $pid"
264 }
265 else
266 {
267 0 "coredump by $pid"
268 }
269 };
270  
271 my $result
272 = ( $reap_only = ! $argz{ finish } )
273 18 ▶ ? "Non-zero exit: Reaping only to complete queue."
Branch: 1 branch
BranchTrueFalseTotal
($reap_only = !$argz{'finish'}) ? : 8 10 18
274 : "Non-zero exit: Continuing queue."
275 ;
276  
277 18 say STDERR "\n$failure\n$result\n";
278 }
279  
280 $reap_only
281 6012 ▶ or &$fork_job
Branch: 1 branch
BranchTrueFalseTotal
$reap_only or &$fork_job 9 6003 6012
282 }
283  
284 return
285 255 };
286  
287 # debug or zero count run the jobs without forking,
288 # simplifies most debugging issues.
289  
290 sub runqueue
291 1466 ▶{
Subroutine runqueue: called CC=5 SCAR=16.2
Pod undocumented
292 1466 my $count = $_[0];
293  
294 1466 ▶ looks_like_number $count
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($count) or croak("Bogus runqueue: '${count}' non-numeric") 1466 0 1466
295 or croak "Bogus runqueue: '$count' non-numeric";
296  
297 1466 ▶ $count < 0
Branch: 1 branch
BranchTrueFalseTotal
$count < 0 and croak("Bogus runqueue: negative count ($count)") 0 1466 1466
298 and croak "Bogus runqueue: negative count ($count)";
299  
300 1466 ▶ $argz{ fork } && $count
Branch: 1 branch
BranchTrueFalseTotal
$argz{'fork'} && $count ? : 489 977 1466
Condition100%3/3
$argz{"fork"} and $count
!ll&&!rl&&r
735242489
MC/DC100%2/2
$argz{"fork"} and $count
$argz{"fork"} $count
Truth table100%3/3
A and B
A= $argz{"fork"}B= $count
ABresult
0X0
100
111
301 ? &$fork_queue
302 : &$run_nofork
303 ;
304  
305 # return the unused portion.
306 # this includes any incomplete iterators.
307  
308 @_
309 1232 }
310  
311 sub configure
312 1706 ▶{
Subroutine configure: called CC=14 SCAR=26.5
Pod undocumented
313 # discard the current patckage
314 1706 ▶ shift if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 1206 500 1706
315  
316 %argz
317 = map
318 {
319 1706 my ( $arg, $val ) = split /=/, $_, 2;
320  
321 2490 ▶ $val //= 1;
Condition100%2/2
$val //= 1
l!l
12489
MC/DC100%1/1
$val //= 1
$val
Truth table100%2/2
A
A= $val
Aresult
11
00
322  
323 2490 ▶ $val = !$val
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^ no//ux 1380 1110 2490
324 if $arg =~ s{^ no}{}x;
325  
326 2490 ( $arg => $val )
327 }
328 @_;
329  
330 @argz{ qw( fork verbose ) } = ( '', 1 )
331 1706 ▶ if delete $argz{ debug };
Branch: 1 branch
BranchTrueFalseTotal
if delete $argz{"debug"} 1 1705 1706
332  
333 1706 for( $argz{ export } )
334 {
335 # numeric for true gets default name.
336 # name for true gets whatever's there.
337  
338 1706 ▶ $_ or next;
Branch: 1 branch
BranchTrueFalseTotal
$_ or next 1 1705 1706
339  
340 # default does the right thing.
341  
342 looks_like_number $_
343 1 ▶ and delete $argz{ export };
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) and delete $argz{"export"} 0 1 1
344  
345 1 ▶ m{ \W }x
Branch: 1 branch
BranchTrueFalseTotal
/ \W /ux and croak("Botched export: '${_}' contains non-word chars.") 0 1 1
346 and croak "Botched export: '$_' contains non-word chars.";
347 }
348  
349 1706 ▶ while( my($k,$v) = each %defaultz )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %defaultz) 8530 1706 10236
350 {
351 8530 ▶ $argz{ $k } //= $v;
Condition66%2/3
$argz{$k} //= $v
l!l&&r!l&&!r
249160390
MC/DC0%0/2
$argz{$k} //= $v
$argz{$k} $v
Truth table66%2/3
A //= B
A= $argz{$k}B= $v
ABresult
1X1
011
000
352 }
353  
354 $debug
355 = $argz{ verbose }
356 1706 ▶ ? $format
Branch: 1 branch
BranchTrueFalseTotal
$argz{'verbose'} ? : 316 1390 1706
357 : $stub
358 ;
359  
360 1706 $debug->( 'Configuration:', \%argz );
361  
362 1706 ▶ if( $argz{ fork } && $^P )
Branch: 1 branch
BranchTrueFalseTotal
if ($argz{"fork"} and $^P) 731 975 1706
Condition66%2/3
$argz{"fork"} and $^P
!ll&&!rl&&r
9750731
MC/DC50%1/2
$argz{"fork"} and $^P
$argz{"fork"} $^P
Truth table66%2/3
A and B
A= $argz{"fork"}B= $^P
ABresult
0X0
100
111
363 {
364 731 say STDERR
365 'Debugger forking. Check TERM=xterm or $DB::debug_TTY.';
366 }
367  
368 return
369 1706 }
370  
371 sub import
372 249 ▶{
Subroutine import: called CC=2 SCAR=6.9
373 249 &configure;
374  
375 249 ▶ if( my $export = $argz{ export } )
Branch: 1 branch
BranchTrueFalseTotal
if (my $export = $argz{"export"}) 164 85 249
376 {
377 164 my $caller = caller;
378 164 my $ref = qualify_to_ref $export => $caller;
379  
380 164 undef &{ *$ref };
381  
382 164 $debug->( "Installing $export -> $caller" );
383  
384 164 *$ref = \&runqueue
385 }
386 }
387  
388 # keep require happy
389  
390 249 1
391  
392 __END__
↑ Summary Queue/Manager.pm »