Skip to content

Commit

Permalink
Use CLOCK_REALTIME when logging and CLOCK_MONOTONIC when timing
Browse files Browse the repository at this point in the history
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 schweikert#203

In seqmap_add(), before checking to see if .time_ts wrapped,
check that .time_ts was set.

See schweikert#288
  • Loading branch information
cagney committed Dec 19, 2023
1 parent a3f4c57 commit 2d9023a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 37 deletions.
14 changes: 0 additions & 14 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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]))
Expand Down
26 changes: 6 additions & 20 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -1510,8 +1492,12 @@ void signal_handler(int signum)

void update_current_time()
{
clock_gettime(CLOCKID, &current_time);
current_time_ns = timespec_ns(&current_time);
/* pull the realtime clock for logging */
clock_gettime(CLOCK_REALTIME, &current_time);
/* pull the monotonic clock for timing */
struct timespec monotonic_time;
clock_gettime(CLOCK_MONOTONIC, &monotonic_time);
current_time_ns = timespec_ns(&monotonic_time);
}

/************************************************************
Expand Down
6 changes: 3 additions & 3 deletions src/seqmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2d9023a

Please sign in to comment.