Skip to content

Commit

Permalink
Use monotonic clock.
Browse files Browse the repository at this point in the history
This avoids a jumping system clock causing issues.
  • Loading branch information
dajohi committed Dec 7, 2018
1 parent e41c213 commit b8999bd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ AC_CHECK_LIB([cap], [cap_set_proc], [],
AS_IF([test "$host_os" = linux-gnu],
AC_MSG_WARN([Capabilities support is strongly recommended for increased security. See SECURITY for more information.])))

AC_CHECK_FUNC([clock_gettime], [
AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define if your system has clock_gettime])
])

# Enable ipinfo
AC_ARG_WITH([ipinfo],
[AS_HELP_STRING([--without-ipinfo], [Do not try to use ipinfo lookup at all])],
Expand Down
20 changes: 10 additions & 10 deletions packet/probe_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ void send_probe(
return;
}

if (gettimeofday(&probe->platform.departure_time, NULL)) {
error(EXIT_FAILURE, errno, "gettimeofday failure");
if (getmonotime(&probe->platform.departure_time)) {
error(EXIT_FAILURE, errno, "getmonotime failure");
}

// there might be an off-by-one in the number of tries here.
Expand Down Expand Up @@ -667,8 +667,8 @@ void receive_probe(
struct timeval now;

if (timestamp == NULL) {
if (gettimeofday(&now, NULL)) {
error(EXIT_FAILURE, errno, "gettimeofday failure");
if (getmonotime(&now)) {
error(EXIT_FAILURE, errno, "getmonotime failure");
}

timestamp = &now;
Expand Down Expand Up @@ -726,8 +726,8 @@ void receive_replies_from_recv_socket(
Get the time immediately after reading the packet to
keep the timing as precise as we can.
*/
if (gettimeofday(&timestamp, NULL)) {
error(EXIT_FAILURE, errno, "gettimeofday failure");
if (getmonotime(&timestamp)) {
error(EXIT_FAILURE, errno, "getmonotime failure");
}

if (packet_length == -1) {
Expand Down Expand Up @@ -975,8 +975,8 @@ void check_probe_timeouts(
struct probe_t *probe;
struct probe_t *probe_safe_iter;

if (gettimeofday(&now, NULL)) {
error(EXIT_FAILURE, errno, "gettimeofday failure");
if (getmonotime(&now)) {
error(EXIT_FAILURE, errno, "getmonotime failure");
}

LIST_FOREACH_SAFE(probe, &net_state->outstanding_probes,
Expand Down Expand Up @@ -1008,8 +1008,8 @@ bool get_next_probe_timeout(
struct timeval now;
struct timeval probe_timeout;

if (gettimeofday(&now, NULL)) {
error(EXIT_FAILURE, errno, "gettimeofday failure");
if (getmonotime(&now)) {
error(EXIT_FAILURE, errno, "getmonotime failure");
}

have_timeout = false;
Expand Down
22 changes: 22 additions & 0 deletions packet/timeval.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,30 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
#endif

#include "timeval.h"

/*
Return timeval using monotonic clock.
*/
int getmonotime(
struct timeval *tv)
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
return -1;
}
TIMESPEC_TO_TIMEVAL(tv, &ts);
return 0;
#else
return gettimeofday(tv, NULL);
#endif
}

/*
Ensure that a timevalue has a microsecond value in the range
[0.0, 1.0e6) microseconds by converting microseconds to full seconds.
Expand Down
3 changes: 3 additions & 0 deletions packet/timeval.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#include <sys/time.h>

int getmonotime(
struct timeval *timeval);

void normalize_timeval(
struct timeval *timeval);

Expand Down
16 changes: 13 additions & 3 deletions ui/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ void select_loop(
struct timeval intervaltime;
static double dnsinterval = 0;

memset(&startgrace, 0, sizeof(startgrace));

#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
TIMESPEC_TO_TIMEVAL(&lasttime, &ts);
#else
gettimeofday(&lasttime, NULL);
#endif

memset(&startgrace, 0, sizeof(startgrace));

while (1) {
dt = calc_deltatime(ctl->WaitTime);
Expand Down Expand Up @@ -123,8 +129,12 @@ void select_loop(
if (ctl->Interactive)
display_redraw(ctl);

#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_MONOTONIC, &ts);
TIMESPEC_TO_TIMEVAL(&thistime, &ts);
#else
gettimeofday(&thistime, NULL);

#endif
if (thistime.tv_sec > lasttime.tv_sec + intervaltime.tv_sec
|| (thistime.tv_sec ==
lasttime.tv_sec + intervaltime.tv_sec
Expand Down

0 comments on commit b8999bd

Please sign in to comment.