Skip to content

Commit

Permalink
fix #6 for OS X. Change deprecated UpTime() to mach_absolute_time()
Browse files Browse the repository at this point in the history
  • Loading branch information
fklassen committed Dec 6, 2013
1 parent 52aeec1 commit 9c3666c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/common/sendpacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/common/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void
init_timestamp(timestamp_t *ctx)
{
#ifdef HAVE_ABSOLUTE_TIME
SetZero(*ctx);
ctx = 0;
#else
timerclear(ctx);
#endif
Expand Down
35 changes: 16 additions & 19 deletions src/common/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,9 @@
#include <math.h>

#ifdef HAVE_ABSOLUTE_TIME
#include <CoreServices/CoreServices.h>
#include <mach/mach_time.h>
#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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/defines.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand Down
2 changes: 1 addition & 1 deletion src/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 5 additions & 10 deletions src/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,21 @@ gettimeofday_sleep(struct timespec nap)


#ifdef HAVE_ABSOLUTE_TIME
#include <CoreServices/CoreServices.h>

/*
* 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 */
Expand Down

0 comments on commit 9c3666c

Please sign in to comment.