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->Initialized())
55 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
58 TLOG(TLVL_INIT) <<
"Initialize transition complete";
59 return (external_request_status_);
64 std::lock_guard<std::mutex> lk(primary_mutex_);
65 external_request_status_ =
true;
66 report_string_ =
"All is OK.";
68 SetMFModuleName(
"Starting");
70 TLOG(TLVL_START) <<
"Start transition started";
71 auto start_time = std::chrono::steady_clock::now();
72 std::string initialState = fsm_.getState().getName();
73 fsm_.start(
id, timeout, timestamp);
74 if (external_request_status_)
76 std::string finalState = fsm_.getState().getName();
78 SetMFModuleName(finalState);
80 <<
"States before and after a start transition: "
81 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
83 if (metricMan && metricMan->Initialized())
85 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
88 TLOG(TLVL_START) <<
"Start transition complete";
89 return (external_request_status_);
94 std::lock_guard<std::mutex> lk(primary_mutex_);
95 external_request_status_ =
true;
96 report_string_ =
"All is OK.";
98 SetMFModuleName(
"Stopping");
100 TLOG(TLVL_STOP) <<
"Stop transition started";
101 auto start_time = std::chrono::steady_clock::now();
102 std::string initialState = fsm_.getState().getName();
103 fsm_.stop(timeout, timestamp);
104 if (external_request_status_)
106 std::string finalState = fsm_.getState().getName();
108 SetMFModuleName(finalState);
110 <<
"States before and after a stop transition: "
111 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
113 if (metricMan && metricMan->Initialized())
115 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
118 TLOG(TLVL_STOP) <<
"Stop transition complete";
119 return (external_request_status_);
124 std::lock_guard<std::mutex> lk(primary_mutex_);
125 external_request_status_ =
true;
126 report_string_ =
"All is OK.";
128 SetMFModuleName(
"Pausing");
130 TLOG(TLVL_PAUSE) <<
"Pause transition started";
131 auto start_time = std::chrono::steady_clock::now();
132 std::string initialState = fsm_.getState().getName();
133 fsm_.pause(timeout, timestamp);
134 if (external_request_status_)
136 std::string finalState = fsm_.getState().getName();
138 SetMFModuleName(finalState);
140 <<
"States before and after a pause transition: "
141 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
143 if (metricMan && metricMan->Initialized())
145 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
148 TLOG(TLVL_PAUSE) <<
"Pause transition complete";
149 return (external_request_status_);
154 std::lock_guard<std::mutex> lk(primary_mutex_);
155 external_request_status_ =
true;
156 report_string_ =
"All is OK.";
158 SetMFModuleName(
"Resuming");
160 TLOG(TLVL_RESUME) <<
"Resume transition started";
161 auto start_time = std::chrono::steady_clock::now();
162 std::string initialState = fsm_.getState().getName();
163 fsm_.resume(timeout, timestamp);
164 if (external_request_status_)
166 std::string finalState = fsm_.getState().getName();
167 SetMFModuleName(finalState);
169 <<
"States before and after a resume transition: "
170 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
172 if (metricMan && metricMan->Initialized())
174 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
177 TLOG(TLVL_RESUME) <<
"Resume transition complete";
178 return (external_request_status_);
183 std::lock_guard<std::mutex> lk(primary_mutex_);
184 external_request_status_ =
true;
185 report_string_ =
"All is OK.";
186 SetMFModuleName(
"Shutting Down");
188 TLOG(TLVL_SHUTDOWN) <<
"Shutdown transition started";
189 auto start_time = std::chrono::steady_clock::now();
190 std::string initialState = fsm_.getState().getName();
191 fsm_.shutdown(timeout);
192 if (external_request_status_)
194 std::string finalState = fsm_.getState().getName();
195 SetMFModuleName(finalState);
197 <<
"States before and after a shutdown transition: "
198 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
200 if (metricMan && metricMan->Initialized())
202 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
205 TLOG(TLVL_SHUTDOWN) <<
"Shutdown transition complete";
206 return (external_request_status_);
211 std::lock_guard<std::mutex> lk(primary_mutex_);
212 external_request_status_ =
true;
213 report_string_ =
"All is OK.";
215 SetMFModuleName(
"Soft_initializing");
217 TLOG(TLVL_SOFT_INIT) <<
"Soft_initialize transition started";
218 auto start_time = std::chrono::steady_clock::now();
219 std::string initialState = fsm_.getState().getName();
220 fsm_.soft_init(pset, timeout, timestamp);
221 if (external_request_status_)
223 std::string finalState = fsm_.getState().getName();
225 SetMFModuleName(finalState);
227 <<
"States before and after a soft_init transition: "
228 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
230 if (metricMan && metricMan->Initialized())
232 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
235 TLOG(TLVL_SOFT_INIT) <<
"Soft_initialize transition complete";
236 return (external_request_status_);
241 std::lock_guard<std::mutex> lk(primary_mutex_);
242 external_request_status_ =
true;
243 report_string_ =
"All is OK.";
245 SetMFModuleName(
"Reinitializing");
247 TLOG(TLVL_REINIT) <<
"Reinitialize transition started";
248 auto start_time = std::chrono::steady_clock::now();
249 std::string initialState = fsm_.getState().getName();
250 fsm_.reinit(pset, timeout, timestamp);
251 if (external_request_status_)
253 std::string finalState = fsm_.getState().getName();
255 SetMFModuleName(finalState);
257 <<
"States before and after a reinit transition: "
258 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
260 if (metricMan && metricMan->Initialized())
262 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
265 TLOG(TLVL_REINIT) <<
"Reinitialize transition complete";
266 return (external_request_status_);
271 std::lock_guard<std::mutex> lk(primary_mutex_);
272 external_request_status_ =
true;
273 report_string_ =
"An error condition was reported while running.";
275 SetMFModuleName(
"Failing");
277 TLOG(TLVL_INRUN_FAILURE) <<
"In_Run_Failure transition started";
278 auto start_time = std::chrono::steady_clock::now();
279 std::string initialState = fsm_.getState().getName();
280 fsm_.in_run_failure();
281 if (external_request_status_)
283 std::string finalState = fsm_.getState().getName();
285 SetMFModuleName(finalState);
287 <<
"States before and after an in_run_failure transition: "
288 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
290 if (metricMan && metricMan->Initialized())
292 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
295 TLOG(TLVL_INRUN_FAILURE) <<
"in_run_failure complete";
296 return (external_request_status_);
301 std::lock_guard<std::mutex> lk(primary_mutex_);
302 TLOG(TLVL_STATUS) <<
"Status command started";
303 std::string currentState = this->current_state();
304 if (currentState ==
"InRunError")
308 TLOG(TLVL_STATUS) <<
"Status command complete";
314 std::lock_guard<std::mutex> lk(primary_mutex_);
315 TLOG(TLVL_LEGAL_COMMANDS) <<
"legal_commands started";
316 std::string currentState = this->current_state();
318 if (currentState ==
"Ready")
320 return {
"init",
"soft_init",
"start",
"shutdown"};
322 if (currentState ==
"Running")
326 return {
"pause",
"stop"};
328 if (currentState ==
"Paused")
330 return {
"resume",
"stop"};
332 if (currentState ==
"InRunError")
334 return {
"pause",
"stop"};
338 TLOG(TLVL_LEGAL_COMMANDS) <<
"legal_commands complete";
339 return {
"init",
"shutdown"};
348 TLOG(TLVL_DEBUG) <<
"do_initialize called.";
349 external_request_status_ =
true;
350 return external_request_status_;
355 TLOG(TLVL_DEBUG) <<
"do_start called.";
356 external_request_status_ =
true;
357 return external_request_status_;
362 TLOG(TLVL_DEBUG) <<
"do_stop called.";
363 external_request_status_ =
true;
364 return external_request_status_;
369 TLOG(TLVL_DEBUG) <<
"do_pause called.";
370 external_request_status_ =
true;
371 return external_request_status_;
376 TLOG(TLVL_DEBUG) <<
"do_resume called.";
377 external_request_status_ =
true;
378 return external_request_status_;
383 TLOG(TLVL_DEBUG) <<
"do_shutdown called.";
384 external_request_status_ =
true;
385 return external_request_status_;
390 TLOG(TLVL_DEBUG) <<
"do_reinitialize called.";
391 external_request_status_ =
true;
392 return external_request_status_;
397 TLOG(TLVL_DEBUG) <<
"do_soft_initialize called.";
398 external_request_status_ =
true;
399 return external_request_status_;
404 std::string currentState = this->current_state();
405 if (currentState ==
"InRunError")
407 currentState =
"Error";
410 report_string_ =
"An invalid transition (";
411 report_string_.append(trans);
412 report_string_.append(
") was requested; transition not allowed from this process's current state of \"");
413 report_string_.append(currentState);
414 report_string_.append(
"\"");
416 TLOG(TLVL_WARNING) << report_string_;
418 external_request_status_ =
false;
423 TLOG(TLVL_DEBUG) <<
"BootedEnter called.";
428 TLOG(TLVL_DEBUG) <<
"InRunExit called.";
433 TLOG(TLVL_DEBUG) <<
"Getting masks for name " << name;
434 std::ostringstream ss;
438 unsigned ee = traceControl_p->num_namLvlTblEnts;
439 for (ii = 0; ii < ee; ++ii)
441 if (traceNamLvls_p[ii].name[0] != 0)
443 ss << traceNamLvls_p[ii].name <<
" " << std::hex << std::showbase << traceNamLvls_p[ii].M <<
" " << traceNamLvls_p[ii].S <<
" " << traceNamLvls_p[ii].T <<
" " << std::endl;
450 unsigned ee = traceControl_p->num_namLvlTblEnts;
451 for (ii = 0; ii < ee; ++ii)
453 if ((traceNamLvls_p[ii].name[0] != 0) && TMATCHCMP(name.c_str(), traceNamLvls_p[ii].name))
463 ss << std::hex << traceNamLvls_p[ii].M <<
" " << traceNamLvls_p[ii].S <<
" " << traceNamLvls_p[ii].T;
470 TLOG(TLVL_DEBUG) <<
"Setting msk " << type <<
" for name " << name <<
" to " << mask_in_string_form;
471 auto mask =
static_cast<uint64_t
>(std::stoull(mask_in_string_form,
nullptr, 0));
472 TLOG(TLVL_DEBUG) <<
"Mask has been converted to " << mask;
481 TRACE_CNTL(
"lvlmsknM", name.c_str(), mask);
484 TRACE_CNTL(
"lvlmsknS", name.c_str(), mask);
487 TRACE_CNTL(
"lvlmsknT", name.c_str(), mask);
493 TLOG(TLVL_ERROR) <<
"Cannot set mask: no type specified!";
503 TRACE_CNTL(
"lvlmskMg", mask);
506 TRACE_CNTL(
"lvlmskSg", mask);
509 TRACE_CNTL(
"lvlmskTg", mask);
515 TLOG(TLVL_ERROR) <<
"Cannot set mask: no type specified!";
523 TLOG(TLVL_DEBUG) <<
"Meta-Command called: cmd=" << cmd <<
", arg=" << arg;
529 TLOG(TLVL_DEBUG) <<
"do_rollover_subrun called.";
530 external_request_status_ =
true;
531 return external_request_status_;
536 TLOG(TLVL_DEBUG) <<
"do_add_config_archive_entry called: key=" << key <<
", value=" << value;
542 TLOG(TLVL_DEBUG) <<
"do_clear_config_archive called.";
543 external_request_status_ =
true;
544 return external_request_status_;
553 std::string fullStateName = (
const_cast<Commandable*
>(
this))->fsm_.getState().getName();
554 size_t pos = fullStateName.rfind(
"::");
555 if (pos != std::string::npos)
557 return fullStateName.substr(pos + 2);
560 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_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...
virtual bool do_meta_command(std::string const &cmd, std::string const &args)
Run a module-defined command with the given parameter string.
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.