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