DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
LivetimeCounter.cpp
Go to the documentation of this file.
2
3#include "logging/Logging.hpp"
4
5#include <sstream>
6
7namespace dunedaq::trigger {
8
10 : m_state(state)
12{
13 TLOG_DEBUG(1) << "Starting LivetimeCounter in state " << get_state_name(state);
17}
18
20{
21 std::string report = get_report_string();
22 TLOG() << "LivetimeCounter stopping. Counts: " << report;
23}
24
25void
27{
28 // Caller must lock the mutex, so we don't here
29 auto current_time = now();
30 auto delta = (current_time - m_last_state_change_time);
31 m_state_times[m_state] += delta;
32 m_last_state_change_time = current_time;
33}
34
35void
37{
38 std::lock_guard<std::mutex> l(m_mutex);
39 update_map(); // Add the time to the old state
40 TLOG_DEBUG(1) << "Changing state from " << get_state_name(m_state) << " to " << get_state_name(state);
41 m_state = state;
42}
43
44std::map<LivetimeCounter::State, LivetimeCounter::state_time_t>
46{
47 std::lock_guard<std::mutex> l(m_mutex);
48 update_map();
49 return m_state_times;
50}
51
54{
55 std::lock_guard<std::mutex> l(m_mutex);
56 update_map();
57 return m_state_times[state];
58}
59
62{
63 using namespace std::chrono;
64 return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
65}
66
67std::string
69{
70 std::lock_guard<std::mutex> l(m_mutex);
71 update_map();
72 std::ostringstream oss;
73 for (auto const& [state, t] : m_state_times) {
74 oss << get_state_name(state) << ": " << t << "ms ";
75 }
76 return oss.str();
77}
78
79std::string
81{
82 switch (state) {
83 case State::kLive:
84 return "live";
85 case State::kDead:
86 return "dead";
87 case State::kPaused:
88 return "paused";
89 default:
90 return "UNKNOWN";
91 }
92}
93
94} // namespace dunedaq::trigger
uint64_t state_time_t
A type to store a time duration in milliseconds.
std::map< State, state_time_t > m_state_times
LivetimeCounter(State state)
Construct a LivetimeCounter in the given state.
std::string get_state_name(State state) const
std::string get_report_string()
Get a nicely-formatted string of the time spent in each state.
std::map< State, state_time_t > get_time_map()
Get a map of accumulated time in milliseconds in each state.
void set_state(State state)
Set the current state to state.
state_time_t get_time(State state)
Get the accumulated time in milliseconds spent in a particular state.
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:116
#define TLOG(...)
Definition macro.hpp:21