That way, I will hopefully not get the maths wrong all the time.
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
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
#include "ctx.h"
#include "daemon.h"
#include "graph.h"
+#include "time.h"
struct collecty_graph {
collecty_ctx* ctx;
}
// 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);
#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;
#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[] = {
// 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);
} 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);
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) {
// 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
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
#include "../ctx.h"
#include "../source.h"
+#include "../time.h"
#include "test-stall.h"
/*
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 = {
--- /dev/null
+/*#############################################################################
+# #
+# 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 */