From: Michael Tremer Date: Wed, 29 Oct 2025 10:55:01 +0000 (+0000) Subject: graphs: Add cpufreq graph X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bce66fd5a742162807f0d0fb974543ba60aff245;p=oddments%2Fcollecty.git graphs: Add cpufreq graph Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e6e581f..aed6bc4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/daemon/colors.h b/src/daemon/colors.h index 09577ce..970bf45 100644 --- a/src/daemon/colors.h +++ b/src/daemon/colors.h @@ -21,6 +21,8 @@ #ifndef TELEMETRY_COLORS_H #define TELEMETRY_COLORS_H +#include "util.h" + #define BLACK "#000000" #define WHITE "#ffffff" #define GREY "#9e9e9e" @@ -43,13 +45,29 @@ // 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 diff --git a/src/daemon/graphs.c b/src/daemon/graphs.c index 829a11c..d7d2b41 100644 --- a/src/daemon/graphs.c +++ b/src/daemon/graphs.c @@ -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 index 0000000..e3b6a8e --- /dev/null +++ b/src/daemon/graphs/cpufreq.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 0000000..489435d --- /dev/null +++ b/src/daemon/graphs/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_GRAPH_CPUFREQ_H +#define TELEMETRY_GRAPH_CPUFREQ_H + +#include "../graph.h" + +extern const td_graph_impl cpufreq_graph; + +#endif /* TELEMETRY_GRAPH_CPUFREQ_H */ diff --git a/src/daemon/graphs/graph.h b/src/daemon/graphs/graph.h index 50910e5..ab4d9d6 100644 --- a/src/daemon/graphs/graph.h +++ b/src/daemon/graphs/graph.h @@ -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" diff --git a/src/daemon/util.h b/src/daemon/util.h index fedb906..8eb1aee 100644 --- a/src/daemon/util.h +++ b/src/daemon/util.h @@ -23,6 +23,8 @@ #include +#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)