From f0246463c01ac8a467ae180fc10e54e7a09aa9cb Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Tue, 1 Oct 2019 15:01:33 +0300 Subject: [PATCH] mdbx: rework sync_timestamp. Change-Id: I0e9ef9722735f89055814ba7eec0610ecdea8f0a --- src/elements/core.c | 23 ++++++++++++----------- src/elements/internals.h | 14 ++++++-------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index 67ed37aa..3e6c680d 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -3491,9 +3491,10 @@ __cold int mdbx_env_sync_ex(MDBX_env *env, int force, int nonblock) { pgno_t unsynced_pages = *env->me_unsynced_pages; if (!META_IS_STEADY(head) || unsynced_pages) { const pgno_t autosync_threshold = *env->me_autosync_threshold; - const uint64_t unsynced_timeout = *env->me_unsynced_timeout; + const uint64_t autosync_period = *env->me_autosync_period; if (force || (autosync_threshold && unsynced_pages >= autosync_threshold) || - (unsynced_timeout && mdbx_osal_monotime() >= unsynced_timeout)) + (autosync_period && + mdbx_osal_monotime() - *env->me_sync_timestamp >= autosync_period)) flags &= MDBX_WRITEMAP /* clear flags for full steady sync */; if (outside_txn) { @@ -5999,9 +6000,10 @@ static int mdbx_sync_locked(MDBX_env *env, unsigned flags, if (flags & (MDBX_NOSYNC | MDBX_MAPASYNC)) { /* Check auto-sync conditions */ const pgno_t autosync_threshold = *env->me_autosync_threshold; - const uint64_t unsynced_timeout = *env->me_unsynced_timeout; + const uint64_t autosync_period = *env->me_autosync_period; if ((autosync_threshold && *env->me_unsynced_pages >= autosync_threshold) || - (unsynced_timeout && mdbx_osal_monotime() >= unsynced_timeout)) + (autosync_period && + mdbx_osal_monotime() - *env->me_sync_timestamp >= autosync_period)) flags &= MDBX_WRITEMAP | MDBX_SHRINK_ALLOWED; /* force steady */ } @@ -6090,12 +6092,9 @@ static int mdbx_sync_locked(MDBX_env *env, unsigned flags, if (rc == MDBX_RESULT_FALSE /* carry steady */) { pending->mm_datasync_sign = mdbx_meta_sign(pending); *env->me_unsynced_pages = 0; - *env->me_unsynced_timeout = 0; + *env->me_sync_timestamp = mdbx_osal_monotime(); } else { assert(rc == MDBX_RESULT_TRUE /* carry non-steady */); - const uint64_t autosync_period = *env->me_autosync_period; - if (autosync_period && *env->me_unsynced_timeout == 0) - *env->me_unsynced_timeout = mdbx_osal_monotime() + autosync_period; pending->mm_datasync_sign = F_ISSET(env->me_flags, MDBX_UTTERLY_NOSYNC) ? MDBX_DATASIGN_NONE : MDBX_DATASIGN_WEAK; @@ -7176,7 +7175,7 @@ static int __cold mdbx_setup_lck(MDBX_env *env, char *lck_pathname, /* end of a locked section ---------------------------------------------- */ env->me_oldest = &env->me_lckless_stub.oldest; - env->me_unsynced_timeout = &env->me_lckless_stub.unsynced_timeout; + env->me_sync_timestamp = &env->me_lckless_stub.sync_timestamp; env->me_autosync_period = &env->me_lckless_stub.autosync_period; env->me_unsynced_pages = &env->me_lckless_stub.autosync_pending; env->me_autosync_threshold = &env->me_lckless_stub.autosync_threshold; @@ -7328,7 +7327,7 @@ static int __cold mdbx_setup_lck(MDBX_env *env, char *lck_pathname, mdbx_assert(env, !MDBX_IS_ERROR(lck_seize_rc)); env->me_oldest = &env->me_lck->mti_oldest_reader; - env->me_unsynced_timeout = &env->me_lck->mti_unsynced_timeout; + env->me_sync_timestamp = &env->me_lck->mti_sync_timestamp; env->me_autosync_period = &env->me_lck->mti_autosync_period; env->me_unsynced_pages = &env->me_lck->mti_unsynced_pages; env->me_autosync_threshold = &env->me_lck->mti_autosync_threshold; @@ -7601,10 +7600,12 @@ static int __cold mdbx_env_close0(MDBX_env *env) { if (env->me_lck) mdbx_munmap(&env->me_lck_mmap); env->me_oldest = nullptr; - env->me_unsynced_timeout = nullptr; + env->me_sync_timestamp = nullptr; env->me_autosync_period = nullptr; env->me_unsynced_pages = nullptr; env->me_autosync_threshold = nullptr; + env->me_discarded_tail = nullptr; + env->me_meta_sync_txnid = nullptr; if (env->me_lfd != INVALID_HANDLE_VALUE) { (void)mdbx_closefile(env->me_lfd); diff --git a/src/elements/internals.h b/src/elements/internals.h index bad5c694..a7814153 100644 --- a/src/elements/internals.h +++ b/src/elements/internals.h @@ -546,12 +546,10 @@ typedef struct MDBX_lockinfo { volatile txnid_t mti_oldest_reader; - /* Timestamp for auto-sync feature, i.e. the steady checkpoint should be - * created at the first commit that will be not early this timestamp. - * The time value is represented in a suitable system-dependent form, for - * example clock_gettime(CLOCK_BOOTTIME) or clock_gettime(CLOCK_MONOTONIC). - * Zero means timed auto-sync is not pending. */ - volatile uint64_t mti_unsynced_timeout; + /* Timestamp of the last steady sync. Value is represented in a suitable + * system-dependent form, for example clock_gettime(CLOCK_BOOTTIME) or + * clock_gettime(CLOCK_MONOTONIC). */ + volatile uint64_t mti_sync_timestamp; /* Number un-synced-with-disk pages for auto-sync feature. */ volatile pgno_t mti_unsynced_pages; @@ -935,7 +933,7 @@ struct MDBX_env { unsigned me_maxkey_limit; /* max size of a key */ uint32_t me_live_reader; /* have liveness lock in reader table */ void *me_userctx; /* User-settable context */ - volatile uint64_t *me_unsynced_timeout; + volatile uint64_t *me_sync_timestamp; volatile uint64_t *me_autosync_period; volatile pgno_t *me_unsynced_pages; volatile pgno_t *me_autosync_threshold; @@ -947,7 +945,7 @@ struct MDBX_env { MDBX_OSAL_LOCK wmutex; #endif txnid_t oldest; - uint64_t unsynced_timeout; + uint64_t sync_timestamp; uint64_t autosync_period; pgno_t autosync_pending; pgno_t autosync_threshold;