Mnet/Batch.pm

filter: Statementstmt 88.8% 127 / 143 Branchbran 71.9% 59 / 82 Conditioncond 55.2% 21 / 38 MC/DCmcdc 13.0% 3 / 23 Subroutinesub 100% 16 / 16 Podpod 50.0% 1 / 2 totaltotal 74.6% 227 / 304 CC 57 SCAR 30.0
CC
57
Cov
79%
_fork_ok_idle
36.1
fork
30.9
_fork_ok
10.1
CRAP 20.0
« Mnet/Log.pm ↑ Summary Mnet/T.pm »
1 package Mnet::Batch;
2  
3 =head1 NAME
4  
5 Mnet::Batch - Concurrently process a list of command line options
6  
7 =head1 SYNOPSIS
8  
9 # usually combined with Mnet::Opts::Cli
10 use Mnet::Batch;
11 use Mnet::Opts::Cli;
12  
13 # define --sample cli option
14 Mnet::Opts::Cli::define({
15 getopt => "sample=s",
16 help_tip => "set to input string",
17 });
18  
19 # usually cli options are read before calling Mnet::Batch::fork()
20 my $cli = Mnet::Opts::Cli->new;
21  
22 # read command line options, fork children if in --batch mode
23 # exit --batch parent process when finished forking children
24 $cli = Mnet::Batch::fork($cli);
25 exit if not $cli;
26  
27 # code below runs for batch child workers and non-batch executions
28 print "sample = $cli->{sample}\n";
29  
30 =head1 DESCRIPTION
31  
32 Mnet::Batch can be used in a script to concurrently process a --batch list
33 of command option lines.
34  
35 For example, you might run a script sequentially over and over, like this:
36  
37 script.pl --sample 1
38 script.pl --sample 2a
39 script.pl --sample ...
40  
41 Or use Mnet::Batch which allows you to process a list of option command
42 lines after loading the script once, like this:
43  
44 echo '
45 --sample 1
46 --sample 2a
47 --sample ...
48 ' | script.pl --batch /dev/stdin
49  
50 In the above example the script accepts a --batch list of command option lines
51 and forks a child worker process for each line in the list. The --batch list
52 option can be set to a file, a named pipe, or /dev/stdin as above.
53  
54 By default --batch list lines are processed one at a time unless linux
55 /proc/stat is detected, in which case batch command lines are processed with
56 as many concurrent processes as possible without overutilizing the cpu. The
57 --batch-idle and --batch-min options can be used to adjust this. On systems
58 without /proc/stat support use --batch-min to set the number of concurrent
59 processes.
60  
61 Note that a script using this module will throw an error when it ends if
62 the L<Mnet::Opts::Cli> new method was used to parse command line arguments
63 and the --batch option is set and the Mnet::Batch::fork function was never
64 called.
65  
66 Refer also to the documentation for the Mnet::Batch::fork function in this
67 module for more information.
68  
69 =head1 FUNCTIONS
70  
71 Mnet::Batch implements the functions listed below.
72  
73 =cut
74  
75 # required modules
76 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 23 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 23 ▶use Mnet::Log::Conditional qw( DEBUG INFO WARN FATAL NOTICE );
Subroutine BEGIN: called CC=1 SCAR=0.0
81 23 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 23 ▶use Mnet::Opts::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 23 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 23 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86  
87  
88 # init global variables and cli options for this module
89 # $fork_called used by end block to warn if Mnet::Batch::fork was not called
90 # $def used to set default options, that may or may not be set via cli
91 23 ▶INIT {
Subroutine INIT: called CC=4 SCAR=13.9
92 23 my $fork_called = undef;
93 23 our $def = { batch_idle => 10, batch_min => 1 };
94 Mnet::Opts::Cli::define({
95 getopt => 'batch=s',
96 help_tip => 'process command lines from file',
97 help_text => '
98 use --batch to process a list of command lines from specified file
99 batch list may be read from standard input by specifying /dev/stdin
100 for a dir try: find /dir -name *.test | sed "s/^/--replay /" | ...
101 children are --silent, warnings are issued for child exit errors
102 use --batch-min to set number of concurrent batch processes
103 use --batch-idle to set percentage of idle cpu to target
104 refer to perldoc Mnet::Batch for more information
105 ',
106 23 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 16 7 23
107 Mnet::Opts::Cli::define({
108 getopt => 'batch-idle=i',
109 default => $def->{batch_idle},
110 help_hide => 1,
111 help_tip => 'set percentage of idle cpu to target',
112 help_text => '
113 default is to fork child processes until idle cpu is at 10%
114 currently linux /proc/stat is supported to determine idle cpu
115 otherwise --batch-min controls count of concurrent children
116 set --batch-idle 100 to limit maximum children to --batch-min
117 refer to --help batch and --help batch-min for more info
118 refer also to perldoc Mnet::Batch for more information
119 ',
120 23 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};;
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 16 7 23
121 Mnet::Opts::Cli::define({
122 getopt => 'batch-min=i',
123 default => $def->{batch_min},
124 help_hide => 1,
125 help_tip => 'set number of concurrent processes',
126 help_text => '
127 default is to allow a minimum of 1 batch child processes
128 this may be increased on systems not having --batch-idle support
129 refer to --help batch and --help --batch-idle for more info
130 refer also to perldoc Mnet::Batch for more information
131 ',
132 23 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};;
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 16 7 23
133 }
134  
135  
136  
137 23 ▶sub fork {
Subroutine fork: called CC=27 SCAR=34.3
Pod documented
138  
139 =head2 Mnet::Batch::fork
140  
141 \%child_opts = Mnet::Batch::fork(\%opts)
142 or (\%child_opts, @child_extras) = Mnet::Batch::fork(\%opts)
143  
144 The Mnet::Batch::fork function requires an input opts hash ref containing at
145 least a 'batch' key. Input can be an L<Mnet::Opts::Cli> object.
146  
147 The returned child opts hash ref will contain settings from the input opts hash
148 overlaid with options from the current batch command options line. Extra args
149 from batch command option lines are also returned if called in list context.
150  
151 The returned child opts hash ref will be undefined for the batch parent process
152 when the parent process is finished.
153  
154 my ($cli, @extras) = Mnet::Opts::Cli->new;
155 ($cli, @extras) = Mnet::Batch::fork($cli);
156 exit if not defined $cli;
157 # child processing continues...
158  
159 Also note that this function can be called by scripts that are not using the
160 L<Mnet::Opts::Cli> module to parse command line options. In this case the
161 returned child_opts value will be a scalar containing the input batch line,
162 as in the following example:
163  
164 ( echo "line = 1"; echo "line = 2" ) | perl -e '
165 use Mnet::Batch
166 my $line = Mnet::Batch::fork({ batch => "/dev/stdin" });
167 exit if not defined $line;
168 die "child should have line set" if $line !~ /^line =/
169 '
170  
171 Refer also to the SYNOPSIS section of this perldoc for more information.
172  
173 =cut
174  
175 # read input opts hash ref arg
176 23 ▶ my $opts = shift // croak("missing opts arg");
Condition50%1/2
shift() // croak("missing opts arg")
l!l
230
MC/DC0%0/1
shift() // croak("missing opts arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
177  
178 # set global fork_called var true for end block warnings
179 # end block warns if --batch opt is set and Mnet::Batch::fork not called
180 23 $Mnet::Batch::fork_called = 1;
181  
182 # return input options if --batch option is not set
183 # this means we are running the script normally - not in batch mode
184 23 ▶ if (not $opts->{batch}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"batch"}) 3 20 23
185 3 DEBUG("fork returning input opts, batch option not set");
186 3 return $opts;
187 }
188  
189 # abort with error if --record is set for batch parent
190 # this would result in all children trying to save to the same file
191 FATAL("invalid non-null --record with --batch on parent command line")
192 20 ▶ if defined $opts->{record} and $opts->{record} ne "";
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"record"} and $opts->{"record"} ne "" 0 20 20
Condition33%1/3
defined $opts->{"record"} and $opts->{"record"} ne ""
!ll&&!rl&&r
2000
MC/DC0%0/2
defined $opts->{"record"} and $opts->{"record"} ne ""
defined $opts->{"record"} $opts->{"record"} ne ""
Truth table33%1/3
A and B
A= defined $opts->{"record"}B= $opts->{"record"} ne ""
ABresult
0X0
100
111
193  
194 # abort with error if --replay would be set the same for all batch children
195 # to be safe, so multiple children don't write same file w/null --record
196 FATAL("invalid --replay with --batch on parent command line")
197 20 ▶ if defined $opts->{replay};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"replay"} 0 20 20
198  
199 # pause Mnet::Tee accumulation of outputs for batch parent while forking
200 # we don't want batch parent polluting child Mnet::Test outputs
201 # we unpause this for children after they fork
202 20 ▶ Mnet::Tee::test_pause() if $INC{"Mnet/Test.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Test.pm"} 11 9 20
203  
204 # read all lines of --batch list file
205 20 my @batch_lines = ();
206 20 ▶ open(my $fh, "<", $opts->{batch}) or FATAL("fork batch $opts->{batch}, $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $opts->{"batch"} or FATAL(qq[fork batch $opts->{"batch"}, $!]) 20 0 20
207 20 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 72 20 92
208 72 chomp(my $line = $_);
209 72 $line =~ s/^\s*#.*//g;
210 72 ▶ next if $line !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 34 38 72
211 38 push @batch_lines, $line;
212 }
213 20 close $fh;
214  
215 # output count of batch lines read
216 20 NOTICE("fork read ".scalar(@batch_lines)." lines from $opts->{batch}");
217  
218 # use hash ref to note the batch line associated with each forked child pid
219 # used in child sig handler to output batch line for reaped child pid
220 20 my $pid_batch_lines = {};
221  
222 # init hash ref used to track when it is ok to fork, refer to _fork_ok()
223 my $fork_data = {
224 child_count => 0,
225 child_min => $opts->{batch_min} // $Mnet::Batch::def->{batch_min},
226 idle_target => $opts->{batch_idle} // $Mnet::Batch::def->{batch_idle},
227 20 ▶ };
Condition66%4/6
$opts->{"batch_min"} // $Mnet::Batch::def->{"batch_min"}
l!l&&r!l&&!r
1460
$opts->{"batch_idle"} // $Mnet::Batch::def->{"batch_idle"}
l!l&&r!l&&!r
1460
MC/DC0%0/2
$opts->{"batch_min"} // $Mnet::Batch::def->{"batch_min"}
$opts->{"batch_min"} $Mnet::Batch::def->{"batch_min"}
MC/DC0%0/2
$opts->{"batch_idle"} // $Mnet::Batch::def->{"batch_idle"}
$opts->{"batch_idle"} $Mnet::Batch::def->{"batch_idle"}
Truth table66%2/3
A // B
A= $opts->{"batch_min"}B= $Mnet::Batch::def->{"batch_min"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $opts->{"batch_idle"}B= $Mnet::Batch::def->{"batch_idle"}
ABresult
1X1
011
000
228  
229 # prepare signal handler to properly wait on forked child processes
230 # otherwise these would remain in process table as zombies
231 # warn when reaped children exited with an error
232 19 ▶ $SIG{CHLD} = sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
233 19 ▶ while ((my $child = waitpid(-1, &POSIX::WNOHANG)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $child = waitpid(-1, &POSIX::WNOHANG)) > 0) 19 19 38
234 19 $fork_data->{child_count}--;
235 19 my ($error, $sig, $dump) = ($? >> 8, $? & 127, $? & 128);
236 19 my $exit_status = "exit with error $error, sig $sig, dump $dump";
237 19 ▶ my $child_batch_line = $pid_batch_lines->{$child} // "";
Condition50%1/2
$pid_batch_lines->{$child} // ""
l!l
190
MC/DC0%0/1
$pid_batch_lines->{$child} // ""
$pid_batch_lines->{$child}
Truth table50%1/2
A
A= $pid_batch_lines->{$child}
Aresult
11
00
238 19 $exit_status .= ", $0 $child_batch_line";
239 19 ▶ if ($error or $sig or $dump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error or $sig or $dump) { } 2 17 19
Condition66%4/6
$error or $sig or $dump
l!l&&r!l&&!r
2017
$error or $sig
l!l&&r!l&&!r
2017
MC/DC33%1/3
$error or $sig or $dump
$error $sig $dump
Truth table50%2/4
A or B or C
A= $errorB= $sigC= $dump
ABCresult
1XX1
01X1
0011
0000
240 2 WARN("fork reaped child pid $child, $!, $exit_status");
241 } else {
242 17 NOTICE("fork reaped child pid $child, $exit_status");
243 }
244 }
245 20 };
246  
247 # note error flag before we start forking
248 # this is passed to Mnet::Log::batch_fork for forked children
249 # this way a late child doesn't inherit a warning of a failed early child
250 20 my $error_prefork = undef;
251 20 ▶ $error_prefork = Mnet::Log::error() if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 20 0 20
252  
253 # loop through batch lines, forking a child worker process for each line
254 20 foreach my $batch_line (@batch_lines) {
255  
256 # wait until it is safe to fork another child
257 # refer to _fork_ok() function for more info
258 32 ▶ while (1) { last if _fork_ok($fork_data); }
Branch: 1 branch
BranchTrueFalseTotal
if _fork_ok($fork_data) 32 67880 67912
259  
260 # fork returns child pid to parent proc, 0 to child, "undef" on failure
261 32 my $pid = fork();
262  
263 # handle failure to fork
264 32 ▶ if (not defined $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 32 32
elsif ($pid == 0) { } 13 19 32
265 0 warn "fork failed forking child, $!";
266 0 sleep 10;
267  
268 # child process returns to continue execution, return forked child opts
269 # children load Mnet::Opts::Set::Silent, quiet/silent opts still work
270 # Mnet::Log::batch_fork resets script start time for forked child
271 # Mnet::Opts::Cli::batch_fork parses cli opts and batch child opts
272 # what is returned depends on context Mnet::Batch::fork() was called
273 # batch_line is returned if script doesn't use Mnet::Opts::Cli
274 # otherwise returns child opts and extras depending on context
275 # Mnet::Tee output is reset and unpaused for child for Mnet::Test
276 } elsif ($pid == 0) {
277 13 Mnet::Opts::Set::enable("silent");
278 13 ▶ Mnet::Log::batch_fork($error_prefork) if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 13 0 13
279 13 ▶ Mnet::Tee::batch_fork() if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 7 6 13
280 13 ▶ Mnet::Tee::test_unpause() if $INC{"Mnet/Test.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Test.pm"} 7 6 13
281 13 ▶ if (not $INC{"Mnet/Opts/Cli.pm"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $INC{'Mnet/Opts/Cli.pm'}) { } 4 9 13
elsif (wantarray) { } 2 7 9
282 4 return $batch_line;
283 } elsif (wantarray) {
284 2 my ($child_opts, @child_extras)
285 = Mnet::Opts::Cli::batch_fork($batch_line);
286 2 return ($child_opts, @child_extras);
287 } else {
288 7 my $child_opts = Mnet::Opts::Cli::batch_fork($batch_line);
289 7 return $child_opts;
290 }
291  
292 # output pid of child that we just forked
293 # parent process tracks batch line for each child pid
294 # parent process increments count of child workers
295 } else {
296 19 NOTICE("fork forked child pid $pid, $0 $batch_line");
297 19 $pid_batch_lines->{$pid} = $batch_line;
298 19 $fork_data->{child_count}++;
299 }
300  
301 # continue loop to process batch list with forked child workers
302 }
303  
304 # wait for remaining child processes to finish, log how many are waiting
305 7 my $wait_count = $fork_data->{child_count} + 1;
306 7 ▶ while ($fork_data->{child_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fork_data->{"child_count"}) 7 7 14
307 7 ▶ if ($fork_data->{child_count} < $wait_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fork_data->{"child_count"} < $wait_count) 7 0 7
308 7 $wait_count = $fork_data->{child_count};
309 7 NOTICE("fork waiting on $wait_count child processes");
310 }
311 7 sleep 1;
312 }
313  
314 # output that parent finished processing batch list, along with idle info
315 7 my $info = "processed ".scalar(@batch_lines)." children";
316 7 ▶ $info .=", max $fork_data->{stat_max} concurrent" if $fork_data->{stat_max};
Branch: 1 branch
BranchTrueFalseTotal
if $fork_data->{"stat_max"} 7 0 7
317 7 ▶ if ($fork_data->{stat_idle_c} and $fork_data->{stat_idle_s}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fork_data->{"stat_idle_c"} and $fork_data->{"stat_idle_s"}) 0 7 7
Condition33%1/3
$fork_data->{"stat_idle_c"} and $fork_data->{"stat_idle_s"}
!ll&&!rl&&r
700
MC/DC0%0/2
$fork_data->{"stat_idle_c"} and $fork_data->{"stat_idle_s"}
$fork_data->{"stat_idle_c"} $fork_data->{"stat_idle_s"}
Truth table33%1/3
A and B
A= $fork_data->{"stat_idle_c"}B= $fork_data->{"stat_idle_s"}
ABresult
0X0
100
111
318 0 my $avg = int($fork_data->{stat_idle_s}/$fork_data->{stat_idle_c});
319 0 $info .= ", average $avg\% idle cpu";
320 }
321 7 NOTICE("fork $info");
322  
323 # finished Mnet::Batch::fork() function, return undef for parent
324 7 DEBUG("fork parent finished");
325 7 return undef;
326 }
327  
328  
329  
330 4 ▶sub fork_called {
Subroutine fork_called: called CC=1 SCAR=0.0
Pod undocumented
331  
332 # $boolean = Mnet::Batch::fork_called()
333 # purpose: return true if Mnet::Batch::fork function was already called
334 # note: use to check if batch mode script has past point of forking chilren
335 # example: $pre_fork = $INC{'Mnet/Batch.pm'} and not Mnet::Batch::fork_called()
336  
337 # return global fork_called flag
338 4 return $Mnet::Batch::fork_called;
339  
340 }
341  
342  
343  
344 67912 ▶sub _fork_ok {
Subroutine _fork_ok: called CC=10 SCAR=23.2
345  
346 # $ok = _fork_ok(\%fork_data)
347 # purpose: return true if it is ok to fork another child process
348 # \%fork_data: caller should init child_count=>0, child_min=>1, idle_target=>10
349 # $ok: set true if ok to fork, otherwise wait in a loop until it is ok
350  
351 # read input fork_data hash, the following keys may be used:
352 # child_count => set to count of currently running forked children
353 # child_max => absolute upper child limit, calculated from cpu_count
354 # child_min => set from caller, may be adjusted for /proc/stat idle cpu
355 # cpu_count => set from /proc/stat, indicates /proc/stat is available
356 # idle_target => set from caller to percent cpu utilization to leave idle
357 # last_idle => set to count of idle ticks from last /proc/stat sample
358 # last_ticks => set to total cpu ticks from last /proc/stat sample
359 # last_time => set to unix time that /proc/stat was last sampled
360 # stat_idle_c => count of idle cpu samples, used to get average idle cpu
361 # stat_idle_s => sum of idle cpu samples, used to get average idle cpu
362 # stat_max => highest number of concurrent children during execution
363 67912 ▶ my $fork_data = shift // die "missing fork_data arg";
Condition50%1/2
shift() // die("missing fork_data arg")
l!l
679120
MC/DC0%0/1
shift() // die("missing fork_data arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
364  
365 # init cpu_count from /proc/stat the first time we are called
366 # this will be used later to adjust child_min based on idle cpu
367 # cpu_count is set undef if /proc/stat is not available
368 # child_max is set based on count of cpu cores
369 67912 ▶ if (not exists $fork_data->{cpu_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fork_data->{"cpu_count"}) 20 67892 67912
370 20 $fork_data->{cpu_count} = undef;
371 20 ▶ if (open(my $fh, "<", "/proc/stat")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "/proc/stat") 20 0 20
372 20 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 420 20 440
373 420 ▶ $fork_data->{cpu_count}++ if $_ =~ /^cpu\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^cpu\d+/ 260 160 420
374 }
375 20 close $fh;
376 20 $fork_data->{child_max} = $fork_data->{cpu_count} * 64;
377 20 my $cpu_cores = "detected $fork_data->{cpu_count} cpu cores";
378 20 my $child_max = "child max $fork_data->{child_max}";
379 20 my $idle_target = "idle cpu target $fork_data->{idle_target}\%";
380 20 NOTICE("fork $cpu_cores, setting $child_max and $idle_target");
381 }
382 }
383  
384 # check idle cpu and adjust child_min if /proc/stat was accessible
385 67912 ▶ _fork_ok_idle($fork_data) if $fork_data->{cpu_count};
Branch: 1 branch
BranchTrueFalseTotal
if $fork_data->{"cpu_count"} 67912 0 67912
386  
387 # maintain stat of greatest number of max children concurrently running
388 $fork_data->{stat_max} = $fork_data->{child_min}
389 if not defined $fork_data->{stat_max}
390 67912 ▶ or $fork_data->{child_min} > $fork_data->{stat_max};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fork_data->{"stat_max"} or $fork_data->{"child_min"} > $fork_data->{"stat_max"} 20 67892 67912
Condition66%2/3
not defined $fork_data->{"stat_max"} or $fork_data->{"child_min"} > $fork_data->{"stat_max"}
l!l&&r!l&&!r
20067892
MC/DC50%1/2
not defined $fork_data->{"stat_max"} or $fork_data->{"child_min"} > $fork_data->{"stat_max"}
not defined $fork_data->{"stat_max"} $fork_data->{"child_min"} > $fork_data->{"stat_max"}
Truth table66%2/3
not(A) or B
A= not defined $fork_data->{"stat_max"}B= $fork_data->{"child_min"} > $fork_data->{"stat_max"}
ABresult
1X1
011
000
391  
392 # return true if count of current child is less than maximum children
393 67912 ▶ return 1 if $fork_data->{child_count} < $fork_data->{child_min};
Branch: 1 branch
BranchTrueFalseTotal
if $fork_data->{"child_count"} < $fork_data->{"child_min"} 32 67880 67912
394  
395 # sleep a minimal amount or time
396 # this gives other procs a chance to run
397 67880 Time::HiRes::usleep(1);
398  
399 # finished _fork_ok, return false
400 # we're not ready to fork another child yet
401 67880 return undef;
402 }
403  
404  
405  
406 67912 ▶sub _fork_ok_idle {
Subroutine _fork_ok_idle: called CC=12 SCAR=35.9
407  
408 # _fork_ok_idle(\%fork_data)
409 # purpose: adjust child_min in fork_data hash ref based on cpu utilization
410 # \%fork_data: refer to the _fork_ok() function for more information
411  
412 # read input fork_data hash
413 67912 ▶ my $fork_data = shift // die "missing fork_data arg";
Condition50%1/2
shift() // die("missing fork_data arg")
l!l
679120
MC/DC0%0/1
shift() // die("missing fork_data arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
414  
415 # return if three seconds hasn't gone by since last cpu utilization check
416 67912 ▶ return if $fork_data->{last_time} and $fork_data->{last_time} + 3 > time;
Branch: 1 branch
BranchTrueFalseTotal
if $fork_data->{"last_time"} and $fork_data->{"last_time"} + 3 > time 67892 20 67912
Condition66%2/3
$fork_data->{"last_time"} and $fork_data->{"last_time"} + 3 > time
!ll&&!rl&&r
20067892
MC/DC50%1/2
$fork_data->{"last_time"} and $fork_data->{"last_time"} + 3 > time
$fork_data->{"last_time"} $fork_data->{"last_time"} + 3 > time
Truth table66%2/3
A and B
A= $fork_data->{"last_time"}B= $fork_data->{"last_time"} + 3 > time
ABresult
0X0
100
111
417  
418 # read first line of /proc/stat, with overall cpu utilization
419 # /proc/stat counts cpu ticks, typically 1/100 of a second but can vary
420 20 ▶ open(my $fh, "<", "/proc/stat") or FATAL("error opening /proc/stat, $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/stat" or FATAL("error opening /proc/stat, $!") 20 0 20
421 20 my $cpu_stats = <$fh>;
422 20 close $fh;
423  
424 # note current idle and total cpu ticks, typically 1/100 sec but can vary
425 20 ▶ FATAL("error parsing /proc/stat, $cpu_stats")
Branch: 1 branch
BranchTrueFalseTotal
unless $cpu_stats =~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ 0 20 20
426 if $cpu_stats !~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
427 20 my ($cpu_idle, $cpu_ticks) = ($4, $1 + $2 + $3 + $4);
428  
429 # note last idle and total cpu ticks, from prior sample
430 # we return later on our first time through when these are not yet set
431 20 my $last_idle = $fork_data->{last_idle};
432 20 my $last_ticks = $fork_data->{last_ticks};
433  
434 # set data for the next call to this sub
435 20 $fork_data->{last_idle} = $cpu_idle;
436 20 $fork_data->{last_ticks} = $cpu_ticks;
437 20 $fork_data->{last_time} = time;
438  
439 # return on our first time through when prior cpu stats are not yet set
440 20 ▶ return if not $last_idle or not $last_ticks;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_idle and $last_ticks 20 0 20
Condition33%1/3
$last_idle and $last_ticks
!ll&&!rl&&r
2000
MC/DC0%0/2
$last_idle and $last_ticks
$last_idle $last_ticks
Truth table33%1/3
A and B
A= $last_idleB= $last_ticks
ABresult
0X0
100
111
441  
442 # calculate cpu idle percentage during last sample time
443 # divide idle ticks by total ticks then multiple by 100
444 # abort if no new total cpu ticks, to avoid a divide by zero error
445 0 ▶ FATAL("error processing /proc/stat") if not $cpu_ticks - $last_ticks;
Branch: 1 branch
BranchTrueFalseTotal
unless $cpu_ticks - $last_ticks 0 0 0
446 0 my $pct_idle = ($cpu_idle - $last_idle) / ($cpu_ticks - $last_ticks) * 100;
447 0 $fork_data->{stat_idle_s} += $pct_idle;
448 0 $fork_data->{stat_idle_c}++;
449  
450 # compute the percentage of cpu idle and available for more batch children
451 0 my $pct_avail = $pct_idle - $fork_data->{idle_target};
452  
453 # compute cpu utilization percentage used by each child process
454 # return on zero child_count, maybe happened due to race with $SIG{CHLD}
455 0 ▶ return if not $fork_data->{child_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $fork_data->{"child_count"} 0 0 0
456 0 my $pct_child = (100 - $pct_idle) / $fork_data->{child_count};
457  
458 # adjust child_min using percentage of available idle cpu utilization
459 # increase child_min if we have extra idle cpu, otherwise decrease
460 # adjustments are based on the estimated cpu used per child
461 0 $fork_data->{child_min} += int($pct_avail/$pct_child);
462  
463 # ensure that child_min is more than one and less than a max per cpu core
464 0 my $limit = $fork_data->{cpu_count} * $fork_data->{child_max};
465 0 ▶ $fork_data->{child_min} = $limit if $fork_data->{child_min} > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $fork_data->{"child_min"} > $limit 0 0 0
466 0 ▶ $fork_data->{child_min} = 1 if $fork_data->{child_min} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $fork_data->{"child_min"} < 1 0 0 0
467  
468 # finished _fork_ok_idle
469 0 return;
470 }
471  
472  
473  
474 # issue a warning if --batch set on cli and Mnet::Batch::fork sub never called
475 23 ▶END {
Subroutine END: called CC=3 SCAR=11.7
476 23 my $opts = Mnet::Opts::Cli::Cache::get({});
477 FATAL("cli --batch was set and Mnet::Batch::fork() was never called")
478 23 ▶ if $opts->{batch} and not $Mnet::Batch::fork_called;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"batch"} and not $Mnet::Batch::fork_called 0 23 23
Condition66%2/3
$opts->{"batch"} and not $Mnet::Batch::fork_called
!ll&&!rl&&r
9140
MC/DC0%0/2
$opts->{"batch"} and not $Mnet::Batch::fork_called
$opts->{"batch"} not $Mnet::Batch::fork_called
Truth table66%2/3
A and not(B)
A= $opts->{"batch"}B= not $Mnet::Batch::fork_called
ABresult
0X0
100
111
479 }
480  
481  
482 =head1 SEE ALSO
483  
484 L<Mnet>
485  
486 L<Mnet::Opts::Cli>
487  
488 L<Mnet::Opts::Set>
489  
490 =cut
491  
492 # normal end of package
493 23 1;
494  
« Mnet/Log.pm ↑ Summary Mnet/T.pm »