From 207e5066efba5910c4503f812a0f54585bc86e76 Mon Sep 17 00:00:00 2001 From: Yash Kumaraswamy Date: Thu, 5 Jan 2017 09:59:48 -0800 Subject: [PATCH] use clock_gettime instead of gettimeofday (#49) ``` - CLOCK_REALTIME_COARSE - clock_getres: 0 seconds | 4000000 nanoseconds Cost (min): 12 cycles Cost (avg): 53 cycles Cost (max): 37061 cycles - GETTIMEOFDAY - gettimeofday_getres: 1us Cost (min): 309 cycles Cost (avg): 429 cycles Cost (max): 107227 cycles ``` /cc @lyft/observability /cc @theatrus --- src/sampling.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sampling.c b/src/sampling.c index c9dd0fe..f69437a 100644 --- a/src/sampling.c +++ b/src/sampling.c @@ -8,6 +8,11 @@ #include "hashmap.h" #include "stats.h" +#ifdef __APPLE__ +#include +#include +#endif + #define HM_SIZE 32768 typedef struct expiring_entry { @@ -135,10 +140,21 @@ int sampler_init(sampler_t** sampler, int threshold, int window, int cardinality } static time_t timestamp() { - struct timeval detail_time; - gettimeofday(&detail_time,NULL); + struct timespec current_time; + time_t timestamp_sec; +#ifdef __APPLE__ + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + timestamp_sec = mts.tv_sec; +#else + clock_gettime(CLOCK_REALTIME_COARSE, ¤t_time); + timestamp_sec = current_time.tv_sec; +#endif - return detail_time.tv_sec; + return timestamp_sec; } static int sampler_update_callback(void* _s, const char* key, void* _value, void *metadata) {