Merge pull request #46 from zorun/monotonic_clock

Use a monotonic clock instead of a realtime clock
This commit is contained in:
Thomas Graf 2016-11-25 15:24:20 +04:00 committed by GitHub
commit 2efc2f4a60
3 changed files with 28 additions and 8 deletions

View File

@ -58,7 +58,7 @@ AC_CHECK_HEADERS(sys/param.h sys/socket.h)
AC_CHECK_TYPES(suseconds_t)
AC_CHECK_FUNCS(atexit gettimeofday memset pow socket strcasecmp)
AC_CHECK_FUNCS(atexit clock_gettime memset pow socket strcasecmp)
AC_CHECK_FUNCS(strchr strdup strerror strncasecmp strstr strtol)
AC_CHECK_FUNCS(uname getdate)
@ -86,6 +86,9 @@ esac
AC_CHECK_LIB(m, pow, [], AC_MSG_ERROR([requires libm]))
# Don't fail if not found (for instance, OS X does not have clock_gettime)
AC_CHECK_LIB(rt, clock_gettime, [], [])
BMON_LIB=""
#####################################################################

View File

@ -9,6 +9,9 @@
/* Define to 1 if you have the `atexit' function. */
#undef HAVE_ATEXIT
/* Define to 1 if you have the `clock_gettime' function. */
#undef HAVE_CLOCK_GETTIME
/* have curses */
#undef HAVE_CURSES
@ -39,15 +42,15 @@
/* Define to 1 if you have the <getopt.h> header file. */
#undef HAVE_GETOPT_H
/* Define to 1 if you have the `gettimeofday' function. */
#undef HAVE_GETTIMEOFDAY
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the `m' library (-lm). */
#undef HAVE_LIBM
/* Define to 1 if you have the `rt' library (-lrt). */
#undef HAVE_LIBRT
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

View File

@ -27,6 +27,11 @@
#include <bmon/conf.h>
#include <bmon/utils.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
void *xcalloc(size_t n, size_t s)
{
void *d = calloc(n, s);
@ -112,12 +117,21 @@ int timestamp_is_negative(timestamp_t *ts)
void update_timestamp(timestamp_t *dst)
{
struct timeval tv;
#ifdef __MACH__
clock_serv_t cclock;
mach_timespec_t tp;
gettimeofday(&tv, NULL);
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &tp);
mach_port_deallocate(mach_task_self(), cclock);
#else
struct timespec tp;
dst->tv_sec = tv.tv_sec;
dst->tv_usec = tv.tv_usec;
clock_gettime(CLOCK_MONOTONIC, &tp);
#endif
dst->tv_sec = tp.tv_sec;
dst->tv_usec = tp.tv_nsec / 1000;
}
void copy_timestamp(timestamp_t *ts1, timestamp_t *ts2)