1 #include "jemalloc/internal/jemalloc_internal.h"
3 #define BILLION UINT64_C(1000000000)
6 nstime_init(nstime_t *time, uint64_t ns)
13 nstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec)
16 time->ns = sec * BILLION + nsec;
20 nstime_ns(const nstime_t *time)
27 nstime_sec(const nstime_t *time)
30 return (time->ns / BILLION);
34 nstime_nsec(const nstime_t *time)
37 return (time->ns % BILLION);
41 nstime_copy(nstime_t *time, const nstime_t *source)
48 nstime_compare(const nstime_t *a, const nstime_t *b)
51 return ((a->ns > b->ns) - (a->ns < b->ns));
55 nstime_add(nstime_t *time, const nstime_t *addend)
58 assert(UINT64_MAX - time->ns >= addend->ns);
60 time->ns += addend->ns;
64 nstime_subtract(nstime_t *time, const nstime_t *subtrahend)
67 assert(nstime_compare(time, subtrahend) >= 0);
69 time->ns -= subtrahend->ns;
73 nstime_imultiply(nstime_t *time, uint64_t multiplier)
76 assert((((time->ns | multiplier) & (UINT64_MAX << (sizeof(uint64_t) <<
77 2))) == 0) || ((time->ns * multiplier) / multiplier == time->ns));
79 time->ns *= multiplier;
83 nstime_idivide(nstime_t *time, uint64_t divisor)
92 nstime_divide(const nstime_t *time, const nstime_t *divisor)
95 assert(divisor->ns != 0);
97 return (time->ns / divisor->ns);
102 #define nstime_update JEMALLOC_N(n_nstime_update)
105 nstime_update(nstime_t *time)
109 nstime_copy(&old_time, time);
115 GetSystemTimeAsFileTime(&ft);
116 ticks = (((uint64_t)ft.dwHighDateTime) << 32) |
118 time->ns = ticks * 100;
120 #elif JEMALLOC_CLOCK_GETTIME
124 if (sysconf(_SC_MONOTONIC_CLOCK) > 0)
125 clock_gettime(CLOCK_MONOTONIC, &ts);
127 clock_gettime(CLOCK_REALTIME, &ts);
128 time->ns = ts.tv_sec * BILLION + ts.tv_nsec;
133 gettimeofday(&tv, NULL);
134 time->ns = tv.tv_sec * BILLION + tv.tv_usec * 1000;
138 /* Handle non-monotonic clocks. */
139 if (unlikely(nstime_compare(&old_time, time) > 0)) {
140 nstime_copy(time, &old_time);
148 #define nstime_update JEMALLOC_N(nstime_update)
149 nstime_update_t *nstime_update = JEMALLOC_N(n_nstime_update);