Prefork.pm

filter: Statementstmt 88.0% 132 / 150 Branchbran 75.0% 54 / 72 Conditioncond 70.5% 12 / 17 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 96.0% 24 / 25 Podpod 83.3% 5 / 6 totaltotal 82.5% 232 / 281 SCAR 40.4
CC
46
Cov
83%
start
27.3
_wait
7.0
wait_all_children
6.0
CRAP 56.7
« Prefork/SpareWorkers/Scoreboard.pm ↑ Summary Prefork/SpareWorkers.pm »
1 package Parallel::Prefork;
2  
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 56 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 56 ▶use base qw/Class::Accessor::Lite/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use List::Util qw/first max min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 56 ▶use Proc::Wait3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 56 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use Class::Accessor::Lite (
14 56 rw => [ qw/max_workers spawn_interval err_respawn_interval trap_signals signal_received manager_pid on_child_reap before_fork after_fork/ ],
15 56 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 56 our $VERSION = '0.18';
18  
19 sub new {
20 54 ▶ my $klass = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
21 54 ▶ my $opts = @_ == 1 ? $_[0] : +{ @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 54 0 54
22 54 my $self = bless {
23 worker_pids => {},
24 max_workers => 10,
25 spawn_interval => 0,
26 err_respawn_interval => 1,
27 trap_signals => {
28 TERM => 'TERM',
29 },
30 signal_received => '',
31 manager_pid => undef,
32 generation => 0,
33 %$opts,
34 _no_adjust_until => 0, # becomes undef in wait_all_children
35 }, $klass;
36 $SIG{$_} = sub {
37 5 ▶ $self->signal_received($_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
38 54 } for keys %{$self->trap_signals};
39 54 ▶ $SIG{CHLD} = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
40 54 $self;
41 }
42  
43 sub start {
44 55 ▶ my ($self, $cb) = @_;
Subroutine start: called CC=20 SCAR=33.1
Pod documented
45
46 55 $self->manager_pid($$);
47 55 $self->signal_received('');
48 55 $self->{generation}++;
49
50 die 'cannot start another process while you are in child process'
51 55 ▶ if $self->{in_child};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_child"} 0 55 55
52
53 # main loop
54 55 ▶ while (! $self->signal_received) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->signal_received) 881 6 887
55 881 ▶ my $action = $self->{_no_adjust_until} <= Time::HiRes::time()
Condition66%2/3
$self->{'_no_adjust_until'} <= &Time::HiRes::time() && $self->_decide_action
!ll&&!rl&&r
059822
MC/DC50%1/2
$self->{'_no_adjust_until'} <= &Time::HiRes::time() && $self->_decide_action
$self->{'_no_adjust_until'} <= &Time::HiRes::time() $self->_decide_action
Truth table66%2/3
A && B
A= $self->{'_no_adjust_until'} <= &Time::HiRes::time()B= $self->_decide_action
ABresult
0X0
100
111
56 && $self->_decide_action;
57 881 ▶ if ($action > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action > 0) { } 817 64 881
elsif ($action < 0) { } 5 59 64
58 # start a new worker
59 817 ▶ if (my $subref = $self->before_fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subref = $self->before_fork) 39 778 817
60 39 $subref->($self);
61 }
62 817 my $pid = fork;
63 817 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 817 817
64 0 warn "fork failed:$!";
65 0 $self->_update_spawn_delay($self->err_respawn_interval);
66 0 next;
67 }
68 817 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 49 768 817
69 # child process
70 49 $self->{in_child} = 1;
71 49 $SIG{$_} = 'DEFAULT' for keys %{$self->trap_signals};
72 49 $SIG{CHLD} = 'DEFAULT'; # revert to original
73 49 ▶ exit 0 if $self->signal_received;
Branch: 1 branch
BranchTrueFalseTotal
if $self->signal_received 0 49 49
74 49 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 7 42 49
75 7 $cb->();
76 7 $self->finish();
77 }
78 42 return;
79 }
80 768 ▶ if (my $subref = $self->after_fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subref = $self->after_fork) 32 736 768
81 32 $subref->($self, $pid);
82 }
83 768 $self->{worker_pids}{$pid} = $self->{generation};
84 768 $self->_update_spawn_delay($self->spawn_interval);
85 } elsif ($action < 0) {
86 # stop an existing worker
87 kill(
88 $self->_action_for('TERM')->[0],
89 5 (keys %{$self->{worker_pids}})[0],
90 );
91 5 $self->_update_spawn_delay($self->spawn_interval);
92 }
93 $self->{__dbg_callback}->()
94 832 ▶ if $self->{__dbg_callback};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__dbg_callback"} 0 832 832
95 832 ▶ if (my ($exit_pid, $status)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->_wait(!$self->{"__dbg_callback"} && $action <= 0)) 25 807 832
Condition66%2/3
!$self->{'__dbg_callback'} && $action <= 0
!ll&&!rl&&r
076864
MC/DC50%1/2
!$self->{'__dbg_callback'} && $action <= 0
!$self->{'__dbg_callback'} $action <= 0
Truth table66%2/3
not(A) && B
A= !$self->{'__dbg_callback'}B= $action <= 0
ABresult
0X0
100
111
96 = $self->_wait(! $self->{__dbg_callback} && $action <= 0)) {
97 25 $self->_on_child_reap($exit_pid, $status);
98 25 ▶ if (delete($self->{worker_pids}{$exit_pid}) == $self->{generation}
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{"worker_pids"}{$exit_pid} == $self->{"generation"} and $status != 0) 5 20 25
Condition100%3/3
delete $self->{"worker_pids"}{$exit_pid} == $self->{"generation"} and $status != 0
!ll&&!rl&&r
1195
MC/DC100%2/2
delete $self->{"worker_pids"}{$exit_pid} == $self->{"generation"} and $status != 0
delete $self->{"worker_pids"}{$exit_pid} == $self->{"generation"} $status != 0
Truth table100%3/3
A and B
A= delete $self->{"worker_pids"}{$exit_pid} == $self->{"generation"}B= $status != 0
ABresult
0X0
100
111
99 && $status != 0) {
100 5 $self->_update_spawn_delay($self->err_respawn_interval);
101 }
102 }
103 }
104 # send signals to workers
105 6 ▶ if (my $action = $self->_action_for($self->signal_received)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $self->_action_for($self->signal_received)) 6 0 6
106 6 my ($sig, $interval) = @$action;
107 6 ▶ if ($interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interval) { } 0 6 6
108 # fortunately we are the only one using delayed_task, so implement
109 # this setup code idempotent and replace the already-registered
110 # callback (if any)
111 0 my @pids = sort keys %{$self->{worker_pids}};
112 $self->{delayed_task} = sub {
113 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 my $pid = shift @pids;
115 0 kill $sig, $pid;
116 0 ▶ if (@pids == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pids == 0) { } 0 0 0
117 0 delete $self->{delayed_task};
118 0 delete $self->{delayed_task_at};
119 } else {
120 0 $self->{delayed_task_at} = Time::HiRes::time() + $interval;
121 }
122 0 };
123 0 $self->{delayed_task_at} = 0;
124 0 $self->{delayed_task}->($self);
125 } else {
126 6 $self->signal_all_children($sig);
127 }
128 }
129
130 6 1; # return from parent process
131 }
132  
133 sub finish {
134 49 ▶ my ($self, $exit_code) = @_;
Subroutine finish: called CC=3 SCAR=11.7
Pod documented
135 49 ▶ die "\$parallel_prefork->finish() shouln't be called within the manager process\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->manager_pid == $$ 0 49 49
136 if $self->manager_pid() == $$;
137 49 ▶ exit($exit_code || 0);
Condition50%1/2
$exit_code || 0
l!l
049
MC/DC0%0/1
$exit_code || 0
$exit_code
Truth table50%1/2
A
A= $exit_code
Aresult
11
00
138 }
139  
140 sub signal_all_children {
141 8 ▶ my ($self, $sig) = @_;
Subroutine signal_all_children: called CC=2 SCAR=6.9
Pod documented
142 8 foreach my $pid (sort keys %{$self->{worker_pids}}) {
143 99 kill $sig, $pid;
144 }
145 }
146  
147 sub num_workers {
148 1092 ▶ my $self = shift;
Subroutine num_workers: called CC=1 SCAR=0.0
Pod undocumented
149 1092 return scalar keys %{$self->{worker_pids}};
150 }
151  
152 sub _decide_action {
153 888 ▶ my $self = shift;
Subroutine _decide_action: called CC=2 SCAR=6.9
154 888 ▶ return 1 if $self->num_workers < $self->max_workers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->num_workers < $self->max_workers 838 50 888
155 50 return 0;
156 }
157  
158 sub _on_child_reap {
159 73 ▶ my ($self, $exit_pid, $status) = @_;
Subroutine _on_child_reap: called CC=2 SCAR=6.9
160 73 my $cb = $self->on_child_reap;
161 73 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 40 33 73
162 40 eval {
163 40 $cb->($self, $exit_pid, $status);
164 };
165 # XXX - hmph, what to do here?
166 }
167 }
168  
169 # runs delayed tasks (if any) and returns how many seconds to wait
170 sub _handle_delayed_task {
171 885 ▶ my $self = shift;
Subroutine _handle_delayed_task: called CC=3 SCAR=16.0
172 885 while (1) {
173 return undef
174 885 ▶ unless $self->{delayed_task};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"delayed_task"} 885 0 885
175 0 my $timeleft = $self->{delayed_task_at} - Time::HiRes::time();
176 0 ▶ return $timeleft
Branch: 1 branch
BranchTrueFalseTotal
if $timeleft > 0 0 0 0
177 if $timeleft > 0;
178 0 $self->{delayed_task}->($self);
179 }
180 }
181  
182 # returns [sig_to_send, interval_bet_procs] or undef for given recved signal
183 sub _action_for {
184 11 ▶ my ($self, $sig) = @_;
Subroutine _action_for: called CC=3 SCAR=11.4
185 11 ▶ my $t = $self->{trap_signals}{$sig}
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"trap_signals"}{$sig} or return undef 0 11 11
186 or return undef;
187 11 ▶ $t = [$t, 0] unless ref $t;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $t 11 0 11
188 11 return $t;
189 }
190  
191 sub wait_all_children {
192 7 ▶ my ($self, $timeout) = @_;
Subroutine wait_all_children: called CC=6 SCAR=17.9
Pod documented
193 7 $self->{_no_adjust_until} = undef;
194  
195 my $call_wait = sub {
196 53 ▶ my $blocking = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
197 53 ▶ if (my ($pid) = $self->_wait($blocking)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($pid) = $self->_wait($blocking)) 48 5 53
198 48 ▶ if (delete $self->{worker_pids}{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{"worker_pids"}{$pid}) 48 0 48
199 48 $self->_on_child_reap($pid, $?);
200 }
201 48 return $pid;
202 }
203 5 return;
204 7 };
205  
206 7 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) { } 2 5 7
207 # the strategy is to use waitpid + sleep that gets interrupted by SIGCHLD
208 # but since there is a race condition bet. waitpid and sleep, the argument
209 # to sleep should be set to a small number (and we use 1 second).
210 2 my $start_at = [Time::HiRes::gettimeofday];
211 2 ▶ while ($self->num_workers != 0 && Time::HiRes::tv_interval($start_at) < $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_workers != 0 and Time::HiRes::tv_interval($start_at) < $timeout) 15 2 17
Condition66%2/3
$self->num_workers != 0 and Time::HiRes::tv_interval($start_at) < $timeout
!ll&&!rl&&r
0215
MC/DC50%1/2
$self->num_workers != 0 and Time::HiRes::tv_interval($start_at) < $timeout
$self->num_workers != 0 Time::HiRes::tv_interval($start_at) < $timeout
Truth table66%2/3
A and B
A= $self->num_workers != 0B= Time::HiRes::tv_interval($start_at) < $timeout
ABresult
0X0
100
111
212 15 ▶ unless ($call_wait->(0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$call_wait(0)) 5 10 15
213 5 sleep 1;
214 }
215 }
216 } else {
217 5 ▶ while ($self->num_workers != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_workers != 0) 38 5 43
218 38 $call_wait->(1);
219 }
220 }
221 7 return $self->num_workers;
222 }
223  
224 sub _update_spawn_delay {
225 778 ▶ my ($self, $secs) = @_;
Subroutine _update_spawn_delay: called CC=2 SCAR=7.2
226 778 ▶ $self->{_no_adjust_until} = $secs ? Time::HiRes::time() + $secs : 0;
Branch: 1 branch
BranchTrueFalseTotal
$secs ? : 0 778 778
227 }
228  
229 # wrapper function of Proc::Wait3::wait3 that executes delayed task if any. assumes wantarray == 1
230 sub _wait {
231 885 ▶ my ($self, $blocking) = @_;
Subroutine _wait: called CC=7 SCAR=19.5
232 885 ▶ if (! $blocking) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $blocking) { } 783 102 885
233 783 $self->_handle_delayed_task();
234 783 return Proc::Wait3::wait3(0);
235 } else {
236 102 my $delayed_task_sleep = $self->_handle_delayed_task();
237 my $delayed_fork_sleep =
238 $self->_decide_action() > 0 && defined $self->{_no_adjust_until}
239 102 ▶ ? max($self->{_no_adjust_until} - Time::HiRes::time(), 0)
Branch: 1 branch
BranchTrueFalseTotal
$self->_decide_action > 0 && defined $self->{'_no_adjust_until'} ? : 0 102 102
Condition66%2/3
$self->_decide_action > 0 and defined $self->{"_no_adjust_until"}
!ll&&!rl&&r
69330
MC/DC0%0/2
$self->_decide_action > 0 and defined $self->{"_no_adjust_until"}
$self->_decide_action > 0 defined $self->{"_no_adjust_until"}
Truth table66%2/3
A and B
A= $self->_decide_action > 0B= defined $self->{"_no_adjust_until"}
ABresult
0X0
100
111
240 : undef;
241 102 my $sleep_secs = min grep { defined $_ } (
242 $delayed_task_sleep,
243 $delayed_fork_sleep,
244 $self->_max_wait(),
245 );
246 102 ▶ if (defined $sleep_secs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sleep_secs) { } 45 57 102
247 # wait max sleep_secs or until signalled
248 45 select(undef, undef, undef, $sleep_secs);
249 45 ▶ if (my @r = Proc::Wait3::wait3(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@r) = Proc::Wait3::wait3(0)) 10 35 45
250 10 return @r;
251 }
252 } else {
253 57 ▶ if (my @r = Proc::Wait3::wait3(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@r) = Proc::Wait3::wait3(1)) 53 4 57
254 53 return @r;
255 }
256 }
257 39 return +();
258 }
259 }
260  
261 sub _max_wait {
262 57 ▶ return undef;
Subroutine _max_wait: called CC=1 SCAR=0.0
263 }
264  
265 56 1;
266  
267 __END__
« Prefork/SpareWorkers/Scoreboard.pm ↑ Summary Prefork/SpareWorkers.pm »