From 9c3666c05fa21cf8bbd2e4dc5c9a4655284dc234 Mon Sep 17 00:00:00 2001 From: Fred Klassen Date: Thu, 5 Dec 2013 22:27:42 -0800 Subject: [PATCH] fix #6 for OS X. Change deprecated UpTime() to mach_absolute_time() --- src/common/sendpacket.c | 2 +- src/common/timer.c | 2 +- src/common/timer.h | 35 ++++++++++++++++------------------- src/defines.h.in | 5 +++++ src/sleep.c | 2 +- src/sleep.h | 15 +++++---------- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/common/sendpacket.c b/src/common/sendpacket.c index c6526d60e..0f3359b6a 100644 --- a/src/common/sendpacket.c +++ b/src/common/sendpacket.c @@ -702,7 +702,7 @@ sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...) } -#if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET) && ! defined INJECT_METHOD +#if defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET /** * Inner sendpacket_open() method for using libpcap */ diff --git a/src/common/timer.c b/src/common/timer.c index afd1fe058..880ab3104 100755 --- a/src/common/timer.c +++ b/src/common/timer.c @@ -101,7 +101,7 @@ void init_timestamp(timestamp_t *ctx) { #ifdef HAVE_ABSOLUTE_TIME - SetZero(*ctx); + ctx = 0; #else timerclear(ctx); #endif diff --git a/src/common/timer.h b/src/common/timer.h index 6da2dde2a..494cfe0c0 100755 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -43,25 +43,9 @@ #include #ifdef HAVE_ABSOLUTE_TIME -#include +#include #endif -/* AbsoluteTime methods */ -#ifndef NonZero -#define NonZero(x) ((x).hi | (x).lo) -#endif -#ifndef SetZero -#define SetZero(x) do { (x).hi = 0 ; (x).lo = 0; } while(0) -#endif -#ifndef CopyAbsolute -#define CopyAbsolute(x, y) do { (x).lo = (y).lo ; (x).hi = (y).hi; } while (0) -#endif -#ifndef AbsoluteCmp -#define AbsoluteCmp(left, right, cmp) \ - (((left)->hi == (right)->hi) ? \ - ((left)->lo cmp (right)->lo) : \ - ((left)->hi cmp (right)->hi)) -#endif /* * 1 sec = 1,0000 millisec (ms) @@ -192,7 +176,20 @@ void timesdiv(struct timespec *tvs, COUNTER div); } while(0) #ifdef HAVE_ABSOLUTE_TIME - typedef AbsoluteTime timestamp_t; + typedef int64_t timestamp_t; + +static inline timestamp_t getUpTime(void) +{ + static mach_timebase_info_data_t s_timebase_info; + + if (s_timebase_info.denom == 0) { + (void) mach_timebase_info(&s_timebase_info); + } + + /* returns nsec (billionth of seconds) */ + return (timestamp_t)((mach_absolute_time() * (timestamp_t)s_timebase_info.numer) / + s_timebase_info.denom); +} #else typedef struct timeval timestamp_t; #endif @@ -205,7 +202,7 @@ static inline void get_packet_timestamp(timestamp_t *ctx) { #ifdef HAVE_ABSOLUTE_TIME - *ctx = UpTime(); + *ctx = getUpTime(); #else gettimeofday(ctx, NULL); #endif diff --git a/src/defines.h.in b/src/defines.h.in index 75fca4507..5187d9464 100644 --- a/src/defines.h.in +++ b/src/defines.h.in @@ -256,6 +256,11 @@ typedef u_int32_t uint32_t #define TIMEVAL_TO_MILLISEC(x) (((x)->tv_sec * 1000) + ((x)->tv_usec / 1000)) #define TIMEVAL_TO_MICROSEC(x) (((x)->tv_sec * 1000000) + (x)->tv_usec) #define TIMEVAL_TO_NANOSEC(x) ((u_int64_t)((x)->tv_sec * 1000000000) + ((u_int64_t)(x)->tv_usec * 1000)) +#ifdef HAVE_ABSOLUTE_TIME +# define TIMSTAMP_TO_MICROSEC(x) ((COUNTER)(*(x) / 1000)) +#else +# define TIMSTAMP_TO_MICROSEC(x) (TIMEVAL_TO_MICROSEC(x)) +#endif #define MILLISEC_TO_TIMEVAL(x, tv) \ do { \ diff --git a/src/sleep.c b/src/sleep.c index 15d217195..2c8d7f80d 100644 --- a/src/sleep.c +++ b/src/sleep.c @@ -188,7 +188,7 @@ do_sleep(struct timeval *time, struct timeval *last, int len, int accurate, * Ignore the time supplied by the capture file and send data at * a constant 'rate' (bytes per second). */ - now_us = TIMEVAL_TO_MICROSEC(sent_timestamp); + now_us = TIMSTAMP_TO_MICROSEC(sent_timestamp); if (now_us) { COUNTER bps = (COUNTER)options.speed.speed; COUNTER bits_sent = ((bytes_sent + (COUNTER)len) * 8LL); diff --git a/src/sleep.h b/src/sleep.h index 8c91ff302..dbb1f7ba3 100644 --- a/src/sleep.h +++ b/src/sleep.h @@ -84,26 +84,21 @@ gettimeofday_sleep(struct timespec nap) #ifdef HAVE_ABSOLUTE_TIME -#include /* - * Apple's AbsoluteTime functions give at least .1usec precision + * Apple's mach_absolute_time function gives nsec precision * which is pretty damn sweet */ static inline void absolute_time_sleep(struct timespec nap) { - AbsoluteTime sleep_until, naptime, time_left; - Nanoseconds nanosec; + timestamp_t sleep_until, time_left; - nanosec = UInt64ToUnsignedWide(TIMESPEC_TO_NANOSEC(&nap)); - naptime = NanosecondsToAbsolute(nanosec); - - sleep_until = AddAbsoluteToAbsolute(UpTime(), naptime); + sleep_until = getUpTime() + TIMESPEC_TO_NANOSEC(&nap); do { - time_left = SubAbsoluteFromAbsolute(sleep_until, UpTime()); - } while (NonZero(time_left)); + time_left = sleep_until - getUpTime(); + } while (time_left < 0); } #endif /* HAVE_ABSOLUTE_TIME */