DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::trigger::LivetimeCounter Class Reference

LivetimeCounter counts the total time in milliseconds spent in each of the available states. More...

#include <LivetimeCounter.hpp>

Public Types

enum class  State { kLive , kDead , kPaused }
 
using state_time_t = uint64_t
 A type to store a time duration in milliseconds.
 

Public Member Functions

 LivetimeCounter (State state)
 Construct a LivetimeCounter in the given state.
 
 ~LivetimeCounter ()
 
void set_state (State state)
 Set the current state to state.
 
std::map< State, state_time_tget_time_map ()
 Get a map of accumulated time in milliseconds in each state.
 
state_time_t get_time (State state)
 Get the accumulated time in milliseconds spent in a particular state.
 
std::string get_report_string ()
 Get a nicely-formatted string of the time spent in each state.
 
std::string get_state_name (State state) const
 

Private Member Functions

state_time_t now () const
 
void update_map ()
 

Private Attributes

std::mutex m_mutex
 
State m_state
 
std::map< State, state_time_tm_state_times
 
state_time_t m_last_state_change_time
 

Detailed Description

LivetimeCounter counts the total time in milliseconds spent in each of the available states.

The current state is set at construction, and can be changed with set_state(). The accumulated time in a particular state can be retrieved with get_time(State), and the times spent in all the states with get_time_map()

Definition at line 18 of file LivetimeCounter.hpp.

Member Typedef Documentation

◆ state_time_t

A type to store a time duration in milliseconds.

Definition at line 30 of file LivetimeCounter.hpp.

Member Enumeration Documentation

◆ State

Enumerator
kLive 
kDead 
kPaused 

Definition at line 21 of file LivetimeCounter.hpp.

21 {
22 kLive, // Live to triggers
23 kDead, // Dead to triggers due to a problem
24 kPaused // Triggers paused (so we are dead to triggers, but intentionally)
25 };

Constructor & Destructor Documentation

◆ LivetimeCounter()

dunedaq::trigger::LivetimeCounter::LivetimeCounter ( LivetimeCounter::State state)

Construct a LivetimeCounter in the given state.

Counting the time in the initial state begins immediately

Definition at line 9 of file LivetimeCounter.cpp.

10 : m_state(state)
12{
13 TLOG_DEBUG(1) << "Starting LivetimeCounter in state " << get_state_name(state);
17}
std::map< State, state_time_t > m_state_times
std::string get_state_name(State state) const
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112

◆ ~LivetimeCounter()

dunedaq::trigger::LivetimeCounter::~LivetimeCounter ( )

Definition at line 19 of file LivetimeCounter.cpp.

20{
21 std::string report=get_report_string();
22 TLOG() << "LivetimeCounter stopping. Counts: " << report;
23}
std::string get_report_string()
Get a nicely-formatted string of the time spent in each state.
#define TLOG(...)
Definition macro.hpp:22

Member Function Documentation

◆ get_report_string()

std::string dunedaq::trigger::LivetimeCounter::get_report_string ( )

Get a nicely-formatted string of the time spent in each state.

Definition at line 69 of file LivetimeCounter.cpp.

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}

◆ get_state_name()

std::string dunedaq::trigger::LivetimeCounter::get_state_name ( LivetimeCounter::State state) const

Definition at line 82 of file LivetimeCounter.cpp.

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}

◆ get_time()

LivetimeCounter::state_time_t dunedaq::trigger::LivetimeCounter::get_time ( LivetimeCounter::State state)

Get the accumulated time in milliseconds spent in a particular state.

Definition at line 54 of file LivetimeCounter.cpp.

55{
56 std::lock_guard<std::mutex> l(m_mutex);
57 update_map();
58 return m_state_times[state];
59}

◆ get_time_map()

std::map< LivetimeCounter::State, LivetimeCounter::state_time_t > dunedaq::trigger::LivetimeCounter::get_time_map ( )

Get a map of accumulated time in milliseconds in each state.

Definition at line 46 of file LivetimeCounter.cpp.

47{
48 std::lock_guard<std::mutex> l(m_mutex);
49 update_map();
50 return m_state_times;
51}

◆ now()

LivetimeCounter::state_time_t dunedaq::trigger::LivetimeCounter::now ( ) const
private

Definition at line 62 of file LivetimeCounter.cpp.

63{
64 using namespace std::chrono;
65 return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
66}

◆ set_state()

void dunedaq::trigger::LivetimeCounter::set_state ( LivetimeCounter::State state)

Set the current state to state.

Updates the accumulated time in the previous state and switches the state

Definition at line 37 of file LivetimeCounter.cpp.

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}

◆ update_map()

void dunedaq::trigger::LivetimeCounter::update_map ( )
private

Definition at line 26 of file LivetimeCounter.cpp.

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}

Member Data Documentation

◆ m_last_state_change_time

state_time_t dunedaq::trigger::LivetimeCounter::m_last_state_change_time
private

Definition at line 75 of file LivetimeCounter.hpp.

◆ m_mutex

std::mutex dunedaq::trigger::LivetimeCounter::m_mutex
private

Definition at line 72 of file LivetimeCounter.hpp.

◆ m_state

State dunedaq::trigger::LivetimeCounter::m_state
private

Definition at line 73 of file LivetimeCounter.hpp.

◆ m_state_times

std::map<State, state_time_t> dunedaq::trigger::LivetimeCounter::m_state_times
private

Definition at line 74 of file LivetimeCounter.hpp.


The documentation for this class was generated from the following files: