1 #include "jemalloc/internal/jemalloc_preamble.h"
2 #include "jemalloc/internal/jemalloc_internal_includes.h"
4 #include "jemalloc/internal/nstime.h"
6 #include "jemalloc/internal/assert.h"
8 #define BILLION UINT64_C(1000000000)
9 #define MILLION UINT64_C(1000000)
12 nstime_init(nstime_t *time, uint64_t ns) {
17 nstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec) {
18 time->ns = sec * BILLION + nsec;
22 nstime_ns(const nstime_t *time) {
27 nstime_msec(const nstime_t *time) {
28 return time->ns / MILLION;
32 nstime_sec(const nstime_t *time) {
33 return time->ns / BILLION;
37 nstime_nsec(const nstime_t *time) {
38 return time->ns % BILLION;
42 nstime_copy(nstime_t *time, const nstime_t *source) {
47 nstime_compare(const nstime_t *a, const nstime_t *b) {
48 return (a->ns > b->ns) - (a->ns < b->ns);
52 nstime_add(nstime_t *time, const nstime_t *addend) {
53 assert(UINT64_MAX - time->ns >= addend->ns);
55 time->ns += addend->ns;
59 nstime_iadd(nstime_t *time, uint64_t addend) {
60 assert(UINT64_MAX - time->ns >= addend);
66 nstime_subtract(nstime_t *time, const nstime_t *subtrahend) {
67 assert(nstime_compare(time, subtrahend) >= 0);
69 time->ns -= subtrahend->ns;
73 nstime_isubtract(nstime_t *time, uint64_t subtrahend) {
74 assert(time->ns >= subtrahend);
76 time->ns -= subtrahend;
80 nstime_imultiply(nstime_t *time, uint64_t multiplier) {
81 assert((((time->ns | multiplier) & (UINT64_MAX << (sizeof(uint64_t) <<
82 2))) == 0) || ((time->ns * multiplier) / multiplier == time->ns));
84 time->ns *= multiplier;
88 nstime_idivide(nstime_t *time, uint64_t divisor) {
95 nstime_divide(const nstime_t *time, const nstime_t *divisor) {
96 assert(divisor->ns != 0);
98 return time->ns / divisor->ns;
102 # define NSTIME_MONOTONIC true
104 nstime_get(nstime_t *time) {
106 uint64_t ticks_100ns;
108 GetSystemTimeAsFileTime(&ft);
109 ticks_100ns = (((uint64_t)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
111 nstime_init(time, ticks_100ns * 100);
113 #elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE)
114 # define NSTIME_MONOTONIC true
116 nstime_get(nstime_t *time) {
119 clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
120 nstime_init2(time, ts.tv_sec, ts.tv_nsec);
122 #elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC)
123 # define NSTIME_MONOTONIC true
125 nstime_get(nstime_t *time) {
128 clock_gettime(CLOCK_MONOTONIC, &ts);
129 nstime_init2(time, ts.tv_sec, ts.tv_nsec);
131 #elif defined(JEMALLOC_HAVE_MACH_ABSOLUTE_TIME)
132 # define NSTIME_MONOTONIC true
134 nstime_get(nstime_t *time) {
135 nstime_init(time, mach_absolute_time());
138 # define NSTIME_MONOTONIC false
140 nstime_get(nstime_t *time) {
143 gettimeofday(&tv, NULL);
144 nstime_init2(time, tv.tv_sec, tv.tv_usec * 1000);
149 nstime_monotonic_impl(void) {
150 return NSTIME_MONOTONIC;
151 #undef NSTIME_MONOTONIC
153 nstime_monotonic_t *JET_MUTABLE nstime_monotonic = nstime_monotonic_impl;
156 nstime_update_impl(nstime_t *time) {
159 nstime_copy(&old_time, time);
162 /* Handle non-monotonic clocks. */
163 if (unlikely(nstime_compare(&old_time, time) > 0)) {
164 nstime_copy(time, &old_time);
170 nstime_update_t *JET_MUTABLE nstime_update = nstime_update_impl;