source: XIOS3/dev/XIOS_FILE_SERVICES/src/manager/ressources_manager.cpp @ 2453

Last change on this file since 2453 was 2453, checked in by ymipsl, 18 months ago

Implementation of files service on dev branch

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.2 KB
Line 
1#include "ressources_manager.hpp"
2#include "server.hpp"
3#include "servers_ressource.hpp"
4#include "timer.hpp"
5
6
7
8
9
10namespace xios
11{
12  using namespace std;
13
14  CRessourcesManager::CRessourcesManager(bool isXiosServer) 
15  {
16   
17    xiosComm_ = CXios::getXiosComm()  ;
18   
19    int commRank ; 
20    MPI_Comm_rank(xiosComm_, &commRank) ;
21    if (commRank==0 && isXiosServer) MPI_Comm_rank(xiosComm_, &commRank) ; 
22    else commRank=0 ;
23    MPI_Allreduce(&commRank, &managerGlobalLeader_, 1, MPI_INT, MPI_SUM, xiosComm_) ;
24
25    MPI_Comm_rank(xiosComm_, &commRank) ;
26    winNotify_ = new CWindowManager(xiosComm_, maxBufferSize_) ;
27   
28
29    winRessources_ = new CWindowManager(xiosComm_, maxBufferSize_) ;
30    winRessources_->lockWindow(commRank,0) ;
31    serverLeader_=-1 ;
32    winRessources_->updateToWindow(commRank, this, &CRessourcesManager::ressourcesDumpOut) ;
33    winRessources_->unlockWindow(commRank,0) ;
34
35    MPI_Barrier(xiosComm_)  ;   
36  }
37 
38  CRessourcesManager::~CRessourcesManager()
39  {
40    delete winNotify_ ;
41    delete winRessources_ ;
42  } 
43
44  void CRessourcesManager::createPool(const string& poolId, int size)
45  {
46    info(40)<<"CRessourcesManager::createPool : calling createPool : "<<poolId<<"  of size"<<size<<endl ;
47    info(40)<<"send notification to leader : "<<serverLeader_<<endl ;
48    winRessources_->lockWindow(managerGlobalLeader_,0) ;
49    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
50    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
51   
52    notifyType_=NOTIFY_CREATE_POOL ;
53    notifyCreatePool_=make_tuple(poolId, size) ;
54    info(40)<<"CRessourcesManager::createPool : send notification creating pool to server leader "<<serverLeader_<<endl ;
55    sendNotification(serverLeader_) ; 
56  }
57 
58  void CRessourcesManager::finalize(void)
59  {
60    winRessources_->lockWindow(managerGlobalLeader_,0) ;
61    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
62    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
63   
64    if (serverLeader_!=-1)
65    {
66      notifyType_=NOTIFY_FINALIZE ;
67      info(40)<<"CRessourcesManager::finalize : send notification finalize to server leader "<<serverLeader_<<endl ;
68      sendNotification(serverLeader_) ;
69    } 
70  }
71
72  void CRessourcesManager::sendNotification(int rank)
73  {
74    winNotify_->lockWindowExclusive(rank) ;
75    winNotify_->pushToLockedWindow(rank, this, &CRessourcesManager::notificationsDumpOut) ;
76    winNotify_->unlockWindow(rank) ;
77  }
78
79 
80  void CRessourcesManager::notificationsDumpOut(CBufferOut& buffer)
81  {
82   
83    buffer.realloc(maxBufferSize_) ;
84   
85    if (notifyType_==NOTIFY_CREATE_POOL)
86    {
87      auto& arg=notifyCreatePool_ ;
88      buffer << notifyType_<< get<0>(arg) << get<1>(arg) ;
89    }
90    else if (notifyType_==NOTIFY_FINALIZE)
91    {
92      buffer << notifyType_ ;
93    }
94  }
95
96  void CRessourcesManager::notificationsDumpIn(CBufferIn& buffer)
97  {
98    if (buffer.bufferSize() == 0) notifyType_= NOTIFY_NOTHING ;
99    else
100    {
101      buffer>>notifyType_;
102      if (notifyType_==NOTIFY_CREATE_POOL)
103      {
104        auto& arg=notifyCreatePool_ ;
105        buffer >> get<0>(arg) >> get<1>(arg)  ;
106      }
107      else if (notifyType_==NOTIFY_FINALIZE) { /*nothing to do*/ }
108    }
109
110  }
111
112  void CRessourcesManager::eventLoop(void)
113  {
114    CTimer::get("CRessourcesManager::eventLoop").resume();
115    int flag ;
116    MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
117    double time=MPI_Wtime() ;
118    if (time-lastEventLoop_ > eventLoopLatency_) 
119    {
120      checkNotifications() ;
121      lastEventLoop_=time ;
122    }
123
124    CTimer::get("CRessourcesManager::eventLoop").suspend();
125  }
126 
127  void CRessourcesManager::checkNotifications(void)
128  {
129    int commRank ;
130    MPI_Comm_rank(xiosComm_, &commRank) ;
131    CTimer::get("CRessourcesManager::checkNotifications lock").resume();
132    winNotify_->lockWindowExclusive(commRank) ;
133    CTimer::get("CRessourcesManager::checkNotifications lock").suspend();
134    CTimer::get("CRessourcesManager::checkNotifications pop").resume();
135    winNotify_->popFromLockedWindow(commRank, this, &CRessourcesManager::notificationsDumpIn) ;
136    CTimer::get("CRessourcesManager::checkNotifications pop").suspend();
137    CTimer::get("CRessourcesManager::checkNotifications unlock").resume();
138    winNotify_->unlockWindow(commRank) ;
139    CTimer::get("CRessourcesManager::checkNotifications unlock").suspend();
140    if (notifyType_==NOTIFY_CREATE_POOL) createPool() ;
141    else if (notifyType_==NOTIFY_FINALIZE) finalizeSignal() ;
142  }
143
144  void CRessourcesManager::createPool(void)
145  {
146   
147    auto& arg=notifyCreatePool_ ;
148    string poolId=get<0>(arg) ;
149    int size=get<1>(arg) ;
150    info(40)<<"CRessourcesManager::createPool : receive create pool notification : "<< poolId<<"  of size "<<size<<endl ;
151    CServer::getServersRessource()->createPool(poolId,size) ;
152  } 
153
154  void CRessourcesManager::finalizeSignal(void)
155  {
156    info(40)<<"CRessourcesManager::createPool : receive finalize notification"<<endl ;
157    CServer::getServersRessource()->finalize() ;
158  }
159
160  void CRessourcesManager::ressourcesDumpOut(CBufferOut& buffer)
161  {
162   
163    buffer.realloc(maxBufferSize_) ;
164   
165    buffer<<ressourcesSize_<<freeRessourcesSize_<<serverLeader_ ; 
166    buffer<<(int) pools_.size();
167    for(auto it=pools_.begin();it!=pools_.end(); ++it)
168    { 
169      auto key = it->first ;
170      auto val = it->second ; 
171      buffer << key<<std::get<0>(val)  << std::get<1>(val)  << std::get<2>(val);
172    }
173  }
174
175  void CRessourcesManager::ressourcesDumpIn(CBufferIn& buffer)
176  {
177    std::string poolId ;
178    int size ;
179    int freeSize ;
180    int leader ;
181   
182    buffer>>ressourcesSize_>>freeRessourcesSize_>>serverLeader_ ;
183    pools_.clear() ;
184    int nbPools ;
185    buffer>>nbPools ;
186    for(int i=0;i<nbPools;i++) 
187    {
188      buffer>>poolId>>size>>freeSize>>leader ;
189      pools_[poolId]=std::make_tuple(size, freeSize, leader) ;
190    }
191  }
192 
193  void CRessourcesManager::registerServerLeader(int serverLeaderRank)
194  {
195    winRessources_->lockWindow(managerGlobalLeader_,0) ;
196    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
197    serverLeader_ = serverLeaderRank ;
198    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
199    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
200  }
201 
202  void CRessourcesManager::registerRessourcesSize(int size)
203  {
204    winRessources_->lockWindow(managerGlobalLeader_,0) ;
205    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
206    ressourcesSize_ = size ;
207    freeRessourcesSize_ = size ;
208    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
209    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
210  }
211
212 
213  void CRessourcesManager::registerPoolClient(const string& poolId, int size, int leader)
214  {
215    winRessources_->lockWindow(managerGlobalLeader_,0) ;
216    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
217    pools_[poolId] = make_tuple(size, size, leader) ;
218    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
219    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
220  }
221
222  void CRessourcesManager::registerPoolServer(const string& poolId, int size, int leader)
223  {
224    winRessources_->lockWindow(managerGlobalLeader_,0) ;
225    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
226    pools_[poolId] = make_tuple(size, size, leader) ;
227    freeRessourcesSize_-=size ;
228    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
229    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
230  }
231
232  bool CRessourcesManager::getPoolInfo(const string& poolId, int& size, int& freeSize, int& leader)
233  {
234    winRessources_->lockWindow(managerGlobalLeader_,0) ;
235    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
236    winRessources_->unlockWindow(managerGlobalLeader_,0) ;
237
238    auto it=pools_.find(poolId) ;
239    if ( it == pools_.end()) return false ;
240    else
241    {
242      size=get<0>(it->second) ;
243      freeSize=get<1>(it->second) ;
244      leader=get<2>(it->second) ;
245      return true ;
246    }
247  }
248
249  bool CRessourcesManager::decreasePoolFreeSize(const string& poolId, int size)
250  {
251    int ret ;
252
253    winRessources_->lockWindow(managerGlobalLeader_,0) ;
254    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
255   
256
257    auto it=pools_.find(poolId) ;
258   
259    if ( it == pools_.end()) ret=false ;
260    else 
261    {
262      get<1>(it->second)-=size ;
263      ret=true ;
264    }
265    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
266    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
267  }
268
269  int CRessourcesManager::getRessourcesSize(void)
270  {
271    winRessources_->lockWindow(managerGlobalLeader_,0) ;
272    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
273    winRessources_->unlockWindow(managerGlobalLeader_,0) ;
274
275    return ressourcesSize_ ;
276  }
277
278  int CRessourcesManager::getFreeRessourcesSize(void)
279  {
280    winRessources_->lockWindow(managerGlobalLeader_,0) ;
281    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
282    winRessources_->unlockWindow(managerGlobalLeader_,0) ;
283
284    return freeRessourcesSize_ ;
285  } 
286
287  bool CRessourcesManager::getPoolLeader(const string& poolId, int& leader)
288  {
289    int size, freeSize ;
290    return getPoolInfo(poolId, size, freeSize, leader) ;
291  }
292
293  bool CRessourcesManager::getPoolSize(const string& poolId, int& size)
294  {
295    int leader,freeSize ;
296    return getPoolInfo(poolId, size, freeSize, leader) ;
297  }
298
299  bool CRessourcesManager::getPoolFreeSize(const string& poolId, int& freeSize)
300  {
301    int leader,size ;
302    return getPoolInfo(poolId, size, freeSize, leader) ;
303  }
304
305  bool CRessourcesManager::hasPool(const string& poolId)
306  {
307    int leader,size,freeSize ;
308    return getPoolInfo(poolId, size, freeSize, leader) ;
309  }
310
311  void CRessourcesManager::waitPoolRegistration(const string& poolId)
312  {
313    while(!hasPool(poolId)) CXios::getDaemonsManager()->servicesEventLoop() ;
314  }
315}
Note: See TracBrowser for help on using the repository browser.