1 // Copyright (C) 2006-2016 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, 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 #include "SalomeContainerTools.hxx"
21 #include "SALOME_LifeCycleCORBA.hxx"
22 #include "SALOME_NamingService.hxx"
23 #include "SALOME_ResourcesManager.hxx"
24 #include "SALOME_ContainerManager.hxx"
25 #include "Container.hxx"
26 #include "AutoLocker.hxx"
28 #include "YacsTrace.hxx"
30 #include "ServiceNode.hxx"
31 #include "ComponentInstance.hxx"
32 #include "SalomeContainerHelper.hxx"
33 #include "RuntimeSALOME.hxx"
34 #include "Exception.hxx"
40 #define getpid _getpid
43 using namespace YACS::ENGINE;
45 SalomeContainerTools::SalomeContainerTools()
47 /* Init ContainerParameters */
48 SALOME_LifeCycleCORBA::preSet(_params);
51 SalomeContainerTools::SalomeContainerTools(const SalomeContainerTools& other):_params(other._params),_propertyMap(other._propertyMap)
55 void SalomeContainerTools::clearProperties()
58 _params=Engines::ContainerParameters();
61 std::string SalomeContainerTools::getProperty(const std::string& name) const
63 std::map<std::string,std::string>::const_iterator it(_propertyMap.find(name));
64 if(it!=_propertyMap.end())
70 void SalomeContainerTools::setProperty(const std::string& name, const std::string& value)
72 //DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
74 if (name == "container_name")
75 _params.container_name = CORBA::string_dup(value.c_str());
76 else if (name == "workingdir")
77 _params.workingdir = CORBA::string_dup(value.c_str());
78 else if (name == "nb_parallel_procs")
80 std::istringstream iss(value);
81 if (!(iss >> _params.nb_proc))
82 throw Exception("salomecontainer::setproperty : params.nb_proc value not correct : " + value);
84 else if (name == "isMPI")
88 else if (value == "false")
89 _params.isMPI = false;
91 throw Exception("SalomeContainer::setProperty : params.isMPI value not correct : " + value);
93 else if (name == "parallelLib")
94 _params.parallelLib = CORBA::string_dup(value.c_str());
97 else if (name == "name")
98 _params.resource_params.name = CORBA::string_dup(value.c_str());
99 else if (name == "hostname")
100 _params.resource_params.hostname = CORBA::string_dup(value.c_str());
101 else if (name == "OS")
102 _params.resource_params.OS = CORBA::string_dup(value.c_str());
103 else if (name == "nb_resource_procs")
105 std::istringstream iss(value);
106 if (!(iss >> _params.resource_params.nb_proc))
107 throw Exception("salomecontainer::setproperty : params.resource_params.nb_proc value not correct : " + value);
109 else if (name == "mem_mb")
111 std::istringstream iss(value);
112 if (!(iss >> _params.resource_params.mem_mb))
113 throw Exception("salomecontainer::setproperty : params.resource_params.mem_mb value not correct : " + value);
115 else if (name == "cpu_clock")
117 std::istringstream iss(value);
118 if (!(iss >> _params.resource_params.cpu_clock))
119 throw Exception("salomecontainer::setproperty : params.resource_params.cpu_clock value not correct : " + value);
121 else if (name == "nb_node")
123 std::istringstream iss(value);
124 if (!(iss >> _params.resource_params.nb_node))
125 throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
127 else if (name == "nb_proc_per_node")
129 std::istringstream iss(value);
130 if (!(iss >> _params.resource_params.nb_proc_per_node))
131 throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value);
133 else if (name == "policy")
134 _params.resource_params.policy = CORBA::string_dup(value.c_str());
135 else if (name == "component_list")
137 std::string clean_value(value);
139 // Step 1: remove blanks
140 while(clean_value.find(" ") != std::string::npos)
141 clean_value = clean_value.erase(clean_value.find(" "), 1);
143 // Step 2: get values
144 while(!clean_value.empty())
146 std::string result("");
147 std::string::size_type loc = clean_value.find(",", 0);
148 if (loc != std::string::npos)
150 result = clean_value.substr(0, loc);
151 clean_value = clean_value.erase(0, loc+1);
155 result = clean_value;
158 if (result != "," && result != "")
160 addToComponentList(result);
165 else if (name == "resource_list")
167 std::string clean_value(value);
169 // Step 1: remove blanks
170 while(clean_value.find(" ") != std::string::npos)
171 clean_value = clean_value.erase(clean_value.find(" "), 1);
173 // Step 2: get values
174 while(!clean_value.empty())
176 std::string result("");
177 std::string::size_type loc = clean_value.find(",", 0);
178 if (loc != std::string::npos)
180 result = clean_value.substr(0, loc);
181 clean_value = clean_value.erase(0, loc+1);
185 result = clean_value;
188 if (result != "," && result != "")
190 addToResourceList(result);
195 _propertyMap[name]=value;
198 void SalomeContainerTools::addToComponentList(const std::string& name)
200 // Search if name is already in the list
201 for (CORBA::ULong i = 0; i < _params.resource_params.componentList.length(); i++)
203 std::string component_name = _params.resource_params.componentList[i].in();
204 if (component_name == name)
208 CORBA::ULong lgth = _params.resource_params.componentList.length();
209 _params.resource_params.componentList.length(lgth + 1);
210 _params.resource_params.componentList[lgth] = CORBA::string_dup(name.c_str());
213 void SalomeContainerTools::addToResourceList(const std::string& name)
215 // Search if name is already in the list
216 for (CORBA::ULong i = 0; i < _params.resource_params.resList.length(); i++)
218 std::string component_name = _params.resource_params.resList[i].in();
219 if (component_name == name)
223 CORBA::ULong lgth = _params.resource_params.resList.length();
224 _params.resource_params.resList.length(lgth + 1);
225 _params.resource_params.resList[lgth] = CORBA::string_dup(name.c_str());
228 std::string SalomeContainerTools::getContainerName() const
230 return std::string(_params.container_name);
233 void SalomeContainerTools::setContainerName(const std::string& name)
235 SetContainerNameOf(_params,name);
238 std::string SalomeContainerTools::getNotNullContainerName(const Container *contPtr, const Task *askingNode, bool& isEmpty) const
241 std::string name(_params.container_name);
249 //give a almost unique name to the container : Pid_Name_Addr
250 std::ostringstream stream;
253 stream << contPtr->getName();
255 stream << contPtr->getDiscreminantStrOfThis(askingNode);
260 std::string SalomeContainerTools::getHostName() const
262 return std::string(_params.resource_params.hostname);
265 void SalomeContainerTools::SetContainerNameOf(Engines::ContainerParameters& params, const std::string& name)
267 params.container_name=CORBA::string_dup(name.c_str());
270 std::map<std::string,std::string> SalomeContainerTools::getResourceProperties(const std::string& name) const
272 std::map<std::string,std::string> properties;
274 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
275 CORBA::ORB_ptr orb = runTime->getOrb();
276 if (!orb) return properties;
277 SALOME_NamingService namingService(orb);
278 SALOME_LifeCycleCORBA lcc(&namingService);
279 CORBA::Object_var obj = namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
280 if (CORBA::is_nil(obj))
282 Engines::ResourcesManager_var resManager = Engines::ResourcesManager::_narrow(obj);
283 if (CORBA::is_nil(resManager))
286 std::ostringstream value;
287 Engines::ResourceDefinition_var resource_definition = resManager->GetResourceDefinition(name.c_str());
288 properties["hostname"]=resource_definition->hostname.in();
289 properties["OS"]=resource_definition->OS.in();
290 value.str(""); value << resource_definition->mem_mb;
291 properties["mem_mb"]=value.str();
292 value.str(""); value << resource_definition->cpu_clock;
293 properties["cpu_clock"]=value.str();
294 value.str(""); value << resource_definition->nb_node;
295 properties["nb_node"]=value.str();
296 value.str(""); value << resource_definition->nb_proc_per_node;
297 properties["nb_proc_per_node"]=value.str();
299 properties["component_list"]="";
300 for(CORBA::ULong i=0; i < resource_definition->componentList.length(); i++)
303 properties["component_list"]=properties["component_list"]+",";
304 properties["component_list"]=properties["component_list"]+resource_definition->componentList[i].in();
311 * \param [in] compoNames
312 * \param [in,out] shutdownLevel
314 void SalomeContainerTools::Start(const std::vector<std::string>& compoNames, SalomeContainerHelper *schelp, SalomeContainerTools& sct, int& shutdownLevel, const Container *cont, const Task *askingNode)
316 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
317 SALOME_NamingService ns;
322 catch(SALOME_Exception& e)
324 throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
326 CORBA::Object_var obj(ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS));
327 Engines::ContainerManager_var contManager(Engines::ContainerManager::_narrow(obj));
330 std::string str(sct.getNotNullContainerName(cont,askingNode,isEmptyName));
331 DEBTRACE("SalomeContainer::start " << str <<";"<< sct.getHostName() <<";"<<shutdownLevel);
333 // Finalize parameters with components found in the container
335 for(std::vector<std::string>::const_iterator iter=compoNames.begin();iter!=compoNames.end();iter++)
336 sct.addToComponentList(*iter);
338 Engines::ContainerParameters myparams(sct.getParameters());
340 std::string dftLauchMode(schelp->getDftLaunchMode());
341 myparams.mode=CORBA::string_dup(dftLauchMode.c_str());
344 //If a container_name is given try to find an already existing container in naming service
345 //If not found start a new container with the given parameters
346 if (dynamic_cast<SalomeContainerMonoHelper *>(schelp) && !isEmptyName)
348 myparams.mode=CORBA::string_dup("getorstart");
355 //sct.setContainerName(str);
356 SetContainerNameOf(myparams,str);
357 Engines::Container_var trueCont(Engines::Container::_nil());
358 if(!isEmptyName && shutdownLevel==999)
360 //Make this only the first time start is called (_shutdownLevel==999)
361 //If the container is named, first try to get an existing container
362 //If there is an existing container use it and set the shutdown level to 3
363 //If there is no existing container, try to launch a new one and set the shutdown level to 2
367 trueCont=contManager->GiveContainer(myparams);
369 catch( const SALOME::SALOME_Exception& ex )
371 std::string msg="SalomeContainer::start : no existing container : ";
373 msg += ex.details.text.in();
380 if(!CORBA::is_nil(trueCont))
383 DEBTRACE( "container found: " << str << " " << shutdownLevel );
388 myparams.mode="start";
389 DEBTRACE( "container not found: " << str << " " << shutdownLevel);
393 if(CORBA::is_nil(trueCont))
396 // --- GiveContainer is used in batch mode to retreive launched containers,
397 // and is equivalent to StartContainer when not in batch.
398 trueCont=contManager->GiveContainer(myparams);
400 catch( const SALOME::SALOME_Exception& ex )
402 std::string msg="SalomeContainer::start : Unable to launch container in Salome : ";
404 msg += ex.details.text.in();
405 throw Exception(msg);
407 catch(CORBA::COMM_FAILURE&)
409 throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
411 catch(CORBA::Exception&)
413 throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
416 if(CORBA::is_nil(trueCont))
417 throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
419 schelp->setContainer(askingNode,trueCont);
421 CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName());
422 std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl;
425 CORBA::Object_ptr SalomeContainerTools::LoadComponent(SalomeContainerHelper *launchModeType, Container *cont, Task *askingNode)
427 DEBTRACE("SalomeContainer::loadComponent ");
428 const ComponentInstance *inst(askingNode?askingNode->getComponent():0);
430 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
431 if(!cont->isAlreadyStarted(askingNode))
432 cont->start(askingNode);
435 throw Exception("SalomeContainerTools::LoadComponent : no instance of component in the task requesting for a load of its component !");
436 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
438 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
439 std::string compoName(inst->getCompoName());
440 Engines::Container_var container(launchModeType->getContainer(askingNode));
443 bool isLoadable(container->load_component_Library(compoName.c_str(), reason));
445 objComponent=CreateComponentInstance(cont,container,inst);
450 CORBA::Object_ptr SalomeContainerTools::CreateComponentInstance(Container *cont, Engines::Container_ptr contPtr, const ComponentInstance *inst)
453 throw Exception("SalomeContainerTools::CreateComponentInstance : no instance of component in the task requesting for a load of its component !");
455 std::string compoName(inst->getCompoName());
456 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
458 Proc* p(cont->getProc());
461 std::string value(p->getProperty("DefaultStudyID"));
463 studyid= atoi(value.c_str());
465 // prepare component instance properties
466 Engines::FieldsDict_var env(new Engines::FieldsDict);
467 std::map<std::string, std::string> properties(inst->getProperties());
470 std::map<std::string,std::string> procMap=p->getProperties();
471 properties.insert(procMap.begin(),procMap.end());
474 std::map<std::string, std::string>::const_iterator itm;
475 env->length(properties.size());
477 for(itm = properties.begin(); itm != properties.end(); ++itm, item++)
479 DEBTRACE("envname="<<itm->first<<" envvalue="<< itm->second);
480 env[item].key= CORBA::string_dup(itm->first.c_str());
481 env[item].value <<= itm->second.c_str();
484 objComponent=contPtr->create_component_instance_env(compoName.c_str(), studyid, env, reason);
485 if(CORBA::is_nil(objComponent))
487 std::string text="Error while trying to create a new component: component '"+ compoName;
488 text=text+"' is not installed or it's a wrong name";
491 CORBA::string_free(reason);
492 throw Exception(text);
497 std::string SalomeContainerTools::GetPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
499 if(cont->isAlreadyStarted(askingNode))
501 Engines::Container_var container(launchModeType->getContainer(askingNode));
502 const char *what="/";
503 CORBA::String_var corbaStr(container->name());
504 std::string ret(corbaStr);
507 std::string::size_type i=ret.find_first_of(what,0);
508 i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
509 if(i!=std::string::npos)
510 return ret.substr(i+1);
514 return "Not placed yet !!!";
517 std::string SalomeContainerTools::GetFullPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
519 if(cont->isAlreadyStarted(askingNode))
521 Engines::Container_var container(launchModeType->getContainer(askingNode));
524 CORBA::String_var corbaStr(container->name());
525 std::string ret(corbaStr);
530 return "Unknown_placement";
534 return "Not_placed_yet";