mdb_stat.c 6.35 KB
Newer Older
Howard Chu's avatar
Howard Chu committed
1
2
/* mdb_stat.c - memory-mapped database status tool */
/*
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
3
 * Copyright 2011-2021 Howard Chu, Symas Corp.
Howard Chu's avatar
Howard Chu committed
4
5
6
7
8
9
10
11
12
13
 * 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>.
 */
Howard Chu's avatar
Howard Chu committed
14
15
#include <stdio.h>
#include <stdlib.h>
Howard Chu's avatar
Howard Chu committed
16
17
#include <string.h>
#include <unistd.h>
Howard Chu's avatar
Howard Chu committed
18
#include "lmdb.h"
Howard Chu's avatar
Howard Chu committed
19

20
#define Z	MDB_FMT_Z
21
#define Yu	MDB_PRIy(u)
22

Howard Chu's avatar
Howard Chu committed
23
static void prstat(MDB_stat *ms)
Howard Chu's avatar
Howard Chu committed
24
{
Howard Chu's avatar
Howard Chu committed
25
26
27
28
#if 0
	printf("  Page size: %u\n", ms->ms_psize);
#endif
	printf("  Tree depth: %u\n", ms->ms_depth);
29
30
31
32
	printf("  Branch pages: %"Yu"\n",   ms->ms_branch_pages);
	printf("  Leaf pages: %"Yu"\n",     ms->ms_leaf_pages);
	printf("  Overflow pages: %"Yu"\n", ms->ms_overflow_pages);
	printf("  Entries: %"Yu"\n",        ms->ms_entries);
Howard Chu's avatar
Howard Chu committed
33
34
35
36
}

