Salome HOME
2584f70bacb6374a30d29e2221cbc7fa50655bc9
[modules/kernel.git] / src / Container / SALOME_ContainerManager.hxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef __SALOME_CONTAINERMANAGER_HXX__
24 #define __SALOME_CONTAINERMANAGER_HXX__
25
26 #include "SALOME_Container.hxx"
27
28 #include <SALOMEconfig.h>
29 #include CORBA_CLIENT_HEADER(SALOME_Component)
30 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
31 #include "SALOME_ResourcesCatalog_Parser.hxx"
32
33 #include "Utils_Mutex.hxx"
34
35 #include <vector>
36 #include <string>
37 #include <utility>
38 #include <functional>
39 #include <set>
40
41 class SALOME_NamingService_Abstract;
42 class SALOME_ResourcesManager_Client;
43
44 class CONTAINER_EXPORT SALOME_ContainerManager : public POA_Engines::ContainerManager
45 {
46
47 public:
48   SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService_Abstract *ns);
49   ~SALOME_ContainerManager();
50
51   // Corba Methods
52   Engines::Container_ptr GiveContainer(const Engines::ContainerParameters& params) override;
53
54   void ShutdownContainers() override;
55
56   void ShutdownContainersNow() override;
57
58   void SetOverrideEnvForContainers(const Engines::KeyValDict& env) override;
59
60   Engines::KeyValDict *GetOverrideEnvForContainers() override;
61
62   void SetCodeOnContainerStartUp(const char *code) override;
63
64   // C++ Methods
65   void Shutdown();
66
67   void DeclareUsingSalomeSession() { _isSSL = false; }
68
69   CORBA::Long GetTimeOutToLaunchServerInSecond() override;
70
71   void SetTimeOutToLaunchServerInSecond(CORBA::Long timeInSecond) override;
72
73   CORBA::Long GetDeltaTimeBetweenNSLookupAtLaunchTimeInMilliSecond() override;
74
75   void SetDeltaTimeBetweenNSLookupAtLaunchTimeInMilliSecond(CORBA::Long timeInMS) override;
76
77   CORBA::Long GetDeltaTimeBetweenCPUMemMeasureInMilliSecond() override;
78
79   void SetDeltaTimeBetweenCPUMemMeasureInMilliSecond(CORBA::Long timeInMS) override;
80
81   void SetBigObjOnDiskThreshold(CORBA::Long thresholdInByte) override;
82
83   void SetBigObjOnDiskDirectory(const char *directory) override;
84
85   static const char *_ContainerManagerNameInNS;
86
87 private:
88
89   void ShutdownContainersGeneric(std::function<void(Engines::Container_ptr)> funcToBeCalledOnContainer);
90
91 protected:
92   // C++ methods
93   Engines::Container_ptr
94   FindContainer(const Engines::ContainerParameters& params,
95                 const Engines::ResourceList& possibleResources);
96
97   Engines::Container_ptr
98   FindContainer(const Engines::ContainerParameters& params,
99                 const std::string& resource);
100
101   std::string GetCppBinaryOfKernelSSLContainer() const;
102
103   std::string GetCppBinaryOfKernelContainer() const;
104   
105   std::string GetRunRemoteExecutableScript() const;
106
107   std::string BuildCommandToLaunchRemoteContainer(const std::string & resource_name,
108                                                   const Engines::ContainerParameters& params,
109                                                   const std::string& container_exe="SALOME_Container") const;
110
111   std::string BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params,
112                                                  const std::string& machinesFile,
113                                                  const std::string& container_exe,//"SALOME_Container"
114                                                  std::string& tmpFileName) const;
115
116   std::string BuildTempFileToLaunchRemoteContainer(const std::string& resource_name,
117                                                    const Engines::ContainerParameters& params, std::string& tmpFileName) const;
118
119   static void RmTmpFile(std::string& tmpFile);
120
121   void AddOmninamesParams(std::string& command) const;
122
123   void AddOmninamesParams(std::ostream& fileStream) const;
124
125   static std::string BuildTemporaryFileName();
126
127   std::string GetMPIZeroNode(const std::string machine, const std::string machinesFile) const;
128
129   std::string machinesFile(const int nbproc);
130   
131   std::string getCommandToRunRemoteProcessCommon(const std::string& templateName, const std::string& remoteScript, 
132                                           AccessProtocolType protocol, 
133                                           const std::string & hostname, 
134                                           const std::string & username,
135                                           const std::string & applipath,
136                                           const std::string & workdir) const;
137
138   std::string getCommandToRunRemoteProcess(AccessProtocolType protocol, 
139                                           const std::string & hostname, 
140                                           const std::string & username,
141                                           const std::string & applipath,
142                                           const std::string & workdir="") const;
143                                           
144   std::string getCommandToRunRemoteProcessNoAppli(AccessProtocolType protocol, 
145                                           const std::string & hostname, 
146                                           const std::string & username,
147                                           const std::string & applipath,
148                                           const std::string & workdir="") const;
149
150   Engines::Container_ptr
151   LaunchContainer(const Engines::ContainerParameters& params,
152                   const std::string & resource_selected,
153                   const std::string & hostname,
154                   const std::string & machFile,
155                   const std::string & containerNameInNS);
156
157   CORBA::ORB_var _orb;
158   PortableServer::POA_var _poa;
159
160   SALOME_ResourcesManager_Client *_resManager;
161   SALOME_NamingService_Abstract *_NS;
162
163   //! different behaviour if $APPLI exists (SALOME Application)
164   bool _isAppliSalomeDefined;
165
166   //! attribute that contains the number of processes used in batch mode by MPI containers
167   int _nbprocUsed;
168
169   //! attribute that specifies the launch mode.
170   bool _isSSL = true;
171
172   static omni_mutex _numInstanceMutex ; // lib and instance protection
173
174   //! attributes to allow concurrency for // GiveContainer
175   Utils_Mutex _giveContainerMutex1;
176
177   pid_t _pid_mpiServer;
178
179   // Begin of PacO++ Parallel extension
180   typedef std::vector<std::string> actual_launch_machine_t;
181
182   bool checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected);
183
184   Engines::Container_ptr
185   StartPaCOPPContainer(const Engines::ContainerParameters& params, std::string resource_selected);
186
187
188
189   std::string BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
190                                                      std::string machine_file_name,
191                                                      std::string & proxy_hostname);
192
193   std::string BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
194                                                     const std::string & machine_file_name,
195                                                     SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
196                                                     const std::string & proxy_hostname);
197
198   void LogConfiguration(const std::string & log_type,
199                         const std::string & exe_type,
200                         const std::string & container_name,
201                         const std::string & hostname,
202                         std::string & begin,
203                         std::string & end);
204
205   CORBA::Object_ptr
206   LaunchPaCOProxyContainer(const std::string& command,
207                            const Engines::ContainerParameters& params,
208                            const std::string& hostname);
209
210   bool
211   LaunchPaCONodeContainer(const std::string& command,
212                           const Engines::ContainerParameters& params,
213                           const std::string& name,
214                           SALOME_ContainerManager::actual_launch_machine_t & vect_machine);
215   // End of PaCO++ Parallel extension
216 public:
217   static char *GetenvThreadSafe(const char *name);
218   static std::string GetenvThreadSafeAsString(const char *name);
219   static int SystemThreadSafe(const char *command);
220   static long SystemWithPIDThreadSafe(const std::vector<std::string>& command);
221   static void AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService_Abstract *ns);
222   static void MakeTheCommandToBeLaunchedASync(std::string& command);
223   static int GetTimeOutToLoaunchServer();
224   static void SleepInSecond(int ellapseTimeInSecond);
225  private:
226   static const int TIME_OUT_TO_LAUNCH_CONT;
227   static const int DFT_DELTA_TIME_NS_LOOKUP_IN_MS;
228   static Utils_Mutex _getenvMutex;
229   static Utils_Mutex _systemMutex;
230 private:
231   std::vector< std::pair<std::string, std::string> > _override_env;
232   int _time_out_in_second;
233   int _delta_time_ns_lookup_in_ms;
234   int _delta_time_measure_in_ms;
235   std::string _code_to_exe_on_startup;
236 };
237 #endif