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