2011-08-09 16:13:55 +08:00
|
|
|
/* mdb_stat.c - memory-mapped database status tool */
|
|
|
|
/*
|
2013-02-17 02:07:16 +08:00
|
|
|
* Copyright 2011-2013 Howard Chu, Symas Corp.
|
2011-08-09 16:13:55 +08:00
|
|
|
* 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>.
|
|
|
|
*/
|
2011-06-28 15:16:05 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-10-17 06:28:20 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2012-12-01 04:30:51 +08:00
|
|
|
#include "lmdb.h"
|
2011-06-28 15:16:05 +08:00
|
|
|
|
2012-10-17 06:28:20 +08:00
|
|
|
static void prstat(MDB_stat *ms)
|
2011-06-28 15:16:05 +08:00
|
|
|
{
|
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);
|
|
|
|
printf(" Branch pages: %zu\n", ms->ms_branch_pages);
|
|
|
|
printf(" Leaf pages: %zu\n", ms->ms_leaf_pages);
|
|
|
|
printf(" Overflow pages: %zu\n", ms->ms_overflow_pages);
|
|
|
|
printf(" Entries: %zu\n", ms->ms_entries);
|
2012-10-17 06:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(char *prog)
|
|
|
|
{
|
2013-02-17 02:07:16 +08:00
|
|
|
fprintf(stderr, "usage: %s dbpath [-e] [-f[f[f]]] [-n] [-a|-s subdb]\n", prog);
|
2012-10-17 06:28:20 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i, rc;
|
2011-06-28 15:16:05 +08:00
|
|
|
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;
|
2012-10-17 07:22:21 +08:00
|
|
|
MDB_envinfo mei;
|
2012-10-17 06:28:20 +08:00
|
|
|
char *prog = argv[0];
|
|
|
|
char *envname;
|
2011-06-28 15:16:05 +08:00
|
|
|
char *subname = NULL;
|
2012-10-17 07:52:13 +08:00
|
|
|
int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 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
|
2012-10-17 07:22:21 +08:00
|
|
|
* -e: print env info
|
2012-10-17 07:52:13 +08:00
|
|
|
* -f: print freelist info
|
|
|
|
* -n: use NOSUBDIR flag on env_open
|
2012-10-17 06:28:20 +08:00
|
|
|
* (default) print stat of only the main DB
|
|
|
|
*/
|
2012-10-17 07:52:13 +08:00
|
|
|
while ((i = getopt(argc, argv, "aefns:")) != EOF) {
|
2012-10-17 06:28:20 +08:00
|
|
|
switch(i) {
|
|
|
|
case 'a':
|
2012-12-08 04:06:30 +08:00
|
|
|
if (subname)
|
|
|
|
usage(prog);
|
2012-10-17 06:28:20 +08:00
|
|
|
alldbs++;
|
|
|
|
break;
|
2012-10-17 07:22:21 +08:00
|
|
|
case 'e':
|
|
|
|
envinfo++;
|
|
|
|
break;
|
2012-10-17 07:52:13 +08:00
|
|
|
case 'f':
|
|
|
|
freinfo++;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
envflags |= MDB_NOSUBDIR;
|
|
|
|
break;
|
2012-10-17 06:28:20 +08:00
|
|
|
case 's':
|
2012-12-08 04:06:30 +08:00
|
|
|
if (alldbs)
|
|
|
|
usage(prog);
|
2012-10-17 06:28:20 +08:00
|
|
|
subname = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc - 1)
|
|
|
|
usage(prog);
|
2011-06-28 15:16:05 +08:00
|
|
|
|
2012-10-17 06:28:20 +08:00
|
|
|
envname = argv[optind];
|
2011-08-12 12:14:29 +08:00
|
|
|
rc = mdb_env_create(&env);
|
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);
|
2011-06-28 15:16:05 +08:00
|
|
|
if (rc) {
|
2012-10-17 06:28:20 +08:00
|
|
|
printf("mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
2012-08-24 20:28:36 +08:00
|
|
|
goto env_close;
|
2011-06-28 15:16:05 +08:00
|
|
|
}
|
2012-09-17 22:01:28 +08:00
|
|
|
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
|
2011-08-03 16:41:54 +08:00
|
|
|
if (rc) {
|
2012-10-17 06:28:20 +08:00
|
|
|
printf("mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
|
2012-08-24 20:28:36 +08:00
|
|
|
goto env_close;
|
2011-08-03 16:41:54 +08:00
|
|
|
}
|
2012-10-17 07:22:21 +08:00
|
|
|
|
|
|
|
if (envinfo) {
|
2012-10-17 07:52:13 +08:00
|
|
|
rc = mdb_env_stat(env, &mst);
|
2012-10-17 07:22:21 +08:00
|
|
|
rc = 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);
|
|
|
|
printf(" Map size: %zu\n", mei.me_mapsize);
|
|
|
|
printf(" Page size: %u\n", mst.ms_psize);
|
|
|
|
printf(" Max pages: %zu\n", mei.me_mapsize / mst.ms_psize);
|
|
|
|
printf(" Number of pages used: %zu\n", mei.me_last_pgno+1);
|
|
|
|
printf(" Last transaction ID: %zu\n", mei.me_last_txnid);
|
|
|
|
printf(" Max readers: %u\n", mei.me_maxreaders);
|
|
|
|
printf(" Number of readers used: %u\n", mei.me_numreaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
printf("mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
|
|
goto txn_abort;
|
|
|
|
}
|
|
|
|
rc = mdb_stat(txn, dbi, &mst);
|
|
|
|
if (rc) {
|
|
|
|
printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
|
|
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) {
|
2013-02-17 02:07:16 +08:00
|
|
|
char *bad = "";
|
|
|
|
size_t pg, prev;
|
|
|
|
ssize_t i, j, span = 0;
|
2012-12-08 05:19:36 +08:00
|
|
|
j = *iptr++;
|
2013-02-17 02:07:16 +08:00
|
|
|
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++) ;
|
|
|
|
}
|
|
|
|
printf(" Transaction %zu, %zd pages, maxspan %zd%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++) ;
|
|
|
|
printf(span>1 ? " %9zu[%zd]\n" : " %9zu\n",
|
|
|
|
pg, span);
|
|
|
|
}
|
|
|
|
}
|
2012-12-08 05:19:36 +08:00
|
|
|
}
|
2012-10-17 07:52:13 +08:00
|
|
|
}
|
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
printf(" Free pages: %zu\n", pages);
|
2012-10-17 07:22:21 +08:00
|
|
|
}
|
|
|
|
|
2011-08-03 16:41:54 +08:00
|
|
|
rc = mdb_open(txn, subname, 0, &dbi);
|
2011-06-28 15:16:05 +08:00
|
|
|
if (rc) {
|
2012-10-17 06:28:20 +08:00
|
|
|
printf("mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
2012-08-24 20:28:36 +08:00
|
|
|
goto txn_abort;
|
2011-06-28 15:16:05 +08:00
|
|
|
}
|
|
|
|
|
2011-08-03 16:41:54 +08:00
|
|
|
rc = mdb_stat(txn, dbi, &mst);
|
2012-10-17 06:28:20 +08:00
|
|
|
if (rc) {
|
|
|
|
printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
|
|
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) {
|
|
|
|
printf("mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
|
|
goto txn_abort;
|
|
|
|
}
|
2013-05-22 04:44:51 +08:00
|
|
|
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
|
2013-05-22 05:48:27 +08:00
|
|
|
char *str;
|
2012-10-17 06:28:20 +08:00
|
|
|
MDB_dbi db2;
|
2013-05-22 05:48:27 +08:00
|
|
|
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) {
|
|
|
|
printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
|
|
goto txn_abort;
|
|
|
|
}
|
|
|
|
prstat(&mst);
|
|
|
|
mdb_close(env, db2);
|
|
|
|
}
|
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
}
|
|
|
|
|
2013-05-22 05:48:27 +08:00
|
|
|
if (rc == MDB_NOTFOUND)
|
|
|
|
rc = MDB_SUCCESS;
|
|
|
|
|
2011-09-19 01:38:08 +08:00
|
|
|
mdb_close(env, dbi);
|
2012-08-24 20:28:36 +08:00
|
|
|
txn_abort:
|
2011-08-03 16:41:54 +08:00
|
|
|
mdb_txn_abort(txn);
|
2012-08-24 20:28:36 +08:00
|
|
|
env_close:
|
2011-08-12 12:14:29 +08:00
|
|
|
mdb_env_close(env);
|
2011-06-28 15:16:05 +08:00
|
|
|
|
2012-08-24 20:28:36 +08:00
|
|
|
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
2011-06-28 15:16:05 +08:00
|
|
|
}
|