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