DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::datahandlinglibs::RateLimiter Class Reference

#include <RateLimiter.hpp>

Public Types

using timestamp_t = std::uint64_t
 

Public Member Functions

 RateLimiter (double kilohertz)
 
void init ()
 
void adjust (double kilohertz)
 
void limit ()
 

Static Public Attributes

static constexpr timestamp_t ns = 1
 
static constexpr timestamp_t us = 1000 * ns
 
static constexpr timestamp_t ms = 1000 * us
 
static constexpr timestamp_t s = 1000 * ms
 

Protected Member Functions

timestamp_t gettime ()
 

Private Attributes

std::atomic< double > m_kilohertz
 
timestamp_t m_max_overshoot
 
std::atomic< timestamp_tm_period
 
timestamp_t m_now
 
timestamp_t m_deadline
 

Detailed Description

RateLimiter usage:

auto limiter = RateLimiter(1000); // 1MHz limiter.init(); while (duration) { // do work limiter.limit(); } NOTES: This rate limiter is a simple implementation that is intended to be used for fast tasks, i.e. tasks that take less or much less time than the time intervals (1 / rate) that the RateLimiter is operating with. It doesn't work correctly if the tasks take longer than 1 / rate.

Definition at line 34 of file RateLimiter.hpp.

Member Typedef Documentation

◆ timestamp_t

Definition at line 37 of file RateLimiter.hpp.

Constructor & Destructor Documentation

◆ RateLimiter()

dunedaq::datahandlinglibs::RateLimiter::RateLimiter ( double kilohertz)
inlineexplicit

Definition at line 43 of file RateLimiter.hpp.

44 : m_max_overshoot(10 * ms)
45 {
46 adjust(kilohertz);
47 init();
48 }
static constexpr timestamp_t ms

Member Function Documentation

◆ adjust()

void dunedaq::datahandlinglibs::RateLimiter::adjust ( double kilohertz)
inline

Optionally: adjust rate from another thread

auto adjuster = std::thread([&]() { int newrate = 1000; while (newrate > 0) { limiter.adjust(newrate); newrate–; std::this_thread::sleep_for(std::chrono::seconds(1)); } }

Definition at line 67 of file RateLimiter.hpp.

68 {
69 m_kilohertz.store(kilohertz);
70 m_period.store(static_cast<timestamp_t>((1000.f / m_kilohertz) * static_cast<double>(us)));
71 }
std::atomic< timestamp_t > m_period
static constexpr timestamp_t us

◆ gettime()

timestamp_t dunedaq::datahandlinglibs::RateLimiter::gettime ( )
inlineprotected

Definition at line 98 of file RateLimiter.hpp.

99 {
100 ::timespec ts;
101 ::clock_gettime(CLOCK_MONOTONIC, &ts);
102 return timestamp_t(ts.tv_sec) * s + timestamp_t(ts.tv_nsec) * ns;
103 }
static constexpr timestamp_t s
static constexpr timestamp_t ns
PDS Frame with unphysical timestamp detected with ts

◆ init()

void dunedaq::datahandlinglibs::RateLimiter::init ( )
inline

◆ limit()

void dunedaq::datahandlinglibs::RateLimiter::limit ( )
inline

Definition at line 73 of file RateLimiter.hpp.

74 {
75 m_now = gettime();
77 m_deadline = m_now + m_period.load();
78 } else {
79 if (m_now < m_deadline) {
80 if (m_deadline - m_now > 0) {
81 timespec tim;
82 tim.tv_sec = 0;
83 tim.tv_nsec = m_deadline - m_now;
84 // The second argument will be overwritten but we
85 // do not care about this temporary variable
86 nanosleep(&tim, &tim);
87 m_now = gettime();
88 }
89 while (m_now < m_deadline) {
90 m_now = gettime();
91 }
92 }
93 m_deadline += m_period.load();
94 }
95 }

Member Data Documentation

◆ m_deadline

timestamp_t dunedaq::datahandlinglibs::RateLimiter::m_deadline
private

Definition at line 110 of file RateLimiter.hpp.

◆ m_kilohertz

std::atomic<double> dunedaq::datahandlinglibs::RateLimiter::m_kilohertz
private

Definition at line 106 of file RateLimiter.hpp.

◆ m_max_overshoot

timestamp_t dunedaq::datahandlinglibs::RateLimiter::m_max_overshoot
private

Definition at line 107 of file RateLimiter.hpp.

◆ m_now

timestamp_t dunedaq::datahandlinglibs::RateLimiter::m_now
private

Definition at line 109 of file RateLimiter.hpp.

◆ m_period

std::atomic<timestamp_t> dunedaq::datahandlinglibs::RateLimiter::m_period
private

Definition at line 108 of file RateLimiter.hpp.

◆ ms

timestamp_t dunedaq::datahandlinglibs::RateLimiter::ms = 1000 * us
inlinestaticconstexpr

Definition at line 40 of file RateLimiter.hpp.

◆ ns

timestamp_t dunedaq::datahandlinglibs::RateLimiter::ns = 1
inlinestaticconstexpr

Definition at line 38 of file RateLimiter.hpp.

◆ s

timestamp_t dunedaq::datahandlinglibs::RateLimiter::s = 1000 * ms
inlinestaticconstexpr

Definition at line 41 of file RateLimiter.hpp.

◆ us

timestamp_t dunedaq::datahandlinglibs::RateLimiter::us = 1000 * ns
inlinestaticconstexpr

Definition at line 39 of file RateLimiter.hpp.


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