xref: /openbsd-src/lib/libc/db/btree/bt_debug.c (revision e20a56a53b18fb9479a580438ff1988d9a3762a7)
1 /*	$OpenBSD: bt_debug.c,v 1.8 2005/03/23 19:34:58 otto 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 defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char sccsid[] = "@(#)bt_debug.c	8.5 (Berkeley) 8/17/94";
38 #else
39 static const char rcsid[] = "$OpenBSD: bt_debug.c,v 1.8 2005/03/23 19:34:58 otto Exp $";
40 #endif
41 #endif /* LIBC_SCCS and not lint */
42 
43 #include <sys/param.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include <db.h>
50 #include "btree.h"
51 
52 #ifdef DEBUG
53 /*
54  * BT_DUMP -- Dump the tree
55  *
56  * Parameters:
57  *	dbp:	pointer to the DB
58  */
59 void
60 __bt_dump(DB *dbp)
61 {
62 	BTREE *t;
63 	PAGE *h;
64 	pgno_t i;
65 	char *sep;
66 
67 	t = dbp->internal;
68 	(void)fprintf(stderr, "%s: pgsz %u",
69 	    F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
70 	if (F_ISSET(t, R_RECNO))
71 		(void)fprintf(stderr, " keys %u", t->bt_nrecs);
72 #undef X
73 #define	X(flag, name) \
74 	if (F_ISSET(t, flag)) { \
75 		(void)fprintf(stderr, "%s%s", sep, name); \
76 		sep = ", "; \
77 	}
78 	if (t->flags != 0) {
79 		sep = " flags (";
80 		X(R_FIXLEN,	"FIXLEN");
81 		X(B_INMEM,	"INMEM");
82 		X(B_NODUPS,	"NODUPS");
83 		X(B_RDONLY,	"RDONLY");
84 		X(R_RECNO,	"RECNO");
85 		X(B_METADIRTY,"METADIRTY");
86 		(void)fprintf(stderr, ")\n");
87 	}
88 #undef X
89 
90 	for (i = P_ROOT;
91 	    (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
92 		__bt_dpage(h);
93 }
94 
95 /*
96  * BT_DMPAGE -- Dump the meta page
97  *
98  * Parameters:
99  *	h:	pointer to the PAGE
100  */
101 void
102 __bt_dmpage(PAGE *h)
103 {
104 	BTMETA *m;
105 	char *sep;
106 
107 	m = (BTMETA *)h;
108 	(void)fprintf(stderr, "magic %x\n", m->magic);
109 	(void)fprintf(stderr, "version %u\n", m->version);
110 	(void)fprintf(stderr, "psize %u\n", m->psize);
111 	(void)fprintf(stderr, "free %u\n", m->free);
112 	(void)fprintf(stderr, "nrecs %u\n", m->nrecs);
113 	(void)fprintf(stderr, "flags %u", m->flags);
114 #undef X
115 #define	X(flag, name) \
116 	if (m->flags & flag) { \
117 		(void)fprintf(stderr, "%s%s", sep, name); \
118 		sep = ", "; \
119 	}
120 	if (m->flags) {
121 		sep = " (";
122 		X(B_NODUPS,	"NODUPS");
123 		X(R_RECNO,	"RECNO");
124 		(void)fprintf(stderr, ")");
125 	}
126 }
127 
128 /*
129  * BT_DNPAGE -- Dump the page
130  *
131  * Parameters:
132  *	n:	page number to dump.
133  */
134 void
135 __bt_dnpage(DB *dbp, pgno_t pgno)
136 {
137 	BTREE *t;
138 	PAGE *h;
139 
140 	t = dbp->internal;
141 	if ((h = mpool_get(t->bt_mp, pgno, MPOOL_IGNOREPIN)) != NULL)
142 		__bt_dpage(h);
143 }
144 
145 /*
146  * BT_DPAGE -- Dump the page
147  *
148  * Parameters:
149  *	h:	pointer to the PAGE
150  */
151 void
152 __bt_dpage(PAGE *h)
153 {
154 	BINTERNAL *bi;
155 	BLEAF *bl;
156 	RINTERNAL *ri;
157 	RLEAF *rl;
158 	indx_t cur, top;
159 	char *sep;
160 
161 	(void)fprintf(stderr, "    page %u: (", h->pgno);
162 #undef X
163 #define	X(flag, name) \
164 	if (h->flags & flag) { \
165 		(void)fprintf(stderr, "%s%s", sep, name); \
166 		sep = ", "; \
167 	}
168 	sep = "";
169 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
170 	X(P_BLEAF,	"BLEAF")
171 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
172 	X(P_RLEAF,	"RLEAF")
173 	X(P_OVERFLOW,	"OVERFLOW")
174 	X(P_PRESERVE,	"PRESERVE");
175 	(void)fprintf(stderr, ")\n");
176 #undef X
177 
178 	(void)fprintf(stderr, "\tprev %2u next %2u", h->prevpg, h->nextpg);
179 	if (h->flags & P_OVERFLOW)
180 		return;
181 
182 	top = NEXTINDEX(h);
183 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
184 	    h->lower, h->upper, top);
185 	for (cur = 0; cur < top; cur++) {
186 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
187 		switch (h->flags & P_TYPE) {
188 		case P_BINTERNAL:
189 			bi = GETBINTERNAL(h, cur);
190 			(void)fprintf(stderr,
191 			    "size %03d pgno %03d", bi->ksize, bi->pgno);
192 			if (bi->flags & P_BIGKEY)
193 				(void)fprintf(stderr, " (indirect)");
194 			else if (bi->ksize)
195 				(void)fprintf(stderr,
196 				    " {%.*s}", (int)bi->ksize, bi->bytes);
197 			break;
198 		case P_RINTERNAL:
199 			ri = GETRINTERNAL(h, cur);
200 			(void)fprintf(stderr, "entries %03d pgno %03d",
201 				ri->nrecs, ri->pgno);
202 			break;
203 		case P_BLEAF:
204 			bl = GETBLEAF(h, cur);
205 			if (bl->flags & P_BIGKEY)
206 				(void)fprintf(stderr,
207 				    "big key page %u size %u/",
208 				    *(pgno_t *)bl->bytes,
209 				    *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
210 			else if (bl->ksize)
211 				(void)fprintf(stderr, "%s/", bl->bytes);
212 			if (bl->flags & P_BIGDATA)
213 				(void)fprintf(stderr,
214 				    "big data page %u size %u",
215 				    *(pgno_t *)(bl->bytes + bl->ksize),
216 				    *(u_int32_t *)(bl->bytes + bl->ksize +
217 				    sizeof(pgno_t)));
218 			else if (bl->dsize)
219 				(void)fprintf(stderr, "%.*s",
220 				    (int)bl->dsize, bl->bytes + bl->ksize);
221 			break;
222 		case P_RLEAF:
223 			rl = GETRLEAF(h, cur);
224 			if (rl->flags & P_BIGDATA)
225 				(void)fprintf(stderr,
226 				    "big data page %u size %u",
227 				    *(pgno_t *)rl->bytes,
228 				    *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
229 			else if (rl->dsize)
230 				(void)fprintf(stderr,
231 				    "%.*s", (int)rl->dsize, rl->bytes);
232 			break;
233 		}
234 		(void)fprintf(stderr, "\n");
235 	}
236 }
237 #endif
238 
239 #ifdef STATISTICS
240 /*
241  * BT_STAT -- Gather/print the tree statistics
242  *
243  * Parameters:
244  *	dbp:	pointer to the DB
245  */
246 void
247 __bt_stat(DB *dbp)
248 {
249 	extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
250 	extern u_long bt_sortsplit, bt_split;
251 	BTREE *t;
252 	PAGE *h;
253 	pgno_t i, pcont, pinternal, pleaf;
254 	u_long ifree, lfree, nkeys;
255 	int levels;
256 
257 	t = dbp->internal;
258 	pcont = pinternal = pleaf = 0;
259 	nkeys = ifree = lfree = 0;
260 	for (i = P_ROOT;
261 	    (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
262 		switch (h->flags & P_TYPE) {
263 		case P_BINTERNAL:
264 		case P_RINTERNAL:
265 			++pinternal;
266 			ifree += h->upper - h->lower;
267 			break;
268 		case P_BLEAF:
269 		case P_RLEAF:
270 			++pleaf;
271 			lfree += h->upper - h->lower;
272 			nkeys += NEXTINDEX(h);
273 			break;
274 		case P_OVERFLOW:
275 			++pcont;
276 			break;
277 		}
278 
279 	/* Count the levels of the tree. */
280 	for (i = P_ROOT, levels = 0 ;; ++levels) {
281 		h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN);
282 		if (h->flags & (P_BLEAF|P_RLEAF)) {
283 			if (levels == 0)
284 				levels = 1;
285 			break;
286 		}
287 		i = F_ISSET(t, R_RECNO) ?
288 		    GETRINTERNAL(h, 0)->pgno :
289 		    GETBINTERNAL(h, 0)->pgno;
290 	}
291 
292 	(void)fprintf(stderr, "%d level%s with %lu keys",
293 	    levels, levels == 1 ? "" : "s", nkeys);
294 	if (F_ISSET(t, R_RECNO))
295 		(void)fprintf(stderr, " (%u header count)", t->bt_nrecs);
296 	(void)fprintf(stderr,
297 	    "\n%u pages (leaf %u, internal %u, overflow %u)\n",
298 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
299 	(void)fprintf(stderr, "%lu cache hits, %lu cache misses\n",
300 	    bt_cache_hit, bt_cache_miss);
301 	(void)fprintf(stderr, "%lu splits (%lu root splits, %lu sort splits)\n",
302 	    bt_split, bt_rootsplit, bt_sortsplit);
303 	pleaf *= t->bt_psize - BTDATAOFF;
304 	if (pleaf)
305 		(void)fprintf(stderr,
306 		    "%.0f%% leaf fill (%lu bytes used, %lu bytes free)\n",
307 		    ((double)(pleaf - lfree) / pleaf) * 100,
308 		    pleaf - lfree, lfree);
309 	pinternal *= t->bt_psize - BTDATAOFF;
310 	if (pinternal)
311 		(void)fprintf(stderr,
312 		    "%.0f%% internal fill (%lu bytes used, %lu bytes free\n",
313 		    ((double)(pinternal - ifree) / pinternal) * 100,
314 		    pinternal - ifree, ifree);
315 	if (bt_pfxsaved)
316 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
317 		    bt_pfxsaved);
318 }
319 #endif
320