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)
11 , m_last_state_change_time(now())
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
35
36void
38{
39 std::lock_guard<std::mutex> l(m_mutex);
40 update_map(); // Add the time to the old state
41 TLOG_DEBUG(1) << "Changing state from " << get_state_name(m_state) << " to " << get_state_name(state);
42 m_state=state;
43}
44
45std::map<LivetimeCounter::State, LivetimeCounter::state_time_t>
47{
48 std::lock_guard<std::mutex> l(m_mutex);
49 update_map();
50 return m_state_times;
51}
52
55{
56 std::lock_guard<std::mutex> l(m_mutex);
57 update_map();
58 return m_state_times[state];
59}
60
63{
64 using namespace std::chrono;
65 return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
66}
67
68std::string
70{
71 std::lock_guard<std::mutex> l(m_mutex);
72 update_map();
73 std::ostringstream oss;
74 for(auto const& [state, t]: m_state_times){
75 oss << get_state_name(state) << ": " << t << "ms ";
76 }
77 return oss.str();
78}
79
80
81std::string
83{
84 switch(state){
85 case State::kLive: return "live";
86 case State::kDead: return "dead";
87 case State::kPaused: return "paused";
88 default:
89 return "UNKNOWN";
90 }
91}
92
93} // 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.
static int64_t now()
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
#define TLOG(...)
Definition macro.hpp:22