source: XIOS3/dev/XIOS_FILE_SERVICES/src/cxios.cpp

Last change on this file was 2438, checked in by jderouillat, 19 months ago

Add the ability to specify the location of iodef through an environment variable XIOS_IODEF_PATH. Included XML sources can integrates environment variables

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:eol-style set to native
File size: 10.6 KB
Line 
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 <boost/functional/hash.hpp>
8#include "mpi.hpp"
9#include "memory.hpp"
10#include <new>
11#include "memtrack.hpp"
12#include "registry.hpp"
13#include "ressources_manager.hpp"
14#include "services_manager.hpp"
15#include "servers_ressource.hpp"
16#include "mem_checker.hpp"
17#include <unistd.h>
18
19namespace 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
61  CDaemonsManager*    CXios::daemonsManager_=nullptr ;
62  CRessourcesManager* CXios::ressourcesManager_=nullptr ;
63  CServicesManager*   CXios::servicesManager_=nullptr ;
64  CContextsManager*   CXios::contextsManager_=nullptr ;
65  CCouplerManager*    CXios::couplerManager_=nullptr ;
66  CRegistryManager*   CXios::registryManager_=nullptr ;
67
68  CMpiGarbageCollector CXios::MpiGarbageCollector_  ;
69
70  //! Parse configuration file and create some objects from it
71  void CXios::initialize()
72  {
73    set_new_handler(noMemory);
74    char startPath[256];
75    getcwd(startPath, sizeof(startPath));
76    if(const char* userPath = std::getenv("XIOS_IODEF_PATH"))
77    {
78      if ( chdir( userPath ) != 0)
79      {
80        ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl );
81      }
82    }
83    parseFile(rootFile);
84    parseXiosConfig();
85    chdir( startPath );
86  }
87
88  /*!
89  \brief Parse xios part of configuration file (.iodef.xml)
90   Both client and server need information returned from this function
91  */
92  void CXios::parseXiosConfig()
93  {
94    usingOasis=getin<bool>("using_oasis",false) ;
95    usingServer=getin<bool>("using_server",false) ;
96    usingServer2=getin<bool>("using_server2",false) ;
97    ratioServer2=getin<int>("ratio_server2",50);
98    nbPoolsServer2=getin<int>("number_pools_server2",0);
99    info.setLevel(getin<int>("info_level",0)) ;
100    report.setLevel(getin<int>("info_level",50));
101    printLogs2Files=getin<bool>("print_file",false);
102
103    xiosStack=getin<bool>("xios_stack",true) ;
104    systemStack=getin<bool>("system_stack",false) ;
105    if (xiosStack && systemStack)
106    {
107      xiosStack = false;
108    }
109
110    StdString bufMemory("memory");
111    StdString bufPerformance("performance");
112    StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance);
113    std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower);
114    if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false;
115    else if (0 != bufOpt.compare(bufPerformance))
116    {
117      ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl );
118    }
119
120    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
121    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
122    maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
123    recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
124    if (recvFieldTimeout < 0.0)
125      ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
126
127    checkEventSync = getin<bool>("check_event_sync", checkEventSync);
128   
129    checkSumSend = getin<bool>("checksum_send_fields", false);
130    checkSumRecv = getin<bool>("checksum_recv_fields", false);
131   
132    logMemory = getin<bool>("log_memory", false);
133
134    globalComm=MPI_COMM_WORLD ;
135  }
136
137  /*!
138  Initialize client
139  \param [in] codeId identity of context
140  \param [in] localComm local communicator
141  \param [in/out] returnComm communicator corresponding to group of client with same codeId
142  */
143  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
144  TRY
145  {
146    initialize() ;
147
148    isClient = true;
149
150    //CClient::initialize(codeId,localComm,returnComm) ;
151    CClient::initialize(codeId,localComm,returnComm) ;
152   
153    // If there are no server processes then we are in attached mode
154    // and the clients are also servers
155    isServer = !usingServer;
156
157    if (printLogs2Files)
158    {
159      CClient::openInfoStream(clientFile);
160      CClient::openErrorStream(clientFile);
161    }
162    else
163    {
164      CClient::openInfoStream();
165      CClient::openErrorStream();
166    }
167    CMemChecker::logMem("CXios::initClientSide");
168  }
169  CATCH
170
171  void CXios::clientFinalize(void)
172  {
173     CMemChecker::logMem("CXios::clientFinalize", true);
174
175     CClient::finalize() ;
176         
177#ifdef XIOS_MEMTRACK
178
179#ifdef XIOS_MEMTRACK_LIGHT
180       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
181       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
182#endif
183
184#ifdef XIOS_MEMTRACK_FULL
185      report(0) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
186      report(0) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
187     
188      ofstream memReport ;
189      std::filebuf* fb = memReport.rdbuf();
190      CClient::openStream(clientFile, ".mem", fb);
191     
192      MemTrack::TrackListMemoryUsage() ;
193      size_t memtrack_blocks=0 ;
194      memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ;
195      size_t memtrack_size=0 ;
196      memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ;
197      MemTrack::TrackDumpBlocks(memReport, memtrack_blocks,memtrack_size);
198      memReport.close();
199#endif
200
201     CClient::closeInfoStream();
202
203#endif
204  }
205
206  //! Init server by parsing only xios part of config file
207  void CXios::initServer()
208  {
209    set_new_handler(noMemory);
210    std::set<StdString> parseList;
211    parseList.insert("xios");
212    char startPath[256];
213    getcwd(startPath, sizeof(startPath));
214    if(const char* userPath = std::getenv("XIOS_IODEF_PATH"))
215    {
216      if ( chdir( userPath ) != 0)
217      {
218        ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl );
219      }
220    }
221    xml::CXMLParser::ParseFile(rootFile, parseList);
222    parseXiosConfig();
223    chdir( startPath );
224  }
225
226  //! Initialize server then put it into listening state
227  void CXios::initServerSide(void)
228  {
229    CMemChecker::get("xios").resume() ;
230    initServer();
231    isClient = false;
232    isServer = true;
233
234    // Initialize all aspects MPI
235    CServer::initialize();
236    CServer::finalize();
237
238#ifdef XIOS_MEMTRACK
239
240#ifdef XIOS_MEMTRACK_LIGHT
241       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
242       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
243#endif
244
245#ifdef XIOS_MEMTRACK_FULL
246      report(0) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
247      report(0) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
248      ofstream memReport ;
249      std::filebuf* fb = memReport.rdbuf();
250      CClient::openStream(serverFile, ".mem", fb);
251     
252      MemTrack::TrackListMemoryUsage() ;
253      size_t memtrack_blocks=0 ;
254      memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ;
255      size_t memtrack_size=0 ;
256      memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ;
257      MemTrack::TrackDumpBlocks(memReport,memtrack_blocks,memtrack_size);
258      memReport.close() ;
259#endif
260#endif
261    CMemChecker::get("xios").suspend() ;
262    CServer::closeInfoStream();
263  }
264
265  //! Parse configuration file
266  void CXios::parseFile(const string& filename)
267  {
268    xml::CXMLParser::ParseFile(filename);
269  }
270
271  //! Set using server
272  void CXios::setUsingServer()
273  {
274    usingServer = true;
275  }
276
277  //! Unset using server
278  void CXios::setNotUsingServer()
279  {
280    usingServer = false;
281  }
282
283  void CXios::launchRegistryManager(bool isXiosServer)
284  {
285    registryManager_ = new CRegistryManager(isXiosServer) ;
286  }
287
288  void CXios::launchRessourcesManager(bool isXiosServer)
289  {
290    ressourcesManager_ = new CRessourcesManager(isXiosServer) ;
291  }
292
293  void CXios::launchCouplerManager(bool isXiosServer)
294  {
295    couplerManager_ = new CCouplerManager(isXiosServer) ;
296  }
297
298  void CXios::launchServicesManager(bool isXiosServer)
299  {
300    servicesManager_ = new CServicesManager(isXiosServer) ;
301  }
302
303  void CXios::launchContextsManager(bool isXiosServer)
304  {
305    contextsManager_ = new CContextsManager(isXiosServer) ;
306  }
307 
308  void CXios::launchDaemonsManager(bool isXiosServer)
309  {
310    daemonsManager_ = new CDaemonsManager(isXiosServer) ;
311  }
312
313 
314  void CXios::finalizeRegistryManager()
315  {
316    delete registryManager_;
317  }
318
319  void CXios::finalizeRessourcesManager()
320  {
321    delete ressourcesManager_;
322  }
323
324  void CXios::finalizeCouplerManager()
325  {
326    delete couplerManager_;
327  }
328
329  void CXios::finalizeServicesManager()
330  {
331    delete servicesManager_  ;
332  }
333
334  void CXios::finalizeContextsManager()
335  {
336    delete contextsManager_  ;
337  }
338 
339  void CXios::finalizeDaemonsManager()
340  {
341    delete daemonsManager_  ;
342  }
343 
344  CPoolRessource* CXios::getPoolRessource(void)
345  {
346    if (isClient) return CClient::getPoolRessource() ;
347    else if (isServer) return CServer::getServersRessource()->getPoolRessource() ;
348   
349    MISSING_RETURN( "CPoolRessource* CXios::getPoolRessource()" );
350    return nullptr;
351  }
352}
353
Note: See TracBrowser for help on using the repository browser.