source: branches/slurm-drmaa-1.0.0/slurm_drmaa/util.c @ 5

Revision 5, 11.2 KB checked in by mmatloka, 14 years ago (diff)

svn:keywords

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1/* $Id$ */
2/*
3 * PSNC DRMAA for SLURM
4 * Copyright (C) 2010 Poznan Supercomputing and Networking Center
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
34        = "$Id$";
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:
177                        if(job_desc->job_min_memory == NO_VAL ||  fsd_atoi(value) > (int)job_desc->job_min_memory) {
178                                fsd_log_debug(("# job_min_memory = %s",value));
179                                job_desc->job_min_memory = fsd_atoi(value);
180                        }
181                        else {
182                                fsd_log_debug(("mem value defined lower or equal to mem-per-cpu or value defined before"));
183                        }
184                        break;
185                case SLURM_NATIVE_MEM_PER_CPU:
186                        if(job_desc->job_min_memory == NO_VAL ||  fsd_atoi(value) > (int)job_desc->job_min_memory) {
187                                fsd_log_debug(("# job_min_memory = %s",value));
188                                job_desc->job_min_memory = fsd_atoi(value);
189                        }
190                        else {
191                                fsd_log_debug(("mem-per-cpu value defined lower or equal to mem or value defined before"));
192                        }
193                        break;
194                case SLURM_NATIVE_MINCPUS:
195                        fsd_log_debug(("# job_min_cpus = %s",value));
196                        job_desc->job_min_cpus = fsd_atoi(value);
197                        break;
198                case SLURM_NATIVE_NODELIST:
199                        fsd_free(job_desc->req_nodes);
200                        fsd_log_debug(("# node-list - > req_nodes = %s",value));
201                        job_desc->req_nodes = fsd_strdup(value);
202                        break;
203                case SLURM_NATIVE_NODES:
204                        ptr = strdup(value);
205
206                        if((token = strtok_r(ptr,"=",&rest)) == NULL){
207                                fsd_log_error(("strtok_r returned NULL"));
208                        }
209                                               
210                        fsd_log_debug(("nodes: %s ->",value));
211                        fsd_log_debug(("# min_nodes = %s",token));
212                        job_desc->min_nodes = fsd_atoi(token);
213                                               
214                        if(strcmp(rest,"") !=0 ) {
215                                fsd_log_debug(("# max_nodes = %s",rest));
216                                job_desc->max_nodes = fsd_atoi(rest);
217                        }
218                        fsd_free(ptr);
219                        break;         
220                case SLURM_NATIVE_NTASKS_PER_NODE:
221                        fsd_log_debug(("# ntasks_per_node = %s",value));
222                        job_desc->ntasks_per_node = fsd_atoi(value);
223                        break;
224                case SLURM_NATIVE_PARTITION:
225                        fsd_free(job_desc->partition);
226                        fsd_log_debug(("# partition = %s",value));
227                        job_desc->partition = fsd_strdup(value);
228                        break;
229                case SLURM_NATIVE_QOS:
230                        fsd_free(job_desc->qos);
231                        fsd_log_debug(("# qos = %s",value));
232                        job_desc->qos = fsd_strdup(value);
233                        break;
234                case SLURM_NATIVE_REQUEUE:
235                        fsd_log_debug(( "# requeue = 1"));
236                        job_desc->requeue = 1;
237                        break;
238                case SLURM_NATIVE_RESERVATION:
239                        fsd_log_debug(("# reservation = %s",value));
240                        job_desc->reservation = fsd_strdup(value);
241                        break;
242                case SLURM_NATIVE_SHARE:
243                        fsd_log_debug(("# shared = 1"));
244                        job_desc->shared = 1;
245                        break;
246                default:
247                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,"Invalid attribute");
248        }
249}
250
251void
252slurmdrmaa_parse_additional_attr(job_desc_msg_t *job_desc,const char *add_attr)
253{
254        char * volatile name = NULL;
255        char *value = NULL;
256        char *ctxt = NULL;
257        char * volatile add_attr_copy = fsd_strdup(add_attr);
258
259        TRY
260          {
261                name = fsd_strdup(strtok_r(add_attr_copy, "=", &ctxt));
262                value = strtok_r(NULL, "=", &ctxt);
263                       
264                if(strcmp(name,"account") == 0) {
265                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_ACCOUNT,value);
266                }
267                else if(strcmp(name,"acctg-freq") == 0) {
268                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_ACCTG_FREQ,value);
269                }
270                else if (strcmp(name,"comment") == 0) {
271                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_COMMENT,value);
272                }
273                else if (strcmp(name,"constraint") == 0) {
274                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_CONSTRAINT,value);
275                }
276                else if (strcmp(name,"contiguous") == 0) {
277                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_CONTIGUOUS,NULL);
278                }
279                else if(strcmp(name,"exclusive") == 0) {
280                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_EXCLUSIVE,NULL);
281                }
282                else if (strcmp(name,"mem") == 0) {
283                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_MEM,value);
284                }
285                else if (strcmp(name,"mem-per-cpu") == 0) {
286                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_MEM_PER_CPU,value);
287                }
288                else if (strcmp(name,"mincpus") == 0) {
289                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_MINCPUS,value);
290                }
291                else if (strcmp(name,"nodelist") == 0) {
292                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODELIST,value);                 
293                }
294                else if (strcmp(name,"nodes") == 0) {
295                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODES,value);
296                }
297                else if (strcmp(name,"ntasks-per-node") == 0) {
298                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NTASKS_PER_NODE,value);
299                }
300                else if (strcmp(name,"partition") == 0) {
301                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_PARTITION,value);
302                }
303                else if (strcmp(name,"qos") == 0) {
304                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_QOS,value);
305                }
306                else if (strcmp(name,"requeue") == 0) {
307                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_REQUEUE,NULL);
308                }
309                else if (strcmp(name,"reservation") == 0) {
310                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_RESERVATION,value);
311                }
312                else if (strcmp(name,"share") == 0) {
313                        slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_SHARE,NULL);
314                }               
315                else {
316                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
317                                        "Invalid native specification: %s (Unsupported option: --%s)",
318                                        add_attr, name);
319                }
320       
321                /*}*/
322          }
323        FINALLY
324          {
325                fsd_free(name);
326                fsd_free(add_attr_copy);
327          }
328        END_TRY
329}
330
331void
332slurmdrmaa_parse_native(job_desc_msg_t *job_desc, const char * value)
333{
334        char *arg = NULL;
335        char * volatile native_specification = fsd_strdup(value);
336        char * volatile native_spec_copy = fsd_strdup(native_specification);
337        char * ctxt = NULL;
338        int opt = 0;
339               
340        fsd_log_enter(( "" ));
341        TRY
342         {
343                for (arg = strtok_r(native_spec_copy, " \t", &ctxt); arg; arg = strtok_r(NULL, " \t",&ctxt) ) {
344                        if (!opt) {
345                                if ( (arg[0] != '-') || ((strlen(arg) != 2) && (strlen(arg) > 2) && arg[2] != ' ' && arg[1] !='-' ) ) {
346                                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
347                                                        "Invalid native specification: %s",
348                                                        native_specification);
349                                }
350                                if(arg[1] == '-') {
351                                        slurmdrmaa_parse_additional_attr(job_desc, arg+2);
352                                }
353                                else {
354                                        opt = arg[1];
355                                }               
356                        } else {
357                                switch (opt) {                 
358                                        case 'A' :
359                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_ACCOUNT, arg);
360                                                break;
361                                        case 'C' :
362                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_CONSTRAINT, arg);
363                                                break; 
364                                        case 'N' :     
365                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODES, arg);
366                                                break; 
367                                        case 'p' :
368                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_PARTITION, arg);
369                                                break;
370                                        case 's' :
371                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_SHARE, NULL);
372                                                break; 
373                                        case 'w' :
374                                                slurmdrmaa_add_attribute(job_desc,SLURM_NATIVE_NODELIST, arg);
375                                                break;         
376                                        default :                                                               
377                                                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
378                                                                        "Invalid native specification: %s (Unsupported option: -%c)",
379                                                                        native_specification, opt);
380                                }
381                                if (arg[0] == '-' && strlen(arg) == 2) { /* attribute without arg */
382                                        opt = arg[1];
383                                }
384                                else {
385                                        opt = 0;
386                                }
387                        }
388                }
389
390                if(strlen(native_spec_copy) == 2 && native_spec_copy[0] == '-' && native_spec_copy[1] == 's')
391                {
392                        slurmdrmaa_add_attribute(job_desc, SLURM_NATIVE_SHARE, NULL);
393                        opt = 0;
394                }
395               
396                if (opt)
397                        fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE,
398                                        "Invalid native specification: %s",
399                                        native_specification);
400
401         }
402        FINALLY
403         {
404                fsd_free(native_spec_copy);
405                fsd_free(native_specification);
406         }
407        END_TRY
408
409        fsd_log_return(( "" ));
410}
411
Note: See TracBrowser for help on using the repository browser.