]> SALOME platform Git repositories - modules/kernel.git/blob - src/Basics/KernelBasis.cxx
Salome HOME
[EDF30057] : Management of SSD non shared fs for out of core management.
[modules/kernel.git] / src / Basics / KernelBasis.cxx
1 // Copyright (C) 2021-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "KernelBasis.hxx"
21
22 #include <sstream>
23 #include <stdexcept>
24
25 static bool DEFAULT_SSL_MODE = true;
26 static bool GUI_MODE = false;
27
28 // IOR of SALOME_Embedded_NamingService servant
29 static std::string IOR_OF_EMBEDDED_NS;
30
31 bool getSSLMode()
32 {
33   return DEFAULT_SSL_MODE;
34 }
35
36 void setSSLMode(bool sslMode)
37 {
38   DEFAULT_SSL_MODE = sslMode;
39 }
40
41 bool getGUIMode()
42 {
43   return GUI_MODE;
44 }
45
46 void setGUIMode(bool guiMode)
47 {
48   GUI_MODE = guiMode;
49 }
50
51 std::string getIOROfEmbeddedNS()
52 {
53   return IOR_OF_EMBEDDED_NS;
54 }
55
56 void setIOROfEmbeddedNS(const std::string& ior)
57 {
58   IOR_OF_EMBEDDED_NS = ior;
59 }
60
61 #include <iostream>
62
63 /*!
64  * Callable from Python in case if sys.stdout is not connected to tty
65  */
66 void WriteInStdout(const std::string& msg)
67 {
68   std::cout << msg << std::endl << std::flush;
69 }
70
71 /*!
72  * Callable from Python in case if sys.stdout is not connected to tty
73  */
74 void WriteInStderr(const std::string& msg)
75 {
76   std::cerr << msg << std::endl << std::flush;
77 }
78
79 namespace SALOME
80 {
81   static constexpr char IN_PROCESS_VALUE = 0;
82   static constexpr char IN_PROCESS_VALUE_STR[] = "InProcess";
83   static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE = 1;
84   static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE_STR[] = "OutOfProcessNoReplay";
85   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE = 2;
86   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR[] = "OutOfProcessWithReplay";
87   static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE = 3;
88   static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR[] = "OutOfProcessNoReplayFT";
89   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE = 4;
90   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR[] = "OutOfProcessWithReplayFT";
91
92   static PyExecutionMode FromIntToPyExecutionMode(char value)
93   {
94     switch(value)
95     {
96       case IN_PROCESS_VALUE:
97         return PyExecutionMode::InProcess;
98       case OUT_OF_PROCESS_NO_REPLAY_VALUE:
99         return PyExecutionMode::OutOfProcessNoReplay;
100       case OUT_OF_PROCESS_WITH_REPLAY_VALUE:
101         return PyExecutionMode::OutOfProcessWithReplay;
102       case OUT_OF_PROCESS_NO_REPLAY_FT_VALUE:
103         return PyExecutionMode::OutOfProcessNoReplayFT;
104       case OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE:
105         return PyExecutionMode::OutOfProcessWithReplayFT;
106     }
107     throw std::range_error("FromIntToPyExecutionMode : Invalid value for Py Execution Mode ! Must be in 0 (InProcess), 1 (OutOfProcessNoReplay) or 2 (OutOfProcessWithReplay) !");
108   }
109   
110   static PyExecutionMode FromStrToPyExecutionMode(const std::string& value)
111   {
112     if(value == IN_PROCESS_VALUE_STR)
113       return PyExecutionMode::InProcess;
114     if(value == OUT_OF_PROCESS_NO_REPLAY_VALUE_STR)
115       return PyExecutionMode::OutOfProcessNoReplay;
116     if(value == OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR)
117       return PyExecutionMode::OutOfProcessWithReplay;
118     if(value == OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR)
119       return PyExecutionMode::OutOfProcessNoReplayFT;
120     if(value == OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR)
121       return PyExecutionMode::OutOfProcessWithReplayFT;
122     throw std::range_error("FromStrToPyExecutionMode : Invalid str value for py execution mode !");
123   }
124
125   static std::string FromExecutionModeToStr(PyExecutionMode execMode)
126   {
127     switch(execMode)
128     {
129       case PyExecutionMode::InProcess:
130         return IN_PROCESS_VALUE_STR;
131       case PyExecutionMode::OutOfProcessNoReplay:
132         return OUT_OF_PROCESS_NO_REPLAY_VALUE_STR;
133       case PyExecutionMode::OutOfProcessWithReplay:
134         return OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR;
135       case PyExecutionMode::OutOfProcessNoReplayFT:
136         return OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR;
137       case PyExecutionMode::OutOfProcessWithReplayFT:
138         return OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR;
139       default:
140         throw std::range_error("FromExecutionModeToStr : Invalid str value for py execution mode !");
141     }
142   }
143 }
144
145 constexpr int SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000;
146
147 static int SALOME_BIG_OBJ_ON_DISK_THRES = SALOME_BIG_OBJ_ON_DISK_THRES_DFT;
148
149 int SALOME::GetBigObjOnDiskThreshold()
150 {
151   return SALOME_BIG_OBJ_ON_DISK_THRES;
152 }
153
154 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
155 {
156   SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
157 }
158
159 constexpr char SALOME_FILE_BIG_OBJ_DIR_SEP = '@';
160
161 static std::string SALOME_FILE_BIG_OBJ_DIR;
162
163 constexpr int DFT_SALOME_NB_RETRY = 1;
164
165 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
166
167 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
168 {
169   switch( protocol )
170   {
171     case SALOME::SHARED_FILE_SYSTEM_PROTOCOL:
172       return SALOME::BigObjTransferProtocol::SharedFileSystem;
173     case SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL:
174       return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
175     default:
176       throw std::runtime_error("FromIntToBigObjOnDiskProtocol unrecognized protocol ! should be in [0,1] !");
177   }
178 }
179
180 SALOME::BigObjTransferProtocol SALOME::BigObjOnDiskProtocolFromStr(const std::string& protocol)
181 {
182   if( protocol == SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR )
183     return SALOME::BigObjTransferProtocol::SharedFileSystem;
184   if( protocol == SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR )
185     return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
186   throw std::runtime_error("BigObjOnDiskProtocolFromStr unrecognized protocol !");
187 }
188
189 std::string SALOME::BigObjOnDiskProtocolToStr(BigObjTransferProtocol protocol)
190 {
191   switch( protocol )
192   {
193     case SALOME::BigObjTransferProtocol::SharedFileSystem:
194       return SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR;
195     case SALOME::BigObjTransferProtocol::SSDCopyFileSystem:
196       return SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR;
197     default:
198       throw std::runtime_error("BigObjOnDiskProtocolToStr unrecognized protocol ! should be in [0,1] !");
199   }
200 }
201
202 /*!
203  * This method returns the protocol of proxy transfert and the directory
204  */
205 SALOME::BigObjTransferProtocol SALOME::GetBigObjOnDiskProtocolAndDirectory(std::string& directory)
206 {
207   if(SALOME_FILE_BIG_OBJ_DIR.size() < 3)
208   {
209     directory = SALOME_FILE_BIG_OBJ_DIR;
210     return SALOME::BigObjTransferProtocol::SharedFileSystem;
211   }
212   std::string protocol = SALOME_FILE_BIG_OBJ_DIR.substr(0,3);
213   directory = SALOME_FILE_BIG_OBJ_DIR.substr(3);
214   if( protocol[0]!=SALOME_FILE_BIG_OBJ_DIR_SEP || protocol[2]!=SALOME_FILE_BIG_OBJ_DIR_SEP)
215   {
216     directory = SALOME_FILE_BIG_OBJ_DIR;
217     return SALOME::BigObjTransferProtocol::SharedFileSystem;
218   }
219   std::istringstream iss(protocol.substr(1,1)); iss.exceptions(std::istringstream::failbit | std::istringstream::badbit);
220   short iproxyprot = 0;
221   iss >> iproxyprot;
222   return FromIntToBigObjOnDiskProtocol( iproxyprot );
223 }
224
225 std::string SALOME::GetBigObjOnDiskDirectoryCoarse()
226 {
227   return SALOME_FILE_BIG_OBJ_DIR;
228 }
229
230 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
231 {
232   SALOME_FILE_BIG_OBJ_DIR = directory;
233 }
234
235 bool SALOME::BigObjOnDiskDirectoryDefined()
236 {
237   return ! SALOME_FILE_BIG_OBJ_DIR.empty();
238 }
239
240 void SALOME::SetNumberOfRetry(int nbRetry)
241 {
242   SALOME_NB_RETRY = nbRetry;
243 }
244
245 int SALOME::GetNumberOfRetry()
246 {
247   return SALOME_NB_RETRY;
248 }
249
250 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
251
252 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
253 {
254   DefaultPyExecMode = mode;
255 }
256
257 void SALOME::SetPyExecutionModeStr(const std::string& mode)
258 {
259   SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
260 }
261
262 std::vector<std::string> SALOME::GetAllPyExecutionModes()
263 {
264   return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
265 }
266
267 std::string SALOME::GetPyExecutionModeStr()
268 {
269   return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
270 }
271
272 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
273 {
274   auto isEnvVarSet = []() -> SALOME::PyExecutionMode
275   {
276     const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
277     if (envVar && (envVar[0] != '\0'))
278     {
279       const int numValue = std::stoi(envVar);
280       return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
281     }
282     return SALOME::PyExecutionMode::InProcess;
283   };
284   if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
285     DefaultPyExecMode = isEnvVarSet();
286   return DefaultPyExecMode;
287 }