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 "SalomeHPContainerTools.hxx"
34 #include "RuntimeSALOME.hxx"
35 #include "Exception.hxx"
41 #define getpid _getpid
44 using namespace YACS::ENGINE;
46 SalomeContainerTools::SalomeContainerTools()
48 /* Init ContainerParameters */
49 SALOME_LifeCycleCORBA::preSet(_params);
52 SalomeContainerTools::SalomeContainerTools(const SalomeContainerTools& other):_params(other._params),_propertyMap(other._propertyMap)
56 void SalomeContainerTools::clearProperties()
59 _params=Engines::ContainerParameters();
62 std::string SalomeContainerTools::getProperty(const std::string& name) const
64 std::map<std::string,std::string>::const_iterator it(_propertyMap.find(name));
65 if(it!=_propertyMap.end())
71 void SalomeContainerTools::setProperty(const std::string& name, const std::string& value)
73 //DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
75 if (name == "container_name")
76 _params.container_name = CORBA::string_dup(value.c_str());
77 else if (name == "workingdir")
78 _params.workingdir = CORBA::string_dup(value.c_str());
79 else if (name == "nb_parallel_procs")
81 std::istringstream iss(value);
82 if (!(iss >> _params.nb_proc))
83 throw Exception("salomecontainer::setproperty : params.nb_proc value not correct : " + value);
85 else if (name == "isMPI")
89 else if (value == "false")
90 _params.isMPI = false;
92 throw Exception("SalomeContainer::setProperty : params.isMPI value not correct : " + value);
94 else if (name == "parallelLib")
95 _params.parallelLib = CORBA::string_dup(value.c_str());
98 else if (name == "name")
99 _params.resource_params.name = CORBA::string_dup(value.c_str());
100 else if (name == "hostname")
101 _params.resource_params.hostname = CORBA::string_dup(value.c_str());
102 else if (name == "OS")
103 _params.resource_params.OS = CORBA::string_dup(value.c_str());
104 else if (name == "nb_resource_procs")
106 std::istringstream iss(value);
107 if (!(iss >> _params.resource_params.nb_proc))
108 throw Exception("salomecontainer::setproperty : params.resource_params.nb_proc value not correct : " + value);
110 else if (name == "mem_mb")
112 std::istringstream iss(value);
113 if (!(iss >> _params.resource_params.mem_mb))
114 throw Exception("salomecontainer::setproperty : params.resource_params.mem_mb value not correct : " + value);
116 else if (name == "cpu_clock")
118 std::istringstream iss(value);
119 if (!(iss >> _params.resource_params.cpu_clock))
120 throw Exception("salomecontainer::setproperty : params.resource_params.cpu_clock value not correct : " + value);
122 else if (name == "nb_node")
124 std::istringstream iss(value);
125 if (!(iss >> _params.resource_params.nb_node))
126 throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
128 else if (name == "nb_proc_per_node")
130 std::istringstream iss(value);
131 if (!(iss >> _params.resource_params.nb_proc_per_node))
132 throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value);
134 else if (name == "policy")
135 _params.resource_params.policy = CORBA::string_dup(value.c_str());
136 else if (name == "component_list")
138 std::string clean_value(value);
140 // Step 1: remove blanks
141 while(clean_value.find(" ") != std::string::npos)
142 clean_value = clean_value.erase(clean_value.find(" "), 1);
144 // Step 2: get values
145 while(!clean_value.empty())
147 std::string result("");
148 std::string::size_type loc = clean_value.find(",", 0);
149 if (loc != std::string::npos)
151 result = clean_value.substr(0, loc);
152 clean_value = clean_value.erase(0, loc+1);
156 result = clean_value;
159 if (result != "," && result != "")
161 addToComponentList(result);
166 else if (name == "resource_list")
168 std::string clean_value(value);
170 // Step 1: remove blanks
171 while(clean_value.find(" ") != std::string::npos)
172 clean_value = clean_value.erase(clean_value.find(" "), 1);
174 // Step 2: get values
175 while(!clean_value.empty())
177 std::string result("");
178 std::string::size_type loc = clean_value.find(",", 0);
179 if (loc != std::string::npos)
181 result = clean_value.substr(0, loc);
182 clean_value = clean_value.erase(0, loc+1);
186 result = clean_value;
189 if (result != "," && result != "")
191 addToResourceList(result);
196 _propertyMap[name]=value;
199 void SalomeContainerTools::addToComponentList(const std::string& name)
201 // Search if name is already in the list
202 for (CORBA::ULong i = 0; i < _params.resource_params.componentList.length(); i++)
204 std::string component_name = _params.resource_params.componentList[i].in();
205 if (component_name == name)
209 CORBA::ULong lgth = _params.resource_params.componentList.length();
210 _params.resource_params.componentList.length(lgth + 1);
211 _params.resource_params.componentList[lgth] = CORBA::string_dup(name.c_str());
214 void SalomeContainerTools::addToResourceList(const std::string& name)
216 // Search if name is already in the list
217 for (CORBA::ULong i = 0; i < _params.resource_params.resList.length(); i++)
219 std::string component_name = _params.resource_params.resList[i].in();
220 if (component_name == name)
224 CORBA::ULong lgth = _params.resource_params.resList.length();
225 _params.resource_params.resList.length(lgth + 1);
226 _params.resource_params.resList[lgth] = CORBA::string_dup(name.c_str());
229 std::string SalomeContainerTools::getContainerName() const
231 return std::string(_params.container_name);
234 int SalomeContainerTools::getNumberOfCoresPerWorker() const
236 return _params.resource_params.nb_proc_per_node;
239 void SalomeContainerTools::setContainerName(const std::string& name)
241 SetContainerNameOf(_params,name);
244 std::string SalomeContainerTools::getNotNullContainerName(const Container *contPtr, const Task *askingNode, bool& isEmpty) const
247 std::string name(_params.container_name);
255 //give a almost unique name to the container : Pid_Name_Addr
256 std::ostringstream stream;
259 stream << contPtr->getName();
261 stream << contPtr->getDiscreminantStrOfThis(askingNode);
266 std::string SalomeContainerTools::getHostName() const
268 return std::string(_params.resource_params.hostname);
271 void SalomeContainerToolsBase::SetContainerNameOf(Engines::ContainerParameters& params, const std::string& name)
273 params.container_name=CORBA::string_dup(name.c_str());
276 std::map<std::string,std::string> SalomeContainerTools::getResourceProperties(const std::string& name) const
278 std::map<std::string,std::string> properties;
280 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
281 CORBA::ORB_ptr orb = runTime->getOrb();
282 if (!orb) return properties;
283 SALOME_NamingService namingService(orb);
284 SALOME_LifeCycleCORBA lcc(&namingService);
285 CORBA::Object_var obj = namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
286 if (CORBA::is_nil(obj))
288 Engines::ResourcesManager_var resManager = Engines::ResourcesManager::_narrow(obj);
289 if (CORBA::is_nil(resManager))
292 std::ostringstream value;
293 Engines::ResourceDefinition_var resource_definition = resManager->GetResourceDefinition(name.c_str());
294 properties["hostname"]=resource_definition->hostname.in();
295 properties["OS"]=resource_definition->OS.in();
296 value.str(""); value << resource_definition->mem_mb;
297 properties["mem_mb"]=value.str();
298 value.str(""); value << resource_definition->cpu_clock;
299 properties["cpu_clock"]=value.str();
300 value.str(""); value << resource_definition->nb_node;
301 properties["nb_node"]=value.str();
302 value.str(""); value << resource_definition->nb_proc_per_node;
303 properties["nb_proc_per_node"]=value.str();
305 properties["component_list"]="";
306 for(CORBA::ULong i=0; i < resource_definition->componentList.length(); i++)
309 properties["component_list"]=properties["component_list"]+",";
310 properties["component_list"]=properties["component_list"]+resource_definition->componentList[i].in();
317 * \param [in] compoNames
318 * \param [in,out] shutdownLevel
320 void SalomeContainerToolsBase::Start(const std::vector<std::string>& compoNames, SalomeContainerHelper *schelp, SalomeContainerToolsBase& sct, int& shutdownLevel, const Container *cont, const Task *askingNode)
322 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
323 SALOME_NamingService ns;
328 catch(SALOME_Exception& e)
330 throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
332 CORBA::Object_var obj(ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS));
333 Engines::ContainerManager_var contManager(Engines::ContainerManager::_narrow(obj));
336 std::string str(sct.getNotNullContainerName(cont,askingNode,isEmptyName));
337 DEBTRACE("SalomeContainer::start " << str <<";"<< _sct.getHostName() <<";"<<_type);
339 // Finalize parameters with components found in the container
341 for(std::vector<std::string>::const_iterator iter=compoNames.begin();iter!=compoNames.end();iter++)
342 sct.addToComponentList(*iter);
344 Engines::ContainerParameters myparams(sct.getParameters());
346 std::string dftLauchMode(schelp->getDftLaunchMode());
347 myparams.mode=CORBA::string_dup(dftLauchMode.c_str());
350 //If a container_name is given try to find an already existing container in naming service
351 //If not found start a new container with the given parameters
352 if (dynamic_cast<SalomeContainerMonoHelper *>(schelp) && !isEmptyName)
354 myparams.mode=CORBA::string_dup("getorstart");
361 //sct.setContainerName(str);
362 SetContainerNameOf(myparams,str);
363 Engines::Container_var trueCont(Engines::Container::_nil());
364 if(!isEmptyName && shutdownLevel==999)
366 //Make this only the first time start is called (_shutdownLevel==999)
367 //If the container is named, first try to get an existing container
368 //If there is an existing container use it and set the shutdown level to 3
369 //If there is no existing container, try to launch a new one and set the shutdown level to 2
373 trueCont=contManager->GiveContainer(myparams);
375 catch( const SALOME::SALOME_Exception& ex )
377 std::string msg="SalomeContainer::start : no existing container : ";
379 msg += ex.details.text.in();
386 if(!CORBA::is_nil(trueCont))
389 DEBTRACE( "container found: " << str << " " << _shutdownLevel );
394 myparams.mode="start";
395 DEBTRACE( "container not found: " << str << " " << _shutdownLevel);
399 if(CORBA::is_nil(trueCont))
402 // --- GiveContainer is used in batch mode to retreive launched containers,
403 // and is equivalent to StartContainer when not in batch.
404 trueCont=contManager->GiveContainer(myparams);
406 catch( const SALOME::SALOME_Exception& ex )
408 std::string msg="SalomeContainer::start : Unable to launch container in Salome : ";
410 msg += ex.details.text.in();
411 throw Exception(msg);
413 catch(CORBA::COMM_FAILURE&)
415 throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
417 catch(CORBA::Exception&)
419 throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
422 if(CORBA::is_nil(trueCont))
423 throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
425 schelp->setContainer(askingNode,trueCont);
427 CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName());
428 std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl;
431 CORBA::Object_ptr SalomeContainerToolsBase::LoadComponent(SalomeContainerHelper *launchModeType, Container *cont, Task *askingNode)
433 DEBTRACE("SalomeContainer::loadComponent ");
434 const ComponentInstance *inst(askingNode?askingNode->getComponent():0);
436 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
437 if(!cont->isAlreadyStarted(askingNode))
438 cont->start(askingNode);
441 throw Exception("SalomeContainerToolsBase::LoadComponent : no instance of component in the task requesting for a load of its component !");
442 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
444 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
445 std::string compoName(inst->getCompoName());
446 Engines::Container_var container(launchModeType->getContainer(askingNode));
449 bool isLoadable(container->load_component_Library(compoName.c_str(), reason));
451 objComponent=CreateComponentInstance(cont,container,inst);
456 CORBA::Object_ptr SalomeContainerToolsBase::CreateComponentInstance(Container *cont, Engines::Container_ptr contPtr, const ComponentInstance *inst)
459 throw Exception("SalomeContainerToolsBase::CreateComponentInstance : no instance of component in the task requesting for a load of its component !");
461 std::string compoName(inst->getCompoName());
462 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
463 Proc* p(cont->getProc());
464 // prepare component instance properties
465 Engines::FieldsDict_var env(new Engines::FieldsDict);
466 std::map<std::string, std::string> properties(inst->getProperties());
469 std::map<std::string,std::string> procMap=p->getProperties();
470 properties.insert(procMap.begin(),procMap.end());
473 std::map<std::string, std::string>::const_iterator itm;
474 env->length(properties.size());
476 for(itm = properties.begin(); itm != properties.end(); ++itm, item++)
478 DEBTRACE("envname="<<itm->first<<" envvalue="<< itm->second);
479 env[item].key= CORBA::string_dup(itm->first.c_str());
480 env[item].value <<= itm->second.c_str();
483 objComponent=contPtr->create_component_instance_env(compoName.c_str(), env, reason);
484 if(CORBA::is_nil(objComponent))
486 std::string text="Error while trying to create a new component: component '"+ compoName;
487 text=text+"' is not installed or it's a wrong name";
490 CORBA::string_free(reason);
491 throw Exception(text);
496 std::string SalomeContainerToolsBase::GetPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
498 if(cont->isAlreadyStarted(askingNode))
500 Engines::Container_var container(launchModeType->getContainer(askingNode));
501 const char *what="/";
502 CORBA::String_var corbaStr(container->name());
503 std::string ret(corbaStr);
506 std::string::size_type i=ret.find_first_of(what,0);
507 i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
508 if(i!=std::string::npos)
509 return ret.substr(i+1);
513 return "Not placed yet !!!";
516 std::string SalomeContainerToolsBase::GetFullPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
518 if(cont->isAlreadyStarted(askingNode))
520 Engines::Container_var container(launchModeType->getContainer(askingNode));
523 CORBA::String_var corbaStr(container->name());
524 std::string ret(corbaStr);
529 return "Unknown_placement";
533 return "Not_placed_yet";
536 Engines::ContainerParameters SalomeContainerToolsDecorator::getParameters() const
538 Engines::ContainerParameters ret(_decorated->getParameters());
539 std::string st(ret.resource_params.hostname);
542 int nbProcPerNode(this->_nb_cores_per_worker);
543 std::size_t iPos(_vh->locateTask(_node)),nPos(_vh->size());
544 if(_vh->size()!=_pg->getNumberOfWorkers(nbProcPerNode))
545 throw YACS::Exception("SalomeContainerToolsDecorator::getParameters : Internal error !");
546 std::string zeMachine(_pg->deduceMachineFrom(iPos,nbProcPerNode));
547 ret.resource_params.hostname=CORBA::string_dup(zeMachine.c_str());