Forker/Process.pm

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