From: Michael Tremer Date: Sun, 26 Oct 2025 17:43:19 +0000 (+0000) Subject: sources: Collect cpufreq information X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4ab992c36a752da48cc15e7179f7b2b5d199edb1;p=telemetry.git sources: Collect cpufreq information Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index bf16b36..e6e581f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -153,6 +153,8 @@ dist_telemetryd_SOURCES = \ src/daemon/sources/conntrack.h \ src/daemon/sources/contextswitches.c \ src/daemon/sources/contextswitches.h \ + src/daemon/sources/cpufreq.c \ + src/daemon/sources/cpufreq.h \ src/daemon/sources/df.c \ src/daemon/sources/df.h \ src/daemon/sources/disk.c \ diff --git a/src/daemon/sources.c b/src/daemon/sources.c index 994d1e5..cf06119 100644 --- a/src/daemon/sources.c +++ b/src/daemon/sources.c @@ -31,6 +31,7 @@ // Load all sources #include "sources/conntrack.h" #include "sources/contextswitches.h" +#include "sources/cpufreq.h" #include "sources/df.h" #include "sources/disk.h" #include "sources/hostapd.h" @@ -67,6 +68,7 @@ static const td_source_impl* source_impls[] = { &conntrack_source, &contextswitches_source, + &cpufreq_source, &df_source, &disk_source, &hostapd_source, diff --git a/src/daemon/sources/cpufreq.c b/src/daemon/sources/cpufreq.c new file mode 100644 index 0000000..d315bc1 --- /dev/null +++ b/src/daemon/sources/cpufreq.c @@ -0,0 +1,110 @@ +/*############################################################################# +# # +# telemetryd - The IPFire Telemetry Collection Service # +# Copyright (C) 2025 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include + +#include "../ctx.h" +#include "../file.h" +#include "../source.h" +#include "../string.h" +#include "cpufreq.h" + +#define BASE_PATH "/sys/devices/system/cpu/cpu%ld/cpufreq/" + +static int cpufreq_heartbeat_cpu(td_ctx* ctx, td_source* source, long cpu) { + char object[NAME_MAX]; + uint64_t freq_cur = 0; + uint64_t freq_min = 0; + uint64_t freq_max = 0; + uint64_t total_trans = 0; + int r; + + // Format the object name + r = td_string_format(object, "%ld", cpu); + if (r < 0) + return r; + + // Fetch the current frequency + r = td_read_uint64(ctx, &freq_cur, BASE_PATH "scaling_cur_freq", cpu); + if (r < 0) + return r; + + // Fetch the minimum frequency + r = td_read_uint64(ctx, &freq_min, BASE_PATH "scaling_min_freq", cpu); + if (r < 0) + return r; + + // Fetch the maximum frequency + r = td_read_uint64(ctx, &freq_max, BASE_PATH "scaling_max_freq", cpu); + if (r < 0) + return r; + + // Fetch the total transitions + r = td_read_uint64(ctx, &total_trans, BASE_PATH "stats/total_trans", cpu); + if (r < 0) + return r; + + // Convert values from kHz to Hz + freq_cur *= 1000; + freq_min *= 1000; + freq_max *= 1000; + + // Submit everything + return td_source_submit_values(source, object, VALUES( + VALUE_UINT64("freq_cur", &freq_cur), + VALUE_UINT64("freq_min", &freq_min), + VALUE_UINT64("freq_max", &freq_max), + VALUE_UINT64("total_trans", &total_trans) + )); +} + +static int cpufreq_heartbeat(td_ctx* ctx, td_source* source) { + int r; + + // Fetch the number of available processors + long num = sysconf(_SC_NPROCESSORS_CONF); + + // Collect metrics for each available processor + for (long i = 0; i < num; i++) { + r = cpufreq_heartbeat_cpu(ctx, source, i); + if (r < 0) + return r; + } + + return 0; +} + +const td_source_impl cpufreq_source = { + .name = "cpufreq", + + // RRD Data Sources + .rrd_dss = { + { "freq_cur", "GAUGE", 0, -1, }, + { "freq_min", "GAUGE", 0, -1, }, + { "freq_max", "GAUGE", 0, -1, }, + { "total_trans", "DERIVE", 0, -1, }, + { NULL }, + }, + + // Methods + .heartbeat = cpufreq_heartbeat, +}; diff --git a/src/daemon/sources/cpufreq.h b/src/daemon/sources/cpufreq.h new file mode 100644 index 0000000..7bb61f7 --- /dev/null +++ b/src/daemon/sources/cpufreq.h @@ -0,0 +1,28 @@ +/*############################################################################# +# # +# telemetryd - The IPFire Telemetry Collection Service # +# Copyright (C) 2025 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef TELEMETRY_SOURCE_CPUFREQ_H +#define TELEMETRY_SOURCE_CPUFREQ_H + +#include "../source.h" + +extern const td_source_impl cpufreq_source; + +#endif /* TELEMETRY_SOURCE_CPUFREQ_H */