xref: /freebsd/lib/libc/db/btree/bt_debug.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ef5d438eSPaul Traina  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Mike Olson.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes #include <sys/param.h>
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #include <stdio.h>
3858f0484fSRodney W. Grimes #include <stdlib.h>
3958f0484fSRodney W. Grimes #include <string.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <db.h>
4258f0484fSRodney W. Grimes #include "btree.h"
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes #ifdef DEBUG
4558f0484fSRodney W. Grimes /*
4658f0484fSRodney W. Grimes  * BT_DUMP -- Dump the tree
4758f0484fSRodney W. Grimes  *
4858f0484fSRodney W. Grimes  * Parameters:
4958f0484fSRodney W. Grimes  *	dbp:	pointer to the DB
5058f0484fSRodney W. Grimes  */
5158f0484fSRodney W. Grimes void
__bt_dump(DB * dbp)520ac22237SXin LI __bt_dump(DB *dbp)
5358f0484fSRodney W. Grimes {
5458f0484fSRodney W. Grimes 	BTREE *t;
5558f0484fSRodney W. Grimes 	PAGE *h;
5658f0484fSRodney W. Grimes 	pgno_t i;
5758f0484fSRodney W. Grimes 	char *sep;
5858f0484fSRodney W. Grimes 
5958f0484fSRodney W. Grimes 	t = dbp->internal;
607efabbb9SXin LI 	(void)fprintf(stderr, "%s: pgsz %u",
61ef5d438eSPaul Traina 	    F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
62ef5d438eSPaul Traina 	if (F_ISSET(t, R_RECNO))
63312f1851SJun Kuriyama 		(void)fprintf(stderr, " keys %u", t->bt_nrecs);
6458f0484fSRodney W. Grimes #undef X
6558f0484fSRodney W. Grimes #define	X(flag, name) \
66ef5d438eSPaul Traina 	if (F_ISSET(t, flag)) { \
6758f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s%s", sep, name); \
6858f0484fSRodney W. Grimes 		sep = ", "; \
6958f0484fSRodney W. Grimes 	}
70ef5d438eSPaul Traina 	if (t->flags != 0) {
7158f0484fSRodney W. Grimes 		sep = " flags (";
7258f0484fSRodney W. Grimes 		X(R_FIXLEN,	"FIXLEN");
7358f0484fSRodney W. Grimes 		X(B_INMEM,	"INMEM");
7458f0484fSRodney W. Grimes 		X(B_NODUPS,	"NODUPS");
7558f0484fSRodney W. Grimes 		X(B_RDONLY,	"RDONLY");
7658f0484fSRodney W. Grimes 		X(R_RECNO,	"RECNO");
7758f0484fSRodney W. Grimes 		X(B_METADIRTY,"METADIRTY");
7858f0484fSRodney W. Grimes 		(void)fprintf(stderr, ")\n");
7958f0484fSRodney W. Grimes 	}
8058f0484fSRodney W. Grimes #undef X
8158f0484fSRodney W. Grimes 
829fc74a87SXin LI 	for (i = P_ROOT;
839fc74a87SXin LI 	    (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
8458f0484fSRodney W. Grimes 		__bt_dpage(h);
8558f0484fSRodney W. Grimes }
8658f0484fSRodney W. Grimes 
8758f0484fSRodney W. Grimes /*
8858f0484fSRodney W. Grimes  * BT_DMPAGE -- Dump the meta page
8958f0484fSRodney W. Grimes  *
9058f0484fSRodney W. Grimes  * Parameters:
9158f0484fSRodney W. Grimes  *	h:	pointer to the PAGE
9258f0484fSRodney W. Grimes  */
9358f0484fSRodney W. Grimes void
__bt_dmpage(PAGE * h)940ac22237SXin LI __bt_dmpage(PAGE *h)
9558f0484fSRodney W. Grimes {
9658f0484fSRodney W. Grimes 	BTMETA *m;
9758f0484fSRodney W. Grimes 	char *sep;
9858f0484fSRodney W. Grimes 
9958f0484fSRodney W. Grimes 	m = (BTMETA *)h;
100312f1851SJun Kuriyama 	(void)fprintf(stderr, "magic %x\n", m->magic);
101312f1851SJun Kuriyama 	(void)fprintf(stderr, "version %u\n", m->version);
102312f1851SJun Kuriyama 	(void)fprintf(stderr, "psize %u\n", m->psize);
103312f1851SJun Kuriyama 	(void)fprintf(stderr, "free %u\n", m->free);
104312f1851SJun Kuriyama 	(void)fprintf(stderr, "nrecs %u\n", m->nrecs);
105312f1851SJun Kuriyama 	(void)fprintf(stderr, "flags %u", m->flags);
10658f0484fSRodney W. Grimes #undef X
10758f0484fSRodney W. Grimes #define	X(flag, name) \
108ef5d438eSPaul Traina 	if (m->flags & flag) { \
10958f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s%s", sep, name); \
11058f0484fSRodney W. Grimes 		sep = ", "; \
11158f0484fSRodney W. Grimes 	}
112ef5d438eSPaul Traina 	if (m->flags) {
11358f0484fSRodney W. Grimes 		sep = " (";
11458f0484fSRodney W. Grimes 		X(B_NODUPS,	"NODUPS");
11558f0484fSRodney W. Grimes 		X(R_RECNO,	"RECNO");
11658f0484fSRodney W. Grimes 		(void)fprintf(stderr, ")");
11758f0484fSRodney W. Grimes 	}
11858f0484fSRodney W. Grimes }
11958f0484fSRodney W. Grimes 
12058f0484fSRodney W. Grimes /*
12158f0484fSRodney W. Grimes  * BT_DNPAGE -- Dump the page
12258f0484fSRodney W. Grimes  *
12358f0484fSRodney W. Grimes  * Parameters:
12458f0484fSRodney W. Grimes  *	n:	page number to dump.
12558f0484fSRodney W. Grimes  */
12658f0484fSRodney W. Grimes void
__bt_dnpage(DB * dbp,pgno_t pgno)1270ac22237SXin LI __bt_dnpage(DB *dbp, pgno_t pgno)
12858f0484fSRodney W. Grimes {
12958f0484fSRodney W. Grimes 	BTREE *t;
13058f0484fSRodney W. Grimes 	PAGE *h;
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes 	t = dbp->internal;
1339fc74a87SXin LI 	if ((h = mpool_get(t->bt_mp, pgno, MPOOL_IGNOREPIN)) != NULL)
13458f0484fSRodney W. Grimes 		__bt_dpage(h);
13558f0484fSRodney W. Grimes }
13658f0484fSRodney W. Grimes 
13758f0484fSRodney W. Grimes /*
13858f0484fSRodney W. Grimes  * BT_DPAGE -- Dump the page
13958f0484fSRodney W. Grimes  *
14058f0484fSRodney W. Grimes  * Parameters:
14158f0484fSRodney W. Grimes  *	h:	pointer to the PAGE
14258f0484fSRodney W. Grimes  */
14358f0484fSRodney W. Grimes void
__bt_dpage(PAGE * h)1440ac22237SXin LI __bt_dpage(PAGE *h)
14558f0484fSRodney W. Grimes {
14658f0484fSRodney W. Grimes 	BINTERNAL *bi;
14758f0484fSRodney W. Grimes 	BLEAF *bl;
14858f0484fSRodney W. Grimes 	RINTERNAL *ri;
14958f0484fSRodney W. Grimes 	RLEAF *rl;
15058f0484fSRodney W. Grimes 	indx_t cur, top;
15158f0484fSRodney W. Grimes 	char *sep;
15258f0484fSRodney W. Grimes 
1537efabbb9SXin LI 	(void)fprintf(stderr, "    page %u: (", h->pgno);
15458f0484fSRodney W. Grimes #undef X
15558f0484fSRodney W. Grimes #define	X(flag, name) \
15658f0484fSRodney W. Grimes 	if (h->flags & flag) { \
15758f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s%s", sep, name); \
15858f0484fSRodney W. Grimes 		sep = ", "; \
15958f0484fSRodney W. Grimes 	}
16058f0484fSRodney W. Grimes 	sep = "";
16158f0484fSRodney W. Grimes 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
16258f0484fSRodney W. Grimes 	X(P_BLEAF,	"BLEAF")
16358f0484fSRodney W. Grimes 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
16458f0484fSRodney W. Grimes 	X(P_RLEAF,	"RLEAF")
16558f0484fSRodney W. Grimes 	X(P_OVERFLOW,	"OVERFLOW")
16658f0484fSRodney W. Grimes 	X(P_PRESERVE,	"PRESERVE");
16758f0484fSRodney W. Grimes 	(void)fprintf(stderr, ")\n");
16858f0484fSRodney W. Grimes #undef X
16958f0484fSRodney W. Grimes 
1707efabbb9SXin LI 	(void)fprintf(stderr, "\tprev %2u next %2u", h->prevpg, h->nextpg);
17158f0484fSRodney W. Grimes 	if (h->flags & P_OVERFLOW)
17258f0484fSRodney W. Grimes 		return;
17358f0484fSRodney W. Grimes 
17458f0484fSRodney W. Grimes 	top = NEXTINDEX(h);
17558f0484fSRodney W. Grimes 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
17658f0484fSRodney W. Grimes 	    h->lower, h->upper, top);
17758f0484fSRodney W. Grimes 	for (cur = 0; cur < top; cur++) {
17858f0484fSRodney W. Grimes 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
17958f0484fSRodney W. Grimes 		switch (h->flags & P_TYPE) {
18058f0484fSRodney W. Grimes 		case P_BINTERNAL:
18158f0484fSRodney W. Grimes 			bi = GETBINTERNAL(h, cur);
18258f0484fSRodney W. Grimes 			(void)fprintf(stderr,
18358f0484fSRodney W. Grimes 			    "size %03d pgno %03d", bi->ksize, bi->pgno);
18458f0484fSRodney W. Grimes 			if (bi->flags & P_BIGKEY)
18558f0484fSRodney W. Grimes 				(void)fprintf(stderr, " (indirect)");
18658f0484fSRodney W. Grimes 			else if (bi->ksize)
18758f0484fSRodney W. Grimes 				(void)fprintf(stderr,
18858f0484fSRodney W. Grimes 				    " {%.*s}", (int)bi->ksize, bi->bytes);
18958f0484fSRodney W. Grimes 			break;
19058f0484fSRodney W. Grimes 		case P_RINTERNAL:
19158f0484fSRodney W. Grimes 			ri = GETRINTERNAL(h, cur);
19258f0484fSRodney W. Grimes 			(void)fprintf(stderr, "entries %03d pgno %03d",
19358f0484fSRodney W. Grimes 				ri->nrecs, ri->pgno);
19458f0484fSRodney W. Grimes 			break;
19558f0484fSRodney W. Grimes 		case P_BLEAF:
19658f0484fSRodney W. Grimes 			bl = GETBLEAF(h, cur);
19758f0484fSRodney W. Grimes 			if (bl->flags & P_BIGKEY)
19858f0484fSRodney W. Grimes 				(void)fprintf(stderr,
199312f1851SJun Kuriyama 				    "big key page %u size %u/",
20058f0484fSRodney W. Grimes 				    *(pgno_t *)bl->bytes,
201ef5d438eSPaul Traina 				    *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
20258f0484fSRodney W. Grimes 			else if (bl->ksize)
203312f1851SJun Kuriyama 				(void)fprintf(stderr, "%.*s/",
204312f1851SJun Kuriyama 				    bl->ksize, bl->bytes);
20558f0484fSRodney W. Grimes 			if (bl->flags & P_BIGDATA)
20658f0484fSRodney W. Grimes 				(void)fprintf(stderr,
207312f1851SJun Kuriyama 				    "big data page %u size %u",
20858f0484fSRodney W. Grimes 				    *(pgno_t *)(bl->bytes + bl->ksize),
209ef5d438eSPaul Traina 				    *(u_int32_t *)(bl->bytes + bl->ksize +
21058f0484fSRodney W. Grimes 				    sizeof(pgno_t)));
21158f0484fSRodney W. Grimes 			else if (bl->dsize)
21258f0484fSRodney W. Grimes 				(void)fprintf(stderr, "%.*s",
21358f0484fSRodney W. Grimes 				    (int)bl->dsize, bl->bytes + bl->ksize);
21458f0484fSRodney W. Grimes 			break;
21558f0484fSRodney W. Grimes 		case P_RLEAF:
21658f0484fSRodney W. Grimes 			rl = GETRLEAF(h, cur);
21758f0484fSRodney W. Grimes 			if (rl->flags & P_BIGDATA)
21858f0484fSRodney W. Grimes 				(void)fprintf(stderr,
219312f1851SJun Kuriyama 				    "big data page %u size %u",
22058f0484fSRodney W. Grimes 				    *(pgno_t *)rl->bytes,
221ef5d438eSPaul Traina 				    *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
22258f0484fSRodney W. Grimes 			else if (rl->dsize)
22358f0484fSRodney W. Grimes 				(void)fprintf(stderr,
22458f0484fSRodney W. Grimes 				    "%.*s", (int)rl->dsize, rl->bytes);
22558f0484fSRodney W. Grimes 			break;
22658f0484fSRodney W. Grimes 		}
22758f0484fSRodney W. Grimes 		(void)fprintf(stderr, "\n");
22858f0484fSRodney W. Grimes 	}
22958f0484fSRodney W. Grimes }
23058f0484fSRodney W. Grimes #endif
23158f0484fSRodney W. Grimes 
23258f0484fSRodney W. Grimes #ifdef STATISTICS
23358f0484fSRodney W. Grimes /*
23458f0484fSRodney W. Grimes  * BT_STAT -- Gather/print the tree statistics
23558f0484fSRodney W. Grimes  *
23658f0484fSRodney W. Grimes  * Parameters:
23758f0484fSRodney W. Grimes  *	dbp:	pointer to the DB
23858f0484fSRodney W. Grimes  */
23958f0484fSRodney W. Grimes void
__bt_stat(DB * dbp)2400ac22237SXin LI __bt_stat(DB *dbp)
24158f0484fSRodney W. Grimes {
24258f0484fSRodney W. Grimes 	extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
24358f0484fSRodney W. Grimes 	extern u_long bt_sortsplit, bt_split;
24458f0484fSRodney W. Grimes 	BTREE *t;
24558f0484fSRodney W. Grimes 	PAGE *h;
24658f0484fSRodney W. Grimes 	pgno_t i, pcont, pinternal, pleaf;
24758f0484fSRodney W. Grimes 	u_long ifree, lfree, nkeys;
24858f0484fSRodney W. Grimes 	int levels;
24958f0484fSRodney W. Grimes 
25058f0484fSRodney W. Grimes 	t = dbp->internal;
25158f0484fSRodney W. Grimes 	pcont = pinternal = pleaf = 0;
25258f0484fSRodney W. Grimes 	nkeys = ifree = lfree = 0;
2539fc74a87SXin LI 	for (i = P_ROOT;
2549fc74a87SXin LI 	    (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
25558f0484fSRodney W. Grimes 		switch (h->flags & P_TYPE) {
25658f0484fSRodney W. Grimes 		case P_BINTERNAL:
25758f0484fSRodney W. Grimes 		case P_RINTERNAL:
25858f0484fSRodney W. Grimes 			++pinternal;
25958f0484fSRodney W. Grimes 			ifree += h->upper - h->lower;
26058f0484fSRodney W. Grimes 			break;
26158f0484fSRodney W. Grimes 		case P_BLEAF:
26258f0484fSRodney W. Grimes 		case P_RLEAF:
26358f0484fSRodney W. Grimes 			++pleaf;
26458f0484fSRodney W. Grimes 			lfree += h->upper - h->lower;
26558f0484fSRodney W. Grimes 			nkeys += NEXTINDEX(h);
26658f0484fSRodney W. Grimes 			break;
26758f0484fSRodney W. Grimes 		case P_OVERFLOW:
26858f0484fSRodney W. Grimes 			++pcont;
26958f0484fSRodney W. Grimes 			break;
27058f0484fSRodney W. Grimes 		}
27158f0484fSRodney W. Grimes 
27258f0484fSRodney W. Grimes 	/* Count the levels of the tree. */
27358f0484fSRodney W. Grimes 	for (i = P_ROOT, levels = 0 ;; ++levels) {
2749fc74a87SXin LI 		h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN);
27558f0484fSRodney W. Grimes 		if (h->flags & (P_BLEAF|P_RLEAF)) {
27658f0484fSRodney W. Grimes 			if (levels == 0)
27758f0484fSRodney W. Grimes 				levels = 1;
27858f0484fSRodney W. Grimes 			break;
27958f0484fSRodney W. Grimes 		}
280ef5d438eSPaul Traina 		i = F_ISSET(t, R_RECNO) ?
28158f0484fSRodney W. Grimes 		    GETRINTERNAL(h, 0)->pgno :
28258f0484fSRodney W. Grimes 		    GETBINTERNAL(h, 0)->pgno;
28358f0484fSRodney W. Grimes 	}
28458f0484fSRodney W. Grimes 
2857efabbb9SXin LI 	(void)fprintf(stderr, "%d level%s with %lu keys",
28658f0484fSRodney W. Grimes 	    levels, levels == 1 ? "" : "s", nkeys);
287ef5d438eSPaul Traina 	if (F_ISSET(t, R_RECNO))
2887efabbb9SXin LI 		(void)fprintf(stderr, " (%u header count)", t->bt_nrecs);
28958f0484fSRodney W. Grimes 	(void)fprintf(stderr,
2907efabbb9SXin LI 	    "\n%u pages (leaf %u, internal %u, overflow %u)\n",
29158f0484fSRodney W. Grimes 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
2927efabbb9SXin LI 	(void)fprintf(stderr, "%lu cache hits, %lu cache misses\n",
29358f0484fSRodney W. Grimes 	    bt_cache_hit, bt_cache_miss);
294312f1851SJun Kuriyama 	(void)fprintf(stderr, "%lu splits (%lu root splits, %lu sort splits)\n",
29558f0484fSRodney W. Grimes 	    bt_split, bt_rootsplit, bt_sortsplit);
29658f0484fSRodney W. Grimes 	pleaf *= t->bt_psize - BTDATAOFF;
29758f0484fSRodney W. Grimes 	if (pleaf)
29858f0484fSRodney W. Grimes 		(void)fprintf(stderr,
2997efabbb9SXin LI 		    "%.0f%% leaf fill (%lu bytes used, %lu bytes free)\n",
30058f0484fSRodney W. Grimes 		    ((double)(pleaf - lfree) / pleaf) * 100,
30158f0484fSRodney W. Grimes 		    pleaf - lfree, lfree);
30258f0484fSRodney W. Grimes 	pinternal *= t->bt_psize - BTDATAOFF;
30358f0484fSRodney W. Grimes 	if (pinternal)
30458f0484fSRodney W. Grimes 		(void)fprintf(stderr,
3057efabbb9SXin LI 		    "%.0f%% internal fill (%lu bytes used, %lu bytes free\n",
30658f0484fSRodney W. Grimes 		    ((double)(pinternal - ifree) / pinternal) * 100,
30758f0484fSRodney W. Grimes 		    pinternal - ifree, ifree);
30858f0484fSRodney W. Grimes 	if (bt_pfxsaved)
30958f0484fSRodney W. Grimes 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
31058f0484fSRodney W. Grimes 		    bt_pfxsaved);
31158f0484fSRodney W. Grimes }
31258f0484fSRodney W. Grimes #endif
313