]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Use macros to convert time
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Oct 2025 10:55:19 +0000 (10:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Oct 2025 10:55:19 +0000 (10:55 +0000)
That way, I will hopefully not get the maths wrong all the time.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/graph.c
src/daemon/queue.c
src/daemon/source.c
src/daemon/sources/test-stall.c
src/daemon/time.h [new file with mode: 0644]

index 9a63a104ea4beaab726160a6770cd8c4e14f5e38..1cd98a00d3f6662d688e3537935e4ee8259c60f3 100644 (file)
@@ -148,6 +148,7 @@ dist_collectyd_SOURCES = \
        src/daemon/sources/test-flapping.h \
        src/daemon/sources/test-stall.c \
        src/daemon/sources/test-stall.h \
+       src/daemon/time.h \
        src/daemon/util.c \
        src/daemon/util.h
 
index a5f26fe4bca1a8ba35a3c24b0c60dc42b33a8fbe..248aac445573e0a63a0cc8fd875e76f602600920 100644 (file)
@@ -29,6 +29,7 @@
 #include "ctx.h"
 #include "daemon.h"
 #include "graph.h"
+#include "time.h"
 
 struct collecty_graph {
        collecty_ctx* ctx;
@@ -358,8 +359,8 @@ int collecty_graph_render(collecty_graph* self, const char* object,
        }
 
        // Log action
-       DEBUG(self->ctx, "Rendered graph %s in %.2fms:\n",
-               collecty_graph_get_name(self), (double)(t_end - t_start) / CLOCKS_PER_SEC * 1000);
+       DEBUG(self->ctx, "Rendered graph %s in %.2fms:\n", collecty_graph_get_name(self),
+                       USEC_TO_MSEC((double)(t_end - t_start) / CLOCKS_PER_SEC));
        DEBUG(self->ctx, "    size     : %d byte(s)\n", ftell(f));
        DEBUG(self->ctx, "    width    : %d\n", w);
        DEBUG(self->ctx, "    height   : %d\n", h);
index 553944e09db18f9e28ebf6004a21bd8bbf92b328..4777056f0f3d435617d534a5a4c9b7f444f6eabf 100644 (file)
@@ -28,8 +28,9 @@
 #include "ctx.h"
 #include "daemon.h"
 #include "queue.h"
+#include "time.h"
 
-#define HEARTBEAT 300000000 // 300s
+#define HEARTBEAT SEC_TO_USEC(300) // 5 minutes
 
 struct collecty_queue_object {
        STAILQ_ENTRY(collecty_queue_object) nodes;
index 6a2e9aee53f3d387ca0d48308841f8807250fabc..9046d2c7ea7a23f4d4a126b238c1b20a2b5db694 100644 (file)
 #include "ctx.h"
 #include "daemon.h"
 #include "source.h"
+#include "time.h"
 #include "util.h"
 
 #define STEPSIZE 60 // seconds
 
 // Interval after which the heartbeat function is being called again
-#define HEARTBEAT (STEPSIZE * 1000000) // usecs
+#define HEARTBEAT SEC_TO_USEC(STEPSIZE)
 
 // XXX We need to check whether it is a good idea to hardcode this here
 #define XFF 0.1
 #define FLAPPING_THRESHOLD     3
 
 // Complain if the heartbeat was delayed by more than this
-#define DELAY_THRESHOLD                1000000 // 1 second
+#define DELAY_THRESHOLD                SEC_TO_USEC(1) // 1 second
 
 // Complain if collect() took longer than this time
-#define RUNTIME_THRESHOLD      250000 // 250 milliseconds
+#define RUNTIME_THRESHOLD      MSEC_TO_USEC(250) // 250 milliseconds
 
 // Define some default RRAs
 static const collecty_rrd_rra default_rras[] = {
@@ -170,7 +171,7 @@ static int collecty_source_error_detection(collecty_source* self, int result, ui
        // Complain if an iteration took too long
        if (runtime >= RUNTIME_THRESHOLD) {
                ERROR(self->ctx, "Heartbeat for %s stalled the event loop for %.2lfms\n",
-                       collecty_source_name(self), (double)runtime / 1000);
+                       collecty_source_name(self), USEC_TO_MSEC((double)runtime));
 
                // Decrease the priority for this source so it won't stall any other sources
                r = sd_event_source_set_priority(self->events.heartbeat, SD_EVENT_PRIORITY_IDLE);
@@ -181,7 +182,7 @@ static int collecty_source_error_detection(collecty_source* self, int result, ui
        } else {
                // Log the runtime
                DEBUG(self->ctx, "Heartbeat for %s took %.2lfms\n",
-                       collecty_source_name(self), (double)runtime / 1000);
+                       collecty_source_name(self), USEC_TO_MSEC((double)runtime));
 
                // Decrease the priority for this source so it won't stall any other sources
                r = sd_event_source_set_priority(self->events.heartbeat, SD_EVENT_PRIORITY_NORMAL);
@@ -252,7 +253,7 @@ static uint64_t collecty_source_elapsed_time(void) {
                return 0;
 
        // Return as µsec
-       return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
+       return SEC_TO_USEC(ts.tv_sec) + NSEC_TO_USEC(ts.tv_nsec);
 }
 
 static int collecty_source_heartbeat(sd_event_source* source, uint64_t usec, void* data) {
@@ -272,7 +273,7 @@ static int collecty_source_heartbeat(sd_event_source* source, uint64_t usec, voi
        // Complain if we got called very late
        if (t_delay >= DELAY_THRESHOLD) {
                ERROR(self->ctx, "Heartbeat for %s was delayed by %.2lfms\n",
-                       collecty_source_name(self), (double)t_delay / 1000);
+                       collecty_source_name(self), USEC_TO_MSEC((double)t_delay));
        }
 
        // Call the collect method
@@ -294,7 +295,7 @@ static int collecty_source_heartbeat(sd_event_source* source, uint64_t usec, voi
        switch (self->state) {
                // If we are in error state we might skip this source for a while
                case STATE_ERROR:
-                       next_heartbeat = usec + 3600000000; // 1 hr
+                       next_heartbeat = usec + SEC_TO_USEC(3600); // 1 hr
                        break;
 
                // Don't call again if we have been disabled
index 8c5c9bbd8b22ba38d7a971afaedec421d481a06f..87306537f5e8295777865719d0c0b5b9bfeecc30 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "../ctx.h"
 #include "../source.h"
+#include "../time.h"
 #include "test-stall.h"
 
 /*
@@ -28,7 +29,7 @@
 
 static int test_stall_collect(collecty_ctx* ctx, collecty_source* source) {
        // Sleep for 500ms
-       return usleep(500000);
+       return usleep(MSEC_TO_USEC(500));
 }
 
 const collecty_source_impl test_stall_source = {
diff --git a/src/daemon/time.h b/src/daemon/time.h
new file mode 100644 (file)
index 0000000..cbcaf14
--- /dev/null
@@ -0,0 +1,51 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# 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 COLLECTY_TIME_H
+#define COLLECTY_TIME_H
+
+// Seconds to milliseconds
+#define SEC_TO_MSEC(s)         ((s) * 1000UL)
+
+// Seconds to microseconds
+#define SEC_TO_USEC(s)         ((s) * 1000000UL)
+
+// Seconds to nanoseconds
+#define SEC_TO_NSEC(s)         ((s) * 1000000000UL)
+
+// Milliseconds to seconds
+#define MSEC_TO_SEC(ms)                ((ms) / 1000UL)
+
+// Milliseconds to microseconds
+#define MSEC_TO_USEC(ms)       ((ms) * 1000UL)
+
+// Microseconds to seconds
+#define USEC_TO_SEC(us)                ((us) / 1000000UL)
+
+// Microseconds to milliseconds
+#define USEC_TO_MSEC(us)       ((us) / 1000UL)
+
+// Nanoseconds to seconds
+#define NSEC_TO_SEC(ns)                ((ns) / 1000000000UL)
+
+// Nanoseconds to microseconds
+#define NSEC_TO_USEC(ns)       ((ns) / 1000UL)
+
+#endif /* COLLECTY_TIME_H */