1 // Copyright (C) 2009-2010 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author: André RIBES - EDF R&D
22 #include "Launcher_Job.hxx"
23 #include "Launcher.hxx"
29 _launch_date = getLaunchDate();
35 _job_file_name_complete = "";
37 _local_directory = "";
38 _result_directory = "";
39 _maximum_duration = "";
40 _maximum_duration_in_second = -1;
41 _resource_required_params.name = "";
42 _resource_required_params.hostname = "";
43 _resource_required_params.OS = "";
44 _resource_required_params.nb_proc = -1;
45 _resource_required_params.nb_node = -1;
46 _resource_required_params.nb_proc_per_node = -1;
47 _resource_required_params.cpu_clock = -1;
48 _resource_required_params.mem_mb = -1;
53 _batch_job = new Batch::Job();
59 LAUNCHER_MESSAGE("Deleting job number: " << _number);
61 if (_batch_job_id.getReference() != "undefined")
65 _batch_job_id.deleteJob();
67 catch (const Batch::EmulationException &ex)
69 LAUNCHER_INFOS("WARNING: exception when deleting the job: " << ex.message);
78 Launcher::Job::getJobType()
84 Launcher::Job::setJobName(const std::string & job_name)
90 Launcher::Job::getJobName()
96 Launcher::Job::setState(const std::string & state)
98 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
99 if (state != "CREATED" &&
100 state != "IN_PROCESS" &&
102 state != "RUNNING" &&
104 state != "FINISHED" &&
108 throw LauncherException("Bad state, this state does not exist: " + state);
114 Launcher::Job::getState()
120 Launcher::Job::setNumber(const int & number)
123 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
128 Launcher::Job::getNumber()
134 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
136 // Check machine_definition
137 std::string user_name = "";
138 if (resource_definition.UserName == "")
140 user_name = getenv("USER");
143 std::string mess = "You must define a user name: into your resource description or with env variable USER";
144 throw LauncherException(mess);
148 user_name = resource_definition.UserName;
150 _resource_definition = resource_definition;
151 _resource_definition.UserName = user_name;
155 Launcher::Job::getResourceDefinition()
157 return _resource_definition;
161 Launcher::Job::setJobFile(const std::string & job_file)
166 std::string mess = "Empty Job File is forbidden !";
167 throw LauncherException(mess);
170 _job_file = job_file;
171 std::string::size_type p1 = _job_file.find_last_of("/");
172 std::string::size_type p2 = _job_file.find_last_of(".");
173 _job_file_name_complete = _job_file.substr(p1+1);
174 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
177 add_in_file(_job_file);
181 Launcher::Job::getJobFile()
186 Launcher::Job::setEnvFile(const std::string & env_file)
188 _env_file = env_file;
190 add_in_file(_env_file);
194 Launcher::Job::getEnvFile()
200 Launcher::Job::setWorkDirectory(const std::string & work_directory)
202 _work_directory = work_directory;
206 Launcher::Job::setLocalDirectory(const std::string & local_directory)
208 _local_directory = local_directory;
212 Launcher::Job::setResultDirectory(const std::string & result_directory)
214 _result_directory = result_directory;
218 Launcher::Job::add_in_file(const std::string & file)
220 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
221 if (it == _in_files.end())
222 _in_files.push_back(file);
224 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
228 Launcher::Job::add_out_file(const std::string & file)
230 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
231 if (it == _out_files.end())
232 _out_files.push_back(file);
234 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
238 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
240 checkMaximumDuration(maximum_duration);
241 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
242 _maximum_duration = maximum_duration;
246 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
248 checkResourceRequiredParams(resource_required_params);
249 _resource_required_params = resource_required_params;
253 Launcher::Job::setQueue(const std::string & queue)
259 Launcher::Job::getWorkDirectory()
261 return _work_directory;
265 Launcher::Job::getLocalDirectory()
267 return _local_directory;
271 Launcher::Job::getResultDirectory()
273 return _result_directory;
276 const std::list<std::string> &
277 Launcher::Job::get_in_files()
282 const std::list<std::string> &
283 Launcher::Job::get_out_files()
289 Launcher::Job::getMaximumDuration()
291 return _maximum_duration;
295 Launcher::Job::getResourceRequiredParams()
297 return _resource_required_params;
301 Launcher::Job::getQueue()
307 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
309 std::string result("");
310 std::string edt_value = maximum_duration;
311 std::size_t pos = edt_value.find(":");
313 if (edt_value != "") {
314 std::string begin_edt_value = edt_value.substr(0, pos);
315 std::string mid_edt_value = edt_value.substr(pos, 1);
316 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
319 std::istringstream iss(begin_edt_value);
320 if (!(iss >> value)) {
321 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
323 else if (value < 0) {
324 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
326 std::istringstream iss_2(end_edt_value);
327 if (!(iss_2 >> value)) {
328 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
330 else if (value < 0) {
331 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
333 if (mid_edt_value != ":") {
334 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
338 throw LauncherException(result);
342 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
344 // nb_proc has be to > 0
345 if (resource_required_params.nb_proc <= 0)
347 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
348 throw LauncherException(message);
353 Launcher::Job::convertMaximumDuration(const std::string & edt)
357 if( edt.size() == 0 )
360 std::string::size_type pos = edt.find(":");
361 std::string h = edt.substr(0,pos);
362 std::string m = edt.substr(pos+1,edt.size()-pos+1);
363 std::istringstream issh(h);
365 std::istringstream issm(m);
374 Launcher::Job::getLaunchDate()
378 std::string launch_date = ctime(&rawtime);
380 for (;i < launch_date.size(); i++)
381 if (launch_date[i] == '/' ||
382 launch_date[i] == '-' ||
383 launch_date[i] == ':' ||
384 launch_date[i] == ' ')
385 launch_date[i] = '_';
386 launch_date.erase(--launch_date.end()); // Last caracter is a \n
392 Launcher::Job::updateJobState()
395 if (_state != "FINISHED" ||
400 if (_batch_job_id.getReference() != "undefined")
402 // A batch manager has been affected to the job
403 Batch::JobInfo job_info = _batch_job_id.queryJob();
404 Batch::Parametre par = job_info.getParametre();
405 _state = par[Batch::STATE].str();
406 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
415 Launcher::Job::getBatchJob()
422 Launcher::Job::common_job_params()
424 Batch::Parametre params;
426 params[Batch::USER] = _resource_definition.UserName;
427 params[Batch::NBPROC] = _resource_required_params.nb_proc;
429 // Memory in megabytes
430 if (_resource_required_params.mem_mb > 0)
432 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
435 // We define a default directory based on user time
436 if (_work_directory == "")
439 Batch::Date date = Batch::Date(time(0));
440 thedate = date.str();
441 int lend = thedate.size() ;
444 if ( thedate[i] == '/' || thedate[i] == '-' || thedate[i] == ':' ) {
449 _work_directory = std::string("$HOME/Batch/");
450 _work_directory += thedate;
452 params[Batch::WORKDIR] = _work_directory;
453 params[Batch::TMPDIR] = _work_directory; // To Compatibility -- remove ??? TODO
455 // If result_directory is not defined, we use HOME environnement
456 if (_result_directory == "")
457 _result_directory = getenv("HOME");
460 for(std::list<std::string>::iterator it = _in_files.begin(); it != _in_files.end(); it++)
462 std::string file = *it;
464 // local file -> If file is not an absolute path, we apply _local_directory
465 std::string local_file;
466 if (file.substr(0, 1) == std::string("/"))
469 local_file = _local_directory + "/" + file;
471 // remote file -> get only file name from _in_files
472 size_t found = file.find_last_of("/");
473 std::string remote_file = _work_directory + "/" + file.substr(found+1);
475 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
479 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
481 std::string file = *it;
484 size_t found = file.find_last_of("/");
485 std::string local_file = _result_directory + "/" + file.substr(found+1);
487 // remote file -> If file is not an absolute path, we apply _work_directory
488 std::string remote_file;
489 if (file.substr(0, 1) == std::string("/"))
492 remote_file = _work_directory + "/" + file;
494 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
498 if (_maximum_duration_in_second != -1)
499 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
503 params[Batch::QUEUE] = _queue;
509 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
511 _batch_job_id = batch_manager_job_id;
515 Launcher::Job::getBatchManagerJobId()
517 return _batch_job_id;
522 Launcher::Job::addToXmlDocument(xmlNodePtr root_node)
525 xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL);
526 xmlNewProp(job_node, xmlCharStrdup("type"), xmlCharStrdup(getJobType().c_str()));
527 xmlNewProp(job_node, xmlCharStrdup("name"), xmlCharStrdup(getJobName().c_str()));
530 xmlNodePtr node = xmlNewChild(job_node, NULL, xmlCharStrdup("user_part"), NULL);
532 xmlNewChild(node, NULL, xmlCharStrdup("job_file"), xmlCharStrdup(getJobFile().c_str()));
533 xmlNewChild(node, NULL, xmlCharStrdup("env_file"), xmlCharStrdup(getEnvFile().c_str()));
534 xmlNewChild(node, NULL, xmlCharStrdup("work_directory"), xmlCharStrdup(getWorkDirectory().c_str()));
535 xmlNewChild(node, NULL, xmlCharStrdup("local_directory"), xmlCharStrdup(getLocalDirectory().c_str()));
536 xmlNewChild(node, NULL, xmlCharStrdup("result_directory"), xmlCharStrdup(getResultDirectory().c_str()));
539 xmlNodePtr files_node = xmlNewChild(node, NULL, xmlCharStrdup("files"), NULL);
540 std::list<std::string> in_files = get_in_files();
541 std::list<std::string> out_files = get_out_files();
542 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
543 xmlNewChild(files_node, NULL, xmlCharStrdup("in_file"), xmlCharStrdup((*it).c_str()));
544 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
545 xmlNewChild(files_node, NULL, xmlCharStrdup("out_file"), xmlCharStrdup((*it).c_str()));
548 resourceParams resource_params = getResourceRequiredParams();
549 xmlNodePtr res_node = xmlNewChild(node, NULL, xmlCharStrdup("resource_params"), NULL);
550 xmlNewChild(res_node, NULL, xmlCharStrdup("name"), xmlCharStrdup(resource_params.name.c_str()));
551 xmlNewChild(res_node, NULL, xmlCharStrdup("hostname"), xmlCharStrdup(resource_params.hostname.c_str()));
552 xmlNewChild(res_node, NULL, xmlCharStrdup("OS"), xmlCharStrdup(resource_params.OS.c_str()));
553 std::ostringstream nb_proc_stream;
554 std::ostringstream nb_node_stream;
555 std::ostringstream nb_proc_per_node_stream;
556 std::ostringstream cpu_clock_stream;
557 std::ostringstream mem_mb_stream;
558 nb_proc_stream << resource_params.nb_proc;
559 nb_node_stream << resource_params.nb_node;
560 nb_proc_per_node_stream << resource_params.nb_proc_per_node;
561 cpu_clock_stream << resource_params.cpu_clock;
562 mem_mb_stream << resource_params.mem_mb;
563 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc"), xmlCharStrdup(nb_proc_stream.str().c_str()));
564 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_node"), xmlCharStrdup(nb_node_stream.str().c_str()));
565 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc_per_node"), xmlCharStrdup(nb_proc_per_node_stream.str().c_str()));
566 xmlNewChild(res_node, NULL, xmlCharStrdup("cpu_clock"), xmlCharStrdup(cpu_clock_stream.str().c_str()));
567 xmlNewChild(res_node, NULL, xmlCharStrdup("mem_mb"), xmlCharStrdup(mem_mb_stream.str().c_str()));
569 xmlNewChild(node, NULL, xmlCharStrdup("maximum_duration"), xmlCharStrdup(getMaximumDuration().c_str()));
570 xmlNewChild(node, NULL, xmlCharStrdup("queue"), xmlCharStrdup(getQueue().c_str()));
573 xmlNodePtr run_node = xmlNewChild(job_node, NULL, xmlCharStrdup("run_part"), NULL);
574 xmlNewChild(run_node, NULL, xmlCharStrdup("job_state"), xmlCharStrdup(getState().c_str()));
575 ParserResourcesType resource_definition = getResourceDefinition();
576 xmlNewChild(run_node, NULL, xmlCharStrdup("resource_choosed_name"), xmlCharStrdup(resource_definition.Name.c_str()));
579 Batch::JobId job_id = getBatchManagerJobId();
580 xmlNewChild(run_node, NULL, xmlCharStrdup("job_reference"), xmlCharStrdup(job_id.getReference().c_str()));