1 | |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "cxios.hpp" |
---|
4 | #include "client.hpp" |
---|
5 | #include "server.hpp" |
---|
6 | #include "xml_parser.hpp" |
---|
7 | #include "mpi.hpp" |
---|
8 | #include "memory.hpp" |
---|
9 | #include <new> |
---|
10 | #include "memtrack.hpp" |
---|
11 | #include "registry.hpp" |
---|
12 | #include "ressources_manager.hpp" |
---|
13 | #include "services_manager.hpp" |
---|
14 | #include "servers_ressource.hpp" |
---|
15 | #include "notifications_manager.hpp" |
---|
16 | #include "mem_checker.hpp" |
---|
17 | #include <unistd.h> |
---|
18 | |
---|
19 | namespace xios |
---|
20 | { |
---|
21 | string CXios::rootFile="./iodef.xml" ; |
---|
22 | string CXios::xiosCodeId="xios.x" ; |
---|
23 | string CXios::clientFile="./xios_client"; |
---|
24 | string CXios::serverFile="./xios_server"; |
---|
25 | string CXios::serverPrmFile="./xios_server1"; |
---|
26 | string CXios::serverSndFile="./xios_server2"; |
---|
27 | const string CXios::defaultPoolId="default_pool_id" ; |
---|
28 | const string CXios::defaultServerId="default_server_id" ; |
---|
29 | const string CXios::defaultWriterId="default_writer_id" ; |
---|
30 | const string CXios::defaultReaderId="default_reader_id" ; |
---|
31 | const string CXios::defaultGathererId="default_gatherer_id" ; |
---|
32 | const string CXios::defaultServicesId="default_services_id" ; |
---|
33 | |
---|
34 | bool CXios::xiosStack = true; |
---|
35 | bool CXios::systemStack = false; |
---|
36 | |
---|
37 | bool CXios::isClient ; |
---|
38 | bool CXios::isServer ; |
---|
39 | |
---|
40 | MPI_Comm CXios::globalComm ; |
---|
41 | MPI_Comm CXios::xiosComm ; |
---|
42 | |
---|
43 | bool CXios::usingOasis ; |
---|
44 | bool CXios::usingServer = false; |
---|
45 | bool CXios::usingServer2 = false; |
---|
46 | int CXios::ratioServer2 = 50; |
---|
47 | int CXios::nbPoolsServer2 = 1; |
---|
48 | double CXios::bufferSizeFactor = 1.0; |
---|
49 | const double CXios::defaultBufferSizeFactor = 1.0; |
---|
50 | StdSize CXios::minBufferSize = 64 * sizeof(double); |
---|
51 | StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ; |
---|
52 | bool CXios::printLogs2Files; |
---|
53 | bool CXios::isOptPerformance = true; |
---|
54 | CRegistry* CXios::globalRegistry = 0; |
---|
55 | double CXios::recvFieldTimeout = 300.0; |
---|
56 | bool CXios::checkEventSync=false ; |
---|
57 | bool CXios::checkSumRecv=false ; |
---|
58 | bool CXios::checkSumSend=false ; |
---|
59 | bool CXios::logMemory=false ; |
---|
60 | bool CXios::reportMemory=true ; |
---|
61 | bool CXios::servicesUseWindowManager=true ; |
---|
62 | |
---|
63 | CDaemonsManager* CXios::daemonsManager_=nullptr ; |
---|
64 | CRessourcesManager* CXios::ressourcesManager_=nullptr ; |
---|
65 | CServicesManager* CXios::servicesManager_=nullptr ; |
---|
66 | CContextsManager* CXios::contextsManager_=nullptr ; |
---|
67 | CCouplerManager* CXios::couplerManager_=nullptr ; |
---|
68 | CRegistryManager* CXios::registryManager_=nullptr ; |
---|
69 | CNotificationsManager* CXios::notificationsManager_=nullptr ; |
---|
70 | |
---|
71 | CMpiGarbageCollector CXios::MpiGarbageCollector_ ; |
---|
72 | |
---|
73 | //! Parse configuration file and create some objects from it |
---|
74 | void CXios::initialize() |
---|
75 | { |
---|
76 | set_new_handler(noMemory); |
---|
77 | char startPath[256]; |
---|
78 | getcwd(startPath, sizeof(startPath)); |
---|
79 | if(const char* userPath = std::getenv("XIOS_IODEF_PATH")) |
---|
80 | { |
---|
81 | if ( chdir( userPath ) != 0) |
---|
82 | { |
---|
83 | ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl ); |
---|
84 | } |
---|
85 | } |
---|
86 | parseFile(rootFile); |
---|
87 | parseXiosConfig(); |
---|
88 | chdir( startPath ); |
---|
89 | } |
---|
90 | |
---|
91 | /*! |
---|
92 | \brief Parse xios part of configuration file (.iodef.xml) |
---|
93 | Both client and server need information returned from this function |
---|
94 | */ |
---|
95 | void CXios::parseXiosConfig() |
---|
96 | { |
---|
97 | usingOasis=getin<bool>("using_oasis",false) ; |
---|
98 | usingServer=getin<bool>("using_server",false) ; |
---|
99 | usingServer2=getin<bool>("using_server2",false) ; |
---|
100 | ratioServer2=getin<int>("ratio_server2",50); |
---|
101 | nbPoolsServer2=getin<int>("number_pools_server2",0); |
---|
102 | info.setLevel(getin<int>("info_level",0)) ; |
---|
103 | report.setLevel(getin<int>("info_level",50)); |
---|
104 | printLogs2Files=getin<bool>("print_file",false); |
---|
105 | |
---|
106 | xiosStack=getin<bool>("xios_stack",true) ; |
---|
107 | systemStack=getin<bool>("system_stack",false) ; |
---|
108 | if (xiosStack && systemStack) |
---|
109 | { |
---|
110 | xiosStack = false; |
---|
111 | } |
---|
112 | |
---|
113 | StdString bufMemory("memory"); |
---|
114 | StdString bufPerformance("performance"); |
---|
115 | StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance); |
---|
116 | std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower); |
---|
117 | if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false; |
---|
118 | else if (0 != bufOpt.compare(bufPerformance)) |
---|
119 | { |
---|
120 | ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl ); |
---|
121 | } |
---|
122 | |
---|
123 | bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor); |
---|
124 | minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double)); |
---|
125 | maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max()); |
---|
126 | recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout); |
---|
127 | if (recvFieldTimeout < 0.0) |
---|
128 | ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative."); |
---|
129 | |
---|
130 | checkEventSync = getin<bool>("check_event_sync", checkEventSync); |
---|
131 | |
---|
132 | checkSumSend = getin<bool>("checksum_send_fields", false); |
---|
133 | checkSumRecv = getin<bool>("checksum_recv_fields", false); |
---|
134 | |
---|
135 | logMemory = getin<bool>("log_memory", false); |
---|
136 | reportMemory = getin<bool>("memory_report", true); |
---|
137 | servicesUseWindowManager = getin<bool>("services_use_window_manager", true); |
---|
138 | |
---|
139 | globalComm=MPI_COMM_WORLD ; |
---|
140 | } |
---|
141 | |
---|
142 | /*! |
---|
143 | Initialize client |
---|
144 | \param [in] codeId identity of context |
---|
145 | \param [in] localComm local communicator |
---|
146 | \param [in/out] returnComm communicator corresponding to group of client with same codeId |
---|
147 | */ |
---|
148 | void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
149 | TRY |
---|
150 | { |
---|
151 | initialize() ; |
---|
152 | |
---|
153 | isClient = true; |
---|
154 | |
---|
155 | //CClient::initialize(codeId,localComm,returnComm) ; |
---|
156 | CClient::initialize(codeId,localComm,returnComm) ; |
---|
157 | |
---|
158 | // If there are no server processes then we are in attached mode |
---|
159 | // and the clients are also servers |
---|
160 | isServer = !usingServer; |
---|
161 | |
---|
162 | if (printLogs2Files) |
---|
163 | { |
---|
164 | CClient::openInfoStream(clientFile); |
---|
165 | CClient::openErrorStream(clientFile); |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | CClient::openInfoStream(); |
---|
170 | CClient::openErrorStream(); |
---|
171 | } |
---|
172 | CMemChecker::logMem("CXios::initClientSide"); |
---|
173 | } |
---|
174 | CATCH |
---|
175 | |
---|
176 | void CXios::clientFinalize(void) |
---|
177 | { |
---|
178 | CMemChecker::logMem("CXios::clientFinalize", true); |
---|
179 | |
---|
180 | CClient::finalize() ; |
---|
181 | |
---|
182 | #ifdef XIOS_MEMTRACK |
---|
183 | |
---|
184 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
185 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
186 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
187 | #endif |
---|
188 | |
---|
189 | #ifdef XIOS_MEMTRACK_FULL |
---|
190 | report(0) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
191 | report(0) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
192 | |
---|
193 | ofstream memReport ; |
---|
194 | std::filebuf* fb = memReport.rdbuf(); |
---|
195 | CClient::openStream(clientFile, ".mem", fb); |
---|
196 | |
---|
197 | MemTrack::TrackListMemoryUsage() ; |
---|
198 | size_t memtrack_blocks=0 ; |
---|
199 | memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ; |
---|
200 | size_t memtrack_size=0 ; |
---|
201 | memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ; |
---|
202 | MemTrack::TrackDumpBlocks(memReport, memtrack_blocks,memtrack_size); |
---|
203 | memReport.close(); |
---|
204 | #endif |
---|
205 | |
---|
206 | CClient::closeInfoStream(); |
---|
207 | |
---|
208 | #endif |
---|
209 | } |
---|
210 | |
---|
211 | //! Init server by parsing only xios part of config file |
---|
212 | void CXios::initServer() |
---|
213 | { |
---|
214 | set_new_handler(noMemory); |
---|
215 | std::set<StdString> parseList; |
---|
216 | parseList.insert("xios"); |
---|
217 | char startPath[256]; |
---|
218 | getcwd(startPath, sizeof(startPath)); |
---|
219 | if(const char* userPath = std::getenv("XIOS_IODEF_PATH")) |
---|
220 | { |
---|
221 | if ( chdir( userPath ) != 0) |
---|
222 | { |
---|
223 | ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl ); |
---|
224 | } |
---|
225 | } |
---|
226 | xml::CXMLParser::ParseFile(rootFile, parseList); |
---|
227 | parseXiosConfig(); |
---|
228 | chdir( startPath ); |
---|
229 | } |
---|
230 | |
---|
231 | //! Initialize server then put it into listening state |
---|
232 | void CXios::initServerSide(void) |
---|
233 | { |
---|
234 | CMemChecker::get("xios").resume() ; |
---|
235 | initServer(); |
---|
236 | isClient = false; |
---|
237 | isServer = true; |
---|
238 | |
---|
239 | // Initialize all aspects MPI |
---|
240 | CServer::initialize(); |
---|
241 | CServer::finalize(); |
---|
242 | |
---|
243 | #ifdef XIOS_MEMTRACK |
---|
244 | |
---|
245 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
246 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
247 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
248 | #endif |
---|
249 | |
---|
250 | #ifdef XIOS_MEMTRACK_FULL |
---|
251 | report(0) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
252 | report(0) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
253 | ofstream memReport ; |
---|
254 | std::filebuf* fb = memReport.rdbuf(); |
---|
255 | CClient::openStream(serverFile, ".mem", fb); |
---|
256 | |
---|
257 | MemTrack::TrackListMemoryUsage() ; |
---|
258 | size_t memtrack_blocks=0 ; |
---|
259 | memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ; |
---|
260 | size_t memtrack_size=0 ; |
---|
261 | memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ; |
---|
262 | MemTrack::TrackDumpBlocks(memReport,memtrack_blocks,memtrack_size); |
---|
263 | memReport.close() ; |
---|
264 | #endif |
---|
265 | #endif |
---|
266 | CMemChecker::get("xios").suspend() ; |
---|
267 | CServer::closeInfoStream(); |
---|
268 | } |
---|
269 | |
---|
270 | //! Parse configuration file |
---|
271 | void CXios::parseFile(const string& filename) |
---|
272 | { |
---|
273 | xml::CXMLParser::ParseFile(filename); |
---|
274 | } |
---|
275 | |
---|
276 | //! Set using server |
---|
277 | void CXios::setUsingServer() |
---|
278 | { |
---|
279 | usingServer = true; |
---|
280 | } |
---|
281 | |
---|
282 | //! Unset using server |
---|
283 | void CXios::setNotUsingServer() |
---|
284 | { |
---|
285 | usingServer = false; |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | void CXios::launchRegistryManager(bool isXiosServer) |
---|
290 | { |
---|
291 | registryManager_ = new CRegistryManager(isXiosServer) ; |
---|
292 | } |
---|
293 | |
---|
294 | void CXios::launchRessourcesManager(bool isXiosServer) |
---|
295 | { |
---|
296 | ressourcesManager_ = new CRessourcesManager(isXiosServer) ; |
---|
297 | } |
---|
298 | |
---|
299 | void CXios::launchCouplerManager(bool isXiosServer) |
---|
300 | { |
---|
301 | couplerManager_ = new CCouplerManager(isXiosServer) ; |
---|
302 | } |
---|
303 | |
---|
304 | void CXios::launchServicesManager(bool isXiosServer) |
---|
305 | { |
---|
306 | servicesManager_ = new CServicesManager(isXiosServer) ; |
---|
307 | } |
---|
308 | |
---|
309 | void CXios::launchContextsManager(bool isXiosServer) |
---|
310 | { |
---|
311 | contextsManager_ = new CContextsManager(isXiosServer) ; |
---|
312 | } |
---|
313 | |
---|
314 | void CXios::launchDaemonsManager(bool isXiosServer) |
---|
315 | { |
---|
316 | daemonsManager_ = new CDaemonsManager(isXiosServer) ; |
---|
317 | } |
---|
318 | |
---|
319 | void CXios::launchNotificationsManager(bool isMaster) |
---|
320 | { |
---|
321 | notificationsManager_ = new CNotificationsManager(isMaster) ; |
---|
322 | } |
---|
323 | |
---|
324 | void CXios::launchThreadManager(bool isXiosServer) |
---|
325 | { |
---|
326 | CThreadManager::initialize(isXiosServer) ; |
---|
327 | } |
---|
328 | |
---|
329 | void CXios::finalizeRegistryManager() |
---|
330 | { |
---|
331 | delete registryManager_; |
---|
332 | } |
---|
333 | |
---|
334 | void CXios::finalizeRessourcesManager() |
---|
335 | { |
---|
336 | delete ressourcesManager_; |
---|
337 | } |
---|
338 | |
---|
339 | void CXios::finalizeCouplerManager() |
---|
340 | { |
---|
341 | delete couplerManager_; |
---|
342 | } |
---|
343 | |
---|
344 | void CXios::finalizeServicesManager() |
---|
345 | { |
---|
346 | delete servicesManager_ ; |
---|
347 | } |
---|
348 | |
---|
349 | void CXios::finalizeContextsManager() |
---|
350 | { |
---|
351 | delete contextsManager_ ; |
---|
352 | } |
---|
353 | |
---|
354 | void CXios::finalizeThreadManager() |
---|
355 | { |
---|
356 | CThreadManager::finalize() ; |
---|
357 | } |
---|
358 | |
---|
359 | void CXios::finalizeDaemonsManager() |
---|
360 | { |
---|
361 | delete daemonsManager_ ; |
---|
362 | } |
---|
363 | |
---|
364 | void CXios::finalizeNotificationsManager() |
---|
365 | { |
---|
366 | delete notificationsManager_ ; |
---|
367 | } |
---|
368 | |
---|
369 | |
---|
370 | CPoolRessource* CXios::getPoolRessource(void) |
---|
371 | { |
---|
372 | if (isClient) return CClient::getPoolRessource() ; |
---|
373 | else if (isServer) return CServer::getServersRessource()->getPoolRessource() ; |
---|
374 | |
---|
375 | MISSING_RETURN( "CPoolRessource* CXios::getPoolRessource()" ); |
---|
376 | return nullptr; |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|