source: trunk/pbs_drmaa/job.c @ 85

Revision 85, 12.3 KB checked in by mmamonski, 11 years ago (diff)

PBS DRMAA autoclose connection

  • Property svn:keywords set to Id
RevLine 
[12]1/* $Id$ */
[1]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>
[57]28#include <sys/stat.h>
[1]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>
[7]36#include <pbs_drmaa/log_reader.h>
[1]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
[12]46        = "$Id$";
[1]47#endif
48
49
[54]50static void pbsdrmaa_job_control( fsd_job_t *self, int action );
[1]51
[54]52static void pbsdrmaa_job_update_status( fsd_job_t *self );
[1]53
[54]54static void pbsdrmaa_job_on_missing( fsd_job_t *self );
[1]55
[54]56static void pbsdrmaa_job_on_missing_standard( fsd_job_t *self );
[7]57
[54]58static void pbsdrmaa_job_update( fsd_job_t *self, struct batch_status* );
[1]59
[54]60static int pbsdrmaa_job_read_exit_status( const char *job_id, const char *job_state_dir_prefix);
[1]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;
[62]70        self->missing_time = 0;
[1]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
[85]82        fsd_log_enter(( "({job_id=%s}, action=%d)", self->job_id, action ));
[1]83
[85]84        while ( true )
[1]85         {
[85]86                switch( action )
[1]87                 {
[85]88                        /*
89                         * We cannot know whether we did suspend job
90                         * in other way than remembering this inside DRMAA session.
91                         */
92                        case DRMAA_CONTROL_SUSPEND:
93                                session->pbs_connection->sigjob( session->pbs_connection, (char*)job_id, "SIGSTOP");
94                                self->flags |= FSD_JOB_SUSPENDED;
[1]95                                break;
[85]96                        case DRMAA_CONTROL_RESUME:
97                                session->pbs_connection->sigjob( session->pbs_connection, (char*)job_id, "SIGCONT");
98                                self->flags &= ~FSD_JOB_SUSPENDED;
99                                break;
100                        case DRMAA_CONTROL_HOLD:
101                                session->pbs_connection->holdjob( session->pbs_connection, (char*)job_id );
102                                self->flags |= FSD_JOB_HOLD;
103                                break;
104                        case DRMAA_CONTROL_RELEASE:
105                                session->pbs_connection->rlsjob( session->pbs_connection, (char*)job_id );
106                                self->flags &= ~FSD_JOB_HOLD;
107                                break;
108                        case DRMAA_CONTROL_TERMINATE:
109                                session->pbs_connection->deljob( session->pbs_connection, (char*)job_id );
110                                /* TODO: make deldelay configurable ???:
111                                 * deldelay=N -- delay between SIGTERM and SIGKILL (default 0) */
112                                self->flags &= FSD_JOB_TERMINATED_MASK;
113                                if( (self->flags & FSD_JOB_TERMINATED) == 0 )
114                                        self->flags |= FSD_JOB_TERMINATED | FSD_JOB_ABORTED;
115                                break;
116                 }
[1]117         }
118
119        fsd_log_return((""));
120}
121
122
123void
124pbsdrmaa_job_update_status( fsd_job_t *self )
125{
126        struct batch_status *volatile status = NULL;
127        pbsdrmaa_session_t *session = (pbsdrmaa_session_t*)self->session;
128
129        fsd_log_enter(( "({job_id=%s})", self->job_id ));
[21]130       
[1]131        TRY
132         {
[16]133
[1]134#ifdef PBS_PROFESSIONAL
[85]135                status = session->pbs_connection->statjob( session->pbs_connection, self->job_id, NULL);
[1]136#else
[85]137                status = session->pbs_connection->statjob( session->pbs_connection, self->job_id, session->status_attrl);
[1]138#endif
[21]139
[1]140                if( status != NULL )
141                 {
142                        ((pbsdrmaa_job_t*)self)->update( self, status );
143                 }
[7]144                else if( self->state < DRMAA_PS_DONE )
[25]145                 {
[1]146                        self->on_missing( self );
[25]147                 }
[1]148         }
149        FINALLY
150         {
151                if( status != NULL )
[85]152                        session->pbs_connection->statjob_free( session->pbs_connection, status );
[1]153         }
154        END_TRY
155
156        fsd_log_return((""));
157}
158
159
160void
161pbsdrmaa_job_update( fsd_job_t *self, struct batch_status *status )
162{
163        struct attrl *attribs = status->attribs;
164        struct attrl *i = NULL;
165        char pbs_state = 0;
[83]166        int exit_status = -101;
[1]167        const char *cpu_usage = NULL;
168        const char *mem_usage = NULL;
169        const char *vmem_usage = NULL;
170        const char *walltime = NULL;
171        long unsigned int modify_time = 0;
172
173        fsd_log_enter(( "({job_id=%s})", self->job_id ));
174#ifdef DEBUGGING
175        pbsdrmaa_dump_attrl( attribs, NULL );
176#endif
177        fsd_assert( !strcmp( self->job_id, status->name ) );
178
179        for( i = attribs;  i != NULL;  i = i->next )
180         {
181                int attr;
182                attr = pbsdrmaa_pbs_attrib_by_name( i->name );
183                switch( attr )
184                 {
185                        case PBSDRMAA_ATTR_JOB_STATE:
186                                pbs_state = i->value[0];                               
187                                break;
188                        case PBSDRMAA_ATTR_EXIT_STATUS:
[29]189                                exit_status = fsd_atoi( i->value );
[1]190                                break;
191                        case PBSDRMAA_ATTR_RESOURCES_USED:
192                                if( !strcmp( i->resource, "cput" ) )
193                                        cpu_usage = i->value;
194                                else if( !strcmp( i->resource, "mem" ) )
195                                        mem_usage = i->value;
196                                else if( !strcmp( i->resource, "vmem" ) )
197                                        vmem_usage = i->value;
198                                else if( !strcmp( i->resource, "walltime" ) )
199                                        walltime = i->value;
200                                break;
201                        case PBSDRMAA_ATTR_QUEUE:
202                                if (!self->queue)
203                                        self->queue = fsd_strdup(i->value);
204                                break;
205                        case PBSDRMAA_ATTR_ACCOUNT_NAME:
206                                if (!self->project)
207                                        self->project = fsd_strdup(i->value);
208                                break;
[26]209#ifndef PBS_PROFESSIONAL
[1]210                        case PBSDRMAA_ATTR_EXECUTION_HOST:
211                                if (!self->execution_hosts) {
212                                        fsd_log_debug(("execution_hosts = %s", i->value));
213                                        self->execution_hosts = fsd_strdup(i->value);
214                                }
215                                break;
[26]216#else
217                        case PBSDRMAA_ATTR_EXECUTION_VNODE:
218                                if (!self->execution_hosts) {
219                                        fsd_log_debug(("execution_hosts = %s", i->value));
220                                        self->execution_hosts = fsd_strdup(i->value);
221                                }
222                                break;
223#endif
[1]224                        case PBSDRMAA_ATTR_START_TIME:
225                                {
226                                  long unsigned int start_time;
227                                  if (self->start_time == 0 && sscanf(i->value, "%lu", &start_time) == 1)
228                                        self->start_time = start_time;
229                                  break;
230                                }
231                        case PBSDRMAA_ATTR_MTIME:
232                                if (sscanf(i->value, "%lu", &modify_time) != 1)
233                                        modify_time = 0;
234                                break;
235                 }
236         }
237
238        if( pbs_state )
239                fsd_log_debug(( "pbs_state: %c", pbs_state ));
240
[83]241        if( exit_status != -101 )
[1]242         {
243                fsd_log_debug(( "exit_status: %d", exit_status ));
244                self->exit_status = exit_status;
[83]245
246                if (self->exit_status < 0)
247                 {
248                        self->exit_status = -1;
249                        fsd_log_error(("ExitStatus = %d, probably system problem, report job %s to the local administrator", exit_status, self->job_id));
250                 }
251
[1]252         }
253        if(pbs_state){
254                switch( pbs_state )
255                 {
256                        case 'C': /* Job is completed after having run. */
257                                self->flags &= FSD_JOB_TERMINATED_MASK;
258                                self->flags |= FSD_JOB_TERMINATED;
[83]259                                if (exit_status != -101) { /* has exit code */
[1]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                                }
[22]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
[1]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;
[12]313                if( cpu_usage && sscanf( cpu_usage, "%d:%d:%d", &hours, &minutes, &seconds ) == 3 )
[1]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                 }
[12]328                if( walltime && sscanf( walltime, "%d:%d:%d", &hours, &minutes, &seconds ) == 3 )
[1]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{
[7]339        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*)self->session;
[62]340        pbsdrmaa_job_t *pbsself = (pbsdrmaa_job_t *)self;
341       
342        if (!pbsself->missing_time)
343         {
344                pbsself->missing_time = time(NULL);
345         }
[12]346
[62]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,
[68]350                (int)self->submit_time,
351                (int)pbsself->missing_time));
[62]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))
[40]355                fsd_log_info(("Job on missing but WT is running. Skipping...")); /* TODO: try to provide implementation that uses accounting/server log files */
[33]356        else
[7]357                pbsdrmaa_job_on_missing_standard( self );       
358}
359
360void
361pbsdrmaa_job_on_missing_standard( fsd_job_t *self )
362{
[1]363        fsd_drmaa_session_t *session = self->session;
[54]364        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t *)session;
365        int exit_status = -1;
[7]366       
[1]367        fsd_log_enter(( "({job_id=%s})", self->job_id ));
[12]368        fsd_log_warning(( "Job %s missing from DRM queue", self->job_id ));
[1]369
[54]370        fsd_log_info(( "job_on_missing: last job_ps: %s (0x%02x)", drmaa_job_ps_to_str(self->state), self->state));
[1]371
[54]372        if( (exit_status = pbsdrmaa_job_read_exit_status(self->job_id, pbssession->job_exit_status_file_prefix)) == 0 )
[7]373        {
[1]374                self->state = DRMAA_PS_DONE;
[54]375                self->exit_status = exit_status;
[7]376        }
[1]377        else
[54]378        {
[1]379                self->state = DRMAA_PS_FAILED;
[54]380                self->exit_status = exit_status;
[7]381        }
[63]382        fsd_log_info(("job_on_missing evaluation result: state=%d exit_status=%d", self->state, self->exit_status));
[1]383
[12]384        fsd_cond_broadcast( &self->status_cond);
[64]385        fsd_cond_broadcast( &self->session->wait_condition );
[12]386
[54]387        fsd_log_return(( "; job_ps=%s, exit_status=%d", drmaa_job_ps_to_str(self->state), self->exit_status ));
[7]388}
389
[54]390int
391pbsdrmaa_job_read_exit_status( const char *job_id, const char *job_state_dir_prefix)
392{
[57]393        char *status_file = NULL, *start_file = NULL;
[54]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
[55]399        status_file = fsd_asprintf("%s/%s.exitcode", job_state_dir_prefix, job_id);
[57]400        start_file = fsd_asprintf("%s/%s.started", job_state_dir_prefix, job_id);
[54]401
402        if ((fhandle = fopen(status_file, "r")) == NULL)
403         {
[56]404                struct stat tmpstat;
405
[54]406                fsd_log_error(("Failed to open job status file: %s", status_file));
[56]407                if (stat(start_file, &tmpstat) == 0 && (tmpstat.st_mode & S_IFREG))
408                 {
409                        exit_status = 143; /* SIGTERM */
[57]410                        fsd_log_info(("But start file exist %s. Assuming that job was killed (exit_status=%d).", start_file, exit_status));
[56]411                 }
[57]412                else
413                 {
414                        fsd_log_error(("Start file not found: %s", start_file));
415                 }
416       
417
[54]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);
[56]426        fsd_free(start_file);
[54]427
428        return exit_status;
429}
430
Note: See TracBrowser for help on using the repository browser.