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