00001 #include "canvas/Utilities/Exception.h"
00002 #include "art/Framework/Art/artapp.h"
00003
00004 #define TRACE_NAME "DataReceiverCore"
00005 #include "artdaq/DAQdata/Globals.hh"
00006 #include "artdaq-core/Core/SimpleMemoryReader.hh"
00007 #include "artdaq-core/Utilities/ExceptionHandler.hh"
00008
00009 #include "artdaq/Application/DataReceiverCore.hh"
00010 #include "artdaq/TransferPlugins/TransferInterface.hh"
00011
00012 #include <iomanip>
00013
00014 artdaq::DataReceiverCore::DataReceiverCore(int rank, std::string name)
00015 : name_(name)
00016 , stop_requested_(false)
00017 , pause_requested_(false)
00018 , run_is_paused_(false)
00019 {
00020 TLOG_DEBUG(name_) << "Constructor" << TLOG_ENDL;
00021 my_rank = rank;
00022 metricMan = &metricMan_;
00023 }
00024
00025 artdaq::DataReceiverCore::~DataReceiverCore()
00026 {
00027 TLOG_DEBUG(name_) << "Destructor" << TLOG_ENDL;
00028 }
00029
00030 bool artdaq::DataReceiverCore::initializeDataReceiver(fhicl::ParameterSet const& pset, fhicl::ParameterSet const& data_pset, fhicl::ParameterSet const& metric_pset)
00031 {
00032
00033 verbose_ = pset.get<bool>("verbose", false);
00034
00035 if (metric_pset.is_empty())
00036 {
00037 TLOG_INFO(name_) << "No metric plugins appear to be defined" << TLOG_ENDL;
00038 }
00039 try
00040 {
00041 metricMan_.initialize(metric_pset, name_ + "." + std::to_string(my_rank));
00042 }
00043 catch (...)
00044 {
00045 ExceptionHandler(ExceptionHandlerRethrow::no,
00046 "Error loading metrics in DataReceiverCore::initialize()");
00047 }
00048
00049 fhicl::ParameterSet tmp = pset;
00050 tmp.erase("daq");
00051
00052 fhicl::ParameterSet data_tmp = data_pset;
00053 if (data_pset.has_key("expected_events_per_bunch"))
00054 {
00055 data_tmp.put<int>("expected_fragments_per_event", data_pset.get<int>("expected_events_per_bunch"));
00056 }
00057
00058 event_store_ptr_.reset(new SharedMemoryEventManager(data_tmp, tmp));
00059
00060 receiver_ptr_.reset(new artdaq::DataReceiverManager(data_tmp, event_store_ptr_));
00061
00062 return true;
00063 }
00064
00065 bool artdaq::DataReceiverCore::start(art::RunID id)
00066 {
00067 stop_requested_.store(false);
00068 pause_requested_.store(false);
00069 run_is_paused_.store(false);
00070 metricMan_.do_start();
00071 event_store_ptr_->startRun(id.run());
00072 receiver_ptr_->start_threads();
00073
00074 logMessage_("Started run " + boost::lexical_cast<std::string>(event_store_ptr_->runID()));
00075 return true;
00076 }
00077
00078 bool artdaq::DataReceiverCore::stop()
00079 {
00080 logMessage_("Stopping run " + boost::lexical_cast<std::string>(event_store_ptr_->runID()) +
00081 ", subrun " + boost::lexical_cast<std::string>(event_store_ptr_->subrunID()));
00082 bool endSucceeded;
00083 int attemptsToEnd;
00084
00085
00086
00087
00088
00089 stop_requested_.store(true);
00090
00091 if (!run_is_paused_.load())
00092 {
00093 endSucceeded = false;
00094 attemptsToEnd = 1;
00095 endSucceeded = event_store_ptr_->endSubrun();
00096 while (!endSucceeded && attemptsToEnd < 3)
00097 {
00098 ++attemptsToEnd;
00099 TLOG_DEBUG(name_) << "Retrying EventStore::endSubrun()" << TLOG_ENDL;
00100 endSucceeded = event_store_ptr_->endSubrun();
00101 }
00102 if (!endSucceeded)
00103 {
00104 TLOG_ERROR(name_)
00105 << "EventStore::endSubrun in stop method failed after three tries." << TLOG_ENDL;
00106 }
00107 }
00108
00109 endSucceeded = false;
00110 attemptsToEnd = 1;
00111 endSucceeded = event_store_ptr_->endRun();
00112 while (!endSucceeded && attemptsToEnd < 3)
00113 {
00114 ++attemptsToEnd;
00115 TLOG_DEBUG(name_) << "Retrying EventStore::endRun()" << TLOG_ENDL;
00116 endSucceeded = event_store_ptr_->endRun();
00117 }
00118 if (!endSucceeded)
00119 {
00120 TLOG_ERROR(name_)
00121 << "EventStore::endRun in stop method failed after three tries." << TLOG_ENDL;
00122 }
00123
00124 run_is_paused_.store(false);
00125 return true;
00126 }
00127
00128 bool artdaq::DataReceiverCore::pause()
00129 {
00130 logMessage_("Pausing run " + boost::lexical_cast<std::string>(event_store_ptr_->runID()) +
00131 ", subrun " + boost::lexical_cast<std::string>(event_store_ptr_->subrunID()));
00132 pause_requested_.store(true);
00133
00134 bool endSucceeded = false;
00135 int attemptsToEnd = 1;
00136 endSucceeded = event_store_ptr_->endSubrun();
00137 while (!endSucceeded && attemptsToEnd < 3)
00138 {
00139 ++attemptsToEnd;
00140 TLOG_DEBUG(name_) << "Retrying EventStore::endSubrun()" << TLOG_ENDL;
00141 endSucceeded = event_store_ptr_->endSubrun();
00142 }
00143 if (!endSucceeded)
00144 {
00145 TLOG_ERROR(name_)
00146 << "EventStore::endSubrun in pause method failed after three tries." << TLOG_ENDL;
00147 }
00148
00149 run_is_paused_.store(true);
00150 return true;
00151 }
00152
00153 bool artdaq::DataReceiverCore::resume()
00154 {
00155 logMessage_("Resuming run " + boost::lexical_cast<std::string>(event_store_ptr_->runID()));
00156 pause_requested_.store(false);
00157 metricMan_.do_start();
00158 event_store_ptr_->startSubrun();
00159 run_is_paused_.store(false);
00160 return true;
00161 }
00162
00163 bool artdaq::DataReceiverCore::shutdown()
00164 {
00165
00166
00167
00168
00169
00170 TLOG_DEBUG("DataReceiverCore") << "shutdown: Shutting down DataReceiverManager" << TLOG_ENDL;
00171 receiver_ptr_.reset(nullptr);
00172
00173 bool endSucceeded = false;
00174 int attemptsToEnd = 1;
00175 TLOG_DEBUG("DataReceiverCore") << "shutdown: Calling EventStore::endOfData" << TLOG_ENDL;
00176 endSucceeded = event_store_ptr_->endOfData();
00177 while (!endSucceeded && attemptsToEnd < 3)
00178 {
00179 ++attemptsToEnd;
00180 TLOG_DEBUG(name_) << "Retrying EventStore::endOfData()" << TLOG_ENDL;
00181 endSucceeded = event_store_ptr_->endOfData();
00182 }
00183
00184 TLOG_DEBUG("DataReceiverCore") << "shutdown: Shutting down SharedMemoryEventManager" << TLOG_ENDL;
00185 event_store_ptr_.reset();
00186
00187 TLOG_DEBUG("DataReceiverCore") << "shutdown: Shutting down MetricManager" << TLOG_ENDL;
00188 metricMan_.shutdown();
00189
00190 TLOG_DEBUG("DataReceiverCore") << "shutdown: Complete" << TLOG_ENDL;
00191 return endSucceeded;
00192 }
00193
00194 bool artdaq::DataReceiverCore::soft_initialize(fhicl::ParameterSet const& pset)
00195 {
00196 TLOG_DEBUG(name_) << "soft_initialize method called with DAQ "
00197 << "ParameterSet = \"" << pset.to_string()
00198 << "\"." << TLOG_ENDL;
00199 return true;
00200 }
00201
00202 bool artdaq::DataReceiverCore::reinitialize(fhicl::ParameterSet const& pset)
00203 {
00204 TLOG_DEBUG(name_) << "reinitialize method called with DAQ "
00205 << "ParameterSet = \"" << pset.to_string()
00206 << "\"." << TLOG_ENDL;
00207 event_store_ptr_ = nullptr;
00208 return initialize(pset);
00209 }
00210
00211 std::string artdaq::DataReceiverCore::report(std::string const& which) const
00212 {
00213 if (which == "incomplete_event_count")
00214 {
00215 if (event_store_ptr_ != nullptr)
00216 {
00217 return boost::lexical_cast<std::string>(event_store_ptr_->GetIncompleteEventCount());
00218 }
00219 else
00220 {
00221 return "-1";
00222 }
00223 }
00224 if (which == "event_count")
00225 {
00226 if (receiver_ptr_ != nullptr)
00227 return boost::lexical_cast<std::string>(receiver_ptr_->GetReceivedFragmentCount()->count());
00228
00229 return "0";
00230 }
00231
00232
00233
00234
00235
00236
00237 std::string tmpString;
00238 if (event_store_ptr_ != nullptr) tmpString.append(name_ + " run number = " + boost::lexical_cast<std::string>(event_store_ptr_->runID()) + ".\n");
00239 tmpString.append("Command \"" + which + "\" is not currently supported.");
00240 return tmpString;
00241 }
00242
00243 void artdaq::DataReceiverCore::logMessage_(std::string const& text)
00244 {
00245 if (verbose_)
00246 {
00247 TLOG_INFO(name_) << text << TLOG_ENDL;
00248 }
00249 else
00250 {
00251 TLOG_DEBUG(name_) << text << TLOG_ENDL;
00252 }
00253 }