static void usage(char *prog)
{
37
	fprintf(stderr, "usage: %s [-V] [-n] [-e] [-r[r]] [-f[f[f]]] [-v] [-a|-s subdb] dbpath\n", prog);
Howard Chu's avatar
Howard Chu committed
38
39
40
41
42
43
	exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
	int i, rc;
Howard Chu's avatar
Howard Chu committed
44
	MDB_env *env;
Howard Chu's avatar
Howard Chu committed
45
46
	MDB_txn *txn;
	MDB_dbi dbi;
Howard Chu's avatar
Howard Chu committed
47
	MDB_stat mst;
48
	MDB_envinfo mei;
Howard Chu's avatar
Howard Chu committed
49
50
	char *prog = argv[0];
	char *envname;
Howard Chu's avatar
Howard Chu committed
51
	char *subname = NULL;
Howard Chu's avatar
Howard Chu committed
52
	int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 0, rdrinfo = 0;
Howard Chu's avatar
Howard Chu committed
53
54
55
56
57
58
59

	if (argc < 2) {
		usage(prog);
	}

	/* -a: print stat of main DB and all subDBs
	 * -s: print stat of only the named subDB
60
	 * -e: print env info
Howard Chu's avatar
Howard Chu committed
61
	 * -f: print freelist info
Howard Chu's avatar
Howard Chu committed
62
	 * -r: print reader info
Howard Chu's avatar
Howard Chu committed
63
	 * -n: use NOSUBDIR flag on env_open
64
	 * -v: use previous snapshot
Howard Chu's avatar
Howard Chu committed
65
	 * -V: print version and exit
Howard Chu's avatar
Howard Chu committed
66
67
	 * (default) print stat of only the main DB
	 */
68
	while ((i = getopt(argc, argv, "Vaefnrs:v")) != EOF) {
Howard Chu's avatar
Howard Chu committed
69
		switch(i) {
Howard Chu's avatar
Howard Chu committed
70
71
72
73
		case 'V':
			printf("%s\n", MDB_VERSION_STRING);
			exit(0);
			break;
Howard Chu's avatar
Howard Chu committed
74
		case 'a':
75
76
			if (subname)
				usage(prog);
Howard Chu's avatar
Howard Chu committed
77
78
			alldbs++;
			break;
79
80
81
		case 'e':
			envinfo++;
			break;
Howard Chu's avatar
Howard Chu committed
82
83
84
85
86
87
		case 'f':
			freinfo++;
			break;
		case 'n':
			envflags |= MDB_NOSUBDIR;
			break;
88
		case 'v':
89
			envflags |= MDB_PREVSNAPSHOT;
90
			break;
Howard Chu's avatar
Howard Chu committed
91
92
93
		case 'r':
			rdrinfo++;
			break;
Howard Chu's avatar
Howard Chu committed
94
		case 's':
95
96
			if (alldbs)
				usage(prog);
Howard Chu's avatar
Howard Chu committed
97
98
99
100
101
102
103
104
105
			subname = optarg;
			break;
		default:
			usage(prog);
		}
	}

	if (optind != argc - 1)
		usage(prog);
Howard Chu's avatar
Howard Chu committed
106

Howard Chu's avatar
Howard Chu committed
107
	envname = argv[optind];
Howard Chu's avatar
Howard Chu committed
108
	rc = mdb_env_create(&env);
Howard Chu's avatar
Howard Chu committed
109
110
111
112
	if (rc) {
		fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
		return EXIT_FAILURE;
	}
Howard Chu's avatar
Howard Chu committed
113

Howard Chu's avatar
Howard Chu committed
114
	if (alldbs || subname) {
Howard Chu's avatar
Howard Chu committed
115
		mdb_env_set_maxdbs(env, 4);
Howard Chu's avatar
Howard Chu committed
116
117
	}

Howard Chu's avatar
Howard Chu committed
118
	rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
Howard Chu's avatar
Howard Chu committed
119
	if (rc) {
120
		fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
121
		goto env_close;
Howard Chu's avatar
Howard Chu committed
122
	}
123
124

	if (envinfo) {
Howard Chu's avatar
Howard Chu committed
125
126
		(void)mdb_env_stat(env, &mst);
		(void)mdb_env_info(env, &mei);
Howard Chu's avatar
Howard Chu committed
127
128
		printf("Environment Info\n");
		printf("  Map address: %p\n", mei.me_mapaddr);
129
		printf("  Map size: %"Yu"\n", mei.me_mapsize);
Howard Chu's avatar
Howard Chu committed
130
		printf("  Page size: %u\n", mst.ms_psize);
131
132
133
		printf("  Max pages: %"Yu"\n", mei.me_mapsize / mst.ms_psize);
		printf("  Number of pages used: %"Yu"\n", mei.me_last_pgno+1);
		printf("  Last transaction ID: %"Yu"\n", mei.me_last_txnid);
Howard Chu's avatar
Howard Chu committed
134
135
136
137
		printf("  Max readers: %u\n", mei.me_maxreaders);
		printf("  Number of readers used: %u\n", mei.me_numreaders);
	}

Howard Chu's avatar
Howard Chu committed
138
139
140
	if (rdrinfo) {
		printf("Reader Table Status\n");
		rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
Howard Chu's avatar
Howard Chu committed
141
142
143
144
145
146
		if (rdrinfo > 1) {
			int dead;
			mdb_reader_check(env, &dead);
			printf("  %d stale readers cleared.\n", dead);
			rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
		}
Howard Chu's avatar
Howard Chu committed
147
148
149
150
151
152
		if (!(subname || alldbs || freinfo))
			goto env_close;
	}

	rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
	if (rc) {
153
		fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
Howard Chu's avatar
Howard Chu committed
154
		goto env_close;
Howard Chu's avatar
Howard Chu committed
155
156
	}

Howard Chu's avatar
Howard Chu committed
157
158
	if (freinfo) {
		MDB_cursor *cursor;
159
		MDB_val key, data;
160
		mdb_size_t pages = 0, *iptr;
Howard Chu's avatar
Howard Chu committed
161
162
163
164
165

		printf("Freelist Status\n");
		dbi = 0;
		rc = mdb_cursor_open(txn, dbi, &cursor);
		if (rc) {
166
			fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
Howard Chu's avatar
Howard Chu committed
167
168
169
170
			goto txn_abort;
		}
		rc = mdb_stat(txn, dbi, &mst);
		if (rc) {
171
			fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
Howard Chu's avatar
Howard Chu committed
172
173
			goto txn_abort;
		}
174
175
		prstat(&mst);
		while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
Howard Chu's avatar
Howard Chu committed
176
177
			iptr = data.mv_data;
			pages += *iptr;
178
			if (freinfo > 1) {
179
				char *bad = "";
180
				mdb_size_t pg, prev;
181
				ssize_t i, j, span = 0;
182
				j = *iptr++;
183
184
185
186
187
188
189
190
				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++) ;
				}
191
				printf("    Transaction %"Yu", %"Z"d pages, maxspan %"Z"d%s\n",
192
					*(mdb_size_t *)key.mv_data, j, span, bad);
193
194
195
196
				if (freinfo > 2) {
					for (--j; j >= 0; ) {
						pg = iptr[j];
						for (span=1; --j >= 0 && iptr[j] == pg+span; span++) ;
197
						printf(span>1 ? "     %9"Yu"[%"Z"d]\n" : "     %9"Yu"\n",
198
199
200
							pg, span);
					}
				}
201
			}
