source: trunk/pbs_drmaa/job.c @ 10

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

DRMAA utils - keep conf in PREFIX/etc, log_info all communication with Torque, do not log_error on timout and vector operations

RevLine 
[1]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>
[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
45        = "$Id: job.c 370 2010-11-16 09:51:00Z mamonski $";
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
207                        fsd_log_error(("pbs_statjob error: %d, %s", pbs_errno, pbse_to_txt(pbs_errno)));
208#endif
209                        switch( pbs_errno )
210                         {
211                                case PBSE_UNKJOBID:
212                                        break;
213                                case PBSE_PROTOCOL:
214                                case PBSE_EXPIRED:
[3]215                                        if ( session->pbs_conn >= 0 )
216                                                pbs_disconnect( session->pbs_conn );
[1]217                                        sleep(1);
218                                        session->pbs_conn = pbs_connect( session->super.contact );
219                                        if( session->pbs_conn < 0 )
220                                                pbsdrmaa_exc_raise_pbs( "pbs_connect" );
221                                        else
222                                         {
223                                                fsd_log_error(("retry:"));
224                                                goto retry;
225                                         }
226                                default:
227                                        pbsdrmaa_exc_raise_pbs( "pbs_statjob" );
228                                        break;
229                                case 0:  /* ? */
230                                        fsd_exc_raise_code( FSD_ERRNO_INTERNAL_ERROR );
231                                        break;
232                         }
233                 }
234
235                conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex );
236
237                if( status != NULL )
238                 {
239                        ((pbsdrmaa_job_t*)self)->update( self, status );
240                 }
[7]241                else if( self->state < DRMAA_PS_DONE )
[1]242                        self->on_missing( self );
243         }
244        FINALLY
245         {
246                if( conn_lock )
247                        conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex );
248                if( status != NULL )
249                        pbs_statfree( status );
250         }
251        END_TRY
252
253        fsd_log_return((""));
254}
255
256
257void
258pbsdrmaa_job_update( fsd_job_t *self, struct batch_status *status )
259{
260        struct attrl *attribs = status->attribs;
261        struct attrl *i = NULL;
262        char pbs_state = 0;
263        int exit_status = -2;
264        const char *cpu_usage = NULL;
265        const char *mem_usage = NULL;
266        const char *vmem_usage = NULL;
267        const char *walltime = NULL;
268        long unsigned int modify_time = 0;
269
270        fsd_log_enter(( "({job_id=%s})", self->job_id ));
271#ifdef DEBUGGING
272        pbsdrmaa_dump_attrl( attribs, NULL );
273#endif
274        fsd_assert( !strcmp( self->job_id, status->name ) );
275
276        for( i = attribs;  i != NULL;  i = i->next )
277         {
278                int attr;
279                attr = pbsdrmaa_pbs_attrib_by_name( i->name );
280                switch( attr )
281                 {
282                        case PBSDRMAA_ATTR_JOB_STATE:
283                                pbs_state = i->value[0];                               
284                                break;
285                        case PBSDRMAA_ATTR_EXIT_STATUS:
286                                exit_status = atoi( i->value );
287                                break;
288                        case PBSDRMAA_ATTR_RESOURCES_USED:
289                                if( !strcmp( i->resource, "cput" ) )
290                                        cpu_usage = i->value;
291                                else if( !strcmp( i->resource, "mem" ) )
292                                        mem_usage = i->value;
293                                else if( !strcmp( i->resource, "vmem" ) )
294                                        vmem_usage = i->value;
295                                else if( !strcmp( i->resource, "walltime" ) )
296                                        walltime = i->value;
297                                break;
298                        case PBSDRMAA_ATTR_QUEUE:
299                                if (!self->queue)
300                                        self->queue = fsd_strdup(i->value);
301                                break;
302                        case PBSDRMAA_ATTR_ACCOUNT_NAME:
303                                if (!self->project)
304                                        self->project = fsd_strdup(i->value);
305                                break;
306                        case PBSDRMAA_ATTR_EXECUTION_HOST:
307                                if (!self->execution_hosts) {
308                                        fsd_log_debug(("execution_hosts = %s", i->value));
309                                        self->execution_hosts = fsd_strdup(i->value);
310                                }
311                                break;
312                        case PBSDRMAA_ATTR_START_TIME:
313                                {
314                                  long unsigned int start_time;
315                                  if (self->start_time == 0 && sscanf(i->value, "%lu", &start_time) == 1)
316                                        self->start_time = start_time;
317                                  break;
318                                }
319                        case PBSDRMAA_ATTR_MTIME:
320                                if (sscanf(i->value, "%lu", &modify_time) != 1)
321                                        modify_time = 0;
322                                break;
323                 }
324         }
325
326        if( pbs_state )
327                fsd_log_debug(( "pbs_state: %c", pbs_state ));
328
329        if( exit_status != -2 )
330         {
331                fsd_log_debug(( "exit_status: %d", exit_status ));
332                self->exit_status = exit_status;
333         }
334        if(pbs_state){
335                switch( pbs_state )
336                 {
337                        case 'C': /* Job is completed after having run. */
338                                self->flags &= FSD_JOB_TERMINATED_MASK;
339                                self->flags |= FSD_JOB_TERMINATED;
340                                if (exit_status != -2) { /* has exit code */
341                                        if( self->exit_status == 0)
342                                                self->state = DRMAA_PS_DONE;
343                                        else
344                                                self->state = DRMAA_PS_FAILED;
345                                } else {
346                                        self->state = DRMAA_PS_FAILED;
347                                        self->exit_status = -1;
348                                }
349                                self->end_time = modify_time; /* take last modify time as end time */
350                                break;
351                        case 'E': /* Job is exiting after having run. - MM: ignore exiting state (transient state) - outputs might have not been transfered yet,
352                                        MM2: mark job as running if current job status is undetermined - fix "ps after job was ripped" */
353                                if (self->state == DRMAA_PS_UNDETERMINED)
354                                        self->state = DRMAA_PS_RUNNING;
355                                break;
356                        case 'H': /* Job is held. */
357                                self->state = DRMAA_PS_USER_ON_HOLD;
358                                self->flags |= FSD_JOB_HOLD;
359                                break;
360                        case 'Q': /* Job is queued, eligible to run or routed. */
361                        case 'W': /* Job is waiting for its execution time to be reached. */
362                                self->state = DRMAA_PS_QUEUED_ACTIVE;
363                                self->flags &= ~FSD_JOB_HOLD;
364                                break;
365                        case 'R': /* Job is running. */
366                        case 'T': /* Job is being moved to new location (?). */
367                         {
368                                if( self->flags & FSD_JOB_SUSPENDED )
369                                        self->state = DRMAA_PS_USER_SUSPENDED;
370                                else
371                                        self->state = DRMAA_PS_RUNNING;
372                                break;
373                         }
374                        case 'S': /* (Unicos only) job is suspend. */
375                                self->state = DRMAA_PS_SYSTEM_SUSPENDED;
376                                break;
377                        case 0:  default:
378                                self->state = DRMAA_PS_UNDETERMINED;
379                                break;
380         }
381}
382        fsd_log_debug(( "job_ps: %s", drmaa_job_ps_to_str(self->state) ));
383
384         {
385                int hours, minutes, seconds;
386                long mem;
387                if( cpu_usage && sscanf( cpu_usage, "%d:%d:%d",
388                                &hours, &minutes, &seconds ) == 3 )
389                 {
390                        self->cpu_usage = 60*( 60*hours + minutes ) + seconds;
391                        fsd_log_debug(( "cpu_usage: %s=%lds", cpu_usage, self->cpu_usage ));
392                 }
393                if( mem_usage && sscanf( mem_usage, "%ldkb", &mem ) == 1 )
394                 {
395                        self->mem_usage = 1024*mem;
396                        fsd_log_debug(( "mem_usage: %s=%ldB", mem_usage, self->mem_usage ));
397                 }
398                if( vmem_usage && sscanf( vmem_usage, "%ldkb", &mem ) == 1 )
399                 {
400                        self->vmem_usage = 1024*mem;
401                        fsd_log_debug(( "vmem_usage: %s=%ldB", vmem_usage, self->vmem_usage ));
402                 }
403                if( walltime && sscanf( walltime, "%d:%d:%d",
404                                        &hours, &minutes, &seconds ) == 3 )
405                 {
406                        self->walltime = 60*( 60*hours + minutes ) + seconds;
407                        fsd_log_debug(( "walltime: %s=%lds", walltime, self->walltime ));
408                 }
409         }
410}
411
412void
413pbsdrmaa_job_on_missing( fsd_job_t *self )
414{
[7]415        pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*)self->session;
416       
417        if( pbssession->pbs_home == NULL )
418                pbsdrmaa_job_on_missing_standard( self );       
419        else
420                pbsdrmaa_job_on_missing_log_based( self );     
421}
422
423void
424pbsdrmaa_job_on_missing_standard( fsd_job_t *self )
425{
[1]426        fsd_drmaa_session_t *session = self->session;
[7]427       
[1]428        unsigned missing_mask = 0;
429
430        fsd_log_enter(( "({job_id=%s})", self->job_id ));
431        fsd_log_warning(( "self %s missing from DRM queue", self->job_id ));
432
433        switch( session->missing_jobs )
[7]434        {
[1]435                case FSD_REVEAL_MISSING_JOBS:         missing_mask = 0;     break;
436                case FSD_IGNORE_MISSING_JOBS:         missing_mask = 0x73;  break;
437                case FSD_IGNORE_QUEUED_MISSING_JOBS:  missing_mask = 0x13;  break;
[7]438        }
[1]439        fsd_log_debug(( "last job_ps: %s (0x%02x); mask: 0x%02x",
440                                drmaa_job_ps_to_str(self->state), self->state, missing_mask ));
441
442        if( self->state < DRMAA_PS_DONE
443                        &&  (self->state & ~missing_mask) )
444                fsd_exc_raise_fmt(
445                                FSD_ERRNO_INTERNAL_ERROR,
446                                "self %s missing from queue", self->job_id
447                                );
448
449        if( (self->flags & FSD_JOB_TERMINATED_MASK) == 0 )
[7]450        {
[1]451                self->flags &= FSD_JOB_TERMINATED_MASK;
452                self->flags |= FSD_JOB_TERMINATED;
[7]453        }
[1]454
455        if( (self->flags & FSD_JOB_ABORTED) == 0
456                        &&  session->missing_jobs == FSD_IGNORE_MISSING_JOBS )
[7]457        { /* assume everthing was ok */
[1]458                self->state = DRMAA_PS_DONE;
459                self->exit_status = 0;
[7]460        }
[1]461        else
[7]462        { /* job aborted */
[1]463                self->state = DRMAA_PS_FAILED;
464                self->exit_status = -1;
[7]465        }
[1]466
467        fsd_log_return(( "; job_ps=%s, exit_status=%d",
468                                drmaa_job_ps_to_str(self->state), self->exit_status ));
[7]469}
470
471void
472pbsdrmaa_job_on_missing_log_based( fsd_job_t *self )
473{
474        fsd_drmaa_session_t *session = self->session;
475        pbsdrmaa_log_reader_t *log_reader = NULL;
476       
477        fsd_log_enter(( "({job_id=%s})", self->job_id ));
478        fsd_log_warning(( "self %s missing from DRM queue", self->job_id ));
479       
480        TRY
481        {       
482                log_reader = pbsdrmaa_log_reader_new( session, self);
483                log_reader->read_log( log_reader );
[1]484        }
[7]485        FINALLY
486        {
487                pbsdrmaa_log_reader_destroy( log_reader );
488        }
489        END_TRY
490
491        fsd_log_return(( "; job_ps=%s, exit_status=%d",
492                                drmaa_job_ps_to_str(self->state), self->exit_status ));
[1]493}
Note: See TracBrowser for help on using the repository browser.