source: trunk/pbs_drmaa/job.c @ 103

Revision 103, 12.3 KB checked in by pkopta, 9 years ago (diff)
  • Property svn:keywords set to Id
Line 
1/* $Id$ */
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#include <sys/stat.h>
29
30#include <drmaa_utils/drmaa.h>
31#include <drmaa_utils/drmaa_util.h>
32#include <pbs_error.h>
33#include <pbs_ifl.h>
34
35#include <pbs_drmaa/job.h>
36#include <pbs_drmaa/log_reader.h>
37#include <pbs_drmaa/pbs_attrib.h>
38#include <pbs_drmaa/session.h>
39#include <pbs_drmaa/util.h>
40
41#ifndef lint
42static char rcsid[]
43#       ifdef __GNUC__
44                __attribute__ ((unused))
45#       endif
46        = "$Id$";
47#endif
48
49
50static void pbsdrmaa_job_control( fsd_job_t *self, int action );
51
52static void pbsdrmaa_job_update_status( fsd_job_t *self );
53
54static void pbsdrmaa_job_on_missing( fsd_job_t *self );
55
56static void pbsdrmaa_job_on_missing_standard( fsd_job_t *self );
57
58static void pbsdrmaa_job_update( fsd_job_t *self, struct batch_status* );
59
60static int pbsdrmaa_job_read_exit_status( const char *job_id, const char *job_state_dir_prefix);
61
62fsd_job_t *
63pbsdrmaa_job_new( char *job_id )
64{
65        pbsdrmaa_job_t *self = (pbsdrmaa_job_t*)fsd_job_new( job_id );
66        fsd_realloc( self, 1, pbsdrmaa_job_t );
67        self->super.control = pbsdrmaa_job_control;
68        self->super.update_status = pbsdrmaa_job_update_status;
69        self->super.on_missing = pbsdrmaa_job_on_missing;
70        self->missing_time = 0;
71        self->update = pbsdrmaa_job_update;
72        return (fsd_job_t*)self;
73}
74
75
76static void
77pbsdrmaa_job_control( fsd_job_t *self, int action )
78{
79        pbsdrmaa_session_t *session = (pbsdrmaa_session_t*)self->session;
80        const char *job_id = self->job_id;
81
82        fsd_log_enter(( "({job_id=%s}, action=%d)", self->job_id, action ));
83
84        switch( action )
85         {
86                /*
87                 * We cannot know whether we did suspend job
88                 * in other way than remembering this inside DRMAA session.
89                 */
90                case DRMAA_CONTROL_SUSPEND:
91                        session->pbs_connection->sigjob( session->pbs_connection, (char*)job_id, "SIGSTOP");
92                        self->flags |= FSD_JOB_SUSPENDED;
93                        break;
94                case DRMAA_CONTROL_RESUME:
95                        session->pbs_connection->sigjob( session->pbs_connection, (char*)job_id, "SIGCONT");
96                        self->flags &= ~FSD_JOB_SUSPENDED;
97                        break;
98                case DRMAA_CONTROL_HOLD:
99                        session->pbs_connection->holdjob( session->pbs_connection, (char*)job_id );
100                        self->flags |= FSD_JOB_HOLD;
101                        break;
102                case DRMAA_CONTROL_RELEASE:
103                        session->pbs_connection->rlsjob( session->pbs_connection, (char*)job_id );
104                        self->flags &= ~FSD_JOB_HOLD;
105                        break;
106                case DRMAA_CONTROL_TERMINATE:
107                        session->pbs_connection->deljob( session->pbs_connection, (char*)job_id );
108                        /* TODO: make deldelay configurable ???:
109                         * deldelay=N -- delay between SIGTERM and SIGKILL (default 0) */
110                        self->flags &= FSD_JOB_TERMINATED_MASK;
111                        if( (self->flags & FSD_JOB_TERMINATED) == 0 )
112                                self->flags |= FSD_JOB_TERMINATED | FSD_JOB_ABORTED;
113                        break;
114         }
115
116        fsd_log_return((""));
117}
118
119
120void
121pbsdrmaa_job_update_status( fsd_job_t *self )
122{
123        struct batch_status *volatile status = NULL;
124        pbsdrmaa_session_t *session = (pbsdrmaa_session_t*)self->session;
125
126        fsd_log_enter(( "({job_id=%s})", self->job_id ));
127       
128        TRY
129         {
130
131#ifdef PBS_PROFESSIONAL
132                status = session->pbs_connection->statjob( session->pbs_connection, self->job_id, NULL);
133#else
134                status = session->pbs_connection->statjob( session->pbs_connection, self->job_id, session->status_attrl);
135#endif
136
137                if( status != NULL )
138                 {
139                        ((pbsdrmaa_job_t*)self)->update( self, status );
140                 }
141                else if( self->state < DRMAA_PS_DONE )
142                 {
143                        self->on_missing( self );
144                 }
145         }
146        FINALLY
147         {
148                if( status != NULL )
149                        session->pbs_connection->statjob_free( session->pbs_connection, status );
150         }
151        END_TRY
152
153        fsd_log_return((""));
154}
155
156
157void
158pbsdrmaa_job_update( fsd_job_t *self, struct batch_status *status )
159{
160        struct attrl *attribs = status->attribs;
161        struct attrl *i = NULL;
162        char pbs_state = 0;
163        int exit_status = -101;
164        const char *cpu_usage = NULL;
165        const char *mem_usage = NULL;
166        const char *vmem_usage = NULL;
167        const char *walltime = NULL;
168        long unsigned int modify_time = 0;
169
170        fsd_log_enter(( "({job_id=%s})", self->job_id ));
171#ifdef DEBUGGING
172        pbsdrmaa_dump_attrl( attribs, NULL );
173#endif
174        fsd_assert( !strcmp( self->job_id, status->name ) );
175
176        for( i = attribs;  i != NULL;  i = i->next )
177         {
178                int attr;
179                attr = pbsdrmaa_pbs_attrib_by_name( i->name );
180                switch( attr )
181                 {
182                        case PBSDRMAA_ATTR_JOB_STATE:
183                                pbs_state = i->value[0];                               
184                                break;
185                        case PBSDRMAA_ATTR_EXIT_STATUS:
186                                exit_status = fsd_atoi( i->value );
187                                break;
188                        case PBSDRMAA_ATTR_RESOURCES_USED:
189                                if( !strcmp( i->resource, "cput" ) )
190                                        cpu_usage = i->value;
191                                else if( !strcmp( i->resource, "mem" ) )
192                                        mem_usage = i->value;
193                                else if( !strcmp( i->resource, "vmem" ) )
194                                        vmem_usage = i->value;
195                                else if( !strcmp( i->resource, "walltime" ) )
196                                        walltime = i->value;
197                                break;
198                        case PBSDRMAA_ATTR_QUEUE:
199                                if (!self->queue)
200                                        self->queue = fsd_strdup(i->value);
201                                break;
202                        case PBSDRMAA_ATTR_ACCOUNT_NAME:
203                                if (!self->project)
204                                        self->project = fsd_strdup(i->value);
205                                break;
206#ifndef PBS_PROFESSIONAL
207                        case PBSDRMAA_ATTR_EXECUTION_HOST:
208                                if (!self->execution_hosts) {
209                                        fsd_log_debug(("execution_hosts = %s", i->value));
210                                        self->execution_hosts = fsd_strdup(i->value);
211                                }
212                                break;
213#else
214                        case PBSDRMAA_ATTR_EXECUTION_VNODE:
215                                if (!self->execution_hosts) {
216                                        fsd_log_debug(("execution_hosts = %s", i->value));
217                                        self->execution_hosts = fsd_strdup(i->value);
218                                }
219                                break;
220#endif
221                        case PBSDRMAA_ATTR_START_TIME:
222                                {
223                                  long unsigned int start_time;
224                                  if (self->start_time == 0 && sscanf(i->value, "%lu", &start_time) == 1)
225                                        self->start_time = start_time;
226                                  break;
227                                }
228                        case PBSDRMAA_ATTR_MTIME:
229                                if (sscanf(i->value, "%lu", &modify_time) != 1)
230                                        modify_time = 0;
231                                break;
232                 }
233         }
234
235        if( pbs_state )
236                fsd_log_debug(( "pbs_state: %c", pbs_state ));
237
238        if( exit_status != -101 )
239         {
240                fsd_log_debug(( "exit_status: %d", exit_status ));
241                self->exit_status = exit_status;
242
243                if (self->exit_status < 0)
244                 {
245                        self->exit_status = -1;
246                        fsd_log_error(("ExitStatus = %d, probably system problem, report job %s to the local administrator", exit_status, self->job_id));
247                 }
248
249         }
250        if(pbs_state){
251                switch( pbs_state )
252                 {
253                        case 'C': /* Job is completed after having run. */
254#ifdef PBS_PROFESSIONAL
255                        case 'F': /* Job is completed after having run. */
256#endif
257                                self->flags &= FSD_JOB_TERMINATED_MASK;
258                                self->flags |= FSD_JOB_TERMINATED;
259                                if (exit_status != -101) { /* has exit code */
260                                        if( self->exit_status == 0)
261                                                self->state = DRMAA_PS_DONE;
262                                        else
263                                                self->state = DRMAA_PS_FAILED;
264                                } else {
265                                        self->state = DRMAA_PS_FAILED;
266                                        self->exit_status = -1;
267                                }
268                                if (modify_time != 0)
269                                        self->end_time = modify_time; /* take last modify time as end time */
270                                else
271                                        self->end_time = time(NULL);
272                               
273                                if (self->start_time == 0)
274                                        self->start_time = self->end_time;
275
276                                break;
277                        case 'E': /* Job is exiting after having run. - MM: ignore exiting state (transient state) - outputs might have not been transfered yet,
278                                        MM2: mark job as running if current job status is undetermined - fix "ps after job was ripped" */
279                                if (self->state == DRMAA_PS_UNDETERMINED)
280                                        self->state = DRMAA_PS_RUNNING;
281                                break;
282                        case 'H': /* Job is held. */
283                                self->state = DRMAA_PS_USER_ON_HOLD;
284                                self->flags |= FSD_JOB_HOLD;
285                                break;
286                        case 'Q': /* Job is queued, eligible to run or routed. */
287                        case 'W': /* Job is waiting for its execution time to be reached. */
288                                self->state = DRMAA_PS_QUEUED_ACTIVE;
289                                self->flags &= ~FSD_JOB_HOLD;
290                                break;
291                        case 'R': /* Job is running. */
292                        case 'T': /* Job is being moved to new location (?). */
293                         {
294                                if( self->flags & FSD_JOB_SUSPENDED )
295                                        self->state = DRMAA_PS_USER_SUSPENDED;
296                                else
297                                        self->state = DRMAA_PS_RUNNING;
298                                break;
299                         }
300                        case 'S': /* (Unicos only) job is suspend. */
301                                self->state = DRMAA_PS_SYSTEM_SUSPENDED;
302                                break;
303                        case 0:  default:
304                                self->state = DRMAA_PS_UNDETERMINED;
305                                break;
306         }
307}
308        fsd_log_debug(( "job_ps: %s", drmaa_job_ps_to_str(self->state) ));
309
310         {
311                int hours, minutes, seconds;
312                long mem;
313                if( cpu_usage && sscanf( cpu_usage, "%d:%d:%d", &hours, &minutes, &seconds ) == 3 )
314                 {
315                        self->cpu_usage = 60*( 60*hours + minutes ) + seconds;
316                        fsd_log_debug(( "cpu_usage: %s=%lds", cpu_usage, self->cpu_usage ));
317                 }
318                if( mem_usage && sscanf( mem_usage, "%ldkb", &mem ) == 1 )
319                 {
320                        self->mem_usage = 1024*mem;
321                        fsd_log_debug(( "mem_usage: %s=%ldB", mem_usage, self->mem_usage ));
322                 }
323                if( vmem_usage && sscanf( vmem_usage, "%ldkb", &mem ) == 1 )
324                 {
325                        self->vmem_usage = 1024*mem;
326                        fsd_log_debug(( "vmem_usage: %s=%ldB", vmem_usage, self->vmem_usage ));
327                 }
328                if( walltime && sscanf( walltime, "%d:%d:%d", &hours, &minutes, &seconds ) == 3 )
329                 {
330                        self->walltime = 60*( 60*hours + minutes ) + seconds;
331                        fsd_log_debug(( "walltime: %s=%lds", walltime, self->walltime ));
332                 }
333         }
334}
335
336void
337pbsdrmaa_job_on_missing( fsd_job_t *self )
338{
339        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*)self->session;
340        pbsdrmaa_job_t *pbsself = (pbsdrmaa_job_t *)self;
341       
342        if (!pbsself->missing_time)
343         {
344                pbsself->missing_time = time(NULL);
345         }
346
347        fsd_log_info(("pbsdrmaa_job_on_missing: pbs_home=%s, wait_thread_started=%d, submit_time=%d, missing_time=%d",
348                pbssession->pbs_home,
349                pbssession->super.wait_thread_started,
350                (int)self->submit_time,
351                (int)pbsself->missing_time));
352       
353        #define DRMAA_MAX_MISSING_TIME (30)
354        if( pbssession->pbs_home != NULL && pbssession->super.wait_thread_started && self->submit_time && (time(NULL) - pbsself->missing_time < DRMAA_MAX_MISSING_TIME))
355                fsd_log_info(("Job on missing but WT is running. Skipping...")); /* TODO: try to provide implementation that uses accounting/server log files */
356        else
357                pbsdrmaa_job_on_missing_standard( self );       
358}
359
360void
361pbsdrmaa_job_on_missing_standard( fsd_job_t *self )
362{
363        fsd_drmaa_session_t *session = self->session;
364        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t *)session;
365        int exit_status = -1;
366       
367        fsd_log_enter(( "({job_id=%s})", self->job_id ));
368        fsd_log_warning(( "Job %s missing from DRM queue", self->job_id ));
369
370        fsd_log_info(( "job_on_missing: last job_ps: %s (0x%02x)", drmaa_job_ps_to_str(self->state), self->state));
371
372        if( (exit_status = pbsdrmaa_job_read_exit_status(self->job_id, pbssession->job_exit_status_file_prefix)) == 0 )
373        {
374                self->state = DRMAA_PS_DONE;
375                self->exit_status = exit_status;
376        }
377        else
378        {
379                self->state = DRMAA_PS_FAILED;
380                self->exit_status = exit_status;
381        }
382        fsd_log_info(("job_on_missing evaluation result: state=%d exit_status=%d", self->state, self->exit_status));
383
384        fsd_cond_broadcast( &self->status_cond);
385        fsd_cond_broadcast( &self->session->wait_condition );
386
387        fsd_log_return(( "; job_ps=%s, exit_status=%d", drmaa_job_ps_to_str(self->state), self->exit_status ));
388}
389
390int
391pbsdrmaa_job_read_exit_status( const char *job_id, const char *job_state_dir_prefix)
392{
393        char *status_file = NULL, *start_file = NULL;
394        FILE *fhandle = NULL;
395        int exit_status = -1;
396
397        fsd_log_enter(("({job_id=%s, job_state_dir_prefix=%s})", job_id, job_state_dir_prefix));
398
399        status_file = fsd_asprintf("%s/%s.exitcode", job_state_dir_prefix, job_id);
400        start_file = fsd_asprintf("%s/%s.started", job_state_dir_prefix, job_id);
401
402        if ((fhandle = fopen(status_file, "r")) == NULL)
403         {
404                struct stat tmpstat;
405
406                fsd_log_error(("Failed to open job status file: %s", status_file));
407                if (stat(start_file, &tmpstat) == 0 && (tmpstat.st_mode & S_IFREG))
408                 {
409                        exit_status = 143; /* SIGTERM */
410                        fsd_log_info(("But start file exist %s. Assuming that job was killed (exit_status=%d).", start_file, exit_status));
411                 }
412                else
413                 {
414                        fsd_log_error(("Start file not found: %s", start_file));
415                 }
416       
417
418         }
419        else
420         {
421                (void)fscanf(fhandle, "%d", &exit_status); /*on error exit_status == -1 */
422                fclose(fhandle);
423         }
424
425        fsd_free(status_file);
426        fsd_free(start_file);
427
428        return exit_status;
429}
430
Note: See TracBrowser for help on using the repository browser.