mdbx: refine usage of posix_fadvise()madvise()/posix_madvise()/F_RDADVISE.

This commit is contained in:
Leonid Yuriev 2019-08-27 21:35:43 +03:00
parent 8329f5b6a1
commit fd0c92927a
2 changed files with 90 additions and 15 deletions

View File

@ -5730,14 +5730,15 @@ bailout:
return rc; return rc;
} }
static int __cold mdbx_env_map(MDBX_env *env, size_t usedsize) { static int __cold mdbx_env_map(MDBX_env *env, const int is_exclusive,
const size_t usedsize) {
int rc = mdbx_mmap(env->me_flags, &env->me_dxb_mmap, env->me_dbgeo.now, int rc = mdbx_mmap(env->me_flags, &env->me_dxb_mmap, env->me_dbgeo.now,
env->me_dbgeo.upper); env->me_dbgeo.upper);
if (unlikely(rc != MDBX_SUCCESS)) if (unlikely(rc != MDBX_SUCCESS))
return rc; return rc;
#ifdef MADV_DONTFORK #ifdef MADV_DONTFORK
if (madvise(env->me_map, env->me_mapsize, MADV_DONTFORK)) if (unlikely(madvise(env->me_map, env->me_mapsize, MADV_DONTFORK) != 0))
return errno; return errno;
#endif #endif
@ -5753,22 +5754,73 @@ static int __cold mdbx_env_map(MDBX_env *env, size_t usedsize) {
MADV_DONTDUMP); MADV_DONTDUMP);
#endif #endif
#ifdef MADV_REMOVE if (is_exclusive && (env->me_flags & MDBX_WRITEMAP) != 0) {
if (usedsize && (env->me_flags & MDBX_WRITEMAP)) { #ifdef MADV_REMOVE_OR_FREE
(void)madvise(env->me_map + usedsize, env->me_mapsize - usedsize, const size_t used_alined2os = mdbx_roundup2(usedsize, env->me_os_psize);
MADV_REMOVE); if (used_alined2os < env->me_mapsize)
} (void)madvise(env->me_map + used_alined2os,
env->me_mapsize - used_alined2os, MADV_REMOVE_OR_FREE);
#else #else
(void)usedsize; (void)usedsize;
#endif #endif
}
#if defined(MADV_RANDOM) && defined(MADV_WILLNEED) #ifdef POSIX_FADV_RANDOM
/* Turn on/off readahead. It's harmful when the DB is larger than RAM. */ /* this also checks that the file size is valid for a particular FS */
if (madvise(env->me_map, env->me_mapsize, rc = posix_fadvise(env->me_fd, 0, env->me_dbgeo.upper, POSIX_FADV_RANDOM);
(env->me_flags & MDBX_NORDAHEAD) ? MADV_RANDOM : MADV_WILLNEED)) if (unlikely(rc != 0))
return rc;
#elif defined(F_RDAHEAD)
if (unlikely(fcntl(env->me_fd, F_RDAHEAD, 0) == -1))
return errno; return errno;
#endif #endif
#if defined(MADV_RANDOM)
if (unlikely(madvise(env->me_map, env->me_mapsize, MADV_RANDOM) != 0))
return errno;
#elif defined(POSIX_MADV_RANDOM)
rc = posix_madvise(env->me_map, env->me_mapsize, POSIX_MADV_RANDOM);
if (unlikely(rc != 0))
return errno;
#endif
/* Turn on/off readahead. It's harmful when the DB is larger than RAM. */
if (env->me_flags & MDBX_NORDAHEAD) {
#ifdef POSIX_FADV_DONTNEED
rc = posix_fadvise(env->me_fd, 0, env->me_mapsize, POSIX_FADV_DONTNEED);
if (unlikely(rc != 0))
return rc;
#endif
#if defined(MADV_DONTNEED)
if (unlikely(madvise(env->me_map, env->me_mapsize, MADV_DONTNEED) != 0))
return errno;
#elif defined(POSIX_MADV_DONTNEED)
rc = posix_madvise(env->me_map, env->me_mapsize, POSIX_MADV_DONTNEED);
if (unlikely(rc != 0))
return errno;
#endif
} else {
#ifdef POSIX_FADV_WILLNEED
rc = posix_fadvise(env->me_fd, 0, usedsize, POSIX_FADV_WILLNEED);
if (unlikely(rc != 0))
return rc;
#elif defined(F_RDADVISE)
struct radvisory hint;
hint.ra_offset = 0;
hint.ra_count = usedsize;
if (unlikely(fcntl(env->me_fd, F_RDADVISE, &hint) == -1))
return errno;
#endif
#if defined(MADV_WILLNEED)
if (unlikely(madvise(env->me_map, usedsize, MADV_WILLNEED) != 0))
return errno;
#elif defined(POSIX_MADV_WILLNEED)
rc = posix_madvise(env->me_map, usedsize, POSIX_MADV_WILLNEED);
if (unlikely(rc != 0))
return errno;
#endif
}
#ifdef USE_VALGRIND #ifdef USE_VALGRIND
env->me_valgrind_handle = env->me_valgrind_handle =
VALGRIND_CREATE_BLOCK(env->me_map, env->me_mapsize, "mdbx"); VALGRIND_CREATE_BLOCK(env->me_map, env->me_mapsize, "mdbx");
@ -5968,6 +6020,15 @@ mdbx_env_set_geometry(MDBX_env *env, intptr_t size_lower, intptr_t size_now,
if (bytes2pgno(env, shrink_threshold) > UINT16_MAX) if (bytes2pgno(env, shrink_threshold) > UINT16_MAX)
shrink_threshold = pgno2bytes(env, UINT16_MAX); shrink_threshold = pgno2bytes(env, UINT16_MAX);
#ifdef POSIX_FADV_RANDOM
if (env->me_fd != INVALID_HANDLE_VALUE) {
/* this also checks that the file size is valid for a particular FS */
rc = posix_fadvise(env->me_fd, 0, env->me_dbgeo.upper, POSIX_FADV_RANDOM);
if (unlikely(rc != 0))
goto bailout;
}
#endif
/* save user's geo-params for future open/create */ /* save user's geo-params for future open/create */
env->me_dbgeo.lower = size_lower; env->me_dbgeo.lower = size_lower;
env->me_dbgeo.now = size_now; env->me_dbgeo.now = size_now;
@ -6269,9 +6330,7 @@ static int __cold mdbx_setup_dxb(MDBX_env *env, const int lck_rc) {
} }
} }
err = mdbx_env_map(env, (lck_rc != /* lck exclusive */ MDBX_RESULT_TRUE) err = mdbx_env_map(env, lck_rc /* exclusive status */, expected_bytes);
? 0
: expected_bytes);
if (err != MDBX_SUCCESS) if (err != MDBX_SUCCESS)
return err; return err;

