lib/Schedule/SGELK.pm

filter: Statementstmt 49.0% 147 / 300 Branchbran 23.1% 31 / 134 Conditioncond 29.0% 9 / 31 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 66.6% 20 / 30 Podpod 78.9% 15 / 19 totaltotal 42.1% 224 / 531 CC 93 SCAR 41.1
CC
93
Cov
40%
waitOnJobs
223.4
checkJob
90.0
pleaseExecute
74.6
CRAP 61.2
« blib/lib/Schedule/SGELK.pm ↑ Summary SGELK.log/qsub.415372.pl »
1 #! /usr/bin/env perl
2  
3 =pod
4  
5 =head1 NAME
6  
7 Schedule::SGELK
8  
9 =head1 SYNOPSIS
10  
11 A module for submitting jobs to an SGE queue.
12 use Schedule::SGELK
13 my $sge=Schedule::SGELK->new(verbose=>1,numnodes=>5,numcpus=>8,workingdir=>"SGE/",waitForEachJobToStart=>1);
14 $sge->set("jobname","thisisaname");
15 # run a series of jobs and wait for them all to finish
16 my $job=$sge->pleaseExecute("sleep 60");
17 my $job2=$sge->pleaseExecute("sleep 60");
18 $sge->wrapItUp();
19 # or you can specify which jobs to wait for
20 $sge->waitOnJobs([$job,$job2],1); # 1 means wait for all jobs to finish; 0 to wait for any free node
21 # or in one step
22 $sge->pleaseExecute_andWait("sleep 60");
23  
24 A quick test for this module is the following one-liner
25 perl -MSchedule::SGELK -e '$sge=Schedule::SGELK->new(numnodes=>5); for(1..3){$sge->pleaseExecute("sleep 3");}$sge->wrapItUp();'
26  
27 Another quick test is to use the test() method, if you want to see standardized text output (see test() below)
28 perl -MSchedule::SGELK -e '$sge=Schedule::SGELK->new(-numnodes=>2,-numcpus=>8); $sge->test(\%tmpSettings);'
29
30  
31 =head1 DESCRIPTION
32  
33 A module for submitting jobs to an SGE queue. Monitoring is
34 performed using a combination of monitoring files
35 written to the hard drive and qstat.
36 Submitting is performed internally by making a perl script.
37  
38 =head1 AUTHOR
39  
40 Author: Lee Katz <lkatz@cdc.gov>
41  
42 =cut
43  
44 package Schedule::SGELK;
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use File::Slurp qw/read_file write_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use String::Escape qw/escape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 1 our $VERSION = version->declare("v1.5");
56  
57 1 my $has_threads=eval{
58 1 return 0; # this isn't working yet
59 0 require threads;
60 0 return 1;
61 };
62  
63 # some global variables
64 1 my @jobsToClean=();
65 1 my @jobsToMonitor=();
66 1 my $numSlots=0; # number of slots that are being used now
67  
68 4 ▶sub logmsg {local $0=basename $0;my $FH = *STDOUT; print $FH "$0: ".(caller(1))[3].": @_\n";}
Subroutine logmsg: called CC=1 SCAR=0.0
Pod undocumented
69 0 ▶local $SIG{'__DIE__'} = sub { my $e = $_[0]; $e =~ s/(at [^\s]+? line \d+\.$)/\nStopped $1/; die("$0: ".(caller(1))[3].": ".$e); };
Subroutine __ANON__: not called CC=1 SCAR=3.5
70 0 ▶local $SIG{INT} = sub{ cleanAllJobs(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
71  
72 # to be called when the script exits
73 sub cleanAllJobs{
74 1 ▶ return if(!@jobsToClean);
Subroutine cleanAllJobs: called CC=3 SCAR=11.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless @jobsToClean 0 1 1
75 1 logmsg "Cleaning all jobs";
76 1 for (@jobsToClean){
77 2 cleanAJob($_);
78 }
79 }
80 END{
81 cleanAllJobs();
82 }
83  
84 =pod
85  
86 =head2 METHODS
87  
88 =over
89  
90 =item sub new
91  
92 create a new instance of a scheduler.
93 Arguments and their defaults:
94 numnodes=>50 maximum nodes to use
95 numcpus=>128 maximum cpus that will be used per node in a script
96 maxslots=>9999 maximum slots that you can use. Useful if you want to be limited by total slots instead of nodes or CPUs. E.g. {numnodes=>100,numcpus=>1,maxslots=>20}
97 verbose=>0
98 workingdir=>$ENV{PWD} a directory that all nodes can access to read/write jobs and log files
99 waitForEachJobToStart=>0 Allow each job to start as it's run (0), or to wait until the qstat sees the job before continuing (1)
100 jobname=>... This is the name given to the job when you view it with qstat. By default, it will be named after the script that calls this module.
101 warn_on_error=>1 This will make the script give a warning instead of exiting
102 qsubxopts=>... These are extra options to pass to qsub. E.g., {qsubxopts=>"-V"} Options are overwritten by appending them to the within-script options. Therefore this is not the best way to choose a different queue but it is a way to change a job name or the number of processors.
103 noqsub=>1 Force performing a system call instead of using qsub
104 queue=>all.q Choose the queue to use for a new job. Default: all.q
105  
106 Examples:
107 {numnodes=>100,numcpus=>1,maxslots=>50} # for many small jobs
108 {numnodes=>5,numcpus=>8,maxslots=>40} # for a few larger jobs (note: maxslots should be >= numnodes * maxslots
109  
110 =back
111  
112 =cut
113  
114 sub new{
115 1 ▶ my($class,%args)=@_;
Subroutine new: called CC=11 SCAR=25.1
Pod documented
116 1 my $self=bless{},$class;
117  
118 # just start with verbosity = 0. This avoids an error in the >= checks
119 1 $self->{'settings'}={};
120 1 $self->{'error'}="";
121 1 $self->{'exit_code'}=0;
122  
123 # load up if we know what we have
124 1 foreach my $key (keys %args) {
125 6 my $nodash=$key;
126 6 $nodash =~ s/^\-//;
127 6 $self->set($nodash,$args{$key});
128 }
129  
130 # set defaults if they are not set
131 1 my %default=(numnodes=>50,numcpus=>128,verbose=>0,waitForEachJobToStart=>0,maxslots=>9999,queue=>"all.q",scheduler=>"SGE");
132 1 ▶ while(my($key,$value)=each(%default)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %default) 7 1 8
133 7 ▶ $self->settings($key,$value) if(!defined($self->settings($key)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->settings($key) 3 4 7
134 }
135 1 ▶ if(!$self->get("workingdir")){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get("workingdir")) 0 1 1
136 0 $self->set("workingdir",$self->mktempdir());
137 0 logmsg "Working directory not set. Using ".$self->get("workingdir");
138 }
139  
140 # executables
141 1 for(qw(qsub qstat qdel)){
142 # See if it exists
143 3 my $exec=`which $_ 2>/dev/null`;
144 3 ▶ $exec="" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 3 0 3
145 3 chomp($exec);
146 3 $self->set($_,$exec);
147  
148 3 ▶ $self->set("scheduler","") if(!$exec);
Branch: 1 branch
BranchTrueFalseTotal
unless $exec 3 0 3
149 }
150  
151 # See if SGE is present
152 1 ▶ if($ENV{SGE_ROOT} && -e $ENV{SGE_ROOT}){
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'SGE_ROOT'} and -e $ENV{'SGE_ROOT'}) { } 0 1 1
Condition33%1/3
$ENV{"SGE_ROOT"} and -e $ENV{"SGE_ROOT"}
!ll&&!rl&&r
100
MC/DC0%0/2
$ENV{"SGE_ROOT"} and -e $ENV{"SGE_ROOT"}
$ENV{"SGE_ROOT"} -e $ENV{"SGE_ROOT"}
Truth table33%1/3
A and B
A= $ENV{"SGE_ROOT"}B= -e $ENV{"SGE_ROOT"}
ABresult
0X0
100
111
153 0 $self->set("scheduler","SGE");
154 } else{
155 1 logmsg "Env variable \$SGE_ROOT is not set. I will not use SGE";
156 1 $self->set("scheduler","");
157 1 $self->set("qsub","");
158 }
159  
160 # Remove the scheduler option if the user explicitly
161 # chooses not to use it.
162 1 ▶ $self->set("scheduler","") if($self->get("noqsub"));
Branch: 1 branch
BranchTrueFalseTotal
if $self->get("noqsub") 1 0 1
163  
164 1 return $self;
165 }
166  
167 =pod
168  
169 =over
170  
171 =item sub error($msg,$exit_code) or error()
172  
173 Get or set the error. Can set the error code too, if provided.
174  
175 =back
176  
177 =cut
178  
179 # Sets the error and returns the previous error message.
180 # Or, simply returns the current error message.
181 sub error{
182 0 ▶ my($self,$msg,$exit_code)=@_;
Subroutine error: not called CC=3 SCAR=24.8
Pod documented
183 0 ▶ return $self->{error} if(!defined($msg));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
184 0 my $oldmsg=$self->{error};
185 0 $self->{error}=$msg;
186 0 ▶ $self->{exit_code}=$exit_code if(defined($exit_code));
Branch: 1 branch
BranchTrueFalseTotal
if defined $exit_code 0 0 0
187 0 return $oldmsg;
188 }
189 =pod
190  
191 =over
192  
193 =item sub set() get() settings()
194  
195 Get or set a setting. All settings are listed under sub new().
196 If a setting is provided without a value, then nothing new will be set,
197 and only the value of the specified setting will be returned.
198  
199 =back
200  
201 =cut
202  
203 sub set{
204 37 ▶ my($self,$key,$value)=@_;
Subroutine set: called CC=5 SCAR=16.2
Pod documented
205 37 ▶ if(defined($key) && defined($value)){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $key and defined $value) { } 18 19 37
elsif (defined $key) { } 17 2 19
Condition100%3/3
defined $key and defined $value
!ll&&!rl&&r
21718
MC/DC100%2/2
defined $key and defined $value
defined $key defined $value
Truth table100%3/3
A and B
A= defined $keyB= defined $value
ABresult
0X0
100
111
206 18 $self->{'settings'}{$key}=$value;
207 } elsif (defined($key)){
208 17 return $self->{'settings'}{$key};
209 }
210 20 ▶ return %{$self->{'settings'}} if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 20 20
211 20 return $self->{'settings'};
212 }
213 # renaming of sub set()
214 sub get{
215 8 ▶ my($self,@args)=@_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
216 8 return $self->set(@args);
217 }
218 # renaming of sub set()
219 sub settings{
220 14 ▶ my($self,@args)=@_;
Subroutine settings: called CC=1 SCAR=0.0
Pod documented
221 14 return $self->set(@args);
222 }
223  
224 =pod
225  
226 =over
227  
228 =item pleaseExecute()
229  
230 This is the main method. It will submit a command to the cluster.
231 $sge->set("jobname","a_nu_start");
232 $sge->pleaseExecute("someCommand with parameters");
233  
234 If you are already occupying more than numnodes, then it will pause before
235 executing the command. It will also create many files under workingdir, so
236 be sure to specify it. The workingdir is the current directory by default.
237  
238 You can also specify temporary settings for this one command with a referenced hash.
239  
240 $sge->pleaseExecute("someCommand with parameters",{jobname=>"a_nu_start",numcpus=>2});
241  
242 =back
243  
244 =cut
245  
246 sub pleaseExecute{
247 2 ▶ my($self,$cmd,$tmpSettings)=@_;
Subroutine pleaseExecute: called CC=25 SCAR=43.1
Pod documented
248 2 local $0=basename $0;
249 2 my %settings=%{ $self->settings };
250  
251 # read in any temporary settings for this command
252 2 ▶ $tmpSettings={} if(!defined($tmpSettings));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmpSettings 2 0 2
253 2 $settings{$_}=$$tmpSettings{$_} for(keys(%$tmpSettings));
254  
255 # default settings for undefined settings
256 2 my $jobid=-1; # -1 is an error state
257 2 ▶ $settings{jobname}||="sgelk$0";
Condition50%1/2
$settings{"jobname"} ||= "sgelk$0"
l!l
02
MC/DC0%0/1
$settings{"jobname"} ||= "sgelk$0"
$settings{"jobname"}
Truth table50%1/2
A
A= $settings{"jobname"}
Aresult
11
00
258 2 ▶ $settings{logfile}||="$0.log";
Condition50%1/2
$settings{"logfile"} ||= "$0.log"
l!l
02
MC/DC0%0/1
$settings{"logfile"} ||= "$0.log"
$settings{"logfile"}
Truth table50%1/2
A
A= $settings{"logfile"}
Aresult
11
00
259 2 ▶ $settings{numcpus}||=1;
Condition50%1/2
$settings{"numcpus"} ||= 1
l!l
20
MC/DC0%0/1
$settings{"numcpus"} ||= 1
$settings{"numcpus"}
Truth table50%1/2
A
A= $settings{"numcpus"}
Aresult
11
00
260 2 ▶ $settings{timeout}||=60; # how long we will wait for qsub to start
Condition50%1/2
$settings{"timeout"} ||= 60
l!l
02
MC/DC0%0/1
$settings{"timeout"} ||= 60
$settings{"timeout"}
Truth table50%1/2
A
A= $settings{"timeout"}
Aresult
11
00
261 2 ▶ return 0 if($cmd=~/^\s*$/); # if there's no command, then no worries
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^\s*$/ 0 2 2
262  
263 2 $self->waitOnJobs(\@jobsToMonitor,0); # wait until the job can be submitted
264  
265 2 my $rand=int(rand(999999));
266 2 my $tempdir=$settings{workingdir};
267 # create a perl script with the literal command in it
268 2 my $script="$tempdir/qsub.$rand.pl";
269  
270 2 ▶ my $prefix=($0 eq '-e')?"STDIN":$0;
Branch: 1 branch
BranchTrueFalseTotal
$0 eq '-e' ? : 0 2 2
271 2 $prefix="$settings{workingdir}/$prefix.$rand";
272 2 my($submitted,$running,$finished,$died,$output)=("$prefix.submitted", "$prefix.running", "$prefix.finished","$prefix.died","$prefix.log");
273
274 2 my $perl=`which perl`; chomp($perl);
275 2 ▶ open(SCRIPT,">",$script) or die "Could not write to temporary script $script: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SCRIPT, ">", $script or die "Could not write to temporary script ${script}: $!" 0 2 2
276 2 print SCRIPT "#! $perl\n\n";
277 # It has SGE params in it.
278 2 print SCRIPT "#\$ -N $settings{jobname}\n";
279 2 print SCRIPT "#\$ -S $perl\n";
280 2 print SCRIPT "#\$ -V\n";
281 2 print SCRIPT "#\$ -wd $ENV{PWD}\n";
282 2 print SCRIPT "#\$ -pe smp $settings{numcpus}\n";
283 2 print SCRIPT "#\$ -o $output\n";
284 2 print SCRIPT "#\$ -e $output\n";
285 2 print SCRIPT "#\$ -q $settings{queue}\n";
286 # qsubxopts get to be in here first but will be overwritten by later qsubopts below
287 2 ▶ if(my $opts=$settings{qsubxopts}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = $settings{"qsubxopts"}) 0 2 2
288 0 print SCRIPT "# options specified by qsubxopts are in the next line:\n";
289 0 print SCRIPT "#\$ $opts\n";
290 }
291 2 print SCRIPT "use strict;\nuse warnings;\n";
292 2 print SCRIPT "use File::Slurp qw/read_file write_file/;\n";
293  
294 # announces that it was submitted
295 2 my $sanitized=escape('qqbackslash',$cmd);
296 2 print SCRIPT "write_file('$submitted',$sanitized);\n";
297 # it runs the command
298 2 print SCRIPT "write_file('$running',$sanitized);\n";
299 2 print SCRIPT "system($sanitized);\n";
300 # let the script try one more time if it fails
301 #print SCRIPT "system($sanitized) if \$?;\n";
302 # print a parsable error if the script dies. This error will be in $output and in file $died
303 2 print SCRIPT <<END;
304 if(\$?){
305 my \$error=\"QSUB ERROR\\n\$?\\n\$!\";
306 write_file('$died',$sanitized);
307 write_file('$died',{append=>1},"\\n\$error\\n");
308 die \$error;
309 }
310 END
311 # announces when it is finished
312 2 print SCRIPT "write_file('$finished',$sanitized);\n";
313 2 close SCRIPT;
314 2 ▶ system("touch $script"); die if $?; # make the system close the script. Why isn't Perl closing it?
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 2 2
315 #system("cat $script");sleep 60;die;
316  
317 # now run the script and get the jobid
318 2 my %return=(submitted=>$submitted,running=>$running,finished=>$finished,died=>$died,tempdir=>$tempdir,output=>$output,cmd=>$cmd,script=>$script,jobname=>$settings{jobname},numcpus=>$settings{numcpus});
319 2 my $qsub=$self->get("qsub");
320 2 ▶ if(!$settings{scheduler}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($settings{"scheduler"}) 2 0 2
321 2 my $job=command("$perl $script",$has_threads,\%settings);
322 2 ▶ $return{thread}=$job if($has_threads);
Branch: 1 branch
BranchTrueFalseTotal
if $has_threads 0 2 2
323 2 ▶ $return{jobid}=$job->tid if($has_threads);
Branch: 1 branch
BranchTrueFalseTotal
if $has_threads 0 2 2
324 2 ▶ push(@jobsToClean,\%return) if(!$self->settings("keep"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings("keep") 2 0 2
325 2 push(@jobsToMonitor,\%return);
326 2 $numSlots+=$settings{numcpus}; # claim these cpus
327 2 ▶ return %return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 2 2
328 2 return \%return;
329 }
330  
331 # At this point, qsub is on this computer. Submit the job.
332 0 my $out=`$qsub $script`; chomp($out);
333 0 ▶ if($out=~/Your job (\d+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /Your job (\d+)/) { } 0 0 0
334 0 $jobid=$1;
335 0 $out.=" from $script";
336 0 ▶ logmsg $out if($settings{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $settings{"verbose"} 0 0 0
337 } else {
338 0 logmsg "WARNING: the last job submitted did not have an obvious jobid. It can't be tracked!";
339 }
340  
341 # monitor for the script to be running before moving on
342 0 my $started=time;
343 0 ▶ while(!-e $submitted){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $submitted) 0 0 0
344 0 ▶ last if(!$self->settings("waitForEachJobToStart"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings("waitForEachJobToStart") 0 0 0
345 0 sleep 1;
346 0 ▶ die "Command timed out!\n $cmd" if((time-$started)>$settings{timeout});
Branch: 1 branch
BranchTrueFalseTotal
if time - $started > $settings{"timeout"} 0 0 0
347 0 ▶ die "Command resulted in an error. qstat -j $jobid for more info\n $cmd" if($self->jobStatus($jobid) eq 'Eqw');
Branch: 1 branch
BranchTrueFalseTotal
if $self->jobStatus($jobid) eq "Eqw" 0 0 0
348 }
349  
350 # TODO create a link from the jobid to the random id
351
352 0 $return{jobid}=$jobid;
353 0 ▶ push(@jobsToClean,\%return) if(!$self->settings("keep"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings("keep") 0 0 0
354 0 push(@jobsToMonitor,\%return);
355 0 $numSlots+=$settings{numcpus}; # claim these cpus
356 0 ▶ return %return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
357 0 return \%return;
358 }
359  
360 =pod
361  
362 =over
363  
364 =item pleaseExecute_andWait()
365  
366 Exact same as pleaseExecute(), except it will wait for the command to finish
367 before continuing. Internally calls pleaseExecute() and then waitOnJobs().
368 However one key difference between pleaseExecute() and this sub is that you can
369 give a list of commands.
370  
371 # this will take 100 seconds because all commands have to finish.
372 $sge->pleaseExecute_andWait(["sleep 60","sleep 100","sleep 3"]);
373  
374 =back
375  
376 =cut
377  
378 sub pleaseExecute_andWait{
379 0 ▶ my($self,$cmd)=@_;
Subroutine pleaseExecute_andWait: not called CC=3 SCAR=24.8
Pod documented
380 0 my %settings=$self->settings;
381 0 my $mustfinish=$self->settings("mustfinish"); # should be restored later
382 0 $self->set("mustfinish",0);
383 0 ▶ $cmd=[$cmd] if(ref($cmd) eq ""); # let cmd be a string but turn it into a list internally
Branch: 1 branch
BranchTrueFalseTotal
if ref $cmd eq "" 0 0 0
384 0 my(@jobid);
385 0 for(@$cmd){
386 0 my $jobid=$self->pleaseExecute($_);
387 0 push(@jobid,$jobid);
388 0 $self->waitOnJobs(\@jobid);
389 }
390 0 $self->waitOnJobs(\@jobid,1);
391 }
392  
393 =pod
394  
395 =over
396  
397 =item checkJob($jobHash)
398  
399 Checks the status of a given job. The job variable is obtained from pleaseExecute().
400 $self->error can be set if there is an error in the job. Return values:
401 1 for finished; 0 for still running or hasn't started; -1 for error.
402  
403 =back
404  
405 =cut
406  
407 sub checkJob{
408 0 ▶ my($self,$job)=@_;
Subroutine checkJob: not called CC=9 SCAR=45.0
Pod documented
409 # See what the job status is {jobid} for fast checking
410 0 my $status=$self->jobStatus($$job{jobid});
411 0 ▶ if($status eq 'qw'){ # queued but not running
Branch: 3 branches
BranchTrueFalseTotal
if ($status eq 'qw') { } 0 0 0
elsif ($status eq 'Eqw') { } 0 0 0
elsif ($status =~ /[rt]/) { } 0 0 0
412 0 return 0;
413 } elsif($status eq 'Eqw'){ # error
414 0 $self->error("Command resulted in an error. qstat -j $$job{jobid} for more info\n $$job{cmd}");
415 0 return -1;
416 } elsif($status=~/[rt]/){ # running or is delayed
417 0 return 0;
418 }
419  
420 # look at files to check on the job status, for slower checking.
421 # see if the job has even started: {submitted}
422 0 ▶ return 0 if(!-e $$job{submitted});
Branch: 1 branch
BranchTrueFalseTotal
unless -e $job->{"submitted"} 0 0 0
423 # if the job finished, then great! {finished}
424 0 ▶ return 1 if(-e $$job{finished});
Branch: 1 branch
BranchTrueFalseTotal
if -e $job->{"finished"} 0 0 0
425 0 ▶ return 1 if(!keys(%$job)); # sometimes a job is blank... why?
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$job 0 0 0
426 # if the job died
427 0 ▶ if(-e $$job{died}){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $job->{"died"}) 0 0 0
428 0 my @content=read_file($$job{output});
429 0 chomp(@content);
430 0 $self->error(join("\n",@content[-3..-1]));
431 0 return -1;
432 }
433 # It's running if the die-file isn't there and if the running file is there
434 0 ▶ return 0 if(-e $$job{running});
Branch: 1 branch
BranchTrueFalseTotal
if -e $job->{"running"} 0 0 0
435 0 logmsg "ERROR: Could not understand what the status is of job $$job{jobid}!\n".Dumper($job);
436 0 return -1;
437 }
438  
439 =pod
440  
441 =over
442  
443 =item jobStatus(jobid)
444  
445 Given an SGE job id, it returns its qstat status
446  
447 =back
448  
449 =cut
450  
451 sub jobStatus{
452 0 ▶ my($self,$jobid)=@_;
Subroutine jobStatus: not called CC=4 SCAR=30.0
Pod documented
453 0 my $state=0;
454 0 ▶ $jobid||=0;
Condition0%0/2
$jobid ||= 0
l!l
00
MC/DC0%0/1
$jobid ||= 0
$jobid
Truth table0%0/2
A
A= $jobid
Aresult
11
00
455 0 my $qstat=$self->qstat;
456 0 for(split(/\n/,$qstat)){
457 0 my @F=split /\s+/;
458 0 ▶ if($F[0] eq $jobid){
Branch: 1 branch
BranchTrueFalseTotal
if ($F[0] eq $jobid) 0 0 0
459 0 $state=$F[4];
460 }
461 }
462 0 close QSTAT;
463 0 return $state;
464 }
465  
466 =pod
467  
468 =over
469  
470 =item qstat
471  
472 Runs qstat and caches the result for one second. Or, returns the cached result of qstat
473  
474 =back
475  
476 =cut
477  
478 sub qstat{
479 0 ▶ my($self)=@_;
Subroutine qstat: not called CC=3 SCAR=24.8
Pod documented
480 # return the cached value if it was just accessed a second ago
481 #return $self->get("qstat") if(defined($self->get("qstat")) && $self->get("qstat_timestamp") <= time - 1);
482  
483 0 my $content="";
484 0 ▶ open(QSTAT,"qstat|") or die "ERROR: could not execute qstat! $!";
Branch: 1 branch
BranchTrueFalseTotal
open QSTAT, "qstat|" or die "ERROR: could not execute qstat! $!" 0 0 0
485 0 ▶ while(my $line=<QSTAT>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline QSTAT)) 0 0 0
486 0 $line=~s/^\s+|\s+$//g;
487 0 $content.="$line\n";
488 }
489 0 close QSTAT;
490 0 $self->set("qstat",$content);
491 0 $self->set("qstat_timestamp",time);
492 0 return $self->get("qstat");
493 }
494  
495 =pod
496  
497 =over
498  
499 =item wrapItUp()
500  
501 Waits on all jobs to finish before pausing the program.
502 Calls waitOnJobs or joinAllThreads internally. Does not take any parameters.
503  
504 =back
505  
506 =cut
507  
508 # Wait on all jobs to finish and clear out the queue.
509 sub wrapItUp{
510 1 ▶ my($self)=@_;
Subroutine wrapItUp: called CC=3 SCAR=13.3
Pod documented
511 1 ▶ if($self->get("scheduler")){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get('scheduler')) { } 0 1 1
elsif ($has_threads) { } 0 1 1
512 0 $self->waitOnJobs(\@jobsToMonitor,1);
513 } elsif($has_threads){
514 0 $self->joinAllThreads(\@jobsToMonitor,1);
515 }
516 1 return 1;
517 }
518  
519 =pod
520  
521 =over
522  
523 =item joinAllThreads($jobList)
524  
525 Joins all threads. This is if you have ithreads and if the scheduler is not set.
526 For example, if you specify noqsub or if qsub executable is not found.
527  
528 =back
529  
530 =cut
531  
532 sub joinAllThreads{
533 0 ▶ my($self,$job)=@_;
Subroutine joinAllThreads: not called CC=8 SCAR=42.8
Pod documented
534  
535 JOINALLTHREADS:
536 0 for my $j(@$job){
537 0 ▶ next if(!$$j{thread});
Branch: 1 branch
BranchTrueFalseTotal
unless $j->{"thread"} 0 0 0
538 0 ▶ next if($$j{thread} && !$$j{thread}->is_joinable);
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"thread"} and not $j->{"thread"}->is_joinable 0 0 0
Condition0%0/3
$j->{"thread"} and not $j->{"thread"}->is_joinable
!ll&&!rl&&r
000
MC/DC0%0/2
$j->{"thread"} and not $j->{"thread"}->is_joinable
$j->{"thread"} not $j->{"thread"}->is_joinable
Truth table0%0/3
A and not(B)
A= $j->{"thread"}B= not $j->{"thread"}->is_joinable
ABresult
0X0
100
111
539 0 logmsg "Joining TID".$$j{jobid};
540 0 $$j{thread}->join;
541 0 $$j{thread}=0;
542 }
543  
544 # clean out the joined jobs
545 0 my @newjob;
546 0 for my $j(@$job){
547 0 ▶ push(@newjob,$j) if($$j{thread});
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"thread"} 0 0 0
548 }
549 0 $job=\@newjob;
550  
551 # if there is still something in @$job, then go for another round
552 0 ▶ if(@$job){
Branch: 1 branch
BranchTrueFalseTotal
if (@$job) 0 0 0
553 0 logmsg "Waiting for ".scalar(@$job)." more jobs to finish...";
554 0 sleep 1;
555 0 goto JOINALLTHREADS;
556 }
557  
558 }
559  
560  
561 =pod
562  
563 =over
564  
565 =item waitOnJobs($jobList,[$mustFinish])
566  
567 Waits on all given jobs to finish. The job list are jobs as given by pleaseExecute().
568 If $mustFinish evaluates to true, then the program will pause until
569 all jobs are finished.
570 Calls on checkJob() internally. Will die with an error message if a job dies.
571  
572 =back
573  
574 =cut
575  
576 # Wait on enough jobs to finish before returning.
577 # If a job finishes, splice it from the job array.
578 sub waitOnJobs{
579 3 ▶ my($self,$job,$mustfinish)=@_;
Subroutine waitOnJobs: called CC=16 SCAR=54.1
Pod documented
580
581 # if there is no qsub, then every job is only going one at a time
582 3 my $qsub=$self->get("qsub");
583 3 ▶ return @$job if(!$qsub);
Branch: 1 branch
BranchTrueFalseTotal
unless $qsub 3 0 3
584  
585 0 my %settings=$self->settings;
586 0 ▶ $settings{mustfinish}=$mustfinish if(defined($mustfinish));
Branch: 1 branch
BranchTrueFalseTotal
if defined $mustfinish 0 0 0
587 0 ▶ if($settings{verbose}){
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{"verbose"}) 0 0 0
588 0 ▶ logmsg "We have reached node capacity ($settings{numnodes})! Waiting for a job to finish." if(@$job >= $settings{numnodes});
Branch: 1 branch
BranchTrueFalseTotal
if @$job >= $settings{"numnodes"} 0 0 0
589 0 ▶ logmsg "We have reached slot capacity ($settings{maxslots})! Waiting for a job to finish." if($numSlots >= $settings{maxslots});
Branch: 1 branch
BranchTrueFalseTotal
if $numSlots >= $settings{"maxslots"} 0 0 0
590 }
591 0 ▶ while(@$job > 0){
Branch: 1 branch
BranchTrueFalseTotal
if (@$job > 0) 0 0 0
592 0 ▶ for(my $i=0;$i<@$job;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$job) 0 0 0
593 0 ▶ $$job[$i]{jobid}||=0;
Condition0%0/2
$job->[$i]{"jobid"} ||= 0
l!l
00
MC/DC0%0/1
$job->[$i]{"jobid"} ||= 0
$job->[$i]{"jobid"}
Truth table0%0/2
A
A= $job->[$i]{"jobid"}
Aresult
11
00
594 0 my $state=$self->checkJob($$job[$i]);
595 0 ▶ if($state==1){
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 1) { } 0 0 0
elsif ($state == -1) { } 0 0 0
596 0 ▶ logmsg "A job finished: $$job[$i]{jobname} ($$job[$i]{jobid})" if($settings{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $settings{"verbose"} 0 0 0
597 0 $numSlots = $numSlots - $$job[$i]{numcpus}; # not using these slots anymore
598 0 splice(@$job,$i,1);
599 0 last;
600 } elsif($state==-1){
601 0 my $msg="A job failed ($$job[$i]{jobname} [$$job[$i]{jobid}]! Look at $$job[$i]{output} for more details.\nError message was ".$self->error()."\n".Dumper($$job[$i]);
602 0 ▶ die $msg if(!$settings{warn_on_error});
Branch: 1 branch
BranchTrueFalseTotal
unless $settings{"warn_on_error"} 0 0 0
603 # just print the warning if the script didn't die and forget about this dead job
604 0 logmsg $msg;
605 0 $numSlots = $numSlots - $$job[$i]{numcpus}; # not using these slots anymore
606 0 $self->error($msg);
607 0 splice(@$job,$i,1);
608 0 last;
609 }
610 }
611 0 sleep 1;
612 # break out if you don't have to finish yet but you can still add in another job
613 0 ▶ last if(!$settings{mustfinish} && @$job<$settings{numnodes} && $numSlots<$settings{maxslots});
Branch: 1 branch
BranchTrueFalseTotal
if not $settings{"mustfinish"} and @$job < $settings{"numnodes"} and $numSlots < $settings{"maxslots"} 0 0 0
Condition0%0/6
not $settings{"mustfinish"} and @$job < $settings{"numnodes"} and $numSlots < $settings{"maxslots"}
!ll&&!rl&&r
000
not $settings{"mustfinish"} and @$job < $settings{"numnodes"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $settings{"mustfinish"} and @$job < $settings{"numnodes"} and $numSlots < $settings{"maxslots"}
not $settings{"mustfinish"} @$job < $settings{"numnodes"} $numSlots < $settings{"maxslots"}
Truth table0%0/4
not(A) and B and C
A= not $settings{"mustfinish"}B= @$job < $settings{"numnodes"}C= $numSlots < $settings{"maxslots"}
ABCresult
0XX0
10X0
1100
1111
614 }
615 0 return @$job;
616 }
617  
618 =pod
619  
620 =over
621  
622 =item cleanAJob
623  
624 This is internally used for cleaning up files after a job is done.
625 Do not use externally.
626  
627 =back
628  
629 =cut
630  
631 sub cleanAJob{
632 2 ▶ my($job)=@_;
Subroutine cleanAJob: called CC=3 SCAR=17.3
Pod documented
633 2 ▶ my $jobid=$$job{jobid} || return 0;
Condition50%1/2
$job->{'jobid'} || (return 0)
l!l
02
MC/DC0%0/1
$job->{'jobid'} || (return 0)
$job->{'jobid'}
Truth table50%1/2
A
A= $job->{'jobid'}
Aresult
11
00
634 0 logmsg $jobid;
635 0 for (qw(running submitted finished output script died)){
636 0 unlink $$job{$_};
637 }
638  
639 0 system("qdel $$job{jobid} 2>/dev/null | grep -v 'does not exist'");
640 #die "Internal error" if $?;
641 0 return 1;
642 }
643  
644 sub mktempdir{
645 0 ▶ my ($self,$settings) = @_;
Subroutine mktempdir: not called CC=4 SCAR=30.0
Pod undocumented
646 0 ▶ $settings||={};
Condition0%0/2
$settings ||= {}
l!l
00
MC/DC0%0/1
$settings ||= {}
$settings
Truth table0%0/2
A
A= $settings
Aresult
11
00
647 # SGELK.22623.XXXXX
648 #my $tempdir_path = File::Spec->join(File::Spec->tmpdir(), (split("::",(caller(1))[3]))[1].".$$.XXXXX");
649 0 ▶ mkdir "./.SGELK" if(!-d "./.SGELK");
Branch: 1 branch
BranchTrueFalseTotal
unless -d "./.SGELK" 0 0 0
650 0 ▶ die if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
651 0 my $tempdir_path = File::Spec->join("./.SGELK",(split("::",(caller(1))[3]))[1].".$$.XXXXX");
652 0 my $tempdir = tempdir($tempdir_path, CLEANUP => !($$settings{keep}));
653 0 return $tempdir;
654 }
655  
656 sub command{
657 2 ▶ my($cmd,$use_threads,$settings)=@_;
Subroutine command: called CC=3 SCAR=11.4
Pod undocumented
658 2 my $job=0;
659 2 ▶ if($use_threads){
Branch: 1 branch
BranchTrueFalseTotal
if ($use_threads) { } 0 2 2
660 0 $job=threads->new(\&command,$cmd,0,$settings);
661 } else {
662 2 logmsg "Running $cmd";
663 2 system($cmd);
664 2 ▶ die "ERROR with command: $!\n $cmd" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 2 2
665 }
666  
667 2 return $job;
668 }
669  
670 =pod
671  
672 =over
673  
674 =item test
675  
676 Use this method to perform a test. The test sends
677 ten jobs that print debugging information.
678  
679 You can give an optional hash argument to send other settings as described in new().
680  
681 perl -MSchedule::SGELK -e '$sge=Schedule::SGELK->new(-numnodes=>2,-numcpus=>8); $sge->test(\%tmpSettings);'
682  
683 =back
684  
685 =cut
686  
687 sub test{
688 0 ▶ my($self,$tmpSettings)=@_;
Subroutine test: not called CC=5 SCAR=34.0
Pod documented
689  
690 # get settings
691 0 my %settings=%{ $self->settings };
692 # read in any temporary settings for this command
693 0 ▶ $tmpSettings={} if(!defined($tmpSettings));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmpSettings 0 0 0
694 0 $$tmpSettings{verbose}=1; # make sure it's verbose for debugging
695 0 $settings{$_}=$$tmpSettings{$_} for(keys(%$tmpSettings));
696  
697 # execute the jobs
698 0 for(1..$self->get("numnodes")){
699 0 logmsg "Job $_ is being submitted";
700 0 my $text="Job count\t$_\n";
701 0 $text.="$_\t$settings{$_}\n" for(keys(%settings));
702 0 $self->pleaseExecute("echo '$text'|column -t",$tmpSettings);
703 }
704 0 $self->wrapItUp();
705 0 return 1;
706 }
707  
708 1 1;
« blib/lib/Schedule/SGELK.pm ↑ Summary SGELK.log/qsub.415372.pl »