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
14
15#include <atomic>
16#include <chrono>
17#include <iostream> // Include for std::ostream
18
19namespace dunedaq {
20 namespace trigger {
21
22 class Latency {
23 using latency = uint64_t;
24
25 public:
26 // Enumeration for selecting time units
27 enum class TimeUnit { Microseconds = 1, Milliseconds = 2 };
28
29 // Constructor with optional time unit selection (defaults to Microseconds)
34
36
37 // Function to update latency_in
41
42 // Function to update latency_out
46
47 // Function to get the value of latency_in
49 return m_latency_in.load();
50 }
51
52 // Function to get the value of latency_out
54 return m_latency_out.load();
55 }
56
57 private:
58 // Set up conversion based on the selected time unit
61 m_clock_ticks_conversion = 16 * 1e-3; // Conversion for microseconds
62 m_get_current_time = []() {
63 return std::chrono::duration_cast<std::chrono::microseconds>(
64 std::chrono::system_clock::now().time_since_epoch()).count();
65 };
66 } else {
67 m_clock_ticks_conversion = 16 * 1e-6; // Conversion for milliseconds
68 m_get_current_time = []() {
69 return std::chrono::duration_cast<std::chrono::milliseconds>(
70 std::chrono::system_clock::now().time_since_epoch()).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 {
77 return m_get_current_time();
78 }
79
80 // Single update function for both latencies
81 void update_single_latency(uint64_t latency, std::atomic<uint64_t>& latency_atomic) {
82 uint64_t current_time = get_current_system_time();
83 uint64_t latency_time = latency * m_clock_ticks_conversion;
84 uint64_t diff = (current_time >= latency_time) ? (current_time - latency_time) : 0;
85 latency_atomic.store(diff);
86 }
87
88 std::atomic<latency> m_latency_in; // Member variable to store latency_in
89 std::atomic<latency> m_latency_out; // Member variable to store latency_out
90 TimeUnit m_time_unit; // Member variable to store the selected time unit (ms or ns)
91 double m_clock_ticks_conversion; // Conversion factor from ticks to the selected time unit
92
93 // Lambda to get the current time
94 std::function<uint64_t()> m_get_current_time;
95 };
96
97 } // namespace trigger
98} // namespace dunedaq
99
100#endif // TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_
std::atomic< latency > m_latency_in
Definition Latency.hpp:88
std::function< uint64_t()> m_get_current_time
Definition Latency.hpp:94
void update_latency_out(uint64_t latency)
Definition Latency.hpp:43
Latency(TimeUnit time_unit=TimeUnit::Microseconds)
Definition Latency.hpp:30
uint64_t get_current_system_time() const
Definition Latency.hpp:76
latency get_latency_in() const
Definition Latency.hpp:48
std::atomic< latency > m_latency_out
Definition Latency.hpp:89
latency get_latency_out() const
Definition Latency.hpp:53
void update_single_latency(uint64_t latency, std::atomic< uint64_t > &latency_atomic)
Definition Latency.hpp:81
void update_latency_in(uint64_t latency)
Definition Latency.hpp:38
Including Qt Headers.