blib/lib/Parallel/Supervisor.pm

filter: Statementstmt 72.3% 102 / 141 Branchbran 40.3% 21 / 52 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 20 / 24 Podpod 77.7% 14 / 18 totaltotal 65.8% 158 / 240 CC 29 SCAR 17.1
CC
29
Cov
63%
get_child
20.0
get_readers
12.0
detach
8.8
CRAP 5.5
↑ Summary
1 package Parallel::Supervisor;
2 # A module for managing children / forked processes
3 # used in a supervisor / worker pattern, eg Parallel::ForkManager
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw(cluck carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Symbol qw(geniosym);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = "0.03";
13  
14 sub new # create a new collection of processes
15 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 2 my $class = shift;
17 2 my $self = {
18 # data
19 'STRUCTS' => {}, # children ready to run (idx name)
20 'PROCESSES' => {}, # children running (idx pid)
21 'FINISHED' => {}, # children finished running (idx name)
22 'NAMES' => {} # index on running child names (idx pid)
23 };
24
25 2 bless($self, $class);
26 2 return $self;
27 }
28  
29 #### Getters for dereferencing Object's data structures
30 sub structs # returns hash of all the jobs prepared but not attached
31 2 ▶{ my $self = shift; return $self->{STRUCTS} }
Subroutine structs: called CC=1 SCAR=0.0
Pod undocumented
32  
33 sub processes # returns hash of all the jobs attached
34 2 ▶{ my $self = shift; return $self->{PROCESSES} }
Subroutine processes: called CC=1 SCAR=0.0
Pod undocumented
35  
36 sub names # returns a hash of all pid->STRUCTS of attached processes (%NAMES)
37 1 ▶{ my $self = shift; return $self->{NAMES} }
Subroutine names: called CC=1 SCAR=0.0
Pod undocumented
38  
39 sub finished # returns a hash of all pid->STRUCTS of completed processes (%FINISHED)
40 3 ▶{ my $self = shift; return $self->{FINISHED} }
Subroutine finished: called CC=1 SCAR=0.0
Pod undocumented
41  
42 sub prepare # (name, cmd_to_eval) create a child struct and add it to %structs
43 7 ▶{
Subroutine prepare: called CC=4 SCAR=14.1
Pod documented
44 7 my ($self, $name, @cmd_args) = @_;
45 7 my $cmd = "@cmd_args";
46  
47 7 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 7 7
48 7 ▶ return undef if $self->is_ready($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready($name) 0 7 7
49  
50 #### Setup IPC
51 7 my ($child_writer, $parent_reader) = (geniosym, geniosym);
52 7 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { pipe $parent_reader, $child_writer } } or cluck("Failed to create pipe for reading child output.") 7 0 7
53 7 pipe( $parent_reader, $child_writer );
54 } or cluck "Failed to create pipe for reading child output.";
55  
56 7 $parent_reader->autoflush(1);
57 7 $child_writer->autoflush(1);
58  
59 7 $self->{STRUCTS}->{$name} = { id => $name,
60 cmd => "$cmd",
61 child_writer => $child_writer,
62 parent_reader => $parent_reader
63 };
64
65 7 return 1;
66 }
67  
68 sub is_ready # check whether name is in %structs;
69 12 ▶{
Subroutine is_ready: called CC=4 SCAR=13.9
Pod documented
70 12 my ($self,$name) = @_;
71  
72 12 ▶ return undef unless (keys(%{$self->{STRUCTS}} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"STRUCTS"};} 4 8 12
73 8 for ( keys %{$self->{STRUCTS}} ) {
74 9 ▶ return 1 if $_ eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $name 3 6 9
75 }
76 5 return undef ;
77 }
78  
79 sub is_attached # check for a running process with this name
80 7 ▶{
Subroutine is_attached: called CC=2 SCAR=7.0
Pod documented
81 7 my ($self,$name) = @_;
82  
83 7 ▶ return undef unless ( $self->{NAMES} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"NAMES"} 0 7 7
84 7 return grep {$_ eq $name} (values( %{ $self->{NAMES} } ) );
85 }
86  
87 # move from %structs to %processes with given pid, register @name
88 sub attach # ident, pid
89 3 ▶{
Subroutine attach: called CC=4 SCAR=14.2
Pod documented
90 3 my ($self,$name,$pid) = @_;
91  
92 3 ▶ return undef unless $pid =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid =~ /^\d+$/ 0 3 3
93 3 ▶ return undef if ( $self->is_attached( $name ) ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_attached($name) 0 3 3
94 3 ▶ return undef unless ( $self->is_ready( $name ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_ready($name) 0 3 3
95  
96 3 $self->{PROCESSES}{$pid} = $self->{STRUCTS}{$name};
97 3 delete $self->{STRUCTS}{$name};
98 3 $self->{NAMES}->{$pid} = $name;
99  
100 3 return $?;
101 }
102  
103 # move from %processes to %structs (does not check whether process still exists)
104 sub detach # pid
105 2 ▶{
Subroutine detach: called CC=6 SCAR=21.8
Pod documented
106 2 my ($self, $pid) = @_;
107  
108 2 ▶ if ( (! defined $pid) || ($pid !~ /^\d+$/) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid and $pid =~ /^\d+$/) 0 2 2
Condition33%1/3
defined $pid and $pid =~ /^\d+$/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $pid and $pid =~ /^\d+$/
defined $pid $pid =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $pidB= $pid =~ /^\d+$/
ABresult
0X0
100
111
109 0 carp "Error! Can't detach non-numeric pid $pid\n";
110 0 return undef;
111 }
112  
113 # TODO : verify actual running process with pid
114 2 my $name = $self->{PROCESSES}{$pid}{id};
115 2 ▶ if (! defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 2 2
116 0 carp "detach: could not find process record for pid $pid\n";
117 0 return undef;
118 }
119 2 ▶ if ($self->is_ready($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ready($name)) 0 2 2
120 0 carp "detach: name $name is ready, not running as $pid\n";
121 0 return undef;
122 }
123 2 ▶ if ($self->{NAMES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NAMES"}) 2 0 2
124 2 delete $self->{NAMES}->{$pid};
125 }
126  
127 2 $self->{FINISHED}{$name} = $self->{PROCESSES}{$pid};
128 2 delete ${$self->{PROCESSES}}{$pid};
129 2 return 1;
130 }
131  
132 # delete the struct
133 sub forget # ident
134 3 ▶{
Subroutine forget: called CC=3 SCAR=12.0
Pod documented
135 3 my ($self, $name) = @_;
136  
137 3 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
138 3 ▶ if ($self->is_attached($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_attached($name)) 0 3 3
139 0 carp "Cannot forget $name because it is alive.";
140 0 return undef;
141 }
142 3 delete $self->{STRUCTS}->{$name};
143 3 delete $self->{FINISHED}->{$name};
144 3 return 1;
145 }
146  
147 sub reset # delete all STRUCTS, PROCESSES, NAMES, FINISHED
148 1 ▶{
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
149 1 my $self = shift;
150 1 $self->{STRUCTS} = {}; # children ready to run (idx name)
151 1 $self->{PROCESSES} = {}; # children running (idx pid)
152 1 $self->{FINISHED} = {}; # children finished running (idx n ame)
153 1 $self->{NAMES} = {}; # index on running child names (idx pid)
154 }
155 # NOTE: an all numeric name could be confused with a PID!!!!
156 # since both structs and processes are checked for a match
157 sub get_child # return the hash for child with this name
158  
159 0 ▶{
Subroutine get_child: not called CC=4 SCAR=30.0
Pod documented
160 0 my ($self, $name) = @_;
161  
162 0 my %ret = ();
163 0 ▶ return \%ret unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
164 0 ▶ if ( $self->is_ready( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ready($name)) 0 0 0
165 # print STDERR "get_child: $name is ready\n";
166 0 %ret = $self->{STRUCTS}->{$name};
167 }
168 0 ▶ if ( $self->is_attached( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_attached($name)) 0 0 0
169 # print STDERR "get_child: $name is alive\n";
170 0 %ret = $self->{PROCESSES}->{$name};
171 0 $ret{ACTIVE} = 1;
172 0 return \%ret;
173 }
174 # print STDERR "Could not find the process $name\n";
175 0 return \%ret;
176 }
177  
178 # avoid iterating through %processes just to look at each id
179 sub get_names # return array of alive processes by name
180 0 ▶{
Subroutine get_names: not called CC=2 SCAR=17.9
Pod documented
181 0 my $self = shift;
182 0 my @ret = ();
183 0 ▶ if ($self->{NAMES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NAMES"}) 0 0 0
184 0 @ret = values(%{$self->{NAMES}} );
185 }
186 0 return \@ret;
187 }
188  
189 sub get_pids # returns a list with pid of all alive processes
190 1 ▶{
Subroutine get_pids: called CC=2 SCAR=7.0
Pod documented
191 1 my $self = shift;
192 1 my @ret = keys %{$self->{PROCESSES}};
193 1 ▶ return wantarray ? @ret : \@ret; #cheating a bit here
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
194 }
195  
196 # NOTE: will retrun fh refs to the read end of the pipe
197 # for all processes which have been attached or detached
198 # check keys($self->processes) to see if any children are running
199 sub get_readers # hash of name=>parent_reader_ IO handles
200 0 ▶{
Subroutine get_readers: not called CC=3 SCAR=24.8
Pod documented
201 0 my $self = shift;
202 0 my %handles = ();
203 0 ▶ while ( my ($k,$v) = each(%{$self->{PROCESSES}} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"PROCESSES"};}) 0 0 0
204 0 $handles{$v->{id} } = $v->{parent_reader};
205 }
206 0 ▶ while ( my ($k,$v) = each(%{$self->{FINISHED}} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"FINISHED"};}) 0 0 0
207 0 $handles{$v->{id} } = $v->{parent_reader};
208 }
209 0 return \%handles;
210 }
211  
212 sub get_all_ready # return list of ready STRUCTS
213 0 ▶{
Subroutine get_all_ready: not called CC=2 SCAR=17.9
Pod documented
214 0 my $self = shift;
215 0 my %ret = ();
216 0 ▶ return \%ret unless ($self->{STRUCTS}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"STRUCTS"} 0 0 0
217 0 %ret = %{ $self->{STRUCTS} };
218 # print STDERR "get_all_ready found ". keys(%ret) . " elements\n";
219 0 return \%ret;
220 }
221  
222 # exercise caution when looping through children:
223 # this can be used to test if child is acivated, otherwise leads to infinite loop
224 sub get_next_ready # returns a hash of a ready child process, or an empty hash
225 2 ▶{
Subroutine get_next_ready: called CC=4 SCAR=14.0
Pod documented
226 2 my $self = shift;
227 2 my %ret = ();
228 2 ▶ return \%ret unless defined $self->{STRUCTS};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"STRUCTS"} 0 2 2
229 2 ▶ return \%ret unless (keys(%{$self->{STRUCTS}} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"STRUCTS"};} 0 2 2
230 2 my @ids = keys(%{$self->{STRUCTS}} );
231 # ensure sort order enforced so caller can plan ahead
232 2 @ids = sort @ids;
233  
234 2 ▶ return \%ret unless defined $ids[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ids[0] 0 2 2
235 2 my $idx = $ids[0];
236 2 %ret = %{$self->{STRUCTS}{$idx}} ;
237 2 return \%ret;
238 }
239  
240 # methods after =cut are processed by the autosplit program and only compiled on demand
241  
242  
243 2 1;
244  
245 __END__
↑ Summary