mdb_stat.c (6401B)
1 /* mdb_stat.c - memory-mapped database status tool */ 2 /* 3 * Copyright 2011-2021 Howard Chu, Symas Corp. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted only as authorized by the OpenLDAP 8 * Public License. 9 * 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>. 13 */ 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <unistd.h> 18 #include "lmdb.h" 19 20 #ifdef _WIN32 21 #define Z "I" 22 #else 23 #define Z "z" 24 #endif 25 26 static void prstat(MDB_stat *ms) 27 { 28 #if 0 29 printf(" Page size: %u\n", ms->ms_psize); 30 #endif 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); 36 } 37 38 static void usage(char *prog) 39 { 40 fprintf(stderr, "usage: %s [-V] [-n] [-e] [-r[r]] [-f[f[f]]] [-a|-s subdb] dbpath\n", prog); 41 exit(EXIT_FAILURE); 42 } 43 44 int main(int argc, char *argv[]) 45 { 46 int i, rc; 47 MDB_env *env; 48 MDB_txn *txn; 49 MDB_dbi dbi; 50 MDB_stat mst; 51 MDB_envinfo mei; 52 char *prog = argv[0]; 53 char *envname; 54 char *subname = NULL; 55 int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 0, rdrinfo = 0; 56 57 if (argc < 2) { 58 usage(prog); 59 } 60 61 /* -a: print stat of main DB and all subDBs 62 * -s: print stat of only the named subDB 63 * -e: print env info 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 69 */ 70 while ((i = getopt(argc, argv, "Vaefnrs:")) != EOF) { 71 switch(i) { 72 case 'V': 73 printf("%s\n", MDB_VERSION_STRING); 74 exit(0); 75 break; 76 case 'a': 77 if (subname) 78 usage(prog); 79 alldbs++; 80 break; 81 case 'e': 82 envinfo++; 83 break; 84 case 'f': 85 freinfo++; 86 break; 87 case 'n': 88 envflags |= MDB_NOSUBDIR; 89 break; 90 case 'r': 91 rdrinfo++; 92 break; 93 case 's': 94 if (alldbs) 95 usage(prog); 96 subname = optarg; 97 break; 98 default: 99 usage(prog); 100 } 101 } 102 103 if (optind != argc - 1) 104 usage(prog); 105 106 envname = argv[optind]; 107 rc = mdb_env_create(&env); 108 if (rc) { 109 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc)); 110 return EXIT_FAILURE; 111 } 112 113 if (alldbs || subname) { 114 mdb_env_set_maxdbs(env, 4); 115 } 116 117 rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664); 118 if (rc) { 119 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc)); 120 goto env_close; 121 } 122 123 if (envinfo) { 124 (void)mdb_env_stat(env, &mst); 125 (void)mdb_env_info(env, &mei); 126 printf("Environment Info\n"); 127 printf(" Map address: %p\n", mei.me_mapaddr); 128 printf(" Map size: %"Z"u\n", mei.me_mapsize); 129 printf(" Page size: %u\n", mst.ms_psize); 130 printf(" Max pages: %"Z"u\n", mei.me_mapsize / mst.ms_psize); 131 printf(" Number of pages used: %"Z"u\n", mei.me_last_pgno+1); 132 printf(" Last transaction ID: %"Z"u\n", mei.me_last_txnid); 133 printf(" Max readers: %u\n", mei.me_maxreaders); 134 printf(" Number of readers used: %u\n", mei.me_numreaders); 135 } 136 137 if (rdrinfo) { 138 printf("Reader Table Status\n"); 139 rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout); 140 if (rdrinfo > 1) { 141 int dead; 142 mdb_reader_check(env, &dead); 143 printf(" %d stale readers cleared.\n", dead); 144 rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout); 145 } 146 if (!(subname || alldbs || freinfo)) 147 goto env_close; 148 } 149 150 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); 151 if (rc) { 152 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc)); 153 goto env_close; 154 } 155 156 if (freinfo) { 157 MDB_cursor *cursor; 158 MDB_val key, data; 159 size_t pages = 0, *iptr; 160 161 printf("Freelist Status\n"); 162 dbi = 0; 163 rc = mdb_cursor_open(txn, dbi, &cursor); 164 if (rc) { 165 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc)); 166 goto txn_abort; 167 } 168 rc = mdb_stat(txn, dbi, &mst); 169 if (rc) { 170 fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc)); 171 goto txn_abort; 172 } 173 prstat(&mst); 174 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { 175 iptr = data.mv_data; 176 pages += *iptr; 177 if (freinfo > 1) { 178 char *bad = ""; 179 size_t pg, prev; 180 ssize_t i, j, span = 0; 181 j = *iptr++; 182 for (i = j, prev = 1; --i >= 0; ) { 183 pg = iptr[i]; 184 if (pg <= prev) 185 bad = " [bad sequence]"; 186 prev = pg; 187 pg += span; 188 for (; i >= span && iptr[i-span] == pg; span++, pg++) ; 189 } 190 printf(" Transaction %"Z"u, %"Z"d pages, maxspan %"Z"d%s\n", 191 *(size_t *)key.mv_data, j, span, bad); 192 if (freinfo > 2) { 193 for (--j; j >= 0; ) { 194 pg = iptr[j]; 195 for (span=1; --j >= 0 && iptr[j] == pg+span; span++) ; 196 printf(span>1 ? " %9"Z"u[%"Z"d]\n" : " %9"Z"u\n", 197 pg, span); 198 } 199 } 200 } 201 } 202 mdb_cursor_close(cursor); 203 printf(" Free pages: %"Z"u\n", pages); 204 } 205 206 rc = mdb_open(txn, subname, 0, &dbi); 207 if (rc) { 208 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc)); 209 goto txn_abort; 210 } 211 212 rc = mdb_stat(txn, dbi, &mst); 213 if (rc) { 214 fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc)); 215 goto txn_abort; 216 } 217 printf("Status of %s\n", subname ? subname : "Main DB"); 218 prstat(&mst); 219 220 if (alldbs) { 221 MDB_cursor *cursor; 222 MDB_val key; 223 224 rc = mdb_cursor_open(txn, dbi, &cursor); 225 if (rc) { 226 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc)); 227 goto txn_abort; 228 } 229 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) { 230 char *str; 231 MDB_dbi db2; 232 if (memchr(key.mv_data, '\0', key.mv_size)) 233 continue; 234 str = malloc(key.mv_size+1); 235 memcpy(str, key.mv_data, key.mv_size); 236 str[key.mv_size] = '\0'; 237 rc = mdb_open(txn, str, 0, &db2); 238 if (rc == MDB_SUCCESS) 239 printf("Status of %s\n", str); 240 free(str); 241 if (rc) continue; 242 rc = mdb_stat(txn, db2, &mst); 243 if (rc) { 244 fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc)); 245 goto txn_abort; 246 } 247 prstat(&mst); 248 mdb_close(env, db2); 249 } 250 mdb_cursor_close(cursor); 251 } 252 253 if (rc == MDB_NOTFOUND) 254 rc = MDB_SUCCESS; 255 256 mdb_close(env, dbi); 257 txn_abort: 258 mdb_txn_abort(txn); 259 env_close: 260 mdb_env_close(env); 261 262 return rc ? EXIT_FAILURE : EXIT_SUCCESS; 263 }