blib/lib/Parallel/ForkControl.pm

filter: Statementstmt 73.1% 226 / 309 Branchbran 50.8% 57 / 112 Conditioncond 33.3% 8 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 84.6% 44 / 52 Podpod 100% 8 / 8 totaltotal 66.2% 345 / 521 CC 72 SCAR 20.7
CC
72
Cov
65%
_overLoad
30.0
_tooManyKids
27.6
run
14.4
CRAP 7.9
↑ Summary
1 #
2 # This is a nonsucking forking module.
3 # Coded by:
4 # Brad Lhotsky <brad@divisionbyzero.net>
5 # Contributions by:
6 # Mark Thomas <mark@ackers.net>
7 #
8 package Parallel::ForkControl;
9 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 ▶use POSIX qw/:signal_h :errno_h :sys_wait_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Storable qw(freeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 our $AUTOLOAD;
18 22 our $VERSION = 0.5;
19  
20 22 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 22 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 22 ▶use constant DEFAULT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 22 ▶use constant PERMISSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 22 ▶use constant PERMIT_ALL => 'init/set/get/copy';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 ##################
28 # Debug constants
29 22 ▶use constant DB_OFF => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 22 ▶use constant DB_INFO => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 22 ▶use constant DB_LOW => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 22 ▶use constant DB_MED => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 22 ▶use constant DB_HIGH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 {
36 # private data members
37 22 my %_attributes = (
38 # Name # defaults # permissions
39 '_name' => [ 'Unnamed Child', PERMIT_ALL ],
40 '_processtimeout' => [ 120, PERMIT_ALL ],
41 '_maxkids' => [ 5, PERMIT_ALL ],
42 '_minkids' => [ 1, PERMIT_ALL ],
43 '_maxload' => [ 4.50, PERMIT_ALL ],
44 '_maxmem' => [ 10.0, PERMIT_ALL ], # non functional
45 '_maxcpu' => [ 25.0, PERMIT_ALL ],
46 '_method' => [ 'cycle', PERMIT_ALL ],
47 '_watchcount' => [ TRUE, PERMIT_ALL ],
48 '_watchload' => [ FALSE, PERMIT_ALL ],
49 '_watchmem' => [ FALSE, PERMIT_ALL ], # non functional
50 '_watchcpu' => [ FALSE, PERMIT_ALL ], # non functional
51 '_parentpid' => [ $$, 'get/set' ],
52 '_code' => [ undef, 'init/get/set' ],
53 '_debug' => [ DB_OFF, 'init/get/set' ],
54 '_check_at' => [ 2, PERMIT_ALL ],
55 '_checked' => [ 0, 'get/init' ],
56 '_accounting' => [ FALSE, PERMIT_ALL ],
57 '_results' => [ undef, 'get' ],
58 '_trackargs' => [ FALSE, PERMIT_ALL ]
59 );
60
61 22 my %_KIDS=();
62 22 my $_KIDS=0;
63 # private member accessors
64 42 ▶ sub _attributes {
Subroutine _attributes: called CC=1 SCAR=0.0
65 # return an array of our attributes
66 42 return keys %_attributes;
67 }
68 693 ▶ sub _default {
Subroutine _default: called CC=2 SCAR=7.0
69 # return the default for a set attribute
70 693 my ($self,$attr) = @_;
71 693 $attr =~ tr/[A-Z]/[a-z]/;
72 693 $attr =~ s/^\s*_?/_/;
73 693 ▶ return unless exists $_attributes{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_attributes{$attr} 0 693 693
74 693 return $_attributes{$attr}->[DEFAULT];
75 }
76 444 ▶ sub _can {
Subroutine _can: called CC=3 SCAR=11.6
77 # return TRUE if we can $perm the $attr
78 444 my ($self,$perm,$attr) = @_;
79 444 $attr =~ tr/[A-Z]/[a-z]/;
80 444 $attr =~ s/^\s*_?/_/;
81 444 ▶ return unless exists $_attributes{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_attributes{$attr} 0 444 444
82 444 $perm =~ tr/[A-Z]/[a-z/;
83 444 ▶ return TRUE if $_attributes{$attr}->[PERMISSION] =~ /$perm/;
Branch: 1 branch
BranchTrueFalseTotal
if $_attributes{$attr}[1] =~ /$perm/ 444 0 444
84 0 return FALSE;
85 }
86 210 ▶ sub _kidstarted {
Subroutine _kidstarted: called CC=2 SCAR=7.0
87 # keep records of our children
88 210 my ($self,$kid,@args) = @_;
89 210 $self->_dbmsg(DB_LOW,"CHILD: $kid STARTING");
90 #
91 # use time() here to implement the process time out
92 210 $_KIDS{$kid} = time;
93  
94 210 ▶ $self->{_results}{$kid} = {
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_accounting 210 0 210
95 status => 'running',
96 signature => undef,
97 result => undef,
98 exitcode => undef,
99 error => undef
100 } if $self->get_accounting();
101  
102 210 $self->_kid_signature($kid,@args);
103 #
104 # increment the KIDS cntr.
105 210 $_KIDS++;
106  
107 #
108 # return the pid!
109 210 return $kid;
110 }
111 149 ▶ sub _kidstopped {
Subroutine _kidstopped: called CC=4 SCAR=14.1
112 # keep track
113 149 my ($self,$kid,$err) = @_;
114 149 $self->_dbmsg(DB_HIGH, "KIDSTOPPED: $kid");
115 149 $self->_dbmsg(DB_HIGH, "KIDS: $_KIDS, (" . join(',',keys %_KIDS) . ")");
116 149 ▶ return unless exists $_KIDS{$kid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_KIDS{$kid} 0 149 149
117 149 ▶ if($self->get_accounting()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_accounting) 149 0 149
118 149 $self->{_results}{$kid}{status} = 'terminated';
119 149 ▶ $self->_kid_err($kid,$err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 149 149
120 }
121 149 $self->_dbmsg(DB_LOW,"CHILD: $kid ENDING");
122 149 delete $_KIDS{$kid};
123 149 return --$_KIDS;
124 }
125 369 ▶ sub kids {
Subroutine kids: called CC=2 SCAR=6.9
Pod documented
126 369 ▶ return wantarray() ? keys %_KIDS : $_KIDS;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 115 254 369
127 }
128 439 ▶ sub kid_time {
Subroutine kid_time: called CC=2 SCAR=7.1
Pod documented
129 439 my ($self,$kid) = @_;
130 439 ▶ return time unless exists $_KIDS{$kid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_KIDS{$kid} 0 439 439
131 439 return $_KIDS{$kid};
132 }
133 0 ▶ sub _pid {
Subroutine _pid: not called CC=1 SCAR=6.9
134 0 return $$;
135 }
136  
137 #
138 # Child Accounting
139 6 ▶ sub clear_results {
Subroutine clear_results: called CC=3 SCAR=11.4
Pod documented
140 6 my ($self) = @_;
141 6 ▶ return unless $self->get_accounting();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting 0 6 6
142 6 ▶ delete $self->{_results} if exists $self->{_results};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_results"} 6 0 6
143 6 $self->{_chi}->clear();
144 }
145  
146 # Always return copies
147 4 ▶ sub get_results {
Subroutine get_results: called CC=6 SCAR=24.6
Pod documented
148 4 my ($self,$key) = @_;
149 4 ▶ return unless $self->get_accounting();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting 0 4 4
150 4 ▶ if( !exists $self->{_results} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_results"}) 0 4 4
151 0 warn "something successfully did not happen\n";
152 0 return;
153 }
154 4 ▶ if( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 4 0 4
155 4 ▶ if( $self->{_results}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_results'}{$key}) { } 4 0 4
156 return {
157 4 %{ $self->{_results}{$key} },
158 4 'return' => $self->{_chi}->get($key),
159 };
160 }
161 else {
162 0 warn "attempt to access undefined child '$key'\n";
163 0 return;
164 }
165 }
166 else {
167 0 my %local_copy = %{ $self->{_results} };
168 0 foreach my $kid ( keys %local_copy ) {
169 0 $local_copy{$kid}->{return} = $self->{_chi}->get( $kid );
170 }
171 0 return \%local_copy;
172 }
173 }
174  
175 12 ▶ sub _kid_err {
Subroutine _kid_err: called CC=2 SCAR=7.0
176 12 my ($self,$kid,$err) = @_;
177 12 ▶ return unless $self->get_accounting();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting 0 12 12
178 12 push @{ $self->{_results}{$kid}{error} }, $err;
179 }
180 138 ▶ sub _kid_exitcode {
Subroutine _kid_exitcode: called CC=2 SCAR=7.1
181 138 my ($self,$kid,$ec) = @_;
182 138 ▶ return unless $self->get_accounting();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting 0 138 138
183 138 $self->{_results}{$kid}{exitcode} = $ec;
184 }
185 20 ▶ sub _kid_return {
Subroutine _kid_return: called CC=2 SCAR=7.1
186 20 my ($self,$kid,$return) = @_;
187 20 ▶ return unless $self->get_accounting();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting 0 20 20
188 20 $self->{_chi}->set( $kid, $return, 'never' );
189 }
190 210 ▶ sub _kid_signature {
Subroutine _kid_signature: called CC=3 SCAR=12.5
191 210 my ($self,$kid,@args) = @_;
192 210 ▶ return unless $self->get_accounting() && $self->get_trackargs();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting and $self->get_trackargs 0 210 210
Condition33%1/3
$self->get_accounting and $self->get_trackargs
!ll&&!rl&&r
00210
MC/DC0%0/2
$self->get_accounting and $self->get_trackargs
$self->get_accounting $self->get_trackargs
Truth table33%1/3
A and B
A= $self->get_accountingB= $self->get_trackargs
ABresult
0X0
100
111
193 210 $self->{_results}{$kid}{signature} = freeze \@args;
194 }
195 0 ▶ sub _kid_status {
Subroutine _kid_status: not called CC=3 SCAR=24.8
196 0 my ($self,$kid) = @_;
197 0 ▶ return unless $self->get_accounting();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_accounting 0 0 0
198 0 foreach my $err ( @{ $self->{_results}{$kid}{error} } ) {
199 0 $self->_dbmsg(DB_HIGH, "ChildError: $kid -> $err");
200 }
201 }
202  
203 }
204  
205 # Class Methods
206 22 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
207  
208 42 ▶sub new {
Subroutine new: called CC=8 SCAR=21.4
Pod documented
209 # Constructor
210 # Builds our initial Fork Object;
211 42 my ($proto,@args) = @_;
212 42 my $proto_is_obj = ref $proto;
213 42 ▶ my $class = $proto_is_obj || $proto;
Condition33%1/3
$proto_is_obj || $proto
l!l&&r!l&&!r
0420
MC/DC0%0/2
$proto_is_obj || $proto
$proto_is_obj $proto
Truth table33%1/3
A || B
A= $proto_is_objB= $proto
ABresult
1X1
011
000
214 42 my $self = bless {}, $class;
215 # take care of capitalization:
216 42 my %args=();
217 42 ▶ while(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 147 42 189
218 147 my $k = shift @args;
219 147 my $v = shift @args;
220 147 ($k) = ($k =~ /^\s*_?(.*)$/);
221 147 $args{lc($k)}=$v;
222 }
223 # now take care of our initialization
224 42 foreach my $attr ($self->_attributes()) {
225 840 my ($arg) = ($attr =~ /^_?(.*)/);
226 # first see its in our argument list
227 840 ▶ if(exists $args{$arg} && $self->_can('init',$attr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{$arg} and $self->_can('init', $attr)) { } 147 693 840
elsif ($proto_is_obj and $self->_can('copy', $attr)) { } 0 693 693
Condition66%2/3
exists $args{$arg} and $self->_can("init", $attr)
!ll&&!rl&&r
6930147
MC/DC50%1/2
exists $args{$arg} and $self->_can("init", $attr)
exists $args{$arg} $self->_can("init", $attr)
Truth table66%2/3
A and B
A= exists $args{$arg}B= $self->_can("init", $attr)
ABresult
0X0
100
111
228 147 $self->{$attr} = $args{$arg};
229 }
230 # if not, check to see if we're copying an
231 # object. Also, make sure we can copy it!
232 ▶ elsif($proto_is_obj && $self->_can('copy', $attr)) {
Condition33%1/3
$proto_is_obj and $self->_can("copy", $attr)
!ll&&!rl&&r
69300
MC/DC0%0/2
$proto_is_obj and $self->_can("copy", $attr)
$proto_is_obj $self->_can("copy", $attr)
Truth table33%1/3
A and B
A= $proto_is_objB= $self->_can("copy", $attr)
ABresult
0X0
100
111
233 0 $self->{$attr} = $proto->{$attr};
234 }
235 # or, just use the default!
236 else {
237 693 $self->{$attr} = $self->_default($attr);
238 }
239 }
240 # set the parent pid
241 42 $self->set_parentpid($$);
242 42 $self->_dbmsg(DB_HIGH,'FORK OBJECT CREATED');
243  
244 # Create the Cache
245 42 $self->{_chi} = CHI->new( driver => 'File', root_dir => '/tmp', namespace => "PFC-$$" );
246 42 return $self;
247 }
248  
249  
250 0 ▶sub _overLoad {
Subroutine _overLoad: not called CC=5 SCAR=34.0
251 # this is a cheap linux only hack
252 # I will be replacing this as soon as I have time
253 0 my $CMDTOCHECK = '/usr/bin/uptime';
254 0 my ($self) = shift;
255 0 ▶ return FALSE unless $self->get_watchload();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_watchload 0 0 0
256 0 ▶ open(LOAD, "$CMDTOCHECK |") or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
open LOAD, "$CMDTOCHECK |" or return 0 0 0 0
257 0 local $/ = undef;
258 0 chomp(local $_ = <LOAD>);
259 0 close LOAD;
260 0 ▶ if(/load average\:\s+(\d+\.\d+)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if (/load average\:\s+(\d+\.\d+)/m) 0 0 0
261 0 my $current = $1;
262 0 my $MAXLOAD = $self->get_maxload();
263 0 ▶ if ($current >= $MAXLOAD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current >= $MAXLOAD) 0 0 0
264 0 $self->_dbmsg(DB_LOW,"OVERLOAD: Current: $current, Max: $MAXLOAD, RETURNING TRUE");
265 0 return TRUE;
266 }
267 0 $self->_dbmsg(DB_LOW,"OVERLOAD: Current: $current, Max: $MAXLOAD, RETURNING FALSE");
268 0 return FALSE;
269 }
270 0 $self->_dbmsg(DB_LOW,'OVERLOAD: ERROR READING LOAD AVERAGE, RETURNING FALSE');
271 0 return FALSE;
272 }
273  
274 230 ▶sub _tooManyKids {
Subroutine _tooManyKids: called CC=8 SCAR=33.2
275 # determine if there are too many forks
276 230 my ($self) = @_;
277 230 my $kids = $self->kids;
278 230 my $MAXKIDS = $self->get_maxkids();
279 230 my $MINKIDS = $self->get_minkids();
280  
281 #
282 # Figure out how to do this check.
283 230 ▶ if($self->get_watchload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_watchload) { } 0 230 230
284 0 $self->_dbmsg(DB_MED,'TOOMANYKIDS - LOAD CHECKING');
285 0 ▶ if($self->get_watchcount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_watchcount) { } 0 0 0
286 0 ▶ if(!$self->_overLoad && ($kids < $MAXKIDS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_overLoad and $kids < $MAXKIDS) 0 0 0
Condition0%0/3
not $self->_overLoad and $kids < $MAXKIDS
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->_overLoad and $kids < $MAXKIDS
not $self->_overLoad $kids < $MAXKIDS
Truth table0%0/3
not(A) and B
A= not $self->_overLoadB= $kids < $MAXKIDS
ABresult
0X0
100
111
287 0 $self->_dbmsg(DB_LOW,"TOOMANYKIDS - MAX: $MAXKIDS, Kids: $kids, Return: FALSE");
288 0 return FALSE;
289 }
290 0 $self->_dbmsg(DB_LOW,"TOOMANYKIDS - MAX: $MAXKIDS, Kids: $kids, Return: TRUE");
291 0 return TRUE;
292 }
293 else {
294 0 $self->_dbmsg(DB_MED,'TOOMANYKIDS - CHECKING LOAD, NOT CHECKING COUNT');
295 0 ▶ if(!$self->_overLoad) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_overLoad) 0 0 0
296 0 $self->_dbmsg(DB_LOW,"TOOMANYKIDS - Kids: $kids, UNCHECKED RETURNING FALSE");
297 0 return FALSE;
298 }
299 0 ▶ if($self->kids < $self->get_minkids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kids < $self->get_minkids) 0 0 0
300 0 $self->_dbmsg(DB_LOW, "TOOMANYKIDS - OVERLOAD BUT REACHING MINIMUM KIDS!");
301 0 return FALSE;
302 }
303 0 $self->_dbmsg(DB_LOW,"TOOMANYKIDS - Kids: $kids, UNCHECKED RETURNING TRUE");
304 0 return TRUE;
305 }
306 } # end of watchload
307 else {
308 # not watching the load, stick to the
309 # maxforks attribute
310 230 $self->_dbmsg(DB_LOW,"TOOMANYKIDS - NOT CHECKING LOAD/MEM/CPU - Kids: $kids MAX: $MAXKIDS");
311 230 ▶ if($kids >= $self->get_maxkids()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kids >= $self->get_maxkids) { } 11 219 230
312 11 $self->_dbmsg(DB_MED,'TOOMANYKIDS - RETURN TRUE');
313 11 return TRUE;
314 } else {
315 219 $self->_dbmsg(DB_MED,'TOOMANYKIDS - RETURN FALSE');
316 219 return FALSE;
317 }
318 }
319  
320 # if we get to this point something is wrong, return true
321 0 return TRUE;
322 }
323  
324 242 ▶sub _check {
Subroutine _check: called CC=5 SCAR=18.0
325 #
326 # this function is here to make sure we don't
327 # freeze up eventually. It should be all good.
328 242 my $self = shift;
329 242 $self->{_checked}++;
330 242 ▶ return if $self->get_check_at > $self->get_checked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_check_at > $self->get_checked 127 115 242
331 115 foreach my $pid ( $self->kids ) {
332 439 my $alive = kill 0, $pid;
333 439 ▶ if($alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alive) { } 439 0 439
334 439 my $start = $self->kid_time($pid);
335 439 ▶ if(time - $start > $self->get_processtimeout()) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $start > $self->get_processtimeout) 0 439 439
336 0 $self->_kid_err($pid,'process timeout');
337 0 kill 15, $pid;
338 0 $self->_kid_status($pid);
339 }
340 }
341 else {
342 0 $self->_dbmsg(DB_INFO, "Child ($pid) evaded the reaper. Caught by _check()\n");
343 0 $self->_kidstopped($pid,'evaded the reaper');
344 }
345 }
346 115 $self->{_checked} = 0;
347 }
348  
349 230 ▶sub run {
Subroutine run: called CC=11 SCAR=26.7
Pod documented
350 # self and args go in, run the code ref or die if
351 # the code ref isn't set
352 230 my ($self,@args) = @_;
353  
354 #
355 # Allow a user to pass a CODE Ref as the first argument,
356 # default to legacy CODE Parameter.
357 230 my $codeRef = shift @args;
358  
359 #
360 # If it's not a code ref, put it back on args and get the code ref.
361 230 ▶ if( ref $codeRef ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codeRef ne "CODE") 230 0 230
362 230 unshift @args, $codeRef;
363 230 $codeRef = $self->get_code();
364 }
365  
366 230 my $typeCodeRef = ref $codeRef;
367 230 ▶ die "CANNOT RUN A $typeCodeRef IN run()\n" unless $typeCodeRef eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless $typeCodeRef eq "CODE" 0 230 230
368  
369 # return if our parent has died
370 230 ▶ unless($self->_parentAlive()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_parentAlive) 0 230 230
371 0 $self->_dbmsg(DB_MED, 'PARENT IS NOT ALIVE: ' . $self->get_parentpid);
372 0 return;
373 }
374  
375 # We might call _check();
376 230 $self->_check();
377  
378 # wait for childern to die if we have too many
379 230 ▶ if($self->get_method =~ /block/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_method =~ /block/) { } 0 230 230
elsif ($self->_tooManyKids) { } 11 219 230
380 0 ▶ $self->waitforkids() if $self->_tooManyKids;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_tooManyKids 0 0 0
381 }
382 elsif($self->_tooManyKids) {
383 11 $self->_kidstopped(wait);
384 }
385 else {
386 #
387 # Due limitations with the speed of process creation
388 # on various modern OS's, its best to limit the maximum number
389 # of processes created per second to 100
390 219 select undef, undef, undef, 0.01;
391 }
392  
393 # Protect us from zombies
394 230 ▶ $SIG{CHLD} = sub { $self->_REAPER };
Subroutine __ANON__: called CC=1 SCAR=0.0
395  
396 # fork();
397 230 my $pid = fork();
398 # check for errors
399 230 ▶ die "*\n* FORK ERROR !!\n*\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 230 230
400  
401 # if we're the parent return
402 230 ▶ if($pid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid > 0) 210 20 230
403 210 return $self->_kidstarted($pid,@args);
404 }
405  
406 # we're the child
407 20 local $0 = ' Child of ' . $self->get_name;
408 20 $self->_dbmsg(DB_HIGH,'Running Fork Code');
409 20 my @trapSignals = qw(INT KILL TERM QUIT HUP ABRT);
410 20 my @return = ();
411 20 my $eval_error = undef;
412 20 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.2
413 20 foreach my $sig (@trapSignals) {
414 0 ▶ $SIG{$sig} = sub { $self->_REAPER; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
415 }
416 20 @return = $codeRef->(@args);
417 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
418 0 $eval_error = shift;
419 0 ▶ if($eval_error =~ /timeout/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eval_error =~ /timeout/) 0 0 0
420 0 $self->_kid_err($$, 'alarmed out');
421 }
422 20 };
423 20 ▶ $self->_kid_return( $$, scalar @return > 1 ? \@return : shift @return );
Branch: 1 branch
BranchTrueFalseTotal
scalar @return > 1 ? : 0 20 20
424  
425 20 ▶ my $CODE = $eval_error ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$eval_error ? : 0 20 20
426 20 exit $CODE;
427 }
428  
429  
430 12 ▶sub waitforkids {
Subroutine waitforkids: called CC=2 SCAR=6.9
Pod documented
431 # We'll just rely on our SIG{'CHLD'} handler to actually
432 # disperse of the children, so all we have to do is wait
433 # here.
434 12 my $self = shift;
435 # using select here because it doesn't interfere
436 # with any signals in the program
437 12 ▶ while( $self->kids ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kids) 12 12 24
438 12 $self->_check;
439 12 select undef, undef, undef, 1;
440 12 $self->_REAPER;
441 }
442 12 return TRUE;
443 }
444 # Provided for legacy support
445 0 ▶sub cleanup {
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod documented
446 0 my $self = shift;
447 0 return $self->waitforkids;
448 }
449  
450 161 ▶sub _REAPER {
Subroutine _REAPER: called CC=5 SCAR=16.1
451 # our SIGCHLD Handler
452 # Code from the Perl Cookbook page 592
453 # - heavily modified
454 161 my $self = shift;
455  
456 161 my $pid = wait;
457  
458 161 ▶ if($pid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid > 0) 138 23 161
459 # a pid did something,
460 138 $self->_dbmsg(DB_HIGH,"_REAPER found a child ($pid)!!!!!");
461 138 my $rc = undef;
462 138 ▶ if(!WIFEXITED($?)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not WIFEXITED($?)) { } 12 126 138
elsif (WIFSIGNALED($?)) { } 0 126 126
463 12 $rc=1;
464 12 $self->_dbmsg(DB_INFO, "Child ($pid) exitted abnormally");
465 12 $self->_kid_err($pid,'abnormal process termination');
466 }
467 elsif( WIFSIGNALED($?) ) {
468 0 $self->_kid_err($pid,"Uncaught signal: " . WTERMSIG($?));
469 }
470 138 ▶ if(not defined $rc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 126 12 138
471 126 $rc = WEXITSTATUS($?);
472 }
473 138 $self->_kid_exitcode($pid,$rc);
474 138 $self->_kidstopped($pid);
475 }
476 161 ▶ $SIG{CHLD} = sub {$self->_REAPER};
Subroutine __ANON__: called CC=1 SCAR=0.0
477 }
478  
479 230 ▶sub _parentAlive {
Subroutine _parentAlive: called CC=1 SCAR=0.0
480 # check to see if the parent is still alive
481 230 my $self = shift;
482 230 return kill 0, $self->get_parentpid();
483 }
484  
485 297 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=18.4
486 # AUTOLOAD our get/set methods
487 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
488 297 ▶ return if $AUTOLOAD =~ /DESTROY/;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::ForkControl::AUTOLOAD =~ /DESTROY/ 0 297 297
489 297 my ($self,$arg) = @_;
490  
491 # get routines
492 297 ▶ if($AUTOLOAD =~ /get(_.*)/ && $self->_can('get', $1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::ForkControl::AUTOLOAD =~ /get(_.*)/ and $self->_can("get", $1)) 270 27 297
Condition66%2/3
$Parallel::ForkControl::AUTOLOAD =~ /get(_.*)/ and $self->_can("get", $1)
!ll&&!rl&&r
270270
MC/DC50%1/2
$Parallel::ForkControl::AUTOLOAD =~ /get(_.*)/ and $self->_can("get", $1)
$Parallel::ForkControl::AUTOLOAD =~ /get(_.*)/ $self->_can("get", $1)
Truth table66%2/3
A and B
A= $Parallel::ForkControl::AUTOLOAD =~ /get(_.*)/B= $self->_can("get", $1)
ABresult
0X0
100
111
493 270 my $attr = lc($1);
494 270 ▶ *{$AUTOLOAD} = sub { return $_[0]->{$attr}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
495 270 return $self->{$attr};
496 }
497  
498 # set routines
499 27 ▶ if($AUTOLOAD =~ /set(_.*)/ && $self->_can('set', $1)){
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::ForkControl::AUTOLOAD =~ /set(_.*)/ and $self->_can("set", $1)) 27 0 27
Condition33%1/3
$Parallel::ForkControl::AUTOLOAD =~ /set(_.*)/ and $self->_can("set", $1)
!ll&&!rl&&r
0027
MC/DC0%0/2
$Parallel::ForkControl::AUTOLOAD =~ /set(_.*)/ and $self->_can("set", $1)
$Parallel::ForkControl::AUTOLOAD =~ /set(_.*)/ $self->_can("set", $1)
Truth table33%1/3
A and B
A= $Parallel::ForkControl::AUTOLOAD =~ /set(_.*)/B= $self->_can("set", $1)
ABresult
0X0
100
111
500 27 my $attr = lc($1);
501 27 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
502 26 my ($self,$val) = @_;
503 26 $self->{$attr} = $val;
504 26 return $self->{$attr};
505 27 };
506 27 $self->{$attr} = $arg;
507 27 return $self->{$attr};
508 }
509  
510 0 warn "AUTOLOAD Could not find method $AUTOLOAD\n";
511 0 return;
512 }
513  
514 # DEBUG AND TESTING SUBS
515 0 ▶sub _print_me {
Subroutine _print_me: not called CC=3 SCAR=24.8
516 0 my $self = shift;
517 0 my $class = ref $self;
518 0 print "$class Object:\n";
519 0 foreach my $attr ($self->_attributes) {
520 0 my ($pa) = ($attr =~ /^_(.*)/);
521 0 $pa = "\L\u$pa";
522 0 ▶ my $val = ref $self->{$attr} || $self->{$attr};
Condition0%0/3
ref $self->{$attr} || $self->{$attr}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self->{$attr} || $self->{$attr}
ref $self->{$attr} $self->{$attr}
Truth table0%0/3
A || B
A= ref $self->{$attr}B= $self->{$attr}
ABresult
1X1
011
000
523 0 print "\t$pa: $val\n";
524 }
525 0 print "\n";
526 }
527  
528 1329 ▶sub _dbmsg {
Subroutine _dbmsg: called CC=3 SCAR=17.3
529 # print debugging messages:
530 1329 my ($self,$pri,@MSGS) = @_;
531 1329 ▶ return unless $self->get_debug() >= $pri;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_debug >= $pri 1329 0 1329
532 0 foreach my $msg (@MSGS) {
533 0 $msg =~ s/[\cM\r\n]+//g;
534 0 my $date = scalar localtime;
535 0 print STDERR "$date - $msg\n";
536 }
537 0 return TRUE;
538 }
539  
540 22 return 1;
541 0 1
542 __END__
↑ Summary