mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.6 KiB
C
102 lines
2.6 KiB
C
4 years ago
|
/**
|
||
6 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
6 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
6 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
6 years ago
|
*
|
||
|
*/
|
||
8 years ago
|
|
||
|
/******************************************************************************
|
||
|
* File: metrics_hub.h
|
||
|
* Description: Centralizes metrics interfaces to allow different metrics clients
|
||
5 years ago
|
* to get metrics
|
||
8 years ago
|
*****************************************************************************/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_METRICS_HUB_H
|
||
|
#define SRSRAN_METRICS_HUB_H
|
||
8 years ago
|
|
||
4 years ago
|
#include "srsran/common/threads.h"
|
||
6 years ago
|
#include <chrono>
|
||
4 years ago
|
#include <mutex>
|
||
|
#include <thread>
|
||
6 years ago
|
#include <vector>
|
||
8 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
8 years ago
|
|
||
5 years ago
|
template <typename metrics_t>
|
||
|
class metrics_interface
|
||
8 years ago
|
{
|
||
|
public:
|
||
6 years ago
|
virtual bool get_metrics(metrics_t* m) = 0;
|
||
5 years ago
|
};
|
||
8 years ago
|
|
||
5 years ago
|
template <typename metrics_t>
|
||
8 years ago
|
class metrics_listener
|
||
|
{
|
||
5 years ago
|
public:
|
||
5 years ago
|
virtual void set_metrics(const metrics_t& m, const uint32_t period_usec) = 0;
|
||
5 years ago
|
virtual void stop() = 0;
|
||
7 years ago
|
};
|
||
8 years ago
|
|
||
5 years ago
|
template <typename metrics_t>
|
||
8 years ago
|
class metrics_hub : public periodic_thread
|
||
|
{
|
||
|
public:
|
||
4 years ago
|
metrics_hub() : sleep_start(std::chrono::steady_clock::now()), periodic_thread("METRICS_HUB") {}
|
||
6 years ago
|
bool init(metrics_interface<metrics_t>* m_, float report_period_secs_ = 1.0)
|
||
|
{
|
||
7 years ago
|
m = m_;
|
||
7 years ago
|
// Start with user-default priority
|
||
5 years ago
|
start_periodic(report_period_secs_ * 1e6, -2);
|
||
8 years ago
|
return true;
|
||
8 years ago
|
}
|
||
5 years ago
|
void stop()
|
||
|
{
|
||
6 years ago
|
stop_thread();
|
||
7 years ago
|
// stop all listeners
|
||
5 years ago
|
for (uint32_t i = 0; i < listeners.size(); i++) {
|
||
7 years ago
|
listeners[i]->stop();
|
||
|
}
|
||
8 years ago
|
thread_cancel();
|
||
7 years ago
|
wait_thread_finish();
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
void add_listener(metrics_listener<metrics_t>* listener)
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
listeners.push_back(listener);
|
||
|
}
|
||
5 years ago
|
|
||
8 years ago
|
private:
|
||
6 years ago
|
void run_period()
|
||
|
{
|
||
4 years ago
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
|
||
7 years ago
|
// get current time and check how long we slept
|
||
6 years ago
|
auto period_usec =
|
||
|
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - sleep_start);
|
||
|
|
||
7 years ago
|
if (m) {
|
||
7 years ago
|
metrics_t metric;
|
||
6 years ago
|
m->get_metrics(&metric);
|
||
5 years ago
|
for (uint32_t i = 0; i < listeners.size(); i++) {
|
||
6 years ago
|
listeners[i]->set_metrics(metric, period_usec.count());
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|
||
7 years ago
|
// store start of sleep period
|
||
6 years ago
|
sleep_start = std::chrono::steady_clock::now();
|
||
8 years ago
|
}
|
||
4 years ago
|
metrics_interface<metrics_t>* m = nullptr;
|
||
7 years ago
|
std::vector<metrics_listener<metrics_t>*> listeners;
|
||
6 years ago
|
std::chrono::steady_clock::time_point sleep_start;
|
||
4 years ago
|
std::mutex mutex;
|
||
8 years ago
|
};
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
8 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_METRICS_HUB_H
|