DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Latency.hpp
Go to the documentation of this file.
1
9#ifndef TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_
10#define TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_
11
12#include <atomic>
13#include <chrono>
14#include <iostream> // Include for std::ostream
15
16namespace dunedaq {
17namespace trigger {
18
20{
21 using latency = uint64_t;
22
23public:
24 // Enumeration for selecting time units
25 enum class TimeUnit
26 {
27 Microseconds = 1,
28 Milliseconds = 2
29 };
30
31 // Constructor with optional time unit selection (defaults to Microseconds)
33 : m_latency_in(0)
34 , m_latency_out(0)
35 , m_time_unit(time_unit)
36 {
38 }
39
41
42 // Function to update latency_in
44
45 // Function to update latency_out
47
48 // Function to get the value of latency_in
49 latency get_latency_in() const { return m_latency_in.load(); }
50
51 // Function to get the value of latency_out
52 latency get_latency_out() const { return m_latency_out.load(); }
53
54private:
55 // Set up conversion based on the selected time unit
57 {
59 m_clock_ticks_conversion = 16 * 1e-3; // Conversion for microseconds
60 m_get_current_time = []() {
61 return std::chrono::duration_cast<std::chrono::microseconds>(
62 std::chrono::system_clock::now().time_since_epoch())
63 .count();
64 };
65 } else {
66 m_clock_ticks_conversion = 16 * 1e-6; // Conversion for milliseconds
67 m_get_current_time = []() {
68 return std::chrono::duration_cast<std::chrono::milliseconds>(
69 std::chrono::system_clock::now().time_since_epoch())
70 .count();
71 };
72 }
73 }
74
75 // Function to get the current system time based on the set time unit
76 uint64_t get_current_system_time() const { return m_get_current_time(); }
77
78 // Single update function for both latencies
79 void update_single_latency(uint64_t latency, std::atomic<uint64_t>& latency_atomic)
80 {
81 uint64_t current_time = get_current_system_time();
82 uint64_t latency_time = latency * m_clock_ticks_conversion;
83 uint64_t diff = (current_time >= latency_time) ? (current_time - latency_time) : 0;
84 latency_atomic.store(diff);
85 }
86
87 std::atomic<latency> m_latency_in; // Member variable to store latency_in
88 std::atomic<latency> m_latency_out; // Member variable to store latency_out
89 TimeUnit m_time_unit; // Member variable to store the selected time unit (ms or ns)
90 double m_clock_ticks_conversion; // Conversion factor from ticks to the selected time unit
91
92 // Lambda to get the current time
93 std::function<uint64_t()> m_get_current_time;
94};
95
96} // namespace trigger
97} // namespace dunedaq
98
99#endif // TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_
std::atomic< latency > m_latency_in
Definition Latency.hpp:87
std::function< uint64_t()> m_get_current_time
Definition Latency.hpp:93
void update_latency_out(uint64_t latency)
Definition Latency.hpp:46
Latency(TimeUnit time_unit=TimeUnit::Microseconds)
Definition Latency.hpp:32
uint64_t get_current_system_time() const
Definition Latency.hpp:76
latency get_latency_in() const
Definition Latency.hpp:49
std::atomic< latency > m_latency_out
Definition Latency.hpp:88
latency get_latency_out() const
Definition Latency.hpp:52
void update_single_latency(uint64_t latency, std::atomic< uint64_t > &latency_atomic)
Definition Latency.hpp:79
void update_latency_in(uint64_t latency)
Definition Latency.hpp:43
The DUNE-DAQ namespace.
Definition DataStore.hpp:57