Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Apr 16, 2024
1 parent a997047 commit c3fb4f6
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 120 deletions.
122 changes: 66 additions & 56 deletions mdbx/mdbx.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* <http://www.OpenLDAP.org/license.html>. */

#define xMDBX_ALLOY 1
#define MDBX_BUILD_SOURCERY 7e1fc281eda601813f191bf4e230e0cd439df56e9fbe6b0cbfee716c27a838cf_v0_13_0_41_g0da3c87a
#define MDBX_BUILD_SOURCERY 130871cb18e12708d402f13187a640da9a3c724f55afabe00d209bb33e3dc647_v0_13_0_43_g661372d6
#ifdef MDBX_CONFIG_H
#include MDBX_CONFIG_H
#endif
Expand Down Expand Up @@ -12310,7 +12310,7 @@ next_gc:;
no_gc:
eASSERT(env, pgno == 0);
#ifndef MDBX_ENABLE_BACKLOG_DEPLETED
#define MDBX_ENABLE_BACKLOG_DEPLETED 1
#define MDBX_ENABLE_BACKLOG_DEPLETED 0
#endif /* MDBX_ENABLE_BACKLOG_DEPLETED*/
if (MDBX_ENABLE_BACKLOG_DEPLETED &&
unlikely(!(txn->mt_flags & MDBX_TXN_DRAINED_GC))) {
Expand Down Expand Up @@ -14732,7 +14732,7 @@ __cold static int audit_ex(MDBX_txn *txn, size_t retired_stored,
typedef struct gc_update_context {
size_t loop, reserve_adj;
size_t retired_stored;
size_t reserved, cleaned_slot, reused_slot, fill_idx;
size_t amount, reserved, cleaned_slot, reused_slot, fill_idx;
txnid_t cleaned_id, rid;
bool lifo, dense;
#if MDBX_ENABLE_BIGFOOT
Expand Down Expand Up @@ -14914,13 +14914,17 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
ctx->cursor.mc_next = txn->mt_cursors[FREE_DBI];
txn->mt_cursors[FREE_DBI] = &ctx->cursor;

pgno_t prev_next_pgno = 0;
/* txn->tw.relist[] can grow and shrink during this call.
* txn->tw.last_reclaimed and txn->tw.retired_pages[] can only grow.
* But page numbers cannot disappear from txn->tw.retired_pages[]. */
retry_clean_adj:
ctx->reserve_adj = 0;
retry:
if (ctx->loop++)
ctx->loop += prev_next_pgno == txn->mt_next_pgno;
prev_next_pgno = txn->mt_next_pgno;

if (ctx->loop)
TRACE("%s", " >> restart");
int rc = MDBX_SUCCESS;
tASSERT(txn, pnl_check_allocated(txn->tw.relist,
Expand All @@ -14941,7 +14945,7 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
ctx->reserved = 0;
ctx->cleaned_slot = 0;
ctx->reused_slot = 0;
ctx->fill_idx = ~0u;
ctx->amount = ctx->fill_idx = ~0u;
ctx->cleaned_id = 0;
ctx->rid = txn->tw.last_reclaimed;
while (true) {
Expand Down Expand Up @@ -15010,9 +15014,11 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
rc = MDBX_CORRUPTED;
goto bailout;
}
ctx->rid = ctx->cleaned_id;
ctx->reserved = 0;
ctx->reused_slot = 0;
if (ctx->rid != ctx->cleaned_id) {
ctx->rid = ctx->cleaned_id;
ctx->reserved = 0;
ctx->reused_slot = 0;
}
ctx->cleaned_id = unaligned_peek_u64(4, key.iov_base);
if (ctx->cleaned_id > txn->tw.last_reclaimed)
break;
Expand Down Expand Up @@ -15145,7 +15151,16 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
#endif /* MDBX_ENABLE_REFUND */
}

const size_t amount = MDBX_PNL_GETSIZE(txn->tw.relist);
if (unlikely(ctx->reserved > MDBX_PNL_GETSIZE(txn->tw.relist)) &&
(ctx->loop < 5 || ctx->reserved - MDBX_PNL_GETSIZE(txn->tw.relist) >
env->me_maxgc_ov1page / 2)) {
TRACE("%s: reclaimed-list changed %zu -> %zu, retry", dbg_prefix_mode,
ctx->amount, MDBX_PNL_GETSIZE(txn->tw.relist));
ctx->reserve_adj += ctx->reserved - MDBX_PNL_GETSIZE(txn->tw.relist);
goto retry;
}
ctx->amount = MDBX_PNL_GETSIZE(txn->tw.relist);

/* handle retired-list - store ones into single gc-record */
if (ctx->retired_stored < MDBX_PNL_GETSIZE(txn->tw.retired_pages)) {
if (unlikely(!ctx->retired_stored)) {
Expand Down Expand Up @@ -15281,14 +15296,6 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
DEBUG_EXTRA_PRINT(" %" PRIaPGNO, txn->tw.retired_pages[i]);
DEBUG_EXTRA_PRINT("%s\n", ".");
}
if (unlikely(amount != MDBX_PNL_GETSIZE(txn->tw.relist) &&
ctx->reserved)) {
TRACE("%s: reclaimed-list changed %zu -> %zu, retry", dbg_prefix_mode,
amount, MDBX_PNL_GETSIZE(txn->tw.relist));
goto retry_clean_adj /* rare case, but avoids GC fragmentation
and one cycle. */
;
}
continue;
}

Expand All @@ -15303,11 +15310,11 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
if (unlikely(rc != MDBX_SUCCESS))
goto bailout;
}
const size_t left = amount - ctx->reserved - ctx->reserve_adj;
TRACE("%s: amount %zu, settled %zd, reserve_adj %zu, left %zd, "
const size_t left = ctx->amount - ctx->reserved - ctx->reserve_adj;
TRACE("%s: amount %zu, reserved %zd, reserve_adj %zu, left %zd, "
"lifo-reclaimed-slots %zu, "
"reused-gc-slots %zu",
dbg_prefix_mode, amount, ctx->reserved, ctx->reserve_adj, left,
dbg_prefix_mode, ctx->amount, ctx->reserved, ctx->reserve_adj, left,
txn->tw.lifo_reclaimed ? MDBX_PNL_GETSIZE(txn->tw.lifo_reclaimed) : 0,
ctx->reused_slot);
if (0 >= (intptr_t)left)
Expand Down Expand Up @@ -15382,7 +15389,7 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
if (unlikely(MDBX_PNL_GETSIZE(txn->tw.lifo_reclaimed) <=
ctx->reused_slot)) {
NOTICE("** restart: reserve depleted (reused_gc_slot %zu >= "
"lifo_reclaimed %zu" PRIaTXN,
"lifo_reclaimed %zu)",
ctx->reused_slot,
MDBX_PNL_GETSIZE(txn->tw.lifo_reclaimed));
goto retry;
Expand Down Expand Up @@ -15469,7 +15476,7 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
if (unlikely(ctx->rid == 0)) {
ERROR("%s", "** no GC tail-space to store (going dense-mode)");
ctx->dense = true;
goto retry_clean_adj;
goto retry;
}
} else if (rc != MDBX_NOTFOUND)
goto bailout;
Expand All @@ -15483,17 +15490,24 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
++ctx->reused_slot;

size_t chunk = left;
if (unlikely(chunk > env->me_maxgc_ov1page)) {
if (unlikely(left > env->me_maxgc_ov1page)) {
const size_t avail_gc_slots =
txn->tw.lifo_reclaimed
? MDBX_PNL_GETSIZE(txn->tw.lifo_reclaimed) - ctx->reused_slot + 1
: (ctx->rid < INT16_MAX) ? (size_t)ctx->rid
: INT16_MAX;
if (avail_gc_slots > 1) {
if (likely(avail_gc_slots > 1)) {
#if MDBX_ENABLE_BIGFOOT
chunk = (chunk < env->me_maxgc_ov1page * (size_t)2)
? chunk / 2
: env->me_maxgc_ov1page;
chunk = env->me_maxgc_ov1page;
if (avail_gc_slots < INT16_MAX &&
unlikely(left > env->me_maxgc_ov1page * avail_gc_slots))
/* TODO: Можно смотреть последовательности какой длины есть в relist
* и пробовать нарезать куски соответствующего размера.
* Смысл в том, чтобы не дробить последовательности страниц,
* а использовать целиком. */
chunk = env->me_maxgc_ov1page +
left / (env->me_maxgc_ov1page * avail_gc_slots) *
env->me_maxgc_ov1page;
#else
if (chunk < env->me_maxgc_ov1page * 2)
chunk /= 2;
Expand All @@ -15510,7 +15524,7 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
size_t avail = ((pgno2bytes(env, span) - PAGEHDRSZ) /
sizeof(pgno_t)) /* - 1 + span */;
if (tail > avail) {
for (size_t i = amount - span; i > 0; --i) {
for (size_t i = ctx->amount - span; i > 0; --i) {
if (MDBX_PNL_ASCENDING ? (txn->tw.relist[i] + span)
: (txn->tw.relist[i] - span) ==
txn->tw.relist[i + span]) {
Expand Down Expand Up @@ -15569,18 +15583,9 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {

gcu_clean_reserved(env, data);
ctx->reserved += chunk;
TRACE("%s: settled %zu (+%zu), continue", dbg_prefix_mode, ctx->reserved,
TRACE("%s: reserved %zu (+%zu), continue", dbg_prefix_mode, ctx->reserved,
chunk);

if (txn->tw.lifo_reclaimed &&
unlikely(amount < MDBX_PNL_GETSIZE(txn->tw.relist)) &&
(ctx->loop < 5 || MDBX_PNL_GETSIZE(txn->tw.relist) - amount >
env->me_maxgc_ov1page / 2)) {
NOTICE("** restart: reclaimed-list growth %zu -> %zu", amount,
MDBX_PNL_GETSIZE(txn->tw.relist));
goto retry_clean_adj;
}

continue;
}

Expand All @@ -15600,13 +15605,12 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
tASSERT(txn, pnl_check_allocated(txn->tw.relist,
txn->mt_next_pgno - MDBX_ENABLE_REFUND));
tASSERT(txn, dirtylist_check(txn));
if (ctx->reserved || MDBX_PNL_GETSIZE(txn->tw.relist)) {
if (ctx->amount) {
MDBX_val key, data;
key.iov_len = data.iov_len = 0; /* avoid MSVC warning */
key.iov_base = data.iov_base = NULL;

const size_t amount = MDBX_PNL_GETSIZE(txn->tw.relist);
size_t left = amount, excess = 0;
size_t left = ctx->amount, excess = 0;
if (txn->tw.lifo_reclaimed == nullptr) {
tASSERT(txn, ctx->lifo == 0);
rc = cursor_first(&ctx->cursor, &key, &data);
Expand Down Expand Up @@ -15691,25 +15695,29 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
gcu_clean_reserved(env, data);

if (unlikely(txn->tw.loose_count ||
amount != MDBX_PNL_GETSIZE(txn->tw.relist))) {
NOTICE("** restart: reclaimed-list growth (%zu -> %zu, loose +%zu)",
amount, MDBX_PNL_GETSIZE(txn->tw.relist), txn->tw.loose_count);
goto retry_clean_adj;
ctx->amount != MDBX_PNL_GETSIZE(txn->tw.relist))) {
NOTICE("** restart: reclaimed-list changed (%zu -> %zu, loose +%zu)",
ctx->amount, MDBX_PNL_GETSIZE(txn->tw.relist),
txn->tw.loose_count);
if (ctx->loop < 5 || (ctx->loop > 10 && (ctx->loop & 1)))
goto retry_clean_adj;
goto retry;
}

if (unlikely(txn->tw.lifo_reclaimed
? ctx->cleaned_slot <
MDBX_PNL_GETSIZE(txn->tw.lifo_reclaimed)
: ctx->cleaned_id < txn->tw.last_reclaimed)) {
NOTICE("%s", "** restart: reclaimed-slots changed");
goto retry_clean_adj;
goto retry;
}
if (unlikely(ctx->retired_stored !=
MDBX_PNL_GETSIZE(txn->tw.retired_pages))) {
tASSERT(txn,
ctx->retired_stored < MDBX_PNL_GETSIZE(txn->tw.retired_pages));
NOTICE("** restart: retired-list growth (%zu -> %zu)",
ctx->retired_stored, MDBX_PNL_GETSIZE(txn->tw.retired_pages));
goto retry_clean_adj;
goto retry;
}

pgno_t *dst = data.iov_base;
Expand All @@ -15723,7 +15731,7 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {

left -= chunk;
if (AUDIT_ENABLED()) {
rc = audit_ex(txn, ctx->retired_stored + amount - left, true);
rc = audit_ex(txn, ctx->retired_stored + ctx->amount - left, true);
if (unlikely(rc != MDBX_SUCCESS))
goto bailout;
}
Expand Down Expand Up @@ -15754,9 +15762,11 @@ static int update_gc(MDBX_txn *txn, gcu_context_t *ctx) {
}

tASSERT(txn, rc == MDBX_SUCCESS);
if (unlikely(txn->tw.loose_count != 0)) {
NOTICE("** restart: got %zu loose pages", txn->tw.loose_count);
goto retry_clean_adj;
if (unlikely(txn->tw.loose_count != 0 ||
ctx->amount != MDBX_PNL_GETSIZE(txn->tw.relist))) {
NOTICE("** restart: got %zu loose pages (reclaimed-list %zu -> %zu)",
txn->tw.loose_count, ctx->amount, MDBX_PNL_GETSIZE(txn->tw.relist));
goto retry;
}

if (unlikely(excess_slots)) {
Expand Down Expand Up @@ -16073,7 +16083,7 @@ static __inline void txn_merge(MDBX_txn *const parent, MDBX_txn *const txn,
}
++w;
}
NOTICE("squash to begin for extending-merge %zu -> %zu", d, w - 1);
VERBOSE("squash to begin for extending-merge %zu -> %zu", d, w - 1);
d = w - 1;
continue;
}
Expand Down Expand Up @@ -16115,7 +16125,7 @@ static __inline void txn_merge(MDBX_txn *const parent, MDBX_txn *const txn,
}
--w;
}
NOTICE("squash to end for shrinking-merge %zu -> %zu", d, w + 1);
VERBOSE("squash to end for shrinking-merge %zu -> %zu", d, w + 1);
d = w + 1;
continue;
}
Expand Down Expand Up @@ -37140,9 +37150,9 @@ __dll_export
0,
13,
0,
41,
{"2024-04-05T00:30:46+03:00", "797cb93cde0a0dde4835bb2754a4c2fd7191863d", "0da3c87a8fd8b69ec85184f27ebef55030c0a3b2",
"v0.13.0-41-g0da3c87a"},
43,
{"2024-04-16T08:47:45+03:00", "8725c090d7241d5aa21bcaadf9f984ed19d9033b", "661372d66de09b094d6ba0fbbe4d4c6c8e9de6cf",
"v0.13.0-43-g661372d6"},
sourcery};

__dll_export
Expand Down
2 changes: 1 addition & 1 deletion mdbxdist/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.13.0.41
0.13.0.43
Loading

0 comments on commit c3fb4f6

Please sign in to comment.