1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library 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 GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 * Auteur : Ivan DUTKA-MALEN - EDF R&D
26 * Mail : mailto:ivan.dutka-malen@der.edf.fr
27 * Date : Fri Nov 14 11:00:39 2003
35 #include "Batch_Job_PBS.hxx"
40 // Ajoute un element (name,resource,value) a la liste chainee d'attributs + operateur
41 void Job_PBS::setResourceAttributeOP(struct attropl ** attr_list, const char * attr_name, const char * attr_resource, const char * attr_value)
43 // L'element (name,resource,value) existe-t-il deja ? si oui ptr != 0
44 struct attropl * ptr = findResourceAttributeOP(*attr_list, attr_name, attr_resource);
46 if (!ptr) { // L'element n'existe pas, il faut le creer
47 if (ptr = lastAttributeOP(*attr_list)) { // la liste n'est pas vide
48 ptr->next = new struct attropl;
52 } else { // la liste est completement vide
53 ptr = *attr_list = new struct attropl;
57 // On remplit les champs (name,resource,value)
58 ptr->name = new char[strlen(attr_name) + 1];
59 strncpy(ptr->name, attr_name, strlen(attr_name));
60 ptr->name[strlen(attr_name)] = 0;
62 ptr->resource = new char[strlen(attr_resource) + 1];
63 strncpy(ptr->resource, attr_resource, strlen(attr_resource));
64 ptr->resource[strlen(attr_resource)] = 0;
66 ptr->value = new char[strlen(attr_value) + 1];
67 strncpy(ptr->value, attr_value, strlen(attr_value));
68 ptr->value[strlen(attr_value)] = 0;
70 } else { // L'attribut existe, on change sa valeur
71 delete[] ptr->value; // On efface la valeur precedente
72 ptr->value = new char[strlen(attr_value) + 1];
73 strncpy(ptr->value, attr_value, strlen(attr_value));
74 ptr->value[strlen(attr_value)] = 0;
80 // Recherche un element (name,resource,value) dans la liste chainee d'attributs + operateur
81 struct attropl * Job_PBS::findResourceAttributeOP(struct attropl * attr_list, const char * attr_name, const char * attr_resource)
83 // On parcoure la liste chainee pour trouver l'element dont les champs name et resource coincident
84 struct attropl * ptr = attr_list;
86 if (!strcmp(ptr->name, attr_name) && !strcmp(ptr->resource, attr_resource)) break;
93 // Recherche le dernier element de la liste chainee d'attributs + operateur
94 struct attropl * Job_PBS::lastAttributeOP(struct attropl * attr_list)
96 struct attropl * ptr = attr_list;
97 while (ptr && ptr->next) {
104 // Convertit un objet Parametre en liste chainee d'attributs + operateur
105 struct attropl * Job_PBS::ParametreToAttributeOPList(struct attropl ** _p_attr_list, Parametre & P)
107 Parametre::iterator it;
109 for(it=P.begin(); it!=P.end(); it++) {
110 if ( (*it).first == ACCOUNT ) {
111 st_second = (*it).second.str();
112 setResourceAttributeOP(_p_attr_list, ATTR_A, "", st_second.c_str());
115 } else if ( (*it).first == CHECKPOINT ) {
116 setResourceAttributeOP(_p_attr_list, ATTR_c, "", "u");
119 } else if ( (*it).first == CKPTINTERVAL ) {
122 } else if ( (*it).first == EXECUTABLE ) {
125 } else if ( (*it).first == HOLD ) {
126 if (static_cast< long >((*it).second))
127 setResourceAttributeOP(_p_attr_list, ATTR_h, "", USER_HOLD);
129 setResourceAttributeOP(_p_attr_list, ATTR_h, "", NO_HOLD);
131 } else if ( (*it).first == INFILE ) {
132 Versatile V = (*it).second;
133 Versatile::iterator Vit;
138 for(Vit=V.begin(); Vit!=V.end(); Vit++, sep=",") {
139 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
141 string local = cp.getLocal();
142 string remote = cp.getRemote();
144 // ATTENTION : les notions de fichier "local" ou "remote" sont inverses de celle de PBS qui a un point de vue serveur et non pas utilisateur
145 stagein += sep + remote + "@" + local;
149 setResourceAttributeOP(_p_attr_list, ATTR_stagein, "", stagein.c_str());
152 } else if ( (*it).first == MAIL ) {
153 st_second = (*it).second.str();
154 setResourceAttributeOP(_p_attr_list, ATTR_M, "", st_second.c_str());
156 } else if ( (*it).first == MAXCPUTIME ) {
158 long secondes = (*it).second;
159 long heures = secondes / 3600L;
160 long minutes = (secondes - 3600L * heures) / 60L;
161 secondes = secondes % 60L;
162 sprintf(attr_value, "%02ld:%02ld:%02ld", heures, minutes, secondes);
164 setResourceAttributeOP(_p_attr_list, ATTR_l, "cput", attr_value);
167 } else if ( (*it).first == MAXDISKSIZE ) {
169 } else if ( (*it).first == MAXRAMSIZE ) {
171 } else if ( (*it).first == MAXWALLTIME ) {
173 long secondes = (*it).second;
174 long heures = secondes / 3600L;
175 long minutes = (secondes - 3600L * heures) / 60L;
176 secondes = secondes % 60L;
177 sprintf(attr_value, "%02ld:%02ld:%02ld", heures, minutes, secondes);
179 setResourceAttributeOP(_p_attr_list, ATTR_l, "walltime", attr_value);
182 } else if ( (*it).first == NAME ) {
183 st_second = (*it).second.str();
184 setResourceAttributeOP(_p_attr_list, ATTR_N, "", st_second.c_str());
187 } else if ( (*it).first == OUTFILE ) {
188 Versatile V = (*it).second;
189 Versatile::iterator Vit;
194 for(Vit=V.begin(); Vit!=V.end(); Vit++, sep=",") {
195 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
197 string local = cp.getLocal();
198 string remote = cp.getRemote();
200 if (remote == "stdout")
201 setResourceAttributeOP(_p_attr_list, ATTR_o, "", local.c_str());
203 else if (remote == "stderr")
204 setResourceAttributeOP(_p_attr_list, ATTR_e, "", local.c_str());
207 // ATTENTION : les notions de fichier "local" ou "remote" sont inverses de celle de PBS qui a un point de vue serveur et non pas utilisateur
208 stageout += sep + remote + "@" + local;
212 setResourceAttributeOP(_p_attr_list, ATTR_stageout, "", stageout.c_str());
214 } else if ( (*it).first == QUEUE ) {
217 } else if ( (*it).first == STARTTIME ) {
219 } else if ( (*it).first == TMPDIR ) {
221 } else if ( (*it).first == USER ) {
222 st_second = (*it).second.str();
223 setResourceAttributeOP(_p_attr_list, ATTR_u, "", st_second.c_str());
227 return *_p_attr_list;
231 // Convertit un objet Environnement en liste chainee d'attributs + operateur
232 struct attropl * Job_PBS::EnvironnementToAttributeOPList(struct attropl ** _p_attr_list, Environnement & E)
234 Environnement::iterator it;
235 for(it=E.begin(); it!=E.end(); it++) {
236 setResourceAttributeOP(_p_attr_list, ATTR_v, (*it).first.c_str(), ( (*it).first + "=" + (*it).second ).c_str());
238 return *_p_attr_list;
242 // Ajoute les variables d'environnement presentes dans tout job PBS
243 void Job_PBS::addPBSEnvironnement(Environnement & E)
248 if (c) E["PBS_O_HOME"] = c;
251 if (c) E["PBS_O_LANG"] = c;
253 c = getenv("LOGNAME");
254 if (c) E["PBS_O_LOGNAME"] = c;
257 if (c) E["PBS_O_PATH"] = c;
259 c = getenv("LD_LIBRARY_PATH");
260 if (c) E["PBS_O_LD_LIBRARY_PATH"] = c;
263 if (c) E["PBS_O_MAIL"] = c;
266 if (c) E["PBS_O_SHELL"] = c;
269 if (c) E["PBS_O_TZ"] = c;
271 /* Recuperation du working directory */
276 if (buf) delete[] buf;
277 buf = new char[size];
278 rc = getcwd(buf, size);
281 E["PBS_O_WORKDIR"] = buf;
286 // Ajoute un element (name,resource,value) a la liste chainee d'attributs
287 void Job_PBS::setResourceAttribute(struct attrl ** attr_list, const char * attr_name, const char * attr_resource, const char * attr_value)
289 // L'element (name,resource,value) existe-t-il deja ? si oui ptr != 0
290 struct attrl * ptr = findResourceAttribute(*attr_list, attr_name, attr_resource);
292 if (!ptr) { // L'attribut n'existe pas, il faut le creer
293 if (ptr = lastAttribute(*attr_list)) { // la liste n'est pas vide
294 ptr->next = new struct attrl;
298 } else { // la liste est completement vide
299 ptr = *attr_list = new struct attrl;
303 // On remplit les champs (name,resource,value)
304 ptr->name = new char[strlen(attr_name) + 1];
305 strncpy(ptr->name, attr_name, strlen(attr_name));
306 ptr->name[strlen(attr_name)] = 0;
308 ptr->resource = new char[strlen(attr_resource) + 1];
309 strncpy(ptr->resource, attr_resource, strlen(attr_resource));
310 ptr->resource[strlen(attr_resource)] = 0;
312 ptr->value = new char[strlen(attr_value) + 1];
313 strncpy(ptr->value, attr_value, strlen(attr_value));
314 ptr->value[strlen(attr_value)] = 0;
316 } else { // L'attribut existe, on change sa valeur
317 delete[] ptr->value; // On efface la valeur precedente
318 ptr->value = new char[strlen(attr_value) + 1];
319 strncpy(ptr->value, attr_value, strlen(attr_value));
320 ptr->value[strlen(attr_value)] = 0;
325 // Recherche un element (name,resource,value) dans la liste chainee d'attributs
326 struct attrl * Job_PBS::findResourceAttribute(struct attrl * attr_list, const char * attr_name, const char * attr_resource)
328 // On parcoure la liste chainee pour trouver l'element dont les champs name et resource coincident
329 struct attrl * ptr = attr_list;
331 if (!strcmp(ptr->name, attr_name) && !strcmp(ptr->resource, attr_resource)) break;
338 // Recherche le dernier element de la liste chainee d'attributs
339 struct attrl * Job_PBS::lastAttribute(struct attrl * attr_list)
341 struct attrl * ptr = attr_list;
342 while (ptr && ptr->next) {
350 Job_PBS::Job_PBS(const Job & job) : _p_attropl(0), _p_attrl(0), _script(0), _destination(0)
352 Parametre P = job.getParametre();
353 Parametre::iterator it;
355 // On extrait de l'objet Parametre le champ EXECUTABLE qui deviendra le script PBS
356 if ( (it=P.find(EXECUTABLE)) != P.end()) {
357 Versatile V = (*it).second;
358 string st_exe = V.str();
359 const char * exe = st_exe.c_str();
360 int lg = strlen(exe);
361 _script = new char[lg + 1];
362 for (int ii=0; ii<lg+1; ii++) _script[ii] = 0;
363 strcpy(_script, exe);
367 _script = new char[1];
371 // On extrait de l'objet Parametre le champ QUEUE qui deviendra la destination
372 if ( (it=P.find(QUEUE)) != P.end()) {
373 Versatile V = (*it).second;
374 string st_dest = V.str();
375 const char * dest = st_dest.c_str();
376 _destination = new char[strlen(dest) + 2 + 1];
378 strcat(_destination, dest);
379 _destination[strlen(dest)] = 0;
381 _destination = new char[1];
385 // On convertit les objets Parametre et Environnement en liste chainee d'attributs + operateur
386 Environnement E = job.getEnvironnement();
387 addPBSEnvironnement(E);
389 _p_attropl = ParametreToAttributeOPList(&_p_attropl, P);
390 _p_attropl = EnvironnementToAttributeOPList(&_p_attropl, E);
398 // On detruit la liste chainee d'attributs + operateur
399 struct attropl * current_p_attropl = _p_attropl;
400 while (current_p_attropl) {
401 struct attropl * next = current_p_attropl->next;
402 delete[] current_p_attropl->name;
403 delete[] current_p_attropl->resource;
404 delete[] current_p_attropl->value;
405 delete current_p_attropl;
406 current_p_attropl = next;
409 // On detruit la liste chainee d'attributs
410 struct attrl * current_p_attrl = _p_attrl;
411 while (current_p_attrl) {
412 struct attrl * next = current_p_attrl->next;
413 delete[] current_p_attrl->name;
414 delete[] current_p_attrl->resource;
415 delete[] current_p_attrl->value;
416 delete current_p_attrl;
417 current_p_attrl = next;
420 // On detruit les champs alloues
422 delete[] _destination;
427 struct attropl * Job_PBS::getAttributesOP()
433 // Cette methode sert pour les pbs_alter de PBS
434 // Pbs_alter est buggé par rapport a la specification ers_all.ps car les
435 // variables d'environnement ne sont pas supportees (ATTR_v)
436 struct attrl * Job_PBS::getAttributes()
440 // On remplit la structure attrl a partir de la strucuture attropl
441 // (elles ne different que par le parametre op, mais elles ne sont pas interchangeables
444 struct attropl * current_p_attropl = _p_attropl;
445 while (current_p_attropl) {
446 if (strcmp(current_p_attropl->name, ATTR_v)) // Bug fix for ATTR_v
447 setResourceAttribute(&_p_attrl,
448 current_p_attropl->name,
449 current_p_attropl->resource,
450 current_p_attropl->value);
452 current_p_attropl = current_p_attropl->next;
461 char * Job_PBS::getScript()
467 char * Job_PBS::getDestination()