source: trunk/pbs_drmaa/job.c @ 15

Revision 15, 13.4 KB checked in by mmamonski, 13 years ago (diff)

other minor fixies

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