1 // Copyright (C) 2006-2024 CEA, EDF
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_Wrapper.hxx"
23 #include "SALOME_ResourcesManager.hxx"
24 #include "SALOME_ContainerManager.hxx"
26 #include "Container.hxx"
27 #include "AutoLocker.hxx"
29 #include "YacsTrace.hxx"
31 #include "ServiceNode.hxx"
32 #include "ComponentInstance.hxx"
33 #include "SalomeContainerHelper.hxx"
34 #include "SalomeHPContainerTools.hxx"
35 #include "RuntimeSALOME.hxx"
36 #include "Exception.hxx"
42 #define getpid _getpid
45 using namespace YACS::ENGINE;
47 SalomeContainerTools::SalomeContainerTools()
49 /* Init ContainerParameters */
50 SALOME_LifeCycleCORBA::preSet(_params);
53 SalomeContainerTools::SalomeContainerTools(const SalomeContainerTools& other):_params(other._params),_propertyMap(other._propertyMap)
57 void SalomeContainerTools::clearProperties()
60 _params=Engines::ContainerParameters();
63 std::string SalomeContainerTools::getProperty(const std::string& name) const
65 std::map<std::string,std::string>::const_iterator it(_propertyMap.find(name));
66 if(it!=_propertyMap.end())
72 void SalomeContainerTools::setProperty(const std::string& name, const std::string& value)
74 //DEBUG_YACSTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
76 if (name == "container_name")
77 _params.container_name = CORBA::string_dup(value.c_str());
78 else if (name == "workingdir")
79 _params.workingdir = CORBA::string_dup(value.c_str());
80 else if (name == "nb_parallel_procs")
82 std::istringstream iss(value);
83 if (!(iss >> _params.nb_proc))
84 throw Exception("salomecontainer::setproperty : params.nb_proc value not correct : " + value);
86 else if (name == "isMPI")
90 else if (value == "false")
91 _params.isMPI = false;
93 throw Exception("SalomeContainer::setProperty : params.isMPI value not correct : " + value);
95 else if (name == "parallelLib")
96 _params.parallelLib = CORBA::string_dup(value.c_str());
99 else if (name == "name")
100 _params.resource_params.name = CORBA::string_dup(value.c_str());
101 else if (name == "hostname")
102 _params.resource_params.hostname = CORBA::string_dup(value.c_str());
103 else if (name == "OS")
104 _params.resource_params.OS = CORBA::string_dup(value.c_str());
105 else if (name == "nb_resource_procs")
107 std::istringstream iss(value);
108 if (!(iss >> _params.resource_params.nb_proc))
109 throw Exception("salomecontainer::setproperty : params.resource_params.nb_proc value not correct : " + value);
111 else if (name == "mem_mb")
113 std::istringstream iss(value);
114 if (!(iss >> _params.resource_params.mem_mb))
115 throw Exception("salomecontainer::setproperty : params.resource_params.mem_mb value not correct : " + value);
117 else if (name == "cpu_clock")
119 std::istringstream iss(value);
120 if (!(iss >> _params.resource_params.cpu_clock))
121 throw Exception("salomecontainer::setproperty : params.resource_params.cpu_clock value not correct : " + value);
123 else if (name == "nb_node")
125 std::istringstream iss(value);
126 if (!(iss >> _params.resource_params.nb_node))
127 throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
129 else if (name == "nb_proc_per_node")
131 std::istringstream iss(value);
132 if (!(iss >> _params.resource_params.nb_proc_per_node))
133 throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value);
135 else if (name == "policy")
136 _params.resource_params.policy = CORBA::string_dup(value.c_str());
137 else if (name == "component_list")
139 std::string clean_value(value);
141 // Step 1: remove blanks
142 while(clean_value.find(" ") != std::string::npos)
143 clean_value = clean_value.erase(clean_value.find(" "), 1);
145 // Step 2: get values
146 while(!clean_value.empty())
148 std::string result("");
149 std::string::size_type loc = clean_value.find(",", 0);
150 if (loc != std::string::npos)
152 result = clean_value.substr(0, loc);
153 clean_value = clean_value.erase(0, loc+1);
157 result = clean_value;
160 if (result != "," && result != "")
162 addToComponentList(result);
167 else if (name == "resource_list")
169 std::string clean_value(value);
171 // Step 1: remove blanks
172 while(clean_value.find(" ") != std::string::npos)
173 clean_value = clean_value.erase(clean_value.find(" "), 1);
175 // Step 2: get values
176 while(!clean_value.empty())
178 std::string result("");
179 std::string::size_type loc = clean_value.find(",", 0);
180 if (loc != std::string::npos)
182 result = clean_value.substr(0, loc);
183 clean_value = clean_value.erase(0, loc+1);
187 result = clean_value;
190 if (result != "," && result != "")
192 addToResourceList(result);
197 _propertyMap[name]=value;
200 void SalomeContainerTools::addToComponentList(const std::string& name)
202 // Search if name is already in the list
203 for (CORBA::ULong i = 0; i < _params.resource_params.componentList.length(); i++)
205 std::string component_name = _params.resource_params.componentList[i].in();
206 if (component_name == name)
210 CORBA::ULong lgth = _params.resource_params.componentList.length();
211 _params.resource_params.componentList.length(lgth + 1);
212 _params.resource_params.componentList[lgth] = CORBA::string_dup(name.c_str());
215 void SalomeContainerTools::addToResourceList(const std::string& name)
217 // Search if name is already in the list
218 for (CORBA::ULong i = 0; i < _params.resource_params.resList.length(); i++)
220 std::string component_name = _params.resource_params.resList[i].in();
221 if (component_name == name)
225 CORBA::ULong lgth = _params.resource_params.resList.length();
226 _params.resource_params.resList.length(lgth + 1);
227 _params.resource_params.resList[lgth] = CORBA::string_dup(name.c_str());
230 std::string SalomeContainerTools::getContainerName() const
232 return std::string(_params.container_name);
235 int SalomeContainerTools::getNumberOfCoresPerWorker() const
237 return _params.resource_params.nb_proc_per_node;
240 void SalomeContainerTools::setContainerName(const std::string& name)
242 SetContainerNameOf(_params,name);
245 std::string SalomeContainerTools::getNotNullContainerName(const Container *contPtr, const Task *askingNode, bool& isEmpty) const
248 std::string name(_params.container_name);
256 //give a almost unique name to the container : Pid_Name_Addr
257 std::ostringstream stream;
260 stream << contPtr->getName();
262 stream << contPtr->getDiscreminantStrOfThis(askingNode);
267 std::string SalomeContainerTools::getHostName() const
269 return std::string(_params.resource_params.hostname);
272 void SalomeContainerToolsBase::SetContainerNameOf(Engines::ContainerParameters& params, const std::string& name)
274 params.container_name=CORBA::string_dup(name.c_str());
277 std::map<std::string,std::string> SalomeContainerTools::getResourceProperties(const std::string& name) const
279 std::map<std::string,std::string> properties;
281 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
282 CORBA::ORB_ptr orb = runTime->getOrb();
283 if (!orb) return properties;
284 SALOME_NamingService_Wrapper namingService(orb);
285 SALOME_LifeCycleCORBA lcc(&namingService);
286 CORBA::Object_var obj = namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
287 if (CORBA::is_nil(obj))
289 Engines::ResourcesManager_var resManager = Engines::ResourcesManager::_narrow(obj);
290 if (CORBA::is_nil(resManager))
293 std::ostringstream value;
294 Engines::ResourceDefinition_var resource_definition = resManager->GetResourceDefinition(name.c_str());
295 properties["hostname"]=resource_definition->hostname.in();
296 properties["OS"]=resource_definition->OS.in();
297 value.str(""); value << resource_definition->mem_mb;
298 properties["mem_mb"]=value.str();
299 value.str(""); value << resource_definition->cpu_clock;
300 properties["cpu_clock"]=value.str();
301 value.str(""); value << resource_definition->nb_node;
302 properties["nb_node"]=value.str();
303 value.str(""); value << resource_definition->nb_proc_per_node;
304 properties["nb_proc_per_node"]=value.str();
306 properties["component_list"]="";
307 for(CORBA::ULong i=0; i < resource_definition->componentList.length(); i++)
310 properties["component_list"]=properties["component_list"]+",";
311 properties["component_list"]=properties["component_list"]+resource_definition->componentList[i].in();
318 * \param [in] compoNames
319 * \param [in,out] shutdownLevel
321 void SalomeContainerToolsBase::Start(const std::vector<std::string>& compoNames, SalomeContainerHelper *schelp, SalomeContainerToolsBase& sct, int& shutdownLevel, const Container *cont, const Task *askingNode)
323 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
324 SALOME_NamingService_Wrapper ns;
329 catch(SALOME_Exception& e)
331 throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
333 CORBA::Object_var obj(ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS));
334 Engines::ContainerManager_var contManager(Engines::ContainerManager::_narrow(obj));
337 std::string str(sct.getNotNullContainerName(cont,askingNode,isEmptyName));
338 DEBUG_YACSTRACE("SalomeContainer::start " << str <<";"<< sct.getHostName() );
340 // Finalize parameters with components found in the container
342 for(std::vector<std::string>::const_iterator iter=compoNames.begin();iter!=compoNames.end();iter++)
343 sct.addToComponentList(*iter);
345 Engines::ContainerParameters myparams(sct.getParameters());
347 std::string dftLauchMode(schelp->getDftLaunchMode());
348 myparams.mode=CORBA::string_dup(dftLauchMode.c_str());
351 //If a container_name is given try to find an already existing container in naming service
352 //If not found start a new container with the given parameters
353 if (dynamic_cast<SalomeContainerMonoHelper *>(schelp) && !isEmptyName)
355 myparams.mode=CORBA::string_dup("getorstart");
362 //sct.setContainerName(str);
363 SetContainerNameOf(myparams,str);
364 Engines::Container_var trueCont(Engines::Container::_nil());
365 if(!isEmptyName && shutdownLevel==999)
367 //Make this only the first time start is called (_shutdownLevel==999)
368 //If the container is named, first try to get an existing container
369 //If there is an existing container use it and set the shutdown level to 3
370 //If there is no existing container, try to launch a new one and set the shutdown level to 2
374 DEBUG_YACSTRACE("GiveContainer " << str << " mode " << myparams.mode);
375 trueCont=contManager->GiveContainer(myparams);
377 catch( const SALOME::SALOME_Exception& ex )
379 std::string msg="SalomeContainer::start : no existing container : ";
381 msg += ex.details.text.in();
382 DEBUG_YACSTRACE( msg );
388 if(!CORBA::is_nil(trueCont))
391 DEBUG_YACSTRACE( "container found: " << str << " " << shutdownLevel );
396 myparams.mode="start";
397 DEBUG_YACSTRACE( "container not found: " << str << " " << shutdownLevel);
402 while(CORBA::is_nil(trueCont))
406 // --- GiveContainer is used in batch mode to retreive launched containers,
407 // and is equivalent to StartContainer when not in batch.
408 DEBUG_YACSTRACE("GiveContainer " << str << " mode " << myparams.mode);
409 trueCont=contManager->GiveContainer(myparams);
411 catch( const SALOME::SALOME_Exception& ex )
413 std::string msg="SalomeContainer::start : Unable to launch container in Salome : ";
415 msg += ex.details.text.in();
416 throw Exception(msg);
418 catch(CORBA::COMM_FAILURE&)
420 //std::cerr << "SalomeContainer::start : CORBA Comm failure detected. Make another try!" << std::endl;
421 DEBUG_YACSTRACE("SalomeContainer::start :" << str << " :CORBA Comm failure detected. Make another try!");
425 std::ostringstream oss; oss << "SalomeContainer::start : Unable to launch container " << myparams.container_name << " in Salome : CORBA Comm failure detected";
426 throw Exception( oss.str() );
429 catch(CORBA::Exception&)
431 std::ostringstream oss; oss << "SalomeContainer::start : Unable to launch container " << myparams.container_name << " in Salome : Unexpected CORBA failure detected";
432 throw Exception( oss.str() );
436 if(CORBA::is_nil(trueCont))
438 std::ostringstream oss; oss << "SalomeContainer::start : Unable to launch container " << myparams.container_name << " in Salome. Check your CatalogResources.xml file";
439 throw Exception( oss.str() );
442 // TODO : thread safety!
443 schelp->setContainer(askingNode,trueCont);
445 CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName());
446 //std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl;
447 DEBUG_YACSTRACE("SalomeContainer launched : NS entry : " << containerName << " PID : " << trueCont->getPID() );
450 CORBA::Object_ptr SalomeContainerToolsBase::LoadComponent(SalomeContainerHelper *launchModeType, Container *cont, Task *askingNode)
452 DEBUG_YACSTRACE("SalomeContainer::loadComponent ");
453 const ComponentInstance *inst(askingNode?askingNode->getComponent():0);
455 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
456 if(!cont->isAlreadyStarted(askingNode))
457 cont->start(askingNode);
460 throw Exception("SalomeContainerToolsBase::LoadComponent : no instance of component in the task requesting for a load of its component !");
461 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
463 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
464 std::string compoName(inst->getCompoName());
465 Engines::Container_var container(launchModeType->getContainer(askingNode));
468 bool isLoadable(container->load_component_Library(compoName.c_str(), reason));
470 objComponent=CreateComponentInstance(cont,container,inst);
475 CORBA::Object_ptr SalomeContainerToolsBase::CreateComponentInstance(Container *cont, Engines::Container_ptr contPtr, const ComponentInstance *inst)
478 throw Exception("SalomeContainerToolsBase::CreateComponentInstance : no instance of component in the task requesting for a load of its component !");
480 std::string compoName(inst->getCompoName());
481 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
482 Proc* p(cont->getProc());
483 // prepare component instance properties
484 Engines::FieldsDict_var env(new Engines::FieldsDict);
485 std::map<std::string, std::string> properties(inst->getProperties());
488 std::map<std::string,std::string> procMap=p->getProperties();
489 properties.insert(procMap.begin(),procMap.end());
492 std::map<std::string, std::string>::const_iterator itm;
493 env->length(properties.size());
495 for(itm = properties.begin(); itm != properties.end(); ++itm, item++)
497 DEBUG_YACSTRACE("envname="<<itm->first<<" envvalue="<< itm->second);
498 env[item].key= CORBA::string_dup(itm->first.c_str());
499 env[item].value <<= itm->second.c_str();
502 objComponent=contPtr->create_component_instance_env(compoName.c_str(), env, reason);
503 if(CORBA::is_nil(objComponent))
505 std::string text="Error while trying to create a new component: component '"+ compoName;
506 text=text+"' is not installed or it's a wrong name";
509 CORBA::string_free(reason);
510 throw Exception(text);
515 std::string SalomeContainerToolsBase::GetPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
517 if(cont->isAlreadyStarted(askingNode))
519 Engines::Container_var container(launchModeType->getContainer(askingNode));
520 const char *what="/";
521 CORBA::String_var corbaStr(container->name());
522 std::string ret(corbaStr);
525 std::string::size_type i=ret.find_first_of(what,0);
526 i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
527 if(i!=std::string::npos)
528 return ret.substr(i+1);
532 return "Not placed yet !!!";
535 std::string SalomeContainerToolsBase::GetFullPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
537 if(cont->isAlreadyStarted(askingNode))
539 Engines::Container_var container(launchModeType->getContainer(askingNode));
542 CORBA::String_var corbaStr(container->name());
543 std::string ret(corbaStr);
548 return "Unknown_placement";
552 return "Not_placed_yet";
555 Engines::ContainerParameters SalomeContainerToolsDecorator::getParameters() const
557 Engines::ContainerParameters ret(_decorated->getParameters());
558 std::string st(ret.resource_params.hostname);
561 int nbProcPerNode(this->_nb_cores_per_worker);
562 std::size_t iPos(_vh->locateTask(_node)),nPos(_vh->size());
563 if(_vh->size()!=_pg->getNumberOfWorkers(nbProcPerNode))
564 throw YACS::Exception("SalomeContainerToolsDecorator::getParameters : Internal error !");
565 std::string zeMachine(_pg->deduceMachineFrom(iPos,nbProcPerNode));
566 ret.resource_params.hostname=CORBA::string_dup(zeMachine.c_str());