1 /* mdb_stat.c - memory-mapped database status tool */
3 * Copyright 2011-2013 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>.
26 static void prstat(MDB_stat *ms)
29 printf(" Page size: %u\n", ms->ms_psize);
31 printf(" Tree depth: %u\n", ms->ms_depth);
32 printf(" Branch pages: %"Z"u\n", ms->ms_branch_pages);
33 printf(" Leaf pages: %"Z"u\n", ms->ms_leaf_pages);
34 printf(" Overflow pages: %"Z"u\n", ms->ms_overflow_pages);
35 printf(" Entries: %"Z"u\n", ms->ms_entries);
38 static void usage(char *prog)
40 fprintf(stderr, "usage: %s dbpath [-V] [-n] [-e] [-r[r]] [-f[f[f]]] [-a|-s subdb]\n", prog);
44 int main(int argc, char *argv[])
55 int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 0, rdrinfo = 0;
61 /* -a: print stat of main DB and all subDBs
62 * -s: print stat of only the named subDB
64 * -f: print freelist info
65 * -r: print reader info
66 * -n: use NOSUBDIR flag on env_open
67 * -V: print version and exit
68 * (default) print stat of only the main DB
70 while ((i = getopt(argc, argv, "Vaefnrs:")) != EOF) {
73 printf("%s\n", MDB_VERSION_STRING);
88 envflags |= MDB_NOSUBDIR;
103 if (optind != argc - 1)
106 envname = argv[optind];
107 rc = mdb_env_create(&env);
109 if (alldbs || subname) {
110 mdb_env_set_maxdbs(env, 4);
113 rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
115 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
120 rc = mdb_env_stat(env, &mst);
121 rc = mdb_env_info(env, &mei);
122 printf("Environment Info\n");
123 printf(" Map address: %p\n", mei.me_mapaddr);
124 printf(" Map size: %"Z"u\n", mei.me_mapsize);
125 printf(" Page size: %u\n", mst.ms_psize);
126 printf(" Max pages: %"Z"u\n", mei.me_mapsize / mst.ms_psize);
127 printf(" Number of pages used: %"Z"u\n", mei.me_last_pgno+1);
128 printf(" Last transaction ID: %"Z"u\n", mei.me_last_txnid);
129 printf(" Max readers: %u\n", mei.me_maxreaders);
130 printf(" Number of readers used: %u\n", mei.me_numreaders);
134 printf("Reader Table Status\n");
135 rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
138 mdb_reader_check(env, &dead);
139 printf(" %d stale readers cleared.\n", dead);
140 rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
142 if (!(subname || alldbs || freinfo))
146 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
148 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
155 size_t pages = 0, *iptr;
157 printf("Freelist Status\n");
159 rc = mdb_cursor_open(txn, dbi, &cursor);
161 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
164 rc = mdb_stat(txn, dbi, &mst);
166 fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
170 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
176 ssize_t i, j, span = 0;
178 for (i = j, prev = 1; --i >= 0; ) {
181 bad = " [bad sequence]";
184 for (; i >= span && iptr[i-span] == pg; span++, pg++) ;
186 printf(" Transaction %"Z"u, %"Z"d pages, maxspan %"Z"d%s\n",
187 *(size_t *)key.mv_data, j, span, bad);
189 for (--j; j >= 0; ) {
191 for (span=1; --j >= 0 && iptr[j] == pg+span; span++) ;
192 printf(span>1 ? " %9"Z"u[%"Z"d]\n" : " %9"Z"u\n",
198 mdb_cursor_close(cursor);
199 printf(" Free pages: %"Z"u\n", pages);
202 rc = mdb_open(txn, subname, 0, &dbi);
204 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
208 rc = mdb_stat(txn, dbi, &mst);
210 fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
213 printf("Status of %s\n", subname ? subname : "Main DB");
220 rc = mdb_cursor_open(txn, dbi, &cursor);
222 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
225 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
228 if (memchr(key.mv_data, '\0', key.mv_size))
230 str = malloc(key.mv_size+1);
231 memcpy(str, key.mv_data, key.mv_size);
232 str[key.mv_size] = '\0';
233 rc = mdb_open(txn, str, 0, &db2);
234 if (rc == MDB_SUCCESS)
235 printf("Status of %s\n", str);
238 rc = mdb_stat(txn, db2, &mst);
240 fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
246 mdb_cursor_close(cursor);
249 if (rc == MDB_NOTFOUND)
258 return rc ? EXIT_FAILURE : EXIT_SUCCESS;