xref: /minix3/lib/libc/db/btree/bt_debug.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1 /*	$NetBSD: bt_debug.c,v 1.16 2011/07/17 20:47:39 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Olson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38 
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: bt_debug.c,v 1.16 2011/07/17 20:47:39 christos Exp $");
41 
42 #include <assert.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include <db.h>
48 #include "btree.h"
49 
50 #ifdef DEBUG
51 /*
52  * BT_DUMP -- Dump the tree
53  *
54  * Parameters:
55  *	dbp:	pointer to the DB
56  */
57 void
__bt_dump(DB * dbp)58 __bt_dump(DB *dbp)
59 {
60 	BTREE *t;
61 	PAGE *h;
62 	pgno_t i;
63 	const char *sep;
64 
65 	t = dbp->internal;
66 	(void)fprintf(stderr, "%s: pgsz %d",
67 	    F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
68 	if (F_ISSET(t, R_RECNO))
69 		(void)fprintf(stderr, " keys %lu", (unsigned long) t->bt_nrecs);
70 #undef X
71 #define	X(flag, name) \
72 	if (F_ISSET(t, flag)) { \
73 		(void)fprintf(stderr, "%s%s", sep, name); \
74 		sep = ", "; \
75 	}
76 	if (t->flags != 0) {
77 		sep = " flags (";
78 		X(R_FIXLEN,	"FIXLEN");
79 		X(B_INMEM,	"INMEM");
80 		X(B_NODUPS,	"NODUPS");
81 		X(B_RDONLY,	"RDONLY");
82 		X(R_RECNO,	"RECNO");
83 		X(B_METADIRTY,"METADIRTY");
84 		(void)fprintf(stderr, ")\n");
85 	}
86 #undef X
87 
88 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
89 		__bt_dpage(h);
90 		(void)mpool_put(t->bt_mp, h, 0);
91 	}
92 }
93 
94 /*
95  * BT_DMPAGE -- Dump the meta page
96  *
97  * Parameters:
98  *	h:	pointer to the PAGE
99  */
100 void
__bt_dmpage(PAGE * h)101 __bt_dmpage(PAGE *h)
102 {
103 	BTMETA *m;
104 	const char *sep;
105 
106 	m = (BTMETA *)(void *)h;
107 	(void)fprintf(stderr, "magic %lx\n", (unsigned long) m->magic);
108 	(void)fprintf(stderr, "version %lu\n", (unsigned long) m->version);
109 	(void)fprintf(stderr, "psize %lu\n", (unsigned long) m->psize);
110 	(void)fprintf(stderr, "free %lu\n", (unsigned long) m->free);
111 	(void)fprintf(stderr, "nrecs %lu\n", (unsigned long) m->nrecs);
112 	(void)fprintf(stderr, "flags %lu", (unsigned long) m->flags);
113 #undef X
114 #define	X(flag, name) \
115 	if (m->flags & flag) { \
116 		(void)fprintf(stderr, "%s%s", sep, name); \
117 		sep = ", "; \
118 	}
119 	if (m->flags) {
120 		sep = " (";
121 		X(B_NODUPS,	"NODUPS");
122 		X(R_RECNO,	"RECNO");
123 		(void)fprintf(stderr, ")");
124 	}
125 }
126 
127 static pgno_t
__bt_pgno_t(const void * m)128 __bt_pgno_t(const void *m)
129 {
130 	pgno_t r;
131 	memcpy(&r, m, sizeof(r));
132 	return r;
133 }
134 
135 static uint32_t
__bt_uint32_t(const void * m)136 __bt_uint32_t(const void *m)
137 {
138 	uint32_t r;
139 	memcpy(&r, m, sizeof(r));
140 	return r;
141 }
142 
143 /*
144  * BT_DNPAGE -- Dump the page
145  *
146  * Parameters:
147  *	n:	page number to dump.
148  */
149 void
__bt_dnpage(DB * dbp,pgno_t pgno)150 __bt_dnpage(DB *dbp, pgno_t pgno)
151 {
152 	BTREE *t;
153 	PAGE *h;
154 
155 	t = dbp->internal;
156 	if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
157 		__bt_dpage(h);
158 		(void)mpool_put(t->bt_mp, h, 0);
159 	}
160 }
161 
162 /*
163  * BT_DPAGE -- Dump the page
164  *
165  * Parameters:
166  *	h:	pointer to the PAGE
167  */
168 void
__bt_dpage(PAGE * h)169 __bt_dpage(PAGE *h)
170 {
171 	BINTERNAL *bi;
172 	BLEAF *bl;
173 	RINTERNAL *ri;
174 	RLEAF *rl;
175 	indx_t cur, top;
176 	const char *sep;
177 
178 	(void)fprintf(stderr, "    page %d: (", h->pgno);
179 #undef X
180 #define	X(flag, name) \
181 	if (h->flags & flag) { \
182 		(void)fprintf(stderr, "%s%s", sep, name); \
183 		sep = ", "; \
184 	}
185 	sep = "";
186 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
187 	X(P_BLEAF,	"BLEAF")
188 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
189 	X(P_RLEAF,	"RLEAF")
190 	X(P_OVERFLOW,	"OVERFLOW")
191 	X(P_PRESERVE,	"PRESERVE");
192 	(void)fprintf(stderr, ")\n");
193 #undef X
194 
195 	(void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
196 	if (h->flags & P_OVERFLOW)
197 		return;
198 
199 	top = NEXTINDEX(h);
200 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
201 	    h->lower, h->upper, top);
202 	for (cur = 0; cur < top; cur++) {
203 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
204 		switch (h->flags & P_TYPE) {
205 		case P_BINTERNAL:
206 			bi = GETBINTERNAL(h, cur);
207 			(void)fprintf(stderr,
208 			    "size %03d pgno %03d", bi->ksize, bi->pgno);
209 			if (bi->flags & P_BIGKEY)
210 				(void)fprintf(stderr, " (indirect)");
211 			else if (bi->ksize)
212 				(void)fprintf(stderr,
213 				    " {%.*s}", (int)bi->ksize, bi->bytes);
214 			break;
215 		case P_RINTERNAL:
216 			ri = GETRINTERNAL(h, cur);
217 			(void)fprintf(stderr, "entries %03d pgno %03d",
218 				ri->nrecs, ri->pgno);
219 			break;
220 		case P_BLEAF:
221 			bl = GETBLEAF(h, cur);
222 			if (bl->flags & P_BIGKEY)
223 				(void)fprintf(stderr,
224 				    "big key page %lu size %u/",
225 				    (unsigned long) __bt_pgno_t(bl->bytes),
226 				    __bt_uint32_t(bl->bytes + sizeof(pgno_t)));
227 			else if (bl->ksize)
228 				(void)fprintf(stderr, "%s/", bl->bytes);
229 			if (bl->flags & P_BIGDATA)
230 				(void)fprintf(stderr,
231 				    "big data page %lu size %u",
232 				    (unsigned long)
233 				    __bt_pgno_t(bl->bytes + bl->ksize),
234 				    __bt_uint32_t(bl->bytes + bl->ksize +
235 				    sizeof(pgno_t)));
236 			else if (bl->dsize)
237 				(void)fprintf(stderr, "%.*s",
238 				    (int)bl->dsize, bl->bytes + bl->ksize);
239 			break;
240 		case P_RLEAF:
241 			rl = GETRLEAF(h, cur);
242 			if (rl->flags & P_BIGDATA)
243 				(void)fprintf(stderr,
244 				    "big data page %lu size %u",
245 				    (unsigned long) __bt_pgno_t(rl->bytes),
246 				    __bt_uint32_t(rl->bytes + sizeof(pgno_t)));
247 			else if (rl->dsize)
248 				(void)fprintf(stderr,
249 				    "%.*s", (int)rl->dsize, rl->bytes);
250 			break;
251 		}
252 		(void)fprintf(stderr, "\n");
253 	}
254 }
255 #endif
256 
257 #ifdef STATISTICS
258 /*
259  * BT_STAT -- Gather/print the tree statistics
260  *
261  * Parameters:
262  *	dbp:	pointer to the DB
263  */
264 void
__bt_stat(DB * dbp)265 __bt_stat(DB *dbp)
266 {
267 	extern unsigned long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
268 	extern unsigned long bt_sortsplit, bt_split;
269 	BTREE *t;
270 	PAGE *h;
271 	pgno_t i, pcont, pinternal, pleaf;
272 	unsigned long ifree, lfree, nkeys;
273 	int levels;
274 
275 	t = dbp->internal;
276 	pcont = pinternal = pleaf = 0;
277 	nkeys = ifree = lfree = 0;
278 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
279 		switch (h->flags & P_TYPE) {
280 		case P_BINTERNAL:
281 		case P_RINTERNAL:
282 			++pinternal;
283 			ifree += h->upper - h->lower;
284 			break;
285 		case P_BLEAF:
286 		case P_RLEAF:
287 			++pleaf;
288 			lfree += h->upper - h->lower;
289 			nkeys += NEXTINDEX(h);
290 			break;
291 		case P_OVERFLOW:
292 			++pcont;
293 			break;
294 		}
295 		(void)mpool_put(t->bt_mp, h, 0);
296 	}
297 
298 	/* Count the levels of the tree. */
299 	for (i = P_ROOT, levels = 0 ;; ++levels) {
300 		h = mpool_get(t->bt_mp, i, 0);
301 		if (h->flags & (P_BLEAF|P_RLEAF)) {
302 			if (levels == 0)
303 				levels = 1;
304 			(void)mpool_put(t->bt_mp, h, 0);
305 			break;
306 		}
307 		i = F_ISSET(t, R_RECNO) ?
308 		    GETRINTERNAL(h, 0)->pgno :
309 		    GETBINTERNAL(h, 0)->pgno;
310 		(void)mpool_put(t->bt_mp, h, 0);
311 	}
312 
313 	(void)fprintf(stderr, "%d level%s with %ld keys",
314 	    levels, levels == 1 ? "" : "s", nkeys);
315 	if (F_ISSET(t, R_RECNO))
316 		(void)fprintf(stderr, " (%ld header count)", (long)t->bt_nrecs);
317 	(void)fprintf(stderr,
318 	    "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
319 	    (long)pinternal + pleaf + pcont, (long)pleaf, (long)pinternal,
320 	    (long)pcont);
321 	(void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
322 	    bt_cache_hit, bt_cache_miss);
323 	(void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
324 	    bt_split, bt_rootsplit, bt_sortsplit);
325 	pleaf *= t->bt_psize - BTDATAOFF;
326 	if (pleaf)
327 		(void)fprintf(stderr,
328 		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
329 		    ((double)(pleaf - lfree) / pleaf) * 100,
330 		    pleaf - lfree, lfree);
331 	pinternal *= t->bt_psize - BTDATAOFF;
332 	if (pinternal)
333 		(void)fprintf(stderr,
334 		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
335 		    ((double)(pinternal - ifree) / pinternal) * 100,
336 		    pinternal - ifree, ifree);
337 	if (bt_pfxsaved)
338 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
339 		    bt_pfxsaved);
340 }
341 #endif
342