mdbx: Merge branch 'master' into devel.

Change-Id: Ic130cd181097332aa2f49019d75403e18d8cba0d
This commit is contained in:
Leonid Yuriev
2020-09-25 01:13:11 +03:00
26 changed files with 161 additions and 155 deletions

View File

@@ -319,7 +319,7 @@ node_largedata_pgno(const MDBX_node *const __restrict node) {
* leaf-page, since dupsort value couldn't be placed on a large/overflow
* page.
*
* - So, the simpliest solution is to use half of branch.maxkey as
* - So, the simplest solution is to use half of branch.maxkey as
* a common maxkey value. Nevertheless, the actual values of maxkey are:
* nondupsort.maxkey = even_floor(pageroom / 3)
* - sizeof(indx_t) - node_hdr_len;
@@ -415,7 +415,8 @@ MDBX_NOTHROW_PURE_FUNCTION static __always_inline size_t
leaf_size(const MDBX_env *env, const MDBX_val *key, const MDBX_val *data) {
size_t node_bytes = node_size(key, data);
/* NOTE: The actual limit is LEAF_NODEMAX(env->me_psize), but it reasonable to
* use env->me_branch_nodemax (which is 3 times less) as the treshold because:
* use env->me_branch_nodemax (which is 3 times less) as the threshold
* because:
* - Large threshold implies that any insertion/update could result split
* a single leaf page to THREE, which requires TWO insertion into parent
* branch page, then could leads to split parent page and so on up to
@@ -1016,7 +1017,7 @@ static void __cold workaround_glibc_bug21031(void) {
/* Workaround for https://sourceware.org/bugzilla/show_bug.cgi?id=21031
*
* Due race between pthread_key_delete() and __nptl_deallocate_tsd()
* The destructor(s) of thread-local-storate object(s) may be running
* The destructor(s) of thread-local-storage object(s) may be running
* in another thread(s) and be blocked or not finished yet.
* In such case we get a SEGFAULT after unload this library DSO.
*
@@ -1084,11 +1085,11 @@ static void thread_rthc_set(mdbx_thread_key_t key, const void *value) {
mdbx_ensure(nullptr, TlsSetValue(key, (void *)value));
#else
#define MDBX_THREAD_RTHC_ZERO 0
#define MDBX_THREAD_RTHC_REGISTERD 1
#define MDBX_THREAD_RTHC_REGISTERED 1
#define MDBX_THREAD_RTHC_COUNTED 2
static __thread uint32_t thread_registration_state;
if (value && unlikely(thread_registration_state == MDBX_THREAD_RTHC_ZERO)) {
thread_registration_state = MDBX_THREAD_RTHC_REGISTERD;
thread_registration_state = MDBX_THREAD_RTHC_REGISTERED;
mdbx_trace("thread registered 0x%" PRIxPTR, mdbx_thread_self());
if (&__cxa_thread_atexit_impl == nullptr ||
__cxa_thread_atexit_impl(mdbx_rthc_thread_dtor,
@@ -4315,10 +4316,10 @@ static __always_inline bool meta_bootid_match(const MDBX_meta *meta) {
}
static bool meta_weak_acceptable(const MDBX_env *env, const MDBX_meta *meta,
const int lck_exlusive) {
return lck_exlusive ? /* exclusive lock */ meta_bootid_match(meta)
: /* db already opened */ env->me_lck &&
(env->me_lck->mti_envmode & MDBX_RDONLY) == 0;
const int lck_exclusive) {
return lck_exclusive ? /* exclusive lock */ meta_bootid_match(meta)
: /* db already opened */ env->me_lck &&
(env->me_lck->mti_envmode & MDBX_RDONLY) == 0;
}
#define METAPAGE(env, n) page_meta(pgno2page(env, n))
@@ -4731,7 +4732,7 @@ static __cold int mdbx_mapresize(MDBX_env *env, const pgno_t used_pgno,
goto bailout;
/* 1) Windows allows only extending a read-write section, but not a
* corresponing mapped view. Therefore in other cases we must suspend
* corresponding mapped view. Therefore in other cases we must suspend
* the local threads for safe remap.
* 2) At least on Windows 10 1803 the entire mapped section is unavailable
* for short time during NtExtendSection() or VirtualAlloc() execution.
@@ -5348,7 +5349,7 @@ skip_cache:
((autosync_threshold | autosync_period) == 0 ||
next >= steady->mm_geo.now)) {
/* wipe steady checkpoint in MDBX_UTTERLY_NOSYNC mode
* without any auto-sync treshold(s). */
* without any auto-sync threshold(s). */
rc = mdbx_wipe_steady(env, oldest);
mdbx_debug("gc-wipe-steady, rc %d", rc);
mdbx_assert(env, steady != mdbx_meta_steady(env));
@@ -6523,7 +6524,7 @@ int mdbx_txn_begin(MDBX_env *env, MDBX_txn *parent, MDBX_txn_flags_t flags,
MDBX_PNL_SIZEOF(parent->tw.reclaimed_pglist));
mdbx_assert(env, mdbx_pnl_check4assert(
txn->tw.reclaimed_pglist,
(txn->mt_next_pgno /* LY: intentional assigment here,
(txn->mt_next_pgno /* LY: intentional assignment here,
only for assertion */
= parent->mt_next_pgno)));
@@ -7394,7 +7395,7 @@ retry_noaccount:
continue;
}
/* handle reclaimed and loost pages - merge and store both into gc */
/* handle reclaimed and lost pages - merge and store both into gc */
mdbx_tassert(txn, mdbx_pnl_check4assert(txn->tw.reclaimed_pglist,
txn->mt_next_pgno));
mdbx_tassert(txn, txn->tw.loose_count == 0);
@@ -8685,7 +8686,7 @@ static MDBX_page *__cold mdbx_meta_model(const MDBX_env *env, MDBX_page *model,
}
/* Fill in most of the zeroed meta-pages for an empty database environment.
* Return pointer to recenly (head) meta-page. */
* Return pointer to recently (head) meta-page. */
static MDBX_meta *__cold mdbx_init_metas(const MDBX_env *env, void *buffer) {
MDBX_page *page0 = (MDBX_page *)buffer;
MDBX_page *page1 = mdbx_meta_model(env, page0, 0);
@@ -8742,20 +8743,20 @@ static int mdbx_sync_locked(MDBX_env *env, unsigned flags,
#if defined(MADV_DONTNEED)
const size_t largest_bytes = pgno2bytes(env, largest_pgno);
/* threshold to avoid unreasonable frequent madvise() calls */
const size_t madvise_treshold = (largest_bytes < 65536 * 256)
? 65536
: (largest_bytes > MEGABYTE * 4 * 256)
? MEGABYTE * 4
: largest_bytes >> 10;
const size_t madvise_threshold = (largest_bytes < 65536 * 256)
? 65536
: (largest_bytes > MEGABYTE * 4 * 256)
? MEGABYTE * 4
: largest_bytes >> 10;
const size_t discard_edge_bytes = bytes_align2os_bytes(
env, ((MDBX_RDONLY &
(env->me_lck ? env->me_lck->mti_envmode : env->me_flags))
? largest_bytes
: largest_bytes + madvise_treshold));
: largest_bytes + madvise_threshold));
const pgno_t discard_edge_pgno = bytes2pgno(env, discard_edge_bytes);
const pgno_t prev_discarded_pgno = *env->me_discarded_tail;
if (prev_discarded_pgno >=
discard_edge_pgno + bytes2pgno(env, madvise_treshold)) {
discard_edge_pgno + bytes2pgno(env, madvise_threshold)) {
mdbx_notice("open-MADV_%s %u..%u", "DONTNEED", *env->me_discarded_tail,
largest_pgno);
*env->me_discarded_tail = discard_edge_pgno;
@@ -9806,7 +9807,7 @@ static int __cold mdbx_setup_dxb(MDBX_env *env, const int lck_rc) {
}
if (!env->me_lck) {
/* LY: without-lck (read-only) mode, so it is imposible that other
/* LY: without-lck (read-only) mode, so it is impossible that other
* process made weak checkpoint. */
mdbx_error("%s", "without-lck, unable recovery/rollback");
return MDBX_WANNA_RECOVERY;
@@ -10077,7 +10078,7 @@ static int __cold mdbx_setup_lck(MDBX_env *env, char *lck_pathname,
struct MDBX_lockinfo *const lck = env->me_lck;
if (lck_seize_rc == MDBX_RESULT_TRUE) {
/* LY: exlcusive mode, check and reset lck content */
/* LY: exclusive mode, check and reset lck content */
memset(lck, 0, (size_t)size);
mdbx_jitter4testing(false);
lck->mti_magic_and_version = MDBX_LOCK_MAGIC;
@@ -10608,7 +10609,7 @@ __cold int mdbx_env_open(MDBX_env *env, const char *pathname,
sizeof(unsigned) + 1);
rc = mdbx_memalign_alloc(
env->me_os_psize,
env->me_psize * (1 /* page buffer */ + 1 /* page killer bufer */),
env->me_psize * (1 /* page buffer */ + 1 /* page killer buffer */),
&env->me_pbuf);
if (rc == MDBX_SUCCESS) {
memset(env->me_pbuf, -1, env->me_psize * 2);
@@ -16227,7 +16228,7 @@ static int __cold mdbx_env_compact(MDBX_env *env, MDBX_txn *read_txn,
if (flags & MDBX_CP_FORCE_DYNAMIC_SIZE)
make_sizeable(meta);
/* copy canary sequenses if present */
/* copy canary sequences if present */
if (read_txn->mt_canary.v) {
meta->mm_canary = read_txn->mt_canary;
meta->mm_canary.v = mdbx_meta_txnid_stable(env, meta);
@@ -16341,7 +16342,7 @@ static int __cold mdbx_env_compact(MDBX_env *env, MDBX_txn *read_txn,
for (size_t offset = used_size; offset < whole_size;) {
const size_t chunk =
(MDBX_WBUF < whole_size - offset) ? MDBX_WBUF : whole_size - offset;
/* copy to avoit EFAULT in case swapped-out */
/* copy to avoid EFAULT in case swapped-out */
int rc = mdbx_write(fd, data_buffer, chunk);
if (unlikely(rc != MDBX_SUCCESS))
return rc;
@@ -16376,7 +16377,7 @@ static int __cold mdbx_env_copy_asis(MDBX_env *env, MDBX_txn *read_txn,
/* Make a snapshot of meta-pages,
* but writing ones after the data was flushed */
memcpy(buffer, env->me_map, meta_bytes);
MDBX_meta *const headcopy = /* LY: get pointer to the spanshot copy */
MDBX_meta *const headcopy = /* LY: get pointer to the snapshot copy */
(MDBX_meta *)(buffer + ((uint8_t *)mdbx_meta_head(env) - env->me_map));
mdbx_txn_unlock(env);
@@ -16433,7 +16434,7 @@ static int __cold mdbx_env_copy_asis(MDBX_env *env, MDBX_txn *read_txn,
/* fallback to portable */
const size_t chunk =
(MDBX_WBUF < used_size - offset) ? MDBX_WBUF : used_size - offset;
/* copy to avoit EFAULT in case swapped-out */
/* copy to avoid EFAULT in case swapped-out */
memcpy(data_buffer, env->me_map + offset, chunk);
rc = mdbx_write(fd, data_buffer, chunk);
offset += chunk;
@@ -16449,7 +16450,7 @@ static int __cold mdbx_env_copy_asis(MDBX_env *env, MDBX_txn *read_txn,
rc == MDBX_SUCCESS && offset < whole_size;) {
const size_t chunk =
(MDBX_WBUF < whole_size - offset) ? MDBX_WBUF : whole_size - offset;
/* copy to avoit EFAULT in case swapped-out */
/* copy to avoid EFAULT in case swapped-out */
rc = mdbx_write(fd, data_buffer, chunk);
offset += chunk;
}

View File

@@ -84,7 +84,7 @@
#pragma warning(disable : 4127) /* conditional expression is constant */
#pragma warning(disable : 4324) /* 'xyz': structure was padded due to alignment specifier */
#pragma warning(disable : 4310) /* cast truncates constant value */
#pragma warning(disable : 4820) /* bytes padding added after data member for aligment */
#pragma warning(disable : 4820) /* bytes padding added after data member for alignment */
#pragma warning(disable : 4548) /* expression before comma has no effect; expected expression with side - effect */
#pragma warning(disable : 4366) /* the result of the unary '&' operator may be unaligned */
#pragma warning(disable : 4200) /* nonstandard extension used: zero-sized array in struct/union */
@@ -101,7 +101,7 @@
#include "defs.h"
#if defined(__GNUC__) && !__GNUC_PREREQ(4,2)
/* Actualy libmdbx was not tested with compilers older than GCC from RHEL6.
/* Actually libmdbx was not tested with compilers older than GCC from RHEL6.
* But you could remove this #error and try to continue at your own risk.
* In such case please don't rise up an issues related ONLY to old compilers.
*/
@@ -109,7 +109,7 @@
#endif
#if defined(__clang__) && !__CLANG_PREREQ(3,8)
/* Actualy libmdbx was not tested with CLANG older than 3.8.
/* Actually libmdbx was not tested with CLANG older than 3.8.
* But you could remove this #error and try to continue at your own risk.
* In such case please don't rise up an issues related ONLY to old compilers.
*/
@@ -117,7 +117,7 @@
#endif
#if defined(__GLIBC__) && !__GLIBC_PREREQ(2,12)
/* Actualy libmdbx was not tested with something older than glibc 2.12 (from RHEL6).
/* Actually libmdbx was not tested with something older than glibc 2.12 (from RHEL6).
* But you could remove this #error and try to continue at your own risk.
* In such case please don't rise up an issues related ONLY to old systems.
*/
@@ -536,7 +536,7 @@ typedef struct MDBX_lockinfo {
alignas(MDBX_CACHELINE_SIZE) /* cacheline ---------------------------------*/
/* Write transation lock. */
/* Write transaction lock. */
#if MDBX_LOCKING > 0
mdbx_ipclock_t mti_wlock;
#endif /* MDBX_LOCKING > 0 */
@@ -884,7 +884,7 @@ struct MDBX_cursor {
#define C_RECLAIMING 0x20 /* GC lookup is prohibited */
#define C_GCFREEZE 0x40 /* reclaimed_pglist must not be updated */
/* Cursor checing flags. */
/* Cursor checking flags. */
#define C_COPYING 0x100 /* skip key-value length check (copying simplify) */
#define C_UPDATING 0x200 /* update/rebalance pending */
#define C_RETIRING 0x400 /* refs to child pages may be invalid */

View File

@@ -183,7 +183,8 @@ MDBX_INTERNAL_FUNC int mdbx_rdt_lock(MDBX_env *env) {
if (env->me_lfd == INVALID_HANDLE_VALUE)
return MDBX_SUCCESS; /* readonly database in readonly filesystem */
/* transite from S-? (used) to S-E (locked), e.g. exclusive lock upper-part */
/* transition from S-? (used) to S-E (locked),
* e.g. exclusive lock upper-part */
if ((env->me_flags & MDBX_EXCLUSIVE) ||
flock(env->me_lfd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_UPPER))
return MDBX_SUCCESS;
@@ -195,7 +196,7 @@ MDBX_INTERNAL_FUNC int mdbx_rdt_lock(MDBX_env *env) {
MDBX_INTERNAL_FUNC void mdbx_rdt_unlock(MDBX_env *env) {
if (env->me_lfd != INVALID_HANDLE_VALUE) {
/* transite from S-E (locked) to S-? (used), e.g. unlock upper-part */
/* transition from S-E (locked) to S-? (used), e.g. unlock upper-part */
if ((env->me_flags & MDBX_EXCLUSIVE) == 0 &&
!funlock(env->me_lfd, LCK_UPPER))
mdbx_panic("%s failed: err %u", __func__, GetLastError());
@@ -277,7 +278,7 @@ mdbx_suspend_threads_before_remap(MDBX_env *env, mdbx_handle_array_t **array) {
}
} else {
/* Without LCK (i.e. read-only mode).
* Walk thougth a snapshot of all running threads */
* Walk through a snapshot of all running threads */
mdbx_assert(env,
env->me_txn0 == NULL || (env->me_flags & MDBX_EXCLUSIVE) != 0);
const HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
@@ -336,7 +337,7 @@ mdbx_resume_threads_after_remap(mdbx_handle_array_t *array) {
/* global `initial` lock for lockfile initialization,
* exclusive/shared locking first cacheline */
/* Briefly descritpion of locking schema/algorithm:
/* Briefly description of locking schema/algorithm:
* - Windows does not support upgrading or downgrading for file locking.
* - Therefore upgrading/downgrading is emulated by shared and exclusive
* locking of upper and lower halves.
@@ -411,7 +412,7 @@ static void lck_unlock(MDBX_env *env) {
/* Seize state as 'exclusive-write' (E-E and returns MDBX_RESULT_TRUE)
* or as 'used' (S-? and returns MDBX_RESULT_FALSE).
* Oherwise returns an error. */
* Otherwise returns an error. */
static int internal_seize_lck(HANDLE lfd) {
int rc;
assert(lfd != INVALID_HANDLE_VALUE);
@@ -450,7 +451,7 @@ static int internal_seize_lck(HANDLE lfd) {
mdbx_error("%s, err %u", "?-E(middle) >> S-E(locked)", rc);
/* 8) now on S-E (locked) or still on ?-E (middle),
* transite to S-? (used) or ?-? (free) */
* transition to S-? (used) or ?-? (free) */
if (!funlock(lfd, LCK_UPPER))
mdbx_panic("%s(%s) failed: err %u", __func__,
"X-E(locked/middle) >> X-?(used/free)", GetLastError());
@@ -512,12 +513,12 @@ MDBX_INTERNAL_FUNC int mdbx_lck_downgrade(MDBX_env *env) {
if (env->me_flags & MDBX_EXCLUSIVE)
return MDBX_SUCCESS /* nope since files were must be opened non-shareable */
;
/* 1) now at E-E (exclusive-write), transite to ?_E (middle) */
/* 1) now at E-E (exclusive-write), transition to ?_E (middle) */
if (!funlock(env->me_lfd, LCK_LOWER))
mdbx_panic("%s(%s) failed: err %u", __func__,
"E-E(exclusive-write) >> ?-E(middle)", GetLastError());
/* 2) now at ?-E (middle), transite to S-E (locked) */
/* 2) now at ?-E (middle), transition to S-E (locked) */
if (!flock(env->me_lfd, LCK_SHARED | LCK_DONTWAIT, LCK_LOWER)) {
int rc = GetLastError() /* 3) something went wrong, give up */;
mdbx_error("%s, err %u", "?-E(middle) >> S-E(locked)", rc);
@@ -549,7 +550,7 @@ MDBX_INTERNAL_FUNC int mdbx_lck_upgrade(MDBX_env *env) {
return rc;
}
/* 3) now on S-E (locked), transite to ?-E (middle) */
/* 3) now on S-E (locked), transition to ?-E (middle) */
if (!funlock(env->me_lfd, LCK_LOWER))
mdbx_panic("%s(%s) failed: err %u", __func__, "S-E(locked) >> ?-E(middle)",
GetLastError());
@@ -666,7 +667,7 @@ static void WINAPI stub_srwlock_AcquireShared(MDBX_srwlock *srwl) {
// Add to the readers list
_InterlockedIncrement(&srwl->readerCount);
// Check for writers again (we may have been pre-empted). If
// Check for writers again (we may have been preempted). If
// there are no writers writing or waiting, then we're done.
if (srwl->writerCount == 0)
break;

View File

@@ -478,7 +478,7 @@ static int handle_freedb(const uint64_t record_number, const MDBX_val *key,
(number + 1) * sizeof(pgno_t), data->iov_len);
number = data->iov_len / sizeof(pgno_t) - 1;
} else if (data->iov_len - (number + 1) * sizeof(pgno_t) >=
/* LY: allow gap upto one page. it is ok
/* LY: allow gap up to one page. it is ok
* and better than shink-and-retry inside mdbx_update_gc() */
envstat.ms_psize)
problem_add("entry", txnid, "extra idl space",
@@ -1318,7 +1318,7 @@ int main(int argc, char *argv[]) {
alloc_pages = backed_pages;
}
} else {
/* LY: DB may be shrinked by writer downto the allocated pages. */
/* LY: DB may be shrinked by writer down to the allocated pages. */
if (alloc_pages > backed_pages) {
print(" ! alloc-pages %" PRIu64 " > backed-pages %" PRIu64 "\n",
alloc_pages, backed_pages);

View File

@@ -224,7 +224,7 @@ static void usage(void) {
" -a\t\tdump main DB and all subDBs,\n"
" \t\tby default dump only the main DB\n"
" -s\t\tdump only the named subDB\n"
" -r\t\trescure mode (ignore errors to dump corrupted DB)\n",
" -r\t\trescue mode (ignore errors to dump corrupted DB)\n",
prog);
exit(EXIT_FAILURE);
}

View File

@@ -459,7 +459,7 @@ static void usage(void) {
" -s name\tload into named subDB\n"
" -N\t\tuse NOOVERWRITE on puts\n"
" -T\t\tread plaintext\n"
" -r\t\trescure mode (ignore errors to load corrupted DB dump)\n",
" -r\t\trescue mode (ignore errors to load corrupted DB dump)\n",
prog);
exit(EXIT_FAILURE);
}

View File

@@ -60,7 +60,7 @@
/** Controls checking PID against reuse DB environment after the fork() */
#ifndef MDBX_ENV_CHECKPID
#if defined(MADV_DONTFORK) || defined(_WIN32) || defined(_WIN64)
/* PID check could be ommited:
/* PID check could be omitted:
* - on Linux when madvise(MADV_DONTFORK) is available. i.e. after the fork()
* mapped pages will not be available for child process.
* - in Windows where fork() not available. */

View File

@@ -1387,7 +1387,7 @@ MDBX_INTERNAL_FUNC int mdbx_mmap(const int flags, mdbx_mmap_t *map,
MDBX_INTERNAL_FUNC int mdbx_munmap(mdbx_mmap_t *map) {
VALGRIND_MAKE_MEM_NOACCESS(map->address, map->current);
/* Unpoisoning is required for ASAN to avoid false-positive diagnostic
* when this memory will re-used by malloc or another mmaping.
* when this memory will re-used by malloc or another mmapping.
* See https://github.com/erthink/libmdbx/pull/93#issuecomment-613687203 */
ASAN_UNPOISON_MEMORY_REGION(map->address, map->limit);
#if defined(_WIN32) || defined(_WIN64)
@@ -1673,7 +1673,7 @@ retry_mapview:;
if (unlikely(ptr == MAP_FAILED)) {
VALGRIND_MAKE_MEM_NOACCESS(map->address, map->current);
/* Unpoisoning is required for ASAN to avoid false-positive diagnostic
* when this memory will re-used by malloc or another mmaping.
* when this memory will re-used by malloc or another mmapping.
* See https://github.com/erthink/libmdbx/pull/93#issuecomment-613687203
*/
ASAN_UNPOISON_MEMORY_REGION(map->address, map->limit);
@@ -1691,7 +1691,7 @@ retry_mapview:;
if (map->address != ptr) {
VALGRIND_MAKE_MEM_NOACCESS(map->address, map->current);
/* Unpoisoning is required for ASAN to avoid false-positive diagnostic
* when this memory will re-used by malloc or another mmaping.
* when this memory will re-used by malloc or another mmapping.
* See https://github.com/erthink/libmdbx/pull/93#issuecomment-613687203
*/
ASAN_UNPOISON_MEMORY_REGION(map->address, map->limit);

View File

@@ -741,12 +741,12 @@ MDBX_INTERNAL_FUNC int mdbx_lck_destroy(MDBX_env *env,
MDBX_INTERNAL_FUNC int mdbx_lck_seize(MDBX_env *env);
/// \brief Downgrades the level of initially acquired lock to
/// operational level specified by agrument. The reson for such downgrade:
/// operational level specified by argument. The reson for such downgrade:
/// - unblocking of other processes that are waiting for access, i.e.
/// if (env->me_flags & MDBX_EXCLUSIVE) != 0, then other processes
/// should be made aware that access is unavailable rather than
/// wait for it.
/// - freeing locks that interfere file operation (expecially for Windows)
/// - freeing locks that interfere file operation (especially for Windows)
/// (env->me_flags & MDBX_EXCLUSIVE) == 0 - downgrade to shared lock.
/// (env->me_flags & MDBX_EXCLUSIVE) != 0 - downgrade to exclusive
/// operational lock.