2016-06-09 18:16:28 +08:00
|
|
|
/* mdbx_chk.c - memory-mapped database check tool */
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2016-06-09 18:16:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015,2016 Leonid Yuriev <leo@yuriev.ru>.
|
|
|
|
* Copyright (c) 2015,2016 Peter-Service R&D LLC.
|
|
|
|
*
|
2016-09-27 18:42:15 +08:00
|
|
|
* This file is part of libmdbx.
|
2016-06-09 18:16:28 +08:00
|
|
|
*
|
|
|
|
* ReOpenMDBX is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* ReOpenMDBX is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
2015-05-08 08:44:30 +08:00
|
|
|
#include <malloc.h>
|
2015-09-01 18:43:22 +08:00
|
|
|
#include <time.h>
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
#include "midl.h"
|
2015-10-13 20:46:59 +08:00
|
|
|
#include "mdbx.h"
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
typedef struct flagbit {
|
|
|
|
int bit;
|
|
|
|
char *name;
|
|
|
|
} flagbit;
|
|
|
|
|
|
|
|
flagbit dbflags[] = {
|
|
|
|
{ MDB_DUPSORT, "dupsort" },
|
|
|
|
{ MDB_INTEGERKEY, "integerkey" },
|
|
|
|
{ MDB_REVERSEKEY, "reversekey" },
|
|
|
|
{ MDB_DUPFIXED, "dupfixed" },
|
|
|
|
{ MDB_REVERSEDUP, "reversedup" },
|
|
|
|
{ MDB_INTEGERDUP, "integerdup" },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static volatile sig_atomic_t gotsignal;
|
|
|
|
|
2016-11-27 17:56:27 +08:00
|
|
|
static void signal_handler( int sig ) {
|
2016-04-16 03:11:40 +08:00
|
|
|
(void) sig;
|
2015-04-29 12:59:42 +08:00
|
|
|
gotsignal = 1;
|
|
|
|
}
|
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
#define MAX_DBI 32768
|
|
|
|
|
2015-09-01 18:31:03 +08:00
|
|
|
#define EXIT_INTERRUPTED (EXIT_FAILURE+4)
|
|
|
|
#define EXIT_FAILURE_SYS (EXIT_FAILURE+3)
|
|
|
|
#define EXIT_FAILURE_MDB (EXIT_FAILURE+2)
|
|
|
|
#define EXIT_FAILURE_CHECK_MAJOR (EXIT_FAILURE+1)
|
|
|
|
#define EXIT_FAILURE_CHECK_MINOR EXIT_FAILURE
|
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
struct {
|
|
|
|
const char* dbi_names[MAX_DBI];
|
|
|
|
size_t dbi_pages[MAX_DBI];
|
2015-09-02 19:36:03 +08:00
|
|
|
size_t dbi_empty_pages[MAX_DBI];
|
2015-09-01 01:58:41 +08:00
|
|
|
size_t dbi_payload_bytes[MAX_DBI];
|
2015-09-02 19:36:03 +08:00
|
|
|
size_t dbi_lost_bytes[MAX_DBI];
|
2015-09-01 01:58:41 +08:00
|
|
|
short *pagemap;
|
|
|
|
size_t total_payload_bytes;
|
|
|
|
size_t pgcount;
|
2016-11-27 04:54:38 +08:00
|
|
|
} walk;
|
|
|
|
|
|
|
|
static __attribute__((constructor))
|
|
|
|
void init_walk(void)
|
|
|
|
{
|
|
|
|
walk.dbi_names[0] = "@gc";
|
|
|
|
}
|
2015-09-01 01:58:41 +08:00
|
|
|
|
|
|
|
size_t total_unused_bytes;
|
2015-08-08 01:05:10 +08:00
|
|
|
int exclusive = 2;
|
2015-05-08 08:44:30 +08:00
|
|
|
|
2015-04-29 12:59:42 +08:00
|
|
|
MDB_env *env;
|
2015-08-07 02:52:38 +08:00
|
|
|
MDB_txn *txn, *locktxn;
|
2015-10-13 20:46:59 +08:00
|
|
|
MDBX_envinfo info;
|
|
|
|
MDBX_stat stat;
|
2015-05-08 08:44:30 +08:00
|
|
|
size_t maxkeysize, reclaimable_pages, freedb_pages, lastpgno;
|
2015-09-01 02:34:57 +08:00
|
|
|
size_t userdb_count, skipped_subdb;
|
2015-08-05 18:13:43 +08:00
|
|
|
unsigned verbose, quiet;
|
2015-09-01 02:34:57 +08:00
|
|
|
const char* only_subdb;
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-08-06 22:11:08 +08:00
|
|
|
struct problem {
|
|
|
|
struct problem* pr_next;
|
|
|
|
size_t count;
|
|
|
|
const char* caption;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct problem* problems_list;
|
|
|
|
size_t total_problems;
|
|
|
|
|
2015-08-05 03:32:34 +08:00
|
|
|
static void __attribute__ ((format (printf, 1, 2)))
|
|
|
|
print(const char* msg, ...) {
|
2015-04-29 12:59:42 +08:00
|
|
|
if (! quiet) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
va_start(args, msg);
|
|
|
|
vfprintf(stdout, msg, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 03:32:34 +08:00
|
|
|
static void __attribute__ ((format (printf, 1, 2)))
|
|
|
|
error(const char* msg, ...) {
|
2015-08-06 22:11:08 +08:00
|
|
|
total_problems++;
|
|
|
|
|
2015-04-29 12:59:42 +08:00
|
|
|
if (! quiet) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
va_start(args, msg);
|
|
|
|
vfprintf(stderr, msg, args);
|
|
|
|
va_end(args);
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:01:31 +08:00
|
|
|
static void pagemap_cleanup(void) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 1; i < MAX_DBI; ++i ) {
|
|
|
|
if (walk.dbi_names[i]) {
|
|
|
|
free((void *) walk.dbi_names[i]);
|
|
|
|
walk.dbi_names[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(walk.pagemap);
|
|
|
|
walk.pagemap = NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
static int pagemap_lookup_dbi(const char* dbi) {
|
|
|
|
static int last;
|
2015-11-19 19:01:31 +08:00
|
|
|
int i;
|
2015-05-08 08:44:30 +08:00
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
if (last > 0 && strcmp(walk.dbi_names[last], dbi) == 0)
|
2015-05-08 08:44:30 +08:00
|
|
|
return last;
|
|
|
|
|
2015-11-19 19:01:31 +08:00
|
|
|
for(i = 1; walk.dbi_names[i] && last < MAX_DBI; ++i)
|
|
|
|
if (strcmp(walk.dbi_names[i], dbi) == 0)
|
|
|
|
return last = i;
|
2015-05-08 08:44:30 +08:00
|
|
|
|
2015-11-19 19:01:31 +08:00
|
|
|
if (i == MAX_DBI)
|
|
|
|
return -1;
|
2015-05-08 08:44:30 +08:00
|
|
|
|
2015-11-19 19:01:31 +08:00
|
|
|
walk.dbi_names[i] = strdup(dbi);
|
2015-09-02 18:30:53 +08:00
|
|
|
|
2015-09-02 19:36:03 +08:00
|
|
|
if (verbose > 1) {
|
2015-09-02 18:30:53 +08:00
|
|
|
print(" - found '%s' area\n", dbi);
|
2015-09-02 19:36:03 +08:00
|
|
|
fflush(NULL);
|
|
|
|
}
|
2015-09-02 18:30:53 +08:00
|
|
|
|
2015-11-19 19:01:31 +08:00
|
|
|
return last = i;
|
2015-05-08 08:44:30 +08:00
|
|
|
}
|
|
|
|
|
2015-09-03 22:20:01 +08:00
|
|
|
static void problem_add(const char* object, size_t entry_number, const char* msg, const char *extra, ...) {
|
2015-04-29 12:59:42 +08:00
|
|
|
total_problems++;
|
|
|
|
|
|
|
|
if (! quiet) {
|
2015-09-01 18:53:38 +08:00
|
|
|
int need_fflush = 0;
|
2015-04-29 12:59:42 +08:00
|
|
|
struct problem* p;
|
|
|
|
|
|
|
|
for (p = problems_list; p; p = p->pr_next)
|
|
|
|
if (p->caption == msg)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (! p) {
|
|
|
|
p = calloc(1, sizeof(*p));
|
|
|
|
p->caption = msg;
|
|
|
|
p->pr_next = problems_list;
|
|
|
|
problems_list = p;
|
2015-09-01 18:53:38 +08:00
|
|
|
need_fflush = 1;
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p->count++;
|
|
|
|
if (verbose > 1) {
|
2015-09-03 22:20:01 +08:00
|
|
|
print(" %s #%zu: %s", object, entry_number, msg);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (extra) {
|
|
|
|
va_list args;
|
2015-09-02 19:36:03 +08:00
|
|
|
printf(" (");
|
2015-04-29 12:59:42 +08:00
|
|
|
va_start(args, extra);
|
|
|
|
vfprintf(stdout, extra, args);
|
|
|
|
va_end(args);
|
2015-09-02 19:36:03 +08:00
|
|
|
printf(")");
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
printf("\n");
|
2015-09-01 18:53:38 +08:00
|
|
|
if (need_fflush)
|
|
|
|
fflush(NULL);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct problem* problems_push() {
|
|
|
|
struct problem* p = problems_list;
|
|
|
|
problems_list = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t problems_pop(struct problem* list) {
|
2015-08-08 02:27:11 +08:00
|
|
|
size_t count = 0;
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
if (problems_list) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
print(" - problems: ");
|
|
|
|
for (i = 0; problems_list; ++i) {
|
|
|
|
struct problem* p = problems_list->pr_next;
|
2015-08-08 02:27:11 +08:00
|
|
|
count += problems_list->count;
|
2015-04-29 12:59:42 +08:00
|
|
|
print("%s%s (%zu)", i ? ", " : "", problems_list->caption, problems_list->count);
|
|
|
|
free(problems_list);
|
|
|
|
problems_list = p;
|
|
|
|
}
|
|
|
|
print("\n");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
problems_list = list;
|
2015-08-08 02:27:11 +08:00
|
|
|
return count;
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 18:13:43 +08:00
|
|
|
static int pgvisitor(size_t pgno, unsigned pgnumber, void* ctx, const char* dbi,
|
2015-09-02 22:53:48 +08:00
|
|
|
const char* type, int nentries, int payload_bytes, int header_bytes, int unused_bytes)
|
2015-05-08 08:44:30 +08:00
|
|
|
{
|
2016-04-16 03:11:40 +08:00
|
|
|
(void) ctx;
|
|
|
|
|
2015-09-02 18:30:53 +08:00
|
|
|
if (type) {
|
|
|
|
size_t page_bytes = payload_bytes + header_bytes + unused_bytes;
|
2015-10-13 20:46:59 +08:00
|
|
|
size_t page_size = pgnumber * stat.base.ms_psize;
|
2015-05-08 08:44:30 +08:00
|
|
|
int index = pagemap_lookup_dbi(dbi);
|
|
|
|
if (index < 0)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2015-09-02 19:36:03 +08:00
|
|
|
if (verbose > 2 && (!only_subdb || strcmp(only_subdb, dbi) == 0)) {
|
2015-09-06 08:37:24 +08:00
|
|
|
if (pgnumber == 1)
|
|
|
|
print(" %s-page %zu", type, pgno);
|
|
|
|
else
|
|
|
|
print(" %s-span %zu[%u]", type, pgno, pgnumber);
|
2015-09-02 18:30:53 +08:00
|
|
|
print(" of %s: header %i, payload %i, unused %i\n",
|
|
|
|
dbi, header_bytes, payload_bytes, unused_bytes);
|
|
|
|
}
|
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
walk.pgcount += pgnumber;
|
|
|
|
|
2015-09-02 18:30:53 +08:00
|
|
|
if (unused_bytes < 0 || (size_t) unused_bytes > page_size)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("page", pgno, "illegal unused-bytes", "%zu < %i < %zu",
|
2015-10-13 20:46:59 +08:00
|
|
|
0, unused_bytes, stat.base.ms_psize);
|
2015-09-02 18:30:53 +08:00
|
|
|
|
2016-04-16 03:11:40 +08:00
|
|
|
if (header_bytes < (int) sizeof(long) || (size_t) header_bytes >= stat.base.ms_psize - sizeof(long))
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("page", pgno, "illegal header-length", "%zu < %i < %zu",
|
2015-10-13 20:46:59 +08:00
|
|
|
sizeof(long), header_bytes, stat.base.ms_psize - sizeof(long));
|
2015-09-03 22:20:01 +08:00
|
|
|
if (payload_bytes < 1) {
|
|
|
|
if (nentries > 0) {
|
|
|
|
problem_add("page", pgno, "zero size-of-entry", "payload %i bytes, %i entries",
|
|
|
|
payload_bytes, nentries);
|
2016-04-16 03:11:40 +08:00
|
|
|
if ((size_t) header_bytes + unused_bytes < page_size) {
|
2015-09-03 22:20:01 +08:00
|
|
|
/* LY: hush a misuse error */
|
|
|
|
page_bytes = page_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
problem_add("page", pgno, "empty", "payload %i bytes, %i entries",
|
|
|
|
payload_bytes, nentries);
|
|
|
|
walk.dbi_empty_pages[index] += 1;
|
|
|
|
}
|
2015-09-02 19:36:03 +08:00
|
|
|
}
|
2015-09-02 18:30:53 +08:00
|
|
|
|
2015-09-02 19:36:03 +08:00
|
|
|
if (page_bytes != page_size) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("page", pgno, "misused", "%zu != %zu (%ih + %ip + %iu)",
|
2015-09-02 18:30:53 +08:00
|
|
|
page_size, page_bytes, header_bytes, payload_bytes, unused_bytes);
|
2015-09-02 19:36:03 +08:00
|
|
|
if (page_size > page_bytes)
|
|
|
|
walk.dbi_lost_bytes[index] += page_size - page_bytes;
|
|
|
|
} else {
|
2015-09-01 01:58:41 +08:00
|
|
|
walk.dbi_payload_bytes[index] += payload_bytes + header_bytes;
|
|
|
|
walk.total_payload_bytes += payload_bytes + header_bytes;
|
2015-08-05 18:13:43 +08:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:30:53 +08:00
|
|
|
if (pgnumber) {
|
|
|
|
do {
|
|
|
|
if (pgno >= lastpgno)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("page", pgno, "wrong page-no",
|
2015-09-02 19:36:03 +08:00
|
|
|
"%zu > %zi", pgno, lastpgno);
|
2015-09-02 18:30:53 +08:00
|
|
|
else if (walk.pagemap[pgno])
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("page", pgno, "already used",
|
2015-09-02 19:36:03 +08:00
|
|
|
"in %s", walk.dbi_names[walk.pagemap[pgno]]);
|
2015-09-02 18:30:53 +08:00
|
|
|
else {
|
|
|
|
walk.pagemap[pgno] = index;
|
|
|
|
walk.dbi_pages[index] += 1;
|
|
|
|
}
|
|
|
|
++pgno;
|
|
|
|
} while(--pgnumber);
|
|
|
|
}
|
2015-05-08 08:44:30 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 02:43:00 +08:00
|
|
|
return gotsignal ? EINTR : MDB_SUCCESS;
|
2015-05-08 08:44:30 +08:00
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
typedef int (visitor)(size_t record_number, MDB_val *key, MDB_val* data);
|
|
|
|
static int process_db(MDB_dbi dbi, char *name, visitor *handler, int silent);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
static int handle_userdb(size_t record_number, MDB_val *key, MDB_val* data) {
|
2016-04-16 03:11:40 +08:00
|
|
|
(void) record_number;
|
|
|
|
(void) key;
|
|
|
|
(void) data;
|
2015-04-29 12:59:42 +08:00
|
|
|
return MDB_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
static int handle_freedb(size_t record_number, MDB_val *key, MDB_val* data) {
|
2015-04-29 12:59:42 +08:00
|
|
|
char *bad = "";
|
|
|
|
size_t pg, prev;
|
|
|
|
ssize_t i, number, span = 0;
|
|
|
|
size_t *iptr = data->mv_data, txnid = *(size_t*)key->mv_data;
|
|
|
|
|
|
|
|
if (key->mv_size != sizeof(txnid))
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "wrong txn-id size", "key-size %zi", key->mv_size);
|
2015-10-13 20:46:59 +08:00
|
|
|
else if (txnid < 1 || txnid > info.base.me_last_txnid)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "wrong txn-id", "%zu", txnid);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
if (data->mv_size < sizeof(size_t) || data->mv_size % sizeof(size_t))
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "wrong idl size", "%zu", data->mv_size);
|
2015-04-29 12:59:42 +08:00
|
|
|
else {
|
|
|
|
number = *iptr++;
|
2016-02-09 14:26:26 +08:00
|
|
|
if (number >= MDB_IDL_UM_MAX)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "wrong idl length", "%zi", number);
|
2015-04-29 12:59:42 +08:00
|
|
|
else if ((number + 1) * sizeof(size_t) != data->mv_size)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "mismatch idl length", "%zi != %zu",
|
2015-04-29 12:59:42 +08:00
|
|
|
number * sizeof(size_t), data->mv_size);
|
|
|
|
else {
|
|
|
|
freedb_pages += number;
|
|
|
|
if (info.me_tail_txnid > txnid)
|
|
|
|
reclaimable_pages += number;
|
|
|
|
for (i = number, prev = 1; --i >= 0; ) {
|
|
|
|
pg = iptr[i];
|
2015-10-13 20:46:59 +08:00
|
|
|
if (pg < 2 /* META_PAGE */ || pg > info.base.me_last_pgno)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "wrong idl entry", "2 < %zi < %zi",
|
2015-10-13 20:46:59 +08:00
|
|
|
pg, info.base.me_last_pgno);
|
2015-08-08 02:27:11 +08:00
|
|
|
else if (pg <= prev) {
|
2015-04-29 12:59:42 +08:00
|
|
|
bad = " [bad sequence]";
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_number, "bad sequence", "%zi <= %zi",
|
2015-08-08 02:27:11 +08:00
|
|
|
pg, prev);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
prev = pg;
|
|
|
|
pg += span;
|
|
|
|
for (; i >= span && iptr[i - span] == pg; span++, pg++) ;
|
|
|
|
}
|
2015-09-02 19:36:03 +08:00
|
|
|
if (verbose > 2 && !only_subdb) {
|
2015-09-02 18:30:53 +08:00
|
|
|
print(" transaction %zu, %zd pages, maxspan %zd%s\n",
|
2015-04-29 12:59:42 +08:00
|
|
|
*(size_t *)key->mv_data, number, span, bad);
|
2015-09-02 19:36:03 +08:00
|
|
|
if (verbose > 3) {
|
|
|
|
int j = number - 1;
|
|
|
|
while (j >= 0) {
|
|
|
|
pg = iptr[j];
|
|
|
|
for (span = 1; --j >= 0 && iptr[j] == pg + span; span++) ;
|
2015-09-06 08:37:24 +08:00
|
|
|
if (span > 1)
|
|
|
|
print(" %9zu[%zd]\n", pg, span);
|
|
|
|
else
|
|
|
|
print(" %9zu\n", pg);
|
2015-09-02 19:36:03 +08:00
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
return MDB_SUCCESS;
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
static int handle_maindb(size_t record_number, MDB_val *key, MDB_val* data) {
|
2015-04-29 12:59:42 +08:00
|
|
|
char *name;
|
2016-04-16 03:11:40 +08:00
|
|
|
int rc;
|
|
|
|
size_t i;
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
name = key->mv_data;
|
|
|
|
for(i = 0; i < key->mv_size; ++i) {
|
|
|
|
if (name[i] < ' ')
|
|
|
|
return handle_userdb(record_number, key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
name = malloc(key->mv_size + 1);
|
|
|
|
memcpy(name, key->mv_data, key->mv_size);
|
|
|
|
name[key->mv_size] = '\0';
|
|
|
|
userdb_count++;
|
|
|
|
|
|
|
|
rc = process_db(-1, name, handle_userdb, 0);
|
|
|
|
free(name);
|
2015-08-03 20:59:19 +08:00
|
|
|
if (rc != MDB_INCOMPATIBLE)
|
2015-04-29 12:59:42 +08:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
return handle_userdb(record_number, key, data);
|
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
static int process_db(MDB_dbi dbi, char *name, visitor *handler, int silent)
|
2015-04-29 12:59:42 +08:00
|
|
|
{
|
|
|
|
MDB_cursor *mc;
|
2015-10-13 20:46:59 +08:00
|
|
|
MDBX_stat ms;
|
2015-04-29 12:59:42 +08:00
|
|
|
MDB_val key, data;
|
|
|
|
MDB_val prev_key, prev_data;
|
|
|
|
unsigned flags;
|
|
|
|
int rc, i;
|
|
|
|
struct problem* saved_list;
|
|
|
|
size_t problems_count;
|
|
|
|
|
|
|
|
unsigned record_count = 0, dups = 0;
|
|
|
|
size_t key_bytes = 0, data_bytes = 0;
|
|
|
|
|
|
|
|
if (0 > (int) dbi) {
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_dbi_open(txn, name, 0, &dbi);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-08-03 20:59:19 +08:00
|
|
|
if (!name || rc != MDB_INCOMPATIBLE) /* LY: mainDB's record is not a user's DB. */ {
|
2015-10-13 20:46:59 +08:00
|
|
|
error(" - mdbx_open '%s' failed, error %d %s\n",
|
|
|
|
name ? name : "main", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 02:34:57 +08:00
|
|
|
if (dbi >= 2 /* CORE_DBS */ && name && only_subdb && strcmp(only_subdb, name)) {
|
2015-09-01 18:53:38 +08:00
|
|
|
if (verbose) {
|
2015-09-03 22:20:01 +08:00
|
|
|
print("Skip processing '%s'...\n", name);
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
|
|
|
}
|
2015-09-01 02:34:57 +08:00
|
|
|
skipped_subdb++;
|
|
|
|
return MDB_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-09-01 18:53:38 +08:00
|
|
|
if (! silent && verbose) {
|
2015-09-03 22:20:01 +08:00
|
|
|
print("Processing '%s'...\n", name ? name : "main");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_dbi_flags(txn, dbi, &flags);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error(" - mdbx_dbi_flags failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_stat(txn, dbi, &ms, sizeof(ms));
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error(" - mdbx_stat failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! silent && verbose) {
|
2015-09-01 02:34:57 +08:00
|
|
|
print(" - dbi-id %d, flags:", dbi);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (! flags)
|
|
|
|
print(" none");
|
|
|
|
else {
|
|
|
|
for (i=0; dbflags[i].bit; i++)
|
|
|
|
if (flags & dbflags[i].bit)
|
|
|
|
print(" %s", dbflags[i].name);
|
|
|
|
}
|
2015-09-01 18:53:38 +08:00
|
|
|
print(" (0x%02X)\n", flags);
|
2015-08-05 18:13:43 +08:00
|
|
|
if (verbose > 1) {
|
2015-10-13 20:46:59 +08:00
|
|
|
print(" - page size %u, entries %zu\n", ms.base.ms_psize, ms.base.ms_entries);
|
2015-08-05 18:13:43 +08:00
|
|
|
print(" - b-tree depth %u, pages: branch %zu, leaf %zu, overflow %zu\n",
|
2015-10-13 20:46:59 +08:00
|
|
|
ms.base.ms_depth, ms.base.ms_branch_pages, ms.base.ms_leaf_pages, ms.base.ms_overflow_pages);
|
2015-08-05 18:13:43 +08:00
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_cursor_open(txn, dbi, &mc);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error(" - mdbx_cursor_open failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_list = problems_push();
|
|
|
|
prev_key.mv_data = NULL;
|
|
|
|
prev_data.mv_size = 0;
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_cursor_get(mc, &key, &data, MDB_FIRST);
|
2015-04-29 12:59:42 +08:00
|
|
|
while (rc == MDB_SUCCESS) {
|
2015-08-08 02:27:11 +08:00
|
|
|
if (gotsignal) {
|
|
|
|
print(" - interrupted by signal\n");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-08-08 02:27:11 +08:00
|
|
|
rc = EINTR;
|
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:59:42 +08:00
|
|
|
if (key.mv_size == 0) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "key with zero length", NULL);
|
2015-04-29 12:59:42 +08:00
|
|
|
} else if (key.mv_size > maxkeysize) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "key length exceeds max-key-size",
|
2015-09-02 19:36:03 +08:00
|
|
|
"%zu > %zu", key.mv_size, maxkeysize);
|
2015-04-29 12:59:42 +08:00
|
|
|
} else if ((flags & MDB_INTEGERKEY)
|
|
|
|
&& key.mv_size != sizeof(size_t) && key.mv_size != sizeof(int)) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "wrong key length",
|
2015-09-02 19:36:03 +08:00
|
|
|
"%zu != %zu", key.mv_size, sizeof(size_t));
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & MDB_INTEGERDUP)
|
|
|
|
&& data.mv_size != sizeof(size_t) && data.mv_size != sizeof(int)) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "wrong data length",
|
2015-09-02 19:36:03 +08:00
|
|
|
"%zu != %zu", data.mv_size, sizeof(size_t));
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prev_key.mv_data) {
|
|
|
|
if ((flags & MDB_DUPFIXED) && prev_data.mv_size != data.mv_size) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "different data length",
|
2015-09-02 19:36:03 +08:00
|
|
|
"%zu != %zu", prev_data.mv_size, data.mv_size);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
int cmp = mdbx_cmp(txn, dbi, &prev_key, &key);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (cmp > 0) {
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "broken ordering of entries", NULL);
|
2015-04-29 12:59:42 +08:00
|
|
|
} else if (cmp == 0) {
|
|
|
|
++dups;
|
|
|
|
if (! (flags & MDB_DUPSORT))
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "duplicated entries", NULL);
|
2015-08-05 05:33:04 +08:00
|
|
|
else if (flags & MDB_INTEGERDUP) {
|
2015-10-13 20:46:59 +08:00
|
|
|
cmp = mdbx_dcmp(txn, dbi, &prev_data, &data);
|
2015-08-05 05:33:04 +08:00
|
|
|
if (cmp > 0)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "broken ordering of multi-values", NULL);
|
2015-08-05 05:33:04 +08:00
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
2015-08-05 18:13:43 +08:00
|
|
|
} else if (verbose) {
|
2015-04-29 12:59:42 +08:00
|
|
|
if (flags & MDB_INTEGERKEY)
|
|
|
|
print(" - fixed key-size %zu\n", key.mv_size );
|
|
|
|
if (flags & (MDB_INTEGERDUP | MDB_DUPFIXED))
|
|
|
|
print(" - fixed data-size %zu\n", data.mv_size );
|
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
if (handler) {
|
|
|
|
rc = handler(record_count, &key, &data);
|
|
|
|
if (rc)
|
|
|
|
goto bailout;
|
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
record_count++;
|
|
|
|
key_bytes += key.mv_size;
|
|
|
|
data_bytes += data.mv_size;
|
|
|
|
|
|
|
|
prev_key = key;
|
|
|
|
prev_data = data;
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_cursor_get(mc, &key, &data, MDB_NEXT);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
2015-08-08 02:27:11 +08:00
|
|
|
if (rc != MDB_NOTFOUND)
|
2015-10-13 20:46:59 +08:00
|
|
|
error(" - mdbx_cursor_get failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-08-08 02:27:11 +08:00
|
|
|
else
|
|
|
|
rc = 0;
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
if (record_count != ms.base.ms_entries)
|
2015-09-03 22:20:01 +08:00
|
|
|
problem_add("entry", record_count, "differentent number of entries",
|
2015-10-13 20:46:59 +08:00
|
|
|
"%zu != %zu", record_count, ms.base.ms_entries);
|
2015-04-29 12:59:42 +08:00
|
|
|
bailout:
|
|
|
|
problems_count = problems_pop(saved_list);
|
|
|
|
if (! silent && verbose) {
|
2015-08-05 18:13:43 +08:00
|
|
|
print(" - summary: %u records, %u dups, %zu key's bytes, %zu data's bytes, %zu problems\n",
|
2015-04-29 12:59:42 +08:00
|
|
|
record_count, dups, key_bytes, data_bytes, problems_count);
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
mdbx_cursor_close(mc);
|
2015-08-08 02:27:11 +08:00
|
|
|
return rc || problems_count;
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(char *prog)
|
|
|
|
{
|
2015-09-01 02:34:57 +08:00
|
|
|
fprintf(stderr, "usage: %s dbpath [-V] [-v] [-n] [-q] [-w] [-c] [-d] [-s subdb]\n"
|
|
|
|
" -V\t\tshow version\n"
|
|
|
|
" -v\t\tmore verbose, could be used multiple times\n"
|
|
|
|
" -n\t\tNOSUBDIR mode for open\n"
|
|
|
|
" -q\t\tbe quiet\n"
|
|
|
|
" -w\t\tlock DB for writing while checking\n"
|
|
|
|
" -d\t\tdisable page-by-page traversal of b-tree\n"
|
|
|
|
" -s subdb\tprocess a specific subdatabase only\n"
|
|
|
|
" -c\t\tforce cooperative mode (don't try exclusive)\n", prog);
|
2015-09-01 18:31:03 +08:00
|
|
|
exit(EXIT_INTERRUPTED);
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 02:50:18 +08:00
|
|
|
const char* meta_synctype(size_t sign) {
|
|
|
|
switch(sign) {
|
|
|
|
case 0:
|
2015-09-01 17:04:08 +08:00
|
|
|
return "no-sync/legacy";
|
2015-05-12 02:50:18 +08:00
|
|
|
case 1:
|
|
|
|
return "weak";
|
|
|
|
default:
|
|
|
|
return "steady";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int meta_lt(size_t txn1, size_t sign1, size_t txn2, size_t sign2) {
|
2015-08-08 01:05:10 +08:00
|
|
|
return ((sign1 > 1) == (sign2 > 1)) ? txn1 < txn2 : txn2 && sign2 > 1;
|
2015-05-12 02:50:18 +08:00
|
|
|
}
|
|
|
|
|
2015-04-29 12:59:42 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i, rc;
|
|
|
|
char *prog = argv[0];
|
|
|
|
char *envname;
|
2015-08-07 02:52:38 +08:00
|
|
|
int envflags = MDB_RDONLY;
|
2015-08-08 02:27:11 +08:00
|
|
|
int problems_maindb = 0, problems_freedb = 0, problems_meta = 0;
|
2015-09-01 01:58:41 +08:00
|
|
|
int dont_traversal = 0;
|
2015-05-08 08:44:30 +08:00
|
|
|
size_t n;
|
2015-09-01 18:43:22 +08:00
|
|
|
struct timespec timestamp_start, timestamp_finish;
|
|
|
|
double elapsed;
|
|
|
|
|
2015-11-19 19:01:31 +08:00
|
|
|
atexit(pagemap_cleanup);
|
|
|
|
|
2015-09-01 18:43:22 +08:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, ×tamp_start)) {
|
|
|
|
rc = errno;
|
2015-10-13 20:46:59 +08:00
|
|
|
error("clock_gettime failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-09-01 18:43:22 +08:00
|
|
|
return EXIT_FAILURE_SYS;
|
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
|
2015-09-01 02:34:57 +08:00
|
|
|
while ((i = getopt(argc, argv, "Vvqnwcds:")) != EOF) {
|
2015-04-29 12:59:42 +08:00
|
|
|
switch(i) {
|
|
|
|
case 'V':
|
|
|
|
printf("%s\n", MDB_VERSION_STRING);
|
2015-09-01 18:31:03 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
2015-04-29 12:59:42 +08:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
envflags |= MDB_NOSUBDIR;
|
|
|
|
break;
|
2015-08-07 02:52:38 +08:00
|
|
|
case 'w':
|
|
|
|
envflags &= ~MDB_RDONLY;
|
|
|
|
break;
|
2015-08-08 01:05:10 +08:00
|
|
|
case 'c':
|
|
|
|
exclusive = 0;
|
|
|
|
break;
|
2015-09-01 01:58:41 +08:00
|
|
|
case 'd':
|
|
|
|
dont_traversal = 1;
|
|
|
|
break;
|
2015-09-01 02:34:57 +08:00
|
|
|
case 's':
|
|
|
|
if (only_subdb && strcmp(only_subdb, optarg))
|
|
|
|
usage(prog);
|
|
|
|
only_subdb = optarg;
|
|
|
|
break;
|
2015-04-29 12:59:42 +08:00
|
|
|
default:
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc - 1)
|
|
|
|
usage(prog);
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
2016-11-27 17:56:27 +08:00
|
|
|
signal(SIGPIPE, signal_handler);
|
2015-04-29 12:59:42 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGHUP
|
2016-11-27 17:56:27 +08:00
|
|
|
signal(SIGHUP, signal_handler);
|
2015-04-29 12:59:42 +08:00
|
|
|
#endif
|
2016-11-27 17:56:27 +08:00
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
envname = argv[optind];
|
2015-10-13 20:46:59 +08:00
|
|
|
print("Running mdbx_chk for '%s' in %s mode...\n",
|
2015-08-07 02:52:38 +08:00
|
|
|
envname, (envflags & MDB_RDONLY) ? "read-only" : "write-lock");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-05-12 02:50:18 +08:00
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_env_create(&env);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_env_create failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-09-01 18:31:03 +08:00
|
|
|
return rc < 0 ? EXIT_FAILURE_MDB : EXIT_FAILURE_SYS;
|
2015-04-29 12:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_env_get_maxkeysize(env);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc < 0) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_env_get_maxkeysize failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
maxkeysize = rc;
|
|
|
|
|
2016-12-22 01:29:19 +08:00
|
|
|
rc = mdbx_env_set_maxdbs(env, MAX_DBI);
|
|
|
|
if (rc < 0) {
|
|
|
|
error("mdbx_env_set_maxdbs failed, error %d %s\n", rc, mdbx_strerror(rc));
|
|
|
|
goto bailout;
|
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_env_open_ex(env, envname, envflags, 0664, &exclusive);
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_env_open failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
2015-08-08 01:05:10 +08:00
|
|
|
if (verbose)
|
|
|
|
print(" - %s mode\n", exclusive ? "monopolistic" : "cooperative");
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-08-07 02:52:38 +08:00
|
|
|
if (! (envflags & MDB_RDONLY)) {
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_txn_begin(env, NULL, 0, &locktxn);
|
2015-08-07 02:52:38 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_txn_begin(lock-write) failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-08-07 02:52:38 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_txn_begin(env, NULL, MDB_RDONLY, &txn);
|
2015-08-08 03:16:15 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_txn_begin(read-only) failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-08-08 03:16:15 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_env_info(env, &info, sizeof(info));
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_env_info failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_env_stat(env, &stat, sizeof(stat));
|
2015-04-29 12:59:42 +08:00
|
|
|
if (rc) {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_env_stat failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-04-29 12:59:42 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
lastpgno = info.base.me_last_pgno + 1;
|
2015-05-08 08:44:30 +08:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (verbose) {
|
2015-05-12 02:50:18 +08:00
|
|
|
double k = 1024.0;
|
|
|
|
const char sf[] = "KMGTPEZY"; /* LY: Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta! */
|
2015-10-13 20:46:59 +08:00
|
|
|
for(i = 0; sf[i+1] && info.base.me_mapsize / k > 1000.0; ++i)
|
2015-05-12 02:50:18 +08:00
|
|
|
k *= 1024;
|
2015-10-13 20:46:59 +08:00
|
|
|
print(" - map size %zu (%.2f %cb)\n", info.base.me_mapsize,
|
|
|
|
info.base.me_mapsize / k, sf[i]);
|
|
|
|
if (info.base.me_mapaddr)
|
|
|
|
print(" - mapaddr %p\n", info.base.me_mapaddr);
|
2015-09-02 18:30:53 +08:00
|
|
|
print(" - pagesize %u, max keysize %zu (%s), max readers %u\n",
|
2015-10-13 20:46:59 +08:00
|
|
|
stat.base.ms_psize, maxkeysize,
|
2015-09-02 18:30:53 +08:00
|
|
|
(maxkeysize == 511) ? "default" :
|
|
|
|
(maxkeysize == 0) ? "devel" : "custom",
|
2015-10-13 20:46:59 +08:00
|
|
|
info.base.me_maxreaders);
|
|
|
|
print(" - transactions: last %zu, bottom %zu, lag reading %zi\n", info.base.me_last_txnid,
|
|
|
|
info.me_tail_txnid, info.base.me_last_txnid - info.me_tail_txnid);
|
2015-05-12 02:50:18 +08:00
|
|
|
|
|
|
|
print(" - meta-1: %s %zu, %s",
|
|
|
|
meta_synctype(info.me_meta1_sign), info.me_meta1_txnid,
|
|
|
|
meta_lt(info.me_meta1_txnid, info.me_meta1_sign,
|
|
|
|
info.me_meta2_txnid, info.me_meta2_sign) ? "tail" : "head");
|
2015-10-13 20:46:59 +08:00
|
|
|
if (info.me_meta1_txnid > info.base.me_last_txnid)
|
2016-12-22 01:31:46 +08:00
|
|
|
print(", rolled-back %zu (%zu >>> %zu)",
|
2015-10-13 20:46:59 +08:00
|
|
|
info.me_meta1_txnid - info.base.me_last_txnid,
|
|
|
|
info.me_meta1_txnid, info.base.me_last_txnid);
|
2015-09-06 08:37:24 +08:00
|
|
|
print("\n");
|
2015-05-12 02:50:18 +08:00
|
|
|
|
|
|
|
print(" - meta-2: %s %zu, %s",
|
|
|
|
meta_synctype(info.me_meta2_sign), info.me_meta2_txnid,
|
|
|
|
meta_lt(info.me_meta2_txnid, info.me_meta2_sign,
|
|
|
|
info.me_meta1_txnid, info.me_meta1_sign) ? "tail" : "head");
|
2015-10-13 20:46:59 +08:00
|
|
|
if (info.me_meta2_txnid > info.base.me_last_txnid)
|
2016-12-22 01:31:46 +08:00
|
|
|
print(", rolled-back %zu (%zu >>> %zu)",
|
2015-10-13 20:46:59 +08:00
|
|
|
info.me_meta2_txnid - info.base.me_last_txnid,
|
|
|
|
info.me_meta2_txnid, info.base.me_last_txnid);
|
2015-09-06 08:37:24 +08:00
|
|
|
print("\n");
|
2015-05-12 02:50:18 +08:00
|
|
|
}
|
|
|
|
|
2015-08-08 01:05:10 +08:00
|
|
|
if (exclusive > 1) {
|
2015-08-08 03:16:15 +08:00
|
|
|
if (verbose)
|
|
|
|
print(" - perform full check last-txn-id with meta-pages\n");
|
|
|
|
|
2015-08-08 01:05:10 +08:00
|
|
|
if (! meta_lt(info.me_meta1_txnid, info.me_meta1_sign,
|
|
|
|
info.me_meta2_txnid, info.me_meta2_sign)
|
2015-10-13 20:46:59 +08:00
|
|
|
&& info.me_meta1_txnid != info.base.me_last_txnid) {
|
2015-08-08 03:16:15 +08:00
|
|
|
print(" - meta-1 txn-id mismatch last-txn-id (%zi != %zi)\n",
|
2015-10-13 20:46:59 +08:00
|
|
|
info.me_meta1_txnid, info.base.me_last_txnid);
|
2015-08-08 01:05:10 +08:00
|
|
|
++problems_meta;
|
|
|
|
}
|
2015-05-12 02:50:18 +08:00
|
|
|
|
2015-08-08 01:05:10 +08:00
|
|
|
if (! meta_lt(info.me_meta2_txnid, info.me_meta2_sign,
|
|
|
|
info.me_meta1_txnid, info.me_meta1_sign)
|
2015-10-13 20:46:59 +08:00
|
|
|
&& info.me_meta2_txnid != info.base.me_last_txnid) {
|
2015-08-08 03:16:15 +08:00
|
|
|
print(" - meta-2 txn-id mismatch last-txn-id (%zi != %zi)\n",
|
2015-10-13 20:46:59 +08:00
|
|
|
info.me_meta2_txnid, info.base.me_last_txnid);
|
2015-08-08 03:16:15 +08:00
|
|
|
++problems_meta;
|
|
|
|
}
|
|
|
|
} else if (locktxn) {
|
|
|
|
if (verbose)
|
|
|
|
print(" - perform lite check last-txn-id with meta-pages (not a monopolistic mode)\n");
|
|
|
|
size_t last = (info.me_meta2_txnid > info.me_meta1_txnid) ? info.me_meta2_txnid : info.me_meta1_txnid;
|
2015-10-13 20:46:59 +08:00
|
|
|
if (last != info.base.me_last_txnid) {
|
2015-08-08 03:16:15 +08:00
|
|
|
print(" - last-meta mismatch last-txn-id (%zi != %zi)\n",
|
2015-10-13 20:46:59 +08:00
|
|
|
last, info.base.me_last_txnid);
|
2015-08-08 01:05:10 +08:00
|
|
|
++problems_meta;
|
|
|
|
}
|
2015-08-08 03:16:15 +08:00
|
|
|
} else if (verbose) {
|
|
|
|
print(" - skip check last-txn-id with meta-pages (monopolistic or write-lock mode only)\n");
|
2015-05-08 08:44:30 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
if (!dont_traversal) {
|
2015-09-02 18:30:53 +08:00
|
|
|
struct problem* saved_list;
|
|
|
|
size_t traversal_problems;
|
2015-09-02 19:36:03 +08:00
|
|
|
size_t empty_pages, lost_bytes;
|
2015-09-02 18:30:53 +08:00
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
print("Traversal b-tree...\n");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-09-01 01:58:41 +08:00
|
|
|
walk.pagemap = calloc(lastpgno, sizeof(*walk.pagemap));
|
|
|
|
if (! walk.pagemap) {
|
|
|
|
rc = errno ? errno : ENOMEM;
|
2015-10-13 20:46:59 +08:00
|
|
|
error("calloc failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-09-01 01:58:41 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
2015-08-05 18:13:43 +08:00
|
|
|
|
2015-09-02 18:30:53 +08:00
|
|
|
saved_list = problems_push();
|
2015-10-13 20:46:59 +08:00
|
|
|
rc = mdbx_env_pgwalk(txn, pgvisitor, NULL);
|
2015-09-02 18:30:53 +08:00
|
|
|
traversal_problems = problems_pop(saved_list);
|
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
if (rc) {
|
2015-09-01 02:43:00 +08:00
|
|
|
if (rc == EINTR && gotsignal) {
|
|
|
|
print(" - interrupted by signal\n");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-09-01 02:43:00 +08:00
|
|
|
} else {
|
2015-10-13 20:46:59 +08:00
|
|
|
error("mdbx_env_pgwalk failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-09-01 02:43:00 +08:00
|
|
|
}
|
2015-09-01 01:58:41 +08:00
|
|
|
goto bailout;
|
|
|
|
}
|
2015-09-02 19:36:03 +08:00
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
for( n = 0; n < lastpgno; ++n)
|
|
|
|
if (! walk.pagemap[n])
|
|
|
|
walk.dbi_pages[0] += 1;
|
|
|
|
|
2015-09-02 19:36:03 +08:00
|
|
|
empty_pages = lost_bytes = 0;
|
|
|
|
for (i = 1; i < MAX_DBI && walk.dbi_names[i]; ++i) {
|
|
|
|
empty_pages += walk.dbi_empty_pages[i];
|
|
|
|
lost_bytes += walk.dbi_lost_bytes[i];
|
|
|
|
}
|
|
|
|
|
2015-09-01 01:58:41 +08:00
|
|
|
if (verbose) {
|
2015-10-13 20:46:59 +08:00
|
|
|
size_t total_page_bytes = walk.pgcount * stat.base.ms_psize;
|
2015-09-01 01:58:41 +08:00
|
|
|
print(" - dbi pages: %zu total", walk.pgcount);
|
|
|
|
if (verbose > 1)
|
|
|
|
for (i = 1; i < MAX_DBI && walk.dbi_names[i]; ++i)
|
|
|
|
print(", %s %zu", walk.dbi_names[i], walk.dbi_pages[i]);
|
|
|
|
print(", %s %zu\n", walk.dbi_names[0], walk.dbi_pages[0]);
|
|
|
|
if (verbose > 1) {
|
2015-09-01 18:53:38 +08:00
|
|
|
print(" - space info: total %zu bytes, payload %zu (%.1f%%), unused %zu (%.1f%%)\n",
|
|
|
|
total_page_bytes, walk.total_payload_bytes,
|
|
|
|
walk.total_payload_bytes * 100.0 / total_page_bytes,
|
|
|
|
total_page_bytes - walk.total_payload_bytes,
|
|
|
|
(total_page_bytes - walk.total_payload_bytes) * 100.0 / total_page_bytes);
|
2015-09-01 01:58:41 +08:00
|
|
|
for (i = 1; i < MAX_DBI && walk.dbi_names[i]; ++i) {
|
2015-10-13 20:46:59 +08:00
|
|
|
size_t dbi_bytes = walk.dbi_pages[i] * stat.base.ms_psize;
|
2015-09-02 19:36:03 +08:00
|
|
|
print(" %s: subtotal %zu bytes (%.1f%%), payload %zu (%.1f%%), unused %zu (%.1f%%)",
|
2015-09-01 01:58:41 +08:00
|
|
|
walk.dbi_names[i],
|
2015-09-01 18:53:38 +08:00
|
|
|
dbi_bytes, dbi_bytes * 100.0 / total_page_bytes,
|
|
|
|
walk.dbi_payload_bytes[i], walk.dbi_payload_bytes[i] * 100.0 / dbi_bytes,
|
|
|
|
dbi_bytes - walk.dbi_payload_bytes[i],
|
|
|
|
(dbi_bytes - walk.dbi_payload_bytes[i]) * 100.0 / dbi_bytes);
|
2015-09-02 19:36:03 +08:00
|
|
|
if (walk.dbi_empty_pages[i])
|
|
|
|
print(", %zu empty pages", walk.dbi_empty_pages[i]);
|
|
|
|
if (walk.dbi_lost_bytes[i])
|
|
|
|
print(", %zu bytes lost", walk.dbi_lost_bytes[i]);
|
|
|
|
print("\n");
|
2015-09-01 01:58:41 +08:00
|
|
|
}
|
2015-08-05 18:13:43 +08:00
|
|
|
}
|
2015-09-02 19:36:03 +08:00
|
|
|
print(" - summary: average fill %.1f%%", walk.total_payload_bytes * 100.0 / total_page_bytes);
|
|
|
|
if (empty_pages)
|
|
|
|
print(", %zu empty pages", empty_pages);
|
|
|
|
if (lost_bytes)
|
|
|
|
print(", %zu bytes lost", lost_bytes);
|
|
|
|
print(", %zu problems\n", traversal_problems);
|
2015-08-05 18:13:43 +08:00
|
|
|
}
|
2015-09-01 01:58:41 +08:00
|
|
|
} else if (verbose) {
|
|
|
|
print("Skipping b-tree walk...\n");
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-05-08 08:44:30 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 18:13:43 +08:00
|
|
|
if (! verbose)
|
|
|
|
print("Iterating DBIs...\n");
|
2015-05-08 08:44:30 +08:00
|
|
|
problems_maindb = process_db(-1, /* MAIN_DBI */ NULL, NULL, 0);
|
|
|
|
problems_freedb = process_db(0 /* FREE_DBI */, "free", handle_freedb, 0);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
if (verbose) {
|
2015-10-13 20:46:59 +08:00
|
|
|
size_t value = info.base.me_mapsize / stat.base.ms_psize;
|
2015-04-29 12:59:42 +08:00
|
|
|
double percent = value / 100.0;
|
2015-05-08 08:44:30 +08:00
|
|
|
print(" - pages info: %zu total", value);
|
|
|
|
print(", allocated %zu (%.1f%%)", lastpgno, lastpgno / percent);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
if (verbose > 1) {
|
2015-10-13 20:46:59 +08:00
|
|
|
value = info.base.me_mapsize / stat.base.ms_psize - lastpgno;
|
2015-05-08 08:44:30 +08:00
|
|
|
print(", remained %zu (%.1f%%)", value, value / percent);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
value = lastpgno - freedb_pages;
|
|
|
|
print(", used %zu (%.1f%%)", value, value / percent);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
print(", gc %zu (%.1f%%)", freedb_pages, freedb_pages / percent);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
value = freedb_pages - reclaimable_pages;
|
2016-04-28 01:06:26 +08:00
|
|
|
print(", detained %zu (%.1f%%)", value, value / percent);
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-05-08 08:44:30 +08:00
|
|
|
print(", reclaimable %zu (%.1f%%)", reclaimable_pages, reclaimable_pages / percent);
|
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-10-13 20:46:59 +08:00
|
|
|
value = info.base.me_mapsize / stat.base.ms_psize - lastpgno + reclaimable_pages;
|
2015-04-29 12:59:42 +08:00
|
|
|
print(", available %zu (%.1f%%)\n", value, value / percent);
|
|
|
|
}
|
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
if (problems_maindb == 0 && problems_freedb == 0) {
|
2015-09-01 01:58:41 +08:00
|
|
|
if (!dont_traversal && (exclusive || locktxn)) {
|
|
|
|
if (walk.pgcount != lastpgno - freedb_pages) {
|
|
|
|
error("used pages mismatch (%zu != %zu)\n", walk.pgcount, lastpgno - freedb_pages);
|
2015-08-08 03:16:15 +08:00
|
|
|
}
|
2015-09-01 01:58:41 +08:00
|
|
|
if (walk.dbi_pages[0] != freedb_pages) {
|
|
|
|
error("gc pages mismatch (%zu != %zu)\n", walk.dbi_pages[0], freedb_pages);
|
2015-08-08 03:16:15 +08:00
|
|
|
}
|
|
|
|
} else if (verbose) {
|
2015-09-01 01:58:41 +08:00
|
|
|
print(" - skip check used and gc pages (btree-traversal with monopolistic or write-lock mode only)\n");
|
2015-08-08 02:27:11 +08:00
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
2015-08-08 02:27:11 +08:00
|
|
|
if (! process_db(-1, NULL, handle_maindb, 1)) {
|
|
|
|
if (! userdb_count && verbose)
|
|
|
|
print(" - does not contain multiple databases\n");
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 12:59:42 +08:00
|
|
|
|
|
|
|
bailout:
|
2015-08-08 02:27:11 +08:00
|
|
|
if (txn)
|
2015-10-13 20:46:59 +08:00
|
|
|
mdbx_txn_abort(txn);
|
2015-08-08 03:16:15 +08:00
|
|
|
if (locktxn)
|
2015-10-13 20:46:59 +08:00
|
|
|
mdbx_txn_abort(locktxn);
|
2015-08-08 03:39:57 +08:00
|
|
|
if (env)
|
2015-10-13 20:46:59 +08:00
|
|
|
mdbx_env_close(env);
|
2015-09-01 18:53:38 +08:00
|
|
|
fflush(NULL);
|
2015-09-01 18:31:03 +08:00
|
|
|
if (rc) {
|
|
|
|
if (rc < 0)
|
|
|
|
return gotsignal ? EXIT_INTERRUPTED : EXIT_FAILURE_SYS;
|
|
|
|
return EXIT_FAILURE_MDB;
|
|
|
|
}
|
|
|
|
|
2015-09-01 18:43:22 +08:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, ×tamp_finish)) {
|
|
|
|
rc = errno;
|
2015-10-13 20:46:59 +08:00
|
|
|
error("clock_gettime failed, error %d %s\n", rc, mdbx_strerror(rc));
|
2015-09-01 18:43:22 +08:00
|
|
|
return EXIT_FAILURE_SYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed = timestamp_finish.tv_sec - timestamp_start.tv_sec
|
|
|
|
+ (timestamp_finish.tv_nsec - timestamp_start.tv_nsec) * 1e-9;
|
|
|
|
|
2015-08-06 22:11:08 +08:00
|
|
|
total_problems += problems_meta;
|
2015-08-08 02:27:11 +08:00
|
|
|
if (total_problems || problems_maindb || problems_freedb) {
|
2015-09-01 18:43:22 +08:00
|
|
|
print("Total %zu error(s) is detected, elapsed %.3f seconds.\n",
|
|
|
|
total_problems, elapsed);
|
2015-08-05 18:13:43 +08:00
|
|
|
if (problems_meta || problems_maindb || problems_freedb)
|
2015-09-01 18:31:03 +08:00
|
|
|
return EXIT_FAILURE_CHECK_MAJOR;
|
|
|
|
return EXIT_FAILURE_CHECK_MINOR;
|
2015-08-05 18:13:43 +08:00
|
|
|
}
|
2015-09-01 18:43:22 +08:00
|
|
|
print("No error is detected, elapsed %.3f seconds\n", elapsed);
|
2015-04-29 12:59:42 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|