Salome HOME
d93c1bccddc1075cbdec0661246c9ad12a7e166e
[tools/libbatch.git] / src / PBS / Batch_JobInfo_PBS.cxx
1 //  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * JobInfo_PBS.cxx : 
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Fri Nov 21 09:42:06 2003
28  * Projet : Salome 2
29  *
30  */
31
32 #include <cstdio>
33 #include <sstream>
34 #include <string.h>
35 #include <stdlib.h>
36
37 #include "Batch_Constants.hxx"
38 #include "Batch_Parametre.hxx"
39 #include "Batch_Environnement.hxx"
40 #include "Batch_RunTimeException.hxx"
41 #include "Batch_JobInfo_PBS.hxx"
42 using namespace std;
43
44 namespace Batch {
45
46   // Constructeurs
47   JobInfo_PBS::JobInfo_PBS(struct batch_status * list, bool tobedeleted) : JobInfo()
48   {
49     // On ne considere que le premier element de la liste
50     // Si tout est OK, la liste ne devrait contenir qu'un element
51     // Sinon on leve une exception.
52     struct batch_status * p_job = list;
53     int i;
54     for(i=0; p_job; p_job = p_job->next) i++;
55     if (i == 0) throw RunTimeException("Liste vide (le job est absent de la file)");
56     if (i > 1) {
57       ostringstream sst;
58       sst << "JobInfo_PBS::JobInfo_PBS(struct batch_status * list, bool tobedeleted) : la liste contient "
59           << i << " elements" << " (1 seul requis)" << endl;
60       throw RunTimeException(sst.str());
61     }
62     p_job = list;
63
64     // On remplit les membres _param et _env
65
66     if (p_job->name && strlen(p_job->name)) _param[ID]   = p_job->name;
67     if (p_job->text && strlen(p_job->text)) _param[TEXT] = p_job->text;
68
69     for(struct attrl * p_attr = p_job->attribs; p_attr; p_attr = p_attr->next) {
70
71       string name, res, value;
72       if (p_attr->name && strlen(p_attr->name)) name = p_attr->name;
73       if (p_attr->resource && strlen(p_attr->resource)) res = p_attr->resource;
74       if (p_attr->value && strlen(p_attr->value)) value = p_attr->value;
75
76       if (name == ATTR_N) {
77         _param[NAME] = value;
78       
79       } else if (name == ATTR_owner) {
80         _param[USER] = value;
81
82       } else if (name == ATTR_state) {
83         string status = value;
84         if (status == "C") {        // Completed
85           _param[STATE] = FINISHED;
86         } else if (status == "E") { // Exiting
87           _param[STATE] = RUNNING;
88         } else if (status == "H") { // Held
89           _param[STATE] = PAUSED;
90         } else if (status == "Q") { // Queued
91           _param[STATE] = QUEUED;
92         } else if (status == "R") { // Running
93           _param[STATE] = RUNNING;
94         } else if (status == "S") { // Suspend
95           _param[STATE] = PAUSED;
96         } else if (status == "T") { // Transiting
97           _param[STATE] = IN_PROCESS;
98         } else if (status == "W") { // Waiting
99           _param[STATE] = PAUSED;
100         } else {
101           cerr << "Unknown job state code: " << status << endl;
102         }
103
104       } else if (name == ATTR_queue) {
105         _param[QUEUE] = value;
106
107       } else if (name == ATTR_A) {
108         _param[ACCOUNT] = value;
109
110       } else if (name == ATTR_M) {
111         _param[MAIL] = value;
112
113       } else if (name == ATTR_c) {
114         if (!strcmp(value.c_str(), CHECKPOINT_UNSPECIFIED)) _param[CHECKPOINT] = 1L;
115         else _param[CHECKPOINT] = 0L;
116
117       } else if (name == ATTR_h) {
118         if (!strcmp(value.c_str(), NO_HOLD)) _param[HOLD] = 0L;
119         else _param[HOLD] = 1L;
120
121       } else if (name == ATTR_ctime) {
122         _param[CREATIONTIME] = atol(value.c_str());
123
124       } else if (name == ATTR_etime) {
125         _param[ELIGIBLETIME] = atol(value.c_str());
126
127       } else if (name == ATTR_mtime) {
128         _param[MODIFICATIONTIME] = atol(value.c_str());
129
130       } else if (name == ATTR_qtime) {
131         _param[QUEUEDTIME] = atol(value.c_str());
132
133       } else if (name == ATTR_exechost) {
134         _param[EXECUTIONHOST] = value;
135
136       } else if (name == ATTR_session) {
137         _param[PID] = atol(value.c_str());
138
139       } else if (name == ATTR_euser) {
140         _param[EUSER] = value;
141
142       } else if (name == ATTR_egroup) {
143         _param[EGROUP] = value;
144
145       } else if (name == ATTR_l) {
146         if (res == "cput") {
147           _param[MAXCPUTIME] = HMStoLong(value);
148
149         } else if (res == "walltime") {
150           _param[MAXWALLTIME] = HMStoLong(value);
151
152         }
153
154       } else if (name == ATTR_used) {
155         if (res == "cput") {
156           _param[USEDCPUTIME] = HMStoLong(value);
157
158         } else if (res == "walltime") {
159           _param[USEDWALLTIME] = HMStoLong(value);
160
161         }
162
163       } else if (name == ATTR_v) {
164         int deb = 0;
165         int pos = 0;
166         bool ok = true;
167
168         while (ok) {
169           pos = value.find(",", deb);
170           string sub = value.substr(deb, pos-deb);
171           deb = pos + 1;
172           if (pos < 0) ok = false;
173
174           int eq = sub.find("=");
175           _env[sub.substr(0, eq)] = sub.substr(eq+1);
176         }
177
178       }
179     }
180
181
182     if (tobedeleted) pbs_statfree(list);
183   }
184
185   // Destructeur
186   JobInfo_PBS::~JobInfo_PBS()
187   {
188     // Nothing to do
189   }
190   
191   // Convertit une date HH:MM:SS en secondes
192   long JobInfo_PBS::HMStoLong(const string & s)
193   {
194     long hour, min, sec;
195
196     sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
197     return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
198   }
199
200   // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
201   string JobInfo_PBS::__str__() const
202   {
203     ostringstream sst;
204     sst << "<JobInfo_PBS (" << this << ") :" << endl;
205     sst << " ID = " <<_param[ID] << endl;
206
207     sst << "  + Parametre :" << endl;
208     Parametre::const_iterator itp;
209     for(itp=_param.begin(); itp!=_param.end(); itp++) {
210       if ( (*itp).first != ID ) {
211         sst << "    * " << (*itp).first << " = " << (*itp).second << endl;
212       }
213     }
214     return sst.str();
215   }
216
217
218 }