1 | /* $Id: job.c 370 2010-11-16 09:51:00Z mamonski $ */ |
---|
2 | /* |
---|
3 | * FedStage DRMAA for PBS Pro |
---|
4 | * Copyright (C) 2006-2009 FedStage Systems |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <signal.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <string.h> |
---|
27 | #include <unistd.h> |
---|
28 | |
---|
29 | #include <drmaa_utils/drmaa.h> |
---|
30 | #include <drmaa_utils/drmaa_util.h> |
---|
31 | #include <pbs_error.h> |
---|
32 | #include <pbs_ifl.h> |
---|
33 | |
---|
34 | #include <pbs_drmaa/job.h> |
---|
35 | #include <pbs_drmaa/pbs_attrib.h> |
---|
36 | #include <pbs_drmaa/session.h> |
---|
37 | #include <pbs_drmaa/util.h> |
---|
38 | |
---|
39 | #ifndef lint |
---|
40 | static char rcsid[] |
---|
41 | # ifdef __GNUC__ |
---|
42 | __attribute__ ((unused)) |
---|
43 | # endif |
---|
44 | = "$Id: job.c 370 2010-11-16 09:51:00Z mamonski $"; |
---|
45 | #endif |
---|
46 | |
---|
47 | |
---|
48 | static void |
---|
49 | pbsdrmaa_job_control( fsd_job_t *self, int action ); |
---|
50 | |
---|
51 | static void |
---|
52 | pbsdrmaa_job_update_status( fsd_job_t *self ); |
---|
53 | |
---|
54 | static void |
---|
55 | pbsdrmaa_job_on_missing( fsd_job_t *self ); |
---|
56 | |
---|
57 | static void |
---|
58 | pbsdrmaa_job_update( fsd_job_t *self, struct batch_status* ); |
---|
59 | |
---|
60 | |
---|
61 | fsd_job_t * |
---|
62 | pbsdrmaa_job_new( char *job_id ) |
---|
63 | { |
---|
64 | pbsdrmaa_job_t *self = (pbsdrmaa_job_t*)fsd_job_new( job_id ); |
---|
65 | fsd_realloc( self, 1, pbsdrmaa_job_t ); |
---|
66 | self->super.control = pbsdrmaa_job_control; |
---|
67 | self->super.update_status = pbsdrmaa_job_update_status; |
---|
68 | self->super.on_missing = pbsdrmaa_job_on_missing; |
---|
69 | self->update = pbsdrmaa_job_update; |
---|
70 | return (fsd_job_t*)self; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | static void |
---|
75 | pbsdrmaa_job_control( fsd_job_t *self, int action ) |
---|
76 | { |
---|
77 | volatile bool conn_lock = false; |
---|
78 | pbsdrmaa_session_t *session = (pbsdrmaa_session_t*)self->session; |
---|
79 | const char *job_id = self->job_id; |
---|
80 | const char *apicall = NULL; |
---|
81 | int rc = PBSE_NONE; |
---|
82 | |
---|
83 | fsd_log_enter(( "({job_id=%s}, action=%d)", |
---|
84 | self->job_id, action )); |
---|
85 | |
---|
86 | TRY |
---|
87 | { |
---|
88 | int try_count; |
---|
89 | const int max_tries = 3; |
---|
90 | |
---|
91 | conn_lock = fsd_mutex_lock( &self->session->drm_connection_mutex ); |
---|
92 | |
---|
93 | /*TODO reconnect */ |
---|
94 | for( try_count=0; try_count < max_tries; try_count++ ) |
---|
95 | { |
---|
96 | switch( action ) |
---|
97 | { |
---|
98 | /* |
---|
99 | * We cannot know whether we did suspend job |
---|
100 | * in other way than remembering this inside DRMAA session. |
---|
101 | */ |
---|
102 | case DRMAA_CONTROL_SUSPEND: |
---|
103 | apicall = "pbs_sigjob"; |
---|
104 | rc = pbs_sigjob( session->pbs_conn, (char*)job_id, |
---|
105 | "SIGSTOP", NULL ); |
---|
106 | if( rc == PBSE_NONE ) |
---|
107 | self->flags |= FSD_JOB_SUSPENDED; |
---|
108 | break; |
---|
109 | case DRMAA_CONTROL_RESUME: |
---|
110 | apicall = "pbs_sigjob"; |
---|
111 | rc = pbs_sigjob( session->pbs_conn, (char*)job_id, |
---|
112 | "SIGCONT", NULL ); |
---|
113 | if( rc == PBSE_NONE ) |
---|
114 | self->flags &= ~FSD_JOB_SUSPENDED; |
---|
115 | break; |
---|
116 | case DRMAA_CONTROL_HOLD: |
---|
117 | apicall = "pbs_holdjob"; |
---|
118 | rc = pbs_holdjob( session->pbs_conn, (char*)job_id, |
---|
119 | USER_HOLD, NULL ); |
---|
120 | if( rc == PBSE_NONE ) |
---|
121 | self->flags |= FSD_JOB_HOLD; |
---|
122 | break; |
---|
123 | case DRMAA_CONTROL_RELEASE: |
---|
124 | apicall = "pbs_rlsjob"; |
---|
125 | rc = pbs_rlsjob( session->pbs_conn, (char*)job_id, |
---|
126 | USER_HOLD, NULL ); |
---|
127 | if( rc == PBSE_NONE ) |
---|
128 | self->flags &= FSD_JOB_HOLD; |
---|
129 | break; |
---|
130 | case DRMAA_CONTROL_TERMINATE: |
---|
131 | apicall = "pbs_deljob"; |
---|
132 | rc = pbs_deljob( session->pbs_conn, (char*)job_id, NULL ); |
---|
133 | /* Torque: |
---|
134 | * deldelay=N -- delay between SIGTERM and SIGKILL (default 0) */ |
---|
135 | if( rc == PBSE_NONE ) |
---|
136 | { |
---|
137 | self->flags &= FSD_JOB_TERMINATED_MASK; |
---|
138 | if( (self->flags & FSD_JOB_TERMINATED) == 0 ) |
---|
139 | self->flags |= FSD_JOB_TERMINATED | FSD_JOB_ABORTED; |
---|
140 | } |
---|
141 | break; |
---|
142 | } |
---|
143 | |
---|
144 | if( rc == PBSE_NONE ) |
---|
145 | break; |
---|
146 | else if( rc == PBSE_INTERNAL ) |
---|
147 | { |
---|
148 | /* |
---|
149 | * In PBS Pro pbs_sigjob raises internal server error (PBSE_INTERNAL) |
---|
150 | * when job just changed its state to running. |
---|
151 | */ |
---|
152 | fsd_log_debug(( "repeating request (%d of %d)", |
---|
153 | try_count+2, max_tries )); |
---|
154 | sleep( 1 ); |
---|
155 | } |
---|
156 | else |
---|
157 | pbsdrmaa_exc_raise_pbs( apicall ); |
---|
158 | } /* end for */ |
---|
159 | } |
---|
160 | FINALLY |
---|
161 | { |
---|
162 | if( conn_lock ) |
---|
163 | conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex ); |
---|
164 | } |
---|
165 | END_TRY |
---|
166 | |
---|
167 | fsd_log_return(("")); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void |
---|
172 | pbsdrmaa_job_update_status( fsd_job_t *self ) |
---|
173 | { |
---|
174 | volatile bool conn_lock = false; |
---|
175 | struct batch_status *volatile status = NULL; |
---|
176 | pbsdrmaa_session_t *session = (pbsdrmaa_session_t*)self->session; |
---|
177 | |
---|
178 | fsd_log_enter(( "({job_id=%s})", self->job_id )); |
---|
179 | TRY |
---|
180 | { |
---|
181 | conn_lock = fsd_mutex_lock( &self->session->drm_connection_mutex ); |
---|
182 | retry: |
---|
183 | #ifdef PBS_PROFESSIONAL |
---|
184 | status = pbs_statjob( session->pbs_conn, self->job_id, NULL, NULL ); |
---|
185 | #else |
---|
186 | status = pbs_statjob( session->pbs_conn, self->job_id, session->status_attrl, NULL ); |
---|
187 | #endif |
---|
188 | fsd_log_debug(( "pbs_statjob(fd=%d, job_id=%s, attribs={...}) =%p", |
---|
189 | session->pbs_conn, self->job_id, (void*)status )); |
---|
190 | if( status == NULL ) |
---|
191 | { |
---|
192 | #ifndef PBS_PROFESSIONAL |
---|
193 | fsd_log_error(("pbs_statjob error: %d, %s, %s", pbs_errno, pbse_to_txt(pbs_errno), pbs_strerror(pbs_errno))); |
---|
194 | #else |
---|
195 | fsd_log_error(("pbs_statjob error: %d, %s", pbs_errno, pbse_to_txt(pbs_errno))); |
---|
196 | #endif |
---|
197 | switch( pbs_errno ) |
---|
198 | { |
---|
199 | case PBSE_UNKJOBID: |
---|
200 | break; |
---|
201 | case PBSE_PROTOCOL: |
---|
202 | case PBSE_EXPIRED: |
---|
203 | pbs_disconnect( session->pbs_conn ); |
---|
204 | sleep(1); |
---|
205 | session->pbs_conn = pbs_connect( session->super.contact ); |
---|
206 | if( session->pbs_conn < 0 ) |
---|
207 | pbsdrmaa_exc_raise_pbs( "pbs_connect" ); |
---|
208 | else |
---|
209 | { |
---|
210 | fsd_log_error(("retry:")); |
---|
211 | goto retry; |
---|
212 | } |
---|
213 | default: |
---|
214 | pbsdrmaa_exc_raise_pbs( "pbs_statjob" ); |
---|
215 | break; |
---|
216 | case 0: /* ? */ |
---|
217 | fsd_exc_raise_code( FSD_ERRNO_INTERNAL_ERROR ); |
---|
218 | break; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex ); |
---|
223 | |
---|
224 | if( status != NULL ) |
---|
225 | { |
---|
226 | ((pbsdrmaa_job_t*)self)->update( self, status ); |
---|
227 | } |
---|
228 | else |
---|
229 | self->on_missing( self ); |
---|
230 | } |
---|
231 | FINALLY |
---|
232 | { |
---|
233 | if( conn_lock ) |
---|
234 | conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex ); |
---|
235 | if( status != NULL ) |
---|
236 | pbs_statfree( status ); |
---|
237 | } |
---|
238 | END_TRY |
---|
239 | |
---|
240 | fsd_log_return(("")); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | void |
---|
245 | pbsdrmaa_job_update( fsd_job_t *self, struct batch_status *status ) |
---|
246 | { |
---|
247 | struct attrl *attribs = status->attribs; |
---|
248 | struct attrl *i = NULL; |
---|
249 | char pbs_state = 0; |
---|
250 | int exit_status = -2; |
---|
251 | const char *cpu_usage = NULL; |
---|
252 | const char *mem_usage = NULL; |
---|
253 | const char *vmem_usage = NULL; |
---|
254 | const char *walltime = NULL; |
---|
255 | long unsigned int modify_time = 0; |
---|
256 | |
---|
257 | fsd_log_enter(( "({job_id=%s})", self->job_id )); |
---|
258 | #ifdef DEBUGGING |
---|
259 | pbsdrmaa_dump_attrl( attribs, NULL ); |
---|
260 | #endif |
---|
261 | fsd_assert( !strcmp( self->job_id, status->name ) ); |
---|
262 | |
---|
263 | for( i = attribs; i != NULL; i = i->next ) |
---|
264 | { |
---|
265 | int attr; |
---|
266 | attr = pbsdrmaa_pbs_attrib_by_name( i->name ); |
---|
267 | switch( attr ) |
---|
268 | { |
---|
269 | case PBSDRMAA_ATTR_JOB_STATE: |
---|
270 | pbs_state = i->value[0]; |
---|
271 | break; |
---|
272 | case PBSDRMAA_ATTR_EXIT_STATUS: |
---|
273 | exit_status = atoi( i->value ); |
---|
274 | break; |
---|
275 | case PBSDRMAA_ATTR_RESOURCES_USED: |
---|
276 | if( !strcmp( i->resource, "cput" ) ) |
---|
277 | cpu_usage = i->value; |
---|
278 | else if( !strcmp( i->resource, "mem" ) ) |
---|
279 | mem_usage = i->value; |
---|
280 | else if( !strcmp( i->resource, "vmem" ) ) |
---|
281 | vmem_usage = i->value; |
---|
282 | else if( !strcmp( i->resource, "walltime" ) ) |
---|
283 | walltime = i->value; |
---|
284 | break; |
---|
285 | case PBSDRMAA_ATTR_QUEUE: |
---|
286 | if (!self->queue) |
---|
287 | self->queue = fsd_strdup(i->value); |
---|
288 | break; |
---|
289 | case PBSDRMAA_ATTR_ACCOUNT_NAME: |
---|
290 | if (!self->project) |
---|
291 | self->project = fsd_strdup(i->value); |
---|
292 | break; |
---|
293 | case PBSDRMAA_ATTR_EXECUTION_HOST: |
---|
294 | if (!self->execution_hosts) { |
---|
295 | fsd_log_debug(("execution_hosts = %s", i->value)); |
---|
296 | self->execution_hosts = fsd_strdup(i->value); |
---|
297 | } |
---|
298 | break; |
---|
299 | case PBSDRMAA_ATTR_START_TIME: |
---|
300 | { |
---|
301 | long unsigned int start_time; |
---|
302 | if (self->start_time == 0 && sscanf(i->value, "%lu", &start_time) == 1) |
---|
303 | self->start_time = start_time; |
---|
304 | break; |
---|
305 | } |
---|
306 | case PBSDRMAA_ATTR_MTIME: |
---|
307 | if (sscanf(i->value, "%lu", &modify_time) != 1) |
---|
308 | modify_time = 0; |
---|
309 | break; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | if( pbs_state ) |
---|
314 | fsd_log_debug(( "pbs_state: %c", pbs_state )); |
---|
315 | |
---|
316 | if( exit_status != -2 ) |
---|
317 | { |
---|
318 | fsd_log_debug(( "exit_status: %d", exit_status )); |
---|
319 | self->exit_status = exit_status; |
---|
320 | } |
---|
321 | if(pbs_state){ |
---|
322 | switch( pbs_state ) |
---|
323 | { |
---|
324 | case 'C': /* Job is completed after having run. */ |
---|
325 | self->flags &= FSD_JOB_TERMINATED_MASK; |
---|
326 | self->flags |= FSD_JOB_TERMINATED; |
---|
327 | if (exit_status != -2) { /* has exit code */ |
---|
328 | if( self->exit_status == 0) |
---|
329 | self->state = DRMAA_PS_DONE; |
---|
330 | else |
---|
331 | self->state = DRMAA_PS_FAILED; |
---|
332 | } else { |
---|
333 | self->state = DRMAA_PS_FAILED; |
---|
334 | self->exit_status = -1; |
---|
335 | } |
---|
336 | self->end_time = modify_time; /* take last modify time as end time */ |
---|
337 | break; |
---|
338 | case 'E': /* Job is exiting after having run. - MM: ignore exiting state (transient state) - outputs might have not been transfered yet, |
---|
339 | MM2: mark job as running if current job status is undetermined - fix "ps after job was ripped" */ |
---|
340 | if (self->state == DRMAA_PS_UNDETERMINED) |
---|
341 | self->state = DRMAA_PS_RUNNING; |
---|
342 | break; |
---|
343 | case 'H': /* Job is held. */ |
---|
344 | self->state = DRMAA_PS_USER_ON_HOLD; |
---|
345 | self->flags |= FSD_JOB_HOLD; |
---|
346 | break; |
---|
347 | case 'Q': /* Job is queued, eligible to run or routed. */ |
---|
348 | case 'W': /* Job is waiting for its execution time to be reached. */ |
---|
349 | self->state = DRMAA_PS_QUEUED_ACTIVE; |
---|
350 | self->flags &= ~FSD_JOB_HOLD; |
---|
351 | break; |
---|
352 | case 'R': /* Job is running. */ |
---|
353 | case 'T': /* Job is being moved to new location (?). */ |
---|
354 | { |
---|
355 | if( self->flags & FSD_JOB_SUSPENDED ) |
---|
356 | self->state = DRMAA_PS_USER_SUSPENDED; |
---|
357 | else |
---|
358 | self->state = DRMAA_PS_RUNNING; |
---|
359 | break; |
---|
360 | } |
---|
361 | case 'S': /* (Unicos only) job is suspend. */ |
---|
362 | self->state = DRMAA_PS_SYSTEM_SUSPENDED; |
---|
363 | break; |
---|
364 | case 0: default: |
---|
365 | self->state = DRMAA_PS_UNDETERMINED; |
---|
366 | break; |
---|
367 | } |
---|
368 | } |
---|
369 | fsd_log_debug(( "job_ps: %s", drmaa_job_ps_to_str(self->state) )); |
---|
370 | |
---|
371 | { |
---|
372 | int hours, minutes, seconds; |
---|
373 | long mem; |
---|
374 | if( cpu_usage && sscanf( cpu_usage, "%d:%d:%d", |
---|
375 | &hours, &minutes, &seconds ) == 3 ) |
---|
376 | { |
---|
377 | self->cpu_usage = 60*( 60*hours + minutes ) + seconds; |
---|
378 | fsd_log_debug(( "cpu_usage: %s=%lds", cpu_usage, self->cpu_usage )); |
---|
379 | } |
---|
380 | if( mem_usage && sscanf( mem_usage, "%ldkb", &mem ) == 1 ) |
---|
381 | { |
---|
382 | self->mem_usage = 1024*mem; |
---|
383 | fsd_log_debug(( "mem_usage: %s=%ldB", mem_usage, self->mem_usage )); |
---|
384 | } |
---|
385 | if( vmem_usage && sscanf( vmem_usage, "%ldkb", &mem ) == 1 ) |
---|
386 | { |
---|
387 | self->vmem_usage = 1024*mem; |
---|
388 | fsd_log_debug(( "vmem_usage: %s=%ldB", vmem_usage, self->vmem_usage )); |
---|
389 | } |
---|
390 | if( walltime && sscanf( walltime, "%d:%d:%d", |
---|
391 | &hours, &minutes, &seconds ) == 3 ) |
---|
392 | { |
---|
393 | self->walltime = 60*( 60*hours + minutes ) + seconds; |
---|
394 | fsd_log_debug(( "walltime: %s=%lds", walltime, self->walltime )); |
---|
395 | } |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | |
---|
400 | void |
---|
401 | pbsdrmaa_job_on_missing( fsd_job_t *self ) |
---|
402 | { |
---|
403 | fsd_drmaa_session_t *session = self->session; |
---|
404 | pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*)session; |
---|
405 | if( !pbssession->wait_thread_log){ |
---|
406 | unsigned missing_mask = 0; |
---|
407 | |
---|
408 | fsd_log_enter(( "({job_id=%s})", self->job_id )); |
---|
409 | fsd_log_warning(( "self %s missing from DRM queue", self->job_id )); |
---|
410 | |
---|
411 | switch( session->missing_jobs ) |
---|
412 | { |
---|
413 | case FSD_REVEAL_MISSING_JOBS: missing_mask = 0; break; |
---|
414 | case FSD_IGNORE_MISSING_JOBS: missing_mask = 0x73; break; |
---|
415 | case FSD_IGNORE_QUEUED_MISSING_JOBS: missing_mask = 0x13; break; |
---|
416 | } |
---|
417 | fsd_log_debug(( "last job_ps: %s (0x%02x); mask: 0x%02x", |
---|
418 | drmaa_job_ps_to_str(self->state), self->state, missing_mask )); |
---|
419 | |
---|
420 | if( self->state < DRMAA_PS_DONE |
---|
421 | && (self->state & ~missing_mask) ) |
---|
422 | fsd_exc_raise_fmt( |
---|
423 | FSD_ERRNO_INTERNAL_ERROR, |
---|
424 | "self %s missing from queue", self->job_id |
---|
425 | ); |
---|
426 | |
---|
427 | if( (self->flags & FSD_JOB_TERMINATED_MASK) == 0 ) |
---|
428 | { |
---|
429 | self->flags &= FSD_JOB_TERMINATED_MASK; |
---|
430 | self->flags |= FSD_JOB_TERMINATED; |
---|
431 | } |
---|
432 | |
---|
433 | if( (self->flags & FSD_JOB_ABORTED) == 0 |
---|
434 | && session->missing_jobs == FSD_IGNORE_MISSING_JOBS ) |
---|
435 | { /* assume everthing was ok */ |
---|
436 | self->state = DRMAA_PS_DONE; |
---|
437 | self->exit_status = 0; |
---|
438 | } |
---|
439 | else |
---|
440 | { /* job aborted */ |
---|
441 | self->state = DRMAA_PS_FAILED; |
---|
442 | self->exit_status = -1; |
---|
443 | } |
---|
444 | |
---|
445 | fsd_log_return(( "; job_ps=%s, exit_status=%d", |
---|
446 | drmaa_job_ps_to_str(self->state), self->exit_status )); |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|