2 #include "artdaq/Application/Commandable.hh"
3 #include "artdaq/DAQdata/Globals.hh"
5 #define TRACE_NAME (app_name + "_CommandableInterface").c_str() // definition with variable should be after includes (which may or may not have TLOG/TRACE statements)
10 #define TLVL_INIT TLVL_INFO
11 #define TLVL_STOP TLVL_INFO
12 #define TLVL_STATUS TLVL_TRACE
13 #define TLVL_START TLVL_INFO
14 #define TLVL_PAUSE TLVL_INFO
15 #define TLVL_RESUME TLVL_INFO
16 #define TLVL_SHUTDOWN TLVL_INFO
17 #define TLVL_ROLLOVER TLVL_INFO
18 #define TLVL_SOFT_INIT TLVL_INFO
19 #define TLVL_REINIT TLVL_INFO
20 #define TLVL_INRUN_FAILURE TLVL_INFO
21 #define TLVL_LEGAL_COMMANDS TLVL_INFO
34 std::lock_guard<std::mutex> lk(primary_mutex_);
35 external_request_status_ =
true;
36 report_string_ =
"All is OK.";
38 SetMFModuleName(
"Initializing");
40 TLOG(TLVL_INIT) <<
"Initialize transition started";
41 auto start_time = std::chrono::steady_clock::now();
42 std::string initialState = fsm_.getState().getName();
43 fsm_.init(pset, timeout, timestamp);
44 if (external_request_status_)
46 std::string finalState = fsm_.getState().getName();
48 SetMFModuleName(finalState);
50 <<
"States before and after an init transition: "
51 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
53 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
55 TLOG(TLVL_INIT) <<
"Initialize transition complete";
56 return (external_request_status_);
61 std::lock_guard<std::mutex> lk(primary_mutex_);
62 external_request_status_ =
true;
63 report_string_ =
"All is OK.";
65 SetMFModuleName(
"Starting");
67 TLOG(TLVL_START) <<
"Start transition started";
68 auto start_time = std::chrono::steady_clock::now();
69 std::string initialState = fsm_.getState().getName();
70 fsm_.start(
id, timeout, timestamp);
71 if (external_request_status_)
73 std::string finalState = fsm_.getState().getName();
75 SetMFModuleName(finalState);
77 <<
"States before and after a start transition: "
78 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
80 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
82 TLOG(TLVL_START) <<
"Start transition complete";
83 return (external_request_status_);
88 std::lock_guard<std::mutex> lk(primary_mutex_);
89 external_request_status_ =
true;
90 report_string_ =
"All is OK.";
92 SetMFModuleName(
"Stopping");
94 TLOG(TLVL_STOP) <<
"Stop transition started";
95 auto start_time = std::chrono::steady_clock::now();
96 std::string initialState = fsm_.getState().getName();
97 fsm_.stop(timeout, timestamp);
98 if (external_request_status_)
100 std::string finalState = fsm_.getState().getName();
102 SetMFModuleName(finalState);
104 <<
"States before and after a stop transition: "
105 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
107 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
109 TLOG(TLVL_STOP) <<
"Stop transition complete";
110 return (external_request_status_);
115 std::lock_guard<std::mutex> lk(primary_mutex_);
116 external_request_status_ =
true;
117 report_string_ =
"All is OK.";
119 SetMFModuleName(
"Pausing");
121 TLOG(TLVL_PAUSE) <<
"Pause transition started";
122 auto start_time = std::chrono::steady_clock::now();
123 std::string initialState = fsm_.getState().getName();
124 fsm_.pause(timeout, timestamp);
125 if (external_request_status_)
127 std::string finalState = fsm_.getState().getName();
129 SetMFModuleName(finalState);
131 <<
"States before and after a pause transition: "
132 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
134 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
136 TLOG(TLVL_PAUSE) <<
"Pause transition complete";
137 return (external_request_status_);
142 std::lock_guard<std::mutex> lk(primary_mutex_);
143 external_request_status_ =
true;
144 report_string_ =
"All is OK.";
146 SetMFModuleName(
"Resuming");
148 TLOG(TLVL_RESUME) <<
"Resume transition started";
149 auto start_time = std::chrono::steady_clock::now();
150 std::string initialState = fsm_.getState().getName();
151 fsm_.resume(timeout, timestamp);
152 if (external_request_status_)
154 std::string finalState = fsm_.getState().getName();
155 SetMFModuleName(finalState);
157 <<
"States before and after a resume transition: "
158 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
160 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
162 TLOG(TLVL_RESUME) <<
"Resume transition complete";
163 return (external_request_status_);
168 std::lock_guard<std::mutex> lk(primary_mutex_);
169 external_request_status_ =
true;
170 report_string_ =
"All is OK.";
171 SetMFModuleName(
"Shutting Down");
173 TLOG(TLVL_SHUTDOWN) <<
"Shutdown transition started";
174 auto start_time = std::chrono::steady_clock::now();
175 std::string initialState = fsm_.getState().getName();
176 fsm_.shutdown(timeout);
177 if (external_request_status_)
179 std::string finalState = fsm_.getState().getName();
180 SetMFModuleName(finalState);
182 <<
"States before and after a shutdown transition: "
183 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
185 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
187 TLOG(TLVL_SHUTDOWN) <<
"Shutdown transition complete";
188 return (external_request_status_);
193 std::lock_guard<std::mutex> lk(primary_mutex_);
194 external_request_status_ =
true;
195 report_string_ =
"All is OK.";
197 SetMFModuleName(
"Soft_initializing");
199 TLOG(TLVL_SOFT_INIT) <<
"Soft_initialize transition started";
200 auto start_time = std::chrono::steady_clock::now();
201 std::string initialState = fsm_.getState().getName();
202 fsm_.soft_init(pset, timeout, timestamp);
203 if (external_request_status_)
205 std::string finalState = fsm_.getState().getName();
207 SetMFModuleName(finalState);
209 <<
"States before and after a soft_init transition: "
210 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
212 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
214 TLOG(TLVL_SOFT_INIT) <<
"Soft_initialize transition complete";
215 return (external_request_status_);
220 std::lock_guard<std::mutex> lk(primary_mutex_);
221 external_request_status_ =
true;
222 report_string_ =
"All is OK.";
224 SetMFModuleName(
"Reinitializing");
226 TLOG(TLVL_REINIT) <<
"Reinitialize transition started";
227 auto start_time = std::chrono::steady_clock::now();
228 std::string initialState = fsm_.getState().getName();
229 fsm_.reinit(pset, timeout, timestamp);
230 if (external_request_status_)
232 std::string finalState = fsm_.getState().getName();
234 SetMFModuleName(finalState);
236 <<
"States before and after a reinit transition: "
237 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
239 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
241 TLOG(TLVL_REINIT) <<
"Reinitialize transition complete";
242 return (external_request_status_);
247 std::lock_guard<std::mutex> lk(primary_mutex_);
248 external_request_status_ =
true;
249 report_string_ =
"An error condition was reported while running.";
251 SetMFModuleName(
"Failing");
253 TLOG(TLVL_INRUN_FAILURE) <<
"In_Run_Failure transition started";
254 auto start_time = std::chrono::steady_clock::now();
255 std::string initialState = fsm_.getState().getName();
256 fsm_.in_run_failure();
257 if (external_request_status_)
259 std::string finalState = fsm_.getState().getName();
261 SetMFModuleName(finalState);
263 <<
"States before and after an in_run_failure transition: "
264 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
266 if (metricMan) metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
268 TLOG(TLVL_INRUN_FAILURE) <<
"in_run_failure complete";
269 return (external_request_status_);
274 std::lock_guard<std::mutex> lk(primary_mutex_);
275 TLOG(TLVL_STATUS) <<
"Status command started";
276 std::string currentState = this->current_state();
277 if (currentState ==
"InRunError")
281 TLOG(TLVL_STATUS) <<
"Status command complete";
287 std::lock_guard<std::mutex> lk(primary_mutex_);
288 TLOG(TLVL_LEGAL_COMMANDS) <<
"legal_commands started";
289 std::string currentState = this->current_state();
291 if (currentState ==
"Ready")
293 return {
"init",
"soft_init",
"start",
"shutdown"};
295 if (currentState ==
"Running")
299 return {
"pause",
"stop"};
301 if (currentState ==
"Paused")
303 return {
"resume",
"stop"};
305 if (currentState ==
"InRunError")
307 return {
"pause",
"stop"};
311 TLOG(TLVL_LEGAL_COMMANDS) <<
"legal_commands complete";
312 return {
"init",
"shutdown"};
321 TLOG(TLVL_DEBUG) <<
"do_initialize called.";
322 external_request_status_ =
true;
323 return external_request_status_;
328 TLOG(TLVL_DEBUG) <<
"do_start called.";
329 external_request_status_ =
true;
330 return external_request_status_;
335 TLOG(TLVL_DEBUG) <<
"do_stop called.";
336 external_request_status_ =
true;
337 return external_request_status_;
342 TLOG(TLVL_DEBUG) <<
"do_pause called.";
343 external_request_status_ =
true;
344 return external_request_status_;
349 TLOG(TLVL_DEBUG) <<
"do_resume called.";
350 external_request_status_ =
true;
351 return external_request_status_;
356 TLOG(TLVL_DEBUG) <<
"do_shutdown called.";
357 external_request_status_ =
true;
358 return external_request_status_;
363 TLOG(TLVL_DEBUG) <<
"do_reinitialize called.";
364 external_request_status_ =
true;
365 return external_request_status_;
370 TLOG(TLVL_DEBUG) <<
"do_soft_initialize called.";
371 external_request_status_ =
true;
372 return external_request_status_;
377 std::string currentState = this->current_state();
378 if (currentState ==
"InRunError")
380 currentState =
"Error";
383 report_string_ =
"An invalid transition (";
384 report_string_.append(trans);
385 report_string_.append(
") was requested; transition not allowed from this process's current state of \"");
386 report_string_.append(currentState);
387 report_string_.append(
"\"");
389 TLOG(TLVL_WARNING) << report_string_;
391 external_request_status_ =
false;
396 TLOG(TLVL_DEBUG) <<
"BootedEnter called.";
401 TLOG(TLVL_DEBUG) <<
"InRunExit called.";
406 TLOG(TLVL_DEBUG) <<
"Getting masks for name " << name;
407 std::ostringstream ss;
411 unsigned ee = traceControl_p->num_namLvlTblEnts;
412 for (ii = 0; ii < ee; ++ii)
414 if (traceNamLvls_p[ii].name[0])
415 ss << traceNamLvls_p[ii].name <<
" " << std::hex << std::showbase << traceNamLvls_p[ii].M <<
" " << traceNamLvls_p[ii].S <<
" " << traceNamLvls_p[ii].T <<
" " << std::endl;
421 unsigned ee = traceControl_p->num_namLvlTblEnts;
422 for (ii = 0; ii < ee; ++ii)
424 if (traceNamLvls_p[ii].name[0] && TMATCHCMP(name.c_str(), traceNamLvls_p[ii].name))
break;
426 if (ii == ee)
return "";
428 ss << std::hex << traceNamLvls_p[ii].M <<
" " << traceNamLvls_p[ii].S <<
" " << traceNamLvls_p[ii].T;
435 TLOG(TLVL_DEBUG) <<
"Setting msk " << type <<
" for name " << name <<
" to " << mask_in_string_form;
436 auto mask =
static_cast<uint64_t
>(std::stoull(mask_in_string_form,
nullptr, 0));
437 TLOG(TLVL_DEBUG) <<
"Mask has been converted to " << mask;
446 TRACE_CNTL(
"lvlmsknM", name.c_str(), mask);
449 TRACE_CNTL(
"lvlmsknS", name.c_str(), mask);
452 TRACE_CNTL(
"lvlmsknT", name.c_str(), mask);
458 TLOG(TLVL_ERROR) <<
"Cannot set mask: no type specified!";
468 TRACE_CNTL(
"lvlmskMg", mask);
471 TRACE_CNTL(
"lvlmskSg", mask);
474 TRACE_CNTL(
"lvlmskTg", mask);
480 TLOG(TLVL_ERROR) <<
"Cannot set mask: no type specified!";
488 TLOG(TLVL_DEBUG) <<
"Meta-Command called: cmd=" << cmd <<
", arg=" << arg;
494 TLOG(TLVL_DEBUG) <<
"do_rollover_subrun called.";
495 external_request_status_ =
true;
496 return external_request_status_;
501 TLOG(TLVL_DEBUG) <<
"do_add_config_archive_entry called: key=" << key <<
", value=" << value;
507 TLOG(TLVL_DEBUG) <<
"do_clear_config_archive called.";
508 external_request_status_ =
true;
509 return external_request_status_;
518 std::string fullStateName = (
const_cast<Commandable*
>(
this))->fsm_.getState().getName();
519 size_t pos = fullStateName.rfind(
"::");
520 if (pos != std::string::npos)
522 return fullStateName.substr(pos + 2);
526 return fullStateName;
virtual bool do_add_config_archive_entry(std::string const &, std::string const &)
Add the specified key-value pair to the configuration archive list.
bool initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Processes the initialize request.
virtual bool do_start(art::RunID, uint64_t, uint64_t)
Perform the start transition.
Commandable is the base class for all artdaq components which implement the artdaq state machine...
virtual bool do_clear_config_archive()
Clears the configuration archive list.
virtual void badTransition(const std::string &trans)
This function is called when an attempt is made to call an illegal transition.
virtual void InRunExit()
Perform actions upon leaving the InRun state.
virtual bool do_pause(uint64_t, uint64_t)
Perform the pause transition.
bool reinitialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Processes the reinitialize request.
std::string current_state() const
Return the name of the current state.
virtual void BootedEnter()
Perform actions upon entering the Booted state.
std::string status() const
Returns the current state of the Commandable.
virtual bool do_shutdown(uint64_t)
Perform the shutdown transition.
virtual bool do_soft_initialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the soft_initialize transition.
virtual bool do_meta_command(std::string const &command, std::string const &args)
Run a module-defined command with the given parameter string.
virtual bool do_trace_set(std::string const &name, std::string const &type, std::string const &mask_in_string_form)
Set the given TRACE mask for the given TRACE name.
bool resume(uint64_t timeout, uint64_t timestamp)
Processes the resume transition.
bool pause(uint64_t timeout, uint64_t timestamp)
Processes the pause transition.
virtual bool do_stop(uint64_t, uint64_t)
Perform the stop transition.
bool in_run_failure()
Actions taken when the in_run_failure state is set.
std::vector< std::string > legal_commands() const
Get the legal transition commands from the current state.
bool soft_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Processes the soft-initialize request.
virtual bool do_resume(uint64_t, uint64_t)
Perform the resume transition.
bool start(art::RunID id, uint64_t timeout, uint64_t timestamp)
Processes the start transition.
virtual bool do_reinitialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the reinitialize transition.
virtual std::string do_trace_get(std::string const &name)
Get the TRACE mask for the given TRACE name If name is "ALL", then all TRACE masks will be printed...
bool stop(uint64_t timeout, uint64_t timestamp)
Processes the stop transition.
virtual bool do_rollover_subrun(uint64_t eventNum, uint32_t subrunNum)
Perform the rollover_subrun transition.
virtual bool do_initialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the initialize transition.
bool shutdown(uint64_t timeout)
Processes the shutdown transition.