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