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 // shutdown the registered container if it exists
284 Engines::Container_var cont=Engines::Container::_narrow(obj);
285 if(!CORBA::is_nil(cont))
291 catch(CORBA::Exception&)
293 INFOS("CORBA::Exception ignored.");
298 //redirect stdout and stderr in a file
299 string logFilename="/tmp/"+_NS->ContainerName(params)+"_"+ theMachine +"_"+getenv( "USER" )+".log" ;
300 command += " > " + logFilename + " 2>&1 &";
302 // launch container with a system call
303 int status=system(command.c_str());
305 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
306 "(system command status -1)");
307 return Engines::Container::_nil();
309 else if (status == 217){
310 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
311 "(system command status 217)");
312 return Engines::Container::_nil();
315 int count=TIME_OUT_TO_LAUNCH_CONT;
316 MESSAGE("count = "<<count);
317 while ( CORBA::is_nil(ret) && count ){
325 MESSAGE( count << ". Waiting for container on " << theMachine);
327 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
328 ret=Engines::Container::_narrow(obj);
331 if ( CORBA::is_nil(ret) )
333 MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed");
337 logFilename=":"+logFilename;
338 logFilename="@"+GetHostname()+logFilename;
339 logFilename=getenv( "USER" )+logFilename;
340 ret->logfilename(logFilename.c_str());
347 //=============================================================================
349 * Start a suitable Container in a list of machines
350 * \param params Machine Parameters required for the container
351 * \param possibleComputers list of machines usable for start
353 //=============================================================================
355 Engines::Container_ptr
356 SALOME_ContainerManager::
357 StartContainer(const Engines::MachineParameters& params,
358 Engines::ResPolicy policy,
359 const Engines::CompoList& componentList)
361 Engines::MachineList_var possibleComputers = _ResManager->GetFittingResources(params,componentList);
363 // Look into ModulCatalog if a specific container must be launched
364 CORBA::String_var container_exe;
368 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
369 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
370 if (CORBA::is_nil (Catalog))
371 return Engines::Container::_nil();
372 // Loop through component list
373 for(int i=0;i<componentList.length();i++)
375 const char* compoi = componentList[i];
376 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
377 if (CORBA::is_nil (compoInfo))
379 INFOS("ContainerManager Error: Component not found in the catalog" );
381 return Engines::Container::_nil();
383 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
384 container_exe=compoInfo->implementation_name();
385 if(impl==SALOME_ModuleCatalog::CEXE)
389 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
390 return Engines::Container::_nil();
396 catch (ServiceUnreachable&)
398 INFOS("Caught exception: Naming Service Unreachable");
399 return Engines::Container::_nil();
403 INFOS("Caught unknown exception.");
404 return Engines::Container::_nil();
408 return StartContainer(params,possibleComputers,policy,container_exe.in());
410 return StartContainer(params,possibleComputers,policy);
413 #ifdef WITH_PACO_PARALLEL
414 //=============================================================================
416 * Find or Start a suitable PaCO++ Parallel Container in a list of machines.
417 * \param params Machine Parameters required for the container
418 * \param possibleComputers list of machines usable for find or start
420 * \return CORBA container reference.
422 //=============================================================================
423 Engines::Container_ptr
424 SALOME_ContainerManager::
425 FindOrStartParallelContainer(const Engines::MachineParameters& params_const,
426 const Engines::MachineList& possibleComputers)
428 CORBA::Object_var obj;
429 PaCO::InterfaceManager_var proxy;
430 Engines::Container_ptr ret = Engines::Container::_nil();
431 Engines::MachineParameters params(params_const);
433 // Step 1 : Try to find a suitable container
434 // Currently not as good as could be since
435 // we have to verified the number of nodes of the container
436 // if a user tell that.
437 ret = FindContainer(params, possibleComputers);
439 if(CORBA::is_nil(ret)) {
440 // Step 2 : Starting a new parallel container
441 INFOS("[FindOrStartParallelContainer] Starting a parallel container");
443 // Step 2.1 : Choose a computer
444 string theMachine = _ResManager->FindFirst(possibleComputers);
445 if(theMachine == "") {
446 INFOS("[FindOrStartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
447 INFOS("[FindOrStartParallelContainer] No possible computer found");
448 INFOS("[FindOrStartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
451 INFOS("[FindOrStartParallelContainer] on machine : " << theMachine);
453 if(theMachine == GetHostname()) {
454 // Step 3 : starting parallel container proxy
455 params.hostname = CORBA::string_dup(theMachine.c_str());
456 Engines::MachineParameters params_proxy(params);
458 command = BuildCommandToLaunchLocalParallelContainer("SALOME_ParallelContainerProxy", params_proxy, "xterm");
460 catch(const SALOME_Exception & ex){
462 return Engines::Container::_nil();
464 // LaunchParallelContainer uses this value to know if it launches the proxy or the nodes
465 params_proxy.nb_component_nodes = 0;
466 obj = LaunchParallelContainer(command, params_proxy, _NS->ContainerName(params));
467 ret = Engines::Container::_narrow(obj);
468 proxy = PaCO::InterfaceManager::_narrow(obj);
470 // Step 4 : starting parallel container nodes
471 command = BuildCommandToLaunchLocalParallelContainer("SALOME_ParallelContainerNode", params, "xterm");
472 string name = _NS->ContainerName(params) + "Node";
473 LaunchParallelContainer(command, params, name);
474 // Step 5 : connecting nodes and the proxy to actually create a parallel container
476 for (int i = 0; i < params.nb_component_nodes; i++) {
480 snprintf(buffer,5,"%d",i);
482 _snprintf(buffer,5,"%d",i);
484 string name_cont = name + string(buffer);
486 string theNodeMachine(CORBA::string_dup(params.hostname));
487 string containerNameInNS = _NS->BuildContainerNameForNS(name_cont.c_str(),theNodeMachine.c_str());
488 int count = TIME_OUT_TO_LAUNCH_CONT;
489 obj = _NS->Resolve(containerNameInNS.c_str());
490 while (CORBA::is_nil(obj) && count) {
491 INFOS("[FindOrStartParallelContainer] CONNECTION FAILED !!!!!!!!!!!!!!!!!!!!!!!!");
498 obj = _NS->Resolve(containerNameInNS.c_str());
501 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
502 MESSAGE("[FindOrStartParallelContainer] Deploying node : " << name);
507 catch(CORBA::SystemException& e)
509 INFOS("Caught CORBA::SystemException. : " << e);
511 catch(PortableServer::POA::ServantAlreadyActive&)
513 INFOS("Caught CORBA::ServantAlreadyActiveException");
515 catch(CORBA::Exception&)
517 INFOS("Caught CORBA::Exception.");
519 catch(std::exception& exc)
521 INFOS("Caught std::exception - "<<exc.what());
525 INFOS("Caught unknown exception.");
527 INFOS("[FindOrStartParallelContainer] node " << name << " deployed");
530 INFOS("[FindOrStartParallelContainer] Currently parallel containers are launched only on the local host");
537 //=============================================================================
539 * Find or Start a suitable PaCO++ Parallel Container in a list of machines.
540 * \param params Machine Parameters required for the container
541 * \param possibleComputers list of machines usable for find or start
543 * \return CORBA container reference.
545 //=============================================================================
546 Engines::Container_ptr
547 SALOME_ContainerManager::
548 FindOrStartParallelContainer(const Engines::MachineParameters& params,
549 const Engines::MachineList& possibleComputers)
551 Engines::Container_ptr ret = Engines::Container::_nil();
552 INFOS("[FindOrStartParallelContainer] is disabled !");
553 INFOS("[FindOrStartParallelContainer] recompile SALOME Kernel to enable parallel extension");
558 //=============================================================================
560 * Give a suitable Container in a list of machines
561 * \param params Machine Parameters required for the container
562 * \param possibleComputers list of machines usable for start
564 //=============================================================================
566 Engines::Container_ptr
567 SALOME_ContainerManager::
568 GiveContainer(const Engines::MachineParameters& params,
569 Engines::ResPolicy policy,
570 const Engines::CompoList& componentList)
572 char *valenv=getenv("SALOME_BATCH");
574 if (strcmp(valenv,"1")==0)
576 if(_batchLaunchedContainers.empty())
577 fillBatchLaunchedContainers();
579 if (_batchLaunchedContainersIter == _batchLaunchedContainers.end())
580 _batchLaunchedContainersIter = _batchLaunchedContainers.begin();
582 Engines::Container_ptr rtn = Engines::Container::_duplicate(*_batchLaunchedContainersIter);
583 _batchLaunchedContainersIter++;
586 return StartContainer(params,policy,componentList);
589 //=============================================================================
593 //=============================================================================
595 Engines::Container_ptr
596 SALOME_ContainerManager::
597 FindContainer(const Engines::MachineParameters& params,
598 const char *theMachine)
600 string containerNameInNS(_NS->BuildContainerNameForNS(params,theMachine));
601 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
602 if( !CORBA::is_nil(obj) )
603 return Engines::Container::_narrow(obj);
605 return Engines::Container::_nil();
608 //=============================================================================
612 //=============================================================================
614 Engines::Container_ptr
615 SALOME_ContainerManager::
616 FindContainer(const Engines::MachineParameters& params,
617 const Engines::MachineList& possibleComputers)
619 MESSAGE("FindContainer "<<possibleComputers.length());
620 for(unsigned int i=0;i<possibleComputers.length();i++)
622 MESSAGE("FindContainer possible " << possibleComputers[i]);
623 Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
624 if( !CORBA::is_nil(cont) )
627 MESSAGE("FindContainer: not found");
628 return Engines::Container::_nil();
631 //=============================================================================
632 /*! This method launches the parallel container.
633 * It will may be placed on the ressources manager.
635 * \param command to launch
636 * \param container's parameters
637 * \param name of the container
639 * \return CORBA container reference
641 //=============================================================================
643 SALOME_ContainerManager::LaunchParallelContainer(const std::string& command,
644 const Engines::MachineParameters& params,
645 const std::string& name)
647 CORBA::Object_ptr obj = CORBA::Object::_nil();
648 string containerNameInNS;
649 MESSAGE("[LaunchParallelContainer] : command to launch...");
651 if (params.nb_component_nodes == 0) {
652 INFOS("[LaunchParallelContainer] launching the proxy of the parallel container");
653 int status = system(command.c_str());
655 INFOS("[LaunchParallelContainer] failed : system command status -1");
657 else if (status == 217) {
658 INFOS("[LaunchParallelContainer] failed : system command status 217");
661 int count = TIME_OUT_TO_LAUNCH_CONT;
662 string theMachine(CORBA::string_dup(params.hostname));
663 containerNameInNS = _NS->BuildContainerNameForNS((char*) name.c_str(),theMachine.c_str());
665 INFOS("[LaunchParallelContainer] Waiting for Parallel Container proxy on " << theMachine);
666 while (CORBA::is_nil(obj) && count) {
673 obj = _NS->Resolve(containerNameInNS.c_str());
677 INFOS("[LaunchParallelContainer] launching the nodes of the parallel container");
678 int status = system(command.c_str());
680 INFOS("[LaunchParallelContainer] failed : system command status -1");
682 else if (status == 217) {
683 INFOS("[LaunchParallelContainer] failed : system command status 217");
685 // We are waiting all the nodes
686 for (int i = 0; i < params.nb_component_nodes; i++) {
687 obj = CORBA::Object::_nil();
688 int count = TIME_OUT_TO_LAUNCH_CONT;
693 snprintf(buffer,5,"%d",i);
695 _snprintf(buffer,5,"%d",i);
698 string name_cont = name + string(buffer);
700 // I don't like this...
701 string theMachine(CORBA::string_dup(params.hostname));
702 containerNameInNS = _NS->BuildContainerNameForNS((char*) name_cont.c_str(),theMachine.c_str());
703 cerr << "[LaunchContainer] Waiting for Parllel Container node " << containerNameInNS << " on " << theMachine << endl;
704 while (CORBA::is_nil(obj) && count) {
711 obj = _NS->Resolve(containerNameInNS.c_str());
716 if ( CORBA::is_nil(obj) ) {
717 INFOS("[LaunchParallelContainer] failed");
722 //=============================================================================
724 * Get Id for container: a parallel container registers in Naming Service
725 * on the machine where is process 0. ContainerManager does'nt know the name
726 * of this machine before the launch of the parallel container. So to get
727 * the IOR of the parallel container in Naming Service, ContainerManager
728 * gives a unique Id. The parallel container registers his name under
729 * /ContainerManager/Id directory in NamingService
731 //=============================================================================
734 long SALOME_ContainerManager::GetIdForContainer(void)
740 void SALOME_ContainerManager::fillBatchLaunchedContainers()
742 _batchLaunchedContainers.clear();
743 _NS->Change_Directory("/Containers");
744 vector<string> vec = _NS->list_directory_recurs();
745 for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++){
746 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
747 Engines::Container_ptr cont=Engines::Container::_narrow(obj);
748 if(!CORBA::is_nil(cont)){
749 _batchLaunchedContainers.push_back(cont);
752 _batchLaunchedContainersIter=_batchLaunchedContainers.begin();
755 //=============================================================================
757 * This is no longer valid (C++ container are also python containers)
759 //=============================================================================
761 bool isPythonContainer(const char* ContainerName)
764 int len = strlen(ContainerName);
767 if (strcmp(ContainerName + len - 2, "Py") == 0)
773 //=============================================================================
775 * Builds the script to be launched
777 * If SALOME Application not defined ($APPLI),
778 * see BuildTempFileToLaunchRemoteContainer()
780 * Else rely on distant configuration. Command is under the form (example):
781 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
782 * SALOME_Container containerName &"
784 * - where user is ommited if not specified in CatalogResources,
785 * - where distant path is always relative to user@machine $HOME, and
786 * equal to $APPLI if not specified in CatalogResources,
787 * - where hostNS is the hostname of CORBA naming server (set by scripts to
788 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
789 * - where portNS is the port used by CORBA naming server (set by scripts to
790 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
791 * - where workingdir is the requested working directory for the container.
792 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
794 //=============================================================================
797 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer
798 (const string& machine,
799 const Engines::MachineParameters& params, const long id,const std::string& container_exe)
803 char idc[3*sizeof(long)];
805 if ( ! _isAppliSalomeDefined )
806 command = BuildTempFileToLaunchRemoteContainer(machine, params);
810 const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
814 if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
816 else if ( params.nb_node == 0 )
817 nbproc = params.nb_proc_per_node;
818 else if ( params.nb_proc_per_node == 0 )
819 nbproc = params.nb_node;
821 nbproc = params.nb_node * params.nb_proc_per_node;
824 // "ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
825 // SALOME_Container containerName &"
827 if (resInfo.Protocol == rsh)
829 else if (resInfo.Protocol == ssh)
832 throw SALOME_Exception("Unknown protocol");
834 if (resInfo.UserName != "")
836 command += resInfo.UserName;
843 if (resInfo.AppliPath != "")
844 command += resInfo.AppliPath; // path relative to user@machine $HOME
847 ASSERT(getenv("APPLI"));
848 command += getenv("APPLI"); // path relative to user@machine $HOME
851 command += "/runRemote.sh ";
853 ASSERT(getenv("NSHOST"));
854 command += getenv("NSHOST"); // hostname of CORBA name server
857 ASSERT(getenv("NSPORT"));
858 command += getenv("NSPORT"); // port of CORBA name server
860 std::string wdir=params.workingdir.in();
863 command += " WORKINGDIR ";
865 if(wdir == "$TEMPDIR")
867 command += wdir; // requested working directory
873 command += " mpirun -np ";
874 std::ostringstream o;
878 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
880 command += " SALOME_MPIContainer ";
883 command += " " +container_exe+ " ";
885 command += _NS->ContainerName(params);
887 sprintf(idc,"%ld",id);
890 AddOmninamesParams(command);
892 MESSAGE("command =" << command);
898 //=============================================================================
900 * builds the command to be launched.
902 //=============================================================================
905 SALOME_ContainerManager::BuildCommandToLaunchLocalContainer
906 (const Engines::MachineParameters& params, const long id,const std::string& container_exe)
911 char idc[3*sizeof(long)];
915 command = "mpirun -np ";
917 if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
919 else if ( params.nb_node == 0 )
920 nbproc = params.nb_proc_per_node;
921 else if ( params.nb_proc_per_node == 0 )
922 nbproc = params.nb_node;
924 nbproc = params.nb_node * params.nb_proc_per_node;
926 std::ostringstream o;
932 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
935 if (isPythonContainer(params.container_name))
936 command += "pyMPI SALOME_ContainerPy.py ";
938 command += "SALOME_MPIContainer ";
944 std::string wdir=params.workingdir.in();
947 // a working directory is requested
948 if(wdir == "$TEMPDIR")
950 // a new temporary directory is requested
951 char dir[]="/tmp/salomeXXXXXX";
952 char* mdir=mkdtemp(dir);
954 std::cerr << "Problem in mkdtemp " << dir << " " << mdir << std::endl;
956 command="cd "+std::string(dir)+";";
960 // a permanent directory is requested use it or create it
961 command="mkdir -p " + wdir + " && cd " + wdir + ";";
964 if (isPythonContainer(params.container_name))
965 command += "SALOME_ContainerPy.py ";
967 command += container_exe + " ";
970 command += _NS->ContainerName(params);
972 sprintf(idc,"%ld",id);
975 AddOmninamesParams(command);
977 MESSAGE("Command is ... " << command);
982 //=============================================================================
984 * removes the generated temporary file in case of a remote launch.
986 //=============================================================================
988 void SALOME_ContainerManager::RmTmpFile()
990 if (_TmpFileName != "")
993 string command = "rm ";
995 string command = "del /F ";
997 command += _TmpFileName;
998 char *temp = strdup(command.c_str());
999 int lgthTemp = strlen(temp);
1000 temp[lgthTemp - 3] = '*';
1001 temp[lgthTemp - 2] = '\0';
1007 //=============================================================================
1009 * add to command all options relative to naming service.
1011 //=============================================================================
1013 void SALOME_ContainerManager::AddOmninamesParams(string& command) const
1015 CORBA::String_var iorstr = _NS->getIORaddr();
1016 command += "ORBInitRef NameService=";
1021 //=============================================================================
1023 * add to command all options relative to naming service.
1025 //=============================================================================
1027 void SALOME_ContainerManager::AddOmninamesParams(ofstream& fileStream) const
1029 CORBA::String_var iorstr = _NS->getIORaddr();
1030 fileStream << "ORBInitRef NameService=";
1031 fileStream << iorstr;
1034 //=============================================================================
1036 * generate a file name in /tmp directory
1038 //=============================================================================
1040 string SALOME_ContainerManager::BuildTemporaryFileName() const
1042 //build more complex file name to support multiple salome session
1043 char *temp = new char[19];
1044 strcpy(temp, "/tmp/command");
1045 strcat(temp, "XXXXXX");
1052 itoa(getpid(), aPID, 10);
1056 string command(temp);
1063 //=============================================================================
1065 * Builds in a temporary file the script to be launched.
1067 * Used if SALOME Application ($APPLI) is not defined.
1068 * The command is build with data from CatalogResources, in which every path
1069 * used on remote computer must be defined.
1071 //=============================================================================
1074 SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer
1075 (const string& machine,
1076 const Engines::MachineParameters& params) throw(SALOME_Exception)
1080 _TmpFileName = BuildTemporaryFileName();
1081 ofstream tempOutputFile;
1082 tempOutputFile.open(_TmpFileName.c_str(), ofstream::out );
1083 const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
1084 tempOutputFile << "#! /bin/sh" << endl;
1088 tempOutputFile << "export SALOME_trace=local" << endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1089 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1095 tempOutputFile << "mpirun -np ";
1098 if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
1100 else if ( params.nb_node == 0 )
1101 nbproc = params.nb_proc_per_node;
1102 else if ( params.nb_proc_per_node == 0 )
1103 nbproc = params.nb_node;
1105 nbproc = params.nb_node * params.nb_proc_per_node;
1107 std::ostringstream o;
1109 tempOutputFile << nbproc << " ";
1111 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1115 tempOutputFile << getenv("KERNEL_ROOT_DIR") << "/bin/salome/";
1119 if (isPythonContainer(params.container_name))
1120 tempOutputFile << "pyMPI SALOME_ContainerPy.py ";
1122 tempOutputFile << "SALOME_MPIContainer ";
1127 if (isPythonContainer(params.container_name))
1128 tempOutputFile << "SALOME_ContainerPy.py ";
1130 tempOutputFile << "SALOME_Container ";
1133 tempOutputFile << _NS->ContainerName(params) << " -";
1134 AddOmninamesParams(tempOutputFile);
1135 tempOutputFile << " &" << endl;
1136 tempOutputFile.flush();
1137 tempOutputFile.close();
1138 chmod(_TmpFileName.c_str(), 0x1ED);
1140 // --- Build command
1144 if (resInfo.Protocol == rsh)
1147 string commandRcp = "rcp ";
1148 commandRcp += _TmpFileName;
1150 commandRcp += machine;
1152 commandRcp += _TmpFileName;
1153 status = system(commandRcp.c_str());
1156 else if (resInfo.Protocol == ssh)
1159 string commandRcp = "scp ";
1160 commandRcp += _TmpFileName;
1162 commandRcp += machine;
1164 commandRcp += _TmpFileName;
1165 status = system(commandRcp.c_str());
1168 throw SALOME_Exception("Unknown protocol");
1171 throw SALOME_Exception("Error of connection on remote host");
1174 _CommandForRemAccess = command;
1176 command += _TmpFileName;
1184 //=============================================================================
1185 /*! Creates a command line that the container manager uses to launch
1186 * a parallel container.
1188 //=============================================================================
1190 SALOME_ContainerManager::BuildCommandToLaunchLocalParallelContainer(const std::string& exe_name,
1191 const Engines::MachineParameters& params,
1192 const std::string& log)
1194 // This method knows the differences between the proxy and the nodes.
1195 // nb_component_nodes is not used in the same way if it is a proxy or
1199 string parallelLib(CORBA::string_dup(params.parallelLib));
1200 string hostname(CORBA::string_dup(params.hostname));
1201 int par = exe_name.find("Proxy");
1202 int nbproc = params.nb_component_nodes;
1204 sprintf(buffer,"%d",nbproc);
1206 Engines::MachineParameters_var rtn = new Engines::MachineParameters();
1207 rtn->container_name = params.container_name;
1208 rtn->hostname = params.hostname;
1209 rtn->OS = params.OS;
1210 rtn->mem_mb = params.mem_mb;
1211 rtn->cpu_clock = params.cpu_clock;
1212 rtn->nb_proc_per_node = params.nb_proc_per_node;
1213 rtn->nb_node = params.nb_node;
1214 rtn->isMPI = params.isMPI;
1216 string real_exe_name = exe_name + parallelLib;
1218 if (parallelLib == "Dummy")
1220 //command = "gdb --args ";
1221 //command = "valgrind --tool=memcheck --log-file=val_log ";
1222 //command += real_exe_name;
1224 command = real_exe_name;
1226 command += " " + _NS->ContainerName(rtn);
1227 command += " " + parallelLib;
1228 command += " " + hostname;
1230 AddOmninamesParams(command);
1233 else if (parallelLib == "Mpi")
1235 // Step 1 : check if MPI is started
1236 if (_MpiStarted == false)
1245 command = "mpiexec -np " + string(buffer) + " ";
1246 // command += "gdb --args ";
1247 command += real_exe_name;
1248 command += " " + _NS->ContainerName(rtn);
1249 command += " " + parallelLib;
1250 command += " " + hostname;
1252 AddOmninamesParams(command);
1257 command = "mpiexec -np 1 ";
1258 command += real_exe_name;
1259 command += " " + _NS->ContainerName(rtn);
1260 command += " " + string(buffer);
1261 command += " " + parallelLib;
1262 command += " " + hostname;
1264 AddOmninamesParams(command);
1269 std::string message("Unknown parallelLib" + parallelLib);
1270 throw SALOME_Exception(message.c_str());
1274 if (log == "default")
1276 command += " > /tmp/";
1277 command += _NS->ContainerName(rtn);
1279 command += GetHostname();
1281 command += getenv( "USER" ) ;
1282 command += ".log 2>&1 &" ;
1286 command = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH; "
1287 + command + " \" &";
1288 // + command + "; echo $LD_LIBRARY_PATH; cat \" &";
1292 /* if (log == "xterm")
1294 command = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH; echo $LD_LIBRARY_PATH; echo $PATH; " + command + "; cat \" &";
1297 /* command = "cd ; rm " + fichier_commande + "; touch " + \
1298 fichier_commande + "; echo \" export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; " + \
1299 command + " >& /tmp/ribes_" + fichier_commande + " & \" > " + fichier_commande + ";";
1300 command += "ssh cn01 sh " + fichier_commande + " &";
1301 cerr << "La commande : " << command << endl;
1305 void SALOME_ContainerManager::startMPI()
1307 cerr << "----------------------------------------------" << endl;
1308 cerr << "----------------------------------------------" << endl;
1309 cerr << "----------------------------------------------" << endl;
1310 cerr << "-Only Lam on Localhost is currently supported-" << endl;
1311 cerr << "----------------------------------------------" << endl;
1312 cerr << "----------------------------------------------" << endl;
1313 cerr << "----------------------------------------------" << endl;
1315 int status = system("lamboot");
1318 INFOS("lamboot failed : system command status -1");
1320 else if (status == 217)
1322 INFOS("lamboot failed : system command status 217");