source: trunk/pbs_drmaa/job.c @ 87

Revision 87, 12.2 KB checked in by mmamonski, 11 years ago (diff)

BUMP version 1.0.13

  • 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                                self->flags &= FSD_JOB_TERMINATED_MASK;
255                                self->flags |= FSD_JOB_TERMINATED;
256                                if (exit_status != -101) { /* has exit code */
257                                        if( self->exit_status == 0)
258                                                self->state = DRMAA_PS_DONE;
259                                        else
260                                                self->state = DRMAA_PS_FAILED;
261                                } else {
262                                        self->state = DRMAA_PS_FAILED;
263                                        self->exit_status = -1;
264                                }
265                                if (modify_time != 0)
266                                        self->end_time = modify_time; /* take last modify time as end time */
267                                else
268                                        self->end_time = time(NULL);
269                               
270                                if (self->start_time == 0)
271                                        self->start_time = self->end_time;
272
273                                break;
274                        case 'E': /* Job is exiting after having run. - MM: ignore exiting state (transient state) - outputs might have not been transfered yet,
275                                        MM2: mark job as running if current job status is undetermined - fix "ps after job was ripped" */
276                                if (self->state == DRMAA_PS_UNDETERMINED)
277                                        self->state = DRMAA_PS_RUNNING;
278                                break;
279                        case 'H': /* Job is held. */
280                                self->state = DRMAA_PS_USER_ON_HOLD;
281                                self->flags |= FSD_JOB_HOLD;
282                                break;
283                        case 'Q': /* Job is queued, eligible to run or routed. */
284                        case 'W': /* Job is waiting for its execution time to be reached. */
285                                self->state = DRMAA_PS_QUEUED_ACTIVE;
286                                self->flags &= ~FSD_JOB_HOLD;
287                                break;
288                        case 'R': /* Job is running. */
289                        case 'T': /* Job is being moved to new location (?). */
290                         {
291                                if( self->flags & FSD_JOB_SUSPENDED )
292                                        self->state = DRMAA_PS_USER_SUSPENDED;
293                                else
294                                        self->state = DRMAA_PS_RUNNING;
295                                break;
296                         }
297                        case 'S': /* (Unicos only) job is suspend. */
298                                self->state = DRMAA_PS_SYSTEM_SUSPENDED;
299                                break;
300                        case 0:  default:
301                                self->state = DRMAA_PS_UNDETERMINED;
302                                break;
303         }
304}
305        fsd_log_debug(( "job_ps: %s", drmaa_job_ps_to_str(self->state) ));
306
307         {
308                int hours, minutes, seconds;
309                long mem;
310                if( cpu_usage && sscanf( cpu_usage, "%d:%d:%d", &hours, &minutes, &seconds ) == 3 )
311                 {
312                        self->cpu_usage = 60*( 60*hours + minutes ) + seconds;
313                        fsd_log_debug(( "cpu_usage: %s=%lds", cpu_usage, self->cpu_usage ));
314                 }
315                if( mem_usage && sscanf( mem_usage, "%ldkb", &mem ) == 1 )
316                 {
317                        self->mem_usage = 1024*mem;
318                        fsd_log_debug(( "mem_usage: %s=%ldB", mem_usage, self->mem_usage ));
319                 }
320                if( vmem_usage && sscanf( vmem_usage, "%ldkb", &mem ) == 1 )
321                 {
322                        self->vmem_usage = 1024*mem;
323                        fsd_log_debug(( "vmem_usage: %s=%ldB", vmem_usage, self->vmem_usage ));
324                 }
325                if( walltime && sscanf( walltime, "%d:%d:%d", &hours, &minutes, &seconds ) == 3 )
326                 {
327                        self->walltime = 60*( 60*hours + minutes ) + seconds;
328                        fsd_log_debug(( "walltime: %s=%lds", walltime, self->walltime ));
329                 }
330         }
331}
332
333void
334pbsdrmaa_job_on_missing( fsd_job_t *self )
335{
336        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*)self->session;
337        pbsdrmaa_job_t *pbsself = (pbsdrmaa_job_t *)self;
338       
339        if (!pbsself->missing_time)
340         {
341                pbsself->missing_time = time(NULL);
342         }
343
344        fsd_log_info(("pbsdrmaa_job_on_missing: pbs_home=%s, wait_thread_started=%d, submit_time=%d, missing_time=%d",
345                pbssession->pbs_home,
346                pbssession->super.wait_thread_started,
347                (int)self->submit_time,
348                (int)pbsself->missing_time));
349       
350        #define DRMAA_MAX_MISSING_TIME (30)
351        if( pbssession->pbs_home != NULL && pbssession->super.wait_thread_started && self->submit_time && (time(NULL) - pbsself->missing_time < DRMAA_MAX_MISSING_TIME))
352                fsd_log_info(("Job on missing but WT is running. Skipping...")); /* TODO: try to provide implementation that uses accounting/server log files */
353        else
354                pbsdrmaa_job_on_missing_standard( self );       
355}
356
357void
358pbsdrmaa_job_on_missing_standard( fsd_job_t *self )
359{
360        fsd_drmaa_session_t *session = self->session;
361        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t *)session;
362        int exit_status = -1;
363       
364        fsd_log_enter(( "({job_id=%s})", self->job_id ));
365        fsd_log_warning(( "Job %s missing from DRM queue", self->job_id ));
366
367        fsd_log_info(( "job_on_missing: last job_ps: %s (0x%02x)", drmaa_job_ps_to_str(self->state), self->state));
368
369        if( (exit_status = pbsdrmaa_job_read_exit_status(self->job_id, pbssession->job_exit_status_file_prefix)) == 0 )
370        {
371                self->state = DRMAA_PS_DONE;
372                self->exit_status = exit_status;
373        }
374        else
375        {
376                self->state = DRMAA_PS_FAILED;
377                self->exit_status = exit_status;
378        }
379        fsd_log_info(("job_on_missing evaluation result: state=%d exit_status=%d", self->state, self->exit_status));
380
381        fsd_cond_broadcast( &self->status_cond);
382        fsd_cond_broadcast( &self->session->wait_condition );
383
384        fsd_log_return(( "; job_ps=%s, exit_status=%d", drmaa_job_ps_to_str(self->state), self->exit_status ));
385}
386
387int
388pbsdrmaa_job_read_exit_status( const char *job_id, const char *job_state_dir_prefix)
389{
390        char *status_file = NULL, *start_file = NULL;
391        FILE *fhandle = NULL;
392        int exit_status = -1;
393
394        fsd_log_enter(("({job_id=%s, job_state_dir_prefix=%s})", job_id, job_state_dir_prefix));
395
396        status_file = fsd_asprintf("%s/%s.exitcode", job_state_dir_prefix, job_id);
397        start_file = fsd_asprintf("%s/%s.started", job_state_dir_prefix, job_id);
398
399        if ((fhandle = fopen(status_file, "r")) == NULL)
400         {
401                struct stat tmpstat;
402
403                fsd_log_error(("Failed to open job status file: %s", status_file));
404                if (stat(start_file, &tmpstat) == 0 && (tmpstat.st_mode & S_IFREG))
405                 {
406                        exit_status = 143; /* SIGTERM */
407                        fsd_log_info(("But start file exist %s. Assuming that job was killed (exit_status=%d).", start_file, exit_status));
408                 }
409                else
410                 {
411                        fsd_log_error(("Start file not found: %s", start_file));
412                 }
413       
414
415         }
416        else
417         {
418                (void)fscanf(fhandle, "%d", &exit_status); /*on error exit_status == -1 */
419                fclose(fhandle);
420         }
421
422        fsd_free(status_file);
423        fsd_free(start_file);
424
425        return exit_status;
426}
427
Note: See TracBrowser for help on using the repository browser.