View File

@ -192,6 +192,22 @@ typedef pthread_mutex_t mdbx_fastmutex_t;
#define SSIZE_MAX INTPTR_MAX #define SSIZE_MAX INTPTR_MAX
#endif #endif
#if !defined(MADV_DODUMP) && defined(MADV_CORE)
#define MADV_DODUMP MADV_CORE
#endif /* MADV_CORE -> MADV_DODUMP */
#if !defined(MADV_DONTDUMP) && defined(MADV_NOCORE)
#define MADV_DONTDUMP MADV_NOCORE
#endif /* MADV_NOCORE -> MADV_DONTDUMP */
#ifndef MADV_REMOVE_OR_FREE
#ifdef MADV_REMOVE
#define MADV_REMOVE_OR_FREE MADV_REMOVE
#elif defined(MADV_FREE)
#define MADV_REMOVE_OR_FREE MADV_FREE
#endif
#endif /* MADV_REMOVE_OR_FREE */
#if defined(i386) || defined(__386) || defined(__i386) || defined(__i386__) || \ #if defined(i386) || defined(__386) || defined(__i386) || defined(__i386__) || \
defined(i486) || defined(__i486) || defined(__i486__) || \ defined(i486) || defined(__i486) || defined(__i486__) || \
defined(i586) | defined(__i586) || defined(__i586__) || defined(i686) || \ defined(i586) | defined(__i586) || defined(__i586__) || defined(i686) || \