DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::utilities::TimestampEstimatorBase Class Referenceabstract

TimestampEstimatorBase is the base class for timestamp-based logic in test systems where the current timestamp must be estimated somehow (eg, because there is no hardware timing system). More...

#include <TimestampEstimatorBase.hpp>

Inheritance diagram for dunedaq::utilities::TimestampEstimatorBase:
[legend]

Public Types

enum  WaitStatus { kFinished , kInterrupted }
 

Public Member Functions

virtual ~TimestampEstimatorBase ()=default
 
virtual uint64_t get_timestamp_estimate () const =0
 
virtual std::chrono::microseconds get_wait_estimate (uint64_t ts) const =0
 
WaitStatus wait_for_valid_timestamp (std::atomic< bool > &continue_flag)
 
WaitStatus wait_for_valid_timestamp (std::atomic< bool > &continue_flag, uint64_t &last_seen_ts)
 Waits for a valid timestamp to become available. Returns a status value that indicates whether a valid timestamp is available or not.
 
WaitStatus wait_for_requested_timestamp (uint64_t ts, std::atomic< bool > &continue_flag)
 
WaitStatus wait_for_requested_timestamp (uint64_t ts, std::atomic< bool > &continue_flag, uint64_t &last_seen_ts)
 

Static Public Attributes

static constexpr uint64_t s_invalid_ts = std::numeric_limits<uint64_t>::max()
 

Detailed Description

TimestampEstimatorBase is the base class for timestamp-based logic in test systems where the current timestamp must be estimated somehow (eg, because there is no hardware timing system).

Definition at line 25 of file TimestampEstimatorBase.hpp.

Member Enumeration Documentation

◆ WaitStatus

Constructor & Destructor Documentation

◆ ~TimestampEstimatorBase()

virtual dunedaq::utilities::TimestampEstimatorBase::~TimestampEstimatorBase ( )
virtualdefault

Member Function Documentation

◆ get_timestamp_estimate()

virtual uint64_t dunedaq::utilities::TimestampEstimatorBase::get_timestamp_estimate ( ) const
pure virtual

◆ get_wait_estimate()

virtual std::chrono::microseconds dunedaq::utilities::TimestampEstimatorBase::get_wait_estimate ( uint64_t ts) const
pure virtual

◆ wait_for_requested_timestamp() [1/2]

WaitStatus dunedaq::utilities::TimestampEstimatorBase::wait_for_requested_timestamp ( uint64_t ts,
std::atomic< bool > & continue_flag )
inline

Wait for the current timestamp estimate to reach ts, or for continue_flag to become false.

Returns kFinished if the timestamp became valid, or kInterrupted if continue_flag became false first

Definition at line 61 of file TimestampEstimatorBase.hpp.

62 {
63 uint64_t ts_discard = s_invalid_ts; // NOLINT(build/unsigned)
64 return wait_for_requested_timestamp(ts, continue_flag, ts_discard);
65 }
WaitStatus wait_for_requested_timestamp(uint64_t ts, std::atomic< bool > &continue_flag)
PDS Frame with unphysical timestamp detected with ts

◆ wait_for_requested_timestamp() [2/2]

TimestampEstimatorBase::WaitStatus dunedaq::utilities::TimestampEstimatorBase::wait_for_requested_timestamp ( uint64_t ts,
std::atomic< bool > & continue_flag,
uint64_t & last_seen_ts )

Definition at line 45 of file TimestampEstimatorBase.cpp.

48{
49 last_seen_ts = s_invalid_ts;
50 auto get_sleep_time = [this, ts]() {
51 auto est = get_wait_estimate(ts);
52 auto pest = static_cast<std::chrono::microseconds::rep>(static_cast<double>(est.count()) * 0.8);
53 if (pest < 1 && est != std::chrono::microseconds(0))
54 return std::chrono::microseconds(1);
55 return std::chrono::microseconds(pest);
56 };
57 auto sleep_time = get_sleep_time();
58 // Always call get_timestamp_estimate at least once
59 while (((last_seen_ts = get_timestamp_estimate()) < ts || last_seen_ts == s_invalid_ts) && continue_flag.load()) {
60 std::this_thread::sleep_for(sleep_time);
61 sleep_time = get_sleep_time();
62 }
63 return (last_seen_ts >= ts && last_seen_ts != s_invalid_ts) ? TimestampEstimatorBase::kFinished
65}
virtual std::chrono::microseconds get_wait_estimate(uint64_t ts) const =0
virtual uint64_t get_timestamp_estimate() const =0

◆ wait_for_valid_timestamp() [1/2]

WaitStatus dunedaq::utilities::TimestampEstimatorBase::wait_for_valid_timestamp ( std::atomic< bool > & continue_flag)
inline

Wait for the current timestamp estimate to become valid, or for continue_flag to become false. The timestamp becomes valid once at least one TimeSync message has been received.

Returns kFinished if the timestamp became valid, or kInterrupted if continue_flag became false first

Definition at line 47 of file TimestampEstimatorBase.hpp.

48 {
49 uint64_t ts_discard = s_invalid_ts; // NOLINT(build/unsigned)
50 return wait_for_valid_timestamp(continue_flag, ts_discard);
51 }
WaitStatus wait_for_valid_timestamp(std::atomic< bool > &continue_flag)

◆ wait_for_valid_timestamp() [2/2]

TimestampEstimatorBase::WaitStatus dunedaq::utilities::TimestampEstimatorBase::wait_for_valid_timestamp ( std::atomic< bool > & continue_flag,
uint64_t & last_seen_ts )

Waits for a valid timestamp to become available. Returns a status value that indicates whether a valid timestamp is available or not.

Parameters
continue_flagwhether to continue waiting until a valid timestamp is available or return immediately
last_seen_ts(Output parameter) the last timestamp seen by the method

The value of the continue_flag can be changed from true to false externally to this method, and that will cause the method to exit soon thereafter (on the order of 10 msec) and return the current status.

Returns
kFinished if a valid timestamp is available or kInterrupted if one is not

Definition at line 27 of file TimestampEstimatorBase.cpp.

28{
29 last_seen_ts = s_invalid_ts;
30 auto sleep_time = std::chrono::microseconds(1);
31 // Always call get_timestamp_estimate at least once
32 while ((last_seen_ts = get_timestamp_estimate()) == s_invalid_ts && continue_flag.load()) {
33 std::this_thread::sleep_for(sleep_time);
34 if (sleep_time < std::chrono::milliseconds(10)) {
35 sleep_time *= 2;
36 }
37 }
38
39 // 27-May-2025, KAB: modified this return statement so that the return code is based on whether a
40 // valid timestamp is available (instead of whether the caller asked the method to wait or not)
42}

Member Data Documentation

◆ s_invalid_ts

uint64_t dunedaq::utilities::TimestampEstimatorBase::s_invalid_ts = std::numeric_limits<uint64_t>::max()
staticconstexpr

Definition at line 28 of file TimestampEstimatorBase.hpp.


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