openldap/libraries/liblmdb/mdb_stat.c

264 lines
6.3 KiB
C
Raw Normal View History

/* mdb_stat.c - memory-mapped database status tool */
/*
2016-01-30 20:55:03 +08:00
* Copyright 2011-2016 Howard Chu, Symas Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include <stdio.h>
#include <stdlib.h>
2012-10-17 06:28:20 +08:00
#include <string.h>
#include <unistd.h>
#include "lmdb.h"
2013-08-08 03:42:46 +08:00
#ifdef _WIN32
#define Z "I"
#else
#define Z "z"
#endif
2012-10-17 06:28:20 +08:00
static void prstat(MDB_stat *ms)
{
2012-10-17 07:52:13 +08:00
#if 0
printf(" Page size: %u\n", ms->ms_psize);
#endif
printf(" Tree depth: %u\n", ms->ms_depth);
2013-08-08 03:42:46 +08:00
printf(" Branch pages: %"Z"u\n", ms->ms_branch_pages);
printf(" Leaf pages: %"Z"u\n", ms->ms_leaf_pages);
printf(" Overflow pages: %"Z"u\n", ms->ms_overflow_pages);
printf(" Entries: %"Z"u\n", ms->ms_entries);
2012-10-17 06:28:20 +08:00
}
static void usage(char *prog)
{
2015-10-01 01:11:20 +08:00
fprintf(stderr, "usage: %s [-V] [-n] [-e] [-r[r]] [-f[f[f]]] [-a|-s subdb] dbpath\n", prog);
2012-10-17 06:28:20 +08:00
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int i, rc;
MDB_env *env;
2011-08-03 16:41:54 +08:00
MDB_txn *txn;
MDB_dbi dbi;
2011-07-03 19:22:10 +08:00
MDB_stat mst;
MDB_envinfo mei;
2012-10-17 06:28:20 +08:00
char *prog = argv[0];
char *envname;
char *subname = NULL;
int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 0, rdrinfo = 0;
2012-10-17 06:28:20 +08:00
if (argc < 2) {
usage(prog);
}
/* -a: print stat of main DB and all subDBs
* -s: print stat of only the named subDB
* -e: print env info
2012-10-17 07:52:13 +08:00
* -f: print freelist info
* -r: print reader info
2012-10-17 07:52:13 +08:00
* -n: use NOSUBDIR flag on env_open
2014-06-20 20:33:31 +08:00
* -V: print version and exit
2012-10-17 06:28:20 +08:00
* (default) print stat of only the main DB
*/
2014-06-20 20:33:31 +08:00
while ((i = getopt(argc, argv, "Vaefnrs:")) != EOF) {
2012-10-17 06:28:20 +08:00
switch(i) {
2014-06-20 20:33:31 +08:00
case 'V':
printf("%s\n", MDB_VERSION_STRING);
exit(0);
break;
2012-10-17 06:28:20 +08:00
case 'a':
if (subname)
usage(prog);
2012-10-17 06:28:20 +08:00
alldbs++;
break;
case 'e':
envinfo++;
break;
2012-10-17 07:52:13 +08:00
case 'f':
freinfo++;
break;
case 'n':
envflags |= MDB_NOSUBDIR;
break;
case 'r':
rdrinfo++;
break;
2012-10-17 06:28:20 +08:00
case 's':
if (alldbs)
usage(prog);
2012-10-17 06:28:20 +08:00
subname = optarg;
break;
default:
usage(prog);
}
}
if (optind != argc - 1)
usage(prog);
2012-10-17 06:28:20 +08:00
envname = argv[optind];
2011-08-12 12:14:29 +08:00
rc = mdb_env_create(&env);
2014-09-17 04:54:07 +08:00
if (rc) {
fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
return EXIT_FAILURE;
}
2011-08-11 18:23:22 +08:00
2012-10-17 06:28:20 +08:00
if (alldbs || subname) {
2011-08-12 12:14:29 +08:00
mdb_env_set_maxdbs(env, 4);
2011-08-11 18:23:22 +08:00
}
2012-10-17 07:52:13 +08:00
rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto env_close;
}
if (envinfo) {
2014-09-17 04:54:07 +08:00
(void)mdb_env_stat(env, &mst);
(void)mdb_env_info(env, &mei);
2012-10-17 07:52:13 +08:00
printf("Environment Info\n");
printf(" Map address: %p\n", mei.me_mapaddr);
2013-08-08 03:42:46 +08:00
printf(" Map size: %"Z"u\n", mei.me_mapsize);
2012-10-17 07:52:13 +08:00
printf(" Page size: %u\n", mst.ms_psize);
2013-08-08 03:42:46 +08:00
printf(" Max pages: %"Z"u\n", mei.me_mapsize / mst.ms_psize);
printf(" Number of pages used: %"Z"u\n", mei.me_last_pgno+1);
printf(" Last transaction ID: %"Z"u\n", mei.me_last_txnid);
2012-10-17 07:52:13 +08:00
printf(" Max readers: %u\n", mei.me_maxreaders);
printf(" Number of readers used: %u\n", mei.me_numreaders);
}
if (rdrinfo) {
printf("Reader Table Status\n");
rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
2013-07-19 01:40:21 +08:00
if (rdrinfo > 1) {
int dead;
mdb_reader_check(env, &dead);
printf(" %d stale readers cleared.\n", dead);
rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
}
if (!(subname || alldbs || freinfo))
goto env_close;
}
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
goto env_close;
}
2012-10-17 07:52:13 +08:00
if (freinfo) {
MDB_cursor *cursor;
2012-12-08 05:19:36 +08:00
MDB_val key, data;
2012-10-17 07:52:13 +08:00
size_t pages = 0, *iptr;
printf("Freelist Status\n");
dbi = 0;
rc = mdb_cursor_open(txn, dbi, &cursor);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
2012-10-17 07:52:13 +08:00
goto txn_abort;
}
rc = mdb_stat(txn, dbi, &mst);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
2012-10-17 07:52:13 +08:00
goto txn_abort;
}
2012-12-08 05:19:36 +08:00
prstat(&mst);
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
2012-10-17 07:52:13 +08:00
iptr = data.mv_data;
pages += *iptr;
2012-12-08 05:19:36 +08:00
if (freinfo > 1) {
char *bad = "";
size_t pg, prev;
ssize_t i, j, span = 0;
2012-12-08 05:19:36 +08:00
j = *iptr++;
for (i = j, prev = 1; --i >= 0; ) {
pg = iptr[i];
if (pg <= prev)
bad = " [bad sequence]";
prev = pg;
pg += span;
for (; i >= span && iptr[i-span] == pg; span++, pg++) ;
}
2013-08-08 03:42:46 +08:00
printf(" Transaction %"Z"u, %"Z"d pages, maxspan %"Z"d%s\n",
*(size_t *)key.mv_data, j, span, bad);
if (freinfo > 2) {
for (--j; j >= 0; ) {
pg = iptr[j];
for (span=1; --j >= 0 && iptr[j] == pg+span; span++) ;
2013-08-08 03:42:46 +08:00
printf(span>1 ? " %9"Z"u[%"Z"d]\n" : " %9"Z"u\n",
pg, span);
}
}
2012-12-08 05:19:36 +08:00
}
2012-10-17 07:52:13 +08:00
}
mdb_cursor_close(cursor);
2013-08-08 03:42:46 +08:00
printf(" Free pages: %"Z"u\n", pages);
}
2011-08-03 16:41:54 +08:00
rc = mdb_open(txn, subname, 0, &dbi);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort;
}
2011-08-03 16:41:54 +08:00
rc = mdb_stat(txn, dbi, &mst);
2012-10-17 06:28:20 +08:00
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
2012-10-17 06:28:20 +08:00
goto txn_abort;
}
2012-10-17 07:52:13 +08:00
printf("Status of %s\n", subname ? subname : "Main DB");
2012-10-17 06:28:20 +08:00
prstat(&mst);
if (alldbs) {
MDB_cursor *cursor;
MDB_val key;
rc = mdb_cursor_open(txn, dbi, &cursor);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
2012-10-17 06:28:20 +08:00
goto txn_abort;
}
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
char *str;
2012-10-17 06:28:20 +08:00
MDB_dbi db2;
if (memchr(key.mv_data, '\0', key.mv_size))
continue;
str = malloc(key.mv_size+1);
2012-10-17 06:28:20 +08:00
memcpy(str, key.mv_data, key.mv_size);
str[key.mv_size] = '\0';
rc = mdb_open(txn, str, 0, &db2);
if (rc == MDB_SUCCESS)
2012-10-17 07:52:13 +08:00
printf("Status of %s\n", str);
2012-10-17 06:28:20 +08:00
free(str);
if (rc) continue;
rc = mdb_stat(txn, db2, &mst);
if (rc) {
2014-07-01 20:11:43 +08:00
fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
2012-10-17 06:28:20 +08:00
goto txn_abort;
}
prstat(&mst);
mdb_close(env, db2);
}
mdb_cursor_close(cursor);
}
if (rc == MDB_NOTFOUND)
rc = MDB_SUCCESS;
mdb_close(env, dbi);
txn_abort:
2011-08-03 16:41:54 +08:00
mdb_txn_abort(txn);
env_close:
2011-08-12 12:14:29 +08:00
mdb_env_close(env);
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}