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