bt_debug.c revision 165903
11844Swollman/*-
25486Swollman * Copyright (c) 1990, 1993, 1994
31638Srgrimes *	The Regents of the University of California.  All rights reserved.
41844Swollman *
51844Swollman * This code is derived from software contributed to Berkeley by
61638Srgrimes * Mike Olson.
71844Swollman *
81638Srgrimes * Redistribution and use in source and binary forms, with or without
91638Srgrimes * modification, are permitted provided that the following conditions
101638Srgrimes * are met:
111638Srgrimes * 1. Redistributions of source code must retain the above copyright
121638Srgrimes *    notice, this list of conditions and the following disclaimer.
135479Swollman * 2. Redistributions in binary form must reproduce the above copyright
141638Srgrimes *    notice, this list of conditions and the following disclaimer in the
151638Srgrimes *    documentation and/or other materials provided with the distribution.
161638Srgrimes * 4. Neither the name of the University nor the names of its contributors
175479Swollman *    may be used to endorse or promote products derived from this software
185479Swollman *    without specific prior written permission.
195479Swollman *
205479Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
215479Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
225479Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
235479Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
245479Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
255479Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
265479Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
275479Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
285479Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
295479Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
305479Swollman * SUCH DAMAGE.
315479Swollman */
325479Swollman
335479Swollman#if defined(LIBC_SCCS) && !defined(lint)
345479Swollmanstatic char sccsid[] = "@(#)bt_debug.c	8.5 (Berkeley) 8/17/94";
355479Swollman#endif /* LIBC_SCCS and not lint */
361844Swollman#include <sys/cdefs.h>
371844Swollman__FBSDID("$FreeBSD: head/lib/libc/db/btree/bt_debug.c 165903 2007-01-09 00:28:16Z imp $");
381844Swollman
391844Swollman#include <sys/param.h>
401844Swollman
411844Swollman#include <stdio.h>
425479Swollman#include <stdlib.h>
431638Srgrimes#include <string.h>
441844Swollman
451638Srgrimes#include <db.h>
461638Srgrimes#include "btree.h"
471844Swollman
481844Swollman#ifdef DEBUG
491638Srgrimes/*
501638Srgrimes * BT_DUMP -- Dump the tree
511638Srgrimes *
521844Swollman * Parameters:
531638Srgrimes *	dbp:	pointer to the DB
541844Swollman */
551844Swollmanvoid
561844Swollman__bt_dump(dbp)
571844Swollman	DB *dbp;
581844Swollman{
591844Swollman	BTREE *t;
602353Sbde	PAGE *h;
611844Swollman	pgno_t i;
621844Swollman	char *sep;
631844Swollman
641844Swollman	t = dbp->internal;
651844Swollman	(void)fprintf(stderr, "%s: pgsz %d",
661844Swollman	    F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
671844Swollman	if (F_ISSET(t, R_RECNO))
681844Swollman		(void)fprintf(stderr, " keys %u", t->bt_nrecs);
692353Sbde#undef X
701844Swollman#define	X(flag, name) \
711844Swollman	if (F_ISSET(t, flag)) { \
721844Swollman		(void)fprintf(stderr, "%s%s", sep, name); \
731844Swollman		sep = ", "; \
741638Srgrimes	}
751844Swollman	if (t->flags != 0) {
761638Srgrimes		sep = " flags (";
771844Swollman		X(R_FIXLEN,	"FIXLEN");
785381Sache		X(B_INMEM,	"INMEM");
791638Srgrimes		X(B_NODUPS,	"NODUPS");
805381Sache		X(B_RDONLY,	"RDONLY");
811844Swollman		X(R_RECNO,	"RECNO");
821844Swollman		X(B_METADIRTY,"METADIRTY");
831638Srgrimes		(void)fprintf(stderr, ")\n");
845486Swollman	}
851844Swollman#undef X
861844Swollman
871844Swollman	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
881844Swollman		__bt_dpage(h);
891844Swollman		(void)mpool_put(t->bt_mp, h, 0);
901844Swollman	}
911844Swollman}
921844Swollman
935388Sache/*
945388Sache * BT_DMPAGE -- Dump the meta page
951638Srgrimes *
965486Swollman * Parameters:
975486Swollman *	h:	pointer to the PAGE
985486Swollman */
995486Swollmanvoid
1005486Swollman__bt_dmpage(h)
1015486Swollman	PAGE *h;
1025486Swollman{
1035486Swollman	BTMETA *m;
1045486Swollman	char *sep;
1055486Swollman
1065486Swollman	m = (BTMETA *)h;
1074442Sphk	(void)fprintf(stderr, "magic %x\n", m->magic);
1084442Sphk	(void)fprintf(stderr, "version %u\n", m->version);
1094442Sphk	(void)fprintf(stderr, "psize %u\n", m->psize);
1104442Sphk	(void)fprintf(stderr, "free %u\n", m->free);
1114442Sphk	(void)fprintf(stderr, "nrecs %u\n", m->nrecs);
1124442Sphk	(void)fprintf(stderr, "flags %u", m->flags);
1131638Srgrimes#undef X
1141844Swollman#define	X(flag, name) \
1151844Swollman	if (m->flags & flag) { \
1161638Srgrimes		(void)fprintf(stderr, "%s%s", sep, name); \
1171638Srgrimes		sep = ", "; \
1185388Sache	}
1195479Swollman	if (m->flags) {
1205479Swollman		sep = " (";
1215479Swollman		X(B_NODUPS,	"NODUPS");
1225479Swollman		X(R_RECNO,	"RECNO");
1235479Swollman		(void)fprintf(stderr, ")");
1245479Swollman	}
1255479Swollman}
1265479Swollman
1275479Swollman/*
1285479Swollman * BT_DNPAGE -- Dump the page
1295479Swollman *
1305479Swollman * Parameters:
1315479Swollman *	n:	page number to dump.
1325479Swollman */
1335479Swollmanvoid
1345479Swollman__bt_dnpage(dbp, pgno)
1355485Swollman	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 */
154void
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 %u 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/",
216				    bl->ksize, bl->bytes);
217			if (bl->flags & P_BIGDATA)
218				(void)fprintf(stderr,
219				    "big data page %u size %u",
220				    *(pgno_t *)(bl->bytes + bl->ksize),
221				    *(u_int32_t *)(bl->bytes + bl->ksize +
222				    sizeof(pgno_t)));
223			else if (bl->dsize)
224				(void)fprintf(stderr, "%.*s",
225				    (int)bl->dsize, bl->bytes + bl->ksize);
226			break;
227		case P_RLEAF:
228			rl = GETRLEAF(h, cur);
229			if (rl->flags & P_BIGDATA)
230				(void)fprintf(stderr,
231				    "big data page %u size %u",
232				    *(pgno_t *)rl->bytes,
233				    *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
234			else if (rl->dsize)
235				(void)fprintf(stderr,
236				    "%.*s", (int)rl->dsize, rl->bytes);
237			break;
238		}
239		(void)fprintf(stderr, "\n");
240	}
241}
242#endif
243
244#ifdef STATISTICS
245/*
246 * BT_STAT -- Gather/print the tree statistics
247 *
248 * Parameters:
249 *	dbp:	pointer to the DB
250 */
251void
252__bt_stat(dbp)
253	DB *dbp;
254{
255	extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
256	extern u_long bt_sortsplit, bt_split;
257	BTREE *t;
258	PAGE *h;
259	pgno_t i, pcont, pinternal, pleaf;
260	u_long ifree, lfree, nkeys;
261	int levels;
262
263	t = dbp->internal;
264	pcont = pinternal = pleaf = 0;
265	nkeys = ifree = lfree = 0;
266	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
267		switch (h->flags & P_TYPE) {
268		case P_BINTERNAL:
269		case P_RINTERNAL:
270			++pinternal;
271			ifree += h->upper - h->lower;
272			break;
273		case P_BLEAF:
274		case P_RLEAF:
275			++pleaf;
276			lfree += h->upper - h->lower;
277			nkeys += NEXTINDEX(h);
278			break;
279		case P_OVERFLOW:
280			++pcont;
281			break;
282		}
283		(void)mpool_put(t->bt_mp, h, 0);
284	}
285
286	/* Count the levels of the tree. */
287	for (i = P_ROOT, levels = 0 ;; ++levels) {
288		h = mpool_get(t->bt_mp, i, 0);
289		if (h->flags & (P_BLEAF|P_RLEAF)) {
290			if (levels == 0)
291				levels = 1;
292			(void)mpool_put(t->bt_mp, h, 0);
293			break;
294		}
295		i = F_ISSET(t, R_RECNO) ?
296		    GETRINTERNAL(h, 0)->pgno :
297		    GETBINTERNAL(h, 0)->pgno;
298		(void)mpool_put(t->bt_mp, h, 0);
299	}
300
301	(void)fprintf(stderr, "%d level%s with %ld keys",
302	    levels, levels == 1 ? "" : "s", nkeys);
303	if (F_ISSET(t, R_RECNO))
304		(void)fprintf(stderr, " (%d header count)", t->bt_nrecs);
305	(void)fprintf(stderr,
306	    "\n%u pages (leaf %d, internal %d, overflow %d)\n",
307	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
308	(void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
309	    bt_cache_hit, bt_cache_miss);
310	(void)fprintf(stderr, "%lu splits (%lu root splits, %lu sort splits)\n",
311	    bt_split, bt_rootsplit, bt_sortsplit);
312	pleaf *= t->bt_psize - BTDATAOFF;
313	if (pleaf)
314		(void)fprintf(stderr,
315		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
316		    ((double)(pleaf - lfree) / pleaf) * 100,
317		    pleaf - lfree, lfree);
318	pinternal *= t->bt_psize - BTDATAOFF;
319	if (pinternal)
320		(void)fprintf(stderr,
321		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
322		    ((double)(pinternal - ifree) / pinternal) * 100,
323		    pinternal - ifree, ifree);
324	if (bt_pfxsaved)
325		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
326		    bt_pfxsaved);
327}
328#endif
329