Skip to content

Commit

Permalink
use clock_gettime instead of gettimeofday (#49)
Browse files Browse the repository at this point in the history
```
- 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
  • Loading branch information
Yash Kumaraswamy authored and lyft-buildnotify committed Jan 5, 2017
1 parent be6e8d1 commit aab840d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "hashmap.h"
#include "stats.h"

#ifdef __APPLE__
#include <mach/clock.h>
#include <mach/mach.h>
#endif

#define HM_SIZE 32768

typedef struct expiring_entry {
Expand Down Expand Up @@ -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, &current_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) {
Expand Down

0 comments on commit aab840d

Please sign in to comment.