1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME (app_name + "_CommandableFragmentGenerator").c_str() // include these 2 first -
4 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
6 #include <boost/exception/all.hpp>
7 #include <boost/throw_exception.hpp>
13 #include "canvas/Utilities/Exception.h"
14 #include "cetlib_except/exception.h"
15 #include "fhiclcpp/ParameterSet.h"
17 #include "artdaq-core/Data/ContainerFragmentLoader.hh"
18 #include "artdaq-core/Data/Fragment.hh"
19 #include "artdaq-core/Utilities/ExceptionHandler.hh"
20 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
21 #include "artdaq-core/Utilities/TimeUtils.hh"
32 #define TLVL_GETNEXT 10
33 #define TLVL_GETNEXT_VERBOSE 20
34 #define TLVL_CHECKSTOP 11
35 #define TLVL_EVCOUNTERINC 12
36 #define TLVL_GETDATALOOP 13
37 #define TLVL_GETDATALOOP_DATABUFFWAIT 21
38 #define TLVL_GETDATALOOP_VERBOSE 20
39 #define TLVL_WAITFORBUFFERREADY 15
40 #define TLVL_GETBUFFERSTATS 16
41 #define TLVL_CHECKDATABUFFER 17
42 #define TLVL_GETMONITORINGDATA 18
43 #define TLVL_APPLYREQUESTS 9
44 #define TLVL_SENDEMPTYFRAGMENTS 19
45 #define TLVL_CHECKWINDOWS 14
46 #define TLVL_EMPTYFRAGMENT 22
50 , useMonitoringThread_(ps.get<bool>(
"separate_monitoring_thread", false))
51 , monitoringInterval_(ps.get<int64_t>(
"hardware_poll_interval_us", 0))
55 , timeout_(std::numeric_limits<uint64_t>::max())
56 , timestamp_(std::numeric_limits<uint64_t>::max())
59 , latest_exception_report_(
"none")
62 , sleep_on_stop_us_(0)
64 board_id_ = ps.get<
int>(
"board_id");
65 instance_name_for_metrics_ =
"BoardReader." + boost::lexical_cast<std::string>(board_id_);
67 auto fragment_ids = ps.get<std::vector<artdaq::Fragment::fragment_id_t>>(
"fragment_ids", std::vector<artdaq::Fragment::fragment_id_t>());
69 TLOG(TLVL_TRACE) <<
"artdaq::CommandableFragmentGenerator::CommandableFragmentGenerator(ps)";
72 if (fragment_id != -99)
74 if (!fragment_ids.empty())
76 latest_exception_report_ = R
"(Error in CommandableFragmentGenerator: can't both define "fragment_id" and "fragment_ids" in FHiCL document)";
77 TLOG(TLVL_ERROR) << latest_exception_report_;
78 throw cet::exception(latest_exception_report_);
81 fragment_ids.emplace_back(fragment_id);
84 for (
auto&
id : fragment_ids)
86 expectedTypes_[id] = artdaq::Fragment::EmptyFragmentType;
89 sleep_on_stop_us_ = ps.get<
int>(
"sleep_on_stop_us", 0);
100 TLOG(TLVL_DEBUG) <<
"Joining monitoringThread";
103 if (monitoringThread_.joinable())
105 monitoringThread_.join();
112 TLOG(TLVL_DEBUG) <<
"joinThreads complete";
119 if (check_stop()) usleep(sleep_on_stop_us_);
120 if (exception() || should_stop_)
return false;
122 if (!useMonitoringThread_ && monitoringInterval_ > 0)
124 TLOG(TLVL_GETNEXT) <<
"getNext: Checking whether to collect Monitoring Data";
125 auto now = std::chrono::steady_clock::now();
127 if (TimeUtils::GetElapsedTimeMicroseconds(lastMonitoringCall_, now) >= static_cast<size_t>(monitoringInterval_))
129 TLOG(TLVL_GETNEXT) <<
"getNext: Collecting Monitoring Data";
130 isHardwareOK_ = checkHWStatus_();
131 TLOG(TLVL_GETNEXT) <<
"getNext: isHardwareOK_ is now " << std::boolalpha << isHardwareOK_;
132 lastMonitoringCall_ = now;
138 std::lock_guard<std::mutex> lk(mutex_);
141 TLOG(TLVL_ERROR) <<
"Stopping CFG because the hardware reports bad status!";
144 TLOG(TLVL_TRACE) <<
"getNext: Calling getNext_ w/ ev_counter()=" << ev_counter();
147 result = getNext_(output);
153 TLOG(TLVL_TRACE) <<
"getNext: Done with getNext_ - ev_counter() now " << ev_counter();
154 for (
auto& dataIter : output)
156 TLOG(TLVL_GETNEXT_VERBOSE) <<
"getNext: getNext_() returned fragment with sequenceID = " << dataIter->sequenceID()
157 <<
", type = " << dataIter->typeString() <<
", id = " << std::to_string(dataIter->fragmentID())
158 <<
", timestamp = " << dataIter->timestamp() <<
", and sizeBytes = " << dataIter->sizeBytes();
160 auto fragId = dataIter->fragmentID();
161 auto type = dataIter->type();
164 if (Fragment::isSystemFragmentType(type)) {
168 if (!expectedTypes_.count(fragId))
170 TLOG(TLVL_ERROR) <<
"Received Fragment with Fragment ID " << fragId <<
", which is not in the declared list of Fragment IDs! Aborting!";
173 if (expectedTypes_[fragId] == Fragment::EmptyFragmentType)
174 expectedTypes_[fragId] = type;
175 else if (expectedTypes_[fragId] != type)
177 TLOG(TLVL_WARNING) <<
"Received Fragment with Fragment ID " << fragId <<
" and type " << dataIter->typeString() <<
"(" << type <<
"), which does not match expected type for this ID (" << expectedTypes_[fragId] <<
")";
181 catch (
const cet::exception& e)
183 latest_exception_report_ =
"cet::exception caught in getNext(): ";
184 latest_exception_report_.append(e.what());
185 TLOG(TLVL_ERROR) <<
"getNext: cet::exception caught: " << e;
189 catch (
const boost::exception& e)
191 latest_exception_report_ =
"boost::exception caught in getNext(): ";
192 latest_exception_report_.append(boost::diagnostic_information(e));
193 TLOG(TLVL_ERROR) <<
"getNext: boost::exception caught: " << boost::diagnostic_information(e);
197 catch (
const std::exception& e)
199 latest_exception_report_ =
"std::exception caught in getNext(): ";
200 latest_exception_report_.append(e.what());
201 TLOG(TLVL_ERROR) <<
"getNext: std::exception caught: " << e.what();
207 latest_exception_report_ =
"Unknown exception caught in getNext().";
208 TLOG(TLVL_ERROR) <<
"getNext: unknown exception caught";
215 TLOG(TLVL_DEBUG) <<
"getNext: Either getNext_ or applyRequests returned false, stopping";
218 if (metricMan && !output.empty())
220 auto timestamp = output.front()->timestamp();
222 if (output.size() > 1)
224 for (
auto& outputfrag : output)
226 if (outputfrag->timestamp() > timestamp)
228 timestamp = outputfrag->timestamp();
233 metricMan->sendMetric(
"Last Timestamp", timestamp,
"Ticks", 1, MetricMode::LastPoint);
241 TLOG(TLVL_CHECKSTOP) <<
"CFG::check_stop: should_stop=" << should_stop() <<
", exception status =" << int(exception());
243 if (!should_stop())
return false;
250 TLOG(TLVL_EVCOUNTERINC) <<
"ev_counter_inc: Incrementing ev_counter from " << ev_counter() <<
" by " << step;
251 return ev_counter_.fetch_add(step);
256 TLOG(TLVL_TRACE) <<
"Start Command received.";
259 TLOG(TLVL_ERROR) <<
"negative run number";
260 throw cet::exception(
"CommandableFragmentGenerator") <<
"negative run number";
264 timestamp_ = timestamp;
265 ev_counter_.store(1);
267 should_stop_.store(
false);
268 exception_.store(
false);
271 latest_exception_report_ =
"none";
275 std::unique_lock<std::mutex> lk(mutex_);
276 if (useMonitoringThread_) startMonitoringThread();
277 TLOG(TLVL_TRACE) <<
"Start Command complete.";
282 TLOG(TLVL_TRACE) <<
"Stop Command received.";
285 timestamp_ = timestamp;
288 should_stop_.store(
true);
289 std::unique_lock<std::mutex> lk(mutex_);
293 TLOG(TLVL_TRACE) <<
"Stop Command complete.";
298 TLOG(TLVL_TRACE) <<
"Pause Command received.";
300 timestamp_ = timestamp;
303 should_stop_.store(
true);
304 std::unique_lock<std::mutex> lk(mutex_);
311 TLOG(TLVL_TRACE) <<
"Resume Command received.";
313 timestamp_ = timestamp;
316 should_stop_ =
false;
321 std::unique_lock<std::mutex> lk(mutex_);
324 TLOG(TLVL_TRACE) <<
"Resume Command complete.";
329 TLOG(TLVL_TRACE) <<
"Report Command received.";
330 std::lock_guard<std::mutex> lk(mutex_);
337 std::string childReport = reportSpecific(which);
338 if (childReport.length() > 0) {
return childReport; }
341 if (which ==
"latest_exception")
343 return latest_exception_report_;
347 childReport = report();
348 if (childReport.length() > 0) {
return childReport; }
358 TLOG(TLVL_TRACE) <<
"Report Command complete.";
365 #pragma message "Using default implementation of CommandableFragmentGenerator::pauseNoMutex()"
370 #pragma message "Using default implementation of CommandableFragmentGenerator::pause()"
374 #pragma message "Using default implementation of CommandableFragmentGenerator::resume()"
379 #pragma message "Using default implementation of CommandableFragmentGenerator::report()"
385 #pragma message "Using default implementation of CommandableFragmentGenerator::reportSpecific(std::string)"
391 #pragma message "Using default implementation of CommandableFragmentGenerator::checkHWStatus_()"
397 #pragma message "Using default implementation of CommandableFragmentGenerator::metaCommand(std::string, std::string)"
403 if (monitoringThread_.joinable())
405 monitoringThread_.join();
407 TLOG(TLVL_INFO) <<
"Starting Hardware Monitoring Thread";
412 catch (
const boost::exception& e)
414 TLOG(TLVL_ERROR) <<
"Caught boost::exception starting Hardware Monitoring thread: " << boost::diagnostic_information(e) <<
", errno=" << errno;
415 throw cet::exception(
"ThreadError") <<
"Caught boost::exception starting Hardware Monitoring thread: " << boost::diagnostic_information(e) <<
", errno=" << errno << std::endl;
421 while (!should_stop())
423 if (should_stop() || monitoringInterval_ <= 0)
425 TLOG(TLVL_DEBUG) <<
"getMonitoringDataLoop: should_stop() is " << std::boolalpha << should_stop()
426 <<
" and monitoringInterval is " << monitoringInterval_ <<
", returning";
429 TLOG(TLVL_GETMONITORINGDATA) <<
"getMonitoringDataLoop: Determining whether to call checkHWStatus_";
431 auto now = std::chrono::steady_clock::now();
432 if (TimeUtils::GetElapsedTimeMicroseconds(lastMonitoringCall_, now) >= static_cast<size_t>(monitoringInterval_))
434 isHardwareOK_ = checkHWStatus_();
435 TLOG(TLVL_GETMONITORINGDATA) <<
"getMonitoringDataLoop: isHardwareOK_ is now " << std::boolalpha << isHardwareOK_;
436 lastMonitoringCall_ = now;
438 usleep(monitoringInterval_ / 10);
CommandableFragmentGenerator(const fhicl::ParameterSet &ps)
CommandableFragmentGenerator Constructor.
virtual bool checkHWStatus_()
Check any relavent hardware status registers. Return false if an error condition exists that should h...
virtual ~CommandableFragmentGenerator()
CommandableFragmentGenerator Destructor.
void getMonitoringDataLoop()
This function regularly calls checkHWStatus_(), and sets the isHardwareOK flag accordingly.
artdaq::Fragment::fragment_id_t fragment_id() const
Get the Fragment ID of this Fragment generator.
std::string ReportCmd(std::string const &which="")
Get a report about a user-specified run-time quantity.
virtual bool metaCommand(std::string const &command, std::string const &arg)
The meta-command is used for implementing user-specific commands in a CommandableFragmentGenerator.
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
virtual void pauseNoMutex()
On call to PauseCmd, pauseNoMutex() is called prior to PauseCmd acquiring the mutex ...
bool check_stop()
Routine used by applyRequests to make sure that all outstanding requests have been fulfilled before r...
void ResumeCmd(uint64_t timeout, uint64_t timestamp)
Resume the CommandableFragmentGenerator.
bool getNext(FragmentPtrs &output) overridefinal
getNext calls either applyRequests or getNext_ to get any data that is ready to be sent to the EventB...
void PauseCmd(uint64_t timeout, uint64_t timestamp)
Pause the CommandableFragmentGenerator.
void startMonitoringThread()
Function that launches the monitoring thread (getMonitoringDataLoop())
virtual void pause()
If a CommandableFragmentGenerator subclass is reading from hardware, the implementation of pause() sh...
virtual void resume()
The subrun number will be incremented before a call to resume.
virtual std::string report()
Let's say that the contract with the report() functions is that they return a non-empty string if the...
size_t ev_counter_inc(size_t step=1)
Increment the event counter.
virtual std::string reportSpecific(std::string const &what)
Report the status of a specific quantity
void joinThreads()
Join any data-taking threads. Should be called when destructing CommandableFragmentGenerator.