1 #include "TRACE/tracemf.h"
2 #include "artdaq/DAQdata/Globals.hh"
3 #define TRACE_NAME (app_name + "_CommandableFragmentGenerator").c_str() // include these 2 first -
5 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
7 #include "artdaq-core/Data/ContainerFragmentLoader.hh"
8 #include "artdaq-core/Data/Fragment.hh"
9 #include "artdaq-core/Utilities/TimeUtils.hh"
12 #include "cetlib_except/exception.h"
13 #include "fhiclcpp/ParameterSet.h"
15 #include <boost/exception/all.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include <boost/thread.hpp>
32 #define TLVL_GETNEXT 35
33 #define TLVL_GETNEXT_VERBOSE 36
34 #define TLVL_CHECKSTOP 37
35 #define TLVL_EVCOUNTERINC 38
36 #define TLVL_GETDATALOOP 39
37 #define TLVL_GETDATALOOP_DATABUFFWAIT 40
38 #define TLVL_GETDATALOOP_VERBOSE 41
39 #define TLVL_WAITFORBUFFERREADY 42
40 #define TLVL_GETBUFFERSTATS 43
41 #define TLVL_CHECKDATABUFFER 44
42 #define TLVL_GETMONITORINGDATA 45
43 #define TLVL_APPLYREQUESTS 46
44 #define TLVL_SENDEMPTYFRAGMENTS 47
45 #define TLVL_CHECKWINDOWS 48
46 #define TLVL_EMPTYFRAGMENT 49
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")
61 , sleep_on_stop_us_(0)
63 auto fragment_ids = ps.get<std::vector<artdaq::Fragment::fragment_id_t>>(
"fragment_ids", std::vector<artdaq::Fragment::fragment_id_t>());
65 TLOG(TLVL_DEBUG + 33) <<
"artdaq::CommandableFragmentGenerator::CommandableFragmentGenerator(ps)";
68 if (fragment_id != -99)
70 if (!fragment_ids.empty())
72 latest_exception_report_ = R
"(Error in CommandableFragmentGenerator: can't both define "fragment_id" and "fragment_ids" in FHiCL document)";
73 TLOG(TLVL_ERROR) << latest_exception_report_;
74 throw cet::exception(latest_exception_report_);
77 fragment_ids.emplace_back(fragment_id);
80 if(ps.has_key(
"generated_fragments_per_event")) {
81 TLOG(TLVL_WARNING) <<
"Ignoring deprecated configuration parameter \"generated_fragments_per_event\"";
84 int first_fragment_id = std::numeric_limits<int>::max();
85 for (
auto&
id : fragment_ids)
87 if (
id < first_fragment_id) first_fragment_id = id;
88 expectedTypes_[id] = artdaq::Fragment::EmptyFragmentType;
90 instance_name_for_metrics_ =
"BoardReader." + boost::lexical_cast<std::string>(first_fragment_id);
92 sleep_on_stop_us_ = ps.get<
int>(
"sleep_on_stop_us", 0);
103 TLOG(TLVL_DEBUG + 32) <<
"Joining monitoringThread";
106 if (monitoringThread_.joinable())
108 monitoringThread_.join();
115 TLOG(TLVL_DEBUG + 32) <<
"joinThreads complete";
122 if (check_stop()) usleep(sleep_on_stop_us_);
123 if (exception() || should_stop_)
return false;
125 if (!useMonitoringThread_ && monitoringInterval_ > 0)
127 TLOG(TLVL_GETNEXT) <<
"getNext: Checking whether to collect Monitoring Data";
128 auto now = std::chrono::steady_clock::now();
130 if (TimeUtils::GetElapsedTimeMicroseconds(lastMonitoringCall_, now) >= static_cast<size_t>(monitoringInterval_))
132 TLOG(TLVL_GETNEXT) <<
"getNext: Collecting Monitoring Data";
133 isHardwareOK_ = checkHWStatus_();
134 TLOG(TLVL_GETNEXT) <<
"getNext: isHardwareOK_ is now " << std::boolalpha << isHardwareOK_;
135 lastMonitoringCall_ = now;
141 std::lock_guard<std::mutex> lk(mutex_);
144 TLOG(TLVL_ERROR) <<
"Stopping CFG because the hardware reports bad status!";
147 TLOG(TLVL_DEBUG + 33) <<
"getNext: Calling getNext_ w/ ev_counter()=" << ev_counter();
150 result = getNext_(output);
156 TLOG(TLVL_DEBUG + 33) <<
"getNext: Done with getNext_ - ev_counter() now " << ev_counter();
157 for (
auto& dataIter : output)
159 TLOG(TLVL_GETNEXT_VERBOSE) <<
"getNext: getNext_() returned fragment with sequenceID = " << dataIter->sequenceID()
160 <<
", type = " << dataIter->typeString() <<
", id = " << std::to_string(dataIter->fragmentID())
161 <<
", timestamp = " << dataIter->timestamp() <<
", and sizeBytes = " << dataIter->sizeBytes();
163 auto fragId = dataIter->fragmentID();
164 auto type = dataIter->type();
167 if (Fragment::isSystemFragmentType(type))
172 if (!expectedTypes_.count(fragId))
174 TLOG(TLVL_ERROR) <<
"Received Fragment with Fragment ID " << fragId <<
", which is not in the declared list of Fragment IDs! Aborting!";
177 if (expectedTypes_[fragId] == Fragment::EmptyFragmentType)
178 expectedTypes_[fragId] = type;
179 else if (expectedTypes_[fragId] != type)
181 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] <<
")";
185 catch (
const cet::exception& e)
187 latest_exception_report_ =
"cet::exception caught in getNext(): ";
188 latest_exception_report_.append(e.what());
189 TLOG(TLVL_ERROR) <<
"getNext: cet::exception caught: " << e;
193 catch (
const boost::exception& e)
195 latest_exception_report_ =
"boost::exception caught in getNext(): ";
196 latest_exception_report_.append(boost::diagnostic_information(e));
197 TLOG(TLVL_ERROR) <<
"getNext: boost::exception caught: " << boost::diagnostic_information(e);
201 catch (
const std::exception& e)
203 latest_exception_report_ =
"std::exception caught in getNext(): ";
204 latest_exception_report_.append(e.what());
205 TLOG(TLVL_ERROR) <<
"getNext: std::exception caught: " << e.what();
211 latest_exception_report_ =
"Unknown exception caught in getNext().";
212 TLOG(TLVL_ERROR) <<
"getNext: unknown exception caught";
219 TLOG(TLVL_DEBUG + 32) <<
"getNext: Either getNext_ or applyRequests returned false, stopping";
222 if (metricMan && !output.empty())
224 auto timestamp = output.front()->timestamp();
226 if (output.size() > 1)
228 for (
auto& outputfrag : output)
230 if (outputfrag->timestamp() > timestamp)
232 timestamp = outputfrag->timestamp();
237 metricMan->sendMetric(
"Last Timestamp", timestamp,
"Ticks", 1, MetricMode::LastPoint);
245 TLOG(TLVL_CHECKSTOP) <<
"CFG::check_stop: should_stop=" << should_stop() <<
", exception status =" << int(exception());
247 if (!should_stop())
return false;
254 TLOG(TLVL_EVCOUNTERINC) <<
"ev_counter_inc: Incrementing ev_counter from " << ev_counter() <<
" by " << step;
255 return ev_counter_.fetch_add(step);
260 TLOG(TLVL_DEBUG + 33) <<
"Start Command received.";
263 TLOG(TLVL_ERROR) <<
"negative run number";
264 throw cet::exception(
"CommandableFragmentGenerator") <<
"negative run number";
268 timestamp_ = timestamp;
269 ev_counter_.store(1);
271 should_stop_.store(
false);
272 exception_.store(
false);
275 latest_exception_report_ =
"none";
279 std::unique_lock<std::mutex> lk(mutex_);
280 if (useMonitoringThread_) startMonitoringThread();
281 TLOG(TLVL_DEBUG + 33) <<
"Start Command complete.";
286 TLOG(TLVL_DEBUG + 33) <<
"Stop Command received.";
289 timestamp_ = timestamp;
292 should_stop_.store(
true);
293 std::unique_lock<std::mutex> lk(mutex_);
297 TLOG(TLVL_DEBUG + 33) <<
"Stop Command complete.";
302 TLOG(TLVL_DEBUG + 33) <<
"Pause Command received.";
304 timestamp_ = timestamp;
307 should_stop_.store(
true);
308 std::unique_lock<std::mutex> lk(mutex_);
315 TLOG(TLVL_DEBUG + 33) <<
"Resume Command received.";
317 timestamp_ = timestamp;
320 should_stop_ =
false;
325 std::unique_lock<std::mutex> lk(mutex_);
328 TLOG(TLVL_DEBUG + 33) <<
"Resume Command complete.";
333 TLOG(TLVL_DEBUG + 33) <<
"Report Command received.";
334 std::lock_guard<std::mutex> lk(mutex_);
341 std::string childReport = reportSpecific(which);
342 if (childReport.length() > 0) {
return childReport; }
345 if (which ==
"latest_exception")
347 return latest_exception_report_;
351 childReport = report();
352 if (childReport.length() > 0) {
return childReport; }
362 TLOG(TLVL_DEBUG + 33) <<
"Report Command complete.";
369 #pragma message "Using default implementation of CommandableFragmentGenerator::pauseNoMutex()"
374 #pragma message "Using default implementation of CommandableFragmentGenerator::pause()"
378 #pragma message "Using default implementation of CommandableFragmentGenerator::resume()"
383 #pragma message "Using default implementation of CommandableFragmentGenerator::report()"
389 #pragma message "Using default implementation of CommandableFragmentGenerator::reportSpecific(std::string)"
395 #pragma message "Using default implementation of CommandableFragmentGenerator::checkHWStatus_()"
401 #pragma message "Using default implementation of CommandableFragmentGenerator::metaCommand(std::string, std::string)"
407 if (monitoringThread_.joinable())
409 monitoringThread_.join();
411 TLOG(TLVL_INFO) <<
"Starting Hardware Monitoring Thread";
416 snprintf(tname,
sizeof(tname) - 1,
"%d-CFGMon", my_rank);
417 tname[
sizeof(tname) - 1] =
'\0';
418 auto handle = monitoringThread_.native_handle();
419 pthread_setname_np(handle, tname);
421 catch (
const boost::exception& e)
423 TLOG(TLVL_ERROR) <<
"Caught boost::exception starting Hardware Monitoring thread: " << boost::diagnostic_information(e) <<
", errno=" << errno;
424 throw cet::exception(
"ThreadError") <<
"Caught boost::exception starting Hardware Monitoring thread: " << boost::diagnostic_information(e) <<
", errno=" << errno << std::endl;
430 while (!should_stop())
432 if (should_stop() || monitoringInterval_ <= 0)
434 TLOG(TLVL_DEBUG + 32) <<
"getMonitoringDataLoop: should_stop() is " << std::boolalpha << should_stop()
435 <<
" and monitoringInterval is " << monitoringInterval_ <<
", returning";
438 TLOG(TLVL_GETMONITORINGDATA) <<
"getMonitoringDataLoop: Determining whether to call checkHWStatus_";
440 auto now = std::chrono::steady_clock::now();
441 if (TimeUtils::GetElapsedTimeMicroseconds(lastMonitoringCall_, now) >= static_cast<size_t>(monitoringInterval_))
443 isHardwareOK_ = checkHWStatus_();
444 TLOG(TLVL_GETMONITORINGDATA) <<
"getMonitoringDataLoop: isHardwareOK_ is now " << std::boolalpha << isHardwareOK_;
445 lastMonitoringCall_ = now;
447 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.