| 1 | package Proc::Topus; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 34 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 34 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 34 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 13 | 34 | our @EXPORT_OK = qw( spawn ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub _CONDUIT () { 'socketpair' } | ||||||||||||||||||||||||||||
| 17 | sub _PARENT () { 0 } | ||||||||||||||||||||||||||||
| 18 | sub _WORKER () { 1 } | ||||||||||||||||||||||||||||
| 19 | sub _READER () { 0 } | ||||||||||||||||||||||||||||
| 20 | sub _WRITER () { 1 } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 193 | ▶ | sub spawn { | ||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 193 | my $args = _args( @_ ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 187 | my $workers = $args->{workers}; | |||||||||||||||||||||||||||
| 27 | 187 | ▶ | die 'No workers defined' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $workers and keys %$workers > 0
MC/DC100%2/2
defined $workers and keys %$workers > 0
defined $workers
keys %$workers > 0
Truth table100%3/3
A and B
A= defined $workersB= keys %$workers > 0
| |||||||||||||||||||||||||||||
| 28 | unless defined $workers && keys %$workers > 0; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 183 | my $pairs = _alloc_pairs( $workers, $args->{conduit}, $args->{autoflush} ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 177 | my %pids; | |||||||||||||||||||||||||||
| 33 | 177 | for my $name ( keys %$workers ) { | |||||||||||||||||||||||||||
| 34 | 177 | my $config = $workers->{$name}; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 177 | my $pid = fork; | |||||||||||||||||||||||||||
| 37 | 177 | ▶ | defined $pid | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | or die 'fork: ', $!; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 177 | $pids{$pid} = $name; | |||||||||||||||||||||||||||
| 41 | next | ||||||||||||||||||||||||||||
| 42 | 177 | ▶ | if $pid != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 31 | my @pairs = map { $_->[_WORKER] } @{ delete $pairs->{$name} }; | |||||||||||||||||||||||||||
| 45 | delete $pairs->{$_} | ||||||||||||||||||||||||||||
| 46 | 31 | for keys %$pairs; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 31 | ▶ | if( $config->{setsid} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 3 | require POSIX; | |||||||||||||||||||||||||||
| 50 | 3 | ▶ | POSIX::setsid() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | or die 'setsid: ', $!; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 31 | my $loader = $config->{loader}; | |||||||||||||||||||||||||||
| 55 | 31 | ▶ | $loader->( @pairs ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | if ref $loader eq 'CODE'; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 30 | ▶ | while( @pairs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 50 | my $pair = shift @pairs; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 50 | my $pid = fork; | |||||||||||||||||||||||||||
| 62 | 50 | ▶ | defined $pid | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | or die 'fork: ', $!; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | next | ||||||||||||||||||||||||||||
| 66 | 50 | ▶ | if $pid != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 20 | splice @pairs; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 20 | my $main = $config->{main}; | |||||||||||||||||||||||||||
| 71 | 18 | ▶ | $main = sub { $pair } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 20 | ▶ | unless ref $main eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 20 | return $main->( @$pair[_READER, _WRITER] ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 10 | exit 0; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 146 | for my $name ( keys %$pairs ) { | |||||||||||||||||||||||||||
| 81 | $pairs->{$name} = [ | ||||||||||||||||||||||||||||
| 82 | 291 | map { { reader => $_->[_READER], writer => $_->[_WRITER] } } | |||||||||||||||||||||||||||
| 83 | 291 | map { $_->[_PARENT] } | |||||||||||||||||||||||||||
| 84 | 146 | @{ $pairs->{$name} } | |||||||||||||||||||||||||||
| 85 | ]; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 146 | _wait( \%pids ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 145 | my $main = $args->{main}; | |||||||||||||||||||||||||||
| 91 | 120 | ▶ | $main = sub { $pairs } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 145 | ▶ | unless ref $main eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 145 | $main->( $pairs ) | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 193 | ▶ | sub _args { | ||||||||||||||||||||||||||
|
Subroutine
_args: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 98 | 193 | ▶ | if( scalar @_ == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 99 | 179 | ▶ | unless( defined $_[0] && ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[0] and ref $_[0] eq "HASH"
MC/DC100%2/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0]
ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 100 | 4 | die "Single arguments must be a HASH ref"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 175 | return $_[0] | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | elsif( @_ % 2 ) { | ||||||||||||||||||||||||||||
| 106 | 2 | die "Odd number of arguments"; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||||||||
| 109 | 12 | return { @_ }; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 183 | ▶ | sub _alloc_pairs { | ||||||||||||||||||||||||||
|
Subroutine
_alloc_pairs: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 114 | 183 | my ( $workers, $_conduit, $_autoflush ) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 183 | ▶ | $_conduit = _CONDUIT | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | unless defined $_conduit; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 183 | ▶ | $_autoflush = 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | unless defined $_autoflush; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 183 | my %pairs; | |||||||||||||||||||||||||||
| 123 | 183 | for my $name ( keys %$workers ) { | |||||||||||||||||||||||||||
| 124 | 183 | my $config = $workers->{$name}; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 183 | my $count = $config->{count}; | |||||||||||||||||||||||||||
| 127 | 183 | ▶ | die "Invalid worker count ($name)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $count and $count > 0
MC/DC100%2/2
defined $count and $count > 0
defined $count
$count > 0
Truth table100%3/3
A and B
A= defined $countB= $count > 0
| |||||||||||||||||||||||||||||
| 128 | unless defined $count && $count > 0; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 179 | my $conduit = $config->{conduit}; | |||||||||||||||||||||||||||
| 131 | 179 | ▶ | $conduit = $_conduit | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | unless defined $conduit; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 179 | my $autoflush = $config->{autoflush}; | |||||||||||||||||||||||||||
| 135 | 179 | ▶ | $autoflush = $_autoflush | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | unless defined $autoflush; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $pairs{$name} = | ||||||||||||||||||||||||||||
| 139 | 179 | [ map { _alloc_pair( $name, $conduit, $autoflush ) } 1 .. $count ]; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 177 | \%pairs | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 354 | ▶ | sub _alloc_pair { | ||||||||||||||||||||||||||
|
Subroutine
_alloc_pair: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 146 | 354 | my ( $name, $conduit, $autoflush ) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 354 | ▶ | if( $conduit eq 'socketpair' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 149 | 282 | ▶ | socketpair my $ps, my $ws, AF_UNIX, SOCK_STREAM, PF_UNSPEC | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | or die 'socketpair: ', $!; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 282 | _autoflush( $autoflush, $ps, $ws ); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 282 | return _pair( $ps, $ps, $ws, $ws ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | elsif( $conduit eq 'pipe' ) { | ||||||||||||||||||||||||||||
| 157 | 70 | ▶ | pipe my $pr, my $ww | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | or die 'pipe: ', $!; | ||||||||||||||||||||||||||||
| 159 | 70 | ▶ | pipe my $wr, my $pw | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | or die 'pipe: ', $!; | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 70 | _autoflush( $autoflush, $pr, $pw, $wr, $ww ); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 70 | return _pair( $pr, $pw, $wr, $ww ); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 2 | die "Invalid worker conduit '$conduit' for worker '$name'"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 352 | ▶ | sub _autoflush { | ||||||||||||||||||||||||||
|
Subroutine
_autoflush: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 171 | return | ||||||||||||||||||||||||||||
| 172 | 352 | ▶ | unless shift; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 306 | my $fh = select; | |||||||||||||||||||||||||||
| 175 | 752 | do { select $_; $| = 1 } | |||||||||||||||||||||||||||
| 176 | 306 | for @_; | |||||||||||||||||||||||||||
| 177 | 306 | select $fh | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 352 | ▶ | sub _pair { | ||||||||||||||||||||||||||
|
Subroutine
_pair: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 352 | my ( $pr, $pw, $wr, $ww ) = @_; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 352 | my $pp; @$pp[_READER, _WRITER] = ( $pr, $pw ); | |||||||||||||||||||||||||||
| 184 | 352 | my $wp; @$wp[_READER, _WRITER] = ( $wr, $ww ); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 352 | my $pair; @$pair[_PARENT, _WORKER] = ( $pp, $wp ); | |||||||||||||||||||||||||||
| 187 | 352 | $pair | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 146 | ▶ | sub _wait { | ||||||||||||||||||||||||||
|
Subroutine
_wait: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 191 | 146 | my ( $pids ) = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 146 | my $errors = 0; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 146 | ▶ | until( keys %$pids == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 146 | my $pid = waitpid -1, 0; | |||||||||||||||||||||||||||
| 197 | 146 | ▶ | die 'waitpid: ', $! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | if $pid == -1; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 146 | my $name = delete $pids->{$pid}; | |||||||||||||||||||||||||||
| 201 | next | ||||||||||||||||||||||||||||
| 202 | 146 | ▶ | unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 146 | my $rc = $? >> 8; | |||||||||||||||||||||||||||
| 205 | 146 | ▶ | $errors++ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | unless $rc == 0; | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 146 | ▶ | die "One or more loaders failed" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | if $errors; | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 34 | 1 | |||||||||||||||||||||||||||
| 215 | __END__ | ||||||||||||||||||||||||||||