-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmdbx_defrag.c
More file actions
443 lines (404 loc) · 16.4 KB
/
mdbx_defrag.c
File metadata and controls
443 lines (404 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* This file is part of the libmdbx amalgamated source code (v0.14.1-574-g936b8871 at 2026-04-17T22:16:01+03:00).
*
* libmdbx (aka MDBX) is an extremely fast, compact, powerful, embeddedable, transactional key-value storage engine with
* open-source code. MDBX has a specific set of properties and capabilities, focused on creating unique lightweight
* solutions. Please visit https://libmdbx.dqdkfa.ru for more information, changelog, documentation, C++ API description
* and links to the original git repo with the source code. Questions, feedback and suggestions are welcome to the
* Telegram' group https://t.me/libmdbx, MAX' chat https://max.ru/join/dKckvyuARxp1vRK-wnPur8zYCEkbR3OUOmpPWkWxp78.
*
* The libmdbx code will forever remain open and with high-quality free support, as far as the life circumstances of the
* project participants allow. Donations are welcome to ETH `0xD104d8f8B2dC312aaD74899F83EBf3EEBDC1EA3A`,
* BTC `bc1qzvl9uegf2ea6cwlytnanrscyv8snwsvrc0xfsu`, SOL `FTCTgbHajoLVZGr8aEFWMzx3NDMyS5wXJgfeMTmJznRi`.
* Всё будет хорошо!
*
* For ease of use and to eliminate potential limitations in both distribution and obstacles in technology development,
* libmdbx is distributed as an amalgamated source code starting at the end of 2025. The source code of the tests, as
* well as the internal documentation, will be available only to the team directly involved in the development.
*
* Copyright 2015-2026 Леонид Юрьев aka Leonid Yuriev <leo@yuriev.ru>
* SPDX-License-Identifier: Apache-2.0
*
* For notes about the license change, credits and acknowledgments, please refer to the COPYRIGHT file. */
/* clang-format off */
#define xMDBX_TOOLS /* Avoid using internal ASSERT(), etc */
#include "mdbx-internals.h"
#if defined(_WIN32) || defined(_WIN64)
/* Bit of madness for Windows console */
#define mdbx_strerror mdbx_strerror_ANSI2OEM
#define mdbx_strerror_r mdbx_strerror_r_ANSI2OEM
#include "mdbx-wingetopt.h"
static volatile BOOL user_break;
static BOOL WINAPI ConsoleBreakHandlerRoutine(DWORD dwCtrlType) {
(void)dwCtrlType;
user_break = user_break ? 2 : 1;
return true;
}
#else /* WINDOWS */
static volatile sig_atomic_t user_break;
static void signal_handler(int sig) {
(void)sig;
user_break = user_break ? 2 : 1;
}
#endif /* !WINDOWS */
static char *prog;
static void usage(void) {
fprintf(stderr,
"usage: %s [-V] [-v[v[v...]]] [-q] [-1..9] [-t seconds] [-f percent] [-r percent] [-s megabytes] [-c] [-u|U] db_pathname\n"
" -V\t\tprint version and exit\n"
" -v\t\tmore verbose, could be repeated for extra details from debug-enabled builds.\n"
" -q\t\tbe quiet.\n"
" -1\t\tperform a single quick defragmentation cycle without two-stage moves.\n"
" -2 .. -9\tlimits the number of defragmentation cycles.\n"
" -t seconds\tlimits the duration of defragmentation in seconds.\n"
" -f percent\tdefragment until free up a given percentage of unused space (default 100).\n"
" -r percent\tacceptable undefragmented residue in percent of used space (0 by default).\n"
" -s megabytes\tpreferred defragmentation step/transaction size.\n"
" -c\t\tforce cooperative mode (don't try exclusive).\n"
" -u\t\twarmup database before defragmenting.\n"
" -U\t\twarmup and try lock database pages in memory before defragmenting.\n"
" db_pathname\tpath to the database.\n",
prog);
exit(EXIT_FAILURE);
}
static MDBX_log_level_t verbosity = MDBX_LOG_WARN;
static bool quiet = false;
static bool is_console = true;
static unsigned cycles_limit;
static unsigned progress_dots;
static void logger(MDBX_log_level_t level, const char *function, int line, const char *fmt, va_list args) {
static const char *const prefixes[] = {
"!!!fatal: ", // 0 fatal
" ! ", // 1 error
" ~ ", // 2 warning
" ", // 3 notice
" // ", // 4 verbose
" //// ", // 5 verbose
" ////// ", // 6 verbose
" //////// ", // 7 verbose
};
if (!quiet && level < MDBX_LOG_DEBUG) {
if (progress_dots) {
putchar(' ');
putchar('\n');
progress_dots = 0;
if (level <= MDBX_LOG_NOTICE)
fflush(nullptr);
}
FILE *out = (level < MDBX_LOG_NOTICE) ? stderr : stdout;
if (function && line)
fprintf(out, "%s", prefixes[level]);
vfprintf(out, fmt, args);
if (level <= MDBX_LOG_NOTICE)
fflush(nullptr);
}
}
static void defrag_report_progress(const MDBX_defrag_result_t *progress, unsigned dots) {
if (!quiet) {
if (progress->cycles == 0)
printf("\r - loading the b-tree structure: %u.%u%%", progress->rough_estimation_cycle_progress_permille / 10,
progress->rough_estimation_cycle_progress_permille % 10);
else {
printf("\r - cycle %u: %u.%u%%", progress->cycles, progress->rough_estimation_cycle_progress_permille / 10,
progress->rough_estimation_cycle_progress_permille % 10);
if (progress->pages_moved) {
printf(", moved %zi", progress->pages_moved);
if (progress->pages_moved < progress->pages_scheduled)
printf(" of %zi", progress->pages_scheduled);
} else if (progress->pages_scheduled)
printf(", scheduled %zi", progress->pages_scheduled);
if (progress->pages_retained)
printf(", retained %zi", progress->pages_retained);
if (progress->pages_shrinked)
printf(", shrinked %zi", progress->pages_shrinked);
printf(", left %zi", progress->pages_left);
}
for (unsigned i = 0; i < 3 || (i < dots / 8 && i < 64); ++i)
putchar('.');
if (is_console && dots) {
static char вертушка[] = "\\|/-\\|/-";
putchar(вертушка[(progress->spent_time_dot16 >> 13) % (ARRAY_LENGTH(вертушка) - 1)]);
putchar('\b');
}
fflush(nullptr);
}
}
static const char *stop_reason(const MDBX_defrag_result_t *progress) {
if (progress->stopping_reasons & MDBX_defrag_error)
return "error";
if (progress->stopping_reasons & MDBX_defrag_aborted)
return "aborted by user";
if (progress->stopping_reasons & MDBX_defrag_discontinued)
return "discontinued by user";
if (progress->stopping_reasons & MDBX_defrag_enough_threshold)
return "enough threshold";
if (progress->stopping_reasons & MDBX_defrag_time_limit)
return "time limit reached";
if (progress->stopping_reasons & MDBX_defrag_laggard_reader)
return "laggard reader";
if (progress->stopping_reasons & MDBX_defrag_large_chunk)
return "large chunk";
if (progress->stopping_reasons & MDBX_defrag_step_size)
return "step size";
return "done";
}
static int defrag_notify(void *ctx, const MDBX_defrag_result_t *progress) {
(void)ctx;
if (!quiet) {
static MDBX_defrag_result_t last_progress = {.cycles = UINT32_MAX};
static uint32_t last_report_spenttime;
bool tick = progress->spent_time_dot16 - last_report_spenttime > 65536 / 8;
if (!ctx || last_progress.cycles != progress->cycles) {
if (progress_dots) {
if (last_progress.cycles == progress->cycles)
last_progress.stopping_reasons = progress->stopping_reasons;
defrag_report_progress(&last_progress, progress_dots);
progress_dots = 0;
printf(" %s%s\n", stop_reason(&last_progress), is_console ? "\033[K" : "");
fflush(nullptr);
}
tick = true;
}
if (ctx && tick) {
last_report_spenttime = progress->spent_time_dot16;
defrag_report_progress(progress, progress_dots++);
}
last_progress = *progress;
}
if (user_break > 1)
return -1;
if (cycles_limit && (progress->cycles > cycles_limit ||
(progress->cycles == cycles_limit && (progress->stopping_reasons & MDBX_defrag_large_chunk))))
return 1;
return user_break;
}
int main(int argc, char *argv[]) {
int rc;
MDBX_env *env = nullptr;
const char *const progname = argv[0];
bool warmup = false;
size_t time_limit_seconds = 0;
unsigned acceptable_residue_percent = 0;
unsigned wanna_defrag_percent = 100;
size_t step_size_MiB = 0;
MDBX_env_flags_t env_flags = MDBX_ENV_DEFAULTS | MDBX_EXCLUSIVE;
MDBX_warmup_flags_t warmup_flags = MDBX_warmup_default;
prog = argv[0];
if (argc < 2)
usage();
for (int i; (i = getopt(argc, argv,
"V"
"v"
"q"
"123456789"
"s:"
"t:"
"f:"
"r:"
"c"
"u"
"U")) != EOF;) {
switch (i) {
case 'V':
printf("mdbx_defrag version %d.%d.%d.%d\n"
" - source: %s %s, commit %s, tree %s\n"
" - anchor: %s\n"
" - build: %s for %s by %s\n"
" - flags: %s\n"
" - options: %s\n",
mdbx_version.major, mdbx_version.minor, mdbx_version.patch, mdbx_version.tweak, mdbx_version.git.describe,
mdbx_version.git.datetime, mdbx_version.git.commit, mdbx_version.git.tree, mdbx_sourcery_anchor,
mdbx_build.datetime, mdbx_build.target, mdbx_build.compiler, mdbx_build.flags, mdbx_build.options);
return EXIT_SUCCESS;
case 'v':
if (++verbosity > 9)
usage();
break;
case 'q':
quiet = true;
break;
case 'c':
env_flags = (env_flags & ~MDBX_EXCLUSIVE) | MDBX_ACCEDE;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
cycles_limit = i - '0';
break;
case 't':
if (sscanf(optarg, "%zu", &time_limit_seconds) != 1) {
if (!quiet)
fprintf(stderr, "%s: %s option: expecting %s, but got '%s'\n", prog, "-t", "unsigned integer value", optarg);
return EXIT_FAILURE;
}
break;
case 'f':
if (sscanf(optarg, "%u", &wanna_defrag_percent) != 1 || wanna_defrag_percent < 1 || wanna_defrag_percent > 100) {
if (!quiet)
fprintf(stderr, "%s: %s option: expecting %s, but got '%s'\n", prog, "-f",
"unsigned integer value in range 1..100", optarg);
return EXIT_FAILURE;
}
break;
case 'r':
if (sscanf(optarg, "%u", &acceptable_residue_percent) != 1 || acceptable_residue_percent < 1 ||
acceptable_residue_percent > 100) {
if (!quiet)
fprintf(stderr, "%s: %s option: expecting %s, but got '%s'\n", prog, "-r",
"unsigned integer value in range 1..100", optarg);
return EXIT_FAILURE;
}
break;
case 's':
if (sscanf(optarg, "%zu", &step_size_MiB) != 1) {
if (!quiet)
fprintf(stderr, "%s: %s option: expecting %s, but got '%s'\n", prog, "-s",
"unsigned integer value in range 1..100", optarg);
return EXIT_FAILURE;
}
break;
case 'u':
warmup = true;
break;
case 'U':
warmup_flags = MDBX_warmup_force | MDBX_warmup_touchlimit | MDBX_warmup_lock;
warmup = true;
break;
default:
usage();
}
}
if (optind != argc - 1)
usage();
#if defined(_WIN32) || defined(_WIN64)
SetConsoleCtrlHandler(ConsoleBreakHandlerRoutine, true);
is_console = _isatty(_fileno(stdout)) != 0;
#else
is_console = isatty(fileno(stdout)) == 1;
#ifdef SIGPIPE
signal(SIGPIPE, signal_handler);
#endif
#ifdef SIGHUP
signal(SIGHUP, signal_handler);
#endif
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#endif /* !WINDOWS */
const char *const db_pathname = argv[optind];
if (!quiet) {
fprintf(stdout, "mdbx_defrag %s (%s, T-%s)\nRunning for %s...\n", mdbx_version.git.describe,
mdbx_version.git.datetime, mdbx_version.git.tree, db_pathname);
if (verbosity > MDBX_LOG_VERBOSE && MDBX_DEBUG < 1)
printf("Verbosity level %u exposures only to"
" a debug/extra-logging-enabled builds (MDBX_DEBUG > 0)\n",
verbosity);
mdbx_setup_debug(verbosity, MDBX_DBG_DONTCHANGE, logger);
fflush(nullptr);
}
const char *act = "opening environment";
rc = mdbx_env_create(&env);
if (rc == MDBX_SUCCESS)
rc = mdbx_env_open(env, db_pathname, env_flags, 0);
if ((env_flags & MDBX_EXCLUSIVE) && (rc == MDBX_BUSY ||
#if defined(_WIN32) || defined(_WIN64)
rc == ERROR_LOCK_VIOLATION || rc == ERROR_SHARING_VIOLATION
#else
rc == EBUSY || rc == EAGAIN
#endif
)) {
env_flags = (env_flags & ~MDBX_EXCLUSIVE) | MDBX_ACCEDE;
rc = mdbx_env_open(env, db_pathname, env_flags, 0);
}
if (rc == MDBX_SUCCESS && warmup) {
act = "warming up";
rc = mdbx_env_warmup(env, nullptr, warmup_flags, 3600 * 65536);
rc = MDBX_IS_ERROR(rc) ? rc : MDBX_SUCCESS;
}
act = "preparing";
MDBX_txn *txn = nullptr;
if (rc == MDBX_SUCCESS)
rc = mdbx_txn_begin(env, nullptr, MDBX_TXN_READWRITE, &txn);
MDBX_envinfo info_env;
memset(&info_env, 0, sizeof(info_env)); /* zap `uninitialized` warning */
if (rc == MDBX_SUCCESS)
rc = mdbx_env_info_ex(env, txn, &info_env, sizeof(info_env));
MDBX_gc_info_t info_gc;
memset(&info_gc, 0, sizeof(info_gc)); /* zap `uninitialized` warning */
if (rc == MDBX_SUCCESS)
rc = mdbx_gc_info(txn, &info_gc, sizeof(info_gc), nullptr, nullptr);
char ratio_buffer[42];
if (rc == MDBX_SUCCESS) {
const size_t pages_per_MiB = MEGABYTE / info_env.mi_dxb_pagesize;
const size_t payload_pages = info_gc.pages_allocated - info_gc.pages_gc;
const size_t preferred_batch = (step_size_MiB > INT_MAX / pages_per_MiB) ? 0 : step_size_MiB * pages_per_MiB;
const size_t duration_limit_dot16 =
(time_limit_seconds > SIZE_MAX >> 16) ? /* unlimited */ 0 : time_limit_seconds << /* seconds to dot16 */ 16;
const size_t defrag_enough = (wanna_defrag_percent < 100u) ? (info_gc.pages_gc + 99u) / 100u : 0;
const intptr_t acceptable_residue =
acceptable_residue_percent ? (intptr_t)((payload_pages + 99u) / 100u * acceptable_residue_percent) : -1;
if (!quiet) {
printf(" - space usage in pages: %zu allocated, payload %zu, unused/GC %zu, pagesize %u\n",
info_gc.pages_allocated, payload_pages, info_gc.pages_gc, info_env.mi_dxb_pagesize);
printf(" - defragmentation: target %u%% (shrink %zu pages), cycles ", wanna_defrag_percent,
defrag_enough ? defrag_enough : info_gc.pages_gc);
if (cycles_limit)
printf("limit %u", cycles_limit);
else
printf("unlimited");
printf(", step ");
if (preferred_batch)
printf("%zu pages (%zu MiB)", preferred_batch, step_size_MiB);
else
printf("unlimited");
if (acceptable_residue_percent)
printf(", acceptable residue %u%% (leftover %zu pages)", acceptable_residue_percent, acceptable_residue);
printf(", duration ");
if (time_limit_seconds)
printf("limit %s seconds\n",
mdbx_ratio2digits(duration_limit_dot16, 65536, 1, ratio_buffer, sizeof(ratio_buffer)));
else
puts("unlimited");
fflush(nullptr);
}
act = "defragmenting";
MDBX_defrag_result_t result;
rc = mdbx_env_defrag(env,
/* defrag_atleast */ 0,
/* time_atleast_dot16 */ 0, defrag_enough, duration_limit_dot16, acceptable_residue,
preferred_batch, defrag_notify, env, &result);
defrag_notify(nullptr, &result);
if (!MDBX_IS_ERROR(rc)) {
rc = MDBX_SUCCESS;
if (!quiet) {
printf("Defragmentation%s: shrinked %zi pages, %u passes, moved %zu pages",
(rc == MDBX_SUCCESS) ? "" : " incomplete", result.pages_shrinked, result.cycles, result.pages_moved);
if (result.stopping_reasons)
printf(", stopping reasons bits 0x%x", result.stopping_reasons);
printf(", took %s seconds, %s\n",
mdbx_ratio2digits(result.spent_time_dot16, 65536, 3, ratio_buffer, sizeof(ratio_buffer)),
stop_reason(&result));
}
}
}
if (txn) {
if (MDBX_IS_ERROR(rc))
mdbx_txn_abort(txn);
else {
act = "final commit";
rc = mdbx_txn_commit(txn);
}
txn = nullptr;
}
if (!quiet && MDBX_IS_ERROR(rc)) {
fflush(nullptr);
fprintf(stderr, "%s: %s failed, error %d (%s)\n", progname, act, rc, mdbx_strerror(rc));
}
mdbx_env_close(env);
fflush(nullptr);
return MDBX_IS_ERROR(rc) ? EXIT_FAILURE : EXIT_SUCCESS;
}