Salome HOME
f771200d60c1bf943fa14386e44f9e1cd5f612b4
[tools/libbatch.git] / src / LSF / Batch_JobInfo_LSF.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_LSF.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>
35
36 #include "Batch_Constants.hxx"
37 #include "Batch_Parametre.hxx"
38 #include "Batch_Environnement.hxx"
39 #include "Batch_RunTimeException.hxx"
40 #include "Batch_APIInternalFailureException.hxx"
41 #include "Batch_JobInfo_LSF.hxx"
42
43 using namespace std;
44
45 namespace Batch {
46
47
48
49   // Constructeurs
50   JobInfo_LSF::JobInfo_LSF(int id) : JobInfo()
51   {
52     struct loadIndexLog * p_ld        = new struct loadIndexLog;
53     struct jobInfoHead  * p_jInfoHead = lsb_openjobinfo_a(id, NULL, NULL, NULL, NULL, ALL_JOB);
54
55     int more = p_jInfoHead->numJobs;
56     if (more != 1) {
57       char * msg = lsb_sysmsg();
58       if (!msg) msg = "unknown";
59       throw APIInternalFailureException(string("LSF lsb_openjobinfo error. Reason : ") + msg);     
60     }
61
62     // on remplit une structure contenant <more> elements
63     struct jobInfoEnt & jobInfo = * lsb_readjobinfo(&more);
64
65
66     // On remplit les membres _param et _env
67     _param[ACCOUNT]          = jobInfo.submit.projectName;
68     _param[CHECKPOINT]       = jobInfo.submit.chkpntPeriod != 0;
69     _param[CKPTINTERVAL]     = jobInfo.submit.chkpntPeriod;
70     _param[CREATIONTIME]     = jobInfo.submitTime;
71     // _param[EGROUP]           = jobInfo.;
72     _param[ELIGIBLETIME]     = jobInfo.reserveTime;
73     _param[ENDTIME]          = jobInfo.endTime;
74     _param[EUSER]            = jobInfo.execUsername;
75     _param[EXECUTABLE]       = jobInfo.submit.command;
76     _param[EXITCODE]         = jobInfo.exitStatus;
77     _param[HOLD]             = jobInfo.status & (JOB_STAT_PSUSP | JOB_STAT_SSUSP | JOB_STAT_USUSP);
78     _param[MAIL]             = jobInfo.submit.mailUser;
79     _param[MAXCPUTIME]       = jobInfo.submit.rLimits[LSF_RLIMIT_CPU];
80     _param[MAXDISKSIZE]      = jobInfo.submit.rLimits[LSF_RLIMIT_FSIZE];
81     _param[MAXRAMSIZE]       = jobInfo.submit.rLimits[LSF_RLIMIT_SWAP];
82     _param[MAXWALLTIME]      = jobInfo.submit.rLimits[LSF_RLIMIT_RUN];
83     _param[MODIFICATIONTIME] = jobInfo.lastEvent;
84     _param[NAME]             = jobInfo.jName;
85     _param[NBPROC]           = jobInfo.submit.numProcessors;
86     _param[PID]              = jobInfo.jobPid;
87     _param[QUEUE]            = jobInfo.submit.queue;
88     _param[QUEUEDTIME]       = jobInfo.submitTime;
89     // _param[SERVER]           = jobInfo.;
90     _param[STARTTIME]        = jobInfo.startTime;
91     _param[TEXT]             = jobInfo.numReasons ? lsb_pendreason(jobInfo.numReasons,
92                                                                    jobInfo.reasonTb, 
93                                                                    p_jInfoHead,
94                                                                    p_ld,0) : "";
95     // _param[TMPDIR]           = jobInfo.;
96     _param[USEDCPUTIME]      = static_cast<long>(jobInfo.cpuTime);
97     // _param[USEDDISKSIZE]     = jobInfo.;
98     _param[USEDRAMSIZE]      = jobInfo.runRusage.mem;
99     _param[USEDWALLTIME]     = jobInfo.duration * 60L;
100     _param[USER]             = jobInfo.user;
101
102
103     ostringstream oss;
104     int jobid = jobInfo.jobId;
105     oss << jobid;
106     _param[ID] = oss.str();
107
108
109     string hosts, sep;
110     for(int i=0; i < jobInfo.numExHosts; i++, sep="+") {
111       hosts += jobInfo.exHosts[i];
112       hosts += sep;
113     }
114     _param[EXECUTIONHOST]    = hosts;
115
116     ostringstream status;
117
118     if (IS_PEND(jobInfo.status))
119       status << " Job is pending;";
120     if (IS_START(jobInfo.status))
121       status << " Job is started;";
122     if (IS_FINISH(jobInfo.status))
123       status << " Job is finished;";
124     if (IS_SUSP(jobInfo.status))
125       status << " Job is suspended;";
126     if (IS_POST_DONE(jobInfo.status))
127       status << " Job is post-done;";
128     if (IS_POST_ERR(jobInfo.status))
129       status << " Job is post-error;";
130
131     // TODO: Use constants for STATE instead
132     _param[STATE] = status.str();
133     _running = IS_FINISH(jobInfo.status) ? false : true;
134
135
136     if (strlen(jobInfo.submit.inFile))
137       _param[INFILE]  += Couple(jobInfo.submit.inFile, "stdin");
138     if (strlen(jobInfo.submit.outFile))
139       _param[OUTFILE]  += Couple(jobInfo.submit.outFile, "stdout");
140     if (strlen(jobInfo.submit.errFile))
141       _param[OUTFILE]  += Couple(jobInfo.submit.errFile, "stderr");
142
143     for(int i=0; i < jobInfo.submit.nxf; i++) {
144       switch (jobInfo.submit.xf[i].options) {
145       case XF_OP_SUB2EXEC:
146         _param[INFILE]  += Couple(jobInfo.submit.xf[i].subFn, jobInfo.submit.xf[i].execFn);
147         break;
148
149       case XF_OP_EXEC2SUB:
150         _param[OUTFILE] += Couple(jobInfo.submit.xf[i].subFn, jobInfo.submit.xf[i].execFn);
151         break;
152
153       default:
154         break;
155       }
156     }
157
158
159     lsb_closejobinfo();
160     delete p_ld;
161   }
162
163
164
165     // Teste si un job est present en machine
166   bool JobInfo_LSF::isRunning() const
167   {
168     return _running;
169   }
170
171
172   // Destructeur
173   JobInfo_LSF::~JobInfo_LSF()
174   {
175     // Nothing to do
176   }
177
178
179   
180   // Convertit une date HH:MM:SS en secondes
181   long JobInfo_LSF::HMStoLong(const string & s)
182   {
183     long hour, min, sec;
184
185     sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
186     return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
187   }
188
189   // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
190   string JobInfo_LSF::__str__() const
191   {
192     ostringstream sst;
193     sst << "<JobInfo_LSF (" << this << ") :" << endl;
194     sst << " ID = " <<_param[ID] << endl;
195
196     sst << "  + Parametre :" << endl;
197     Parametre::const_iterator itp;
198     for(itp=_param.begin(); itp!=_param.end(); itp++) {
199       if ( (*itp).first != ID ) {
200         sst << "    * " << (*itp).first << " = " << (*itp).second << endl;
201       }
202     }
203     return sst.str();
204   }
205
206
207 }