00001 #include "otsdaq-core/DataManager/DataManager.h"
00002 #include "otsdaq-core/DataManager/CircularBuffer.h"
00003 #include "otsdaq-core/DataManager/DataProducer.h"
00004 #include "otsdaq-core/DataManager/DataConsumer.h"
00005 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00006 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00007 #include "otsdaq-core/PluginMakers/MakeDataProcessor.h"
00008 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <iostream>
00027 #include <vector>
00028 #include <unistd.h>
00029
00030 using namespace ots;
00031
00032
00033 DataManager::DataManager(const ConfigurationTree& theXDAQContextConfigTree, const std::string& supervisorConfigurationPath)
00034 : Configurable(theXDAQContextConfigTree, supervisorConfigurationPath)
00035 {}
00036
00037
00038 DataManager::~DataManager(void)
00039 {
00040 eraseAllBuffers();
00041 }
00042
00043
00044 void DataManager::configure(void)
00045 {
00046 eraseAllBuffers();
00047
00048 for (const auto& buffer : theXDAQContextConfigTree_.getNode(theConfigurationPath_ + "/LinkToDataManagerConfiguration").getChildren())
00049 {
00050 __COUT__ << "Data Buffer Name: " << buffer.first << std::endl;
00051 if (buffer.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00052 {
00053 std::vector<unsigned int> producersVectorLocation;
00054 std::vector<unsigned int> consumersVectorLocation;
00055 auto bufferConfigurationList = buffer.second.getNode("LinkToDataBufferConfiguration").getChildren();
00056 unsigned int location = 0;
00057 for (const auto& bufferConfiguration : bufferConfigurationList)
00058 {
00059 __COUT__ << "Processor id: " << bufferConfiguration.first << std::endl;
00060 if (bufferConfiguration.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00061 {
00062 if (bufferConfiguration.second.getNode("ProcessorType").getValue<std::string>() == "Producer")
00063 {
00064 producersVectorLocation.push_back(location);
00065 }
00066 else if (bufferConfiguration.second.getNode("ProcessorType").getValue<std::string>() == "Consumer")
00067 {
00068 consumersVectorLocation.push_back(location);
00069 }
00070 else
00071 {
00072 __SS__ << "Node ProcessorType in "
00073 << bufferConfiguration.first
00074 << " of type "
00075 << bufferConfiguration.second.getNode("ProcessorType").getValue<std::string>()
00076 << " is invalid. The only accepted types are Producer and Consumer" << std::endl;
00077 throw std::runtime_error(ss.str());
00078 }
00079 }
00080 ++location;
00081
00082 }
00083
00084 if (producersVectorLocation.size() == 0)
00085 {
00086 __SS__ << "Node Data Buffer "
00087 << buffer.first
00088 << " has " << producersVectorLocation.size() << " Producers"
00089 << " and " << consumersVectorLocation.size() << " Consumers"
00090 << " there must be at least 1 Producer " <<
00091 "for the buffer!" << std::endl;
00092 throw std::runtime_error(ss.str());
00093
00094 }
00095
00096 configureBuffer<std::string, std::map<std::string, std::string>>(buffer.first);
00097 for (auto& producerLocation : producersVectorLocation)
00098 {
00099
00100
00101 __COUT__ << bufferConfigurationList[producerLocation].first << std::endl;
00102
00103
00104
00105
00106 buffers_[buffer.first].producers_.push_back(std::shared_ptr<DataProducer>(dynamic_cast<DataProducer*>(
00107 makeDataProcessor
00108 (
00109 bufferConfigurationList[producerLocation].second.getNode("ProcessorPluginName").getValue<std::string>()
00110 , theXDAQContextConfigTree_.getBackNode(theConfigurationPath_).getNode("ApplicationUID").getValue<std::string>()
00111 , buffer.first
00112 , bufferConfigurationList[producerLocation].first
00113 , theXDAQContextConfigTree_
00114 , theConfigurationPath_ + "/LinkToDataManagerConfiguration/" + buffer.first + "/LinkToDataBufferConfiguration/" + bufferConfigurationList[producerLocation].first + "/LinkToProcessorConfiguration"
00115 ))));
00116 __COUT__ << bufferConfigurationList[producerLocation].first << " has been created!" << std::endl;
00117 }
00118 for (auto& consumerLocation : consumersVectorLocation)
00119 {
00120
00121
00122 __COUT__ << bufferConfigurationList[consumerLocation].first << std::endl;
00123
00124
00125
00126
00127
00128 buffers_[buffer.first].consumers_.push_back(std::shared_ptr<DataConsumer>(dynamic_cast<DataConsumer*>(
00129 makeDataProcessor
00130 (
00131 bufferConfigurationList[consumerLocation].second.getNode("ProcessorPluginName").getValue<std::string>()
00132 , theXDAQContextConfigTree_.getBackNode(theConfigurationPath_).getNode("ApplicationUID").getValue<std::string>()
00133 , buffer.first
00134 , bufferConfigurationList[consumerLocation].first
00135 , theXDAQContextConfigTree_
00136 , theConfigurationPath_ + "/LinkToDataManagerConfiguration/" + buffer.first + "/LinkToDataBufferConfiguration/" + bufferConfigurationList[consumerLocation].first + "/LinkToProcessorConfiguration"
00137 ))));
00138 __COUT__ << bufferConfigurationList[consumerLocation].first << " has been created!" << std::endl;
00139 }
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 }
00216
00217
00218 void DataManager::halt(void)
00219 {
00220 stop();
00221 DataManager::eraseAllBuffers();
00222 }
00223
00224
00225 void DataManager::pause(void)
00226 {
00227 __COUT__ << "Pausing..." << std::endl;
00228 DataManager::pauseAllBuffers();
00229 }
00230
00231
00232 void DataManager::resume(void)
00233 {
00234 DataManager::resumeAllBuffers();
00235 }
00236
00237 void DataManager::start(std::string runNumber)
00238 {
00239 DataManager::startAllBuffers(runNumber);
00240 }
00241
00242
00243 void DataManager::stop()
00244 {
00245 DataManager::stopAllBuffers();
00246 }
00247
00248
00249 void DataManager::eraseAllBuffers(void)
00250 {
00251 for (auto& it : buffers_)
00252 deleteBuffer(it.first);
00253
00254 buffers_.clear();
00255 }
00256
00257
00258 void DataManager::eraseBuffer(std::string bufferUID)
00259 {
00260 if (deleteBuffer(bufferUID))
00261 buffers_.erase(bufferUID);
00262 }
00263
00264
00265 bool DataManager::unregisterConsumer(std::string consumerID)
00266 {
00267 for (auto it = buffers_.begin(); it != buffers_.end(); it++)
00268 for (auto& itc : it->second.consumers_)
00269 {
00270 if (itc->getProcessorID() == consumerID)
00271 {
00272 it->second.buffer_->unregisterConsumer(itc.get());
00273 return true;
00274 }
00275 }
00276
00277 return false;
00278 }
00279
00280
00281 bool DataManager::deleteBuffer(std::string bufferUID)
00282 {
00283 auto it = buffers_.find(bufferUID);
00284 if (it != buffers_.end())
00285 {
00286 auto aBuffer = it->second;
00287 if (aBuffer.status_ == Running)
00288 stopBuffer(bufferUID);
00289
00290 for (auto& itc : aBuffer.consumers_)
00291 {
00292 aBuffer.buffer_->unregisterConsumer(itc.get());
00293
00294 }
00295 aBuffer.consumers_.clear();
00296
00297
00298 aBuffer.producers_.clear();
00299
00300 delete aBuffer.buffer_;
00301 return true;
00302 }
00303 return false;
00304 }
00305
00306
00307 void DataManager::registerProducer(std::string bufferUID, DataProducer* producer)
00308 {
00309 buffers_[bufferUID].buffer_->registerProducer(producer, producer->getBufferSize());
00310 }
00311
00312
00313 void DataManager::registerConsumer(std::string bufferUID, DataConsumer* consumer, bool registerToBuffer)
00314 {
00315 if (registerToBuffer)
00316 {
00317 if (buffers_.find(bufferUID) == buffers_.end())
00318 {
00319 __SS__ << ("Can't find buffer UID: " + bufferUID + ". Make sure that your configuration is correct!") << std::endl;
00320 __COUT_ERR__ << ss.str();
00321 throw std::runtime_error(ss.str());
00322 }
00323 buffers_[bufferUID].buffer_->registerConsumer(consumer);
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334 void DataManager::startAllBuffers(std::string runNumber)
00335 {
00336 for (auto it = buffers_.begin(); it != buffers_.end(); it++)
00337 startBuffer(it->first, runNumber);
00338 }
00339
00340
00341 void DataManager::stopAllBuffers(void)
00342 {
00343 for (auto it = buffers_.begin(); it != buffers_.end(); it++)
00344 stopBuffer(it->first);
00345 }
00346
00347
00348 void DataManager::resumeAllBuffers(void)
00349 {
00350 for (auto it = buffers_.begin(); it != buffers_.end(); it++)
00351 resumeBuffer(it->first);
00352 }
00353
00354
00355 void DataManager::pauseAllBuffers(void)
00356 {
00357 for (auto it = buffers_.begin(); it != buffers_.end(); it++)
00358 pauseBuffer(it->first);
00359 }
00360
00361
00362 void DataManager::startBuffer(std::string bufferUID, std::string runNumber)
00363 {
00364 buffers_[bufferUID].buffer_->reset();
00365 for (auto& it : buffers_[bufferUID].consumers_)
00366 it->startProcessingData(runNumber);
00367 for (auto& it : buffers_[bufferUID].producers_)
00368 it->startProcessingData(runNumber);
00369 buffers_[bufferUID].status_ = Running;
00370 }
00371
00372
00373 void DataManager::stopBuffer(std::string bufferUID)
00374 {
00375 for (auto& it : buffers_[bufferUID].producers_)
00376 it->stopProcessingData();
00377
00378
00379 unsigned int timeOut = 0;
00380 const unsigned int ratio = 100;
00381 const unsigned int sleepTime = 1000 * ratio;
00382 unsigned int totalSleepTime = sleepTime / ratio * buffers_[bufferUID].buffer_->getNumberOfBuffers();
00383 if (totalSleepTime < 5000000)
00384 totalSleepTime = 5000000;
00385 while (!buffers_[bufferUID].buffer_->isEmpty())
00386 {
00387 usleep(sleepTime);
00388 timeOut += sleepTime;
00389 if (timeOut > totalSleepTime)
00390 {
00391 std::cout << "Couldn't flush all buffers! Timing out after " << totalSleepTime / 1000000. << " seconds!" << std::endl;
00392 buffers_[bufferUID].buffer_->isEmpty();
00393 break;
00394 }
00395 }
00396 std::cout << "Stopping consumers, buffer MUST BE EMPTY. Is buffer empty? " << buffers_[bufferUID].buffer_->isEmpty() << std::endl;
00397 for (auto& it : buffers_[bufferUID].consumers_)
00398 it->stopProcessingData();
00399 buffers_[bufferUID].buffer_->reset();
00400 buffers_[bufferUID].status_ = Initialized;
00401 }
00402
00403
00404 void DataManager::resumeBuffer(std::string bufferUID)
00405 {
00406 for (auto& it : buffers_[bufferUID].consumers_)
00407 it->resumeProcessingData();
00408 for (auto& it : buffers_[bufferUID].producers_)
00409 it->resumeProcessingData();
00410 buffers_[bufferUID].status_ = Running;
00411 }
00412
00413
00414 void DataManager::pauseBuffer(std::string bufferUID)
00415 {
00416 __COUT__ << "Pausing..." << std::endl;
00417
00418 for (auto& it : buffers_[bufferUID].producers_)
00419 it->pauseProcessingData();
00420
00421 unsigned int timeOut = 0;
00422 const unsigned int sleepTime = 1000;
00423 while (!buffers_[bufferUID].buffer_->isEmpty())
00424 {
00425 usleep(sleepTime);
00426 timeOut += sleepTime;
00427 if (timeOut > sleepTime*buffers_[bufferUID].buffer_->getNumberOfBuffers())
00428 {
00429 std::cout << "Couldn't flush all buffers! Timing out after " << buffers_[bufferUID].buffer_->getNumberOfBuffers()*sleepTime / 1000000. << " seconds!" << std::endl;
00430 break;
00431 }
00432 }
00433 for (auto& it : buffers_[bufferUID].consumers_)
00434 it->pauseProcessingData();
00435 buffers_[bufferUID].status_ = Initialized;
00436 }