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_t > get_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_t > m_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 31 of file LivetimeCounter.hpp.

Member Enumeration Documentation

◆ State

Enumerator
kLive 
kDead 
kPaused 

Definition at line 21 of file LivetimeCounter.hpp.

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

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.

◆ ~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:21

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 68 of file LivetimeCounter.cpp.

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}

◆ get_state_name()

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

Definition at line 80 of file LivetimeCounter.cpp.

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}

◆ 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 53 of file LivetimeCounter.cpp.

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

◆ 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 45 of file LivetimeCounter.cpp.

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

◆ now()

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

Definition at line 61 of file LivetimeCounter.cpp.

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

◆ 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 36 of file LivetimeCounter.cpp.

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}

◆ 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 74 of file LivetimeCounter.hpp.

◆ m_mutex

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

Definition at line 71 of file LivetimeCounter.hpp.

◆ m_state

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

Definition at line 72 of file LivetimeCounter.hpp.

◆ m_state_times

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

Definition at line 73 of file LivetimeCounter.hpp.


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