blib/lib/Parallel/PreForkManager.pm

filter: Statementstmt 89.5% 171 / 191 Branchbran 73.6% 53 / 72 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 94.7% 18 / 19 Podpod 100% 10 / 10 totaltotal 83.2% 259 / 311 CC 45 SCAR 23.3
CC
45
Cov
84%
RunJobs
19.0
Child
14.9
new
9.7
CRAP 10.3
↑ Summary
1 package Parallel::PreForkManager;
2  
3 224 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 224 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 224 our $VERSION = '1.20170415'; # VERSION
7  
8 224 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 224 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 224 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 224 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 224 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 224 my $DEBUG = 0;
15  
16 sub new {
17 222 ▶ my ( $Class, $Args ) = @_;
Subroutine new: called CC=8 SCAR=22.8
Pod documented
18  
19 222 ▶ croak "No ChildHandler set" if ! exists ( $Args->{'ChildHandler'} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Args->{"ChildHandler"} 0 222 222
20  
21 my $Self = {
22 'ChildHandler' => $Args->{'ChildHandler'},
23 'ChildCount' => $Args->{'ChildCount'} || 10,
24 'Timeout' => $Args->{'Timeout'} || 0,
25 222 ▶ 'WaitComplete' => $Args->{'WaitComplete'} || 1,
Condition50%3/6
$Args->{'ChildCount'} || 10
l!l
2220
$Args->{'Timeout'} || 0
l!l
0222
$Args->{'WaitComplete'} || 1
l!l
0222
MC/DC0%0/1
$Args->{'ChildCount'} || 10
$Args->{'ChildCount'}
MC/DC0%0/1
$Args->{'Timeout'} || 0
$Args->{'Timeout'}
MC/DC0%0/1
$Args->{'WaitComplete'} || 1
$Args->{'WaitComplete'}
Truth table50%1/2
A
A= $Args->{'ChildCount'}
Aresult
11
00
Truth table50%1/2
A
A= $Args->{'Timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $Args->{'WaitComplete'}
Aresult
11
00
26 'JobQueue' => [],
27 'Select' => IO::Select->new(),
28 };
29  
30 222 foreach my $Arg ( qw { ParentCallback ProgressCallback JobsPerChild ChildSetupHook ChildTeardownHook } ) {
31 1110 ▶ $Self->{ $Arg } = $Args->{ $Arg } if exists ( $Args->{ $Arg } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $Args->{$Arg} 655 455 1110
32 }
33  
34 222 ▶ bless $Self, ref($Class) || $Class;
Condition33%1/3
ref $Class || $Class
l!l&&r!l&&!r
02220
MC/DC0%0/2
ref $Class || $Class
ref $Class $Class
Truth table33%1/3
A || B
A= ref $ClassB= $Class
ABresult
1X1
011
000
35  
36 222 return $Self;
37 }
38  
39 sub AddJob {
40 40590 ▶ my ( $Self, $Job ) = @_;
Subroutine AddJob: called CC=1 SCAR=0.0
Pod documented
41 40590 push @{ $Self->{'JobQueue'} }, $Job;
42 40590 return;
43 }
44  
45 sub RunJobs {
46 222 ▶ my ($Self) = @_;
Subroutine RunJobs: called CC=17 SCAR=29.5
Pod documented
47  
48 # If a worker dies, there's a problem
49 local $SIG{CHLD} = sub {
50 5568 ▶ my $pid = wait();
Subroutine __ANON__: called CC=2 SCAR=8.1
51 5568 ▶ if ( exists ( $Self->{'ToChild'}->{$pid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"ToChild"}{$pid}) 0 5568 5568
52 0 confess("Worker $pid died.");
53 }
54 222 };
55  
56 # Start the workers
57 222 $Self->StartChildren();
58  
59 # Read from the workers, loop until they all shut down
60 190 ▶ while ( %{ $Self->{'ToChild'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$Self->{"ToChild"};}) 190 3 193
61 READYLOOP:
62 190 ▶ while ( my @Ready = $Self->{'Select'}->can_read() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@Ready) = $Self->{"Select"}->can_read) 8617 3 8620
63 READLOOP:
64 8617 foreach my $fh (@Ready) {
65 50229 my $Result = $Self->Receive($fh);
66  
67 50229 ▶ if ( !$Result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Result) 0 50229 50229
68 0 $Self->{'Select'}->remove($fh);
69 0 print STDERR "$fh got eof\n";
70 0 next READLOOP;
71 }
72  
73 50229 my $ResultMethod = $Result->{ 'Method' };
74 50229 ▶ warn "Parent working on Method $ResultMethod\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 50229 50229
75  
76 # Handle the initial request for work
77 50229 ▶ if ( $ResultMethod eq 'Startup' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ResultMethod eq "Startup") 17127 33102 50229
78 17127 ▶ if ( $#{ $Self->{'JobQueue'} } > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$Self{'JobQueue'};} > -1) { } 17127 0 17127
79 #my $Child = $Self->{ 'ToChild' }->{ $Result->{ 'pid' } };
80 17127 my $NextJob = shift( @{ $Self->{'JobQueue'} } );
81 17127 $Self->Send( $Self->{'ToChild'}->{ $Result->{'pid'} }, { 'Job' => $NextJob, }, );
82 17127 next READLOOP;
83 }
84 else {
85 # Nothing to do, shut down
86 0 $Self->{'Select'}->remove($fh);
87 0 my $fh = $Self->{'ToChild'}->{ $Result->{'pid'} };
88 0 delete( $Self->{'ToChild'}->{ $Result->{'pid'} } );
89 0 $Self->Send( $fh, { 'Shutdown' => 1, }, );
90 0 close($fh);
91 }
92 }
93  
94 # Process the result handler
95 33102 ▶ if ( $ResultMethod eq 'Completed' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ResultMethod eq "Completed") 16361 16741 33102
96 # The child has completed it's work, process the results.
97 16361 ▶ if ( $Result->{'Data'} && exists( $Self->{'ParentCallback'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Result->{"Data"} and exists $Self->{"ParentCallback"}) 16311 50 16361
Condition66%2/3
$Result->{"Data"} and exists $Self->{"ParentCallback"}
!ll&&!rl&&r
05016311
MC/DC50%1/2
$Result->{"Data"} and exists $Self->{"ParentCallback"}
$Result->{"Data"} exists $Self->{"ParentCallback"}
Truth table66%2/3
A and B
A= $Result->{"Data"}B= exists $Self->{"ParentCallback"}
ABresult
0X0
100
111
98 16311 &{ $Self->{'ParentCallback'} }( $Self, $Result->{'Data'} );
99 }
100  
101 # If the child has reached its processing limit then shut it down
102 16361 ▶ if ( exists( $Result->{'JobsPerChildLimitReached'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Result->{"JobsPerChildLimitReached"}) 16052 309 16361
103 16052 $Self->{'Select'}->remove($fh);
104 16052 my $fh = $Self->{'ToChild'}->{ $Result->{'pid'} };
105 16052 delete( $Self->{'ToChild'}->{ $Result->{'pid'} } );
106 16052 $Self->Send( $fh, { 'Shutdown' => 1, }, );
107 16052 close($fh);
108 # If there are still jobs to be done then start a new child
109 16052 ▶ if ( $#{ $Self->{'JobQueue'} } > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$Self{"JobQueue"};} > -1) 16052 0 16052
110 16052 $Self->StartChild();
111 }
112 15865 next READLOOP;
113 }
114  
115 # If there's still work to be done, send it to the child
116 309 ▶ if ( $#{ $Self->{'JobQueue'} } > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$Self{"JobQueue"};} > -1) 287 22 309
117 287 my $NextJob = shift( @{ $Self->{'JobQueue'} } );
118 287 $Self->Send( $Self->{'ToChild'}->{ $Result->{'pid'} }, { 'Job' => $NextJob, }, );
119 287 next READLOOP;
120 }
121  
122 # There is no more work to be done, shut down this child
123 22 $Self->{'Select'}->remove($fh);
124 22 my $fh = $Self->{'ToChild'}->{ $Result->{pid} };
125 22 delete( $Self->{'ToChild'}->{ $Result->{pid} } );
126 22 close($fh);
127 22 next READLOOP;
128 }
129  
130 16741 ▶ if ( $ResultMethod eq 'ProgressCallback' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ResultMethod eq "ProgressCallback") 16741 0 16741
131 16741 my $Method = $Result->{'ProgressCallbackMethod'};
132 16741 my $Data = $Result->{'ProgressCallbackData'};
133 16741 ▶ if ( exists( $Self->{'ProgressCallback'}->{$Method} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{'ProgressCallback'}{$Method}) { } 16741 0 16741
134 16741 my $MethodResult = &{ $Self->{'ProgressCallback'}->{$Method} }( $Self, $Data );
135 16741 $Self->Send( $Self->{'ToChild'}->{ $Result->{'pid'} }, $MethodResult );
136  
137 }
138 else {
139 0 confess "Unknown callback method";
140 }
141  
142 16741 next READLOOP;
143 }
144  
145 }
146 }
147 }
148  
149 3 ▶ if ( $Self->{ 'WaitComplete' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{"WaitComplete"}) 3 0 3
150 3 $Self->WaitComplete();
151 }
152  
153 3 return;
154 }
155  
156 sub WaitComplete {
157 3 ▶ my ( $Self ) = @_;
Subroutine WaitComplete: called CC=2 SCAR=6.9
Pod documented
158 3 ▶ while ( ( my $pid = wait() ) != -1 ) { }
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pid = wait) != -1) 4 3 7
159 3 return;
160 }
161  
162 sub StartChildren {
163 222 ▶ my ($Self) = @_;
Subroutine StartChildren: called CC=3 SCAR=11.2
Pod documented
164  
165 222 my $MaxChildren = $Self->{ 'ChildCount' };
166 222 my $ActualJobs = scalar @{ $Self->{ 'JobQueue' } };
167  
168 222 ▶ if ( $ActualJobs < $MaxChildren ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ActualJobs < $MaxChildren) 0 222 222
169 0 $MaxChildren = $ActualJobs;
170 }
171  
172 222 foreach ( 1 .. $MaxChildren ) {
173 1988 $Self->StartChild();
174 }
175  
176 190 return;
177 }
178  
179 sub StartChild {
180 18040 ▶ my ($Self) = @_;
Subroutine StartChild: called CC=4 SCAR=14.0
Pod documented
181  
182 # Open a pipe for the worker
183 18040 my ( $FromParent, $FromChild, $ToParent, $ToChild );
184 18040 pipe( $FromParent, $ToChild );
185 18040 pipe( $FromChild, $ToParent );
186  
187 # Fork off a worker
188 18040 my $pid = fork();
189  
190 18040 ▶ if ($pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid) { } 17821 219 18040
elsif ($pid == 0) { } 219 0 219
191 # Parent
192  
193 # Close unused pipes
194 17821 close($ToParent);
195 17821 close($FromParent);
196  
197 17821 $Self->{'ToChild'}->{$pid} = $ToChild;
198 17821 $Self->{'FromChild'}->{$pid} = $FromChild;
199 17821 $Self->{'Select'}->add($FromChild);
200  
201 }
202 elsif ( $pid == 0 ) {
203 # Child
204  
205 219 ▶ warn "Child $PID spawned" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 219 219
206  
207 # Close unused pipes
208 219 close($FromChild);
209 219 close($ToChild);
210  
211 # Setup communication pipes
212 219 $Self->{'ToParent'} = $ToParent;
213 219 open( STDIN, '<', '/dev/null' );
214  
215 # Send the initial request
216 219 $Self->Send( $ToParent, { 'Method' => 'Startup' } );
217  
218 # Start processing
219 219 $Self->Child($FromParent);
220  
221 # When the worker subroutine completes, exit
222 0 exit 0;
223 }
224 else {
225 0 confess("Failed to fork: $!");
226 }
227  
228 17821 return;
229 }
230  
231 sub Child {
232 219 ▶ my ( $Self, $FromParent ) = @_;
Subroutine Child: called CC=13 SCAR=27.0
Pod documented
233 219 $Self->{'FromParent'} = $FromParent;
234  
235 219 ▶ if ( exists( $Self->{'ChildSetupHook'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"ChildSetupHook"}) 10 209 219
236 10 &{ $Self->{'ChildSetupHook'} }( $Self );
237 }
238  
239 # Read instructions from the parent
240 219 ▶ while ( my $Instructions = $Self->Receive($FromParent) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Instructions = $Self->Receive($FromParent)) 641 31 672
241  
242 # If the handler's children die, that's not our business
243 641 $SIG{CHLD} = 'IGNORE';
244  
245 641 ▶ if ( exists( $Instructions->{'Shutdown'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Instructions->{"Shutdown"}) 188 453 641
246 188 ▶ warn "Child $PID shutdown" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 188 188
247 188 ▶ if ( exists( $Self->{'ChildTeardownHook'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"ChildTeardownHook"}) 0 188 188
248 0 &{ $Self->{'ChildTeardownHook'} }( $Self );
249 }
250 188 exit 0;
251 }
252  
253 # Execute the handler with the given instructions
254 453 my $Result;
255 453 eval {
256  
257 # Handle alarms
258 local $SIG{ALRM} = sub {
259 0 ▶ die "Child timed out.";
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 453 };
261  
262 # Set alarm
263 453 alarm( $Self->{'Timeout'} );
264  
265 # Execute the handler and get it's result
266 453 ▶ if ( exists( $Self->{'ChildHandler'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"ChildHandler"}) 453 0 453
267 453 $Result = &{ $Self->{'ChildHandler'} }( $Self, $Instructions->{'Job'} );
268 }
269  
270 # Disable alarm
271 453 alarm(0);
272  
273 };
274  
275 # Warn on errors
276 453 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 453 453
277 0 ▶ if ( exists( $Self->{'ChildTeardownHook'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"ChildTeardownHook"}) 0 0 0
278 0 &{ $Self->{'ChildTeardownHook'} }( $Self );
279 }
280 0 croak("Child $PID error: $@");
281 }
282  
283 453 my $ResultToParent = {
284 'Method' => 'Completed',
285 'Data' => $Result,
286 };
287  
288 453 ▶ if ( exists( $Self->{'JobsPerChild'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"JobsPerChild"}) 203 250 453
289 203 $Self->{'JobsPerChild'} = $Self->{'JobsPerChild'} - 1;
290 203 ▶ if ( $Self->{'JobsPerChild'} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{"JobsPerChild"} == 0) 192 11 203
291 192 $ResultToParent->{'JobsPerChildLimitReached'} = 1;
292 }
293 }
294  
295 # Send the result to the server
296 453 $Self->Send( $Self->{'ToParent'}, $ResultToParent );
297 }
298  
299 31 ▶ if ( exists( $Self->{'ChildTeardownHook'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Self->{"ChildTeardownHook"}) 10 21 31
300 10 &{ $Self->{'ChildTeardownHook'} }( $Self );
301 }
302  
303 31 ▶ warn "Child $PID completed" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 31 31
304 31 exit 0;
305 }
306  
307 sub ProgressCallback {
308 403 ▶ my ( $Self, $Method, $Data ) = @_;
Subroutine ProgressCallback: called CC=1 SCAR=0.0
Pod documented
309 403 $Self->Send( $Self->{'ToParent'}, {
310 'Method' => 'ProgressCallback',
311 'ProgressCallbackMethod' => $Method,
312 'ProgressCallbackData' => $Data,
313 } );
314 403 my $Result = $Self->Receive( $Self->{'FromParent'} );
315 403 return $Result;
316 }
317  
318 sub Receive {
319 51304 ▶ my ( $Self, $fh ) = @_;
Subroutine Receive: called CC=3 SCAR=11.0
Pod documented
320  
321 # Get a value from the file handle
322 51304 my $Value;
323 my $Char;
324 51304 ▶ while ( read( $fh, $Char, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, $Char, 1) 4242194 33 4242227
325 4242194 ▶ if ( $Char eq "\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Char eq "\n") 51271 4190923 4242194
326 51271 last;
327 }
328 4190923 $Value .= $Char;
329 }
330  
331 # Deserialize the data
332 51304 my $Data = eval { decode_json($Value) };
333  
334 51304 return $Data;
335 }
336  
337 sub Send {
338 51282 ▶ my ( $Self, $fh, $Value ) = @_;
Subroutine Send: called CC=1 SCAR=0.0
Pod documented
339  
340 51282 $Value->{'pid'} = $PID;
341  
342 51282 my $Encoded = encode_json($Value);
343 51282 print $fh "$Encoded\n";
344  
345 # Force the file handle to flush
346 51282 $fh->flush();
347  
348 51282 return;
349 }
350  
351 224 1;
352  
353 __END__
↑ Summary