Forker/Process.pm

filter: Statementstmt 66.0% 187 / 283 Branchbran 46.5% 67 / 144 Conditioncond 37.2% 22 / 59 MC/DCmcdc 22.8% 8 / 35 Subroutinesub 60.8% 28 / 46 Podpod 80.7% 21 / 26 totaltotal 56.1% 333 / 593 CC 108 SCAR 38.3
CC
108
Cov
57%
_write_tree_line
272.0
_subprocesses
56.0
_calc_eqns
44.2
CRAP 45.9
« Forker.pm ↑ Summary
1 # See copyright, etc in below POD section.
2 ######################################################################
3  
4 package Parallel::Forker::Process;
5 34 require 5.006;
6 34 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use POSIX qw(sys_wait_h :signal_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use vars qw($Debug $VERSION $HashId);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 $VERSION = '1.258';
16  
17 34 $Debug = $Parallel::Forker::Debug;
18 34 $HashId = 0;
19  
20 sub _new {
21 414 ▶ my $class = shift;
Subroutine _new: called CC=6 SCAR=20.3
22 my $self = {
23 _forkref => undef, # Upper Fork object
24 name => $HashId++, # ID for hashing. User may override it
25 label => undef, # Label for run_after's
26 _after_children => {}, # IDs that are waiting on this event
27 _after_parents => {}, # IDs that we need to wait for
28 _state => 'idle', # 'idle', 'ready', 'runable', 'running', 'done', 'parerr'
29 _ref_count => 0, # number of people depending on us
30 pid => undef, # Pid # running as, undef=not running
31 run_after => [], # Process objects that are prereqs
32 ▶ run_pre_start => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
33 0 ▶ run_on_start => sub {confess "%Error: No run_on_start defined\n";},
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 ▶ run_on_finish => sub {my ($procref,$status) = @_;}, # Routine taking child and exit status
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 @_
36 414 };
37 414 $Debug = $Parallel::Forker::Debug;
38 414 ▶ bless $self, ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
04140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
39 # Users need to delete the old one first, if they care.
40 # We don't do that automatically, as generally this is a mistake, and
41 # deleting the old one may terminate a process or have other nasty effects.
42 (!exists $self->{_forkref}{_processes}{$self->{name}})
43 414 ▶ or croak "%Error: Creating a new process under the same name as an existing process: $self->{name},";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"_forkref"}{"_processes"}{$self->{"name"}} and croak(qq[%Error: Creating a new process under the same name as an existing process: $self->{"name"},]) 0 414 414
44 414 $self->{_forkref}{_processes}{$self->{name}} = $self;
45 414 weaken($self->{_forkref});
46  
47 414 ▶ if (defined $self->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"label"}) 108 306 414
48 108 ▶ if (ref $self->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'label'}) { } 0 108 108
49 0 foreach my $label (@{$self->{label}}) {
50 0 push @{$self->{_forkref}{_labels}{$label}}, $self;
51 }
52 } else {
53 108 push @{$self->{_forkref}{_labels}{$self->{label}}}, $self;
54 }
55 }
56 414 $self->_calc_runable; # Recalculate
57 414 return $self;
58 }
59  
60 sub DESTROY {
61 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
62 0 delete $self->{_forkref}{_processes}{$self->{name}};
63 }
64  
65 ##### ACCESSORS
66  
67 301 ▶sub name { return $_[0]->{name}; }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
68 0 ▶sub label { return $_[0]->{label}; }
Subroutine label: not called CC=1 SCAR=6.9
Pod documented
69 0 ▶sub pid { return $_[0]->{pid}; }
Subroutine pid: not called CC=1 SCAR=6.9
Pod documented
70 0 ▶sub status { return $_[0]->{status}; } # Maybe undef
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
71 234 ▶sub status_ok { return defined $_[0]->{status} && $_[0]->{status}==0; }
Subroutine status_ok: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
defined $_[0]{'status'} && $_[0]{'status'} == 0
!ll&&!rl&&r
065169
MC/DC50%1/2
defined $_[0]{'status'} && $_[0]{'status'} == 0
defined $_[0]{'status'} $_[0]{'status'} == 0
Truth table66%2/3
A && B
A= defined $_[0]{'status'}B= $_[0]{'status'} == 0
ABresult
0X0
100
111
72 0 ▶sub forkref { return $_[0]->{_forkref}; }
Subroutine forkref: not called CC=1 SCAR=6.9
Pod documented
73  
74 0 ▶sub state { return $_[0]->{_state}; }
Subroutine state: not called CC=1 SCAR=6.9
Pod documented
75 525 ▶sub is_idle { return $_[0]->{_state} eq 'idle'; }
Subroutine is_idle: called CC=1 SCAR=0.0
Pod documented
76 525 ▶sub is_ready { return $_[0]->{_state} eq 'ready'; }
Subroutine is_ready: called CC=1 SCAR=0.0
Pod documented
77 0 ▶sub is_runable { return $_[0]->{_state} eq 'runable'; }
Subroutine is_runable: not called CC=1 SCAR=6.9
Pod documented
78 223 ▶sub is_running { return $_[0]->{_state} eq 'running'; }
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
79 1446 ▶sub is_done { return $_[0]->{_state} eq 'done'; }
Subroutine is_done: called CC=1 SCAR=0.0
Pod documented
80 754 ▶sub is_parerr { return $_[0]->{_state} eq 'parerr'; }
Subroutine is_parerr: called CC=1 SCAR=0.0
Pod documented
81 sub is_reapable {
82 0 ▶ my $self = shift;
Subroutine is_reapable: not called CC=3 SCAR=24.8
Pod documented
83 0 ▶ return $self->{_ref_count} == 0 && ($self->is_done || $self->is_parerr);
Condition0%0/6
$self->{'_ref_count'} == 0 && ($self->is_done || $self->is_parerr)
!ll&&!rl&&r
000
$self->is_done or $self->is_parerr
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{'_ref_count'} == 0 && ($self->is_done || $self->is_parerr)
$self->{'_ref_count'} == 0 $self->is_done $self->is_parerr
Truth table0%0/4
A && B or C
A= $self->{'_ref_count'} == 0B= $self->is_doneC= $self->is_parerr
ABCresult
0XX0
1000
11X1
1011
84 }
85  
86 394 ▶sub reference { $_[0]->{_ref_count}++ }
Subroutine reference: called CC=1 SCAR=0.0
Pod undocumented
87 294 ▶sub unreference { $_[0]->{_ref_count}-- }
Subroutine unreference: called CC=1 SCAR=0.0
Pod undocumented
88  
89 ##### METHODS
90  
91 sub _calc_eqns {
92 414 ▶ my $self = shift;
Subroutine _calc_eqns: called CC=35 SCAR=37.9
93  
94 # Convert references to names of the reference
95 $self->{run_after} = [map
96 {
97 394 ▶ if (ref $_) { $_ = $_->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 96 298 394
98 394 $_;
99 414 } @{$self->{run_after}} ];
100  
101 414 my $run_after = (join " & ", @{$self->{run_after}});
102 414 $run_after =~ s/([&\|\!\^\---\(\)])/ $1 /g;
103 414 ▶ print " FrkRunafter $self->{name}: $run_after\n" if ($Debug||0)>=2;
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::Forker::Process::Debug || 0) >= 2 0 414 414
Condition50%1/2
$Parallel::Forker::Process::Debug || 0
l!l
4140
MC/DC0%0/1
$Parallel::Forker::Process::Debug || 0
$Parallel::Forker::Process::Debug
Truth table50%1/2
A
A= $Parallel::Forker::Process::Debug
Aresult
11
00
104  
105 414 my $runable_eqn = "";
106 414 my $parerr_eqn = "";
107 414 my $ignerr;
108 414 my $flip_op = ''; # ~ or ^ or empty
109 414 my $between_op = '&&';
110 414 my $between_op_not = '||';
111 414 my $need_op_next = 0;
112 414 my $any_refs = 0;
113 414 foreach my $token (split /\s+/, " $run_after ") {
114 980 ▶ next if $token =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /^\s*$/ 323 657 980
115 #print "TOKE $token\n" if $Debug;
116 657 ▶ if ($token eq '!' || $token eq '^') {
Branch: 6 branches
BranchTrueFalseTotal
if ($token eq '!' or $token eq '^') { } 64 593 657
elsif ($token eq '-') { } 32 561 593
elsif ($token eq '(' or $token eq ')') { } 0 561 561
elsif ($token eq '&') { } 71 490 561
elsif ($token eq '|') { } 64 426 490
elsif ($token =~ /^[a-z0-9_]*$/i) { } 426 0 426
Condition100%3/3
$token eq "!" or $token eq "^"
l!l&&r!l&&!r
3232593
MC/DC100%2/2
$token eq "!" or $token eq "^"
$token eq "!" $token eq "^"
Truth table100%3/3
A or B
A= $token eq "!"B= $token eq "^"
ABresult
1X1
011
000
117 64 $flip_op = $token;
118 } elsif ($token eq '-') {
119 32 $ignerr = 1;
120 ▶ } elsif ($token eq '(' || $token eq ')') {
Condition33%1/3
$token eq "(" or $token eq ")"
l!l&&r!l&&!r
00561
MC/DC0%0/2
$token eq "(" or $token eq ")"
$token eq "(" $token eq ")"
Truth table33%1/3
A or B
A= $token eq "("B= $token eq ")"
ABresult
1X1
011
000
121 0 ▶ if ($token eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "(") 0 0 0
122 0 ▶ $runable_eqn .= " ${between_op}" if $need_op_next;
Branch: 1 branch
BranchTrueFalseTotal
if $need_op_next 0 0 0
123 0 ▶ $parerr_eqn .= " ${between_op_not}" if $need_op_next;
Branch: 1 branch
BranchTrueFalseTotal
if $need_op_next 0 0 0
124 0 $need_op_next = 0;
125 }
126 0 $runable_eqn .= " $token ";
127 0 $parerr_eqn.= " $token ";
128 } elsif ($token eq '&') {
129 71 $between_op = '&&'; $between_op_not = '||';
130 } elsif ($token eq '|') {
131 64 $between_op = '||'; $between_op_not = '&&';
132 } elsif ($token =~ /^[a-z0-9_]*$/i) {
133 # Find it
134 426 my @found = $self->{_forkref}->find_proc_name($token);
135 426 ▶ if (defined $found[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found[0]) { } 394 32 426
136 394 foreach my $aftref (@found) {
137 394 my $aftname = $aftref->{name};
138 394 ▶ ($aftref ne $self) or die "%Error: Id $self->{name} has a run_after on itself; it will never start\n";
Branch: 1 branch
BranchTrueFalseTotal
$aftref ne $self or die qq[%Error: Id $self->{"name"} has a run_after on itself; it will never start\n] 0 394 394
139 394 ▶ $runable_eqn .= " ${between_op}" if $need_op_next;
Branch: 1 branch
BranchTrueFalseTotal
if $need_op_next 103 291 394
140 394 ▶ $parerr_eqn .= " ${between_op_not}" if $need_op_next;
Branch: 1 branch
BranchTrueFalseTotal
if $need_op_next 103 291 394
141 # _ranok, _ranfail, _nofail
142 394 ▶ if ($flip_op eq '!') {
Branch: 2 branches
BranchTrueFalseTotal
if ($flip_op eq '!') { } 32 362 394
elsif ($flip_op eq '^') { } 32 330 362
143 32 $runable_eqn .= " (_ranfail('$aftname')||_parerr('$aftname'))";
144 32 $parerr_eqn .= " (_ranok('$aftname'))";
145 } elsif ($flip_op eq '^') {
146 32 $runable_eqn .= " (_ranok('$aftname')||_ranfail('$aftname')||_parerr('$aftname'))";
147 32 $parerr_eqn .= " (0)";
148 } else {
149 330 $runable_eqn .= " (_ranok('$aftname'))";
150 330 $parerr_eqn .= " (_ranfail('$aftname')||_parerr('$aftname'))";
151 }
152  
153 394 $aftref->{_after_children}{$self->{name}} = $self;
154 394 $self->{_after_parents}{$aftref->{name}} = $aftref;
155 394 weaken($aftref->{_after_children}{$self->{name}});
156 394 weaken($self->{_after_parents}{$aftref->{name}});
157  
158 394 ▶ my $apo = $flip_op; $apo ||= 'O' if $between_op eq '||';
Branch: 1 branch
BranchTrueFalseTotal
if $between_op eq "||" 64 330 394
Condition50%1/2
$apo ||= "O"
l!l
064
MC/DC0%0/1
$apo ||= "O"
$apo
Truth table50%1/2
A
A= $apo
Aresult
11
00
159 394 ▶ $apo ||= '&'; $apo='E' if $apo eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $apo eq "!" 32 362 394
Condition100%2/2
$apo ||= "&"
l!l
128266
MC/DC100%1/1
$apo ||= "&"
$apo
Truth table100%2/2
A
A= $apo
Aresult
11
00
160 394 $self->{_after_parents_op}{$aftref->{name}} = $apo;
161 394 $need_op_next = 1;
162 394 $any_refs = 1;
163 }
164 } else {
165 32 ▶ if ($ignerr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignerr) { } 32 0 32
166 32 ▶ print " FrkProc $self->{name} run_after process/label $token not found ignored.\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 32 0 32
167 } else {
168 0 croak "%Error: run_after process/label $token not found,";
169 }
170 }
171 # Prep for next
172 426 $ignerr = 0;
173 426 $flip_op = '';
174 } else {
175 0 croak "%Error: run_after parse error of $token in: $run_after,";
176 }
177 }
178 414 ▶ $runable_eqn = "1" if !$any_refs;
Branch: 1 branch
BranchTrueFalseTotal
unless $any_refs 123 291 414
179 414 ▶ $parerr_eqn = "0" if !$any_refs;
Branch: 1 branch
BranchTrueFalseTotal
unless $any_refs 123 291 414
180 414 $self->{_runafter_text} = $run_after;
181 414 $self->{_runable_eqn_text} = $runable_eqn;
182 414 $self->{_parerr_eqn_text} = $parerr_eqn;
183 414 my $set = ("\t\$self->{_runable_eqn} = sub { return $runable_eqn; };\n"
184 ."\t\$self->{_parerr_eqn} = sub { return $parerr_eqn; };1;\n");
185 414 ▶ print "$set" if ($Debug||0)>=2;
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::Forker::Process::Debug || 0) >= 2 0 414 414
Condition50%1/2
$Parallel::Forker::Process::Debug || 0
l!l
4140
MC/DC0%0/1
$Parallel::Forker::Process::Debug || 0
$Parallel::Forker::Process::Debug
Truth table50%1/2
A
A= $Parallel::Forker::Process::Debug
Aresult
11
00
186 414 ▶ eval $set or die ("%Error: Can't eval:\n$@\n"
Branch: 1 branch
BranchTrueFalseTotal
eval $set or die "%Error: Can't eval:\n$@\n" . qq[ $self->{"_runafter_text"}\n $self->{"_runable_eqn_text"}\n $self->{"_parerr_eqn_text"}\n] 0 414 414
187 ." $self->{_runafter_text}\n $self->{_runable_eqn_text}\n $self->{_parerr_eqn_text}\n");
188 }
189  
190 sub ready {
191 414 ▶ my $self = shift;
Subroutine ready: called CC=5 SCAR=16.5
Pod documented
192 # User is indicating ready.
193 414 ▶ ($self->{_state} eq 'idle') or croak "%Error: Signalling ready to already ready process,";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_state"} eq "idle" or croak("%Error: Signalling ready to already ready process,") 0 414 414
194  
195 414 $self->_calc_eqns;
196  
197 # Transition: idle -> 'ready'
198 414 ▶ print " FrkProc $self->{name} $self->{_state} -> ready\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 414 0 414
199 414 ▶ if (not $self->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_ready) 414 0 414
200 414 $_->reference for values %{$self->{_after_parents}};
201 }
202 414 $self->{_state} = 'ready';
203 414 $self->_calc_runable;
204 }
205  
206 sub parerr {
207 111 ▶ my $self = shift;
Subroutine parerr: called CC=5 SCAR=17.8
Pod undocumented
208 # Mark process as never to be run
209 111 ▶ if ($self->is_idle || $self->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_idle or $self->is_ready) { } 111 0 111
Condition33%1/3
$self->is_idle or $self->is_ready
l!l&&r!l&&!r
01110
MC/DC0%0/2
$self->is_idle or $self->is_ready
$self->is_idle $self->is_ready
Truth table33%1/3
A or B
A= $self->is_idleB= $self->is_ready
ABresult
1X1
011
000
210 111 ▶ print " FrkProc $self->{name} $self->{_state} -> parerr\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 111 0 111
211 111 $self->{_state} = 'parerr'; # "can't run due to parent status" is more accurate
212 } else {
213 0 croak "%Error: process isn't ready\n";
214 }
215 # May need to spawn/kill children
216 111 foreach my $ra (values %{$self->{_after_children}}) {
217 111 $ra->_calc_runable;
218 }
219 }
220  
221 sub run {
222 223 ▶ my $self = shift;
Subroutine run: called CC=5 SCAR=16.2
Pod documented
223 # Transition: Any state -> 'running', ignoring run_after's
224 223 ▶ !$self->{pid} or croak "%Error: process is already running,";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"pid"} and croak("%Error: process is already running,") 0 223 223
225 223 ▶ !$self->is_running or croak "%Error: process is already running,";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_running and croak("%Error: process is already running,") 0 223 223
226  
227 223 ▶ print " FrkProc $self->{name} $self->{_state} -> running\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 223 0 223
228 223 $self->{_state} = 'running';
229 223 $self->{start_time} = time();
230 223 $self->{run_pre_start}->($self);
231 223 ▶ if (my $pid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 195 28 223
232 195 $self->{pid} = $pid;
233 195 $self->{pid_last_run} = $pid;
234 195 $self->{_forkref}{_running}{$self->{pid}} = $self;
235 195 delete $self->{_forkref}{_runable}{$self->{name}};
236 } else {
237 28 $self->{run_on_start}->($self);
238 23 exit(0); # Don't close anything
239 }
240 195 return $self; # So can chain commands
241 }
242  
243 sub run_after {
244 0 ▶ my $self = shift;
Subroutine run_after: not called CC=2 SCAR=17.9
Pod documented
245 # @_ = objects to add as prereqs
246 0 ▶ ($self->{_state} eq 'idle') or croak "%Error: Must set run_after's before marking the process ready,";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_state"} eq "idle" or croak("%Error: Must set run_after's before marking the process ready,") 0 0 0
247 0 push @{$self->{run_after}}, @_;
248 0 return $self; # So can chain commands
249 }
250  
251 sub reap {
252 0 ▶ my $self = shift;
Subroutine reap: not called CC=5 SCAR=34.0
Pod documented
253  
254 0 ▶ $self->is_reapable or croak "%Error: process is not reapable,";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_reapable or croak("%Error: process is not reapable,") 0 0 0
255 0 delete $self->{_forkref}{_processes}{$self->{name}};
256 0 ▶ if (defined $self->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"label"}) 0 0 0
257 0 ▶ if (ref $self->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'label'}) { } 0 0 0
258 0 foreach my $label (@{$self->{label}}) {
259 0 @{$self->{_forkref}{_labels}{$label}} =
260 0 grep { $_->{name} ne $self->{name} }
261 0 @{$self->{_forkref}{_labels}{$label}};
262 }
263 } else {
264 0 @{$self->{_forkref}{_labels}{$self->{label}}} =
265 0 grep { $_->{name} ne $self->{name} }
266 0 @{$self->{_forkref}{_labels}{$self->{label}}};
267 }
268 }
269 }
270  
271 34 ▶use vars qw($_Calc_Runable_Fork);
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 sub _calc_runable {
274 1158 ▶ my $self = shift;
Subroutine _calc_runable: called CC=6 SCAR=18.1
275 # @_ = objects to add as prereqs
276 1158 ▶ return if ($self->{_state} ne 'ready');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_state"} ne "ready" 454 704 1158
277 #use Data::Dumper; print "CR ",Dumper($self),"\n";
278  
279 # Used by the callbacks
280 704 local $_Calc_Runable_Fork = $self->{_forkref};
281 sub _ranok {
282 653 ▶ my $procref = $_Calc_Runable_Fork->{_processes}{$_[0]};
Subroutine _ranok: called CC=4 SCAR=14.2
283 653 ▶ print " _ranok $procref->{name} State $procref->{_state}\n" if ($Debug||0)>=2;
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::Forker::Process::Debug || 0) >= 2 0 653 653
Condition50%1/2
$Parallel::Forker::Process::Debug || 0
l!l
6530
MC/DC0%0/1
$Parallel::Forker::Process::Debug || 0
$Parallel::Forker::Process::Debug
Truth table50%1/2
A
A= $Parallel::Forker::Process::Debug
Aresult
11
00
284 653 ▶ return ($procref->is_done && $procref->status_ok);
Condition100%3/3
$procref->is_done && $procref->status_ok
!ll&&!rl&&r
48926138
MC/DC100%2/2
$procref->is_done && $procref->status_ok
$procref->is_done $procref->status_ok
Truth table100%3/3
A && B
A= $procref->is_doneB= $procref->status_ok
ABresult
0X0
100
111
285 }
286 sub _ranfail {
287 604 ▶ my $procref = $_Calc_Runable_Fork->{_processes}{$_[0]};
Subroutine _ranfail: called CC=4 SCAR=14.2
288 604 ▶ print " _ranfail $procref->{name} State $procref->{_state}\n" if ($Debug||0)>=2;
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::Forker::Process::Debug || 0) >= 2 0 604 604
Condition50%1/2
$Parallel::Forker::Process::Debug || 0
l!l
6040
MC/DC0%0/1
$Parallel::Forker::Process::Debug || 0
$Parallel::Forker::Process::Debug
Truth table50%1/2
A
A= $Parallel::Forker::Process::Debug
Aresult
11
00
289 604 ▶ return ($procref->is_done && !$procref->status_ok);
Condition100%3/3
$procref->is_done && !$procref->status_ok
!ll&&!rl&&r
5343139
MC/DC100%2/2
$procref->is_done && !$procref->status_ok
$procref->is_done !$procref->status_ok
Truth table100%3/3
A && not(B)
A= $procref->is_doneB= !$procref->status_ok
ABresult
0X0
100
111
290 }
291 sub _parerr {
292 565 ▶ my $procref = $_Calc_Runable_Fork->{_processes}{$_[0]};
Subroutine _parerr: called CC=3 SCAR=11.7
293 565 ▶ print " _parerr $procref->{name} State $procref->{_state}\n" if ($Debug||0)>=2;
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::Forker::Process::Debug || 0) >= 2 0 565 565
Condition50%1/2
$Parallel::Forker::Process::Debug || 0
l!l
5650
MC/DC0%0/1
$Parallel::Forker::Process::Debug || 0
$Parallel::Forker::Process::Debug
Truth table50%1/2
A
A= $Parallel::Forker::Process::Debug
Aresult
11
00
294 565 return ($procref->is_parerr);
295 }
296  
297 704 ▶ if (&{$self->{_runable_eqn}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (&{$self->{'_runable_eqn'};}) { } 258 446 704
elsif (&{$self->{'_parerr_eqn'};}) { } 111 335 446
298 # Transition: ready -> runable
299 258 ▶ print " FrkProc $self->{name} $self->{_state} -> runable\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 258 0 258
300 258 $self->{_state} = 'runable'; # No dependencies (yet) so can launch it
301 258 $self->{_forkref}{_runable}{$self->{name}} = $self;
302 446 } elsif (&{$self->{_parerr_eqn}}) {
303 111 $_->unreference for values %{$self->{_after_parents}};
304 111 $self->parerr;
305 }
306 }
307  
308 ##### STATE TRANSITIONS
309  
310 34 our $_Warned_Waitpid;
311  
312 sub poll {
313 331 ▶ my $self = shift;
Subroutine poll: called CC=9 SCAR=24.4
Pod documented
314 331 ▶ return undef if !$self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 331 331
315  
316 331 my $got = waitpid($self->{pid}, WNOHANG);
317 331 ▶ if ($got!=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got != 0) 168 163 331
318 168 ▶ if ($got>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got > 0) { } 168 0 168
319 168 $self->{status} = $?; # convert wait return to status
320 } else {
321 0 $self->{status} = undef;
322 0 ▶ carp "%Warning: waitpid($self->{pid}) returned -1 instead of status; perhaps you're ignoring SIG{CHLD}?"
Branch: 1 branch
BranchTrueFalseTotal
if $^W and not $Parallel::Forker::Process::_Warned_Waitpid 0 0 0
Condition0%0/3
$^W and not $Parallel::Forker::Process::_Warned_Waitpid
!ll&&!rl&&r
000
MC/DC0%0/2
$^W and not $Parallel::Forker::Process::_Warned_Waitpid
$^W not $Parallel::Forker::Process::_Warned_Waitpid
Truth table0%0/3
A and not(B)
A= $^WB= not $Parallel::Forker::Process::_Warned_Waitpid
ABresult
0X0
100
111
323 if ($^W && !$_Warned_Waitpid);
324 0 $_Warned_Waitpid = 1;
325 }
326 # Transition: running -> 'done'
327 168 ▶ print " FrkProc $self->{name} $self->{_state} -> done ($self->{status})\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 168 0 168
328 168 delete $self->{_forkref}{_running}{$self->{pid}};
329 168 $self->{pid} = undef;
330 168 $self->{_state} = 'done';
331 168 $self->{end_time} = time();
332 168 $self->{run_on_finish}->($self, $self->{status});
333 # Transition children: ready -> runable
334 168 foreach my $ra (values %{$self->{_after_children}}) {
335 219 $ra->_calc_runable;
336 }
337 168 $_->unreference for values %{$self->{_after_parents}};
338 # Done
339 168 return $self;
340 }
341 163 return undef;
342 }
343  
344 sub kill {
345 0 ▶ my $self = shift;
Subroutine kill: not called CC=3 SCAR=24.8
Pod documented
346 0 ▶ my $signal = shift || 9;
Condition0%0/2
shift() || 9
l!l
00
MC/DC0%0/1
shift() || 9
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
347 0 ▶ CORE::kill($signal, $self->{pid}) if $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} 0 0 0
348 # We don't remove it's pid, we'll get a child exit that will do it
349 }
350  
351 sub kill_tree {
352 0 ▶ my $self = shift;
Subroutine kill_tree: not called CC=5 SCAR=34.0
Pod documented
353 0 ▶ my $signal = shift || 9;
Condition0%0/2
shift() || 9
l!l
00
MC/DC0%0/1
shift() || 9
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
354 0 ▶ return if !$self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 0 0
355 0 my @proc = (_subprocesses($self->{pid}), $self->{pid});
356 0 foreach my $pid (@proc) {
357 0 ▶ print " Fork Kill -$signal $pid (child of $pid)\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Parallel::Forker::Process::Debug 0 0 0
358 0 CORE::kill($signal, $pid);
359 }
360 # We don't remove it's pid, we'll get a child exit that will do it
361 }
362  
363 sub format_time {
364 0 ▶ my $secs = shift;
Subroutine format_time: not called CC=1 SCAR=6.9
Pod undocumented
365 0 return sprintf("%02d:%02d:%02d", int($secs/3600), int(($secs%3600)/60), $secs % 60);
366 }
367  
368 sub format_loctime {
369 0 ▶ my $time = shift;
Subroutine format_loctime: not called CC=1 SCAR=6.9
Pod undocumented
370 0 my ($sec,$min,$hour) = localtime($time);
371 0 return sprintf("%02d:%02d:%02d", $hour, $min, $sec);
372 }
373  
374 sub _write_tree_line {
375 0 ▶ my $self = shift;
Subroutine _write_tree_line: not called CC=16 SCAR=56.1
376 0 my $level = shift;
377 0 my $linenum = shift;
378 0 my $cmt = "";
379 0 ▶ if (!$linenum) {
Branch: 5 branches
BranchTrueFalseTotal
if (not $linenum) { } 0 0 0
elsif ($linenum == 1) { } 0 0 0
elsif ($linenum == 2) { } 0 0 0
elsif ($linenum == 3) { } 0 0 0
elsif ($linenum == 4) { } 0 0 0
380 0 my $state = uc $self->{_state};
381 0 ▶ $state .= "-ok" if $self->is_done && $self->status_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_done and $self->status_ok 0 0 0
Condition0%0/3
$self->is_done and $self->status_ok
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_done and $self->status_ok
$self->is_done $self->status_ok
Truth table0%0/3
A and B
A= $self->is_doneB= $self->status_ok
ABresult
0X0
100
111
382 0 ▶ $state .= "-err" if $self->is_done && !$self->status_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_done and not $self->status_ok 0 0 0
Condition0%0/3
$self->is_done and not $self->status_ok
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_done and not $self->status_ok
$self->is_done not $self->status_ok
Truth table0%0/3
A and not(B)
A= $self->is_doneB= not $self->status_ok
ABresult
0X0
100
111
383 return sprintf("%s %-27s %-8s %s\n",
384 "--", #x$level
385 $self->{name},
386 $state, # DONE-err is longest
387 0 ▶ ($self->{comment}||""));
Condition0%0/2
$self->{'comment'} || ''
l!l
00
MC/DC0%0/1
$self->{'comment'} || ''
$self->{'comment'}
Truth table0%0/2
A
A= $self->{'comment'}
Aresult
11
00
388 } elsif ($linenum == 1) {
389 0 ▶ if ($self->{start_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"start_time"}) 0 0 0
390 0 $cmt .= "Start ".format_loctime($self->{start_time});
391 0 ▶ if ($self->{end_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"end_time"}) 0 0 0
392 0 $cmt .= ", End ".format_loctime($self->{end_time});
393 0 $cmt .= ", Took ".format_time(($self->{end_time}-$self->{start_time}));
394 0 $cmt .= ", Pid ".$self->{pid_last_run};
395 }
396 }
397 } elsif ($linenum == 2) {
398 0 ▶ $cmt .= "Runaft = ".$self->{_runafter_text} if defined $self->{_runafter_text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_runafter_text"} 0 0 0
399 } elsif ($linenum == 3) {
400 0 ▶ $cmt .= "RunEqn = ".$self->{_runable_eqn_text} if defined $self->{_runable_eqn_text} ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_runable_eqn_text"} 0 0 0
401 } elsif ($linenum == 4) {
402 0 ▶ $cmt .= "ErrEqn = ".$self->{_parerr_eqn_text} if defined $self->{_parerr_eqn_text} ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_parerr_eqn_text"} 0 0 0
403 }
404 0 return sprintf("%s %-27s %-8s %s\n",
405 " ", #x$level
406 "",
407 "",
408 $cmt);
409 }
410  
411 sub _subprocesses {
412 0 ▶ my $parent = shift || $$;
Subroutine _subprocesses: not called CC=7 SCAR=40.3
Condition0%0/3
shift() || $$
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $$
shift() $$
Truth table0%0/3
A || B
A= shift()B= $$
ABresult
1X1
011
000
413 # All pids under the given parent
414 # Used by testing module
415 # Same function in Schedule::Load::_subprocesses
416 0 my $pt = new Proc::ProcessTable( 'cache_ttys' => 1);
417 0 my %parent_pids;
418 0 foreach my $p (@{$pt->table}) {
419 0 $parent_pids{$p->pid} = $p->ppid;
420 }
421 0 my @out;
422 0 my @search = ($parent);
423 0 ▶ while ($#search > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#search > -1) 0 0 0
424 0 my $pid = shift @search;
425 0 ▶ push @out, $pid if $pid ne $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $pid ne $parent 0 0 0
426 0 foreach (keys %parent_pids) {
427 0 ▶ push @search, $_ if $parent_pids{$_} == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $parent_pids{$_} == $pid 0 0 0
428 }
429 }
430 0 return @out;
431 }
432  
433 ######################################################################
434 #### Package return
435 34 1;
436 =pod
437  
438 =head1 NAME
439  
440 Parallel::Forker::Process - Single parallel fork process object
441  
442 =head1 SYNOPSIS
443  
444 $obj->run;
445 $obj->poll;
446 $obj->kill(<"SIGNAL">);
447 $obj->kill_tree(<"SIGNAL">);
448  
449 =head1 DESCRIPTION
450  
451 Manage a single process under the control of Parallel::Forker.
452  
453 Processes are created by calling a Parallel::Forker object's schedule
454 method, and retrieved by various methods in that class.
455  
456 Processes transition over 6 states. They begin in idle state, and are
457 transitioned by the user into ready state. As their dependencies complete,
458 Parallel::Forker transitions them to the runable state. As the
459 Parallel::Forker object's C<max_proc> limit permits, they transition to the
460 running state, and get executed. On completion, they transition to the
461 done state. If a process depends on another process, and that other
462 process fails, the dependant process transitions to the parerr (parent
463 error) state, and is never run.
464  
465 =head1 METHODS
466  
467 =over 4
468  
469 =item forkref
470  
471 Return the parent Parallel::Forker object this process belongs to.
472  
473 =item is_done
474  
475 Returns true if the process is in the done state.
476  
477 =item is_idle
478  
479 Returns true if the process is in the idle state.
480  
481 =item is_parerr
482  
483 Returns true if the process is in the parent error state.
484  
485 =item is_ready
486  
487 Returns true if the process is in the ready state.
488  
489 =item is_reapable
490  
491 Returns true if the process is reapable (->reap may be called on it).
492  
493 =item is_runable
494  
495 Returns true if the process is in the runable state.
496  
497 =item is_running
498  
499 Returns true if the process is in the running state.
500  
501 =item kill(<signal>)
502  
503 Send the specified signal to the process if it is running. If no signal is
504 specified, send a SIGKILL (9).
505  
506 =item kill_tree(<signal>)
507  
508 Send the specified signal to the process (and its subchildren) if it is
509 running. If no signal is specified, send a SIGKILL (9).
510  
511 =item kill_tree_all(<signal>)
512  
513 Send a signal to this child (and its subchildren) if it is running. If no
514 signal is specified, send a SIGKILL (9).
515  
516 =item label
517  
518 Return the label of the process, if any, else undef.
519  
520 =item name
521  
522 Return the name of the process.
523  
524 =item pid
525  
526 Return the process ID if this job is running, else undef.
527  
528 =item poll
529  
530 Check the process for activity, invoking callbacks if needed.
531 Generally Parallel::Forker's object method C<poll()> is used instead.
532  
533 =item ready
534  
535 Mark this process as being ready for execution when all C<run_after>'s are
536 ready and CPU resources permit. When that occurs, run will be called on
537 the process automatically.
538  
539 =item reap
540  
541 When the process has no other processes waiting for it, and the process is
542 is_done or is_parerr, remove the data structures for it. This reclaims
543 memory for when a large number of processes are being created, run, and
544 destroyed.
545  
546 =item run
547  
548 Unconditionally move the process to the "running" state and start it.
549  
550 =item run_after
551  
552 Add a new (or list of) processes that must be completed before this process
553 can be runnable. You may pass a process object (from schedule), a process
554 name, or a process label. You may use "|" or "&" in a string to run this
555 process after ANY processes exit, or after ALL exit (the default.)
556 ! in front of a process name indicates to run if that process fails with
557 bad exit status. ^ in front of a process indicates to run if that process
558 succeeds OR fails.
559  
560 =item state
561  
562 Returns the name of the current state, 'idle', 'ready', 'runable',
563 'running', 'done' or 'parerr'. For forward compatibility, use the is_idle
564 etc. methods instead of comparing this accessor's value to a constant
565 string.
566  
567 =item status
568  
569 Return the exit status of this process if it has completed. The exit
570 status will only be correct if a CHLD signal handler is installed,
571 otherwise it may be undef.
572  
573 =item status_ok
574  
575 Return true if the exit status of this process was zero. Return false if
576 not ok, or if the status has not been determined, or if the status was
577 undef.
578  
579 =back
580  
581 =head1 DISTRIBUTION
582  
583 The latest version is available from CPAN and from
584 L<https://www.veripool.org/parallel-forker>.
585  
586 Copyright 2002-2020 by Wilson Snyder. This package is free software; you
587 can redistribute it and/or modify it under the terms of either the GNU
588 Lesser General Public License Version 3 or the Perl Artistic License
589 Version 2.0.
590  
591 =head1 AUTHORS
592  
593 Wilson Snyder <wsnyder@wsnyder.org>
594  
595 =head1 SEE ALSO
596  
597 L<Parallel::Forker>
598  
599 =cut
600 ######################################################################
« Forker.pm ↑ Summary