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;
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::GenericException &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::GenericException &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);
211 Launcher::Job::getJobFile()
216 Launcher::Job::setEnvFile(const std::string & env_file)
218 _env_file = env_file;
222 Launcher::Job::getEnvFile()
228 Launcher::Job::setWorkDirectory(const std::string & work_directory)
230 _work_directory = work_directory;
234 Launcher::Job::setLocalDirectory(const std::string & local_directory)
236 _local_directory = local_directory;
240 Launcher::Job::setResultDirectory(const std::string & result_directory)
242 _result_directory = result_directory;
246 Launcher::Job::add_in_file(const std::string & file)
248 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
249 if (it == _in_files.end())
250 _in_files.push_back(file);
252 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
256 Launcher::Job::add_out_file(const std::string & file)
258 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
259 if (it == _out_files.end())
260 _out_files.push_back(file);
262 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
266 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
268 checkMaximumDuration(maximum_duration);
269 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
270 _maximum_duration = maximum_duration;
274 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
276 checkResourceRequiredParams(resource_required_params);
277 _resource_required_params = resource_required_params;
281 Launcher::Job::setQueue(const std::string & queue)
287 Launcher::Job::getWorkDirectory()
289 return _work_directory;
293 Launcher::Job::getLocalDirectory()
295 return _local_directory;
299 Launcher::Job::getResultDirectory()
301 return _result_directory;
304 const std::list<std::string> &
305 Launcher::Job::get_in_files()
310 const std::list<std::string> &
311 Launcher::Job::get_out_files()
317 Launcher::Job::getMaximumDuration()
319 return _maximum_duration;
323 Launcher::Job::getResourceRequiredParams()
325 return _resource_required_params;
329 Launcher::Job::getQueue()
335 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
337 std::string result("");
338 std::string edt_value = maximum_duration;
339 std::size_t pos = edt_value.find(":");
341 if (edt_value != "") {
342 if (pos == edt_value.npos) {
343 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
345 std::string begin_edt_value = edt_value.substr(0, pos);
346 std::string mid_edt_value = edt_value.substr(pos, 1);
347 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
350 std::istringstream iss(begin_edt_value);
351 if (!(iss >> value)) {
352 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
354 else if (value < 0) {
355 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
357 std::istringstream iss_2(end_edt_value);
358 if (!(iss_2 >> value)) {
359 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
361 else if (value < 0) {
362 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
364 if (mid_edt_value != ":") {
365 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
369 throw LauncherException(result);
373 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
375 // nb_proc has be to > 0
376 if (resource_required_params.nb_proc <= 0)
378 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
379 throw LauncherException(message);
384 Launcher::Job::convertMaximumDuration(const std::string & edt)
388 if( edt.size() == 0 )
391 std::string::size_type pos = edt.find(":");
392 std::string h = edt.substr(0,pos);
393 std::string m = edt.substr(pos+1,edt.size()-pos+1);
394 std::istringstream issh(h);
396 std::istringstream issm(m);
405 Launcher::Job::getLaunchDate()
409 std::string launch_date = ctime(&rawtime);
411 for (;i < launch_date.size(); i++)
412 if (launch_date[i] == '/' ||
413 launch_date[i] == '-' ||
414 launch_date[i] == ':' ||
415 launch_date[i] == ' ')
416 launch_date[i] = '_';
417 launch_date.erase(--launch_date.end()); // Last caracter is a \n
423 Launcher::Job::updateJobState()
426 if (_state != "FINISHED" &&
431 if (_batch_job_id.getReference() != "undefined")
433 // A batch manager has been affected to the job
434 Batch::JobInfo job_info = _batch_job_id.queryJob();
435 Batch::Parametre par = job_info.getParametre();
436 _state = par[Batch::STATE].str();
437 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
446 Launcher::Job::getBatchJob()
453 Launcher::Job::common_job_params()
455 Batch::Parametre params;
457 params[Batch::NAME] = getJobName();
458 params[Batch::NBPROC] = _resource_required_params.nb_proc;
459 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
461 // Memory in megabytes
462 if (_resource_required_params.mem_mb > 0)
464 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
467 // We define a default directory based on user time
468 if (_work_directory == "")
470 const size_t BUFSIZE = 32;
472 time_t curtime = time(NULL);
473 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
474 _work_directory = std::string("$HOME/Batch/workdir_");
475 _work_directory += date;
477 params[Batch::WORKDIR] = _work_directory;
479 // If result_directory is not defined, we use HOME environnement
480 if (_result_directory == "")
481 _result_directory = getenv("HOME");
484 std::list<std::string> in_files(_in_files);
485 in_files.push_back(_job_file);
487 in_files.push_back(_env_file);
488 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
490 std::string file = *it;
492 // local file -> If file is not an absolute path, we apply _local_directory
493 std::string local_file;
494 if (file.substr(0, 1) == std::string("/"))
498 local_file = _local_directory + "/" + file;
503 // remote file -> get only file name from in_files
504 size_t found = file.find_last_of("/");
505 std::string remote_file = _work_directory + "/" + file.substr(found+1);
507 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
511 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
513 std::string file = *it;
516 size_t found = file.find_last_of("/");
517 std::string local_file = _result_directory + "/" + file.substr(found+1);
519 // remote file -> If file is not an absolute path, we apply _work_directory
520 std::string remote_file;
521 if (file.substr(0, 1) == std::string("/"))
524 remote_file = _work_directory + "/" + file;
526 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
530 if (_maximum_duration_in_second != -1)
531 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
535 params[Batch::QUEUE] = _queue;
537 // Specific parameters
538 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
539 if (it != _specific_parameters.end())
540 params["LL_JOBTYPE"] = it->second;
545 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
547 _batch_job_id = batch_manager_job_id;
551 Launcher::Job::getBatchManagerJobId()
553 return _batch_job_id;
558 Launcher::Job::addToXmlDocument(xmlNodePtr root_node)
561 xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL);
562 xmlNewProp(job_node, xmlCharStrdup("type"), xmlCharStrdup(getJobType().c_str()));
563 xmlNewProp(job_node, xmlCharStrdup("name"), xmlCharStrdup(getJobName().c_str()));
566 xmlNodePtr node = xmlNewChild(job_node, NULL, xmlCharStrdup("user_part"), NULL);
568 xmlNewChild(node, NULL, xmlCharStrdup("job_file"), xmlCharStrdup(getJobFile().c_str()));
569 xmlNewChild(node, NULL, xmlCharStrdup("env_file"), xmlCharStrdup(getEnvFile().c_str()));
570 xmlNewChild(node, NULL, xmlCharStrdup("work_directory"), xmlCharStrdup(getWorkDirectory().c_str()));
571 xmlNewChild(node, NULL, xmlCharStrdup("local_directory"), xmlCharStrdup(getLocalDirectory().c_str()));
572 xmlNewChild(node, NULL, xmlCharStrdup("result_directory"), xmlCharStrdup(getResultDirectory().c_str()));
575 xmlNodePtr files_node = xmlNewChild(node, NULL, xmlCharStrdup("files"), NULL);
576 std::list<std::string> in_files = get_in_files();
577 std::list<std::string> out_files = get_out_files();
578 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
579 xmlNewChild(files_node, NULL, xmlCharStrdup("in_file"), xmlCharStrdup((*it).c_str()));
580 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
581 xmlNewChild(files_node, NULL, xmlCharStrdup("out_file"), xmlCharStrdup((*it).c_str()));
584 resourceParams resource_params = getResourceRequiredParams();
585 xmlNodePtr res_node = xmlNewChild(node, NULL, xmlCharStrdup("resource_params"), NULL);
586 xmlNewChild(res_node, NULL, xmlCharStrdup("name"), xmlCharStrdup(resource_params.name.c_str()));
587 xmlNewChild(res_node, NULL, xmlCharStrdup("hostname"), xmlCharStrdup(resource_params.hostname.c_str()));
588 xmlNewChild(res_node, NULL, xmlCharStrdup("OS"), xmlCharStrdup(resource_params.OS.c_str()));
589 std::ostringstream nb_proc_stream;
590 std::ostringstream nb_node_stream;
591 std::ostringstream nb_proc_per_node_stream;
592 std::ostringstream cpu_clock_stream;
593 std::ostringstream mem_mb_stream;
594 nb_proc_stream << resource_params.nb_proc;
595 nb_node_stream << resource_params.nb_node;
596 nb_proc_per_node_stream << resource_params.nb_proc_per_node;
597 cpu_clock_stream << resource_params.cpu_clock;
598 mem_mb_stream << resource_params.mem_mb;
599 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc"), xmlCharStrdup(nb_proc_stream.str().c_str()));
600 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_node"), xmlCharStrdup(nb_node_stream.str().c_str()));
601 xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc_per_node"), xmlCharStrdup(nb_proc_per_node_stream.str().c_str()));
602 xmlNewChild(res_node, NULL, xmlCharStrdup("cpu_clock"), xmlCharStrdup(cpu_clock_stream.str().c_str()));
603 xmlNewChild(res_node, NULL, xmlCharStrdup("mem_mb"), xmlCharStrdup(mem_mb_stream.str().c_str()));
605 xmlNewChild(node, NULL, xmlCharStrdup("maximum_duration"), xmlCharStrdup(getMaximumDuration().c_str()));
606 xmlNewChild(node, NULL, xmlCharStrdup("queue"), xmlCharStrdup(getQueue().c_str()));
608 // Specific parameters part
609 xmlNodePtr specific_parameters_node = xmlNewChild(node, NULL, xmlCharStrdup("specific_parameters"), NULL);
610 std::map<std::string, std::string> specific_parameters = getSpecificParameters();
611 for(std::map<std::string, std::string>::iterator it = specific_parameters.begin(); it != specific_parameters.end(); it++)
613 xmlNodePtr specific_parameter_node = xmlNewChild(specific_parameters_node, NULL, xmlCharStrdup("specific_parameter"), NULL);
614 xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("name"), xmlCharStrdup((it->first).c_str()));
615 xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("value"), xmlCharStrdup((it->second).c_str()));
619 xmlNodePtr run_node = xmlNewChild(job_node, NULL, xmlCharStrdup("run_part"), NULL);
620 xmlNewChild(run_node, NULL, xmlCharStrdup("job_state"), xmlCharStrdup(getState().c_str()));
621 ParserResourcesType resource_definition = getResourceDefinition();
622 xmlNewChild(run_node, NULL, xmlCharStrdup("resource_choosed_name"), xmlCharStrdup(resource_definition.Name.c_str()));
625 Batch::JobId job_id = getBatchManagerJobId();
626 xmlNewChild(run_node, NULL, xmlCharStrdup("job_reference"), xmlCharStrdup(job_id.getReference().c_str()));
631 Launcher::Job::addSpecificParameter(const std::string & name,
632 const std::string & value)
634 _specific_parameters[name] = value;
637 const std::map<std::string, std::string> &
638 Launcher::Job::getSpecificParameters()
640 return _specific_parameters;
644 Launcher::Job::checkSpecificParameters()