From 1d01e4c770c3229041f1010037da2533568fef05 Mon Sep 17 00:00:00 2001 From: Yinan Zhang Date: Mon, 16 Dec 2019 12:41:06 -0800 Subject: [PATCH] Initialization utilities for nstime --- include/jemalloc/internal/nstime.h | 8 ++++++++ src/arena.c | 9 +++------ src/background_thread.c | 7 +++---- src/ctl.c | 2 +- src/mutex.c | 8 ++++---- src/nstime.c | 6 ++++++ src/prof.c | 4 ++-- src/prof_log.c | 12 +++++++----- test/src/timer.c | 3 +-- test/unit/background_thread.c | 9 ++++----- test/unit/decay.c | 9 +++------ test/unit/nstime.c | 10 ++++------ 12 files changed, 46 insertions(+), 41 deletions(-) diff --git a/include/jemalloc/internal/nstime.h b/include/jemalloc/internal/nstime.h index 17c177c7..a3766ff2 100644 --- a/include/jemalloc/internal/nstime.h +++ b/include/jemalloc/internal/nstime.h @@ -31,4 +31,12 @@ extern nstime_monotonic_t *JET_MUTABLE nstime_monotonic; typedef bool (nstime_update_t)(nstime_t *); 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 */ diff --git a/src/arena.c b/src/arena.c index 5537e66f..05c4021c 100644 --- a/src/arena.c +++ b/src/arena.c @@ -667,8 +667,7 @@ arena_decay_reinit(arena_decay_t *decay, ssize_t decay_ms) { nstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS); } - nstime_init(&decay->epoch, 0); - nstime_update(&decay->epoch); + nstime_init_update(&decay->epoch); decay->jitter_state = (uint64_t)(uintptr_t)decay; arena_decay_deadline_init(decay); decay->nunpurged = 0; @@ -726,8 +725,7 @@ arena_maybe_decay(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay, } nstime_t time; - nstime_init(&time, 0); - nstime_update(&time); + nstime_init_update(&time); if (unlikely(!nstime_monotonic() && nstime_compare(&decay->epoch, &time) > 0)) { /* @@ -2066,8 +2064,7 @@ arena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) { /* Set arena before creating background threads. */ arena_set(ind, arena); - nstime_init(&arena->create_time, 0); - nstime_update(&arena->create_time); + nstime_init_update(&arena->create_time); /* We don't support reentrancy for arena 0 bootstrapping. */ if (ind != 0) { diff --git a/src/background_thread.c b/src/background_thread.c index 4a74edbf..400dae53 100644 --- a/src/background_thread.c +++ b/src/background_thread.c @@ -74,7 +74,7 @@ background_thread_info_init(tsdn_t *tsdn, background_thread_info_t *info) { info->npages_to_purge_new = 0; if (config_stats) { 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); /* We need malloc clock (can be different from tv). */ nstime_t next_wakeup; - nstime_init(&next_wakeup, 0); - nstime_update(&next_wakeup); + nstime_init_update(&next_wakeup); nstime_iadd(&next_wakeup, interval); assert(nstime_ns(&next_wakeup) < BACKGROUND_THREAD_INDEFINITE_SLEEP); @@ -794,7 +793,7 @@ background_thread_stats_read(tsdn_t *tsdn, background_thread_stats_t *stats) { 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)); uint64_t num_runs = 0; diff --git a/src/ctl.c b/src/ctl.c index e2cdc29d..24f530f0 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -1037,7 +1037,7 @@ ctl_background_thread_stats_read(tsdn_t *tsdn) { if (!have_background_thread || background_thread_stats_read(tsdn, stats)) { memset(stats, 0, sizeof(background_thread_stats_t)); - nstime_init(&stats->run_interval, 0); + nstime_init_zero(&stats->run_interval); } malloc_mutex_prof_copy( &ctl_stats->mutex_prof_data[global_prof_mutex_max_per_bg_thd], diff --git a/src/mutex.c b/src/mutex.c index 3f920f5b..bffcfb5f 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -46,7 +46,7 @@ JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, void malloc_mutex_lock_slow(malloc_mutex_t *mutex) { mutex_prof_data_t *data = &mutex->prof_data; - nstime_t before = NSTIME_ZERO_INITIALIZER; + nstime_t before; if (ncpus == 1) { goto label_spin_done; @@ -68,7 +68,7 @@ malloc_mutex_lock_slow(malloc_mutex_t *mutex) { return; } label_spin_done: - nstime_update(&before); + nstime_init_update(&before); /* Copy before to after to avoid clock skews. */ nstime_t after; nstime_copy(&after, &before); @@ -104,8 +104,8 @@ label_spin_done: static void mutex_prof_data_init(mutex_prof_data_t *data) { memset(data, 0, sizeof(mutex_prof_data_t)); - nstime_init(&data->max_wait_time, 0); - nstime_init(&data->tot_wait_time, 0); + nstime_init_zero(&data->max_wait_time); + nstime_init_zero(&data->tot_wait_time); data->prev_owner = NULL; } diff --git a/src/nstime.c b/src/nstime.c index 71db3539..eb8f6c0a 100644 --- a/src/nstime.c +++ b/src/nstime.c @@ -168,3 +168,9 @@ nstime_update_impl(nstime_t *time) { return false; } 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); +} diff --git a/src/prof.c b/src/prof.c index 4d3a8006..d1d46e2e 100644 --- a/src/prof.c +++ b/src/prof.c @@ -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 * when free() is called. */ - nstime_t t = NSTIME_ZERO_INITIALIZER; - nstime_update(&t); + nstime_t t; + nstime_init_update(&t); prof_alloc_time_set(tsd, ptr, &t); malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock); diff --git a/src/prof_log.c b/src/prof_log.c index b5879348..2904f0c6 100644 --- a/src/prof_log.c +++ b/src/prof_log.c @@ -38,7 +38,7 @@ static char log_filename[ 1]; /* 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. */ 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 free_time = NSTIME_ZERO_INITIALIZER; - nstime_update(&free_time); + nstime_t free_time; + nstime_init_update(&free_time); size_t sz = sizeof(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) { 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); emitter_json_kv(emitter, "duration", emitter_type_uint64, &ns); @@ -702,6 +702,8 @@ bool prof_log_init(tsd_t *tsd) { return true; } + nstime_init_zero(&log_start_timestamp); + log_tables_initialized = true; return false; } diff --git a/test/src/timer.c b/test/src/timer.c index c451c639..6e8b8edb 100644 --- a/test/src/timer.c +++ b/test/src/timer.c @@ -2,8 +2,7 @@ void timer_start(timedelta_t *timer) { - nstime_init(&timer->t0, 0); - nstime_update(&timer->t0); + nstime_init_update(&timer->t0); } void diff --git a/test/unit/background_thread.c b/test/unit/background_thread.c index f7bd37c4..f597285a 100644 --- a/test/unit/background_thread.c +++ b/test/unit/background_thread.c @@ -83,9 +83,8 @@ TEST_BEGIN(test_background_thread_running) { assert_b_eq(info->state, background_thread_started, "Background_thread did not start.\n"); - nstime_t start, now; - nstime_init(&start, 0); - nstime_update(&start); + nstime_t start; + nstime_init_update(&start); bool ran = false; while (true) { @@ -98,8 +97,8 @@ TEST_BEGIN(test_background_thread_running) { break; } - nstime_init(&now, 0); - nstime_update(&now); + nstime_t now; + nstime_init_update(&now); nstime_subtract(&now, &start); assert_u64_lt(nstime_sec(&now), 1000, "Background threads did not run for 1000 seconds."); diff --git a/test/unit/decay.c b/test/unit/decay.c index cf3c0796..59936db3 100644 --- a/test/unit/decay.c +++ b/test/unit/decay.c @@ -384,8 +384,7 @@ decay_ticker_helper(unsigned arena_ind, int flags, bool dirty, ssize_t dt, #define NINTERVALS 101 nstime_t time, update_interval, decay_ms, deadline; - nstime_init(&time, 0); - nstime_update(&time); + nstime_init_update(&time); nstime_init2(&decay_ms, dt, 0); nstime_copy(&deadline, &time); @@ -456,8 +455,7 @@ TEST_BEGIN(test_decay_ticker) { } nupdates_mock = 0; - nstime_init(&time_mock, 0); - nstime_update(&time_mock); + nstime_init_update(&time_mock); monotonic_mock = true; nstime_monotonic_orig = nstime_monotonic; @@ -507,8 +505,7 @@ TEST_BEGIN(test_decay_nonmonotonic) { npurge0 = get_arena_npurge(0); nupdates_mock = 0; - nstime_init(&time_mock, 0); - nstime_update(&time_mock); + nstime_init_update(&time_mock); monotonic_mock = false; nstime_monotonic_orig = nstime_monotonic; diff --git a/test/unit/nstime.c b/test/unit/nstime.c index f3137805..5a736bba 100644 --- a/test/unit/nstime.c +++ b/test/unit/nstime.c @@ -25,7 +25,7 @@ TEST_BEGIN(test_nstime_copy) { nstime_t nsta, nstb; nstime_init2(&nsta, 42, 43); - nstime_init(&nstb, 0); + nstime_init_zero(&nstb); nstime_copy(&nstb, &nsta); assert_u64_eq(nstime_sec(&nstb), 42, "sec 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_copy(&nstb, &nsta); nstime_subtract(&nsta, &nstb); - nstime_init(&nstb, 0); + nstime_init_zero(&nstb); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect subtraction result"); @@ -126,7 +126,7 @@ TEST_BEGIN(test_nstime_isubtract) { nstime_init2(&nsta, 42, 43); nstime_isubtract(&nsta, 42*BILLION + 43); - nstime_init(&nstb, 0); + nstime_init_zero(&nstb); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect subtraction result"); @@ -209,9 +209,7 @@ TEST_END TEST_BEGIN(test_nstime_update) { nstime_t nst; - nstime_init(&nst, 0); - - assert_false(nstime_update(&nst), "Basic time update failed."); + assert_false(nstime_init_update(&nst), "Basic time update failed."); /* Only Rip Van Winkle sleeps this long. */ {