| 1 | 203 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Ekorn::Pipeline::Workers::Pool; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # The pool Ekorn::Pipeline::Workers' stages are made on: it takes values | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # tagged by its caller, and gives back what each finished with, tagged, | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # knowing nothing of order or keys. For its own stages, not callers. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 203 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 203 | ▶ | use IO::Select (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 203 | ▶ | use IO::Socket::INET (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 203 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 203 | ▶ | use Storable qw(nfreeze thaw); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 203 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 203 | ▶ | use Ekorn::Pipeline::Errors (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 203 | ▶ | use Ekorn::Pipeline::Workers::Wire qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | write_frame read_frame unplaced | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 203 | token_variable); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 203 | our $VERSION = 'v0.1.0'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | our @CARP_NOT | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 203 | = (@Ekorn::Pipeline::Errors::INTERNAL, 'Ekorn::Pipeline::Workers'); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # Reaping, for the tests' %Ekorn::Pipeline::Workers::REAPED, as kill 0 | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # can't say on Windows, where a pid is soon another process's. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # A worker killed, whose end Windows leaves waitpid nothing of (-1), | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # as Perl's kill there has already collected it, is put down as | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # reaped, by its pid: it's gone. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 203 | our %KILLED; # pids this pool killed, for _reap | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 6248 | ▶ | sub _reap ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reap: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 6248 | my $got = waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 6248 | ▶ | $got = $pid if $got == -1 && delete $KILLED{$pid}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$got == -1 and delete $Ekorn::Pipeline::Workers::Pool::KILLED{$pid}
MC/DC0%0/2
$got == -1 and delete $Ekorn::Pipeline::Workers::Pool::KILLED{$pid}
$got == -1
delete $Ekorn::Pipeline::Workers::Pool::KILLED{$pid}
Truth table33%1/3
A and B
A= $got == -1B= delete $Ekorn::Pipeline::Workers::Pool::KILLED{$pid}
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6248 | $Ekorn::Pipeline::Workers::REAPED{$pid} = $got; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 6248 | return; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 795 | ▶ | sub _kill (@pids) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_kill: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 795 | $KILLED{$_} = 1 for @pids; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 795 | kill 'KILL', @pids; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 795 | return; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # A pool of $n workers, each a new perl, started together as the | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # first value is sent (D1), each running the named block on one value | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # at a time, and talking to the parent on a socket of its own, on the | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # loopback, which select can wait on, on Windows too. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 2482 | ▶ | sub new ($class, $n, $block, %options) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | # The stage's name, and what it sends, for its messages (2026-0065's | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # D22): once_per sends keys. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 2482 | ▶ | my $stage = $options{stage} // 'in_workers'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options{"stage"} // "in_workers"
MC/DC0%0/1
$options{"stage"} // "in_workers"
$options{"stage"}
Truth table50%1/2
A
A= $options{"stage"}
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2482 | ▶ | my $what = $options{what} // 'value'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"what"} // "value"
MC/DC100%1/1
$options{"what"} // "value"
$options{"what"}
Truth table100%2/2
A
A= $options{"what"}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | return bless { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | n => $n, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | stage => $stage, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | what => $what, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | setup => { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | stage => $stage, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | what => $what, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | block => $block, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | init => $options{init}, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | init_args => $options{init_args} // [], | ||||||||||||||||||||||||||||||||||||||||||
| 68 | done => $options{done}, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 2482 | ▶ | owner => undef, | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"init_args"} // []
MC/DC100%1/1
$options{"init_args"} // []
$options{"init_args"}
Truth table100%2/2
A
A= $options{"init_args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | workers => [], # {pid, socket, busy => job or undef} | ||||||||||||||||||||||||||||||||||||||||||
| 72 | queue => [], # jobs not yet sent: {tag, seq, value, where} | ||||||||||||||||||||||||||||||||||||||||||
| 73 | select => IO::Select->new, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # Each value's number, for messages, counted from 1, as Ekorn | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # counts results and stages (D15). | ||||||||||||||||||||||||||||||||||||||||||
| 76 | seq => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Queue $value, tagged, with where it stands in its run, and send it | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # if a worker's free. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 15316 | ▶ | sub submit ($self, $tag, $value, $where) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
submit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 15316 | ▶ | $self->_start unless $self->{owner}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 15098 | push @{ $self->{queue} }, | |||||||||||||||||||||||||||||||||||||||||
| 86 | 15098 | {tag => $tag, seq => $self->{seq}++, value => $value, where => $where}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 15098 | $self->_dispatch; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 15032 | return; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # How many values are queued or with a worker. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 32748 | ▶ | sub in_flight ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_flight: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 32748 | return @{ $self->{queue} } + grep { defined $_->{busy} } @{ $self->{workers} }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # The replies that are ready within $timeout seconds (undef: wait for | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # one, if any's in flight), each [tag, kind, payload]; and the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # workers freed are sent more. A worker gone without replying is that | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # value's error, as its socket says which it had. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 32748 | ▶ | sub poll ($self, $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
poll: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 32748 | ▶ | return () unless $self->in_flight; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 30048 | my @replies; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 30048 | for my $socket ($self->{select}->can_read($timeout)) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 13902 | my($worker) = grep { $_->{socket} == $socket } @{ $self->{workers} }; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 13902 | my $job = $worker->{busy}; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 13902 | my $frame = read_frame($socket); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 13902 | ▶ | if (defined $frame) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 13826 | my(undef, $kind, $payload) = @{ thaw($frame) }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 13826 | push @replies, [$job->{tag}, $kind, $payload]; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 76 | _reap($worker->{pid}); | |||||||||||||||||||||||||||||||||||||||||
| 115 | push @replies, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 117 | $job->{tag}, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 76 | err => "$self->{stage} lost worker $worker->{pid}, which exited, " | |||||||||||||||||||||||||||||||||||||||||
| 119 | . "status $?, with $self->{what} #$job->{seq}\n", | ||||||||||||||||||||||||||||||||||||||||||
| 120 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 76 | $self->{workers} = [grep { $_ != $worker } @{ $self->{workers} }]; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 13902 | $worker->{busy} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 13902 | $self->{select}->remove($socket); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 30048 | $self->_dispatch; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 30048 | return @replies; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # However the run ended: close each worker's socket, so an idle one | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # runs done and exits; kill one still working, unless the run was | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # done. Only the process that started them ends them. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 2312 | ▶ | sub shut_down ($self, $how) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shut_down: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 2312 | ▶ | return unless $self->{owner} && $self->{owner} == $$; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"owner"} and $self->{"owner"} == $$
MC/DC50%1/2
$self->{"owner"} and $self->{"owner"} == $$
$self->{"owner"}
$self->{"owner"} == $$
Truth table66%2/3
A and B
A= $self->{"owner"}B= $self->{"owner"} == $$
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 2186 | my @workers = @{ $self->{workers} }; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 2186 | for my $worker (@workers) { | |||||||||||||||||||||||||||||||||||||||||
| 138 | _kill($worker->{pid}) | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 6172 | ▶ | if defined $worker->{busy} || ($how ne 'done' && @{ $self->{queue} }); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $worker->{"busy"} or $how ne "done" and @{$self->{"queue"};}
$how ne "done" and @{$self->{"queue"};}
MC/DC33%1/3
defined $worker->{"busy"} or $how ne "done" and @{$self->{"queue"};}
defined $worker->{"busy"}
$how ne "done"
@{$self->{"queue"};}
Truth table75%3/4
A or B and C
A= defined $worker->{"busy"}B= $how ne "done"C= @{$self->{"queue"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 6172 | close $worker->{socket}; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 2186 | _reap($_->{pid}) for @workers; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 2186 | @$self{qw(workers queue owner)} = ([], [], undef); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 2186 | $self->{select} = IO::Select->new; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 2186 | return; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | # Send queued values to idle workers. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 45146 | ▶ | sub _dispatch ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dispatch: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 45146 | my $queue = $self->{queue}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 45146 | ▶ | while (@$queue) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 37893 | my($worker) = grep { !defined $_->{busy} } @{ $self->{workers} }; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 37893 | ▶ | last unless $worker; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 14763 | my $job = shift @$queue; | |||||||||||||||||||||||||||||||||||||||||
| 156 | # Where the value stands in the run goes with it. A context or | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # an original that can't cross, such as a handle, is left | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # behind, and the block sees undef for both. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 14763 | my($seq, $value, $where) = @$job{qw(seq value where)}; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 14763 | ▶ | my $frame = eval { nfreeze([$seq, $value, $where]) } // eval { | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
(eval {
do {
nfreeze([$seq, $value, $where])
}
} // eval {
do {
nfreeze([$seq, $value, {%$where, "context", undef, "original", undef}])
}
}) // croak(qq[$self->{"stage"} can't send $self->{"what"} #$seq to a worker: ] . unplaced($@))
eval {
do {
nfreeze([$seq, $value, $where])
}
} // eval {
do {
nfreeze([$seq, $value, {%$where, "context", undef, "original", undef}])
}
}
MC/DC50%1/2
(eval {
do {
nfreeze([$seq, $value, $where])
}
} // eval {
do {
nfreeze([$seq, $value, {%$where, "context", undef, "original", undef}])
}
}) // croak(qq[$self->{"stage"} can't send $self->{"what"} #$seq to a worker: ] . unplaced($@))
eval {
do {
nfreeze([$seq, $value, $where])
}
}
eval {
do {
nfreeze([$seq, $value, {%$where, "context", undef, "original", undef}])
}
}
Truth table66%2/3
A // B
A= eval {
do {
nfreeze([$seq, $value, $where])
}
}B= eval {
do {
nfreeze([$seq, $value, {%$where, "context", undef, "original", undef}])
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 66 | nfreeze([$seq, $value, {%$where, context => undef, original => undef}]); | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | // croak "$self->{stage} can't send $self->{what} #$seq to a worker: " | ||||||||||||||||||||||||||||||||||||||||||
| 164 | . unplaced($@); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 14697 | ▶ | write_frame($worker->{socket}, $frame) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | or croak "$self->{stage} lost worker $worker->{pid}: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 14697 | delete $job->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 14697 | $worker->{busy} = $job; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 14697 | $self->{select}->add($worker->{socket}); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 45080 | return; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | # Start the workers: each a new perl, given this one's @INC, which | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # connects back with a token only it was given, in its environment, and | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # is set up, init run, before the first value goes. A failed init | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # croaks here. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 2380 | ▶ | sub _start ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_start: called CC=21 SCAR=34.1
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | my $setup | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 2380 | ▶ | = eval { nfreeze($self->{setup}) } | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
nfreeze($self->{"setup"})
}
} // croak(qq[$self->{"stage"} can't send its init_args to a worker: ] . unplaced($@))
MC/DC0%0/1
eval {
do {
nfreeze($self->{"setup"})
}
} // croak(qq[$self->{"stage"} can't send its init_args to a worker: ] . unplaced($@))
eval {
do {
nfreeze($self->{"setup"})
}
}
Truth table50%1/2
A
A= eval {
do {
nfreeze($self->{"setup"})
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | // croak "$self->{stage} can't send its init_args to a worker: " | ||||||||||||||||||||||||||||||||||||||||||
| 183 | . unplaced($@); | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 2380 | ▶ | my $listener = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | Listen => 128, LocalAddr => '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||
| 186 | LocalPort => 0, Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 187 | ) or croak "$self->{stage} can't listen for its workers: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 2380 | my $port = $listener->sockport; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 2380 | my $token = _token(); | |||||||||||||||||||||||||||||||||||||||||
| 190 | # The worker's module is loaded by its -e code, not by -M, so that | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # what PERL5OPT asks for, which perl takes after the command line's | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # switches, such as Devel::Cover, is loaded first. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | my @command = ( | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 2380 | $^X, (map { "-I$_" } grep { !ref } @INC), | |||||||||||||||||||||||||||||||||||||||||
| 195 | '-e', | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 'require Ekorn::Pipeline::Workers::Worker; Ekorn::Pipeline::Workers::Worker::run()', | ||||||||||||||||||||||||||||||||||||||||||
| 197 | $port, | ||||||||||||||||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 2380 | STDOUT->flush; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 2380 | STDERR->flush; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 2380 | $self->{owner} = $$; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 2380 | my %pids; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 2380 | for (1 .. $self->{n}) { | |||||||||||||||||||||||||||||||||||||||||
| 204 | 6681 | my $pid; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 6681 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | local $ENV{token_variable()} = $token; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $pid = system 1, @command; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | croak "$self->{stage} couldn't start a worker: $!" if $pid <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 6681 | ▶ | $pid = fork // croak "$self->{stage} couldn't start a worker: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
fork // croak(qq[$self->{"stage"} couldn't start a worker: $!])
MC/DC0%0/1
fork // croak(qq[$self->{"stage"} couldn't start a worker: $!])
fork
Truth table50%1/2
A
A= fork
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 6681 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 194 | local $ENV{token_variable()} = $token; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 194 | ▶ | exec @command or POSIX::_exit(127); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 6487 | $pids{$pid} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 6487 | push @Ekorn::Pipeline::Workers::FORKED, $pid; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 2186 | my $wait = IO::Select->new($listener); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 2186 | my $until = time + 30; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 2186 | ▶ | while (@{ $self->{workers} } < $self->{n}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 6248 | my $remaining = $until - time; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 6248 | ▶ | if ($remaining <= 0 || !$wait->can_read($remaining)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$remaining <= 0 or not $wait->can_read($remaining)
MC/DC0%0/2
$remaining <= 0 or not $wait->can_read($remaining)
$remaining <= 0
not $wait->can_read($remaining)
Truth table33%1/3
A or not(B)
A= $remaining <= 0B= not $wait->can_read($remaining)
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | $self->_abandon(\%pids); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | croak "$self->{stage} waited 30s for its workers to start"; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 6248 | ▶ | my $socket = $listener->accept or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 6248 | binmode $socket; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 6248 | my $hello = read_frame($socket); | |||||||||||||||||||||||||||||||||||||||||
| 231 | 6248 | ▶ | my($said, $pid) = $hello ? @{ thaw($hello) } : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 6248 | ▶ | unless (defined $said && $said eq $token && $pids{$pid}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $said and $said eq $token and $pids{$pid}
defined $said and $said eq $token
MC/DC0%0/3
defined $said and $said eq $token and $pids{$pid}
defined $said
$said eq $token
$pids{$pid}
Truth table25%1/4
A and B and C
A= defined $saidB= $said eq $tokenC= $pids{$pid}
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | close $socket; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 6248 | push @{ $self->{workers} }, {pid => $pid, socket => $socket}; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 2186 | close $listener; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 2186 | for my $worker (@{ $self->{workers} }) { | |||||||||||||||||||||||||||||||||||||||||
| 240 | 6248 | write_frame($worker->{socket}, $setup); | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 2186 | for my $worker (@{ $self->{workers} }) { | |||||||||||||||||||||||||||||||||||||||||
| 243 | 6224 | my $frame = read_frame($worker->{socket}); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 6224 | ▶ | my($ready, $error) = $frame ? @{ thaw($frame) } : (0, "it exited\n"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 6224 | ▶ | next if $ready; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 24 | my $pid = $worker->{pid}; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 24 | $self->shut_down('died'); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 24 | chomp $error; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 24 | croak "$self->{stage} couldn't set up a worker: $error"; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 2162 | return; | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # A token for the workers to give back: 16 bytes from /dev/urandom, | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # where there is one, as rand isn't made to be unguessable; rand where | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # there isn't, as on Windows, where the token's being in the worker's | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # environment, which only its own user can read, is what keeps it | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # (D16). | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 2380 | ▶ | sub _token () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token: called CC=4 SCAR=16.4
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 2380 | ▶ | if (open my $random, '<:raw', '/dev/urandom') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 2380 | my $read = read $random, my $bytes, 16; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 2380 | close $random; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 2380 | ▶ | return unpack 'H*', $bytes if ($read // 0) == 16; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$read // 0
MC/DC0%0/1
$read // 0
$read
Truth table50%1/2
A
A= $read
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | return join '', map { sprintf '%04x', int rand 65536 } 1 .. 8; | |||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub _abandon ($self, $pids) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_abandon: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | _kill(keys %$pids); | |||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | _reap($_) for keys %$pids; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | close $_->{socket} for @{ $self->{workers} }; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | @$self{qw(workers owner)} = ([], undef); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 203 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | __END__ | ||||||||||||||||||||||||||||||||||||||||||