]> git.ipfire.org Git - oddments/collecty.git/commitdiff
graphs: Add cpufreq graph
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Oct 2025 10:55:01 +0000 (10:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Oct 2025 11:19:01 +0000 (11:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/colors.h
src/daemon/graphs.c
src/daemon/graphs/cpufreq.c [new file with mode: 0644]
src/daemon/graphs/cpufreq.h [new file with mode: 0644]
src/daemon/graphs/graph.h
src/daemon/util.h

index e6e581f2e8e57ca4f5e6186a20381e3ca22d8267..aed6bc41804f938ed90cc3b589d02452e68ba45d 100644 (file)
@@ -116,6 +116,8 @@ dist_telemetryd_SOURCES = \
        src/daemon/graphs/conntrack.h \
        src/daemon/graphs/contextswitches.c \
        src/daemon/graphs/contextswitches.h \
+       src/daemon/graphs/cpufreq.c \
+       src/daemon/graphs/cpufreq.h \
        src/daemon/graphs/graph.h \
        src/daemon/graphs/hostapd-station-bandwidth.c \
        src/daemon/graphs/hostapd-station-bandwidth.h \
index 09577ce2848e9558720e863798138659206e0a2d..970bf4590b02c4e75d7fb2dd5081e127ecde036a 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef TELEMETRY_COLORS_H
 #define TELEMETRY_COLORS_H
 
+#include "util.h"
+
 #define BLACK                  "#000000"
 #define WHITE                  "#ffffff"
 #define GREY                   "#9e9e9e"
 // Default - If something does not need to have a special meaning
 #define DEFAULT                        GREY
 
-// Limit - When there is a ceiling to resource usage
-#define LIMIT                  RED
+// Limits - When there is a floor/ceiling to resource usage
+#define MINIMUM                        GREEN
+#define MAXIMUM                        RED
+#define LIMIT                  MAXIMUM
 
 // Define colors for incoming/outgoing traffic
 #define COLOR_RX               GREEN
 #define COLOR_TX               RED
 
+// CPU Colors
+
+static inline const char* COLOR_CPU(long i) {
+       const char* cpu_colors[] = {
+               RED,
+               YELLOW,
+               GREEN,
+               ORANGE,
+               NULL,
+       };
+
+       return (cpu_colors[i % STATIC_ARRAY_SIZE(cpu_colors)]);
+}
+
 // Macro to make colours transparent
 #define COLOR_WITH_ALPHA(base, alpha) base alpha
 
index 829a11c9b2d01985b368180eb5fb8eacc859ca3b..d7d2b4115a6d0de3d265053b936dc71d6a5203ba 100644 (file)
@@ -30,6 +30,7 @@
 // Load all graphs
 #include "graphs/conntrack.h"
 #include "graphs/contextswitches.h"
+#include "graphs/cpufreq.h"
 #include "graphs/hostapd-station-bandwidth.h"
 #include "graphs/hostapd-station-rate-info.h"
 #include "graphs/hostapd-station-signal.h"
@@ -42,6 +43,7 @@
 static const td_graph_impl* graph_impls[] = {
        &conntrack_graph,
        &contextswitches_graph,
+       &cpufreq_graph,
        &hostapd_station_bandwidth_graph,
        &hostapd_station_rate_info_graph,
        &hostapd_station_signal_graph,
diff --git a/src/daemon/graphs/cpufreq.c b/src/daemon/graphs/cpufreq.c
new file mode 100644 (file)
index 0000000..e3b6a8e
--- /dev/null
@@ -0,0 +1,83 @@
+/*#############################################################################
+#                                                                             #
+# 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 <limits.h>
+
+#include "../string.h"
+#include "cpufreq.h"
+#include "graph.h"
+
+static int cpufreq_title(td_ctx* ctx,
+               td_graph* graph, const char* object, char* title, size_t length) {
+       return __td_string_set(title, length, _("CPU Frequency"));
+}
+
+static int cpufreq_vlabel(td_ctx* ctx,
+               td_graph* graph, const char* object, char* vlabel, size_t length) {
+       return __td_string_set(vlabel, length, _("Hz"));
+}
+
+static int cpufreq_render(td_ctx* ctx,
+               td_graph* graph, td_args* args, const char* _object) {
+       char object[NAME_MAX];
+       int r;
+
+       // Header
+       PRINT_HEADER4(args, _("Current"), _("Average"), _("Minimum"), _("Maximum"));
+
+       // Fetch the number of available processors
+       long num = sysconf(_SC_NPROCESSORS_CONF);
+
+       // This requires the sources for all CPU cores
+       for (long i = 0; i < num; i++) {
+               // Format the object
+               r = td_string_format(object, "%ld", i);
+               if (r < 0)
+                       return r;
+
+               // Require the source
+               r = td_graph_require_source(graph, args, "cpufreq", object);
+               if (r < 0)
+                       return r;
+
+               // Draw the line
+               DRAW_LINE1_WITH_LABEL(args, "freq_cur", object, COLOR_CPU(i), _("CPU Core %ld"));
+               PRINT_CAMM(args, "freq_cur", object, HZ, _("Hz"));
+
+               // Draw the minimum
+               DRAW_LINE2(args, "freq_min", object, MINIMUM);
+
+               // Draw the maximum
+               DRAW_LINE2(args, "freq_max", object, MAXIMUM);
+       }
+
+       return 0;
+}
+
+const td_graph_impl cpufreq_graph = {
+       .name    = "CPUFreq",
+       .render  = cpufreq_render,
+       .title   = cpufreq_title,
+       .vlabel  = cpufreq_vlabel,
+
+       // Limits
+       .lower_limit = 0,
+       .upper_limit = LONG_MAX,
+};
diff --git a/src/daemon/graphs/cpufreq.h b/src/daemon/graphs/cpufreq.h
new file mode 100644 (file)
index 0000000..489435d
--- /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_GRAPH_CPUFREQ_H
+#define TELEMETRY_GRAPH_CPUFREQ_H
+
+#include "../graph.h"
+
+extern const td_graph_impl cpufreq_graph;
+
+#endif /* TELEMETRY_GRAPH_CPUFREQ_H */
index 50910e51e46b6b533a1acca3b570706fae286c4c..ab4d9d6289f552ff7998a11d2253fc0eaf8fbc3c 100644 (file)
@@ -58,6 +58,7 @@
 #define FLOAT_WITH_UNIT "%%10.2lf %3s"
 #define LARGE_FLOAT            "%%14.2lf %%s"
 #define BPS                            "%%9.2lf %%s%3s"
+#define HZ                             "%%10.2lf %%s%2s"
 
 // Macro to terminate a line
 #define EOL                            "\\j"
index fedb90691751dcbd02999cdc1980e3fe417d888c..8eb1aee9ec130cedaf4355d5663961b6d075831a 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <stddef.h>
 
+#define STATIC_ARRAY_SIZE(a)   (sizeof(a) / sizeof(*a))
+
 // Preprocessor macro to make something a string
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)