1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SALOME_ContainerManager.hxx"
21 #include "SALOME_NamingService.hxx"
22 #include "SALOME_ModuleCatalog.hh"
24 #include <sys/types.h>
30 #include "Utils_CorbaException.hxx"
31 #include "Batch_Date.hxx"
33 #ifdef WITH_PACO_PARALLEL
37 #define TIME_OUT_TO_LAUNCH_CONT 21
41 vector<Engines::Container_ptr> SALOME_ContainerManager::_batchLaunchedContainers;
43 vector<Engines::Container_ptr>::iterator SALOME_ContainerManager::_batchLaunchedContainersIter;
45 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
48 //=============================================================================
52 * Define a CORBA single thread policy for the server, which avoid to deal
53 * with non thread-safe usage like Change_Directory in SALOME naming service
55 //=============================================================================
57 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_ResourcesManager *rm, SALOME_NamingService *ns)
59 MESSAGE("constructor");
64 PortableServer::POAManager_var pman = poa->the_POAManager();
65 _orb = CORBA::ORB::_duplicate(orb) ;
66 CORBA::PolicyList policies;
68 PortableServer::ThreadPolicy_var threadPol =
69 poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
70 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
72 _poa = poa->create_POA("SThreadPOA",pman,policies);
74 PortableServer::ObjectId_var id = _poa->activate_object(this);
75 CORBA::Object_var obj = _poa->id_to_reference(id);
76 Engines::ContainerManager_var refContMan =
77 Engines::ContainerManager::_narrow(obj);
79 _NS->Register(refContMan,_ContainerManagerNameInNS);
81 _isAppliSalomeDefined = (getenv("APPLI") != 0);
82 MESSAGE("constructor end");
85 //=============================================================================
89 //=============================================================================
91 SALOME_ContainerManager::~SALOME_ContainerManager()
93 MESSAGE("destructor");
96 //=============================================================================
98 * shutdown all the containers, then the ContainerManager servant
100 //=============================================================================
102 void SALOME_ContainerManager::Shutdown()
105 ShutdownContainers();
106 _NS->Destroy_Name(_ContainerManagerNameInNS);
107 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
108 _poa->deactivate_object(oid);
109 //_remove_ref() has already been done at creation
113 //=============================================================================
115 * Loop on all the containers listed in naming service, ask shutdown on each
117 //=============================================================================
119 void SALOME_ContainerManager::ShutdownContainers()
121 MESSAGE("ShutdownContainers");
123 isOK = _NS->Change_Directory("/Containers");
125 vector<string> vec = _NS->list_directory_recurs();
126 list<string> lstCont;
127 for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++){
129 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
130 Engines::Container_var cont=Engines::Container::_narrow(obj);
131 if(!CORBA::is_nil(cont)){
132 lstCont.push_back((*iter));
135 MESSAGE("Container list: ");
136 for(list<string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
139 for(list<string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
141 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
142 Engines::Container_var cont=Engines::Container::_narrow(obj);
143 if(!CORBA::is_nil(cont))
145 MESSAGE("ShutdownContainers: " << (*iter));
150 catch(CORBA::SystemException& e)
152 INFOS("CORBA::SystemException ignored : " << e);
154 catch(CORBA::Exception&)
156 INFOS("CORBA::Exception ignored.");
160 INFOS("Unknown exception ignored.");
164 MESSAGE("ShutdownContainers: no container ref for " << (*iter));
169 //=============================================================================
171 * Find a suitable Container in a list of machines, or start one
172 * \param params Machine Parameters required for the container
173 * \param possibleComputers list of machines usable for find or start
175 //=============================================================================
177 Engines::Container_ptr
178 SALOME_ContainerManager::
179 FindOrStartContainer(const Engines::MachineParameters& params,
180 const Engines::MachineList& possibleComputers)
182 Engines::Container_ptr ret = FindContainer(params,possibleComputers);
183 if(!CORBA::is_nil(ret))
185 MESSAGE("Container doesn't exist try to launch it ...");
187 return StartContainer(params,possibleComputers,Engines::P_FIRST);
191 //=============================================================================
193 * Start a suitable Container in a list of machines
194 * \param params Machine Parameters required for the container
195 * \param possibleComputers list of machines usable for start
197 //=============================================================================
199 Engines::Container_ptr
200 SALOME_ContainerManager::
201 StartContainer(const Engines::MachineParameters& params,
202 const Engines::MachineList& possibleComputers,
203 Engines::ResPolicy policy,const std::string& container_exe)
205 #ifdef WITH_PACO_PARALLEL
206 std::string parallelLib(params.parallelLib);
207 if (parallelLib != "")
208 return FindOrStartParallelContainer(params, possibleComputers);
211 string containerNameInNS;
212 char idc[3*sizeof(long)];
213 Engines::Container_ptr ret = Engines::Container::_nil();
215 MESSAGE("SALOME_ContainerManager::StartContainer " <<
216 possibleComputers.length());
219 for(int i=0;i<possibleComputers.length();i++)
220 lm.push_back(string(possibleComputers[i]));
225 case Engines::P_FIRST:
226 theMachine=_ResManager->GetImpl()->FindFirst(lm);
228 case Engines::P_CYCL:
229 theMachine=_ResManager->GetImpl()->FindNext(lm);
231 case Engines::P_BEST:
232 theMachine=_ResManager->GetImpl()->FindBest(lm);
236 catch( const SALOME_Exception &ex ){
238 return Engines::Container::_nil();
241 //If the machine name is localhost use the real name
242 if(theMachine == "localhost")
243 theMachine=GetHostname();
245 MESSAGE("try to launch it on " << theMachine);
247 // Get Id for container: a parallel container registers in Naming Service
248 // on the machine where is process 0. ContainerManager does'nt know the name
249 // of this machine before the launch of the parallel container. So to get
250 // the IOR of the parallel container in Naming Service, ContainerManager
251 // gives a unique Id. The parallel container registers his name under
252 // /ContainerManager/Id directory in NamingService
254 id = GetIdForContainer();
258 MESSAGE("SALOME_ContainerManager::StartContainer : " <<
259 "no possible computer");
260 return Engines::Container::_nil();
262 else if(theMachine==GetHostname())
263 command = BuildCommandToLaunchLocalContainer(params,id,container_exe);
265 command = BuildCommandToLaunchRemoteContainer(theMachine,params,id,container_exe);
269 //check if an entry exists in Naming service
272 containerNameInNS = "/ContainerManager/id";
273 sprintf(idc,"%ld",id);
274 containerNameInNS += idc;
277 containerNameInNS = _NS->BuildContainerNameForNS(params,theMachine.c_str());
279 SCRUTE(containerNameInNS);
280 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
281 if ( !CORBA::is_nil(obj) )
283 // unregister the registered container if it exists
284 _NS->Destroy_Name(containerNameInNS.c_str());
285 // unregister component instances ???
286 //Engines::Container_var cont=Engines::Container::_narrow(obj);
289 //redirect stdout and stderr in a file
290 string logFilename="/tmp/"+_NS->ContainerName(params)+"_"+ theMachine +"_"+getenv( "USER" )+".log" ;
291 command += " > " + logFilename + " 2>&1 &";
293 // launch container with a system call
294 int status=system(command.c_str());
296 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
297 "(system command status -1)");
298 return Engines::Container::_nil();
300 else if (status == 217){
301 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
302 "(system command status 217)");
303 return Engines::Container::_nil();
306 int count=TIME_OUT_TO_LAUNCH_CONT;
307 MESSAGE("count = "<<count);
308 while ( CORBA::is_nil(ret) && count ){
316 MESSAGE( count << ". Waiting for container on " << theMachine);
318 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
319 ret=Engines::Container::_narrow(obj);
322 if ( CORBA::is_nil(ret) )
324 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed");
328 logFilename=":"+logFilename;
329 logFilename="@"+GetHostname()+logFilename;
330 logFilename=getenv( "USER" )+logFilename;
331 ret->logfilename(logFilename.c_str());
338 //=============================================================================
340 * Start a suitable Container in a list of machines
341 * \param params Machine Parameters required for the container
342 * \param possibleComputers list of machines usable for start
344 //=============================================================================
346 Engines::Container_ptr
347 SALOME_ContainerManager::
348 StartContainer(const Engines::MachineParameters& params,
349 Engines::ResPolicy policy,
350 const Engines::CompoList& componentList)
352 Engines::MachineList_var possibleComputers = _ResManager->GetFittingResources(params,componentList);
354 // Look into ModulCatalog if a specific container must be launched
355 CORBA::String_var container_exe;
359 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
360 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
361 if (CORBA::is_nil (Catalog))
362 return Engines::Container::_nil();
363 // Loop through component list
364 for(int i=0;i<componentList.length();i++)
366 const char* compoi = componentList[i];
367 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
368 if (CORBA::is_nil (compoInfo))
370 INFOS("ContainerManager Error: Component not found in the catalog" );
372 return Engines::Container::_nil();
374 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
375 container_exe=compoInfo->implementation_name();
376 if(impl==SALOME_ModuleCatalog::CEXE)
380 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
381 return Engines::Container::_nil();
387 catch (ServiceUnreachable&)
389 INFOS("Caught exception: Naming Service Unreachable");
390 return Engines::Container::_nil();
394 INFOS("Caught unknown exception.");
395 return Engines::Container::_nil();
399 return StartContainer(params,possibleComputers,policy,container_exe.in());
401 return StartContainer(params,possibleComputers,policy);
404 #ifdef WITH_PACO_PARALLEL
405 //=============================================================================
407 * Find or Start a suitable PaCO++ Parallel Container in a list of machines.
408 * \param params Machine Parameters required for the container
409 * \param possibleComputers list of machines usable for find or start
411 * \return CORBA container reference.
413 //=============================================================================
414 Engines::Container_ptr
415 SALOME_ContainerManager::
416 FindOrStartParallelContainer(const Engines::MachineParameters& params_const,
417 const Engines::MachineList& possibleComputers)
419 CORBA::Object_var obj;
420 PaCO::InterfaceManager_var proxy;
421 Engines::Container_ptr ret = Engines::Container::_nil();
422 Engines::MachineParameters params(params_const);
424 // Step 1 : Try to find a suitable container
425 // Currently not as good as could be since
426 // we have to verified the number of nodes of the container
427 // if a user tell that.
428 ret = FindContainer(params, possibleComputers);
430 if(CORBA::is_nil(ret)) {
431 // Step 2 : Starting a new parallel container
432 INFOS("[FindOrStartParallelContainer] Starting a parallel container");
434 // Step 2.1 : Choose a computer
435 string theMachine = _ResManager->FindFirst(possibleComputers);
436 if(theMachine == "") {
437 INFOS("[FindOrStartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
438 INFOS("[FindOrStartParallelContainer] No possible computer found");
439 INFOS("[FindOrStartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
442 INFOS("[FindOrStartParallelContainer] on machine : " << theMachine);
444 if(theMachine == GetHostname()) {
445 // Step 3 : starting parallel container proxy
446 params.hostname = CORBA::string_dup(theMachine.c_str());
447 Engines::MachineParameters params_proxy(params);
449 command = BuildCommandToLaunchLocalParallelContainer("SALOME_ParallelContainerProxy", params_proxy, "xterm");
451 catch(const SALOME_Exception & ex){
453 return Engines::Container::_nil();
455 // LaunchParallelContainer uses this value to know if it launches the proxy or the nodes
456 params_proxy.nb_component_nodes = 0;
457 obj = LaunchParallelContainer(command, params_proxy, _NS->ContainerName(params));
458 ret = Engines::Container::_narrow(obj);
459 proxy = PaCO::InterfaceManager::_narrow(obj);
461 // Step 4 : starting parallel container nodes
462 command = BuildCommandToLaunchLocalParallelContainer("SALOME_ParallelContainerNode", params, "xterm");
463 string name = _NS->ContainerName(params) + "Node";
464 LaunchParallelContainer(command, params, name);
465 // Step 5 : connecting nodes and the proxy to actually create a parallel container
467 for (int i = 0; i < params.nb_component_nodes; i++) {
471 snprintf(buffer,5,"%d",i);
473 _snprintf(buffer,5,"%d",i);
475 string name_cont = name + string(buffer);
477 string theNodeMachine(CORBA::string_dup(params.hostname));
478 string containerNameInNS = _NS->BuildContainerNameForNS(name_cont.c_str(),theNodeMachine.c_str());
479 int count = TIME_OUT_TO_LAUNCH_CONT;
480 obj = _NS->Resolve(containerNameInNS.c_str());
481 while (CORBA::is_nil(obj) && count) {
482 INFOS("[FindOrStartParallelContainer] CONNECTION FAILED !!!!!!!!!!!!!!!!!!!!!!!!");
489 obj = _NS->Resolve(containerNameInNS.c_str());
492 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
493 MESSAGE("[FindOrStartParallelContainer] Deploying node : " << name);
498 catch(CORBA::SystemException& e)
500 INFOS("Caught CORBA::SystemException. : " << e);
502 catch(PortableServer::POA::ServantAlreadyActive&)
504 INFOS("Caught CORBA::ServantAlreadyActiveException");
506 catch(CORBA::Exception&)
508 INFOS("Caught CORBA::Exception.");
510 catch(std::exception& exc)
512 INFOS("Caught std::exception - "<<exc.what());
516 INFOS("Caught unknown exception.");
518 INFOS("[FindOrStartParallelContainer] node " << name << " deployed");
521 INFOS("[FindOrStartParallelContainer] Currently parallel containers are launched only on the local host");
528 //=============================================================================
530 * Find or Start a suitable PaCO++ Parallel Container in a list of machines.
531 * \param params Machine Parameters required for the container
532 * \param possibleComputers list of machines usable for find or start
534 * \return CORBA container reference.
536 //=============================================================================
537 Engines::Container_ptr
538 SALOME_ContainerManager::
539 FindOrStartParallelContainer(const Engines::MachineParameters& params,
540 const Engines::MachineList& possibleComputers)
542 Engines::Container_ptr ret = Engines::Container::_nil();
543 INFOS("[FindOrStartParallelContainer] is disabled !");
544 INFOS("[FindOrStartParallelContainer] recompile SALOME Kernel to enable parallel extension");
549 //=============================================================================
551 * Give a suitable Container in a list of machines
552 * \param params Machine Parameters required for the container
553 * \param possibleComputers list of machines usable for start
555 //=============================================================================
557 Engines::Container_ptr
558 SALOME_ContainerManager::
559 GiveContainer(const Engines::MachineParameters& params,
560 Engines::ResPolicy policy,
561 const Engines::CompoList& componentList)
563 char *valenv=getenv("SALOME_BATCH");
565 if (strcmp(valenv,"1")==0)
567 if(_batchLaunchedContainers.empty())
568 fillBatchLaunchedContainers();
570 if (_batchLaunchedContainersIter == _batchLaunchedContainers.end())
571 _batchLaunchedContainersIter = _batchLaunchedContainers.begin();
573 Engines::Container_ptr rtn = Engines::Container::_duplicate(*_batchLaunchedContainersIter);
574 _batchLaunchedContainersIter++;
577 return StartContainer(params,policy,componentList);
580 //=============================================================================
584 //=============================================================================
586 Engines::Container_ptr
587 SALOME_ContainerManager::
588 FindContainer(const Engines::MachineParameters& params,
589 const char *theMachine)
591 string containerNameInNS(_NS->BuildContainerNameForNS(params,theMachine));
592 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
593 if( !CORBA::is_nil(obj) )
594 return Engines::Container::_narrow(obj);
596 return Engines::Container::_nil();
599 //=============================================================================
603 //=============================================================================
605 Engines::Container_ptr
606 SALOME_ContainerManager::
607 FindContainer(const Engines::MachineParameters& params,
608 const Engines::MachineList& possibleComputers)
610 MESSAGE("FindContainer "<<possibleComputers.length());
611 for(unsigned int i=0;i<possibleComputers.length();i++)
613 MESSAGE("FindContainer possible " << possibleComputers[i]);
614 Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
615 if( !CORBA::is_nil(cont) )
618 MESSAGE("FindContainer: not found");
619 return Engines::Container::_nil();
622 //=============================================================================
623 /*! This method launches the parallel container.
624 * It will may be placed on the ressources manager.
626 * \param command to launch
627 * \param container's parameters
628 * \param name of the container
630 * \return CORBA container reference
632 //=============================================================================
634 SALOME_ContainerManager::LaunchParallelContainer(const std::string& command,
635 const Engines::MachineParameters& params,
636 const std::string& name)
638 CORBA::Object_ptr obj = CORBA::Object::_nil();
639 string containerNameInNS;
640 MESSAGE("[LaunchParallelContainer] : command to launch...");
642 if (params.nb_component_nodes == 0) {
643 INFOS("[LaunchParallelContainer] launching the proxy of the parallel container");
644 int status = system(command.c_str());
646 INFOS("[LaunchParallelContainer] failed : system command status -1");
648 else if (status == 217) {
649 INFOS("[LaunchParallelContainer] failed : system command status 217");
652 int count = TIME_OUT_TO_LAUNCH_CONT;
653 string theMachine(CORBA::string_dup(params.hostname));
654 containerNameInNS = _NS->BuildContainerNameForNS((char*) name.c_str(),theMachine.c_str());
656 INFOS("[LaunchParallelContainer] Waiting for Parallel Container proxy on " << theMachine);
657 while (CORBA::is_nil(obj) && count) {
664 obj = _NS->Resolve(containerNameInNS.c_str());
668 INFOS("[LaunchParallelContainer] launching the nodes of the parallel container");
669 int status = system(command.c_str());
671 INFOS("[LaunchParallelContainer] failed : system command status -1");
673 else if (status == 217) {
674 INFOS("[LaunchParallelContainer] failed : system command status 217");
676 // We are waiting all the nodes
677 for (int i = 0; i < params.nb_component_nodes; i++) {
678 obj = CORBA::Object::_nil();
679 int count = TIME_OUT_TO_LAUNCH_CONT;
684 snprintf(buffer,5,"%d",i);
686 _snprintf(buffer,5,"%d",i);
689 string name_cont = name + string(buffer);
691 // I don't like this...
692 string theMachine(CORBA::string_dup(params.hostname));
693 containerNameInNS = _NS->BuildContainerNameForNS((char*) name_cont.c_str(),theMachine.c_str());
694 cerr << "[LaunchContainer] Waiting for Parllel Container node " << containerNameInNS << " on " << theMachine << endl;
695 while (CORBA::is_nil(obj) && count) {
702 obj = _NS->Resolve(containerNameInNS.c_str());
707 if ( CORBA::is_nil(obj) ) {
708 INFOS("[LaunchParallelContainer] failed");
713 //=============================================================================
715 * Get Id for container: a parallel container registers in Naming Service
716 * on the machine where is process 0. ContainerManager does'nt know the name
717 * of this machine before the launch of the parallel container. So to get
718 * the IOR of the parallel container in Naming Service, ContainerManager
719 * gives a unique Id. The parallel container registers his name under
720 * /ContainerManager/Id directory in NamingService
722 //=============================================================================
725 long SALOME_ContainerManager::GetIdForContainer(void)
731 void SALOME_ContainerManager::fillBatchLaunchedContainers()
733 _batchLaunchedContainers.clear();
734 _NS->Change_Directory("/Containers");
735 vector<string> vec = _NS->list_directory_recurs();
736 for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++){
737 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
738 Engines::Container_ptr cont=Engines::Container::_narrow(obj);
739 if(!CORBA::is_nil(cont)){
740 _batchLaunchedContainers.push_back(cont);
743 _batchLaunchedContainersIter=_batchLaunchedContainers.begin();
746 //=============================================================================
748 * This is no longer valid (C++ container are also python containers)
750 //=============================================================================
752 bool isPythonContainer(const char* ContainerName)
755 int len = strlen(ContainerName);
758 if (strcmp(ContainerName + len - 2, "Py") == 0)
764 //=============================================================================
766 * Builds the script to be launched
768 * If SALOME Application not defined ($APPLI),
769 * see BuildTempFileToLaunchRemoteContainer()
771 * Else rely on distant configuration. Command is under the form (example):
772 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
773 * SALOME_Container containerName &"
775 * - where user is ommited if not specified in CatalogResources,
776 * - where distant path is always relative to user@machine $HOME, and
777 * equal to $APPLI if not specified in CatalogResources,
778 * - where hostNS is the hostname of CORBA naming server (set by scripts to
779 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
780 * - where portNS is the port used by CORBA naming server (set by scripts to
781 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
782 * - where workingdir is the requested working directory for the container.
783 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
785 //=============================================================================
788 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer
789 (const string& machine,
790 const Engines::MachineParameters& params, const long id,const std::string& container_exe)
794 char idc[3*sizeof(long)];
796 if ( ! _isAppliSalomeDefined )
797 command = BuildTempFileToLaunchRemoteContainer(machine, params);
801 const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
805 if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
807 else if ( params.nb_node == 0 )
808 nbproc = params.nb_proc_per_node;
809 else if ( params.nb_proc_per_node == 0 )
810 nbproc = params.nb_node;
812 nbproc = params.nb_node * params.nb_proc_per_node;
815 // "ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
816 // SALOME_Container containerName &"
818 if (resInfo.Protocol == rsh)
820 else if (resInfo.Protocol == ssh)
823 throw SALOME_Exception("Unknown protocol");
825 if (resInfo.UserName != "")
827 command += resInfo.UserName;
834 if (resInfo.AppliPath != "")
835 command += resInfo.AppliPath; // path relative to user@machine $HOME
838 ASSERT(getenv("APPLI"));
839 command += getenv("APPLI"); // path relative to user@machine $HOME
842 command += "/runRemote.sh ";
844 ASSERT(getenv("NSHOST"));
845 command += getenv("NSHOST"); // hostname of CORBA name server
848 ASSERT(getenv("NSPORT"));
849 command += getenv("NSPORT"); // port of CORBA name server
851 std::string wdir=params.workingdir.in();
854 command += " WORKINGDIR ";
856 if(wdir == "$TEMPDIR")
858 command += wdir; // requested working directory
864 command += " mpirun -np ";
865 std::ostringstream o;
869 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
871 command += " SALOME_MPIContainer ";
874 command += " " +container_exe+ " ";
876 command += _NS->ContainerName(params);
878 sprintf(idc,"%ld",id);
881 AddOmninamesParams(command);
883 MESSAGE("command =" << command);
889 //=============================================================================
891 * builds the command to be launched.
893 //=============================================================================
896 SALOME_ContainerManager::BuildCommandToLaunchLocalContainer
897 (const Engines::MachineParameters& params, const long id,const std::string& container_exe)
902 char idc[3*sizeof(long)];
906 command = "mpirun -np ";
908 if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
910 else if ( params.nb_node == 0 )
911 nbproc = params.nb_proc_per_node;
912 else if ( params.nb_proc_per_node == 0 )
913 nbproc = params.nb_node;
915 nbproc = params.nb_node * params.nb_proc_per_node;
917 std::ostringstream o;
923 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
926 if (isPythonContainer(params.container_name))
927 command += "pyMPI SALOME_ContainerPy.py ";
929 command += "SALOME_MPIContainer ";
935 std::string wdir=params.workingdir.in();
938 // a working directory is requested
939 if(wdir == "$TEMPDIR")
941 // a new temporary directory is requested
942 char dir[]="/tmp/salomeXXXXXX";
943 char* mdir=mkdtemp(dir);
945 std::cerr << "Problem in mkdtemp " << dir << " " << mdir << std::endl;
947 command="cd "+std::string(dir)+";";
951 // a permanent directory is requested use it or create it
952 command="mkdir -p " + wdir + " && cd " + wdir + ";";
955 if (isPythonContainer(params.container_name))
956 command += "SALOME_ContainerPy.py ";
958 command += container_exe + " ";
961 command += _NS->ContainerName(params);
963 sprintf(idc,"%ld",id);
966 AddOmninamesParams(command);
968 MESSAGE("Command is ... " << command);
973 //=============================================================================
975 * removes the generated temporary file in case of a remote launch.
977 //=============================================================================
979 void SALOME_ContainerManager::RmTmpFile()
981 if (_TmpFileName != "")
984 string command = "rm ";
986 string command = "del /F ";
988 command += _TmpFileName;
989 char *temp = strdup(command.c_str());
990 int lgthTemp = strlen(temp);
991 temp[lgthTemp - 3] = '*';
992 temp[lgthTemp - 2] = '\0';
998 //=============================================================================
1000 * add to command all options relative to naming service.
1002 //=============================================================================
1004 void SALOME_ContainerManager::AddOmninamesParams(string& command) const
1006 CORBA::String_var iorstr = _NS->getIORaddr();
1007 command += "ORBInitRef NameService=";
1012 //=============================================================================
1014 * add to command all options relative to naming service.
1016 //=============================================================================
1018 void SALOME_ContainerManager::AddOmninamesParams(ofstream& fileStream) const
1020 CORBA::String_var iorstr = _NS->getIORaddr();
1021 fileStream << "ORBInitRef NameService=";
1022 fileStream << iorstr;
1025 //=============================================================================
1027 * generate a file name in /tmp directory
1029 //=============================================================================
1031 string SALOME_ContainerManager::BuildTemporaryFileName() const
1033 //build more complex file name to support multiple salome session
1034 char *temp = new char[19];
1035 strcpy(temp, "/tmp/command");
1036 strcat(temp, "XXXXXX");
1043 itoa(getpid(), aPID, 10);
1047 string command(temp);
1054 //=============================================================================
1056 * Builds in a temporary file the script to be launched.
1058 * Used if SALOME Application ($APPLI) is not defined.
1059 * The command is build with data from CatalogResources, in which every path
1060 * used on remote computer must be defined.
1062 //=============================================================================
1065 SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer
1066 (const string& machine,
1067 const Engines::MachineParameters& params) throw(SALOME_Exception)
1071 _TmpFileName = BuildTemporaryFileName();
1072 ofstream tempOutputFile;
1073 tempOutputFile.open(_TmpFileName.c_str(), ofstream::out );
1074 const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
1075 tempOutputFile << "#! /bin/sh" << endl;
1079 tempOutputFile << "export SALOME_trace=local" << endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1080 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1086 tempOutputFile << "mpirun -np ";
1089 if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
1091 else if ( params.nb_node == 0 )
1092 nbproc = params.nb_proc_per_node;
1093 else if ( params.nb_proc_per_node == 0 )
1094 nbproc = params.nb_node;
1096 nbproc = params.nb_node * params.nb_proc_per_node;
1098 std::ostringstream o;
1100 tempOutputFile << nbproc << " ";
1102 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1106 tempOutputFile << getenv("KERNEL_ROOT_DIR") << "/bin/salome/";
1110 if (isPythonContainer(params.container_name))
1111 tempOutputFile << "pyMPI SALOME_ContainerPy.py ";
1113 tempOutputFile << "SALOME_MPIContainer ";
1118 if (isPythonContainer(params.container_name))
1119 tempOutputFile << "SALOME_ContainerPy.py ";
1121 tempOutputFile << "SALOME_Container ";
1124 tempOutputFile << _NS->ContainerName(params) << " -";
1125 AddOmninamesParams(tempOutputFile);
1126 tempOutputFile << " &" << endl;
1127 tempOutputFile.flush();
1128 tempOutputFile.close();
1129 chmod(_TmpFileName.c_str(), 0x1ED);
1131 // --- Build command
1135 if (resInfo.Protocol == rsh)
1138 string commandRcp = "rcp ";
1139 commandRcp += _TmpFileName;
1141 commandRcp += machine;
1143 commandRcp += _TmpFileName;
1144 status = system(commandRcp.c_str());
1147 else if (resInfo.Protocol == ssh)
1150 string commandRcp = "scp ";
1151 commandRcp += _TmpFileName;
1153 commandRcp += machine;
1155 commandRcp += _TmpFileName;
1156 status = system(commandRcp.c_str());
1159 throw SALOME_Exception("Unknown protocol");
1162 throw SALOME_Exception("Error of connection on remote host");
1165 _CommandForRemAccess = command;
1167 command += _TmpFileName;
1175 //=============================================================================
1176 /*! Creates a command line that the container manager uses to launch
1177 * a parallel container.
1179 //=============================================================================
1181 SALOME_ContainerManager::BuildCommandToLaunchLocalParallelContainer(const std::string& exe_name,
1182 const Engines::MachineParameters& params,
1183 const std::string& log)
1185 // This method knows the differences between the proxy and the nodes.
1186 // nb_component_nodes is not used in the same way if it is a proxy or
1190 string parallelLib(CORBA::string_dup(params.parallelLib));
1191 string hostname(CORBA::string_dup(params.hostname));
1192 int par = exe_name.find("Proxy");
1193 int nbproc = params.nb_component_nodes;
1195 sprintf(buffer,"%d",nbproc);
1197 Engines::MachineParameters_var rtn = new Engines::MachineParameters();
1198 rtn->container_name = params.container_name;
1199 rtn->hostname = params.hostname;
1200 rtn->OS = params.OS;
1201 rtn->mem_mb = params.mem_mb;
1202 rtn->cpu_clock = params.cpu_clock;
1203 rtn->nb_proc_per_node = params.nb_proc_per_node;
1204 rtn->nb_node = params.nb_node;
1205 rtn->isMPI = params.isMPI;
1207 string real_exe_name = exe_name + parallelLib;
1209 if (parallelLib == "Dummy")
1211 //command = "gdb --args ";
1212 //command = "valgrind --tool=memcheck --log-file=val_log ";
1213 //command += real_exe_name;
1215 command = real_exe_name;
1217 command += " " + _NS->ContainerName(rtn);
1218 command += " " + parallelLib;
1219 command += " " + hostname;
1221 AddOmninamesParams(command);
1224 else if (parallelLib == "Mpi")
1226 // Step 1 : check if MPI is started
1227 if (_MpiStarted == false)
1236 command = "mpiexec -np " + string(buffer) + " ";
1237 // command += "gdb --args ";
1238 command += real_exe_name;
1239 command += " " + _NS->ContainerName(rtn);
1240 command += " " + parallelLib;
1241 command += " " + hostname;
1243 AddOmninamesParams(command);
1248 command = "mpiexec -np 1 ";
1249 command += real_exe_name;
1250 command += " " + _NS->ContainerName(rtn);
1251 command += " " + string(buffer);
1252 command += " " + parallelLib;
1253 command += " " + hostname;
1255 AddOmninamesParams(command);
1260 std::string message("Unknown parallelLib" + parallelLib);
1261 throw SALOME_Exception(message.c_str());
1265 if (log == "default")
1267 command += " > /tmp/";
1268 command += _NS->ContainerName(rtn);
1270 command += GetHostname();
1272 command += getenv( "USER" ) ;
1273 command += ".log 2>&1 &" ;
1277 command = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH; "
1278 + command + " \" &";
1279 // + command + "; echo $LD_LIBRARY_PATH; cat \" &";
1283 /* if (log == "xterm")
1285 command = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH; echo $LD_LIBRARY_PATH; echo $PATH; " + command + "; cat \" &";
1288 /* command = "cd ; rm " + fichier_commande + "; touch " + \
1289 fichier_commande + "; echo \" export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; " + \
1290 command + " >& /tmp/ribes_" + fichier_commande + " & \" > " + fichier_commande + ";";
1291 command += "ssh cn01 sh " + fichier_commande + " &";
1292 cerr << "La commande : " << command << endl;
1296 void SALOME_ContainerManager::startMPI()
1298 cerr << "----------------------------------------------" << endl;
1299 cerr << "----------------------------------------------" << endl;
1300 cerr << "----------------------------------------------" << endl;
1301 cerr << "-Only Lam on Localhost is currently supported-" << endl;
1302 cerr << "----------------------------------------------" << endl;
1303 cerr << "----------------------------------------------" << endl;
1304 cerr << "----------------------------------------------" << endl;
1306 int status = system("lamboot");
1309 INFOS("lamboot failed : system command status -1");
1311 else if (status == 217)
1313 INFOS("lamboot failed : system command status 217");