DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TimestampEstimatorTimeSync.cpp
Go to the documentation of this file.
1
10#include "utilities/Issues.hpp"
11
12#include "logging/Logging.hpp"
13
14#include <memory>
15#include <unistd.h>
16
17#define TRACE_NAME "TimestampEstimatorTimeSync" // NOLINT
18
19namespace dunedaq::utilities {
20
21TimestampEstimatorTimeSync::TimestampEstimatorTimeSync(uint32_t run_number, // NOLINT(build/unsigned)
22 uint64_t clock_frequency_hz) // NOLINT(build/unsigned)
23 : TimestampEstimatorTimeSync(clock_frequency_hz)
24{
25 m_run_number = run_number;
26}
27
28TimestampEstimatorTimeSync::TimestampEstimatorTimeSync(uint64_t clock_frequency_hz) // NOLINT(build/unsigned)
29 : m_current_timestamp_estimate(TimeSyncPoint{ s_invalid_ts, std::chrono::time_point<std::chrono::steady_clock>() })
30 , m_clock_frequency_hz(clock_frequency_hz)
31 , m_most_recent_daq_time(s_invalid_ts)
32 , m_most_recent_system_time(0)
33 , m_run_number(0)
34 , m_received_timesync_count(0)
35{
36 m_current_process_id = static_cast<uint32_t>(getpid()); // NOLINT(build/unsigned)
37}
38
40
46uint64_t // NOLINT(build/unsigned)
48{
49 using namespace std::chrono;
50
52 // 27-May-2025, KAB: added check if a valid timestamp is available and, if not, return early
53 // with the special value that indicates that none is available.
54 if (estimate.daq_time == s_invalid_ts) {
55 return estimate.daq_time;
56 }
57
58 auto delta_time_us = duration_cast<microseconds>(steady_clock::now() - estimate.system_time).count();
59
60 const uint64_t new_timestamp = estimate.daq_time + delta_time_us * m_clock_frequency_hz / 1000000; // NOLINT
61
62 return new_timestamp;
63}
64
65std::chrono::microseconds
66TimestampEstimatorTimeSync::get_wait_estimate(uint64_t ts) const // NOLINT(build/unsigned)
67{
68 auto now = get_timestamp_estimate();
69 if (now > ts)
70 return std::chrono::microseconds(0);
71 auto diff = ts - now;
72 return std::chrono::microseconds(static_cast<std::chrono::microseconds::rep>(
73 static_cast<double>(diff) * 1000000. / static_cast<double>(m_clock_frequency_hz)));
74}
75
76void
77TimestampEstimatorTimeSync::add_timestamp_datapoint(uint64_t daq_time, uint64_t system_time) // NOLINT(build/unsigned)
78{
79 using namespace std::chrono;
80
81 std::scoped_lock<std::mutex> lk(m_datapoint_mutex);
82
83 // First, update the latest timestamp
85 auto diff = static_cast<int64_t>(estimate.daq_time - daq_time);
86 TLOG_DEBUG(TLVL_TIME_SYNC_PROPERTIES) << "Got a TimeSync timestamp = " << daq_time
87 << ", system time = " << system_time << " when current timestamp estimate was "
88 << estimate.daq_time << ". diff=" << diff;
89
91 m_most_recent_daq_time = daq_time;
92 m_most_recent_system_time = system_time;
93 }
94
96 // Update the current timestamp estimate, based on the most recently-read TimeSync
97 using namespace std::chrono;
98
99 auto time_now =
100 static_cast<uint64_t>(duration_cast<microseconds>(system_clock::now().time_since_epoch()).count()); // NOLINT
101 auto steady_time_now = steady_clock::now();
102
103 // (PAR 2021-07-22) We only want to _increase_ our timestamp
104 // estimate, not _decrease_ it, so we only attempt the update if
105 // our system time is later than the latest time sync's system
106 // time. We can get TimeSync messages from the "future" if
107 // they're coming from another host whose clock is not exactly
108 // synchronized with ours: that's fine, but if the discrepancy
109 // is large, then badness could happen, so emit a warning
110
111 if (time_now < m_most_recent_system_time - 10000) {
112 ers::warning(EarlyTimeSync(ERS_HERE, m_most_recent_system_time - time_now));
113 }
114
115 if (time_now > m_most_recent_system_time) {
116
117 auto delta_time = time_now - m_most_recent_system_time;
119 << "Time diff between current system and latest TimeSync system time [us]: " << delta_time;
120
121 // Warn user if current system time is more than 1s ahead of latest TimeSync system time. This could be a sign of
122 // an issue, e.g. machine times out of sync
123 if (delta_time > 1'000'000)
124 ers::warning(LateTimeSync(ERS_HERE, delta_time));
125
126 const uint64_t new_timestamp = m_most_recent_daq_time + delta_time * m_clock_frequency_hz / 1000000; // NOLINT
127
128 // Don't ever decrease the timestamp; just wait until enough
129 // time passes that we want to increase it
130 if (estimate.daq_time == s_invalid_ts || new_timestamp >= estimate.daq_time) {
132 << "Storing new timestamp estimate of " << new_timestamp << " ticks (..." << std::fixed
133 << std::setprecision(8)
134 << (static_cast<double>(new_timestamp % (m_clock_frequency_hz * 1000)) /
135 static_cast<double>(m_clock_frequency_hz))
136 << " sec), mrt.daq_time is " << m_most_recent_daq_time << " ticks (..."
137 << (static_cast<double>(m_most_recent_daq_time % (m_clock_frequency_hz * 1000)) /
138 static_cast<double>(m_clock_frequency_hz))
139 << " sec), delta_time is " << delta_time << " usec, clock_freq is " << m_clock_frequency_hz << " Hz";
140 m_current_timestamp_estimate.store(TimeSyncPoint{ new_timestamp, steady_time_now });
141 } else {
142 TLOG_DEBUG(TLVL_TIME_SYNC_NOTES) << "Not updating timestamp estimate backwards from "
143 << m_current_timestamp_estimate.load().daq_time << " to " << new_timestamp;
144 }
145 }
146 }
147}
148
149} // namespace dunedaq::utilities
#define ERS_HERE
TimestampEstimatorTimeSync is an implementation of TimestampEstimatorBase that uses TimeSync messages...
void add_timestamp_datapoint(uint64_t daq_time, uint64_t system_time)
uint64_t get_timestamp_estimate() const override
Returns the current timestamp estimate or a special value if no valid timestamp is available.
std::chrono::microseconds get_wait_estimate(uint64_t ts) const override
TimestampEstimatorTimeSync(uint32_t run_number, uint64_t clock_frequency_hz)
static int64_t now()
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
PDS Frame with unphysical timestamp detected with ts
void warning(const Issue &issue)
Definition ers.hpp:115
std::chrono::time_point< std::chrono::steady_clock > system_time