lib/Parallel/Mpich/MPD/Job.pm

filter: Statementstmt 48.0% 24 / 50 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.5% 8 / 13 Podpod n/a totaltotal 40.5% 32 / 79 CC 9 SCAR 27.0
CC
9
Cov
36%
hosts
42.0
_init
12.0
_automethod
6.0
CRAP 14.9
« lib/Parallel/Mpich/MPD/Common.pm ↑ Summary script/mpd-kill.pl »
1 package Parallel::Mpich::MPD::Job;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Parallel::Mpich::MPD::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Parallel::Mpich::MPD::Job - Mpich MPD job wrapper
12  
13 =head1 SYNOPSIS
14  
15 =head1 EXPORT
16  
17 =head1 FUNCTIONS
18  
19 =head1 METHOD
20  
21 =head2 infos
22  
23 Get information of this job. Information contains these values:
24 my %info=$job->infos();
25 $info{jobid};
26 $info{jobalias};
27 $info{username};
28 $info{hosts}[0..N]{host};
29 $info{hosts}[0..N]{pid};
30 $info{hosts}[0..N]{sid};
31 $info{hosts}[0..N]{rank};
32 $info{hosts}[0..N]{cmd};
33  
34 =head2 kill
35  
36 Kill this job. See examples:
37 Parallel::Mpich::MPD::findJob(alias => 'olivier1')->kill();
38 Parallel::Mpich::MPD::findJob(jobid => '1@linux02_32996')->kill();
39  
40 =head2 signal
41  
42 Send a sig to this job.
43 It return false if not ok.
44 examples:
45 # SIGQUIT, SIGKILL, SIGSTOP, SIGCONT, SIGXCPU, SIGUSR1, SIGUSR2
46 Parallel::Mpich::MPD::findJob(alias => 'olivier1')->signal("SIGSTOP");
47 Parallel::Mpich::MPD::findJob(alias => 'olivier1')->signal("SIGCONT");
48 Parallel::Mpich::MPD::findJob(jobid => '1@linux02_32996')->kill();
49  
50 =over 4
51  
52 NOTE:
53 You couLd check the state with the following ps command:
54 ps -eo state,nice,user,comm
55 ps state des:
56 D Uninterruptible sleep (usually IO)
57 R Running or runnable (on run queue)
58 S Interruptible sleep (waiting for an event to complete)
59 T Stopped, either by a job control signal or because it is being traced.
60 W paging (not valid since the 2.6.xx kernel)
61 X dead (should never be seen)
62 Z Defunct ("zombie") process, terminated but not reaped by its parent.
63  
64 =back
65  
66 =head2 sig_stop
67  
68 Stop this job.
69 return false if not ok
70  
71 =head2 sig_cont
72  
73 Continue this job.
74 return false if not ok
75  
76 =head2 kill
77  
78 Send a kill signal
79  
80 =head2 equals($job)
81  
82 compare towo jobs and return true/false
83 FIXME.: should be implemented
84  
85 =head2 toSummaryString
86  
87 return a string for the current job
88  
89 =head1 AUTHOR
90  
91 Olivier Evalet, Alexandre Masselot, C<< <alexandre.masselot at genebio.com> >>
92  
93 =head1 BUGS
94  
95 Please report any bugs or feature requests to
96 C<bug-parallel-mpich-mpd at rt.cpan.org>, or through the web interface at
97 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parallel-Mpich-MPD>.
98 I will be notified, and then you'll automatically be notified of progress on
99 your bug as I make changes.
100  
101 =head1 SUPPORT
102  
103 You can find documentation for this module with the perldoc command.
104  
105 perldoc Parallel::Mpich::MPD
106  
107 You can also look for information at:
108  
109 =over 4
110  
111 =item * AnnoCPAN: Annotated CPAN documentation
112  
113 L<http://annocpan.org/dist/Parallel-Mpich-MPD>
114  
115 =item * CPAN Ratings
116  
117 L<http://cpanratings.perl.org/d/Parallel-Mpich-MPD>
118  
119 =item * RT: CPAN's request tracker
120  
121 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parallel-Mpich-MPD>
122  
123 =item * Search CPAN
124  
125 L<http://search.cpan.org/dist/Parallel-Mpich-MPD>
126  
127 =back
128  
129 =head1 ACKNOWLEDGEMENTS
130  
131 =head1 COPYRIGHT & LICENSE
132  
133 Copyright 2006 Olivier Evalet, Alexandre Masselot, all rights reserved.
134  
135 This program is free software; you can redistribute it and/or modify it
136 under the same terms as Perl itself.
137  
138 =cut
139  
140 12 ▶use Object::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 my @jobid :Field(Accessor => 'jobid');
142 #my @infos :Field(Accessor => 'infos');
143 my @jobalias :Field(Accessor => 'jobalias');
144 my @username :Field(Accessor => 'username');
145 my @hosts :Field(Accessor => '_hosts', Type=> 'List', Permission => 'private');
146  
147  
148 # Query or Manage a Job instance defined by his jobid.
149 my %init_args :InitArgs = (
150 JOBALIAS=>qr/^jobalias$/i,
151 JOBID=>qr/^jobid$/i,
152 );
153 0 ▶sub _init :Init{
Subroutine _init: not called CC=3 SCAR=24.8
154 0 my ($self, $h) = @_;
155 0 ▶ $self->jobalias($h->{JOBALIAS})if $h->{JOBALIAS};
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"JOBALIAS"} 0 0 0
156 0 ▶ $self->jobid($h->{JOBID}) if $h->{JOBID};
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"JOBID"} 0 0 0
157 0 $self->_hosts([]);
158 12 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 0 ▶sub _automethod :Automethod{
Subroutine _automethod: not called CC=2 SCAR=17.9
161 0 my ($self, $val) = @_;
162 0 my $set=exists $_[1];
163 0 my $name=$_;
164 0 ▶ if ($name=~/sig(kill|cont|stop)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /sig(kill|cont|stop)/) 0 0 0
165 0 my $sig=uc $1;
166 0 ▶ return sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 return $self->signal("SIG$sig");
168 }
169 0 }
170 12 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 # n host index return hash of host
173 # k key return value of the key
174 # v value set value of the key
175  
176 0 ▶sub hosts{
Subroutine hosts: not called CC=6 SCAR=37.4
177 0 my ($self, $n, $k, $val)=@_;
178  
179 #return the array
180 0 ▶ unless(defined $n){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
181 0 ▶ return wantarray?@{$self->_hosts()}:$self->_hosts();
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
182 }
183 0 ▶ unless(defined $k){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $k) 0 0 0
184 0 return $self->_hosts()->[$n];
185 }
186  
187 0 my $reHostField="(host|pid|sid|rank|cmd)";
188 0 ▶ croak "host parameter not in $reHostField" unless $k=~/^$reHostField$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^$reHostField$/o 0 0 0
189 0 ▶ if(defined $val){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
190 0 $self->_hosts()->[$n]{$k}=$val;
191 0 return $val;
192 }
193 0 return $self->_hosts()->[$n]{$k};
194 }
195  
196 0 ▶sub hosts_push{
Subroutine hosts_push: not called CC=1 SCAR=6.9
197 0 my ($self, $h)=@_;
198 0 push @{$self->_hosts()}, $h;
199 }
200  
201  
202 # \$out \$err
203 sub kill{
204 my $this=shift;
205 my ($out,$err)=@_;
206 my $stdout="";
207 my $stderr="";
208 my $pid="";
209 env_Init() unless defined(%env);
210 my $params;
211 $params=$this->jobid();
212  
213 my $ret=Parallel::Mpich::MPD::Common::__exec(cmd => commandPath('mpdkilljob')." $params");
214 return $ret==0;
215 }
216  
217 # return true if the criteria match this job
218 # $criteria{pid} [ && $criteria{rank}]
219 # $criteria{jobid}
220 # $criteria{jobalias}
221 # $criteria{username}
222  
223 sub equals{
224 my $this=shift;
225 my %criteria=@_;
226
227 #print Dumper \%criteria;
228 #print $this;
229
230 if (defined($criteria{jobalias}) && $criteria{jobalias}{$this->jobalias}){
231 return 1;
232 }
233
234 if (defined($criteria{username}) && $criteria{username}{$this->username}){
235 return 1;
236 }
237
238 if (defined($criteria{jobid}) && $criteria{jobid}{$this->jobid}){
239 return 1;
240 }
241
242 foreach my $host (@{$this->_hosts()}){
243 if (defined($criteria{pid})) {
244 if (defined($criteria{rank})) {
245 return 1 if ($criteria{rank} eq $host->{rank} && $criteria{pid} eq $host->{rank});
246 }else{
247 return 1 if ($criteria{pid} eq $host->{pid});
248 }
249 }
250 }
251
252 }
253  
254 # Send a signal to all process for the current job.
255 # SIGQUIT, SIGKILL, SIGSTOP, SIGCONT, SIGXCPU, SIGUSR1, SIGUSR2
256 sub signal{
257 my $stdout="";
258 my $stderr="";
259 my $pid="";
260 my $this=shift;
261 my ($sigtype)=@_;
262 if (!defined($sigtype)){
263 printf STDERR "ERROR:".__PACKAGE__."::__sig() sigtype is not defined. \n";
264 return 0;
265 }
266 env_Init() unless defined(%env);
267 my $params;
268 $params="-j ".$this->jobid();
269  
270 my $ret=Parallel::Mpich::MPD::Common::__exec(cmd => commandPath('mpdsigjob')." $sigtype $params");
271  
272 return $ret==0;
273 }
274  
275 #sub stop{
276 # my $this=shift;
277 # return $this->signal("SIGSTOP");
278 #}
279  
280 #sub continue{
281 # my $this=shift;
282 # return $this->signal("SIGCONT");
283 #}
284  
285 #sub destroy{
286 # my $this=shift;
287 # return $this->signal("SIGKILL");
288 #}
289  
290  
291 use overload '""' => \&toSummaryString;
292  
293 sub toSummaryString{
294 my $self=shift;
295  
296 my $ret=$self->jobid."\t(alias='".$self->jobalias."', user='".$self->username."')\n";
297 my @h=$self->hosts();
298 foreach (sort {$a->{rank} <=> $b->{rank}} @h){
299 $ret.="$_->{rank}\t".($_->{host}||'')."\t".($_->{pid}||'')."\t".($_->{sid}||'')."\t".($_->{pgm}||'')."\n";
300 }
301 return $ret."\n";
302 }
303  
304  
305 1; # End of Parallel::Mpich::MPD
« lib/Parallel/Mpich/MPD/Common.pm ↑ Summary script/mpd-kill.pl »