1 // Copyright (C) 2009-2014 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, or (at your option) any later version.
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>
35 _launch_date = getLaunchDate();
41 _job_file_name_complete = "";
43 _local_directory = "";
44 _result_directory = "";
45 _maximum_duration = "";
46 _maximum_duration_in_second = -1;
47 _resource_required_params.name = "";
48 _resource_required_params.hostname = "";
49 _resource_required_params.OS = "";
50 _resource_required_params.nb_proc = -1;
51 _resource_required_params.nb_node = -1;
52 _resource_required_params.nb_proc_per_node = -1;
53 _resource_required_params.cpu_clock = -1;
54 _resource_required_params.mem_mb = -1;
60 // Parameters for COORM
65 _batch_job = new Batch::Job();
71 LAUNCHER_MESSAGE("Deleting job number: " << _number);
79 Launcher::Job::stopJob()
81 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
84 if (_batch_job_id.getReference() != "undefined")
88 _batch_job_id.deleteJob();
90 catch (const Batch::GenericException &ex)
92 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
100 Launcher::Job::removeJob()
102 LAUNCHER_MESSAGE("Removing job number: " << _number);
104 if (_batch_job_id.getReference() != "undefined")
108 _batch_job_id.deleteJob();
110 catch (const Batch::GenericException &ex)
112 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
119 Launcher::Job::getJobType() const
125 Launcher::Job::setJobName(const std::string & job_name)
127 _job_name = job_name;
131 Launcher::Job::getJobName() const
137 Launcher::Job::setState(const std::string & state)
139 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
140 if (state != "CREATED" &&
141 state != "IN_PROCESS" &&
143 state != "RUNNING" &&
145 state != "FINISHED" &&
149 throw LauncherException("Bad state, this state does not exist: " + state);
155 Launcher::Job::getState() const
160 // Get names or ids of hosts assigned to the job
162 Launcher::Job::getAssignedHostnames()
164 return _assigned_hostnames;
168 Launcher::Job::setNumber(const int & number)
171 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
176 Launcher::Job::getNumber()
182 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
184 // Check machine_definition
185 std::string user_name = "";
186 if (resource_definition.UserName == "")
188 user_name = getenv("USER");
191 std::string mess = "You must define a user name: into your resource description or with env variable USER";
192 throw LauncherException(mess);
196 user_name = resource_definition.UserName;
198 _resource_definition = resource_definition;
199 _resource_definition.UserName = user_name;
203 Launcher::Job::getResourceDefinition() const
205 return _resource_definition;
209 Launcher::Job::setJobFile(const std::string & job_file)
214 std::string mess = "Empty Job File is forbidden !";
215 throw LauncherException(mess);
218 _job_file = job_file;
219 std::string::size_type p1 = _job_file.find_last_of("/");
220 std::string::size_type p2 = _job_file.find_last_of(".");
221 _job_file_name_complete = _job_file.substr(p1+1);
222 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
226 Launcher::Job::getJobFile() const
231 Launcher::Job::setEnvFile(const std::string & env_file)
233 _env_file = env_file;
237 Launcher::Job::getEnvFile() const
243 Launcher::Job::setWorkDirectory(const std::string & work_directory)
245 _work_directory = work_directory;
249 Launcher::Job::setLocalDirectory(const std::string & local_directory)
251 _local_directory = local_directory;
255 Launcher::Job::setResultDirectory(const std::string & result_directory)
257 _result_directory = result_directory;
261 Launcher::Job::add_in_file(const std::string & file)
263 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
264 if (it == _in_files.end())
265 _in_files.push_back(file);
267 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
271 Launcher::Job::add_out_file(const std::string & file)
273 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
274 if (it == _out_files.end())
275 _out_files.push_back(file);
277 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
281 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
283 checkMaximumDuration(maximum_duration);
284 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
285 _maximum_duration = maximum_duration;
290 Launcher::Job::setLauncherFile(const std::string & launcher_file)
292 _launcher_file = launcher_file;
295 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
297 _launcher_args = launcher_args;
301 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
303 checkResourceRequiredParams(resource_required_params);
304 _resource_required_params = resource_required_params;
308 Launcher::Job::setQueue(const std::string & queue)
314 Launcher::Job::setExclusive(bool exclusive)
316 _exclusive = exclusive;
320 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
322 if (exclusiveStr == "true")
324 else if (exclusiveStr == "false")
327 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
331 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
333 _mem_per_cpu = mem_per_cpu;
337 Launcher::Job::setReference(const std::string & reference)
339 _reference = reference;
343 Launcher::Job::getWorkDirectory() const
345 return _work_directory;
349 Launcher::Job::getLocalDirectory() const
351 return _local_directory;
355 Launcher::Job::getResultDirectory() const
357 return _result_directory;
360 const std::list<std::string> &
361 Launcher::Job::get_in_files() const
366 const std::list<std::string> &
367 Launcher::Job::get_out_files() const
373 Launcher::Job::getMaximumDuration() const
375 return _maximum_duration;
380 Launcher::Job::getLauncherFile() const
382 return _launcher_file;
385 Launcher::Job::getLauncherArgs() const
387 return _launcher_args;
391 Launcher::Job::getResourceRequiredParams() const
393 return _resource_required_params;
397 Launcher::Job::getQueue() const
403 Launcher::Job::getExclusive() const
409 Launcher::Job::getExclusiveStr() const
411 return _exclusive ? "true" : "false";
415 Launcher::Job::getMemPerCpu() const
421 Launcher::Job::getReference() const
427 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
429 std::string result("");
430 std::string edt_value = maximum_duration;
431 std::size_t pos = edt_value.find(":");
433 if (edt_value != "") {
434 if (pos == edt_value.npos) {
435 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
437 std::string begin_edt_value = edt_value.substr(0, pos);
438 std::string mid_edt_value = edt_value.substr(pos, 1);
439 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
442 std::istringstream iss(begin_edt_value);
443 if (!(iss >> value)) {
444 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
446 else if (value < 0) {
447 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
449 std::istringstream iss_2(end_edt_value);
450 if (!(iss_2 >> value)) {
451 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
453 else if (value < 0) {
454 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
456 if (mid_edt_value != ":") {
457 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
461 throw LauncherException(result);
465 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
467 // nb_proc has be to > 0
468 if (resource_required_params.nb_proc <= 0)
470 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
471 throw LauncherException(message);
476 Launcher::Job::convertMaximumDuration(const std::string & edt)
480 if( edt.size() == 0 )
483 std::string::size_type pos = edt.find(":");
484 std::string h = edt.substr(0,pos);
485 std::string m = edt.substr(pos+1,edt.size()-pos+1);
486 std::istringstream issh(h);
488 std::istringstream issm(m);
497 Launcher::Job::getLaunchDate() const
501 std::string launch_date = ctime(&rawtime);
503 for (;i < launch_date.size(); i++)
504 if (launch_date[i] == '/' ||
505 launch_date[i] == '-' ||
506 launch_date[i] == ':' ||
507 launch_date[i] == ' ')
508 launch_date[i] = '_';
509 launch_date.erase(--launch_date.end()); // Last caracter is a \n
515 Launcher::Job::updateJobState()
518 if (_state != "FINISHED" &&
523 if (_batch_job_id.getReference() != "undefined")
525 // A batch manager has been affected to the job
526 Batch::JobInfo job_info = _batch_job_id.queryJob();
527 Batch::Parametre par = job_info.getParametre();
528 _state = par[Batch::STATE].str();
529 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
530 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
531 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
540 Launcher::Job::getBatchJob()
547 Launcher::Job::common_job_params()
549 Batch::Parametre params;
551 params[Batch::NAME] = getJobName();
552 params[Batch::NBPROC] = _resource_required_params.nb_proc;
553 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
555 // Memory in megabytes
556 if (_resource_required_params.mem_mb > 0)
558 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
560 else if (_mem_per_cpu > 0)
562 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
565 // We define a default directory based on user time
566 if (_work_directory == "")
568 const size_t BUFSIZE = 32;
570 time_t curtime = time(NULL);
571 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
572 _work_directory = std::string("$HOME/Batch/workdir_");
573 _work_directory += date;
575 params[Batch::WORKDIR] = _work_directory;
577 // Parameters for COORM
578 params[Batch::LAUNCHER_FILE] = _launcher_file;
579 params[Batch::LAUNCHER_ARGS] = _launcher_args;
581 // If result_directory is not defined, we use HOME environnement
582 if (_result_directory == "")
583 _result_directory = getenv("HOME");
586 std::list<std::string> in_files(_in_files);
587 in_files.push_back(_job_file);
589 in_files.push_back(_env_file);
590 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
592 std::string file = *it;
594 // local file -> If file is not an absolute path, we apply _local_directory
595 std::string local_file;
596 if (file.substr(0, 1) == std::string("/"))
600 local_file = _local_directory + "/" + file;
605 // remote file -> get only file name from in_files
606 size_t found = file.find_last_of("/");
607 std::string remote_file = _work_directory + "/" + file.substr(found+1);
609 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
613 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
615 std::string file = *it;
618 size_t found = file.find_last_of("/");
619 std::string local_file = _result_directory + "/" + file.substr(found+1);
621 // remote file -> If file is not an absolute path, we apply _work_directory
622 std::string remote_file;
623 if (file.substr(0, 1) == std::string("/"))
626 remote_file = _work_directory + "/" + file;
628 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
632 if (_maximum_duration_in_second != -1)
633 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
637 params[Batch::QUEUE] = _queue;
641 params[Batch::EXCLUSIVE] = true;
643 // Specific parameters
644 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
645 if (it != _specific_parameters.end())
646 params["LL_JOBTYPE"] = it->second;
651 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
653 _batch_job_id = batch_manager_job_id;
657 Launcher::Job::getBatchManagerJobId() const
659 return _batch_job_id;
664 Launcher::Job::addSpecificParameter(const std::string & name,
665 const std::string & value)
667 _specific_parameters[name] = value;
670 const std::map<std::string, std::string> &
671 Launcher::Job::getSpecificParameters() const
673 return _specific_parameters;
677 Launcher::Job::checkSpecificParameters()