From 14ddec2051b8778f1765a6c42d24ab5914bcb929 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Sun, 13 Mar 2016 18:48:21 +0300 Subject: [PATCH] mdbx: refine mdb_env_sync(). Change-Id: I37e6079a94a20624e65aed3e2e599bb4490afcc0 --- mdb.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/mdb.c b/mdb.c index b2125195..46d07823 100644 --- a/mdb.c +++ b/mdb.c @@ -2588,31 +2588,28 @@ mdb_env_sync(MDB_env *env, int force) return EACCES; head = mdb_meta_head_r(env); - if (force || head->mm_mapsize != env->me_mapsize) - flags &= MDB_WRITEMAP; - - /* LY: just only for 'early sync' to reduce writer latency */ - if (env->me_sync_threshold && env->me_sync_pending >= env->me_sync_threshold) - flags &= MDB_WRITEMAP; - - if ((flags & MDB_NOSYNC) == 0) { - /* LY: early sync before acquiring the mutex, - * this reduces latency for writer */ - if (flags & MDB_WRITEMAP) { - if (msync(env->me_map, env->me_mapsize, - (flags & MDB_MAPASYNC) ? MS_ASYNC : MS_SYNC)) - return errno; - } else if (fdatasync(env->me_fd)) - return errno; - /* LY: head may be changed during the sync. */ - head = mdb_meta_head_r(env); - } - if (! META_IS_WEAK(head) && env->me_sync_pending == 0 && env->me_mapsize == head->mm_mapsize) /* LY: nothing to do */ return MDB_SUCCESS; + if (force || head->mm_mapsize != env->me_mapsize + || (env->me_sync_threshold && env->me_sync_pending >= env->me_sync_threshold)) + flags &= MDB_WRITEMAP; + + /* LY: early sync before acquiring the mutex to reduce writer's latency */ + if (env->me_sync_pending > env->me_psize * 16 && (flags & MDB_NOSYNC) == 0) { + if (flags & MDB_WRITEMAP) { + size_t used_size = env->me_psize * (head->mm_last_pg + 1); + rc = msync(env->me_map, used_size, + (flags & MDB_MAPASYNC) ? MS_ASYNC : MS_SYNC); + } else { + rc = fdatasync(env->me_fd); + } + if (unlikely(rc)) + return errno; + } + mutex = MDB_MUTEX(env, w); rc = mdb_mutex_lock(env, mutex); if (unlikely(rc))