1 | /* $Id$ */ |
---|
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 <unistd.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | #include <pbs_ifl.h> |
---|
28 | #include <pbs_error.h> |
---|
29 | |
---|
30 | #include <drmaa_utils/conf.h> |
---|
31 | #include <drmaa_utils/drmaa.h> |
---|
32 | #include <drmaa_utils/drmaa_util.h> |
---|
33 | #include <drmaa_utils/datetime.h> |
---|
34 | #include <drmaa_utils/iter.h> |
---|
35 | #include <drmaa_utils/template.h> |
---|
36 | #include <pbs_drmaa/pbs_attrib.h> |
---|
37 | #include <pbs_drmaa/session.h> |
---|
38 | #include <pbs_drmaa/submit.h> |
---|
39 | #include <pbs_drmaa/util.h> |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | #ifndef lint |
---|
44 | static char rcsid[] |
---|
45 | # ifdef __GNUC__ |
---|
46 | __attribute__ ((unused)) |
---|
47 | # endif |
---|
48 | = "$Id$"; |
---|
49 | #endif |
---|
50 | |
---|
51 | static void pbsdrmaa_submit_destroy( pbsdrmaa_submit_t *self ); |
---|
52 | |
---|
53 | static char *pbsdrmaa_submit_submit( pbsdrmaa_submit_t *self ); |
---|
54 | |
---|
55 | static void pbsdrmaa_submit_eval( pbsdrmaa_submit_t *self ); |
---|
56 | |
---|
57 | static void pbsdrmaa_submit_set( pbsdrmaa_submit_t *self, const char *pbs_attr, char *value, unsigned placeholders ); |
---|
58 | |
---|
59 | static void pbsdrmaa_submit_apply_defaults( pbsdrmaa_submit_t *self ); |
---|
60 | static void pbsdrmaa_submit_apply_job_script( pbsdrmaa_submit_t *self ); |
---|
61 | static void pbsdrmaa_submit_apply_job_state( pbsdrmaa_submit_t *self ); |
---|
62 | static void pbsdrmaa_submit_apply_job_files( pbsdrmaa_submit_t *self ); |
---|
63 | static void pbsdrmaa_submit_apply_file_staging( pbsdrmaa_submit_t *self ); |
---|
64 | static void pbsdrmaa_submit_apply_job_resources( pbsdrmaa_submit_t *self ); |
---|
65 | static void pbsdrmaa_submit_apply_job_environment( pbsdrmaa_submit_t *self ); |
---|
66 | static void pbsdrmaa_submit_apply_email_notification( pbsdrmaa_submit_t *self ); |
---|
67 | static void pbsdrmaa_submit_apply_job_category( pbsdrmaa_submit_t *self ); |
---|
68 | |
---|
69 | |
---|
70 | pbsdrmaa_submit_t * |
---|
71 | pbsdrmaa_submit_new( fsd_drmaa_session_t *session, |
---|
72 | const fsd_template_t *job_template, int bulk_idx ) |
---|
73 | { |
---|
74 | pbsdrmaa_submit_t *volatile self = NULL; |
---|
75 | TRY |
---|
76 | { |
---|
77 | fsd_malloc( self, pbsdrmaa_submit_t ); |
---|
78 | self->session = session; |
---|
79 | self->job_template = job_template; |
---|
80 | self->script_filename = NULL; |
---|
81 | self->destination_queue = NULL; |
---|
82 | self->pbs_job_attributes = NULL; |
---|
83 | self->expand_ph = NULL; |
---|
84 | self->destroy = pbsdrmaa_submit_destroy; |
---|
85 | self->submit = pbsdrmaa_submit_submit; |
---|
86 | self->eval = pbsdrmaa_submit_eval; |
---|
87 | self->set = pbsdrmaa_submit_set; |
---|
88 | self->apply_defaults = pbsdrmaa_submit_apply_defaults; |
---|
89 | self->apply_job_category = pbsdrmaa_submit_apply_job_category; |
---|
90 | self->apply_job_script = pbsdrmaa_submit_apply_job_script; |
---|
91 | self->apply_job_state = pbsdrmaa_submit_apply_job_state; |
---|
92 | self->apply_job_files = pbsdrmaa_submit_apply_job_files; |
---|
93 | self->apply_file_staging = pbsdrmaa_submit_apply_file_staging; |
---|
94 | self->apply_job_resources = pbsdrmaa_submit_apply_job_resources; |
---|
95 | self->apply_job_environment = pbsdrmaa_submit_apply_job_environment; |
---|
96 | self->apply_email_notification = pbsdrmaa_submit_apply_email_notification; |
---|
97 | self->apply_native_specification = pbsdrmaa_submit_apply_native_specification; |
---|
98 | |
---|
99 | self->pbs_job_attributes = pbsdrmaa_pbs_template_new(); |
---|
100 | self->expand_ph = fsd_expand_drmaa_ph_new( NULL, NULL, |
---|
101 | (bulk_idx >= 0) ? fsd_asprintf("%d", bulk_idx) : NULL ); |
---|
102 | } |
---|
103 | EXCEPT_DEFAULT |
---|
104 | { |
---|
105 | if( self ) |
---|
106 | self->destroy( self ); |
---|
107 | } |
---|
108 | END_TRY |
---|
109 | return self; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void |
---|
114 | pbsdrmaa_submit_destroy( pbsdrmaa_submit_t *self ) |
---|
115 | { |
---|
116 | if( self->script_filename ) |
---|
117 | { |
---|
118 | unlink( self->script_filename ); |
---|
119 | fsd_free( self->script_filename ); |
---|
120 | } |
---|
121 | if( self->pbs_job_attributes ) |
---|
122 | self->pbs_job_attributes->destroy( self->pbs_job_attributes ); |
---|
123 | if( self->expand_ph ) |
---|
124 | self->expand_ph->destroy( self->expand_ph ); |
---|
125 | fsd_free( self->destination_queue ); |
---|
126 | fsd_free( self ); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | char * |
---|
131 | pbsdrmaa_submit_submit( pbsdrmaa_submit_t *self ) |
---|
132 | { |
---|
133 | volatile bool conn_lock = false; |
---|
134 | struct attrl *volatile pbs_attr = NULL; |
---|
135 | char *volatile job_id = NULL; |
---|
136 | TRY |
---|
137 | { |
---|
138 | fsd_template_t *pbs_tmpl = self->pbs_job_attributes; |
---|
139 | int i; |
---|
140 | int tries_left = ((pbsdrmaa_session_t *)self->session)->max_retries_count; |
---|
141 | int sleep_time = 1; |
---|
142 | |
---|
143 | for( i = PBSDRMAA_N_PBS_ATTRIBUTES - 1; i >= 0; i-- ) /* down loop -> start with custom resources */ |
---|
144 | { |
---|
145 | const char *name = pbs_tmpl->by_code( pbs_tmpl, i )->name; |
---|
146 | const char *value = pbs_tmpl->get_attr( pbs_tmpl, name ); |
---|
147 | |
---|
148 | if (!value) |
---|
149 | continue; |
---|
150 | |
---|
151 | if ( i == PBSDRMAA_ATTR_CUSTOM_RESOURCES) |
---|
152 | { |
---|
153 | char *value_copy = fsd_strdup(value); |
---|
154 | char *tok_comma_ctx = NULL; |
---|
155 | char *res_token = NULL; |
---|
156 | /* matlab:2,simulink:1 */ |
---|
157 | |
---|
158 | for (res_token = strtok_r(value_copy, ",", &tok_comma_ctx); res_token; res_token = strtok_r(NULL, ",", &tok_comma_ctx)) |
---|
159 | { |
---|
160 | char *value_p = strstr(res_token, ":"); |
---|
161 | |
---|
162 | if (value_p) |
---|
163 | { |
---|
164 | char *name_p = NULL; |
---|
165 | *value_p = '\0'; |
---|
166 | value_p++; |
---|
167 | name_p = fsd_asprintf("Resource_List.%s",res_token); |
---|
168 | pbs_attr = pbsdrmaa_add_attr( pbs_attr, name_p, value_p ); |
---|
169 | fsd_free(name_p); |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | fsd_exc_raise_code( FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | fsd_free(value_copy); |
---|
178 | } |
---|
179 | else if (i == PBSDRMAA_ATTR_NODE_PROPERTIES) |
---|
180 | { |
---|
181 | const char *nodes_value = pbs_tmpl->get_attr( pbs_tmpl, PBSDRMAA_NODES ); |
---|
182 | char *final_value = NULL; |
---|
183 | |
---|
184 | if (nodes_value) |
---|
185 | { |
---|
186 | final_value = fsd_asprintf("%s:%s",nodes_value, value); |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | final_value = fsd_asprintf("1:%s", value); |
---|
191 | } |
---|
192 | |
---|
193 | pbs_tmpl->set_attr( pbs_tmpl, PBSDRMAA_NODES, final_value); |
---|
194 | fsd_free(final_value); |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | pbs_attr = pbsdrmaa_add_attr( pbs_attr, name, value ); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | conn_lock = fsd_mutex_lock( &self->session->drm_connection_mutex ); |
---|
203 | retry: |
---|
204 | job_id = pbs_submit( ((pbsdrmaa_session_t*)self->session)->pbs_conn, |
---|
205 | (struct attropl*)pbs_attr, self->script_filename, |
---|
206 | self->destination_queue, NULL ); |
---|
207 | |
---|
208 | fsd_log_info(("pbs_submit(%s, %s) =%s", self->script_filename, self->destination_queue, job_id)); |
---|
209 | |
---|
210 | if( job_id == NULL ) |
---|
211 | { |
---|
212 | if (pbs_errno == PBSE_PROTOCOL || pbs_errno == PBSE_EXPIRED) |
---|
213 | { |
---|
214 | pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self->session; |
---|
215 | if (pbsself->pbs_conn >= 0 ) |
---|
216 | pbs_disconnect( pbsself->pbs_conn ); |
---|
217 | retry_connect: |
---|
218 | sleep(sleep_time++); |
---|
219 | pbsself->pbs_conn = pbs_connect( pbsself->super.contact ); |
---|
220 | if( pbsself->pbs_conn < 0) |
---|
221 | if (tries_left--) |
---|
222 | goto retry_connect; |
---|
223 | else |
---|
224 | pbsdrmaa_exc_raise_pbs( "pbs_connect" ); |
---|
225 | else |
---|
226 | goto retry; |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | pbsdrmaa_exc_raise_pbs( "pbs_submit" ); |
---|
231 | } |
---|
232 | } |
---|
233 | conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex ); |
---|
234 | } |
---|
235 | EXCEPT_DEFAULT |
---|
236 | { |
---|
237 | fsd_free( job_id ); |
---|
238 | fsd_exc_reraise(); |
---|
239 | } |
---|
240 | FINALLY |
---|
241 | { |
---|
242 | if( conn_lock ) |
---|
243 | conn_lock = fsd_mutex_unlock( &self->session->drm_connection_mutex ); |
---|
244 | if( pbs_attr ) |
---|
245 | pbsdrmaa_free_attrl( pbs_attr ); |
---|
246 | } |
---|
247 | END_TRY |
---|
248 | return job_id; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | void |
---|
253 | pbsdrmaa_submit_eval( pbsdrmaa_submit_t *self ) |
---|
254 | { |
---|
255 | self->apply_defaults( self ); |
---|
256 | self->apply_job_category( self ); |
---|
257 | self->apply_job_script( self ); |
---|
258 | self->apply_job_state( self ); |
---|
259 | self->apply_job_files( self ); |
---|
260 | self->apply_file_staging( self ); |
---|
261 | self->apply_job_resources( self ); |
---|
262 | self->apply_job_environment( self ); |
---|
263 | self->apply_email_notification( self ); |
---|
264 | self->apply_native_specification( self, NULL ); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | void |
---|
269 | pbsdrmaa_submit_set( pbsdrmaa_submit_t *self, const char *name, |
---|
270 | char *value, unsigned placeholders ) |
---|
271 | { |
---|
272 | fsd_template_t *pbs_attr = self->pbs_job_attributes; |
---|
273 | TRY |
---|
274 | { |
---|
275 | if( placeholders ) |
---|
276 | value = self->expand_ph->expand( |
---|
277 | self->expand_ph, value, placeholders ); |
---|
278 | pbs_attr->set_attr( pbs_attr, name, value ); |
---|
279 | } |
---|
280 | FINALLY |
---|
281 | { |
---|
282 | fsd_free( value ); |
---|
283 | } |
---|
284 | END_TRY |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | void |
---|
289 | pbsdrmaa_submit_apply_defaults( pbsdrmaa_submit_t *self ) |
---|
290 | { |
---|
291 | fsd_template_t *pbs_attr = self->pbs_job_attributes; |
---|
292 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_CHECKPOINT, "u" ); |
---|
293 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_KEEP_FILES, "n" ); |
---|
294 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_PRIORITY, "0" ); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | void |
---|
299 | pbsdrmaa_submit_apply_job_script( pbsdrmaa_submit_t *self ) |
---|
300 | { |
---|
301 | const fsd_template_t *jt = self->job_template; |
---|
302 | /* fsd_template_t *pbs_attr = self->pbs_job_attributes; */ |
---|
303 | fsd_expand_drmaa_ph_t *expand = self->expand_ph; |
---|
304 | char *script = NULL; |
---|
305 | size_t script_len; |
---|
306 | const char *executable; |
---|
307 | const char *wd; |
---|
308 | const char *const *argv; |
---|
309 | const char *input_path; |
---|
310 | const char *const *i; |
---|
311 | |
---|
312 | executable = jt->get_attr( jt, DRMAA_REMOTE_COMMAND ); |
---|
313 | wd = jt->get_attr( jt, DRMAA_WD ); |
---|
314 | argv = jt->get_v_attr( jt, DRMAA_V_ARGV ); |
---|
315 | input_path = jt->get_attr( jt, DRMAA_INPUT_PATH ); |
---|
316 | |
---|
317 | if( wd ) |
---|
318 | { |
---|
319 | char *cwd = NULL; |
---|
320 | cwd = expand->expand( expand, fsd_strdup(wd), |
---|
321 | FSD_DRMAA_PH_HD | FSD_DRMAA_PH_INCR ); |
---|
322 | expand->set( expand, FSD_DRMAA_PH_WD, cwd ); |
---|
323 | } |
---|
324 | |
---|
325 | if( executable == NULL ) |
---|
326 | fsd_exc_raise_code( FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE ); |
---|
327 | |
---|
328 | if( input_path != NULL ) |
---|
329 | { |
---|
330 | if( input_path[0] == ':' ) |
---|
331 | input_path++; |
---|
332 | } |
---|
333 | |
---|
334 | { /* compute script length */ |
---|
335 | script_len = 0; |
---|
336 | if( wd != NULL ) |
---|
337 | script_len += strlen("cd ") + strlen(wd) + strlen("; "); |
---|
338 | script_len += strlen(executable); |
---|
339 | if( argv != NULL ) |
---|
340 | for( i = argv; *i != NULL; i++ ) |
---|
341 | script_len += 3+strlen(*i); |
---|
342 | if( input_path != NULL ) |
---|
343 | script_len += strlen(" <") + strlen(input_path); |
---|
344 | if (((pbsdrmaa_session_t *)self->session)->job_exit_status_file_prefix) |
---|
345 | script_len += strlen("; echo $? >") + strlen(((pbsdrmaa_session_t *)self->session)->job_exit_status_file_prefix) + strlen("/$PBS_JOBID.exitcode"); |
---|
346 | } |
---|
347 | |
---|
348 | fsd_calloc( script, script_len+1, char ); |
---|
349 | |
---|
350 | { |
---|
351 | char *s; |
---|
352 | s = script; |
---|
353 | if( wd != NULL ) |
---|
354 | s += sprintf( s, "cd %s; ", wd ); |
---|
355 | s += sprintf( s, "%s", executable ); |
---|
356 | if( argv != NULL ) |
---|
357 | for( i = argv; *i != NULL; i++ ) |
---|
358 | s += sprintf( s, " '%s'", *i ); |
---|
359 | if( input_path != NULL ) |
---|
360 | s += sprintf( s, " <%s", input_path ); |
---|
361 | if (((pbsdrmaa_session_t *)self->session)->job_exit_status_file_prefix) |
---|
362 | s += sprintf( s, "; echo $? >%s/$PBS_JOBID.exitcode", ((pbsdrmaa_session_t *)self->session)->job_exit_status_file_prefix); |
---|
363 | |
---|
364 | fsd_assert( s == script+script_len ); |
---|
365 | } |
---|
366 | |
---|
367 | script = expand->expand( expand, script, |
---|
368 | FSD_DRMAA_PH_HD | FSD_DRMAA_PH_WD | FSD_DRMAA_PH_INCR ); |
---|
369 | |
---|
370 | /* pbs_attr->set_attr( pbs_attr, "!script", script ); */ |
---|
371 | |
---|
372 | self->script_filename = pbsdrmaa_write_tmpfile( script, strlen(script) ); |
---|
373 | fsd_free( script ); |
---|
374 | } |
---|
375 | |
---|
376 | |
---|
377 | void |
---|
378 | pbsdrmaa_submit_apply_job_state( pbsdrmaa_submit_t *self ) |
---|
379 | { |
---|
380 | const fsd_template_t *jt = self->job_template; |
---|
381 | fsd_template_t *pbs_attr = self->pbs_job_attributes; |
---|
382 | const char *job_name = NULL; |
---|
383 | const char *submit_state = NULL; |
---|
384 | const char *drmaa_start_time = NULL; |
---|
385 | |
---|
386 | job_name = jt->get_attr( jt, DRMAA_JOB_NAME ); |
---|
387 | submit_state = jt->get_attr( jt, DRMAA_JS_STATE ); |
---|
388 | drmaa_start_time = jt->get_attr( jt, DRMAA_START_TIME ); |
---|
389 | |
---|
390 | if( job_name != NULL ) |
---|
391 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_JOB_NAME, job_name ); |
---|
392 | |
---|
393 | if( submit_state != NULL ) |
---|
394 | { |
---|
395 | const char *hold_types; |
---|
396 | if( !strcmp(submit_state, DRMAA_SUBMISSION_STATE_ACTIVE) ) |
---|
397 | hold_types = "n"; |
---|
398 | else if( !strcmp(submit_state, DRMAA_SUBMISSION_STATE_HOLD) ) |
---|
399 | hold_types = "u"; |
---|
400 | else |
---|
401 | fsd_exc_raise_fmt( FSD_ERRNO_INVALID_VALUE, |
---|
402 | "invalid value of %s attribute (%s|%s)", |
---|
403 | DRMAA_JS_STATE, DRMAA_SUBMISSION_STATE_ACTIVE, |
---|
404 | DRMAA_SUBMISSION_STATE_HOLD ); |
---|
405 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_HOLD_TYPES, hold_types ); |
---|
406 | } |
---|
407 | |
---|
408 | if( drmaa_start_time != NULL ) |
---|
409 | { |
---|
410 | time_t start_time; |
---|
411 | char pbs_start_time[20]; |
---|
412 | struct tm start_time_tm; |
---|
413 | start_time = fsd_datetime_parse( drmaa_start_time ); |
---|
414 | localtime_r( &start_time, &start_time_tm ); |
---|
415 | sprintf( pbs_start_time, "%04d%02d%02d%02d%02d.%02d", |
---|
416 | start_time_tm.tm_year + 1900, |
---|
417 | start_time_tm.tm_mon + 1, |
---|
418 | start_time_tm.tm_mday, |
---|
419 | start_time_tm.tm_hour, |
---|
420 | start_time_tm.tm_min, |
---|
421 | start_time_tm.tm_sec |
---|
422 | ); |
---|
423 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_EXECUTION_TIME, pbs_start_time ); |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | |
---|
428 | void |
---|
429 | pbsdrmaa_submit_apply_job_files( pbsdrmaa_submit_t *self ) |
---|
430 | { |
---|
431 | const fsd_template_t *jt = self->job_template; |
---|
432 | fsd_template_t *pbs_attr = self->pbs_job_attributes; |
---|
433 | const char *join_files; |
---|
434 | bool b_join_files; |
---|
435 | int i; |
---|
436 | |
---|
437 | for( i = 0; i < 2; i++ ) |
---|
438 | { |
---|
439 | const char *drmaa_name; |
---|
440 | const char *pbs_name; |
---|
441 | const char *path; |
---|
442 | |
---|
443 | if( i == 0 ) |
---|
444 | { |
---|
445 | drmaa_name = DRMAA_OUTPUT_PATH; |
---|
446 | pbs_name = PBSDRMAA_OUTPUT_PATH; |
---|
447 | } |
---|
448 | else |
---|
449 | { |
---|
450 | drmaa_name = DRMAA_ERROR_PATH; |
---|
451 | pbs_name = PBSDRMAA_ERROR_PATH; |
---|
452 | } |
---|
453 | |
---|
454 | path = jt->get_attr( jt, drmaa_name ); |
---|
455 | if( path != NULL ) |
---|
456 | { |
---|
457 | if( path[0] == ':' ) |
---|
458 | path++; |
---|
459 | self->set(self, pbs_name, fsd_strdup(path), FSD_DRMAA_PH_HD | FSD_DRMAA_PH_WD | FSD_DRMAA_PH_INCR); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | join_files = jt->get_attr( jt, DRMAA_JOIN_FILES ); |
---|
464 | b_join_files = join_files != NULL && !strcmp(join_files,"1"); |
---|
465 | pbs_attr->set_attr( pbs_attr, PBSDRMAA_JOIN_FILES, (b_join_files ? "y" : "n") ); |
---|
466 | } |
---|
467 | |
---|
468 | |
---|
469 | void |
---|
470 | pbsdrmaa_submit_apply_file_staging( pbsdrmaa_submit_t *self ) |
---|
471 | { |
---|
472 | /* TODO */ |
---|
473 | } |
---|
474 | |
---|
475 | |
---|
476 | void |
---|
477 | pbsdrmaa_submit_apply_job_resources( pbsdrmaa_submit_t *self ) |
---|
478 | { |
---|
479 | const fsd_template_t *jt = self->job_template; |
---|
480 | fsd_template_t *pbs_attr = self->pbs_job_attributes; |
---|
481 | const char *cpu_time_limit = NULL; |
---|
482 | const char *walltime_limit = NULL; |
---|
483 | |
---|
484 | cpu_time_limit = jt->get_attr( jt, DRMAA_DURATION_HLIMIT ); |
---|
485 | walltime_limit = jt->get_attr( jt, DRMAA_WCT_HLIMIT ); |
---|
486 | if( cpu_time_limit ) |
---|
487 | { |
---|
488 | pbs_attr->set_attr( pbs_attr, "Resource_List.pcput", cpu_time_limit ); |
---|
489 | pbs_attr->set_attr( pbs_attr, "Resource_List.cput", cpu_time_limit ); |
---|
490 | } |
---|
491 | if( walltime_limit ) |
---|
492 | pbs_attr->set_attr( pbs_attr, "Resource_List.walltime", walltime_limit ); |
---|
493 | } |
---|
494 | |
---|
495 | |
---|
496 | |
---|
497 | void |
---|
498 | pbsdrmaa_submit_apply_job_environment( pbsdrmaa_submit_t *self ) |
---|
499 | { |
---|
500 | const fsd_template_t *jt = self->job_template; |
---|
501 | const char *const *env_v; |
---|
502 | const char *jt_wd; |
---|
503 | char *wd; |
---|
504 | char *env_c = NULL; |
---|
505 | int ii = 0, len = 0; |
---|
506 | |
---|
507 | env_v = jt->get_v_attr( jt, DRMAA_V_ENV); |
---|
508 | jt_wd = jt->get_attr( jt, DRMAA_WD ); |
---|
509 | |
---|
510 | if (!jt_wd) |
---|
511 | { |
---|
512 | wd = fsd_getcwd(); |
---|
513 | } |
---|
514 | else |
---|
515 | { |
---|
516 | wd = fsd_strdup(jt_wd); |
---|
517 | } |
---|
518 | |
---|
519 | if (env_v) |
---|
520 | { |
---|
521 | ii = 0; |
---|
522 | while (env_v[ii]) |
---|
523 | { |
---|
524 | len += strlen(env_v[ii]) + 1; |
---|
525 | ii++; |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | len+= strlen("PBS_O_WORKDIR=") + strlen(wd); |
---|
530 | |
---|
531 | fsd_calloc(env_c, len + 1, char); |
---|
532 | env_c[0] = '\0'; |
---|
533 | |
---|
534 | if (env_v) |
---|
535 | { |
---|
536 | ii = 0; |
---|
537 | while (env_v[ii]) { |
---|
538 | strcat(env_c, env_v[ii]); |
---|
539 | strcat(env_c, ","); |
---|
540 | ii++; |
---|
541 | } |
---|
542 | |
---|
543 | } |
---|
544 | |
---|
545 | strcat(env_c, "PBS_O_WORKDIR="); |
---|
546 | strcat(env_c, wd); |
---|
547 | |
---|
548 | self->pbs_job_attributes->set_attr(self->pbs_job_attributes, "Variable_List", env_c); |
---|
549 | |
---|
550 | fsd_free(env_c); |
---|
551 | fsd_free(wd); |
---|
552 | } |
---|
553 | |
---|
554 | |
---|
555 | void |
---|
556 | pbsdrmaa_submit_apply_email_notification( pbsdrmaa_submit_t *self ) |
---|
557 | { |
---|
558 | /* TODO */ |
---|
559 | } |
---|
560 | |
---|
561 | |
---|
562 | void |
---|
563 | pbsdrmaa_submit_apply_job_category( pbsdrmaa_submit_t *self ) |
---|
564 | { |
---|
565 | const char *job_category = NULL; |
---|
566 | const char *category_spec = NULL; |
---|
567 | fsd_conf_option_t *value = NULL; |
---|
568 | |
---|
569 | job_category = self->job_template->get_attr( |
---|
570 | self->job_template, DRMAA_JOB_CATEGORY ); |
---|
571 | if( job_category == NULL || job_category[0] == '\0' ) |
---|
572 | job_category = "default"; |
---|
573 | value = fsd_conf_dict_get( self->session->job_categories, |
---|
574 | job_category ); |
---|
575 | if( value != NULL && value->type == FSD_CONF_STRING ) |
---|
576 | category_spec = value->val.string; |
---|
577 | if( category_spec != NULL ) |
---|
578 | self->apply_native_specification( self, category_spec ); |
---|
579 | } |
---|
580 | |
---|
581 | static const char *get_job_env(pbsdrmaa_submit_t *self, const char *env_name) |
---|
582 | { |
---|
583 | const fsd_template_t *jt = self->job_template; |
---|
584 | const char *const *env_v = jt->get_v_attr( jt, DRMAA_V_ENV); |
---|
585 | int ii = 0; |
---|
586 | |
---|
587 | while (env_v[ii]) |
---|
588 | { |
---|
589 | char *eq_p = strstr(env_v[ii], "="); |
---|
590 | |
---|
591 | if ((eq_p) && (strncmp(env_v[ii], env_name, eq_p - env_v[ii]) == 0)) |
---|
592 | return ++eq_p; |
---|
593 | |
---|
594 | ii++; |
---|
595 | } |
---|
596 | |
---|
597 | return NULL; |
---|
598 | } |
---|
599 | |
---|
600 | static void parse_resources(pbsdrmaa_submit_t *self, fsd_template_t *pbs_attr,const char *resources) |
---|
601 | { |
---|
602 | char * volatile name = NULL; |
---|
603 | char *arg = NULL; |
---|
604 | char *value = NULL; |
---|
605 | char *ctxt = NULL; |
---|
606 | char * volatile resources_copy = fsd_strdup(resources); |
---|
607 | |
---|
608 | TRY |
---|
609 | { |
---|
610 | for (arg = strtok_r(resources_copy, ",", &ctxt); arg; arg = strtok_r(NULL, ",",&ctxt) ) |
---|
611 | { |
---|
612 | char *psep = strchr(arg, '='); |
---|
613 | |
---|
614 | if (psep) |
---|
615 | { |
---|
616 | *psep = '\0'; |
---|
617 | name = fsd_asprintf("Resource_List.%s", arg); |
---|
618 | value = ++psep; |
---|
619 | if (value[0] == '$' && get_job_env(self, value + 1)) |
---|
620 | pbs_attr->set_attr( pbs_attr, name , get_job_env(self, value + 1) ); /*get value from job env variable */ |
---|
621 | else |
---|
622 | pbs_attr->set_attr( pbs_attr, name , value ); |
---|
623 | fsd_free(name); |
---|
624 | name = NULL; |
---|
625 | } |
---|
626 | else |
---|
627 | { |
---|
628 | fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE, "Invalid native specification: %s (Invalid resource specification: %s)", resources, arg); |
---|
629 | } |
---|
630 | } |
---|
631 | } |
---|
632 | FINALLY |
---|
633 | { |
---|
634 | fsd_free(name); |
---|
635 | fsd_free(resources_copy); |
---|
636 | } |
---|
637 | END_TRY |
---|
638 | } |
---|
639 | |
---|
640 | static void parse_additional_attr(fsd_template_t *pbs_attr,const char *add_attr) |
---|
641 | { |
---|
642 | char * volatile name = NULL; |
---|
643 | char *arg = NULL; |
---|
644 | char *value = NULL; |
---|
645 | char *ctxt = NULL, *ctxt2 = NULL; |
---|
646 | char * volatile add_attr_copy = fsd_strdup(add_attr); |
---|
647 | |
---|
648 | TRY |
---|
649 | { |
---|
650 | for (arg = strtok_r(add_attr_copy, ";", &ctxt); arg; arg = strtok_r(NULL, ";",&ctxt) ) |
---|
651 | { |
---|
652 | name = fsd_strdup(strtok_r(arg, "=", &ctxt2)); |
---|
653 | value = strtok_r(NULL, "=", &ctxt2); |
---|
654 | pbs_attr->set_attr( pbs_attr, name , value ); |
---|
655 | fsd_free(name); |
---|
656 | name = NULL; |
---|
657 | } |
---|
658 | } |
---|
659 | FINALLY |
---|
660 | { |
---|
661 | fsd_free(name); |
---|
662 | fsd_free(add_attr_copy); |
---|
663 | } |
---|
664 | END_TRY |
---|
665 | } |
---|
666 | |
---|
667 | |
---|
668 | void |
---|
669 | pbsdrmaa_submit_apply_native_specification( pbsdrmaa_submit_t *self, |
---|
670 | const char *native_specification ) |
---|
671 | { |
---|
672 | fsd_log_enter(( "({native_specification=%s})", native_specification )); |
---|
673 | |
---|
674 | if( native_specification == NULL ) |
---|
675 | native_specification = self->job_template->get_attr( |
---|
676 | self->job_template, DRMAA_NATIVE_SPECIFICATION ); |
---|
677 | if( native_specification == NULL ) |
---|
678 | return; |
---|
679 | |
---|
680 | { |
---|
681 | fsd_iter_t * volatile args_list = fsd_iter_new(NULL, 0); |
---|
682 | fsd_template_t *pbs_attr = self->pbs_job_attributes; |
---|
683 | char *arg = NULL; |
---|
684 | volatile char * native_spec_copy = fsd_strdup(native_specification); |
---|
685 | char * ctxt = NULL; |
---|
686 | int opt = 0; |
---|
687 | |
---|
688 | TRY |
---|
689 | { |
---|
690 | for (arg = strtok_r((char *)native_spec_copy, " \t", &ctxt); arg; arg = strtok_r(NULL, " \t",&ctxt) ) { |
---|
691 | if (!opt) |
---|
692 | { |
---|
693 | if ( (arg[0] != '-') || (strlen(arg) != 2) ) |
---|
694 | fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE, |
---|
695 | "Invalid native specification: -o(ption) expected (arg=%s native=%s).", |
---|
696 | arg, native_specification); |
---|
697 | |
---|
698 | opt = arg[1]; |
---|
699 | |
---|
700 | /* handle NO-arg options */ |
---|
701 | |
---|
702 | switch (opt) { |
---|
703 | case 'h' : |
---|
704 | pbs_attr->set_attr( pbs_attr, "Hold_Types" , "u" ); |
---|
705 | break; |
---|
706 | default : |
---|
707 | continue; /*no NO-ARG option */ |
---|
708 | } |
---|
709 | |
---|
710 | opt = 0; |
---|
711 | } |
---|
712 | else |
---|
713 | { |
---|
714 | switch (opt) { |
---|
715 | |
---|
716 | case 'W' : |
---|
717 | parse_additional_attr(pbs_attr, arg); |
---|
718 | break; |
---|
719 | case 'N' : |
---|
720 | pbs_attr->set_attr( pbs_attr, "Job_Name" , arg ); |
---|
721 | break; |
---|
722 | case 'o' : |
---|
723 | pbs_attr->set_attr( pbs_attr, "Output_Path" , arg ); |
---|
724 | break; |
---|
725 | case 'e' : |
---|
726 | pbs_attr->set_attr( pbs_attr, "Error_Path" , arg ); |
---|
727 | break; |
---|
728 | case 'j' : |
---|
729 | pbs_attr->set_attr( pbs_attr, "Join_Path" , arg ); |
---|
730 | break; |
---|
731 | case 'm' : |
---|
732 | pbs_attr->set_attr( pbs_attr, "Mail_Points" , arg ); |
---|
733 | break; |
---|
734 | case 'a' : |
---|
735 | pbs_attr->set_attr( pbs_attr, "Execution_Time" , arg ); |
---|
736 | break; |
---|
737 | case 'A' : |
---|
738 | pbs_attr->set_attr( pbs_attr, "Account_Name" , arg ); |
---|
739 | break; |
---|
740 | case 'c' : |
---|
741 | pbs_attr->set_attr( pbs_attr, "Checkpoint" , arg ); |
---|
742 | break; |
---|
743 | case 'k' : |
---|
744 | pbs_attr->set_attr( pbs_attr, "Keep_Files" , arg ); |
---|
745 | break; |
---|
746 | case 'p' : |
---|
747 | pbs_attr->set_attr( pbs_attr, "Priority" , arg ); |
---|
748 | break; |
---|
749 | case 'q' : |
---|
750 | if (self->destination_queue) |
---|
751 | fsd_free(self->destination_queue); |
---|
752 | |
---|
753 | self->destination_queue = fsd_strdup( arg ); |
---|
754 | fsd_log_debug(("self->destination_queue = %s", self->destination_queue)); |
---|
755 | break; |
---|
756 | case 'r' : |
---|
757 | pbs_attr->set_attr( pbs_attr, "Rerunable" , arg ); |
---|
758 | break; |
---|
759 | case 'S' : |
---|
760 | pbs_attr->set_attr( pbs_attr, "Shell_Path_List" , arg ); |
---|
761 | break; |
---|
762 | case 'u' : |
---|
763 | pbs_attr->set_attr( pbs_attr, "User_List" , arg ); |
---|
764 | break; |
---|
765 | case 'v' : |
---|
766 | pbs_attr->set_attr( pbs_attr, "Variable_List" , arg ); |
---|
767 | break; |
---|
768 | case 'M' : |
---|
769 | pbs_attr->set_attr( pbs_attr, "Mail_Users" , arg ); |
---|
770 | break; |
---|
771 | case 'l' : |
---|
772 | parse_resources( self, pbs_attr, arg); |
---|
773 | break; |
---|
774 | default : |
---|
775 | |
---|
776 | fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE, |
---|
777 | "Invalid native specification: %s (Unsupported option: -%c)", |
---|
778 | native_specification, opt); |
---|
779 | } |
---|
780 | opt = 0; |
---|
781 | } |
---|
782 | } |
---|
783 | |
---|
784 | if (opt) /* option without optarg */ |
---|
785 | fsd_exc_raise_fmt(FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE, |
---|
786 | "Invalid native specification: %s", |
---|
787 | native_specification); |
---|
788 | |
---|
789 | } |
---|
790 | FINALLY |
---|
791 | { |
---|
792 | #ifndef PBS_PROFESSIONAL |
---|
793 | pbs_attr->set_attr( pbs_attr, "submit_args", native_specification); |
---|
794 | #endif |
---|
795 | args_list->destroy(args_list); |
---|
796 | fsd_free((char *)native_spec_copy); |
---|
797 | } |
---|
798 | END_TRY |
---|
799 | } |
---|
800 | } |
---|
801 | |
---|