xref: /netbsd-src/lib/libc/db/btree/bt_debug.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 /*static char *sccsid = "from: @(#)bt_debug.c	8.1 (Berkeley) 6/4/93";*/
39 static char *rcsid = "$Id: bt_debug.c,v 1.3 1993/08/26 00:43:18 jtc Exp $";
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include <sys/param.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include <db.h>
49 #include "btree.h"
50 
51 #ifdef DEBUG
52 /*
53  * BT_DUMP -- Dump the tree
54  *
55  * Parameters:
56  *	dbp:	pointer to the DB
57  */
58 void
59 __bt_dump(dbp)
60 	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 %d",
69 	    ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
70 	if (ISSET(t, R_RECNO))
71 		(void)fprintf(stderr, " keys %lu", t->bt_nrecs);
72 #undef X
73 #define	X(flag, name) \
74 	if (ISSET(t, flag)) { \
75 		(void)fprintf(stderr, "%s%s", sep, name); \
76 		sep = ", "; \
77 	}
78 	if (t->bt_flags) {
79 		sep = " flags (";
80 		X(B_DELCRSR,	"DELCRSR");
81 		X(R_FIXLEN,	"FIXLEN");
82 		X(B_INMEM,	"INMEM");
83 		X(B_NODUPS,	"NODUPS");
84 		X(B_RDONLY,	"RDONLY");
85 		X(R_RECNO,	"RECNO");
86 		X(B_SEQINIT,	"SEQINIT");
87 		X(B_METADIRTY,"METADIRTY");
88 		(void)fprintf(stderr, ")\n");
89 	}
90 #undef X
91 
92 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
93 		__bt_dpage(h);
94 		(void)mpool_put(t->bt_mp, h, 0);
95 	}
96 }
97 
98 /*
99  * BT_DMPAGE -- Dump the meta page
100  *
101  * Parameters:
102  *	h:	pointer to the PAGE
103  */
104 void
105 __bt_dmpage(h)
106 	PAGE *h;
107 {
108 	BTMETA *m;
109 	char *sep;
110 
111 	m = (BTMETA *)h;
112 	(void)fprintf(stderr, "magic %lx\n", m->m_magic);
113 	(void)fprintf(stderr, "version %lu\n", m->m_version);
114 	(void)fprintf(stderr, "psize %lu\n", m->m_psize);
115 	(void)fprintf(stderr, "free %lu\n", m->m_free);
116 	(void)fprintf(stderr, "nrecs %lu\n", m->m_nrecs);
117 	(void)fprintf(stderr, "flags %lu", m->m_flags);
118 #undef X
119 #define	X(flag, name) \
120 	if (m->m_flags & flag) { \
121 		(void)fprintf(stderr, "%s%s", sep, name); \
122 		sep = ", "; \
123 	}
124 	if (m->m_flags) {
125 		sep = " (";
126 		X(B_NODUPS,	"NODUPS");
127 		X(R_RECNO,	"RECNO");
128 		(void)fprintf(stderr, ")");
129 	}
130 }
131 
132 /*
133  * BT_DNPAGE -- Dump the page
134  *
135  * Parameters:
136  *	n:	page number to dump.
137  */
138 void
139 __bt_dnpage(dbp, pgno)
140 	DB *dbp;
141 	pgno_t pgno;
142 {
143 	BTREE *t;
144 	PAGE *h;
145 
146 	t = dbp->internal;
147 	if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
148 		__bt_dpage(h);
149 		(void)mpool_put(t->bt_mp, h, 0);
150 	}
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 %d: (", 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 %2d next %2d", 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 %lu size %u/",
217 				    *(pgno_t *)bl->bytes,
218 				    *(size_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 %lu size %u",
224 				    *(pgno_t *)(bl->bytes + bl->ksize),
225 				    *(size_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 %lu size %u",
236 				    *(pgno_t *)rl->bytes,
237 				    *(size_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;
260 	extern u_long bt_rootsplit, bt_split, bt_sortsplit;
261 	extern u_long bt_pfxsaved;
262 	BTREE *t;
263 	PAGE *h;
264 	pgno_t i, pcont, pinternal, pleaf;
265 	u_long ifree, lfree, nkeys;
266 	int levels;
267 
268 	t = dbp->internal;
269 	pcont = pinternal = pleaf = 0;
270 	nkeys = ifree = lfree = 0;
271 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != 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 		(void)mpool_put(t->bt_mp, h, 0);
289 	}
290 
291 	/* Count the levels of the tree. */
292 	for (i = P_ROOT, levels = 0 ;; ++levels) {
293 		h = mpool_get(t->bt_mp, i, 0);
294 		if (h->flags & (P_BLEAF|P_RLEAF)) {
295 			if (levels == 0)
296 				levels = 1;
297 			(void)mpool_put(t->bt_mp, h, 0);
298 			break;
299 		}
300 		i = ISSET(t, R_RECNO) ?
301 		    GETRINTERNAL(h, 0)->pgno :
302 		    GETBINTERNAL(h, 0)->pgno;
303 		(void)mpool_put(t->bt_mp, h, 0);
304 	}
305 
306 	(void)fprintf(stderr, "%d level%s with %ld keys",
307 	    levels, levels == 1 ? "" : "s", nkeys);
308 	if (ISSET(t, R_RECNO))
309 		(void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
310 	(void)fprintf(stderr,
311 	    "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
312 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
313 	(void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
314 	    bt_cache_hit, bt_cache_miss);
315 	(void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
316 	    bt_split, bt_rootsplit, bt_sortsplit);
317 	pleaf *= t->bt_psize - BTDATAOFF;
318 	if (pleaf)
319 		(void)fprintf(stderr,
320 		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
321 		    ((double)(pleaf - lfree) / pleaf) * 100,
322 		    pleaf - lfree, lfree);
323 	pinternal *= t->bt_psize - BTDATAOFF;
324 	if (pinternal)
325 		(void)fprintf(stderr,
326 		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
327 		    ((double)(pinternal - ifree) / pinternal) * 100,
328 		    pinternal - ifree, ifree);
329 	if (bt_pfxsaved)
330 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
331 		    bt_pfxsaved);
332 }
333 #endif
334