Skip to content

Commit 630f865

Browse files
Yash Kumaraswamylyft-buildnotify
authored andcommitted
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
1 parent 532a9fe commit 630f865

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/sampling.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#include "hashmap.h"
99
#include "stats.h"
1010

11+
#ifdef __APPLE__
12+
#include <mach/clock.h>
13+
#include <mach/mach.h>
14+
#endif
15+
1116
#define HM_SIZE 32768
1217

1318
typedef struct expiring_entry {
@@ -135,10 +140,21 @@ int sampler_init(sampler_t** sampler, int threshold, int window, int cardinality
135140
}
136141

137142
static time_t timestamp() {
138-
struct timeval detail_time;
139-
gettimeofday(&detail_time,NULL);
143+
struct timespec current_time;
144+
time_t timestamp_sec;
145+
#ifdef __APPLE__
146+
clock_serv_t cclock;
147+
mach_timespec_t mts;
148+
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
149+
clock_get_time(cclock, &mts);
150+
mach_port_deallocate(mach_task_self(), cclock);
151+
timestamp_sec = mts.tv_sec;
152+
#else
153+
clock_gettime(CLOCK_REALTIME_COARSE, &current_time);
154+
timestamp_sec = current_time.tv_sec;
155+
#endif
140156

141-
return detail_time.tv_sec;
157+
return timestamp_sec;
142158
}
143159

144160
static int sampler_update_callback(void* _s, const char* key, void* _value, void *metadata) {

0 commit comments

Comments
 (0)