Skip to content

Commit

Permalink
small rework of latency class
Browse files Browse the repository at this point in the history
  • Loading branch information
MRiganSUSX committed Oct 8, 2024
1 parent bbddc50 commit 6b33efd
Showing 1 changed file with 77 additions and 78 deletions.
155 changes: 77 additions & 78 deletions include/trigger/Latency.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,88 +9,87 @@
#ifndef TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_
#define TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_

#include "utilities/TimestampEstimator.hpp"
#include "utilities/TimestampEstimatorSystem.hpp"

#include <atomic>
#include <chrono>
#include <iostream> // Include for std::ostream

namespace dunedaq {
namespace trigger {

class Latency {
public:

// Enumeration for selecting time units
enum class TimeUnit { Milliseconds, Microseconds };

// Constructor with optional time unit selection (defaults to Milliseconds)
Latency(TimeUnit time_unit = TimeUnit::Milliseconds)
: m_latency_in(0), m_latency_out(0), m_time_unit(time_unit)
{
// Set the clock tick conversion factor based on time unit
if (m_time_unit == TimeUnit::Milliseconds) {
m_clock_ticks_conversion = 16 * 1e-6; // For milliseconds: 1 tick = 16 * 10^-6 ms
} else {
m_clock_ticks_conversion = 16 * 1e-3;
}
// to convert 62.5MHz clock ticks to ms: 1/62500000 = 0.000000016 <- seconds per tick; 0.000016 <- ms per tick;
// 16*1e-6 <- sci notation
}

// Function to get the current system time in ms or ns based on time unit
uint64_t get_current_system_time() const
{
if (m_time_unit == TimeUnit::Milliseconds) {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
} else {
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
}
}

// Function to update latency_in
void update_latency_in(uint64_t latency)
{
m_latency_in.store(latency * m_clock_ticks_conversion);
}

// Function to update latency_out
void update_latency_out(uint64_t latency)
{
m_latency_out.store(latency * m_clock_ticks_conversion);
}

// Function to get the value of latency_in
uint64_t get_latency_in() const
{
if (m_latency_in.load() != 0) {
// in edge cases the TP time was more recent then current sys time...
// this is a catch for that
uint64_t diff = abs(int64_t(get_current_system_time()) - int64_t(m_latency_in.load()));
return diff;
} else {
return 0;
}
}

// Function to get the value of latency_out
uint64_t get_latency_out() const
{
if (m_latency_out.load() != 0) {
uint64_t diff = abs(int64_t(get_current_system_time()) - int64_t(m_latency_out.load()));
return diff;
} else {
return 0;
}
}

private:
std::atomic<uint64_t> m_latency_in; // Member variable to store latency_in
std::atomic<uint64_t> m_latency_out; // Member variable to store latency_out
double m_clock_ticks_conversion; // Dynamically adjusted conversion factor for clock ticks
TimeUnit m_time_unit; // Member variable to store the selected time unit (ms or ns)
};

} // namespace trigger
namespace trigger {

class Latency {
using latency = double;

public:
// Enumeration for selecting time units
enum class TimeUnit { Milliseconds = 1, Microseconds = 2 };

// Constructor with optional time unit selection (defaults to Milliseconds)
Latency(TimeUnit time_unit = TimeUnit::Milliseconds)
: m_latency_in(0), m_latency_out(0), m_time_unit(time_unit), clock_freq(62500000) {

setup_conversion();

// Start timestamp estimator
m_timestamp_estimator.reset(new utilities::TimestampEstimatorSystem(clock_freq));
}

~Latency() {
m_timestamp_estimator.reset(nullptr); // Calls TimestampEstimator dtor
}

// Function to get the current system time in ms or ns based on time unit
uint64_t get_current_system_time() const {
return m_timestamp_estimator->get_timestamp_estimate();
}

// Function to update latency_in
void update_latency_in(uint64_t latency) {
uint64_t current_time = get_current_system_time();
uint64_t diff = (current_time >= latency) ? (current_time - latency) : 0;
m_latency_in.store( diff * m_clock_ticks_conversion );
TLOG() << static_cast<int>(m_time_unit) << " " << current_time << " " << latency << " " << diff << " " << m_latency_in.load();
}

// Function to update latency_out
void update_latency_out(uint64_t latency) {
uint64_t current_time = get_current_system_time();
uint64_t diff = (current_time >= latency) ? (current_time - latency) : 0;
m_latency_out.store( diff * m_clock_ticks_conversion );
}

// Function to get the value of latency_in
latency get_latency_in() const {
return m_latency_in.load();
}

// Function to get the value of latency_out
latency get_latency_out() const {
return m_latency_out.load();
}

private:
void setup_conversion() {
if (m_time_unit == TimeUnit::Milliseconds) {
m_clock_ticks_conversion = 16 * 1e-6; // For milliseconds
} else {
m_clock_ticks_conversion = 16 * 1e-3; // For microseconds
}
}

std::atomic<latency> m_latency_in; // Member variable to store latency_in
std::atomic<latency> m_latency_out; // Member variable to store latency_out
TimeUnit m_time_unit; // Member variable to store the selected time unit (ms or ns)
latency m_clock_ticks_conversion; // Conversion factor from ticks to the selected time unit
std::unique_ptr<utilities::TimestampEstimatorBase> m_timestamp_estimator;
latency clock_freq;
// Function pointer or lambda for conversion based on time unit
std::function<uint64_t(uint64_t)> m_convert_latency;
};

} // namespace trigger
} // namespace dunedaq

#endif // TRIGGER_INCLUDE_TRIGGER_LATENCY_HPP_

0 comments on commit 6b33efd

Please sign in to comment.