2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
2016-02-22 03:25:02 +08:00
|
|
|
|
2017-04-18 07:17:02 +08:00
|
|
|
#include "jemalloc/internal/nstime.h"
|
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define BILLION UINT64_C(1000000000)
|
2017-03-04 11:58:43 +08:00
|
|
|
#define MILLION UINT64_C(1000000)
|
2016-02-22 03:25:02 +08:00
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_init(nstime_t *time, uint64_t ns) {
|
2016-02-22 03:25:02 +08:00
|
|
|
time->ns = ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec) {
|
2016-02-22 03:25:02 +08:00
|
|
|
time->ns = sec * BILLION + nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_ns(const nstime_t *time) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return time->ns;
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
|
|
|
|
2017-03-04 11:58:43 +08:00
|
|
|
uint64_t
|
|
|
|
nstime_msec(const nstime_t *time) {
|
|
|
|
return time->ns / MILLION;
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_sec(const nstime_t *time) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return time->ns / BILLION;
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_nsec(const nstime_t *time) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return time->ns % BILLION;
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_copy(nstime_t *time, const nstime_t *source) {
|
2016-02-22 03:25:02 +08:00
|
|
|
*time = *source;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_compare(const nstime_t *a, const nstime_t *b) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return (a->ns > b->ns) - (a->ns < b->ns);
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_add(nstime_t *time, const nstime_t *addend) {
|
2016-02-22 03:25:02 +08:00
|
|
|
assert(UINT64_MAX - time->ns >= addend->ns);
|
|
|
|
|
|
|
|
time->ns += addend->ns;
|
|
|
|
}
|
|
|
|
|
2017-05-13 03:30:33 +08:00
|
|
|
void
|
|
|
|
nstime_iadd(nstime_t *time, uint64_t addend) {
|
|
|
|
assert(UINT64_MAX - time->ns >= addend);
|
|
|
|
|
|
|
|
time->ns += addend;
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_subtract(nstime_t *time, const nstime_t *subtrahend) {
|
2016-02-22 03:25:02 +08:00
|
|
|
assert(nstime_compare(time, subtrahend) >= 0);
|
|
|
|
|
|
|
|
time->ns -= subtrahend->ns;
|
|
|
|
}
|
|
|
|
|
2017-05-13 03:30:33 +08:00
|
|
|
void
|
|
|
|
nstime_isubtract(nstime_t *time, uint64_t subtrahend) {
|
|
|
|
assert(time->ns >= subtrahend);
|
|
|
|
|
|
|
|
time->ns -= subtrahend;
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_imultiply(nstime_t *time, uint64_t multiplier) {
|
2016-02-22 03:25:02 +08:00
|
|
|
assert((((time->ns | multiplier) & (UINT64_MAX << (sizeof(uint64_t) <<
|
|
|
|
2))) == 0) || ((time->ns * multiplier) / multiplier == time->ns));
|
|
|
|
|
|
|
|
time->ns *= multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_idivide(nstime_t *time, uint64_t divisor) {
|
2016-02-22 03:25:02 +08:00
|
|
|
assert(divisor != 0);
|
|
|
|
|
|
|
|
time->ns /= divisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_divide(const nstime_t *time, const nstime_t *divisor) {
|
2016-02-22 03:25:02 +08:00
|
|
|
assert(divisor->ns != 0);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return time->ns / divisor->ns;
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 23:47:16 +08:00
|
|
|
#ifdef _WIN32
|
2016-10-11 13:15:10 +08:00
|
|
|
# define NSTIME_MONOTONIC true
|
2016-10-07 23:47:16 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_get(nstime_t *time) {
|
2016-10-07 23:47:16 +08:00
|
|
|
FILETIME ft;
|
|
|
|
uint64_t ticks_100ns;
|
|
|
|
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
ticks_100ns = (((uint64_t)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
|
|
|
|
|
|
|
|
nstime_init(time, ticks_100ns * 100);
|
|
|
|
}
|
2017-04-22 04:47:49 +08:00
|
|
|
#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE)
|
2016-10-11 13:15:10 +08:00
|
|
|
# define NSTIME_MONOTONIC true
|
2016-10-07 23:47:16 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_get(nstime_t *time) {
|
2016-10-07 23:47:16 +08:00
|
|
|
struct timespec ts;
|
|
|
|
|
2016-10-30 13:55:08 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
|
2016-10-07 23:47:16 +08:00
|
|
|
nstime_init2(time, ts.tv_sec, ts.tv_nsec);
|
|
|
|
}
|
2017-04-22 04:47:49 +08:00
|
|
|
#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC)
|
2016-10-11 13:15:10 +08:00
|
|
|
# define NSTIME_MONOTONIC true
|
2016-10-07 23:47:16 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_get(nstime_t *time) {
|
2016-10-07 23:47:16 +08:00
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
nstime_init2(time, ts.tv_sec, ts.tv_nsec);
|
|
|
|
}
|
2017-04-22 04:47:49 +08:00
|
|
|
#elif defined(JEMALLOC_HAVE_MACH_ABSOLUTE_TIME)
|
2016-10-11 13:15:10 +08:00
|
|
|
# define NSTIME_MONOTONIC true
|
2016-10-07 23:47:16 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_get(nstime_t *time) {
|
2016-10-07 23:47:16 +08:00
|
|
|
nstime_init(time, mach_absolute_time());
|
|
|
|
}
|
|
|
|
#else
|
2016-10-11 13:15:10 +08:00
|
|
|
# define NSTIME_MONOTONIC false
|
2016-10-07 23:47:16 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
nstime_get(nstime_t *time) {
|
2016-10-07 23:47:16 +08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
nstime_init2(time, tv.tv_sec, tv.tv_usec * 1000);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 14:10:42 +08:00
|
|
|
static bool
|
|
|
|
nstime_monotonic_impl(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NSTIME_MONOTONIC;
|
2016-10-11 13:15:10 +08:00
|
|
|
#undef NSTIME_MONOTONIC
|
|
|
|
}
|
2017-05-02 14:10:42 +08:00
|
|
|
nstime_monotonic_t *JET_MUTABLE nstime_monotonic = nstime_monotonic_impl;
|
2016-10-11 13:15:10 +08:00
|
|
|
|
2017-05-02 14:10:42 +08:00
|
|
|
static bool
|
|
|
|
nstime_update_impl(nstime_t *time) {
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_t old_time;
|
|
|
|
|
|
|
|
nstime_copy(&old_time, time);
|
2016-10-07 23:47:16 +08:00
|
|
|
nstime_get(time);
|
2016-02-22 03:25:02 +08:00
|
|
|
|
|
|
|
/* Handle non-monotonic clocks. */
|
|
|
|
if (unlikely(nstime_compare(&old_time, time) > 0)) {
|
|
|
|
nstime_copy(time, &old_time);
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-02-22 03:25:02 +08:00
|
|
|
}
|
2017-05-02 14:10:42 +08:00
|
|
|
nstime_update_t *JET_MUTABLE nstime_update = nstime_update_impl;
|