source: trunk/slurm_drmaa/util.c @ 13

Revision 13, 11.9 KB checked in by mmatloka, 14 years ago (diff)

support for slurm 2.2

  • Property svn:executable set to *
  • Property svn:keywords set to Id
RevLine 
[5]1/* $Id$ */
[1]2/*
3 * PSNC DRMAA for SLURM
[13]4 * Copyright (C) 2011 Poznan Supercomputing and Networking Center
[1]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#include <drmaa_utils/common.h>
21#include <drmaa_utils/exception.h>
22#include <slurm_drmaa/util.h>
23#include <string.h>
24
25#include <time.h>
26#include <drmaa_utils/datetime.h>
27#include <drmaa_utils/datetime_impl.h>
28
29#ifndef lint
30static char rcsid[]
31#       ifdef __GNUC__
32                __attribute__ ((unused))
33#       endif
[5]34        = "$Id$";
[1]35#endif
36
37unsigned
38slurmdrmaa_datetime_parse( const char *string )
39{
40#ifdef DEBUGGING
41        char dbg[256];
42#endif
43        fsd_dt_parser_t *volatile parser = NULL;
44        fsd_dt_lexer_t *volatile lexer = NULL;
45        int parse_err = 0;
46        fsd_datetime_t dt;
47
48        memset(&dt, 0, sizeof(fsd_datetime_t));
49
50        fsd_log_enter(( "(%s)", string ));
51        TRY
52         {
53                fsd_malloc( parser, fsd_dt_parser_t );
54                fsd_malloc( lexer, fsd_dt_lexer_t );
55
56                parser->lexer = lexer;
57                parser->n_errors = 0;
58                lexer->parser = parser;
59                lexer->begin = lexer->p = (unsigned char*)string;
60                lexer->end = (unsigned char*)( string + strlen(string) );
61                parse_err = fsd_dt_parse( parser, lexer );
62                if( parse_err || parser->n_errors )
63                        fsd_exc_raise_fmt(
64                                        FSD_ERRNO_INVALID_VALUE_FORMAT,
65                                        "invalid date/time format: %s", string
66                                        );
67
68                dt = parser->result;
69#ifdef DEBUGGING
70                fsd_datetime_dump( &dt, dbg, sizeof(dbg) );
71                fsd_log_debug(( "parsed: %s", dbg ));
72#endif
73        }
74        FINALLY
75         {
76                fsd_free( parser );
77                fsd_free( lexer );
78         }
79        END_TRY
80
81        fsd_log_return(( "(%s) =%u", string, (unsigned)60*dt.hour+dt.minute ));
82        return 60*dt.hour+dt.minute;
83}
84
85enum slurm_native {
86        SLURM_NATIVE_ACCOUNT,
87        SLURM_NATIVE_ACCTG_FREQ,
88        SLURM_NATIVE_COMMENT,
89        SLURM_NATIVE_CONSTRAINT,
90        SLURM_NATIVE_CONTIGUOUS,
91        SLURM_NATIVE_EXCLUSIVE,
92        SLURM_NATIVE_MEM,
93        SLURM_NATIVE_MEM_PER_CPU,
94        SLURM_NATIVE_MINCPUS,
95        SLURM_NATIVE_NODELIST,
96        SLURM_NATIVE_NODES,
97        SLURM_NATIVE_NTASKS_PER_NODE,
98        SLURM_NATIVE_PARTITION,
99        SLURM_NATIVE_QOS,
100        SLURM_NATIVE_REQUEUE,
101        SLURM_NATIVE_RESERVATION,
102        SLURM_NATIVE_SHARE
103};
104
105void
106slurmdrmaa_init_job_desc(job_desc_msg_t *job_desc)
107{
108        memset(job_desc, 0, sizeof(job_desc_msg_t));
109}
110
111void
112slurmdrmaa_free_job_desc(job_desc_msg_t *job_desc)
113{
114        unsigned i = 0;
115        fsd_log_enter(( "" ));
116        fsd_free(job_desc->account);   
117        fsd_free(job_desc->comment);
118       
119        for(i = 0;i<job_desc->env_size;i++)
120        {
121                fsd_free(job_desc->environment[i]);
122        }       
123        fsd_free(job_desc->environment);
124        fsd_free(job_desc->features);
125        fsd_free(job_desc->name);       
126        fsd_free(job_desc->mail_user);
127        fsd_free(job_desc->partition);
128        fsd_free(job_desc->qos);
129        fsd_free(job_desc->req_nodes);
130        fsd_free(job_desc->reservation);
131        fsd_free(job_desc->script);
132        fsd_free(job_desc->std_in);
133        fsd_free(job_desc->std_out);
134        fsd_free(job_desc->std_err);   
135        fsd_free(job_desc->work_dir);
136       
137        fsd_log_return(( "" ));
138}
139
140void
141slurmdrmaa_add_attribute(job_desc_msg_t *job_desc, unsigned attr, const char *value)
142{
143        char * ptr = NULL;
144        char * rest = NULL;
145        char * token = NULL;
146
147        switch(attr)
148        {
149                case SLURM_NATIVE_ACCOUNT:
150                        fsd_free(job_desc->account);
151                        fsd_log_debug(("# account = %s",value));
152                        job_desc->account = fsd_strdup(value);
153                        break;
154                case SLURM_NATIVE_ACCTG_FREQ:
155                        fsd_log_debug(("# acctg_freq = %s",value));
156                        job_desc->acctg_freq = fsd_atoi(value);
157                        break;
158                case SLURM_NATIVE_COMMENT:
159                        fsd_free(job_desc->comment);
160                        fsd_log_debug(("# comment = %s",value));
161                        job_desc->comment = fsd_strdup(value);
162                        break;
163                case SLURM_NATIVE_CONSTRAINT:
164                        fsd_free(job_desc->features);
165                        fsd_log_debug(("# constraints -> features = %s",value));
166                        job_desc->features = fsd_strdup(value);
167                        break;
168                case SLURM_NATIVE_CONTIGUOUS:
169                        fsd_log_debug(( "# contiguous = 1"));
170                        job_desc->contiguous = 1;
171                        break;
172                case SLURM_NATIVE_EXCLUSIVE:
173                        fsd_log_debug(( "# exclusive -> shared = 0"));
174                        job_desc->shared = 0;
175                        break;
176                case SLURM_NATIVE_MEM:
[13]177                #if SLURM_VERSION_NUMBER < SLURM_VERSION_NUM(2,2,0)
[1]178                        if(job_desc->job_min_memory == NO_VAL ||  fsd_atoi(value) > (int)job_desc->job_min_memory) {
179                                fsd_log_debug(("# job_min_memory = %s",value));
180                                job_desc->job_min_memory = fsd_atoi(value);
181                        }
[13]182                #else
183                        if(job_desc->pn_min_memory == NO_VAL ||  fsd_atoi(value) > (int)job_desc->pn_min_memory) {
184                                fsd_log_debug(("# pn_min_memory = %s",value));
185                                job_desc->pn_min_memory = fsd_atoi(value);
186                        }
187                #endif
188                        else {
[1]189                                fsd_log_debug(("mem value defined lower or equal to mem-per-cpu or value defined before"));
190                        }
191                        break;
192                case SLURM_NATIVE_MEM_PER_CPU:
[13]193                #if SLURM_VERSION_NUMBER < SLURM_VERSION_NUM(2,2,0)
[1]194                        if(job_desc->job_min_memory == NO_VAL ||  fsd_atoi(value) > (int)job_desc->job_min_memory) {
195                                fsd_log_debug(("# job_min_memory = %s",value));
196                                job_desc->job_min_memory = fsd_atoi(value);
197                        }
[13]198                #else
199                        if(job_desc->pn_min_memory == NO_VAL ||  fsd_atoi(value) > (int)job_desc->pn_min_memory) {
200                                fsd_log_debug(("# pn_min_memory = %s",value));
201                                job_desc->pn_min_memory = fsd_atoi(value);
202                        }
203                #endif
204                        else {
[1]205                                fsd_log_debug(("mem-per-cpu value defined lower or equal to mem or value defined before"));
206                        }
207                        break;
208                case SLURM_NATIVE_MINCPUS:
[13]209                #if SLURM_VERSION_NUMBER < SLURM_VERSION_NUM(2,2,0)
[1]210                        fsd_log_debug(("# job_min_cpus = %s",value));
211                        job_desc->job_min_cpus = fsd_atoi(value);
[13]212                #else
213                        fsd_log_debug(("# min_cpus = %s",value));
214                        job_desc->min_cpus = fsd_atoi(value);
215                #endif
[1]216                        break;
217                case SLURM_NATIVE_NODELIST:
218                        fsd_free(job_desc->req_nodes);
219                        fsd_log_debug(("# node-list - > req_nodes = %s",value));
220                        job_desc->req_nodes = fsd_strdup(value);
221                        break;
222                case SLURM_NATIVE_NODES:
223                        ptr = strdup(value);
224
225                        if((token = strtok_r(ptr,"=",&rest)) == NULL){
226                                fsd_log_error(("strtok_r returned NULL"));
227                        }
228                                               
229                        fsd_log_debug(("nodes: %s ->",value));
230                        fsd_log_debug(("# min_nodes = %s",token));
231                        job_desc->min_nodes = fsd_atoi(token);
232                                               
233                        if(strcmp(rest,"") !=0 ) {
234                                fsd_log_debug(("# max_nodes = %s",rest));
235                                job_desc->max_nodes = fsd_atoi(rest);
236                        }
237                        fsd_free(ptr);
238                        break;         
239                case SLURM_NATIVE_NTASKS_PER_NODE:
240                        fsd_log_debug(("# ntasks_per_node = %s",value));
241                        job_desc->ntasks_per_node = fsd_atoi(value);
242                        break;
243                case SLURM_NATIVE_PARTITION:
244                        fsd_free(job_desc->partition);
245                        fsd_log_debug(("# partition = %s",value));
246                        job_desc->partition = fsd_strdup(value);
247                        break;
248                case SLURM_NATIVE_QOS:
249                        fsd_free(job_desc->qos);
250                        fsd_log_debug(("# qos = %s",value));
251                        job_desc->qos = fsd_strdup(value);
252                        break;
253                case SLURM_NATIVE_REQUEUE:
254                        fsd_log_debug(( "# requeue = 1"));
255                        job_desc->requeue = 1;
256                        break;
257                case SLURM_NATIVE_RESERVATION:
258                        fsd_log_debug(("# reservation = %s",value));
259                        job_desc->reservation = fsd_strdup(value);
260                        break;
261                case SLURM_NATIVE_SHARE:
262                        fsd_log_debug(("# shared = 1"));
263                        job_desc->shared = 1;
264                        break;
265                default:
266                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,"Invalid attribute");
267        }
268}
269
270void
271slurmdrmaa_parse_additional_attr(job_desc_msg_t *job_desc,const char *add_attr)
272{
273        char * volatile name = NULL;
274        char *value = NULL;
275        char *ctxt = NULL;
276        char * volatile add_attr_copy = fsd_strdup(add_attr);
277
278        TRY
279          {
280                name = fsd_strdup(strtok_r(add_attr_copy, "=", &ctxt));
281                value = strtok_r(NULL, "=", &ctxt);
282                       
283                if(strcmp(name,"account") == 0) {
284                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_ACCOUNT,value);
285                }
286                else if(strcmp(name,"acctg-freq") == 0) {
287                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_ACCTG_FREQ,value);
288                }
289                else if (strcmp(name,"comment") == 0) {
290                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_COMMENT,value);
291                }
292                else if (strcmp(name,"constraint") == 0) {
293                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_CONSTRAINT,value);
294                }
295                else if (strcmp(name,"contiguous") == 0) {
296                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_CONTIGUOUS,NULL);
297                }
298                else if(strcmp(name,"exclusive") == 0) {
299                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_EXCLUSIVE,NULL);
300                }
301                else if (strcmp(name,"mem") == 0) {
302                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_MEM,value);
303                }
304                else if (strcmp(name,"mem-per-cpu") == 0) {
305                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_MEM_PER_CPU,value);
306                }
307                else if (strcmp(name,"mincpus") == 0) {
308                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_MINCPUS,value);
309                }
310                else if (strcmp(name,"nodelist") == 0) {
311                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODELIST,value);                 
312                }
313                else if (strcmp(name,"nodes") == 0) {
314                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODES,value);
315                }
316                else if (strcmp(name,"ntasks-per-node") == 0) {
317                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NTASKS_PER_NODE,value);
318                }
319                else if (strcmp(name,"partition") == 0) {
320                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_PARTITION,value);
321                }
322                else if (strcmp(name,"qos") == 0) {
323                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_QOS,value);
324                }
325                else if (strcmp(name,"requeue") == 0) {
326                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_REQUEUE,NULL);
327                }
328                else if (strcmp(name,"reservation") == 0) {
329                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_RESERVATION,value);
330                }
331                else if (strcmp(name,"share") == 0) {
332                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_SHARE,NULL);
333                }               
334                else {
335                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
336                                        "Invalid native specification: %s (Unsupported option: --%s)",
337                                        add_attr, name);
338                }
339       
340                /*}*/
341          }
342        FINALLY
343          {
344                fsd_free(name);
345                fsd_free(add_attr_copy);
346          }
347        END_TRY
348}
349
350void
351slurmdrmaa_parse_native(job_desc_msg_t *job_desc, const char * value)
352{
353        char *arg = NULL;
354        char * volatile native_specification = fsd_strdup(value);
355        char * volatile native_spec_copy = fsd_strdup(native_specification);
356        char * ctxt = NULL;
357        int opt = 0;
358               
359        fsd_log_enter(( "" ));
360        TRY
361         {
362                for (arg = strtok_r(native_spec_copy, " \t", &ctxt); arg; arg = strtok_r(NULL, " \t",&ctxt) ) {
363                        if (!opt) {
364                                if ( (arg[0] != '-') || ((strlen(arg) != 2) && (strlen(arg) > 2) && arg[2] != ' ' && arg[1] !='-' ) ) {
365                                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
366                                                        "Invalid native specification: %s",
367                                                        native_specification);
368                                }
369                                if(arg[1] == '-') {
370                                        slurmdrmaa_parse_additional_attr(job_desc, arg+2);
371                                }
372                                else {
373                                        opt = arg[1];
374                                }               
375                        } else {
376                                switch (opt) {                 
377                                        case 'A' :
378                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_ACCOUNT, arg);
379                                                break;
380                                        case 'C' :
381                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_CONSTRAINT, arg);
382                                                break; 
383                                        case 'N' :     
384                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODES, arg);
385                                                break; 
386                                        case 'p' :
387                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_PARTITION, arg);
388                                                break;
389                                        case 's' :
390                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_SHARE, NULL);
391                                                break; 
392                                        case 'w' :
393                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODELIST, arg);
394                                                break;         
395                                        default :                                                               
396                                                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
397                                                                        "Invalid native specification: %s (Unsupported option: -%c)",
398                                                                        native_specification, opt);
399                                }
400                                if (arg[0] == '-' && strlen(arg) == 2) { /* attribute without arg */
401                                        opt = arg[1];
402                                }
403                                else {
404                                        opt = 0;
405                                }
406                        }
407                }
408
409                if(strlen(native_spec_copy) == 2 && native_spec_copy[0] == '-' && native_spec_copy[1] == 's')
410                {
411                        slurmdrmaa_add_attribute(job_desc, SLURM_NATIVE_SHARE, NULL);
412                        opt = 0;
413                }
414               
415                if (opt)
416                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
417                                        "Invalid native specification: %s",
418                                        native_specification);
419
420         }
421        FINALLY
422         {
423                fsd_free(native_spec_copy);
424                fsd_free(native_specification);
425         }
426        END_TRY
427
428        fsd_log_return(( "" ));
429}
430
Note: See TracBrowser for help on using the repository browser.