Initialization utilities for nstime

This commit is contained in:
Yinan Zhang 2019-12-16 12:41:06 -08:00
parent dd649c9485
commit 1d01e4c770
12 changed files with 46 additions and 41 deletions

View File

@ -31,4 +31,12 @@ extern nstime_monotonic_t *JET_MUTABLE nstime_monotonic;
typedef bool (nstime_update_t)(nstime_t *); typedef bool (nstime_update_t)(nstime_t *);
extern nstime_update_t *JET_MUTABLE nstime_update; extern nstime_update_t *JET_MUTABLE nstime_update;
bool nstime_init_update(nstime_t *time);
JEMALLOC_ALWAYS_INLINE void
nstime_init_zero(nstime_t *time) {
static const nstime_t zero = NSTIME_ZERO_INITIALIZER;
nstime_copy(time, &zero);
}
#endif /* JEMALLOC_INTERNAL_NSTIME_H */ #endif /* JEMALLOC_INTERNAL_NSTIME_H */

View File

@ -667,8 +667,7 @@ arena_decay_reinit(arena_decay_t *decay, ssize_t decay_ms) {
nstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS); nstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS);
} }
nstime_init(&decay->epoch, 0); nstime_init_update(&decay->epoch);
nstime_update(&decay->epoch);
decay->jitter_state = (uint64_t)(uintptr_t)decay; decay->jitter_state = (uint64_t)(uintptr_t)decay;
arena_decay_deadline_init(decay); arena_decay_deadline_init(decay);
decay->nunpurged = 0; decay->nunpurged = 0;
@ -726,8 +725,7 @@ arena_maybe_decay(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,
} }
nstime_t time; nstime_t time;
nstime_init(&time, 0); nstime_init_update(&time);
nstime_update(&time);
if (unlikely(!nstime_monotonic() && nstime_compare(&decay->epoch, &time) if (unlikely(!nstime_monotonic() && nstime_compare(&decay->epoch, &time)
> 0)) { > 0)) {
/* /*
@ -2066,8 +2064,7 @@ arena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {
/* Set arena before creating background threads. */ /* Set arena before creating background threads. */
arena_set(ind, arena); arena_set(ind, arena);
nstime_init(&arena->create_time, 0); nstime_init_update(&arena->create_time);
nstime_update(&arena->create_time);
/* We don't support reentrancy for arena 0 bootstrapping. */ /* We don't support reentrancy for arena 0 bootstrapping. */
if (ind != 0) { if (ind != 0) {

View File

@ -74,7 +74,7 @@ background_thread_info_init(tsdn_t *tsdn, background_thread_info_t *info) {
info->npages_to_purge_new = 0; info->npages_to_purge_new = 0;
if (config_stats) { if (config_stats) {
info->tot_n_runs = 0; info->tot_n_runs = 0;
nstime_init(&info->tot_sleep_time, 0); nstime_init_zero(&info->tot_sleep_time);
} }
} }
@ -236,8 +236,7 @@ background_thread_sleep(tsdn_t *tsdn, background_thread_info_t *info,
interval <= BACKGROUND_THREAD_INDEFINITE_SLEEP); interval <= BACKGROUND_THREAD_INDEFINITE_SLEEP);
/* We need malloc clock (can be different from tv). */ /* We need malloc clock (can be different from tv). */
nstime_t next_wakeup; nstime_t next_wakeup;
nstime_init(&next_wakeup, 0); nstime_init_update(&next_wakeup);
nstime_update(&next_wakeup);
nstime_iadd(&next_wakeup, interval); nstime_iadd(&next_wakeup, interval);
assert(nstime_ns(&next_wakeup) < assert(nstime_ns(&next_wakeup) <
BACKGROUND_THREAD_INDEFINITE_SLEEP); BACKGROUND_THREAD_INDEFINITE_SLEEP);
@ -794,7 +793,7 @@ background_thread_stats_read(tsdn_t *tsdn, background_thread_stats_t *stats) {
return true; return true;
} }
nstime_init(&stats->run_interval, 0); nstime_init_zero(&stats->run_interval);
memset(&stats->max_counter_per_bg_thd, 0, sizeof(mutex_prof_data_t)); memset(&stats->max_counter_per_bg_thd, 0, sizeof(mutex_prof_data_t));
uint64_t num_runs = 0; uint64_t num_runs = 0;

View File

@ -1037,7 +1037,7 @@ ctl_background_thread_stats_read(tsdn_t *tsdn) {
if (!have_background_thread || if (!have_background_thread ||
background_thread_stats_read(tsdn, stats)) { background_thread_stats_read(tsdn, stats)) {
memset(stats, 0, sizeof(background_thread_stats_t)); memset(stats, 0, sizeof(background_thread_stats_t));
nstime_init(&stats->run_interval, 0); nstime_init_zero(&stats->run_interval);
} }
malloc_mutex_prof_copy( malloc_mutex_prof_copy(
&ctl_stats->mutex_prof_data[global_prof_mutex_max_per_bg_thd], &ctl_stats->mutex_prof_data[global_prof_mutex_max_per_bg_thd],

View File

@ -46,7 +46,7 @@ JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
void void
malloc_mutex_lock_slow(malloc_mutex_t *mutex) { malloc_mutex_lock_slow(malloc_mutex_t *mutex) {
mutex_prof_data_t *data = &mutex->prof_data; mutex_prof_data_t *data = &mutex->prof_data;
nstime_t before = NSTIME_ZERO_INITIALIZER; nstime_t before;
if (ncpus == 1) { if (ncpus == 1) {
goto label_spin_done; goto label_spin_done;
@ -68,7 +68,7 @@ malloc_mutex_lock_slow(malloc_mutex_t *mutex) {
return; return;
} }
label_spin_done: label_spin_done:
nstime_update(&before); nstime_init_update(&before);
/* Copy before to after to avoid clock skews. */ /* Copy before to after to avoid clock skews. */
nstime_t after; nstime_t after;
nstime_copy(&after, &before); nstime_copy(&after, &before);
@ -104,8 +104,8 @@ label_spin_done:
static void static void
mutex_prof_data_init(mutex_prof_data_t *data) { mutex_prof_data_init(mutex_prof_data_t *data) {
memset(data, 0, sizeof(mutex_prof_data_t)); memset(data, 0, sizeof(mutex_prof_data_t));
nstime_init(&data->max_wait_time, 0); nstime_init_zero(&data->max_wait_time);
nstime_init(&data->tot_wait_time, 0); nstime_init_zero(&data->tot_wait_time);
data->prev_owner = NULL; data->prev_owner = NULL;
} }

View File

@ -168,3 +168,9 @@ nstime_update_impl(nstime_t *time) {
return false; return false;
} }
nstime_update_t *JET_MUTABLE nstime_update = nstime_update_impl; nstime_update_t *JET_MUTABLE nstime_update = nstime_update_impl;
bool
nstime_init_update(nstime_t *time) {
nstime_init_zero(time);
return nstime_update(time);
}

View File

@ -166,8 +166,8 @@ prof_malloc_sample_object(tsd_t *tsd, const void *ptr, size_t usize,
/* Get the current time and set this in the extent_t. We'll read this /* Get the current time and set this in the extent_t. We'll read this
* when free() is called. */ * when free() is called. */
nstime_t t = NSTIME_ZERO_INITIALIZER; nstime_t t;
nstime_update(&t); nstime_init_update(&t);
prof_alloc_time_set(tsd, ptr, &t); prof_alloc_time_set(tsd, ptr, &t);
malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock); malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);

View File

@ -38,7 +38,7 @@ static char log_filename[
1]; 1];
/* Timestamp for most recent call to log_start(). */ /* Timestamp for most recent call to log_start(). */
static nstime_t log_start_timestamp = NSTIME_ZERO_INITIALIZER; static nstime_t log_start_timestamp;
/* Increment these when adding to the log_bt and log_thr linked lists. */ /* Increment these when adding to the log_bt and log_thr linked lists. */
static size_t log_bt_index = 0; static size_t log_bt_index = 0;
@ -231,8 +231,8 @@ prof_try_log(tsd_t *tsd, size_t usize, prof_info_t *prof_info) {
} }
nstime_t alloc_time = prof_info->alloc_time; nstime_t alloc_time = prof_info->alloc_time;
nstime_t free_time = NSTIME_ZERO_INITIALIZER; nstime_t free_time;
nstime_update(&free_time); nstime_init_update(&free_time);
size_t sz = sizeof(prof_alloc_node_t); size_t sz = sizeof(prof_alloc_node_t);
prof_alloc_node_t *new_node = (prof_alloc_node_t *) prof_alloc_node_t *new_node = (prof_alloc_node_t *)
@ -556,9 +556,9 @@ static void
prof_log_emit_metadata(emitter_t *emitter) { prof_log_emit_metadata(emitter_t *emitter) {
emitter_json_object_kv_begin(emitter, "info"); emitter_json_object_kv_begin(emitter, "info");
nstime_t now = NSTIME_ZERO_INITIALIZER; nstime_t now;
nstime_update(&now); nstime_init_update(&now);
uint64_t ns = nstime_ns(&now) - nstime_ns(&log_start_timestamp); uint64_t ns = nstime_ns(&now) - nstime_ns(&log_start_timestamp);
emitter_json_kv(emitter, "duration", emitter_type_uint64, &ns); emitter_json_kv(emitter, "duration", emitter_type_uint64, &ns);
@ -702,6 +702,8 @@ bool prof_log_init(tsd_t *tsd) {
return true; return true;
} }
nstime_init_zero(&log_start_timestamp);
log_tables_initialized = true; log_tables_initialized = true;
return false; return false;
} }

View File

@ -2,8 +2,7 @@
void void
timer_start(timedelta_t *timer) { timer_start(timedelta_t *timer) {
nstime_init(&timer->t0, 0); nstime_init_update(&timer->t0);
nstime_update(&timer->t0);
} }
void void

View File

@ -83,9 +83,8 @@ TEST_BEGIN(test_background_thread_running) {
assert_b_eq(info->state, background_thread_started, assert_b_eq(info->state, background_thread_started,
"Background_thread did not start.\n"); "Background_thread did not start.\n");
nstime_t start, now; nstime_t start;
nstime_init(&start, 0); nstime_init_update(&start);
nstime_update(&start);
bool ran = false; bool ran = false;
while (true) { while (true) {
@ -98,8 +97,8 @@ TEST_BEGIN(test_background_thread_running) {
break; break;
} }
nstime_init(&now, 0); nstime_t now;
nstime_update(&now); nstime_init_update(&now);
nstime_subtract(&now, &start); nstime_subtract(&now, &start);
assert_u64_lt(nstime_sec(&now), 1000, assert_u64_lt(nstime_sec(&now), 1000,
"Background threads did not run for 1000 seconds."); "Background threads did not run for 1000 seconds.");

View File

@ -384,8 +384,7 @@ decay_ticker_helper(unsigned arena_ind, int flags, bool dirty, ssize_t dt,
#define NINTERVALS 101 #define NINTERVALS 101
nstime_t time, update_interval, decay_ms, deadline; nstime_t time, update_interval, decay_ms, deadline;
nstime_init(&time, 0); nstime_init_update(&time);
nstime_update(&time);
nstime_init2(&decay_ms, dt, 0); nstime_init2(&decay_ms, dt, 0);
nstime_copy(&deadline, &time); nstime_copy(&deadline, &time);
@ -456,8 +455,7 @@ TEST_BEGIN(test_decay_ticker) {
} }
nupdates_mock = 0; nupdates_mock = 0;
nstime_init(&time_mock, 0); nstime_init_update(&time_mock);
nstime_update(&time_mock);
monotonic_mock = true; monotonic_mock = true;
nstime_monotonic_orig = nstime_monotonic; nstime_monotonic_orig = nstime_monotonic;
@ -507,8 +505,7 @@ TEST_BEGIN(test_decay_nonmonotonic) {
npurge0 = get_arena_npurge(0); npurge0 = get_arena_npurge(0);
nupdates_mock = 0; nupdates_mock = 0;
nstime_init(&time_mock, 0); nstime_init_update(&time_mock);
nstime_update(&time_mock);
monotonic_mock = false; monotonic_mock = false;
nstime_monotonic_orig = nstime_monotonic; nstime_monotonic_orig = nstime_monotonic;

View File

@ -25,7 +25,7 @@ TEST_BEGIN(test_nstime_copy) {
nstime_t nsta, nstb; nstime_t nsta, nstb;
nstime_init2(&nsta, 42, 43); nstime_init2(&nsta, 42, 43);
nstime_init(&nstb, 0); nstime_init_zero(&nstb);
nstime_copy(&nstb, &nsta); nstime_copy(&nstb, &nsta);
assert_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied"); assert_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied");
assert_u64_eq(nstime_nsec(&nstb), 43, "nsec incorrectly copied"); assert_u64_eq(nstime_nsec(&nstb), 43, "nsec incorrectly copied");
@ -108,7 +108,7 @@ TEST_BEGIN(test_nstime_subtract) {
nstime_init2(&nsta, 42, 43); nstime_init2(&nsta, 42, 43);
nstime_copy(&nstb, &nsta); nstime_copy(&nstb, &nsta);
nstime_subtract(&nsta, &nstb); nstime_subtract(&nsta, &nstb);
nstime_init(&nstb, 0); nstime_init_zero(&nstb);
assert_d_eq(nstime_compare(&nsta, &nstb), 0, assert_d_eq(nstime_compare(&nsta, &nstb), 0,
"Incorrect subtraction result"); "Incorrect subtraction result");
@ -126,7 +126,7 @@ TEST_BEGIN(test_nstime_isubtract) {
nstime_init2(&nsta, 42, 43); nstime_init2(&nsta, 42, 43);
nstime_isubtract(&nsta, 42*BILLION + 43); nstime_isubtract(&nsta, 42*BILLION + 43);
nstime_init(&nstb, 0); nstime_init_zero(&nstb);
assert_d_eq(nstime_compare(&nsta, &nstb), 0, assert_d_eq(nstime_compare(&nsta, &nstb), 0,
"Incorrect subtraction result"); "Incorrect subtraction result");
@ -209,9 +209,7 @@ TEST_END
TEST_BEGIN(test_nstime_update) { TEST_BEGIN(test_nstime_update) {
nstime_t nst; nstime_t nst;
nstime_init(&nst, 0); assert_false(nstime_init_update(&nst), "Basic time update failed.");
assert_false(nstime_update(&nst), "Basic time update failed.");
/* Only Rip Van Winkle sleeps this long. */ /* Only Rip Van Winkle sleeps this long. */
{ {