From: Michael Tremer Date: Sun, 5 Oct 2025 10:55:19 +0000 (+0000) Subject: daemon: Use macros to convert time X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a9c17cf58f76c515e0399772986bd262417cfd7d;p=collecty.git daemon: Use macros to convert time That way, I will hopefully not get the maths wrong all the time. Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 9a63a10..1cd98a0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/src/daemon/graph.c b/src/daemon/graph.c index a5f26fe..248aac4 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -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); diff --git a/src/daemon/queue.c b/src/daemon/queue.c index 553944e..4777056 100644 --- a/src/daemon/queue.c +++ b/src/daemon/queue.c @@ -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; diff --git a/src/daemon/source.c b/src/daemon/source.c index 6a2e9ae..9046d2c 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -33,12 +33,13 @@ #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 @@ -51,10 +52,10 @@ #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 diff --git a/src/daemon/sources/test-stall.c b/src/daemon/sources/test-stall.c index 8c5c9bb..8730653 100644 --- a/src/daemon/sources/test-stall.c +++ b/src/daemon/sources/test-stall.c @@ -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 index 0000000..cbcaf14 --- /dev/null +++ b/src/daemon/time.h @@ -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 . # +# # +#############################################################################*/ + +#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 */