]> git.ipfire.org Git - collecty.git/commitdiff
sources: Collect cpufreq information
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Oct 2025 17:43:19 +0000 (17:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Oct 2025 17:43:19 +0000 (17:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/sources.c
src/daemon/sources/cpufreq.c [new file with mode: 0644]
src/daemon/sources/cpufreq.h [new file with mode: 0644]

index bf16b367a3f705379ce3a12f7d0fa18fd253e1d5..e6e581f2e8e57ca4f5e6186a20381e3ca22d8267 100644 (file)
@@ -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 \
index 994d1e5226638afd697c58ef52b021530d1f00b0..cf06119e8ae534ad5c1c11ab919a112080de6edf 100644 (file)
@@ -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 (file)
index 0000000..d315bc1
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..7bb61f7
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */