blib/lib/HPCD/SGE/Run.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 144 CC 10 SCAR 28.5
CC
10
Cov
0%
_collect_qacct_info
42.0
_get_submit_command
20.0
_res_value_map
6.0
CRAP 17.2
1 package HPCD::SGE::Run;
2  
3 ### INCLUDES ######################################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8 use Carp;
9 use HPCD::SGE::DRMAACheck;
10  
11 use Moose;
12  
13 sub _register_status {
14 }
15  
16 sub _collect_job_stats {
17 }
18  
19 with 'HPCI::Run' => { theDriver => 'HPCD::SGE' }, @HPCD::SGE::DRMAACheck::RunWith;
20  
21 my %job_info_mappings = (
22 jobname => 'name',
23 jobnumber => 'number',
24 taskid => 'task_number',
25 qname => 'queue_name',
26 hostname => 'host',
27 qsub_time => 'submission_time',
28 ru_wallclock => 'wallclock_time',
29 cpu => 'cpu_time',
30 io => 'io_transfers_made',
31 iow => 'io_wait_time',
32 ru_maxrss => 'peak_real_memory',
33 maxvmem => 'peak_virtual_memory',
34 mem => 'integral_memory_time',
35 exit_status => 'qacct_exit_status'
36 );
37  
38 my @job_info_fields = (
39 qw(
40 account arid department end_time failed granted_pe group
41 owner priority project ru_idrss ru_inblock ru_ismrss ru_isrss
42 ru_ixrss ru_majflt ru_minflt ru_msgrcv ru_msgsnd ru_nivcsw ru_nsignals
43 ru_nswap ru_nvcsw ru_oublock ru_stime ru_utime slots start_time
44 )
45 );
46  
47  
48 has 'stats_keys' => (
49 is => 'ro',
50 isa => 'ArrayRef[Str]',
51 init_arg => undef,
52 default => sub { [ values %job_info_mappings, @job_info_fields ] }
53 );
54  
55 has '_cpu_multiplier' => (
56 is => 'rw',
57 default => 1,
58 init_arg => undef
59 );
60  
61 after '_collect_job_stats' => sub {
62 my $self = shift;
63 my $status = shift;
64  
65 my $stath = $self->stats;
66  
67 my $stage = $self->stage;
68 my $id = $self->unique_id;
69  
70 $stath->{exit_status} = $self->status;
71 $stath->{aborted} = $self->abort if $self->abort;
72 $stath->{internal_killed} = $self->killsignal if $self->kill;
73  
74 $stage->info("collecting job stats ($id)\n");
75 my $qacct_info = $self->_collect_qacct_info;
76 while (my ( $k, $v ) = each %$qacct_info) {
77 $k = $job_info_mappings{$k} // $k;
78 $stath->{$k} = $v;
79 }
80 $self->_base_register_status( $stath->{qacct_exit_status} );
81 $stath->{killed} = $self->killsignal if $self->killsignal;
82 };
83  
84 sub _collect_qacct_info {
85 my $self = shift;
86  
87 my $id = $self->unique_id;
88 my %info = ();
89 my $retries = 0;
90  
91 GET_STATS:
92 while (1) {
93 open my $fh, '-|', "qacct -j $id 2>&1"
94 or $self->croak("cannot open qacct command: $!");
95 my $divcnt = 0;
96 %info = ();
97 while (<$fh>) {
98 next GET_STATS if /^error:/;
99 if (/^===*$/) {
100 %info = ();
101 $self->warn(
102 "found duplicate jobs with same id ($id), using last one listed in qacct"
103 ) if ++$divcnt == 2;
104 next;
105 }
106 if (my ( $key, $val ) = /(\w+)\s+(.*?)\s*$/) {
107 $info{$key} = $val;
108 }
109 }
110 }
111 continue {
112 last GET_STATS if keys %info || ++$retries == 5;
113 sleep(10);
114 }
115 return \%info;
116 }
117  
118 around 'soft_timeout' => sub {
119 my $orig = shift;
120 my $self = shift;
121 $self->_delete_job;
122 };
123  
124 around 'hard_timeout' => sub {
125 my $orig = shift;
126 my $self = shift;
127 $self->_delete_job;
128 sleep(2);
129 $self->$orig(@_);
130 };
131  
132 sub _delete_job {
133 my $self = shift;
134 my $id = $self->unique_id;
135 $self->info( "Running: (qdel $id) to terminate stage job" );
136 system( "qdel $id" );
137 }
138  
139 sub _res_value_map {
140 my $self = shift;
141 my $key = shift;
142 my $val = shift;
143 $val = $self->stage->_time_to_secs( $val )
144 if $key =~ /^[hs]_rt$/;
145 return "$key=$val";
146 }
147  
148 sub _get_mapped_resource_string {
149 my $self = shift;
150 my $res = shift;
151 join( ",",
152 map { $self->_res_value_map( $_, $res->{$_} ) }
153 sort keys %$res
154 );
155 }
156  
157 sub _get_submit_command {
158 my $self = shift;
159  
160 my $stage = $self->stage;
161 my $shell_script = $stage->script_file;
162 my $name = $self->unique_id;
163 my $extras = $stage->extra_sge_args_string;
164 my $res = $stage->_use_resources_required;
165 my $resources = $self->_get_mapped_resource_string( $res );
166 $resources = "-l $resources" if $resources;
167  
168 my $qsub_command = join( ' ',
169 "qsub",
170 ( length($extras) ? $extras : () ),
171 ( length($resources) ? $resources : () ),
172 "-N '$name'",
173 "-o", $self->_stdout,
174 "-e", $self->_stderr,
175 "-sync y", "-cwd",
176 "-S /bin/bash",
177 "-b y", "'$shell_script'" );
178  
179 return $qsub_command;
180 }
181  
182 1;