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