source: trunk/pbs_drmaa/job.c @ 26

Revision 26, 14.6 KB checked in by mmamonski, 12 years ago (diff)

try pbs

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