| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/clock.h> |
| 7 |
|
| 8 |
#include <time.h> |
| 9 |
|
| 10 |
static const uint64_t NS_PER_SEC = 1000000000; |
| 11 |
|
| 12 |
#if defined(CLOCK_BOOTTIME) |
| 13 |
# define HIGH_RES_CLOCK CLOCK_BOOTTIME |
| 14 |
#elif defined(CLOCK_MONOTONIC_RAW) |
| 15 |
# define HIGH_RES_CLOCK CLOCK_MONOTONIC_RAW |
| 16 |
#else |
| 17 |
# define HIGH_RES_CLOCK CLOCK_MONOTONIC |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#if defined(__MACH__) |
| 27 |
# include <AvailabilityMacros.h> |
| 28 |
# include <aws/common/thread.h> |
| 29 |
# include <dlfcn.h> |
| 30 |
# include <sys/time.h> |
| 31 |
|
| 32 |
static int s_legacy_get_time(uint64_t *timestamp) { |
| 33 |
struct timeval tv; |
| 34 |
int ret_val = gettimeofday(&tv, NULL); |
| 35 |
|
| 36 |
if (ret_val) { |
| 37 |
return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); |
| 38 |
} |
| 39 |
|
| 40 |
uint64_t secs = (uint64_t)tv.tv_sec; |
| 41 |
uint64_t u_secs = (uint64_t)tv.tv_usec; |
| 42 |
*timestamp = (secs * NS_PER_SEC) + (u_secs * 1000); |
| 43 |
return AWS_OP_SUCCESS; |
| 44 |
} |
| 45 |
|
| 46 |
# if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 |
| 47 |
static aws_thread_once s_thread_once_flag = AWS_THREAD_ONCE_STATIC_INIT; |
| 48 |
static int (*s_gettime_fn)(clockid_t clock_id, struct timespec *tp) = NULL; |
| 49 |
|
| 50 |
static void s_do_osx_loads(void *user_data) { |
| 51 |
(void)user_data; |
| 52 |
s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec *tp))dlsym(RTLD_DEFAULT, "clock_gettime"); |
| 53 |
} |
| 54 |
|
| 55 |
int aws_high_res_clock_get_ticks(uint64_t *timestamp) { |
| 56 |
aws_thread_call_once(&s_thread_once_flag, s_do_osx_loads, NULL); |
| 57 |
int ret_val = 0; |
| 58 |
|
| 59 |
if (s_gettime_fn) { |
| 60 |
struct timespec ts; |
| 61 |
ret_val = s_gettime_fn(HIGH_RES_CLOCK, &ts); |
| 62 |
|
| 63 |
if (ret_val) { |
| 64 |
return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); |
| 65 |
} |
| 66 |
|
| 67 |
uint64_t secs = (uint64_t)ts.tv_sec; |
| 68 |
uint64_t n_secs = (uint64_t)ts.tv_nsec; |
| 69 |
*timestamp = (secs * NS_PER_SEC) + n_secs; |
| 70 |
return AWS_OP_SUCCESS; |
| 71 |
} |
| 72 |
|
| 73 |
return s_legacy_get_time(timestamp); |
| 74 |
} |
| 75 |
|
| 76 |
int aws_sys_clock_get_ticks(uint64_t *timestamp) { |
| 77 |
aws_thread_call_once(&s_thread_once_flag, s_do_osx_loads, NULL); |
| 78 |
int ret_val = 0; |
| 79 |
|
| 80 |
if (s_gettime_fn) { |
| 81 |
struct timespec ts; |
| 82 |
ret_val = s_gettime_fn(CLOCK_REALTIME, &ts); |
| 83 |
if (ret_val) { |
| 84 |
return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); |
| 85 |
} |
| 86 |
|
| 87 |
uint64_t secs = (uint64_t)ts.tv_sec; |
| 88 |
uint64_t n_secs = (uint64_t)ts.tv_nsec; |
| 89 |
*timestamp = (secs * NS_PER_SEC) + n_secs; |
| 90 |
return AWS_OP_SUCCESS; |
| 91 |
} |
| 92 |
return s_legacy_get_time(timestamp); |
| 93 |
} |
| 94 |
# else |
| 95 |
int aws_high_res_clock_get_ticks(uint64_t *timestamp) { |
| 96 |
return s_legacy_get_time(timestamp); |
| 97 |
} |
| 98 |
|
| 99 |
int aws_sys_clock_get_ticks(uint64_t *timestamp) { |
| 100 |
return s_legacy_get_time(timestamp); |
| 101 |
} |
| 102 |
|
| 103 |
# endif |
| 104 |
|
| 105 |
#else |
| 106 |
int aws_high_res_clock_get_ticks(uint64_t *timestamp) { |
| 107 |
int ret_val = 0; |
| 108 |
|
| 109 |
struct timespec ts; |
| 110 |
|
| 111 |
ret_val = clock_gettime(HIGH_RES_CLOCK, &ts); |
| 112 |
|
| 113 |
if (ret_val) { |
| 114 |
return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); |
| 115 |
} |
| 116 |
|
| 117 |
uint64_t secs = (uint64_t)ts.tv_sec; |
| 118 |
uint64_t n_secs = (uint64_t)ts.tv_nsec; |
| 119 |
*timestamp = (secs * NS_PER_SEC) + n_secs; |
| 120 |
return AWS_OP_SUCCESS; |
| 121 |
} |
| 122 |
|
| 123 |
int aws_sys_clock_get_ticks(uint64_t *timestamp) { |
| 124 |
int ret_val = 0; |
| 125 |
|
| 126 |
struct timespec ts; |
| 127 |
ret_val = clock_gettime(CLOCK_REALTIME, &ts); |
| 128 |
if (ret_val) { |
| 129 |
return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); |
| 130 |
} |
| 131 |
|
| 132 |
uint64_t secs = (uint64_t)ts.tv_sec; |
| 133 |
uint64_t n_secs = (uint64_t)ts.tv_nsec; |
| 134 |
*timestamp = (secs * NS_PER_SEC) + n_secs; |
| 135 |
|
| 136 |
return AWS_OP_SUCCESS; |
| 137 |
} |
| 138 |
#endif |