1 #include "TRACE/tracemf.h"
2 #include "artdaq/DAQdata/Globals.hh"
3 #define TRACE_NAME (app_name + "_CommandableInterface").c_str() // definition with variable should be after includes (which may or may not have TLOG/TRACE statements)
5 #include "artdaq-core/Utilities/TimeUtils.hh"
6 #include "artdaq/Application/Commandable.hh"
11 #define TLVL_INIT TLVL_INFO
12 #define TLVL_STOP TLVL_INFO
13 #define TLVL_STATUS TLVL_DEBUG + 33
14 #define TLVL_START TLVL_INFO
15 #define TLVL_PAUSE TLVL_INFO
16 #define TLVL_RESUME TLVL_INFO
17 #define TLVL_SHUTDOWN TLVL_INFO
18 #define TLVL_ROLLOVER TLVL_INFO
19 #define TLVL_SOFT_INIT TLVL_INFO
20 #define TLVL_REINIT TLVL_INFO
21 #define TLVL_INRUN_FAILURE TLVL_INFO
22 #define TLVL_LEGAL_COMMANDS TLVL_INFO
35 std::lock_guard<std::mutex> lk(primary_mutex_);
36 external_request_status_ =
true;
37 report_string_ =
"All is OK.";
39 SetMFModuleName(
"Initializing");
41 TLOG(TLVL_INIT) <<
"Initialize transition started";
42 auto start_time = std::chrono::steady_clock::now();
43 std::string initialState = fsm_.getState().getName();
44 fsm_.init(pset, timeout, timestamp);
45 if (external_request_status_)
47 std::string finalState = fsm_.getState().getName();
49 SetMFModuleName(finalState);
51 <<
"States before and after an init transition: "
52 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
54 if (metricMan && metricMan->Initialized())
56 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
59 TLOG(TLVL_INIT) <<
"Initialize transition complete";
60 return (external_request_status_);
65 std::lock_guard<std::mutex> lk(primary_mutex_);
66 external_request_status_ =
true;
67 report_string_ =
"All is OK.";
69 SetMFModuleName(
"Starting");
71 TLOG(TLVL_START) <<
"Start transition started";
72 auto start_time = std::chrono::steady_clock::now();
73 std::string initialState = fsm_.getState().getName();
74 fsm_.start(
id, timeout, timestamp);
75 if (external_request_status_)
77 std::string finalState = fsm_.getState().getName();
79 SetMFModuleName(finalState);
81 <<
"States before and after a start transition: "
82 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
84 if (metricMan && metricMan->Initialized())
86 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
89 TLOG(TLVL_START) <<
"Start transition complete";
90 return (external_request_status_);
95 std::lock_guard<std::mutex> lk(primary_mutex_);
96 external_request_status_ =
true;
97 report_string_ =
"All is OK.";
99 SetMFModuleName(
"Stopping");
101 TLOG(TLVL_STOP) <<
"Stop transition started";
102 auto start_time = std::chrono::steady_clock::now();
103 std::string initialState = fsm_.getState().getName();
104 fsm_.stop(timeout, timestamp);
105 if (external_request_status_)
107 std::string finalState = fsm_.getState().getName();
109 SetMFModuleName(finalState);
110 TLOG(TLVL_DEBUG + 32)
111 <<
"States before and after a stop transition: "
112 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
114 if (metricMan && metricMan->Initialized())
116 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
119 TLOG(TLVL_STOP) <<
"Stop transition complete";
120 return (external_request_status_);
125 std::lock_guard<std::mutex> lk(primary_mutex_);
126 external_request_status_ =
true;
127 report_string_ =
"All is OK.";
129 SetMFModuleName(
"Pausing");
131 TLOG(TLVL_PAUSE) <<
"Pause transition started";
132 auto start_time = std::chrono::steady_clock::now();
133 std::string initialState = fsm_.getState().getName();
134 fsm_.pause(timeout, timestamp);
135 if (external_request_status_)
137 std::string finalState = fsm_.getState().getName();
139 SetMFModuleName(finalState);
140 TLOG(TLVL_DEBUG + 32)
141 <<
"States before and after a pause transition: "
142 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
144 if (metricMan && metricMan->Initialized())
146 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
149 TLOG(TLVL_PAUSE) <<
"Pause transition complete";
150 return (external_request_status_);
155 std::lock_guard<std::mutex> lk(primary_mutex_);
156 external_request_status_ =
true;
157 report_string_ =
"All is OK.";
159 SetMFModuleName(
"Resuming");
161 TLOG(TLVL_RESUME) <<
"Resume transition started";
162 auto start_time = std::chrono::steady_clock::now();
163 std::string initialState = fsm_.getState().getName();
164 fsm_.resume(timeout, timestamp);
165 if (external_request_status_)
167 std::string finalState = fsm_.getState().getName();
168 SetMFModuleName(finalState);
169 TLOG(TLVL_DEBUG + 32)
170 <<
"States before and after a resume transition: "
171 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
173 if (metricMan && metricMan->Initialized())
175 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
178 TLOG(TLVL_RESUME) <<
"Resume transition complete";
179 return (external_request_status_);
184 std::lock_guard<std::mutex> lk(primary_mutex_);
185 external_request_status_ =
true;
186 report_string_ =
"All is OK.";
187 SetMFModuleName(
"Shutting Down");
189 TLOG(TLVL_SHUTDOWN) <<
"Shutdown transition started";
190 auto start_time = std::chrono::steady_clock::now();
191 std::string initialState = fsm_.getState().getName();
192 fsm_.shutdown(timeout);
193 if (external_request_status_)
195 std::string finalState = fsm_.getState().getName();
197 TLOG(TLVL_DEBUG + 32)
198 <<
"States before and after a shutdown transition: "
199 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
201 if (metricMan && metricMan->Initialized())
203 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
206 TLOG(TLVL_SHUTDOWN) <<
"Shutdown transition complete";
207 return (external_request_status_);
212 std::lock_guard<std::mutex> lk(primary_mutex_);
213 external_request_status_ =
true;
214 report_string_ =
"All is OK.";
216 SetMFModuleName(
"Soft_initializing");
218 TLOG(TLVL_SOFT_INIT) <<
"Soft_initialize transition started";
219 auto start_time = std::chrono::steady_clock::now();
220 std::string initialState = fsm_.getState().getName();
221 fsm_.soft_init(pset, timeout, timestamp);
222 if (external_request_status_)
224 std::string finalState = fsm_.getState().getName();
226 SetMFModuleName(finalState);
227 TLOG(TLVL_DEBUG + 32)
228 <<
"States before and after a soft_init transition: "
229 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
231 if (metricMan && metricMan->Initialized())
233 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
236 TLOG(TLVL_SOFT_INIT) <<
"Soft_initialize transition complete";
237 return (external_request_status_);
242 std::lock_guard<std::mutex> lk(primary_mutex_);
243 external_request_status_ =
true;
244 report_string_ =
"All is OK.";
246 SetMFModuleName(
"Reinitializing");
248 TLOG(TLVL_REINIT) <<
"Reinitialize transition started";
249 auto start_time = std::chrono::steady_clock::now();
250 std::string initialState = fsm_.getState().getName();
251 fsm_.reinit(pset, timeout, timestamp);
252 if (external_request_status_)
254 std::string finalState = fsm_.getState().getName();
256 SetMFModuleName(finalState);
257 TLOG(TLVL_DEBUG + 32)
258 <<
"States before and after a reinit transition: "
259 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
261 if (metricMan && metricMan->Initialized())
263 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
266 TLOG(TLVL_REINIT) <<
"Reinitialize transition complete";
267 return (external_request_status_);
272 std::lock_guard<std::mutex> lk(primary_mutex_);
273 external_request_status_ =
true;
274 report_string_ =
"An error condition was reported while running.";
276 SetMFModuleName(
"Failing");
278 TLOG(TLVL_INRUN_FAILURE) <<
"In_Run_Failure transition started";
279 auto start_time = std::chrono::steady_clock::now();
280 std::string initialState = fsm_.getState().getName();
281 fsm_.in_run_failure();
282 if (external_request_status_)
284 std::string finalState = fsm_.getState().getName();
286 SetMFModuleName(finalState);
287 TLOG(TLVL_DEBUG + 32)
288 <<
"States before and after an in_run_failure transition: "
289 << initialState <<
" and " << finalState <<
". Transition Duration: " << TimeUtils::GetElapsedTime(start_time) <<
" s.";
291 if (metricMan && metricMan->Initialized())
293 metricMan->sendMetric(
"DAQ Transition Time", TimeUtils::GetElapsedTime(start_time),
"s", 4, artdaq::MetricMode::Accumulate);
296 TLOG(TLVL_INRUN_FAILURE) <<
"in_run_failure complete";
297 return (external_request_status_);
302 std::lock_guard<std::mutex> lk(primary_mutex_);
303 TLOG(TLVL_STATUS) <<
"Status command started";
304 std::string currentState = this->current_state();
305 if (currentState ==
"InRunError")
309 TLOG(TLVL_STATUS) <<
"Status command complete";
315 std::lock_guard<std::mutex> lk(primary_mutex_);
316 TLOG(TLVL_LEGAL_COMMANDS) <<
"legal_commands started";
317 std::string currentState = this->current_state();
319 if (currentState ==
"Ready")
321 return {
"init",
"soft_init",
"start",
"shutdown"};
323 if (currentState ==
"Running")
327 return {
"pause",
"stop"};
329 if (currentState ==
"Paused")
331 return {
"resume",
"stop"};
333 if (currentState ==
"InRunError")
335 return {
"pause",
"stop"};
339 TLOG(TLVL_LEGAL_COMMANDS) <<
"legal_commands complete";
340 return {
"init",
"shutdown"};
349 TLOG(TLVL_DEBUG + 32) <<
"do_initialize called.";
350 external_request_status_ =
true;
351 return external_request_status_;
356 TLOG(TLVL_DEBUG + 32) <<
"do_start called.";
357 external_request_status_ =
true;
358 return external_request_status_;
363 TLOG(TLVL_DEBUG + 32) <<
"do_stop called.";
364 external_request_status_ =
true;
365 return external_request_status_;
370 TLOG(TLVL_DEBUG + 32) <<
"do_pause called.";
371 external_request_status_ =
true;
372 return external_request_status_;
377 TLOG(TLVL_DEBUG + 32) <<
"do_resume called.";
378 external_request_status_ =
true;
379 return external_request_status_;
384 TLOG(TLVL_DEBUG + 32) <<
"do_shutdown called.";
385 external_request_status_ =
true;
386 return external_request_status_;
391 TLOG(TLVL_DEBUG + 32) <<
"do_reinitialize called.";
392 external_request_status_ =
true;
393 return external_request_status_;
398 TLOG(TLVL_DEBUG + 32) <<
"do_soft_initialize called.";
399 external_request_status_ =
true;
400 return external_request_status_;
405 std::string currentState = this->current_state();
406 if (currentState ==
"InRunError")
408 currentState =
"Error";
411 report_string_ =
"An invalid transition (";
412 report_string_.append(trans);
413 report_string_.append(
") was requested; transition not allowed from this process's current state of \"");
414 report_string_.append(currentState);
415 report_string_.append(
"\"");
417 TLOG(TLVL_WARNING) << report_string_;
419 external_request_status_ =
false;
424 TLOG(TLVL_DEBUG + 32) <<
"BootedEnter called.";
429 TLOG(TLVL_DEBUG + 32) <<
"InRunExit called.";
432 #if TRACE_REVNUM < 1394
433 #define traceLvls_p traceNamLvls_p
434 #define TRACE_TID2NAME(idx) traceNamLvls_p[idx].name
438 TLOG(TLVL_DEBUG + 32) <<
"Getting masks for name " << name;
439 std::ostringstream ss;
443 unsigned ee = traceControl_p->num_namLvlTblEnts;
444 for (ii = 0; ii < ee; ++ii)
446 if (TRACE_TID2NAME(ii)[0] != 0)
448 ss << TRACE_TID2NAME(ii) <<
" " << std::hex << std::showbase << traceLvls_p[ii].M <<
" " << traceLvls_p[ii].S <<
" " << traceLvls_p[ii].T <<
" " << std::endl;
455 unsigned ee = traceControl_p->num_namLvlTblEnts;
456 for (ii = 0; ii < ee; ++ii)
458 if ((TRACE_TID2NAME(ii)[0] != 0) && TMATCHCMP(name.c_str(), TRACE_TID2NAME(ii)))
468 ss << std::hex << traceLvls_p[ii].M <<
" " << traceLvls_p[ii].S <<
" " << traceLvls_p[ii].T;
475 TLOG(TLVL_DEBUG + 32) <<
"Setting msk " << type <<
" for name " << name <<
" to " << mask_in_string_form;
476 auto mask =
static_cast<uint64_t
>(std::stoull(mask_in_string_form,
nullptr, 0));
477 TLOG(TLVL_DEBUG + 32) <<
"Mask has been converted to " << mask;
486 TRACE_CNTL(
"lvlmsknM", name.c_str(), mask);
489 TRACE_CNTL(
"lvlmsknS", name.c_str(), mask);
492 TRACE_CNTL(
"lvlmsknT", name.c_str(), mask);
498 TLOG(TLVL_ERROR) <<
"Cannot set mask: no type specified!";
508 TRACE_CNTL(
"lvlmskMg", mask);
511 TRACE_CNTL(
"lvlmskSg", mask);
514 TRACE_CNTL(
"lvlmskTg", mask);
520 TLOG(TLVL_ERROR) <<
"Cannot set mask: no type specified!";
528 TLOG(TLVL_DEBUG + 32) <<
"Meta-Command called: cmd=" << cmd <<
", arg=" << arg;
534 TLOG(TLVL_DEBUG + 32) <<
"do_rollover_subrun called.";
535 external_request_status_ =
true;
536 return external_request_status_;
541 TLOG(TLVL_DEBUG + 32) <<
"do_add_config_archive_entry called: key=" << key <<
", value=" << value;
547 TLOG(TLVL_DEBUG + 32) <<
"do_clear_config_archive called.";
548 external_request_status_ =
true;
549 return external_request_status_;
558 std::string fullStateName = (
const_cast<Commandable*
>(
this))->fsm_.getState().getName();
559 size_t pos = fullStateName.rfind(
"::");
560 if (pos != std::string::npos)
562 return fullStateName.substr(pos + 2);
565 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.