1 // Copyright (C) 2009-2013 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 <libbatch/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;
56 // Parameters for COORM
61 _batch_job = new Batch::Job();
67 LAUNCHER_MESSAGE("Deleting job number: " << _number);
75 Launcher::Job::stopJob()
77 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
80 if (_batch_job_id.getReference() != "undefined")
84 _batch_job_id.deleteJob();
86 catch (const Batch::GenericException &ex)
88 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
96 Launcher::Job::removeJob()
98 LAUNCHER_MESSAGE("Removing job number: " << _number);
100 if (_batch_job_id.getReference() != "undefined")
104 _batch_job_id.deleteJob();
106 catch (const Batch::GenericException &ex)
108 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
115 Launcher::Job::getJobType()
121 Launcher::Job::setJobName(const std::string & job_name)
123 _job_name = job_name;
127 Launcher::Job::getJobName()
133 Launcher::Job::setState(const std::string & state)
135 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
136 if (state != "CREATED" &&
137 state != "IN_PROCESS" &&
139 state != "RUNNING" &&
141 state != "FINISHED" &&
145 throw LauncherException("Bad state, this state does not exist: " + state);
151 Launcher::Job::getState()
156 // Get names or ids of hosts assigned to the job
158 Launcher::Job::getAssignedHostnames()
160 return _assigned_hostnames;
164 Launcher::Job::setNumber(const int & number)
167 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
172 Launcher::Job::getNumber()
178 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
180 // Check machine_definition
181 std::string user_name = "";
182 if (resource_definition.UserName == "")
184 user_name = getenv("USER");
187 std::string mess = "You must define a user name: into your resource description or with env variable USER";
188 throw LauncherException(mess);
192 user_name = resource_definition.UserName;
194 _resource_definition = resource_definition;
195 _resource_definition.UserName = user_name;
199 Launcher::Job::getResourceDefinition()
201 return _resource_definition;
205 Launcher::Job::setJobFile(const std::string & job_file)
210 std::string mess = "Empty Job File is forbidden !";
211 throw LauncherException(mess);
214 _job_file = job_file;
215 std::string::size_type p1 = _job_file.find_last_of("/");
216 std::string::size_type p2 = _job_file.find_last_of(".");
217 _job_file_name_complete = _job_file.substr(p1+1);
218 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
222 Launcher::Job::getJobFile()
227 Launcher::Job::setEnvFile(const std::string & env_file)
229 _env_file = env_file;
233 Launcher::Job::getEnvFile()
239 Launcher::Job::setWorkDirectory(const std::string & work_directory)
241 _work_directory = work_directory;
245 Launcher::Job::setLocalDirectory(const std::string & local_directory)
247 _local_directory = local_directory;
251 Launcher::Job::setResultDirectory(const std::string & result_directory)
253 _result_directory = result_directory;
257 Launcher::Job::add_in_file(const std::string & file)
259 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
260 if (it == _in_files.end())
261 _in_files.push_back(file);
263 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
267 Launcher::Job::add_out_file(const std::string & file)
269 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
270 if (it == _out_files.end())
271 _out_files.push_back(file);
273 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
277 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
279 checkMaximumDuration(maximum_duration);
280 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
281 _maximum_duration = maximum_duration;
286 Launcher::Job::setLauncherFile(const std::string & launcher_file)
288 _launcher_file = launcher_file;
291 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
293 _launcher_args = launcher_args;
297 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
299 checkResourceRequiredParams(resource_required_params);
300 _resource_required_params = resource_required_params;
304 Launcher::Job::setQueue(const std::string & queue)
310 Launcher::Job::getWorkDirectory()
312 return _work_directory;
316 Launcher::Job::getLocalDirectory()
318 return _local_directory;
322 Launcher::Job::getResultDirectory()
324 return _result_directory;
327 const std::list<std::string> &
328 Launcher::Job::get_in_files()
333 const std::list<std::string> &
334 Launcher::Job::get_out_files()
340 Launcher::Job::getMaximumDuration()
342 return _maximum_duration;
347 Launcher::Job::getLauncherFile()
349 return _launcher_file;
352 Launcher::Job::getLauncherArgs()
354 return _launcher_args;
358 Launcher::Job::getResourceRequiredParams()
360 return _resource_required_params;
364 Launcher::Job::getQueue()
370 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
372 std::string result("");
373 std::string edt_value = maximum_duration;
374 std::size_t pos = edt_value.find(":");
376 if (edt_value != "") {
377 if (pos == edt_value.npos) {
378 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
380 std::string begin_edt_value = edt_value.substr(0, pos);
381 std::string mid_edt_value = edt_value.substr(pos, 1);
382 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
385 std::istringstream iss(begin_edt_value);
386 if (!(iss >> value)) {
387 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
389 else if (value < 0) {
390 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
392 std::istringstream iss_2(end_edt_value);
393 if (!(iss_2 >> value)) {
394 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
396 else if (value < 0) {
397 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
399 if (mid_edt_value != ":") {
400 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
404 throw LauncherException(result);
408 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
410 // nb_proc has be to > 0
411 if (resource_required_params.nb_proc <= 0)
413 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
414 throw LauncherException(message);
419 Launcher::Job::convertMaximumDuration(const std::string & edt)
423 if( edt.size() == 0 )
426 std::string::size_type pos = edt.find(":");
427 std::string h = edt.substr(0,pos);
428 std::string m = edt.substr(pos+1,edt.size()-pos+1);
429 std::istringstream issh(h);
431 std::istringstream issm(m);
440 Launcher::Job::getLaunchDate()
444 std::string launch_date = ctime(&rawtime);
446 for (;i < launch_date.size(); i++)
447 if (launch_date[i] == '/' ||
448 launch_date[i] == '-' ||
449 launch_date[i] == ':' ||
450 launch_date[i] == ' ')
451 launch_date[i] = '_';
452 launch_date.erase(--launch_date.end()); // Last caracter is a \n
458 Launcher::Job::updateJobState()
461 if (_state != "FINISHED" &&
466 if (_batch_job_id.getReference() != "undefined")
468 // A batch manager has been affected to the job
469 Batch::JobInfo job_info = _batch_job_id.queryJob();
470 Batch::Parametre par = job_info.getParametre();
471 _state = par[Batch::STATE].str();
472 _assigned_hostnames = par[Batch::ASSIGNEDHOSTNAMES].str();
473 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
482 Launcher::Job::getBatchJob()
489 Launcher::Job::common_job_params()
491 Batch::Parametre params;
493 params[Batch::NAME] = getJobName();
494 params[Batch::NBPROC] = _resource_required_params.nb_proc;
495 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
497 // Memory in megabytes
498 if (_resource_required_params.mem_mb > 0)
500 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
503 // We define a default directory based on user time
504 if (_work_directory == "")
506 const size_t BUFSIZE = 32;
508 time_t curtime = time(NULL);
509 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
510 _work_directory = std::string("$HOME/Batch/workdir_");
511 _work_directory += date;
513 params[Batch::WORKDIR] = _work_directory;
515 // Parameters for COORM
516 params[Batch::LAUNCHER_FILE] = _launcher_file;
517 params[Batch::LAUNCHER_ARGS] = _launcher_args;
519 // If result_directory is not defined, we use HOME environnement
520 if (_result_directory == "")
521 _result_directory = getenv("HOME");
524 std::list<std::string> in_files(_in_files);
525 in_files.push_back(_job_file);
527 in_files.push_back(_env_file);
528 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
530 std::string file = *it;
532 // local file -> If file is not an absolute path, we apply _local_directory
533 std::string local_file;
534 if (file.substr(0, 1) == std::string("/"))
538 local_file = _local_directory + "/" + file;
543 // remote file -> get only file name from in_files
544 size_t found = file.find_last_of("/");
545 std::string remote_file = _work_directory + "/" + file.substr(found+1);
547 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
551 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
553 std::string file = *it;
556 size_t found = file.find_last_of("/");
557 std::string local_file = _result_directory + "/" + file.substr(found+1);
559 // remote file -> If file is not an absolute path, we apply _work_directory
560 std::string remote_file;
561 if (file.substr(0, 1) == std::string("/"))
564 remote_file = _work_directory + "/" + file;
566 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
570 if (_maximum_duration_in_second != -1)
571 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
575 params[Batch::QUEUE] = _queue;
577 // Specific parameters
578 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
579 if (it != _specific_parameters.end())
580 params["LL_JOBTYPE"] = it->second;
585 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
587 _batch_job_id = batch_manager_job_id;
591 Launcher::Job::getBatchManagerJobId()
593 return _batch_job_id;
598 Launcher::Job::addToXmlDocument(xmlNodePtr root_node)
601 xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL);
602 xmlNewProp(job_node, xmlCharStrdup("type"), xmlCharStrdup(getJobType().c_str()));
603 xmlNewProp(job_node, xmlCharStrdup("name"), xmlCharStrdup(getJobName().c_str()));
606 xmlNodePtr node = xmlNewChild(job_node, NULL, xmlCharStrdup("user_part"), NULL);
608 xmlNewChild(node, NULL, xmlCharStrdup("job_file"), xmlCharStrdup(getJobFile().c_str()));
609 xmlNewChild(node, NULL, xmlCharStrdup("env_file"), xmlCharStrdup(getEnvFile().c_str()));
610 xmlNewChild(node, NULL, xmlCharStrdup("work_directory"), xmlCharStrdup(getWorkDirectory().c_str()));
611 xmlNewChild(node, NULL, xmlCharStrdup("local_directory"), xmlCharStrdup(getLocalDirectory().c_str()));
612 xmlNewChild(node, NULL, xmlCharStrdup("result_directory"), xmlCharStrdup(getResultDirectory().c_str()));
614 // Parameters for COORM
615 xmlNewChild(node, NULL, xmlCharStrdup("launcher_file"), xmlCharStrdup(getLauncherFile().c_str()));
618 xmlNodePtr files_node = xmlNewChild(node, NULL, xmlCharStrdup("files"), NULL);
619 std::list<std::string> in_files = get_in_files();
620 std::list<std::string> out_files = get_out_files();
621 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
622 xmlNewChild(files_node, NULL, xmlCharStrdup("in_file"), xmlCharStrdup((*it).c_str()));
623 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
624 xmlNewChild(files_node, NULL, xmlCharStrdup("out_file"), xmlCharStrdup((*it).c_str()));
627 resourceParams resource_params = getResourceRequiredParams();
628 xmlNodePtr res_node = xmlNewChild(node, NULL, xmlCharStrdup("resource_params"), NULL);
629 xmlNewChild(res_node, NULL, xmlCharStrdup("name"), xmlCharStrdup(resource_params.name.c_str()));
630 xmlNewChild(res_node, NULL, xmlCharStrdup("hostname"), xmlCharStrdup(resource_params.hostname.c_str()));
631 xmlNewChild(res_node, NULL, xmlCharStrdup("OS"), xmlCharStrdup(resource_params.OS.c_str()));
632 std::ostringstream nb_proc_stream;
633 std::ostringstream nb_node_stream;
634 std::ostringstream nb_proc_per_node_stream;
635 std::ostringstream cpu_clock_stream;
636 std::ostringstream mem_mb_stream;
637 nb_proc_stream << resource_params.nb_proc;
638 nb_node_stream << resource_params.nb_node;
639 nb_proc_per_node_stream << resource_params.nb_proc_per_node;
640 cpu_clock_stream << resource_params.cpu_clock;
641 mem_mb_stream << resource_params.mem_mb;
642 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc"), xmlCharStrdup(nb_proc_stream.str().c_str()));
643 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_node"), xmlCharStrdup(nb_node_stream.str().c_str()));
644 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc_per_node"), xmlCharStrdup(nb_proc_per_node_stream.str().c_str()));
645 xmlNewChild(res_node, NULL, xmlCharStrdup("cpu_clock"), xmlCharStrdup(cpu_clock_stream.str().c_str()));
646 xmlNewChild(res_node, NULL, xmlCharStrdup("mem_mb"), xmlCharStrdup(mem_mb_stream.str().c_str()));
648 xmlNewChild(node, NULL, xmlCharStrdup("maximum_duration"), xmlCharStrdup(getMaximumDuration().c_str()));
649 xmlNewChild(node, NULL, xmlCharStrdup("queue"), xmlCharStrdup(getQueue().c_str()));
652 xmlNewChild(node, NULL, xmlCharStrdup("launcher_args"), xmlCharStrdup(getLauncherArgs().c_str()));
654 // Specific parameters part
655 xmlNodePtr specific_parameters_node = xmlNewChild(node, NULL, xmlCharStrdup("specific_parameters"), NULL);
656 std::map<std::string, std::string> specific_parameters = getSpecificParameters();
657 for(std::map<std::string, std::string>::iterator it = specific_parameters.begin(); it != specific_parameters.end(); it++)
659 xmlNodePtr specific_parameter_node = xmlNewChild(specific_parameters_node, NULL, xmlCharStrdup("specific_parameter"), NULL);
660 xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("name"), xmlCharStrdup((it->first).c_str()));
661 xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("value"), xmlCharStrdup((it->second).c_str()));
665 xmlNodePtr run_node = xmlNewChild(job_node, NULL, xmlCharStrdup("run_part"), NULL);
666 xmlNewChild(run_node, NULL, xmlCharStrdup("job_state"), xmlCharStrdup(getState().c_str()));
667 ParserResourcesType resource_definition = getResourceDefinition();
668 xmlNewChild(run_node, NULL, xmlCharStrdup("resource_choosed_name"), xmlCharStrdup(resource_definition.Name.c_str()));
671 Batch::JobId job_id = getBatchManagerJobId();
672 xmlNewChild(run_node, NULL, xmlCharStrdup("job_reference"), xmlCharStrdup(job_id.getReference().c_str()));
677 Launcher::Job::addSpecificParameter(const std::string & name,
678 const std::string & value)
680 _specific_parameters[name] = value;
683 const std::map<std::string, std::string> &
684 Launcher::Job::getSpecificParameters()
686 return _specific_parameters;
690 Launcher::Job::checkSpecificParameters()