Howard Chu's avatar
Howard Chu committed
202
203
		}
		mdb_cursor_close(cursor);
204
		printf("  Free pages: %"Yu"\n", pages);
205
206
	}

Howard Chu's avatar
Howard Chu committed
207
	rc = mdb_open(txn, subname, 0, &dbi);
Howard Chu's avatar
Howard Chu committed
208
	if (rc) {
209
		fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
210
		goto txn_abort;
Howard Chu's avatar
Howard Chu committed
211
	}
212

Howard Chu's avatar
Howard Chu committed
213
	rc = mdb_stat(txn, dbi, &mst);
Howard Chu's avatar
Howard Chu committed
214
	if (rc) {
215
		fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
Howard Chu's avatar
Howard Chu committed
216
217
		goto txn_abort;
	}
Howard Chu's avatar
Howard Chu committed
218
	printf("Status of %s\n", subname ? subname : "Main DB");
Howard Chu's avatar
Howard Chu committed
219
220
221
222
223
224
225
226
	prstat(&mst);

	if (alldbs) {
		MDB_cursor *cursor;
		MDB_val key;

		rc = mdb_cursor_open(txn, dbi, &cursor);
		if (rc) {
227
			fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
Howard Chu's avatar
Howard Chu committed
228
229
			goto txn_abort;
		}
230
		while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
Hallvard Furuseth's avatar
Hallvard Furuseth committed
231
			char *str;
Howard Chu's avatar
Howard Chu committed
232
			MDB_dbi db2;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
233
234
235
			if (memchr(key.mv_data, '\0', key.mv_size))
				continue;
			str = malloc(key.mv_size+1);
Howard Chu's avatar
Howard Chu committed
236
237
238
239
			memcpy(str, key.mv_data, key.mv_size);
			str[key.mv_size] = '\0';
			rc = mdb_open(txn, str, 0, &db2);
			if (rc == MDB_SUCCESS)
Howard Chu's avatar
Howard Chu committed
240
				printf("Status of %s\n", str);
Howard Chu's avatar
Howard Chu committed
241
242
243
244
			free(str);
			if (rc) continue;
			rc = mdb_stat(txn, db2, &mst);
			if (rc) {
245
				fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
Howard Chu's avatar
Howard Chu committed
246
247
248
249
250
251
252
253
				goto txn_abort;
			}
			prstat(&mst);
			mdb_close(env, db2);
		}
		mdb_cursor_close(cursor);
	}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
254
255
256
	if (rc == MDB_NOTFOUND)
		rc = MDB_SUCCESS;

Howard Chu's avatar
Howard Chu committed
257
	mdb_close(env, dbi);
258
txn_abort:
Howard Chu's avatar
Howard Chu committed
259
	mdb_txn_abort(txn);
260
env_close:
Howard Chu's avatar
Howard Chu committed
261
	mdb_env_close(env);
Howard Chu's avatar
Howard Chu committed
262

263
	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
Howard Chu's avatar
Howard Chu committed
264
}