Decay: Take current time as an argument.

This better facilitates testing.
This commit is contained in:
David Goldblatt 2020-03-10 08:52:58 -07:00 committed by David Goldblatt
parent bf55e58e63
commit f77cec311e
3 changed files with 14 additions and 9 deletions

View File

@ -133,14 +133,14 @@ bool decay_ms_valid(ssize_t decay_ms);
* *
* Returns true on error. * Returns true on error.
*/ */
bool decay_init(decay_t *decay, ssize_t decay_ms); bool decay_init(decay_t *decay, nstime_t *cur_time, ssize_t decay_ms);
/* /*
* Given an already-initialized decay_t, reinitialize it with the given decay * Given an already-initialized decay_t, reinitialize it with the given decay
* time. The decay_t must have previously been initialized (and should not then * time. The decay_t must have previously been initialized (and should not then
* be zeroed). * be zeroed).
*/ */
void decay_reinit(decay_t *decay, ssize_t decay_ms); void decay_reinit(decay_t *decay, nstime_t *cur_time, ssize_t decay_ms);
/* Returns true if the epoch advanced and there are pages to purge. */ /* Returns true if the epoch advanced and there are pages to purge. */
bool decay_maybe_advance_epoch(decay_t *decay, nstime_t *new_time, bool decay_maybe_advance_epoch(decay_t *decay, nstime_t *new_time,

View File

@ -619,7 +619,9 @@ arena_decay_ms_set(tsdn_t *tsdn, arena_t *arena, decay_t *decay,
* infrequent, either between the {-1, 0, >0} states, or a one-time * infrequent, either between the {-1, 0, >0} states, or a one-time
* arbitrary change during initial arena configuration. * arbitrary change during initial arena configuration.
*/ */
decay_reinit(decay, decay_ms); nstime_t cur_time;
nstime_init_update(&cur_time);
decay_reinit(decay, &cur_time, decay_ms);
arena_maybe_decay(tsdn, arena, decay, decay_stats, ecache, false); arena_maybe_decay(tsdn, arena, decay, decay_stats, ecache, false);
malloc_mutex_unlock(tsdn, &decay->mtx); malloc_mutex_unlock(tsdn, &decay->mtx);
@ -1846,11 +1848,14 @@ arena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {
goto label_error; goto label_error;
} }
if (decay_init(&arena->pa_shard.decay_dirty, nstime_t cur_time;
nstime_init_update(&cur_time);
if (decay_init(&arena->pa_shard.decay_dirty, &cur_time,
arena_dirty_decay_ms_default_get())) { arena_dirty_decay_ms_default_get())) {
goto label_error; goto label_error;
} }
if (decay_init(&arena->pa_shard.decay_muzzy, if (decay_init(&arena->pa_shard.decay_muzzy, &cur_time,
arena_muzzy_decay_ms_default_get())) { arena_muzzy_decay_ms_default_get())) {
goto label_error; goto label_error;
} }

View File

@ -21,7 +21,7 @@ decay_deadline_init(decay_t *decay) {
} }
void void
decay_reinit(decay_t *decay, ssize_t decay_ms) { decay_reinit(decay_t *decay, nstime_t *cur_time, ssize_t decay_ms) {
atomic_store_zd(&decay->time_ms, decay_ms, ATOMIC_RELAXED); atomic_store_zd(&decay->time_ms, decay_ms, ATOMIC_RELAXED);
if (decay_ms > 0) { if (decay_ms > 0) {
nstime_init(&decay->interval, (uint64_t)decay_ms * nstime_init(&decay->interval, (uint64_t)decay_ms *
@ -29,7 +29,7 @@ decay_reinit(decay_t *decay, ssize_t decay_ms) {
nstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS); nstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS);
} }
nstime_init_update(&decay->epoch); nstime_copy(&decay->epoch, cur_time);
decay->jitter_state = (uint64_t)(uintptr_t)decay; decay->jitter_state = (uint64_t)(uintptr_t)decay;
decay_deadline_init(decay); decay_deadline_init(decay);
decay->nunpurged = 0; decay->nunpurged = 0;
@ -37,7 +37,7 @@ decay_reinit(decay_t *decay, ssize_t decay_ms) {
} }
bool bool
decay_init(decay_t *decay, ssize_t decay_ms) { decay_init(decay_t *decay, nstime_t *cur_time, ssize_t decay_ms) {
if (config_debug) { if (config_debug) {
for (size_t i = 0; i < sizeof(decay_t); i++) { for (size_t i = 0; i < sizeof(decay_t); i++) {
assert(((char *)decay)[i] == 0); assert(((char *)decay)[i] == 0);
@ -49,7 +49,7 @@ decay_init(decay_t *decay, ssize_t decay_ms) {
return true; return true;
} }
decay->purging = false; decay->purging = false;
decay_reinit(decay, decay_ms); decay_reinit(decay, cur_time, decay_ms);
return false; return false;
} }