From 293e457d0808f1b51abed3172d3c18daadd48bb4 Mon Sep 17 00:00:00 2001 From: Andrew Cagney Date: Tue, 19 Dec 2023 16:23:26 -0500 Subject: [PATCH] Use CLOCK_REALTIME when logging and CLOCK_MONOTONIC when timing Change update_current_time() so that current_time is set from CLOCK_REALTIME and current_time_ms is set from CLOCK_MONOTONIC. This way log messages display wall clock time while any timing calculations use monotonic time. See #203 In seqmap_add(), before checking to see if .time_ts wrapped, check that .time_ts was set. See #288 --- configure.ac | 14 -------------- src/fping.c | 26 ++++++-------------------- src/seqmap.c | 6 +++--- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/configure.ac b/configure.ac index cfe702f9..27c99dd6 100644 --- a/configure.ac +++ b/configure.ac @@ -9,19 +9,6 @@ m4_ifdef([AC_AUTOCONF_VERSION],[AC_USE_SYSTEM_EXTENSIONS], [AC_GNU_SOURCE]) # Detect Operatingsystem AC_CANONICAL_TARGET -only_clock_realtime=no - -case "${target}" in - *darwin*) - only_clock_realtime=yes - ;; - *freebsd*) - only_clock_realtime=yes - ;; - *openbsd*) - only_clock_realtime=yes - ;; -esac dnl --disable-ipv4 AC_ARG_ENABLE([ipv4], @@ -60,7 +47,6 @@ dnl Test if --enable-timestamp is explicitely enabled and make an error if this AS_IF([test "x$enable_timestamp" = "xyes" -a "x$have_so_timestamp" = "xno"], [ AC_MSG_ERROR([--enable-timestamp not supported on this platform]) ]) -AS_IF([test "x$only_clock_realtime" = "xyes"], [AC_DEFINE(ONLY_CLOCK_REALTIME, [1], [ONLY_CLOCK_REALTIME is defined])]) AC_ARG_ENABLE([safe-limits], AS_HELP_STRING([--enable-safe-limits], [Restrict timing parameters (-i, -p) within "safe" limits])) diff --git a/src/fping.c b/src/fping.c index 27711c8f..95932ff7 100644 --- a/src/fping.c +++ b/src/fping.c @@ -114,24 +114,6 @@ extern int h_errno; } #endif /* __cplusplus */ -/*** Constants ***/ - -/* CLOCK_MONTONIC starts under macOS, OpenBSD and FreeBSD with undefined positive point and can not be use - * see github PR #217 - * The configure script detect the predefined operating systems an set CLOCK_REALTIME using over ONLY_CLOCK_REALTIME variable - */ -#if HAVE_SO_TIMESTAMPNS || ONLY_CLOCK_REALTIME -#define CLOCKID CLOCK_REALTIME -#endif - -#if !defined(CLOCKID) -#if defined(CLOCK_MONOTONIC) -#define CLOCKID CLOCK_MONOTONIC -#else -#define CLOCKID CLOCK_REALTIME -#endif -#endif - /*** Ping packet defines ***/ #define MAX_IP_PACKET 65536 /* (theoretical) max IP packet size */ @@ -1510,8 +1492,12 @@ void signal_handler(int signum) void update_current_time() { - clock_gettime(CLOCKID, ¤t_time); - current_time_ns = timespec_ns(¤t_time); + /* pull the realtime clock for logging */ + clock_gettime(CLOCK_REALTIME, ¤t_time); + /* pull the monotonic clock for timing */ + struct timespec monotonic_time; + clock_gettime(CLOCK_MONOTONIC, &monotonic_time); + current_time_ns = timespec_ns(&monotonic_time); } /************************************************************ diff --git a/src/seqmap.c b/src/seqmap.c index 4a4e38a0..8f7f74d2 100644 --- a/src/seqmap.c +++ b/src/seqmap.c @@ -81,9 +81,9 @@ unsigned int seqmap_add(unsigned int host_nr, unsigned int ping_count, int64_t t /* check if expired (note that unused seqmap values will have fields set to * 0, so will be seen as expired */ next_value = &seqmap_map[seqmap_next_id]; - if (timestamp - next_value->ping_ts < SEQMAP_TIMEOUT_IN_NS) { - fprintf(stderr, "fping error: not enough sequence numbers available! (expire_timeout=%" PRId64 ", host_nr=%d, ping_count=%d, seqmap_next_id=%d)\n", - SEQMAP_TIMEOUT_IN_NS, host_nr, ping_count, seqmap_next_id); + if (next_value->ping_ts != 0 && timestamp - next_value->ping_ts < SEQMAP_TIMEOUT_IN_NS) { + fprintf(stderr, "fping error: not enough sequence numbers available! (expire_timeout=%" PRId64 ", host_nr=%d, ping_count=%d, seqmap_next_id=%d ping_st=%" PRId64 ")\n", + SEQMAP_TIMEOUT_IN_NS, host_nr, ping_count, seqmap_next_id, next_value->ping_ts); exit(4); }