Prefork/SpareWorkers/Scoreboard.pm

filter: Statementstmt 74.3% 61 / 82 Branchbran 30.4% 14 / 46 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 5 totaltotal 59.0% 91 / 154 SCAR 42.5
CC
25
Cov
58%
child_start
72.0
set_status
20.0
clear_child
7.4
CRAP 69.8
↑ Summary Prefork.pm »
1 package Parallel::Prefork::SpareWorkers::Scoreboard;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scope::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Signal::Mask;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Parallel::Prefork::SpareWorkers qw(:status);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # format of each slot: STATUS_CHAR PID(15bytes,left-aligned) "\n"
15 1 ▶use constant SLOT_SIZE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant EMPTY_SLOT => STATUS_NEXIST . (' ' x (SLOT_SIZE - 2)) . "\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 sub _format_slot {
18 0 ▶ my ($state, $pid) = @_;
Subroutine _format_slot: not called CC=1 SCAR=6.9
19 0 substr($state, 0, 1) . sprintf "%-14d\n", $pid;
20 }
21  
22 sub new {
23 1 ▶ my ($klass, $filename, $max_workers) = @_;
Subroutine new: called CC=4 SCAR=14.3
Pod undocumented
24 # create scoreboard file
25 1 ▶ $filename ||= File::Temp::tempdir(CLEANUP => 1) . '/scoreboard';
Condition50%1/2
$filename ||= File::Temp::tempdir("CLEANUP", 1) . '/scoreboard'
l!l
01
MC/DC0%0/1
$filename ||= File::Temp::tempdir("CLEANUP", 1) . '/scoreboard'
$filename
Truth table50%1/2
A
A= $filename
Aresult
11
00
26 1 ▶ sysopen my $fh, $filename, O_RDWR | O_CREAT | O_EXCL
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $filename, 194 or die "failed to create scoreboard file:${filename}:$!" 0 1 1
27 or die "failed to create scoreboard file:$filename:$!";
28 1 my $wlen = syswrite $fh, EMPTY_SLOT x $max_workers;
29 1 ▶ die "failed to initialize scoreboad file:$filename:$!"
Branch: 1 branch
BranchTrueFalseTotal
unless $wlen == 16 * $max_workers 0 1 1
30 unless $wlen == SLOT_SIZE * $max_workers;
31 1 my $self = bless {
32 filename => $filename,
33 fh => $fh,
34 max_workers => $max_workers,
35 slot => undef,
36 }, $klass;
37 1 $self;
38 }
39  
40 sub get_statuses {
41 99 ▶ local ($Signal::Mask{CHLD}, $Signal::Mask{TERM}, $Signal::Mask{INT}) = (1, 1, 1);
Subroutine get_statuses: called CC=4 SCAR=14.5
Pod undocumented
42  
43 99 my $self = shift;
44 99 ▶ sysseek $self->{fh}, 0, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
sysseek $self->{"fh"}, 0, 0 or die "seek failed:$!" 0 99 99
45 or die "seek failed:$!";
46 sysread($self->{fh}, my $sb, $self->{max_workers} * SLOT_SIZE)
47 99 ▶ == $self->{max_workers} * SLOT_SIZE
Branch: 1 branch
BranchTrueFalseTotal
sysread($self->{"fh"}, my $sb, $self->{"max_workers"} * 16) == $self->{"max_workers"} * 16 or die "failed to read status:$!" 0 99 99
48 or die "failed to read status:$!";
49 my @s = map {
50 99 ▶ $_ =~ /^(.)/ ? ($1) : ()
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^(.)/ ? : 990 0 990
51 } split /\n/, $sb;
52 }
53  
54 sub clear_child {
55 10 ▶ local ($Signal::Mask{CHLD}, $Signal::Mask{TERM}, $Signal::Mask{INT}) = (1, 1, 1);
Subroutine clear_child: called CC=7 SCAR=20.1
Pod undocumented
56  
57 10 my ($self, $pid) = @_;
58 10 my $lock = $self->_lock_file;
59 10 ▶ sysseek $self->{fh}, 0, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
sysseek $self->{"fh"}, 0, 0 or die "seek failed:$!" 0 10 10
60 or die "seek failed:$!";
61 10 ▶ for (my $slot = 0; $slot < $self->{max_workers}; $slot++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot < $self->{"max_workers"}) 55 0 55
62 55 my $rlen = sysread($self->{fh}, my $data, SLOT_SIZE);
63 55 ▶ die "unexpected eof while reading scoreboard file:$!"
Branch: 1 branch
BranchTrueFalseTotal
unless $rlen == 16 0 55 55
64 unless $rlen == SLOT_SIZE;
65 55 ▶ if ($data =~ /^.$pid[ ]*\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^.${pid}[ ]*\n$/) 10 45 55
66 # found
67 10 ▶ sysseek $self->{fh}, SLOT_SIZE * $slot, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
sysseek $self->{"fh"}, 16 * $slot, 0 or die "seek failed:$!" 0 10 10
68 or die "seek failed:$!";
69 10 my $wlen = syswrite $self->{fh}, EMPTY_SLOT;
70 10 ▶ die "failed to clear scoreboard file:$self->{filename}:$!"
Branch: 1 branch
BranchTrueFalseTotal
unless $wlen == 16 0 10 10
71 unless $wlen == SLOT_SIZE;
72 10 last;
73 }
74 }
75 }
76  
77 sub child_start {
78 0 ▶ local ($Signal::Mask{CHLD}, $Signal::Mask{TERM}, $Signal::Mask{INT}) = (1, 1, 1);
Subroutine child_start: not called CC=8 SCAR=42.8
Pod undocumented
79  
80 0 my $self = shift;
81 die "child_start cannot be called twite"
82 0 ▶ if defined $self->{slot};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"slot"} 0 0 0
83 close $self->{fh}
84 0 ▶ or die "failed to close scoreboard file:$!";
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"fh"} or die "failed to close scoreboard file:$!" 0 0 0
85 0 ▶ sysopen $self->{fh}, $self->{filename}, O_RDWR
Branch: 1 branch
BranchTrueFalseTotal
sysopen $self->{"fh"}, $self->{"filename"}, 2 or die qq[failed to create scoreboard file:$self->{"filename"}:$!] 0 0 0
86 or die "failed to create scoreboard file:$self->{filename}:$!";
87 0 my $lock = $self->_lock_file;
88 0 ▶ for ($self->{slot} = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"slot"} < $self->{"max_workers"}) 0 0 0
89 $self->{slot} < $self->{max_workers};
90 $self->{slot}++) {
91 0 my $rlen = sysread $self->{fh}, my $data, SLOT_SIZE;
92 0 ▶ die "unexpected response from sysread:$rlen, expected @{[SLOT_SIZE]}:$!"
Branch: 1 branch
BranchTrueFalseTotal
if $rlen != 16 0 0 0
93 if $rlen != SLOT_SIZE;
94 0 ▶ if ($data =~ /^.[ ]+\n$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^.[ ]+\n$/o) 0 0 0
95 0 last;
96 }
97 }
98 die "no empty slot in scoreboard"
99 0 ▶ if $self->{slot} >= $self->{max_workers};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"slot"} >= $self->{"max_workers"} 0 0 0
100 0 $self->set_status(STATUS_IDLE);
101 }
102  
103 sub set_status {
104 0 ▶ my ($self, $status) = @_;
Subroutine set_status: not called CC=4 SCAR=30.0
Pod undocumented
105 die "child_start not called?"
106 0 ▶ unless defined $self->{slot};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"slot"} 0 0 0
107 0 ▶ sysseek $self->{fh}, $self->{slot} * SLOT_SIZE, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
sysseek $self->{"fh"}, $self->{"slot"} * 16, 0 or die "seek failed:$!" 0 0 0
108 or die "seek failed:$!";
109 0 my $wlen = syswrite $self->{fh}, _format_slot($status, $$);
110 0 ▶ die "failed to write status into scoreboard:$!"
Branch: 1 branch
BranchTrueFalseTotal
unless $wlen == 16 0 0 0
111 unless $wlen == SLOT_SIZE;
112 }
113  
114 sub _lock_file {
115 10 ▶ my $self = shift;
Subroutine _lock_file: called CC=2 SCAR=7.1
116 10 my $fh = $self->{fh};
117 10 ▶ flock $fh, LOCK_EX
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "failed to lock scoreboard file:$!" 0 10 10
118 or die "failed to lock scoreboard file:$!";
119 return Scope::Guard->new(
120 sub {
121 10 ▶ flock $fh, LOCK_UN
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 8 or die "failed to unlock scoreboard file:$!" 0 10 10
122 or die "failed to unlock scoreboard file:$!";
123 },
124 10 );
125 }
126  
127 1 1;
↑ Summary Prefork.pm »