openldap/libraries/libmdb/mdb_stat.c

156 lines
3.6 KiB
C
Raw Normal View History

/* mdb_stat.c - memory-mapped database status tool */
/*
* Copyright 2011 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 "mdb.h"
2012-10-17 06:28:20 +08:00
static void prstat(MDB_stat *ms)
{
2012-10-17 06:28:20 +08:00
printf("Page size: %u\n", ms->ms_psize);
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);
}
static void usage(char *prog)
{
fprintf(stderr, "usage: %s dbpath [-a|-s subdb]\n", prog);
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;
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 06:28:20 +08:00
* (default) print stat of only the main DB
*/
while ((i = getopt(argc, argv, "aes:")) != EOF) {
2012-10-17 06:28:20 +08:00
switch(i) {
case 'a':
alldbs++;
break;
case 'e':
envinfo++;
break;
2012-10-17 06:28:20 +08:00
case 's':
subname = optarg;
break;
default:
fprintf(stderr, "%s: unrecognized option -%c\n", prog, optopt);
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);
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 06:28:20 +08:00
rc = mdb_env_open(env, envname, MDB_RDONLY, 0664);
if (rc) {
2012-10-17 06:28:20 +08:00
printf("mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto env_close;
}
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));
goto env_close;
2011-08-03 16:41:54 +08:00
}
if (envinfo) {
rc = mdb_env_info(env, &mei);
printf("Map size: %zu \n", mei.me_mapsize);
printf("Last transaction ID: %zu\n", mei.me_last_txnid);
printf("Last page used: %zu\n", mei.me_last_pgno);
printf("Max readers: %u\n", mei.me_maxreaders);
printf("Number of readers used: %u\n", mei.me_numreaders);
}
2011-08-03 16:41:54 +08:00
rc = mdb_open(txn, subname, 0, &dbi);
if (rc) {
2012-10-17 06:28:20 +08:00
printf("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) {
printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort;
}
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;
}
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT)) == 0) {
char *str = malloc(key.mv_size+1);
MDB_dbi db2;
memcpy(str, key.mv_data, key.mv_size);
str[key.mv_size] = '\0';
rc = mdb_open(txn, str, 0, &db2);
if (rc == MDB_SUCCESS)
printf("\n%s\n", str);
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);
}
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;
}