1 // Copyright (C) 2006-2014 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"
38 using namespace YACS::ENGINE;
40 SalomeContainerTools::SalomeContainerTools()
42 /* Init ContainerParameters */
43 SALOME_LifeCycleCORBA::preSet(_params);
46 SalomeContainerTools::SalomeContainerTools(const SalomeContainerTools& other):_params(other._params),_propertyMap(other._propertyMap)
50 void SalomeContainerTools::clearProperties()
53 _params=Engines::ContainerParameters();
56 std::string SalomeContainerTools::getProperty(const std::string& name) const
58 std::map<std::string,std::string>::const_iterator it(_propertyMap.find(name));
59 if(it!=_propertyMap.end())
65 void SalomeContainerTools::setProperty(const std::string& name, const std::string& value)
67 //DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
69 if (name == "container_name")
70 _params.container_name = CORBA::string_dup(value.c_str());
71 else if (name == "workingdir")
72 _params.workingdir = CORBA::string_dup(value.c_str());
73 else if (name == "nb_parallel_procs")
75 std::istringstream iss(value);
76 if (!(iss >> _params.nb_proc))
77 throw Exception("salomecontainer::setproperty : params.nb_proc value not correct : " + value);
79 else if (name == "isMPI")
83 else if (value == "false")
84 _params.isMPI = false;
86 throw Exception("SalomeContainer::setProperty : params.isMPI value not correct : " + value);
88 else if (name == "parallelLib")
89 _params.parallelLib = CORBA::string_dup(value.c_str());
92 else if (name == "name")
93 _params.resource_params.name = CORBA::string_dup(value.c_str());
94 else if (name == "hostname")
95 _params.resource_params.hostname = CORBA::string_dup(value.c_str());
96 else if (name == "OS")
97 _params.resource_params.OS = CORBA::string_dup(value.c_str());
98 else if (name == "nb_resource_procs")
100 std::istringstream iss(value);
101 if (!(iss >> _params.resource_params.nb_proc))
102 throw Exception("salomecontainer::setproperty : params.resource_params.nb_proc value not correct : " + value);
104 else if (name == "mem_mb")
106 std::istringstream iss(value);
107 if (!(iss >> _params.resource_params.mem_mb))
108 throw Exception("salomecontainer::setproperty : params.resource_params.mem_mb value not correct : " + value);
110 else if (name == "cpu_clock")
112 std::istringstream iss(value);
113 if (!(iss >> _params.resource_params.cpu_clock))
114 throw Exception("salomecontainer::setproperty : params.resource_params.cpu_clock value not correct : " + value);
116 else if (name == "nb_node")
118 std::istringstream iss(value);
119 if (!(iss >> _params.resource_params.nb_node))
120 throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
122 else if (name == "nb_proc_per_node")
124 std::istringstream iss(value);
125 if (!(iss >> _params.resource_params.nb_proc_per_node))
126 throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value);
128 else if (name == "policy")
129 _params.resource_params.policy = CORBA::string_dup(value.c_str());
130 else if (name == "component_list")
132 std::string clean_value(value);
134 // Step 1: remove blanks
135 while(clean_value.find(" ") != std::string::npos)
136 clean_value = clean_value.erase(clean_value.find(" "), 1);
138 // Step 2: get values
139 while(!clean_value.empty())
141 std::string result("");
142 std::string::size_type loc = clean_value.find(",", 0);
143 if (loc != std::string::npos)
145 result = clean_value.substr(0, loc);
146 clean_value = clean_value.erase(0, loc+1);
150 result = clean_value;
153 if (result != "," && result != "")
155 addToComponentList(result);
160 else if (name == "resource_list")
162 std::string clean_value(value);
164 // Step 1: remove blanks
165 while(clean_value.find(" ") != std::string::npos)
166 clean_value = clean_value.erase(clean_value.find(" "), 1);
168 // Step 2: get values
169 while(!clean_value.empty())
171 std::string result("");
172 std::string::size_type loc = clean_value.find(",", 0);
173 if (loc != std::string::npos)
175 result = clean_value.substr(0, loc);
176 clean_value = clean_value.erase(0, loc+1);
180 result = clean_value;
183 if (result != "," && result != "")
185 addToResourceList(result);
190 _propertyMap[name]=value;
193 void SalomeContainerTools::addToComponentList(const std::string& name)
195 // Search if name is already in the list
196 for (CORBA::ULong i = 0; i < _params.resource_params.componentList.length(); i++)
198 std::string component_name = _params.resource_params.componentList[i].in();
199 if (component_name == name)
203 CORBA::ULong lgth = _params.resource_params.componentList.length();
204 _params.resource_params.componentList.length(lgth + 1);
205 _params.resource_params.componentList[lgth] = CORBA::string_dup(name.c_str());
208 void SalomeContainerTools::addToResourceList(const std::string& name)
210 // Search if name is already in the list
211 for (CORBA::ULong i = 0; i < _params.resource_params.resList.length(); i++)
213 std::string component_name = _params.resource_params.resList[i].in();
214 if (component_name == name)
218 CORBA::ULong lgth = _params.resource_params.resList.length();
219 _params.resource_params.resList.length(lgth + 1);
220 _params.resource_params.resList[lgth] = CORBA::string_dup(name.c_str());
223 std::string SalomeContainerTools::getContainerName() const
225 return std::string(_params.container_name);
228 void SalomeContainerTools::setContainerName(const std::string& name)
230 SetContainerNameOf(_params,name);
233 std::string SalomeContainerTools::getNotNullContainerName(const Container *contPtr, const Task *askingNode, bool& isEmpty) const
236 std::string name(_params.container_name);
244 //give a almost unique name to the container : Pid_Name_Addr
245 std::ostringstream stream;
248 stream << contPtr->getName();
250 stream << contPtr->getDiscreminantStrOfThis(askingNode);
255 std::string SalomeContainerTools::getHostName() const
257 return std::string(_params.resource_params.hostname);
260 void SalomeContainerTools::SetContainerNameOf(Engines::ContainerParameters& params, const std::string& name)
262 params.container_name=CORBA::string_dup(name.c_str());
265 std::map<std::string,std::string> SalomeContainerTools::getResourceProperties(const std::string& name) const
267 std::map<std::string,std::string> properties;
269 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
270 CORBA::ORB_ptr orb = runTime->getOrb();
271 if (!orb) return properties;
272 SALOME_NamingService namingService(orb);
273 SALOME_LifeCycleCORBA lcc(&namingService);
274 CORBA::Object_var obj = namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
275 if (CORBA::is_nil(obj))
277 Engines::ResourcesManager_var resManager = Engines::ResourcesManager::_narrow(obj);
278 if (CORBA::is_nil(resManager))
281 std::ostringstream value;
282 Engines::ResourceDefinition_var resource_definition = resManager->GetResourceDefinition(name.c_str());
283 properties["hostname"]=resource_definition->hostname.in();
284 properties["OS"]=resource_definition->OS.in();
285 value.str(""); value << resource_definition->mem_mb;
286 properties["mem_mb"]=value.str();
287 value.str(""); value << resource_definition->cpu_clock;
288 properties["cpu_clock"]=value.str();
289 value.str(""); value << resource_definition->nb_node;
290 properties["nb_node"]=value.str();
291 value.str(""); value << resource_definition->nb_proc_per_node;
292 properties["nb_proc_per_node"]=value.str();
294 properties["component_list"]="";
295 for(CORBA::ULong i=0; i < resource_definition->componentList.length(); i++)
298 properties["component_list"]=properties["component_list"]+",";
299 properties["component_list"]=properties["component_list"]+resource_definition->componentList[i].in();
306 * \param [in] compoNames
307 * \param [in,out] shutdownLevel
309 void SalomeContainerTools::Start(const std::vector<std::string>& compoNames, SalomeContainerHelper *schelp, SalomeContainerTools& sct, int& shutdownLevel, const Container *cont, const Task *askingNode)
311 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
312 SALOME_NamingService ns;
317 catch(SALOME_Exception& e)
319 throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
321 CORBA::Object_var obj(ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS));
322 Engines::ContainerManager_var contManager(Engines::ContainerManager::_narrow(obj));
325 std::string str(sct.getNotNullContainerName(cont,askingNode,isEmptyName));
326 DEBTRACE("SalomeContainer::start " << str <<";"<< _sct.getHostName() <<";"<<_type);
328 // Finalize parameters with components found in the container
330 for(std::vector<std::string>::const_iterator iter=compoNames.begin();iter!=compoNames.end();iter++)
331 sct.addToComponentList(*iter);
333 Engines::ContainerParameters myparams(sct.getParameters());
335 std::string dftLauchMode(schelp->getDftLaunchMode());
336 myparams.mode=CORBA::string_dup(dftLauchMode.c_str());
339 //If a container_name is given try to find an already existing container in naming service
340 //If not found start a new container with the given parameters
341 if (dynamic_cast<SalomeContainerMonoHelper *>(schelp) && !isEmptyName)
343 myparams.mode=CORBA::string_dup("getorstart");
350 //sct.setContainerName(str);
351 SetContainerNameOf(myparams,str);
352 Engines::Container_var trueCont(Engines::Container::_nil());
353 if(!isEmptyName && shutdownLevel==999)
355 //Make this only the first time start is called (_shutdownLevel==999)
356 //If the container is named, first try to get an existing container
357 //If there is an existing container use it and set the shutdown level to 3
358 //If there is no existing container, try to launch a new one and set the shutdown level to 2
362 trueCont=contManager->GiveContainer(myparams);
364 catch( const SALOME::SALOME_Exception& ex )
366 std::string msg="SalomeContainer::start : no existing container : ";
368 msg += ex.details.text.in();
375 if(!CORBA::is_nil(trueCont))
378 DEBTRACE( "container found: " << str << " " << _shutdownLevel );
383 myparams.mode="start";
384 DEBTRACE( "container not found: " << str << " " << _shutdownLevel);
388 if(CORBA::is_nil(trueCont))
391 // --- GiveContainer is used in batch mode to retreive launched containers,
392 // and is equivalent to StartContainer when not in batch.
393 trueCont=contManager->GiveContainer(myparams);
395 catch( const SALOME::SALOME_Exception& ex )
397 std::string msg="SalomeContainer::start : Unable to launch container in Salome : ";
399 msg += ex.details.text.in();
400 throw Exception(msg);
402 catch(CORBA::COMM_FAILURE&)
404 throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
406 catch(CORBA::Exception&)
408 throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
411 if(CORBA::is_nil(trueCont))
412 throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
414 schelp->setContainer(askingNode,trueCont);
416 CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName());
417 std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl;
420 CORBA::Object_ptr SalomeContainerTools::LoadComponent(SalomeContainerHelper *launchModeType, Container *cont, Task *askingNode)
422 DEBTRACE("SalomeContainer::loadComponent ");
423 const ComponentInstance *inst(askingNode?askingNode->getComponent():0);
425 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
426 if(!cont->isAlreadyStarted(askingNode))
427 cont->start(askingNode);
430 throw Exception("SalomeContainerTools::LoadComponent : no instance of component in the task requesting for a load of its component !");
431 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
433 YACS::BASES::AutoLocker<Container> alck(cont);//To be sure
434 std::string compoName(inst->getCompoName());
435 Engines::Container_var container(launchModeType->getContainer(askingNode));
438 bool isLoadable(container->load_component_Library(compoName.c_str(), reason));
440 objComponent=CreateComponentInstance(cont,container,inst);
445 CORBA::Object_ptr SalomeContainerTools::CreateComponentInstance(Container *cont, Engines::Container_ptr contPtr, const ComponentInstance *inst)
448 throw Exception("SalomeContainerTools::CreateComponentInstance : no instance of component in the task requesting for a load of its component !");
450 std::string compoName(inst->getCompoName());
451 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
453 Proc* p(cont->getProc());
456 std::string value(p->getProperty("DefaultStudyID"));
458 studyid= atoi(value.c_str());
460 // prepare component instance properties
461 Engines::FieldsDict_var env(new Engines::FieldsDict);
462 std::map<std::string, std::string> properties(inst->getProperties());
465 std::map<std::string,std::string> procMap=p->getProperties();
466 properties.insert(procMap.begin(),procMap.end());
469 std::map<std::string, std::string>::const_iterator itm;
470 env->length(properties.size());
472 for(itm = properties.begin(); itm != properties.end(); ++itm, item++)
474 DEBTRACE("envname="<<itm->first<<" envvalue="<< itm->second);
475 env[item].key= CORBA::string_dup(itm->first.c_str());
476 env[item].value <<= itm->second.c_str();
479 objComponent=contPtr->create_component_instance_env(compoName.c_str(), studyid, env, reason);
480 if(CORBA::is_nil(objComponent))
482 std::string text="Error while trying to create a new component: component '"+ compoName;
483 text=text+"' is not installed or it's a wrong name";
486 CORBA::string_free(reason);
487 throw Exception(text);
492 std::string SalomeContainerTools::GetPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
494 if(cont->isAlreadyStarted(askingNode))
496 Engines::Container_var container(launchModeType->getContainer(askingNode));
497 const char *what="/";
498 CORBA::String_var corbaStr(container->name());
499 std::string ret(corbaStr);
502 std::string::size_type i=ret.find_first_of(what,0);
503 i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
504 if(i!=std::string::npos)
505 return ret.substr(i+1);
509 return "Not placed yet !!!";
512 std::string SalomeContainerTools::GetFullPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
514 if(cont->isAlreadyStarted(askingNode))
516 Engines::Container_var container(launchModeType->getContainer(askingNode));
519 CORBA::String_var corbaStr(container->name());
520 std::string ret(corbaStr);
525 return "Unknown_placement";
529 return "Not_placed_yet";