RTC Toolkit  2.0.0
componentMetrics.hpp
Go to the documentation of this file.
1 
11 #ifndef RTCTK_COMPONENTFRAMEWORK_COMPONENTMETRICS_HPP
12 #define RTCTK_COMPONENTFRAMEWORK_COMPONENTMETRICS_HPP
13 
14 #include <condition_variable>
15 #include <optional>
16 #include <thread>
17 
18 #include <log4cplus/logger.h>
19 
22 
23 namespace rtctk::componentFramework {
24 
25 class RepositoryIf;
26 class OldbIf;
27 
40 public:
50  ComponentMetricsConfigurator(std::weak_ptr<ComponentMetricsIf> service,
51  std::shared_ptr<RepositoryIf> repo,
52  DataPointPath root,
53  log4cplus::Logger logger,
54  std::chrono::seconds interval = std::chrono::seconds(10));
56 
57 private:
58  void ConfigurationLoop();
59 
60  std::weak_ptr<ComponentMetricsIf> m_service;
61  std::shared_ptr<RepositoryIf> m_repo;
62  const DataPointPath m_root;
63  const std::chrono::seconds m_interval;
64 
65  bool m_stop_flag;
66  std::condition_variable m_cond;
67  std::mutex m_mutex;
68  std::thread m_thread;
69  log4cplus::Logger m_logger;
70 };
71 
89 public:
92 
101  explicit ComponentMetrics(std::shared_ptr<OldbIf> oldb,
102  DataPointPath root,
103  log4cplus::Logger logger,
104  ComponentMetricsParams const& initial_params = {});
108  ~ComponentMetrics() noexcept;
109 
110  void StartMonitoring() override;
111  void StopMonitoring() noexcept override;
112  bool IsMonitoring() noexcept override;
113  void UpdateParams(ComponentMetricsParams const& params) override;
114 
115  [[nodiscard]] virtual perfc::ScopedRegistration
116  AddCounter(CounterVariant counter, CounterMetricInfo info) override;
117  bool RemoveCounter(CounterVariant counter) override;
118 
119  void AddThread(pid_t pid, ThreadMetricInfo info) override;
120  void RemoveThread(pid_t thread) override;
121 
122  struct Thread {
123  pid_t pid;
125  };
126 
127 private:
132  struct Parameters {
133  std::chrono::milliseconds counter_poll_interval = std::chrono::milliseconds(5000);
134  std::chrono::milliseconds thread_poll_interval = std::chrono::milliseconds(5000);
135  int thread_nice = 5;
136  };
137 
138  struct MonitoringThread {
139  MonitoringThread(std::thread t) : stop_flag(false), id(0), thread(std::move(t)) {
140  }
144  bool stop_flag;
148  pid_t id;
149  std::thread thread;
150  };
151 
158  void MonitoringLoop();
159 
163  std::shared_ptr<OldbIf> m_oldb;
164  DataPointPath m_root;
165 
173  std::recursive_mutex m_api_mtx;
174 
180  std::mutex m_mon_mtx;
181 
186  std::condition_variable m_cond;
187 
191  Parameters m_params;
192 
193  perfc::Register<CounterTypes, CounterMetricInfo> m_counters;
194  std::vector<Thread> m_threads;
195  std::optional<MonitoringThread> m_monitoring_thread;
196  log4cplus::Logger m_logger;
197 };
198 
199 } // namespace rtctk::componentFramework
200 
201 #endif // RTCTK_COMPONENTFRAMEWORK_COMPONENTMETRICS_HPP
rtctk::componentFramework::ComponentMetrics::StartMonitoring
void StartMonitoring() override
Start monitoring and publishing metrics.
Definition: componentMetrics.cpp:355
rtctk::componentFramework::ComponentMetrics::UpdateParams
void UpdateParams(ComponentMetricsParams const &params) override
Updates parameters from params.
Definition: componentMetrics.cpp:406
rtctk::componentFramework::ThreadMetricInfo
Defines auxiliary information associated with each thread registered with ComponentMetricsIf.
Definition: componentMetricsIf.hpp:80
componentMetricsIf.hpp
Header file for ComponentMetricsIf.
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::ComponentMetricsParams
Dynamic parameters for ComponentMetrics.
Definition: componentMetricsIf.hpp:112
rtctk::componentFramework::ComponentMetrics::AddCounter
virtual perfc::ScopedRegistration AddCounter(CounterVariant counter, CounterMetricInfo info) override
Add a counter to be included in component metrics, identified by its address, together with info to t...
Definition: componentMetrics.cpp:421
rtctk::componentFramework::ComponentMetrics::StopMonitoring
void StopMonitoring() noexcept override
Stop monitoring.
Definition: componentMetrics.cpp:384
rtctk::componentFramework::ComponentMetrics::Thread::info
ThreadMetricInfo info
Definition: componentMetrics.hpp:124
rtctk::componentFramework::ComponentMetricsIf::CounterVariant
typename CounterTypes::CounterVariant CounterVariant
std::variant of pointers to each supported type in CounterTypes.
Definition: componentMetricsIf.hpp:236
rtctk::componentFramework::ComponentMetrics::AddThread
void AddThread(pid_t pid, ThreadMetricInfo info) override
Add a thread to be included in component metrics.
Definition: componentMetrics.cpp:466
rtctk::componentFramework::ComponentMetrics::IsMonitoring
bool IsMonitoring() noexcept override
Query whether monitoring is active.
Definition: componentMetrics.cpp:400
rtctk::componentFramework::CounterMetricInfo
Defines auxiliary information associated with each counter registered with ComponentMetricsIf.
Definition: componentMetricsIf.hpp:42
rtctk::componentFramework::ComponentMetricsIf::CounterTypes
perfc::CounterTypes< perfc::CounterDouble, perfc::CounterU64, perfc::CounterI64 > CounterTypes
Defines standard performance counter types:
Definition: componentMetricsIf.hpp:230
rtctk::componentFramework::ComponentMetricsIf
Component metrics interface.
Definition: componentMetricsIf.hpp:177
rtctk::componentFramework::ComponentMetrics::Thread::pid
pid_t pid
Definition: componentMetrics.hpp:123
rtctk::componentFramework::ComponentMetricsConfigurator
Configures ComponentMetrics from a runtime repository.
Definition: componentMetrics.hpp:39
rtctk::componentFramework::ComponentMetricsConfigurator::ComponentMetricsConfigurator
ComponentMetricsConfigurator(std::weak_ptr< ComponentMetricsIf > service, std::shared_ptr< RepositoryIf > repo, DataPointPath root, log4cplus::Logger logger, std::chrono::seconds interval=std::chrono::seconds(10))
Constructs configuration with provided arguments.
Definition: componentMetrics.cpp:237
rtctk::componentFramework::ComponentMetrics::RemoveCounter
bool RemoveCounter(CounterVariant counter) override
Remove counter from register.
Definition: componentMetrics.cpp:460
std
Definition: mudpiProcessingError.hpp:119
rtctk::componentFramework::ComponentMetrics::RemoveThread
void RemoveThread(pid_t thread) override
Remove previously registered thread.
Definition: componentMetrics.cpp:495
rtctk::componentFramework::ComponentMetrics::ComponentMetrics
ComponentMetrics(std::shared_ptr< OldbIf > oldb, DataPointPath root, log4cplus::Logger logger, ComponentMetricsParams const &initial_params={})
Definition: componentMetrics.cpp:343
dataPointPath.hpp
Header file for DataPointPath.
rtctk::componentFramework::ComponentMetricsConfigurator::~ComponentMetricsConfigurator
~ComponentMetricsConfigurator() noexcept
Definition: componentMetrics.cpp:332
rtctk::componentFramework::DataPointPath
This class provides a wrapper for a data point path.
Definition: dataPointPath.hpp:65
rtctk::componentFramework::ComponentMetrics::~ComponentMetrics
~ComponentMetrics() noexcept
Destructor will automatically stop monitoring.
Definition: componentMetrics.cpp:351
rtctk::componentFramework::ComponentMetrics::Thread
Definition: componentMetrics.hpp:122
rtctk::componentFramework::ComponentMetrics
Implementation of ComponentMetricsIf.
Definition: componentMetrics.hpp:88