[metrics] Adjusted function names and documentation

master
AlaiaL 4 years ago committed by Andre Puschmann
parent b1483802a2
commit 41cf15f5ad

@ -28,14 +28,13 @@
namespace srslte {
/// Process the data from the proc_stats_info.
/// Process information from the system to create sys_metrics_t. The information is processed from the /proc/ system.
class sys_metrics_processor
{
/// Contains the information of a process.
/// Helper class used to parse the information from the /proc/[pid]/stats.
struct proc_stats_info {
proc_stats_info();
/// Parsed every field in /proc/[pid]/stats file.
int pid, ppid, pgrp, session, tty_nr, tpgid, exit_signal, processor, exit_code;
unsigned flags, rt_priority, policy;
unsigned long minflt, cminflt, majflt, cmajflt, utime, stime, vsize, rsslim, startcode, endcode, startstack,
@ -48,17 +47,18 @@ class sys_metrics_processor
};
public:
/// Performs a new measure and returns the values.
/// Measures and returns the system metrics.
sys_metrics_t get_metrics();
private:
/// Returns the cpu usage in %. current_query is the most recent proc_stats_info, and delta_time_in_seconds is the
/// elapsed time between the last measure and current in seconds.
/// NOTE: Returns -1.0f on error.
float cpu_usage(const proc_stats_info& current_query, float delta_time_in_seconds) const;
/// Calculates and returns the cpu usage in %. current_query is the most recent proc_stats_info, and
/// delta_time_in_seconds is the elapsed time between the last measure and current in seconds. NOTE: Returns -1.0f on
/// error.
float calculate_cpu_usage(const proc_stats_info& current_query, float delta_time_in_seconds) const;
/// Returns the memory usage in kB. First element is the real mem whereas the second is the virtual mem.
void mem_usage(sys_metrics_t& metrics) const;
/// Calculate the memory parameters and writes them in metrics.
/// NOTE: on error, metrics memory parameters are set to 0.
void calculate_mem_usage(sys_metrics_t& metrics) const;
private:
proc_stats_info last_query;

@ -58,12 +58,12 @@ sys_metrics_t sys_metrics_processor::get_metrics()
sys_metrics_t metrics;
// Get the memory metrics.
mem_usage(metrics);
calculate_mem_usage(metrics);
// Get the stats from the proc.
proc_stats_info current_query;
metrics.thread_count = current_query.num_threads;
metrics.process_cpu_usage = cpu_usage(current_query, measure_interval_ms / 1000.f);
metrics.process_cpu_usage = calculate_cpu_usage(current_query, measure_interval_ms / 1000.f);
// Update the last values.
last_query_time = current_time;
@ -72,7 +72,8 @@ sys_metrics_t sys_metrics_processor::get_metrics()
return metrics;
}
float sys_metrics_processor::cpu_usage(const proc_stats_info& current_query, float delta_time_in_seconds) const
float sys_metrics_processor::calculate_cpu_usage(const proc_stats_info& current_query,
float delta_time_in_seconds) const
{
// Error current value has to be greater than last value.
if (current_query.stime < last_query.stime || current_query.utime < last_query.utime) {
@ -103,11 +104,26 @@ static unsigned read_memory_value_from_line(const std::string& line)
return value;
}
/// Sets the memory parameters of the given metrics to zero.
static void set_mem_to_zero(sys_metrics_t& metrics)
{
metrics.process_realmem_kB = 0;
metrics.process_virtualmem_kB = 0;
metrics.process_virtualmem = 0;
metrics.process_realmem = 0;
metrics.system_mem = 0;
}
static void calculate_percentage_memory(sys_metrics_t& metrics)
{
std::ifstream file("/proc/meminfo");
std::string line;
if (!file) {
set_mem_to_zero(metrics);
return;
}
// Total system's memory is in the first line.
std::getline(file, line);
unsigned long long total_mem_kB = read_memory_value_from_line(line);
@ -123,11 +139,16 @@ static void calculate_percentage_memory(sys_metrics_t& metrics)
metrics.system_mem = (1.f - float(available_mem_kB) / float(total_mem_kB)) * 100.f;
}
void sys_metrics_processor::mem_usage(sys_metrics_t& metrics) const
void sys_metrics_processor::calculate_mem_usage(sys_metrics_t& metrics) const
{
std::ifstream file("/proc/self/status");
std::string line;
if (!file) {
set_mem_to_zero(metrics);
return;
}
while (std::getline(file, line)) {
// Looks for Virtual memory.
if (line.find("VmSize:") != std::string::npos) {

Loading…
Cancel
Save