| 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'
MC/DC0%0/1
$filename ||= File::Temp::tempdir("CLEANUP", 1) . '/scoreboard'
$filename
Truth table50%1/2
A
A= $filename
| |||||||||||||
| 26 | 1 | ▶ | sysopen my $fh, $filename, O_RDWR | O_CREAT | O_EXCL | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 48 | or die "failed to read status:$!"; | ||||||||||||
| 49 | my @s = map { | ||||||||||||
| 50 | 99 | ▶ | $_ =~ /^(.)/ ? ($1) : () | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 60 | or die "seek failed:$!"; | ||||||||||||
| 61 | 10 | ▶ | for (my $slot = 0; $slot < $self->{max_workers}; $slot++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 55 | my $rlen = sysread($self->{fh}, my $data, SLOT_SIZE); | |||||||||||
| 63 | 55 | ▶ | die "unexpected eof while reading scoreboard file:$!" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | unless $rlen == SLOT_SIZE; | ||||||||||||
| 65 | 55 | ▶ | if ($data =~ /^.$pid[ ]*\n$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | # found | ||||||||||||
| 67 | 10 | ▶ | sysseek $self->{fh}, SLOT_SIZE * $slot, SEEK_SET | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 83 | close $self->{fh} | ||||||||||||
| 84 | 0 | ▶ | or die "failed to close scoreboard file:$!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | ▶ | sysopen $self->{fh}, $self->{filename}, O_RDWR | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 93 | if $rlen != SLOT_SIZE; | ||||||||||||
| 94 | 0 | ▶ | if ($data =~ /^.[ ]+\n$/o) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | last; | |||||||||||
| 96 | } | ||||||||||||
| 97 | } | ||||||||||||
| 98 | die "no empty slot in scoreboard" | ||||||||||||
| 99 | 0 | ▶ | if $self->{slot} >= $self->{max_workers}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 107 | 0 | ▶ | sysseek $self->{fh}, $self->{slot} * SLOT_SIZE, SEEK_SET | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 122 | or die "failed to unlock scoreboard file:$!"; | ||||||||||||
| 123 | }, | ||||||||||||
| 124 | 10 | ); | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 1 | 1; | |||||||||||