1 /* mdb_stat.c - memory-mapped database status tool */
3 * Copyright 2011 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
10 * A copy of this license is available in the file LICENSE in the
11 * top-level directory of the distribution or, alternatively, at
12 * <http://www.OpenLDAP.org/license.html>.
20 static void prstat(MDB_stat *ms)
23 printf(" Page size: %u\n", ms->ms_psize);
25 printf(" Tree depth: %u\n", ms->ms_depth);
26 printf(" Branch pages: %zu\n", ms->ms_branch_pages);
27 printf(" Leaf pages: %zu\n", ms->ms_leaf_pages);
28 printf(" Overflow pages: %zu\n", ms->ms_overflow_pages);
29 printf(" Entries: %zu\n", ms->ms_entries);
32 static void usage(char *prog)
34 fprintf(stderr, "usage: %s dbpath [-e] [-f] [-n] [-a|-s subdb]\n", prog);
38 int main(int argc, char *argv[])
49 int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 0;
55 /* -a: print stat of main DB and all subDBs
56 * -s: print stat of only the named subDB
58 * -f: print freelist info
59 * -n: use NOSUBDIR flag on env_open
60 * (default) print stat of only the main DB
62 while ((i = getopt(argc, argv, "aefns:")) != EOF) {
74 envflags |= MDB_NOSUBDIR;
84 if (optind != argc - 1)
87 envname = argv[optind];
88 rc = mdb_env_create(&env);
90 if (alldbs || subname) {
91 mdb_env_set_maxdbs(env, 4);
94 rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
96 printf("mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
99 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
101 printf("mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
106 rc = mdb_env_stat(env, &mst);
107 rc = mdb_env_info(env, &mei);
108 printf("Environment Info\n");
109 printf(" Map address: %p\n", mei.me_mapaddr);
110 printf(" Map size: %zu\n", mei.me_mapsize);
111 printf(" Page size: %u\n", mst.ms_psize);
112 printf(" Max pages: %zu\n", mei.me_mapsize / mst.ms_psize);
113 printf(" Number of pages used: %zu\n", mei.me_last_pgno+1);
114 printf(" Last transaction ID: %zu\n", mei.me_last_txnid);
115 printf(" Max readers: %u\n", mei.me_maxreaders);
116 printf(" Number of readers used: %u\n", mei.me_numreaders);
122 size_t pages = 0, *iptr;
124 printf("Freelist Status\n");
126 rc = mdb_cursor_open(txn, dbi, &cursor);
128 printf("mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
131 rc = mdb_stat(txn, dbi, &mst);
133 printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
136 while ((rc = mdb_cursor_get(cursor, NULL, &data, MDB_NEXT)) == 0) {
140 mdb_cursor_close(cursor);
142 printf(" Free pages: %zu\n", pages);
145 rc = mdb_open(txn, subname, 0, &dbi);
147 printf("mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
151 rc = mdb_stat(txn, dbi, &mst);
153 printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
156 printf("Status of %s\n", subname ? subname : "Main DB");
163 rc = mdb_cursor_open(txn, dbi, &cursor);
165 printf("mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
168 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT)) == 0) {
169 char *str = malloc(key.mv_size+1);
171 memcpy(str, key.mv_data, key.mv_size);
172 str[key.mv_size] = '\0';
173 rc = mdb_open(txn, str, 0, &db2);
174 if (rc == MDB_SUCCESS)
175 printf("Status of %s\n", str);
178 rc = mdb_stat(txn, db2, &mst);
180 printf("mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
186 mdb_cursor_close(cursor);
195 return rc ? EXIT_FAILURE : EXIT_SUCCESS;