mdb_stat.c 6.25 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
21
22
23
24
25
#ifdef	_WIN32
#define	Z	"I"
#else
#define	Z	"z"
#endif

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

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

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

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

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

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

Howard Chu's avatar
Howard Chu committed
106
	envname = argv[optind];
Howard Chu's avatar
Howard Chu committed
107
	rc = mdb_env_create(&env);
Howard Chu's avatar
Howard Chu committed
108
109
110
111
	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
112

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

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

	if (envinfo) {
Howard Chu's avatar
Howard Chu committed
124
125
		(void)mdb_env_stat(env, &mst);
		(void)mdb_env_info(env, &mei);
Howard Chu's avatar
Howard Chu committed
126
127
		printf("Environment Info\n");
		printf("  Map address: %p\n", mei.me_mapaddr);
128
		printf("  Map size: %"Z"u\n", mei.me_mapsize);
Howard Chu's avatar
Howard Chu committed
129
		printf("  Page size: %u\n", mst.ms_psize);
130
131
132
		printf("  Max pages: %"Z"u\n", mei.me_mapsize / mst.ms_psize);
		printf("  Number of pages used: %"Z"u\n", mei.me_last_pgno+1);
		printf("  Last transaction ID: %"Z"u\n", mei.me_last_txnid);
Howard Chu's avatar
Howard Chu committed
133
134
135
136
		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
137
138
139
	if (rdrinfo) {
		printf("Reader Table Status\n");
		rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
Howard Chu's avatar
Howard Chu committed
140
141
142
143
144
145
		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
146
147
148
149
150
151
		if (!(subname || alldbs || freinfo))
			goto env_close;
	}

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

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

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

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

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

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

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

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

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

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