blib/lib/Parallel/Fork/BossWorker.pm

filter: Statementstmt 91.4% 118 / 129 Branchbran 72.2% 26 / 36 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 94.4% 17 / 18 Podpod 42.8% 3 / 7 totaltotal 81.1% 172 / 212 CC 27 SCAR 17.3
CC
27
Cov
84%
process
10.5
new
9.7
receive
5.2
CRAP 5.7
↑ Summary
1 package Parallel::Fork::BossWorker;
2 #
3 # $Id: BossWorker.pm 11 2011-07-16 15:49:45Z twilde $
4 #
5  
6 6 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Perl module variables
15 6 our @ISA = qw();
16 6 our $VERSION = '0.05';
17  
18 6 ▶sub new {
Subroutine new: called CC=7 SCAR=22.7
Pod documented
19 6 my $class = shift;
20 6 my %values = @_;
21
22 my $self = {
23 result_handler => $values{result_handler} || undef, # Method for handling output of the workers
24 worker_count => $values{worker_count} || 10, # Number of workers
25 global_timeout => $values{global_timeout} || 0, # Number of seconds before the worker terminates the job, 0 for unlimited
26 work_handler => $values{work_handler}, # Handler which will process the data from the boss
27 work_queue => [],
28 6 ▶ msg_delimiter => $values{msg_delimiter} || "\0\0\0",
Condition50%4/8
$values{'result_handler'} || undef
l!l
60
$values{'worker_count'} || 10
l!l
60
$values{'global_timeout'} || 0
l!l
06
$values{'msg_delimiter'} || "\000\000\000"
l!l
06
MC/DC0%0/1
$values{'result_handler'} || undef
$values{'result_handler'}
MC/DC0%0/1
$values{'worker_count'} || 10
$values{'worker_count'}
MC/DC0%0/1
$values{'global_timeout'} || 0
$values{'global_timeout'}
MC/DC0%0/1
$values{'msg_delimiter'} || "\000\000\000"
$values{'msg_delimiter'}
Truth table50%1/2
A
A= $values{'result_handler'}
Aresult
11
00
Truth table50%1/2
A
A= $values{'worker_count'}
Aresult
11
00
Truth table50%1/2
A
A= $values{'global_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $values{'msg_delimiter'}
Aresult
11
00
29 select => IO::Select->new(),
30 };
31 6 $self->{msg_delimiter_length} = length($self->{msg_delimiter});
32 6 ▶ bless $self, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
33  
34 # The work handler is required
35 6 ▶ if (not defined $self->{work_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"work_handler"}) 0 6 6
36 0 croak("Parameters \`work_handler' is required.");
37 }
38  
39 6 return $self;
40 }
41  
42 48 ▶sub add_work(\@) {
Subroutine add_work: called CC=1 SCAR=0.0
Pod documented
43 48 my $self = shift;
44 48 my $work = shift;
45 48 unshift (@{ $self->{work_queue} }, $work);
46 }
47  
48 6 ▶sub process {
Subroutine process: called CC=10 SCAR=23.6
Pod documented
49 6 my $self = shift;
50 6 my $handler = shift;
51
52 6 eval {
53
54 # If a worker dies, there's a problem
55 5 ▶ local $SIG{CHLD} = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
56 5 my $pid = wait();
57 5 ▶ if (defined $self->{workers}->{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"workers"}{$pid}) 0 5 5
58 0 confess("Worker $pid died.");
59 }
60 6 };
61
62 # Start the workers
63 6 $self->start();
64
65 # Read from the workers, loop until they all shut down
66 1 ▶ while (%{$self->{workers}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->{"workers"};}) 4 1 5
67 4 ▶ while (my @ready = $self->{select}->can_read()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $self->{"select"}->can_read) 7 4 11
68 7 foreach my $fh (@ready) {
69 13 my $result = $self->receive($fh);
70 13 ▶ if (!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 13 13
71 0 $self->{select}->remove($fh);
72 0 print STDERR "$fh got eof\n";
73 0 next;
74 }
75
76 # Process the result handler
77 13 ▶ if ($result->{data} && defined $self->{result_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"data"} and defined $self->{"result_handler"}) 8 5 13
Condition66%2/3
$result->{"data"} and defined $self->{"result_handler"}
!ll&&!rl&&r
508
MC/DC50%1/2
$result->{"data"} and defined $self->{"result_handler"}
$result->{"data"} defined $self->{"result_handler"}
Truth table66%2/3
A and B
A= $result->{"data"}B= defined $self->{"result_handler"}
ABresult
0X0
100
111
78 8 &{ $self->{result_handler} }( $result->{data} );
79 }
80
81 # If there's still work to be done, send it to the worker, otherwise shut it down
82 13 ▶ if ($#{ $self->{work_queue} } > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{'work_queue'};} > -1) { } 8 5 13
83 8 my $worker = $self->{workers}->{$result->{pid}};
84 $self->send(
85 $self->{workers}->{ $result->{pid} }, # Worker's pipe
86 8 pop(@{ $self->{work_queue} })
87 );
88 } else {
89 5 $self->{select}->remove($fh);
90 5 my $fh = $self->{workers}->{ $result->{pid} };
91 5 delete($self->{workers}->{ $result->{pid} });
92 5 close($fh);
93 }
94 }
95 }
96 }
97
98 # Wait for our children so the process table won't fill up
99 1 ▶ while ((my $pid = wait()) != -1) { }
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pid = wait) != -1) 1 1 2
100 };
101
102 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
103 0 croak($@);
104 }
105 }
106  
107 6 ▶sub start {
Subroutine start: called CC=4 SCAR=13.9
Pod undocumented
108 6 my $self = shift();
109
110 # Create a pipe for the workers to communicate to the boss
111
112 # Create the workers
113 6 foreach (1..$self->{worker_count}) {
114
115 # Open a pipe for the worker
116 20 pipe(my $from_boss, my $to_worker);
117 20 pipe(my $from_worker, my $to_boss);
118
119 # Fork off a worker
120 20 my $pid = fork();
121
122 20 ▶ if ($pid > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid > 0) { } 15 5 20
elsif ($pid == 0) { } 5 0 5
123
124 # Boss
125 15 $self->{workers}->{$pid} = $to_worker;
126 15 $self->{from_worker}->{$pid} = $from_worker;
127 15 $self->{select}->add($from_worker);
128  
129 # Close unused pipes
130 15 close($to_boss);
131 15 close($from_boss);
132
133 } elsif ($pid == 0) {
134
135 # Worker
136
137 # Close unused pipes
138 5 close($from_worker);
139 5 close($to_worker);
140
141 # Setup communication pipes
142 5 $self->{to_boss} = $to_boss;
143 5 open(STDIN, '/dev/null');
144
145 # Send the initial request
146 5 $self->send($to_boss, {pid => $$});
147
148 # Start processing
149 5 $self->worker($from_boss);
150
151 # When the worker subroutine completes, exit
152 5 exit;
153 } else {
154 0 confess("Failed to fork: $!");
155 }
156 }
157 }
158  
159 5 ▶sub worker(\*) {
Subroutine worker: called CC=4 SCAR=14.1
Pod undocumented
160 5 my $self = shift();
161 5 my $from_boss = shift();
162
163 # Read instructions from the server
164 5 ▶ while (my $instructions = $self->receive($from_boss)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $instructions = $self->receive($from_boss)) 8 5 13
165
166 # If the handler's children die, that's not our business
167 8 $SIG{CHLD} = 'IGNORE';
168
169 # Execute the handler with the given instructions
170 8 my $result;
171 8 eval {
172 # Handle alarms
173 0 ▶ local $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 die "Work handler timed out."
175 8 };
176
177 # Set alarm
178 8 alarm($self->{global_timeout});
179
180 # Execute the handler and get it's result
181 8 ▶ if (defined $self->{work_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"work_handler"}) 8 0 8
182 8 $result = &{ $self->{work_handler} }($instructions);
183 }
184
185 # Disable alarm
186 8 alarm(0);
187 };
188
189 # Warn on errors
190 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
191 0 croak("Worker $$ error: $@");
192 }
193
194 # Send the result to the server
195 8 $self->send($self->{to_boss}, {pid => $$, data => $result});
196 }
197 }
198  
199 26 ▶sub receive(\*) {
Subroutine receive: called CC=5 SCAR=16.4
Pod undocumented
200 26 my $self = shift();
201  
202 # Get the file handle
203 26 my $fh = shift();
204
205 # Get a value from the file handle
206 26 my $value;
207 my $char;
208 26 ▶ while (read($fh, $char, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, $char, 1) 1687 5 1692
209 1687 $value .= $char;
210 1687 ▶ if (substr($value, -($self->{msg_delimiter_length})) eq $self->{msg_delimiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($value, -$self->{"msg_delimiter_length"}) eq $self->{"msg_delimiter"}) 21 1666 1687
211 21 $value = substr($value, 0, -($self->{msg_delimiter_length}));
212 21 last;
213 }
214 }
215
216 # Deserialize the data
217 6 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 6 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 26 my $data = eval($value);
220  
221 26 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 26 26
222 0 ▶ print STDERR "Value: '$value'\n" if $ENV{PFBW_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PFBW_DEBUG"} 0 0 0
223 0 confess("Failed to deserialize data: $@");
224 }
225  
226 26 return $data;
227 }
228  
229 21 ▶sub send(\*$) {
Subroutine send: called CC=1 SCAR=0.0
Pod undocumented
230 21 my $self = shift();
231  
232 # Get the file handle
233 21 my $fh = shift();
234  
235 # Get the value which will be sent
236 21 my $value = shift();
237  
238 # Print the value to the file handle
239 21 local $Data::Dumper::Deepcopy = 1;
240 21 local $Data::Dumper::Indent = 0;
241 21 local $Data::Dumper::Purity = 1;
242 21 print $fh Dumper($value) . $self->{msg_delimiter};
243
244 # Force the file handle to flush
245 21 $fh->flush();
246 }
247  
248 6 1;
249 __END__
↑ Summary