1 // Copyright (C) 2009-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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"
26 #include <Batch/Batch_Constants.hxx>
33 _launch_date = getLaunchDate();
39 _job_file_name_complete = "";
41 _local_directory = "";
42 _result_directory = "";
43 _maximum_duration = "";
44 _maximum_duration_in_second = -1;
45 _resource_required_params.name = "";
46 _resource_required_params.hostname = "";
47 _resource_required_params.OS = "";
48 _resource_required_params.nb_proc = -1;
49 _resource_required_params.nb_node = -1;
50 _resource_required_params.nb_proc_per_node = -1;
51 _resource_required_params.cpu_clock = -1;
52 _resource_required_params.mem_mb = -1;
57 _batch_job = new Batch::Job();
63 LAUNCHER_MESSAGE("Deleting job number: " << _number);
71 Launcher::Job::stopJob()
73 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
76 if (_batch_job_id.getReference() != "undefined")
80 _batch_job_id.deleteJob();
82 catch (const Batch::EmulationException &ex)
84 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
92 Launcher::Job::removeJob()
94 LAUNCHER_MESSAGE("Removing job number: " << _number);
96 if (_batch_job_id.getReference() != "undefined")
100 _batch_job_id.deleteJob();
102 catch (const Batch::EmulationException &ex)
104 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
111 Launcher::Job::getJobType()
117 Launcher::Job::setJobName(const std::string & job_name)
119 _job_name = job_name;
123 Launcher::Job::getJobName()
129 Launcher::Job::setState(const std::string & state)
131 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
132 if (state != "CREATED" &&
133 state != "IN_PROCESS" &&
135 state != "RUNNING" &&
137 state != "FINISHED" &&
141 throw LauncherException("Bad state, this state does not exist: " + state);
147 Launcher::Job::getState()
153 Launcher::Job::setNumber(const int & number)
156 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
161 Launcher::Job::getNumber()
167 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
169 // Check machine_definition
170 std::string user_name = "";
171 if (resource_definition.UserName == "")
173 user_name = getenv("USER");
176 std::string mess = "You must define a user name: into your resource description or with env variable USER";
177 throw LauncherException(mess);
181 user_name = resource_definition.UserName;
183 _resource_definition = resource_definition;
184 _resource_definition.UserName = user_name;
188 Launcher::Job::getResourceDefinition()
190 return _resource_definition;
194 Launcher::Job::setJobFile(const std::string & job_file)
199 std::string mess = "Empty Job File is forbidden !";
200 throw LauncherException(mess);
203 _job_file = job_file;
204 std::string::size_type p1 = _job_file.find_last_of("/");
205 std::string::size_type p2 = _job_file.find_last_of(".");
206 _job_file_name_complete = _job_file.substr(p1+1);
207 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
210 add_in_file(_job_file);
214 Launcher::Job::getJobFile()
219 Launcher::Job::setEnvFile(const std::string & env_file)
221 _env_file = env_file;
223 add_in_file(_env_file);
227 Launcher::Job::getEnvFile()
233 Launcher::Job::setWorkDirectory(const std::string & work_directory)
235 _work_directory = work_directory;
239 Launcher::Job::setLocalDirectory(const std::string & local_directory)
241 _local_directory = local_directory;
245 Launcher::Job::setResultDirectory(const std::string & result_directory)
247 _result_directory = result_directory;
251 Launcher::Job::add_in_file(const std::string & file)
253 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
254 if (it == _in_files.end())
255 _in_files.push_back(file);
257 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
261 Launcher::Job::add_out_file(const std::string & file)
263 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
264 if (it == _out_files.end())
265 _out_files.push_back(file);
267 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
271 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
273 checkMaximumDuration(maximum_duration);
274 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
275 _maximum_duration = maximum_duration;
279 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
281 checkResourceRequiredParams(resource_required_params);
282 _resource_required_params = resource_required_params;
286 Launcher::Job::setQueue(const std::string & queue)
292 Launcher::Job::getWorkDirectory()
294 return _work_directory;
298 Launcher::Job::getLocalDirectory()
300 return _local_directory;
304 Launcher::Job::getResultDirectory()
306 return _result_directory;
309 const std::list<std::string> &
310 Launcher::Job::get_in_files()
315 const std::list<std::string> &
316 Launcher::Job::get_out_files()
322 Launcher::Job::getMaximumDuration()
324 return _maximum_duration;
328 Launcher::Job::getResourceRequiredParams()
330 return _resource_required_params;
334 Launcher::Job::getQueue()
340 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
342 std::string result("");
343 std::string edt_value = maximum_duration;
344 std::size_t pos = edt_value.find(":");
346 if (edt_value != "") {
347 std::string begin_edt_value = edt_value.substr(0, pos);
348 std::string mid_edt_value = edt_value.substr(pos, 1);
349 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
352 std::istringstream iss(begin_edt_value);
353 if (!(iss >> value)) {
354 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
356 else if (value < 0) {
357 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
359 std::istringstream iss_2(end_edt_value);
360 if (!(iss_2 >> value)) {
361 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
363 else if (value < 0) {
364 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
366 if (mid_edt_value != ":") {
367 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
371 throw LauncherException(result);
375 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
377 // nb_proc has be to > 0
378 if (resource_required_params.nb_proc <= 0)
380 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
381 throw LauncherException(message);
386 Launcher::Job::convertMaximumDuration(const std::string & edt)
390 if( edt.size() == 0 )
393 std::string::size_type pos = edt.find(":");
394 std::string h = edt.substr(0,pos);
395 std::string m = edt.substr(pos+1,edt.size()-pos+1);
396 std::istringstream issh(h);
398 std::istringstream issm(m);
407 Launcher::Job::getLaunchDate()
411 std::string launch_date = ctime(&rawtime);
413 for (;i < launch_date.size(); i++)
414 if (launch_date[i] == '/' ||
415 launch_date[i] == '-' ||
416 launch_date[i] == ':' ||
417 launch_date[i] == ' ')
418 launch_date[i] = '_';
419 launch_date.erase(--launch_date.end()); // Last caracter is a \n
425 Launcher::Job::updateJobState()
428 if (_state != "FINISHED" &&
433 if (_batch_job_id.getReference() != "undefined")
435 // A batch manager has been affected to the job
436 Batch::JobInfo job_info = _batch_job_id.queryJob();
437 Batch::Parametre par = job_info.getParametre();
438 _state = par[Batch::STATE].str();
439 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
448 Launcher::Job::getBatchJob()
455 Launcher::Job::common_job_params()
457 Batch::Parametre params;
459 params[Batch::NAME] = getJobName();
460 params[Batch::USER] = _resource_definition.UserName;
461 params[Batch::NBPROC] = _resource_required_params.nb_proc;
463 // Memory in megabytes
464 if (_resource_required_params.mem_mb > 0)
466 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
469 // We define a default directory based on user time
470 if (_work_directory == "")
473 Batch::Date date = Batch::Date(time(0));
474 thedate = date.str();
475 int lend = thedate.size() ;
478 if ( thedate[i] == '/' || thedate[i] == '-' || thedate[i] == ':' ) {
483 _work_directory = std::string("$HOME/Batch/");
484 _work_directory += thedate;
486 params[Batch::WORKDIR] = _work_directory;
487 params[Batch::TMPDIR] = _work_directory; // To Compatibility -- remove ??? TODO
489 // If result_directory is not defined, we use HOME environnement
490 if (_result_directory == "")
491 _result_directory = getenv("HOME");
494 for(std::list<std::string>::iterator it = _in_files.begin(); it != _in_files.end(); it++)
496 std::string file = *it;
498 // local file -> If file is not an absolute path, we apply _local_directory
499 std::string local_file;
500 if (file.substr(0, 1) == std::string("/"))
503 local_file = _local_directory + "/" + file;
505 // remote file -> get only file name from _in_files
506 size_t found = file.find_last_of("/");
507 std::string remote_file = _work_directory + "/" + file.substr(found+1);
509 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
513 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
515 std::string file = *it;
518 size_t found = file.find_last_of("/");
519 std::string local_file = _result_directory + "/" + file.substr(found+1);
521 // remote file -> If file is not an absolute path, we apply _work_directory
522 std::string remote_file;
523 if (file.substr(0, 1) == std::string("/"))
526 remote_file = _work_directory + "/" + file;
528 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
532 if (_maximum_duration_in_second != -1)
533 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
537 params[Batch::QUEUE] = _queue;
539 // Specific parameters
540 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
541 if (it != _specific_parameters.end())
542 params["LL_JOBTYPE"] = it->second;
547 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
549 _batch_job_id = batch_manager_job_id;
553 Launcher::Job::getBatchManagerJobId()
555 return _batch_job_id;
560 Launcher::Job::addToXmlDocument(xmlNodePtr root_node)
563 xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL);
564 xmlNewProp(job_node, xmlCharStrdup("type"), xmlCharStrdup(getJobType().c_str()));
565 xmlNewProp(job_node, xmlCharStrdup("name"), xmlCharStrdup(getJobName().c_str()));
568 xmlNodePtr node = xmlNewChild(job_node, NULL, xmlCharStrdup("user_part"), NULL);
570 xmlNewChild(node, NULL, xmlCharStrdup("job_file"), xmlCharStrdup(getJobFile().c_str()));
571 xmlNewChild(node, NULL, xmlCharStrdup("env_file"), xmlCharStrdup(getEnvFile().c_str()));
572 xmlNewChild(node, NULL, xmlCharStrdup("work_directory"), xmlCharStrdup(getWorkDirectory().c_str()));
573 xmlNewChild(node, NULL, xmlCharStrdup("local_directory"), xmlCharStrdup(getLocalDirectory().c_str()));
574 xmlNewChild(node, NULL, xmlCharStrdup("result_directory"), xmlCharStrdup(getResultDirectory().c_str()));
577 xmlNodePtr files_node = xmlNewChild(node, NULL, xmlCharStrdup("files"), NULL);
578 std::list<std::string> in_files = get_in_files();
579 std::list<std::string> out_files = get_out_files();
580 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
581 xmlNewChild(files_node, NULL, xmlCharStrdup("in_file"), xmlCharStrdup((*it).c_str()));
582 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
583 xmlNewChild(files_node, NULL, xmlCharStrdup("out_file"), xmlCharStrdup((*it).c_str()));
586 resourceParams resource_params = getResourceRequiredParams();
587 xmlNodePtr res_node = xmlNewChild(node, NULL, xmlCharStrdup("resource_params"), NULL);
588 xmlNewChild(res_node, NULL, xmlCharStrdup("name"), xmlCharStrdup(resource_params.name.c_str()));
589 xmlNewChild(res_node, NULL, xmlCharStrdup("hostname"), xmlCharStrdup(resource_params.hostname.c_str()));
590 xmlNewChild(res_node, NULL, xmlCharStrdup("OS"), xmlCharStrdup(resource_params.OS.c_str()));
591 std::ostringstream nb_proc_stream;
592 std::ostringstream nb_node_stream;
593 std::ostringstream nb_proc_per_node_stream;
594 std::ostringstream cpu_clock_stream;
595 std::ostringstream mem_mb_stream;
596 nb_proc_stream << resource_params.nb_proc;
597 nb_node_stream << resource_params.nb_node;
598 nb_proc_per_node_stream << resource_params.nb_proc_per_node;
599 cpu_clock_stream << resource_params.cpu_clock;
600 mem_mb_stream << resource_params.mem_mb;
601 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc"), xmlCharStrdup(nb_proc_stream.str().c_str()));
602 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_node"), xmlCharStrdup(nb_node_stream.str().c_str()));
603 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc_per_node"), xmlCharStrdup(nb_proc_per_node_stream.str().c_str()));
604 xmlNewChild(res_node, NULL, xmlCharStrdup("cpu_clock"), xmlCharStrdup(cpu_clock_stream.str().c_str()));
605 xmlNewChild(res_node, NULL, xmlCharStrdup("mem_mb"), xmlCharStrdup(mem_mb_stream.str().c_str()));
607 xmlNewChild(node, NULL, xmlCharStrdup("maximum_duration"), xmlCharStrdup(getMaximumDuration().c_str()));
608 xmlNewChild(node, NULL, xmlCharStrdup("queue"), xmlCharStrdup(getQueue().c_str()));
610 // Specific parameters part
611 xmlNodePtr specific_parameters_node = xmlNewChild(node, NULL, xmlCharStrdup("specific_parameters"), NULL);
612 std::map<std::string, std::string> specific_parameters = getSpecificParameters();
613 for(std::map<std::string, std::string>::iterator it = specific_parameters.begin(); it != specific_parameters.end(); it++)
615 xmlNodePtr specific_parameter_node = xmlNewChild(specific_parameters_node, NULL, xmlCharStrdup("specific_parameter"), NULL);
616 xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("name"), xmlCharStrdup((it->first).c_str()));
617 xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("value"), xmlCharStrdup((it->second).c_str()));
621 xmlNodePtr run_node = xmlNewChild(job_node, NULL, xmlCharStrdup("run_part"), NULL);
622 xmlNewChild(run_node, NULL, xmlCharStrdup("job_state"), xmlCharStrdup(getState().c_str()));
623 ParserResourcesType resource_definition = getResourceDefinition();
624 xmlNewChild(run_node, NULL, xmlCharStrdup("resource_choosed_name"), xmlCharStrdup(resource_definition.Name.c_str()));
627 Batch::JobId job_id = getBatchManagerJobId();
628 xmlNewChild(run_node, NULL, xmlCharStrdup("job_reference"), xmlCharStrdup(job_id.getReference().c_str()));
633 Launcher::Job::addSpecificParameter(const std::string & name,
634 const std::string & value)
636 _specific_parameters[name] = value;
639 const std::map<std::string, std::string> &
640 Launcher::Job::getSpecificParameters()
642 return _specific_parameters;
646 Launcher::Job::checkSpecificParameters()