RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
frequencyEstimator.hpp
Go to the documentation of this file.
1
11#ifndef RTCTK_COMPONENTFRAMEWORK_FREQUENCYESTIMATOR_HPP
12#define RTCTK_COMPONENTFRAMEWORK_FREQUENCYESTIMATOR_HPP
13
16
17#include <chrono>
18#include <string>
19
21
29template <typename ClockType = std::chrono::steady_clock>
31public:
40 const std::string& desc,
41 const std::string& prefix = "")
42 : m_start_time(ClockType::now()), m_frequency_estimate(0) {
43 std::string path;
44 if (prefix == "") {
45 path = "frequency_estimate";
46 } else {
47 path = prefix + "/frequency_estimate";
48 }
49
50 const InfluxTagMap base_tags = {{"prefix", prefix}};
51
52 try {
53 m_frequency_estimate_reg = metrics.AddCounter(
54 &m_frequency_estimate,
56 path, desc + " [Hz]", base_tags, "frequency_estimate", Unit::HERTZ));
57 } catch (...) {
58 LOG4CPLUS_ERROR(GetLogger("rtctk"),
59 "FrequencyEstimator: Failed to register to metrics service. "
60 "Will ignore and continue anyway: "
61 << NestedExceptionPrinter(std::current_exception()));
62 }
63 }
64
73 inline void Tick() noexcept {
74 using namespace std::chrono_literals;
75
76 ++m_num_samples;
77 auto now_time = ClockType::now();
78 std::chrono::duration<double, std::ratio<1>> elapsed_time = now_time - m_start_time;
79 if (elapsed_time > 1s) {
80 m_start_time = now_time;
81 m_frequency_estimate.Store(m_num_samples / elapsed_time.count());
82 m_num_samples = 0;
83 }
84 }
85
94 double GetValue() const {
95 return m_frequency_estimate.Load();
96 }
97
103 void Reset() {
104 m_frequency_estimate.Store(0);
105 m_start_time = ClockType::now();
106 m_num_samples = 0;
107 }
108
109private:
110 uint32_t m_num_samples = 0;
111 typename ClockType::time_point m_start_time;
112
113 perfc::CounterDouble m_frequency_estimate;
114 perfc::ScopedRegistration m_frequency_estimate_reg;
115};
116
117} // namespace rtctk::componentFramework
118
119#endif // RTCTK_COMPONENTFRAMEWORK_FREQUENCYESTIMATOR_HPP
Component metrics interface.
Definition componentMetricsIf.hpp:163
virtual perfc::ScopedRegistration AddCounter(CounterVariant counter, CounterMetricInfo info)=0
Add a counter to be included in component metrics, identified by its address, together with info to t...
Defines auxiliary information associated with each counter registered with ComponentMetricsIf.
Definition componentMetricsIf.hpp:48
FrequencyEstimator(ComponentMetricsIf &metrics, const std::string &desc, const std::string &prefix="")
Construct instance.
Definition frequencyEstimator.hpp:39
double GetValue() const
Method to get the measured frequency in Hz.
Definition frequencyEstimator.hpp:94
void Reset()
Method to reset the estimator.
Definition frequencyEstimator.hpp:103
void Tick() noexcept
Method to be called repeatedly from within the hot loop that should be measured.
Definition frequencyEstimator.hpp:73
Helper class for passing tags in Telegraf.
Definition influxTagMap.hpp:26
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:168
Header file for ComponentMetricsIf.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
Logging Support Library based on log4cplus.
Definition commandReplier.cpp:21
static constexpr std::string HERTZ
Definition componentMetricsIf.hpp:258