RTC Toolkit 6.0.0-pre2
Loading...
Searching...
No Matches
frequencyEstimator.hpp
Go to the documentation of this file.
1
12#ifndef RTCTK_COMPONENTFRAMEWORK_FREQUENCYESTIMATOR_HPP
13#define RTCTK_COMPONENTFRAMEWORK_FREQUENCYESTIMATOR_HPP
14
17
18#include <chrono>
19#include <string>
20
22
30template <typename ClockType = std::chrono::steady_clock>
32public:
41 const std::string& desc,
42 const std::string& prefix = "")
43 : m_start_time(ClockType::now()), m_frequency_estimate(0) {
44 std::string path;
45 if (prefix == "") {
46 path = "frequency_estimate";
47 } else {
48 path = prefix + "/frequency_estimate";
49 }
50
51 const InfluxTagMap base_tags = {
52 {"prefix", prefix}
53 };
54
55 try {
56 m_frequency_estimate_reg =
57 metrics.AddCounter(&m_frequency_estimate, CounterMetricInfo(
58 path,
59 desc + " [Hz]",
60 base_tags,
61 "frequency_estimate",
63 )
64 );
65 } catch (...) {
66 LOG4CPLUS_ERROR(GetLogger("rtctk"),
67 "FrequencyEstimator: Failed to register to metrics service. "
68 "Will ignore and continue anyway: "
69 << NestedExceptionPrinter(std::current_exception()));
70 }
71 }
72
81 inline void Tick() noexcept {
82 using namespace std::chrono_literals;
83
84 ++m_num_samples;
85 auto now_time = ClockType::now();
86 std::chrono::duration<double, std::ratio<1>> elapsed_time = now_time - m_start_time;
87 if (elapsed_time > 1s) {
88 m_start_time = now_time;
89 m_frequency_estimate.Store(m_num_samples / elapsed_time.count());
90 m_num_samples = 0;
91 }
92 }
93
102 double GetValue() const {
103 return m_frequency_estimate.Load();
104 }
105
111 void Reset() {
112 m_frequency_estimate.Store(0);
113 m_start_time = ClockType::now();
114 m_num_samples = 0;
115 }
116
117private:
118 uint32_t m_num_samples = 0;
119 typename ClockType::time_point m_start_time;
120
121 perfc::CounterDouble m_frequency_estimate;
122 perfc::ScopedRegistration m_frequency_estimate_reg;
123};
124
125} // namespace rtctk::componentFramework
126
127#endif // RTCTK_COMPONENTFRAMEWORK_FREQUENCYESTIMATOR_HPP
Component metrics interface.
Definition componentMetricsIf.hpp:164
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:49
FrequencyEstimator(ComponentMetricsIf &metrics, const std::string &desc, const std::string &prefix="")
Construct instance.
Definition frequencyEstimator.hpp:40
double GetValue() const
Method to get the measured frequency in Hz.
Definition frequencyEstimator.hpp:102
void Reset()
Method to reset the estimator.
Definition frequencyEstimator.hpp:111
void Tick() noexcept
Method to be called repeatedly from within the hot loop that should be measured.
Definition frequencyEstimator.hpp:81
Helper class for passing tags in Telegraf.
Definition influxTagMap.hpp:27
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:161
Header file for ComponentMetricsIf.
Logging Support Library based on log4cplus.
Definition commandReplier.cpp:22
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:192
static constexpr std::string HERTZ
Definition componentMetricsIf.hpp:259