Salome HOME
merge from branch BR_For40_DSC tag mergeto_V4_1_0_maintainance_29may08
[modules/kernel.git] / src / Batch / Batch_JobInfo_LSF.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21  * JobInfo_LSF.cxx : 
22  *
23  * Auteur : Ivan DUTKA-MALEN - EDF R&D
24  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
25  * Date   : Fri Nov 21 09:42:06 2003
26  * Projet : Salome 2
27  *
28  */
29
30 #include <cstdio>
31 #include <sstream>
32 #include "Batch_Parametre.hxx"
33 #include "Batch_Environnement.hxx"
34 #include "Batch_RunTimeException.hxx"
35 #include "Batch_APIInternalFailureException.hxx"
36 #include "Batch_JobInfo_LSF.hxx"
37
38 namespace Batch {
39
40
41
42   // Constructeurs
43   JobInfo_LSF::JobInfo_LSF(int id) : JobInfo()
44   {
45     struct loadIndexLog * p_ld        = new struct loadIndexLog;
46     struct jobInfoHead  * p_jInfoHead = lsb_openjobinfo_a(id, NULL, NULL, NULL, NULL, ALL_JOB);
47
48     int more = p_jInfoHead->numJobs;
49     if (more != 1) {
50       char * msg = lsb_sysmsg();
51       if (!msg) msg = "unknown";
52       throw APIInternalFailureException(string("LSF lsb_openjobinfo error. Reason : ") + msg);     
53     }
54
55     // on remplit une structure contenant <more> elements
56     struct jobInfoEnt & jobInfo = * lsb_readjobinfo(&more);
57
58
59     // On remplit les membres _param et _env
60     _param[ACCOUNT]          = jobInfo.submit.projectName;
61     _param[CHECKPOINT]       = jobInfo.submit.chkpntPeriod != 0;
62     _param[CKPTINTERVAL]     = jobInfo.submit.chkpntPeriod;
63     _param[CREATIONTIME]     = jobInfo.submitTime;
64     // _param[EGROUP]           = jobInfo.;
65     _param[ELIGIBLETIME]     = jobInfo.reserveTime;
66     _param[ENDTIME]          = jobInfo.endTime;
67     _param[EUSER]            = jobInfo.execUsername;
68     _param[EXECUTABLE]       = jobInfo.submit.command;
69     _param[EXITCODE]         = jobInfo.exitStatus;
70     _param[HOLD]             = jobInfo.status & (JOB_STAT_PSUSP | JOB_STAT_SSUSP | JOB_STAT_USUSP);
71     _param[MAIL]             = jobInfo.submit.mailUser;
72     _param[MAXCPUTIME]       = jobInfo.submit.rLimits[LSF_RLIMIT_CPU];
73     _param[MAXDISKSIZE]      = jobInfo.submit.rLimits[LSF_RLIMIT_FSIZE];
74     _param[MAXRAMSIZE]       = jobInfo.submit.rLimits[LSF_RLIMIT_SWAP];
75     _param[MAXWALLTIME]      = jobInfo.submit.rLimits[LSF_RLIMIT_RUN];
76     _param[MODIFICATIONTIME] = jobInfo.lastEvent;
77     _param[NAME]             = jobInfo.jName;
78     _param[NBPROC]           = jobInfo.submit.numProcessors;
79     _param[PID]              = jobInfo.jobPid;
80     _param[QUEUE]            = jobInfo.submit.queue;
81     _param[QUEUEDTIME]       = jobInfo.submitTime;
82     // _param[SERVER]           = jobInfo.;
83     _param[STARTTIME]        = jobInfo.startTime;
84     _param[TEXT]             = jobInfo.numReasons ? lsb_pendreason(jobInfo.numReasons,
85                                                                    jobInfo.reasonTb, 
86                                                                    p_jInfoHead,
87                                                                    p_ld,0) : "";
88     // _param[TMPDIR]           = jobInfo.;
89     _param[USEDCPUTIME]      = static_cast<long>(jobInfo.cpuTime);
90     // _param[USEDDISKSIZE]     = jobInfo.;
91     _param[USEDRAMSIZE]      = jobInfo.runRusage.mem;
92     _param[USEDWALLTIME]     = jobInfo.duration * 60L;
93     _param[USER]             = jobInfo.user;
94
95
96     ostringstream oss;
97     int jobid = jobInfo.jobId;
98     oss << jobid;
99     _param[ID] = oss.str();
100
101
102     string hosts, sep;
103     for(int i=0; i < jobInfo.numExHosts; i++, sep="+") {
104       hosts += jobInfo.exHosts[i];
105       hosts += sep;
106     }
107     _param[EXECUTIONHOST]    = hosts;
108
109     ostringstream status;
110
111     if (IS_PEND(jobInfo.status))
112       status << " Job is pending;";
113     if (IS_START(jobInfo.status))
114       status << " Job is started;";
115     if (IS_FINISH(jobInfo.status))
116       status << " Job is finished;";
117     if (IS_SUSP(jobInfo.status))
118       status << " Job is suspended;";
119     if (IS_POST_DONE(jobInfo.status))
120       status << " Job is post-done;";
121     if (IS_POST_ERR(jobInfo.status))
122       status << " Job is post-error;";
123
124     _param[STATE] = status.str();
125     _running = IS_FINISH(jobInfo.status) ? false : true;
126
127
128     if (strlen(jobInfo.submit.inFile))
129       _param[INFILE]  += Couple(jobInfo.submit.inFile, "stdin");
130     if (strlen(jobInfo.submit.outFile))
131       _param[OUTFILE]  += Couple(jobInfo.submit.outFile, "stdout");
132     if (strlen(jobInfo.submit.errFile))
133       _param[OUTFILE]  += Couple(jobInfo.submit.errFile, "stderr");
134
135     for(int i=0; i < jobInfo.submit.nxf; i++) {
136       switch (jobInfo.submit.xf[i].options) {
137       case XF_OP_SUB2EXEC:
138         _param[INFILE]  += Couple(jobInfo.submit.xf[i].subFn, jobInfo.submit.xf[i].execFn);
139         break;
140
141       case XF_OP_EXEC2SUB:
142         _param[OUTFILE] += Couple(jobInfo.submit.xf[i].subFn, jobInfo.submit.xf[i].execFn);
143         break;
144
145       default:
146         break;
147       }
148     }
149
150
151     lsb_closejobinfo();
152     delete p_ld;
153   }
154
155
156
157     // Teste si un job est present en machine
158   bool JobInfo_LSF::isRunning() const
159   {
160     return _running;
161   }
162
163
164   // Destructeur
165   JobInfo_LSF::~JobInfo_LSF()
166   {
167     // Nothing to do
168   }
169
170
171   
172   // Convertit une date HH:MM:SS en secondes
173   long JobInfo_LSF::HMStoLong(const string & s)
174   {
175     long hour, min, sec;
176
177     sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
178     return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
179   }
180
181   // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
182   string JobInfo_LSF::__str__() const
183   {
184     ostringstream sst;
185     sst << "<JobInfo_LSF (" << this << ") :" << endl;
186     sst << " ID = " <<_param[ID] << endl;
187
188     sst << "  + Parametre :" << endl;
189     Parametre::const_iterator itp;
190     for(itp=_param.begin(); itp!=_param.end(); itp++) {
191       if ( (*itp).first != ID ) {
192         sst << "    * " << (*itp).first << " = " << (*itp).second << endl;
193       }
194     }
195     return sst.str();
196   }
197
198
199 }