xref: /csrg-svn/lib/libc/db/btree/bt_debug.c (revision 56739)
150990Sbostic /*-
250990Sbostic  * Copyright (c) 1990 The Regents of the University of California.
350990Sbostic  * All rights reserved.
450990Sbostic  *
550990Sbostic  * This code is derived from software contributed to Berkeley by
650990Sbostic  * Mike Olson.
750990Sbostic  *
850990Sbostic  * %sccs.include.redist.c%
950990Sbostic  */
1050990Sbostic 
1150990Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*56739Sbostic static char sccsid[] = "@(#)bt_debug.c	5.3 (Berkeley) 11/13/92";
1350990Sbostic #endif /* LIBC_SCCS and not lint */
1450990Sbostic 
1550990Sbostic #include <sys/param.h>
16*56739Sbostic 
1750990Sbostic #include <db.h>
1850990Sbostic #include <stdio.h>
1950990Sbostic #include <stdlib.h>
2050990Sbostic #include <string.h>
21*56739Sbostic 
2250990Sbostic #include "btree.h"
2350990Sbostic 
2450990Sbostic #ifdef DEBUG
2550990Sbostic /*
2650990Sbostic  * BT_DUMP -- Dump the tree
2750990Sbostic  *
2850990Sbostic  * Parameters:
2950990Sbostic  *	dbp:	pointer to the DB
3050990Sbostic  */
3150990Sbostic void
3250990Sbostic __bt_dump(dbp)
3350990Sbostic 	DB *dbp;
3450990Sbostic {
3550990Sbostic 	BTREE *t;
3650990Sbostic 	PAGE *h;
3750990Sbostic 	pgno_t i;
3850990Sbostic 	char *sep;
3950990Sbostic 
4050990Sbostic 	t = dbp->internal;
4151102Sbostic 	(void)fprintf(stderr, "%s: pgsz %d",
4250990Sbostic 	    ISSET(t, BTF_INMEM) ? "memory" : "disk", t->bt_psize);
4350990Sbostic 	if (ISSET(t, BTF_RECNO))
4450990Sbostic 		(void)fprintf(stderr, " keys %lu", t->bt_nrecs);
4551102Sbostic #undef X
4650990Sbostic #define	X(flag, name) \
4750990Sbostic 	if (ISSET(t, flag)) { \
4850990Sbostic 		(void)fprintf(stderr, "%s%s", sep, name); \
4950990Sbostic 		sep = ", "; \
5050990Sbostic 	}
5150990Sbostic 	if (t->bt_flags) {
5250990Sbostic 		sep = " flags (";
5350990Sbostic 		X(BTF_DELCRSR,	"DELCRSR");
5450990Sbostic 		X(BTF_FIXEDLEN,	"FIXEDLEN");
5550990Sbostic 		X(BTF_INMEM,	"INMEM");
5650990Sbostic 		X(BTF_NODUPS,	"NODUPS");
5750990Sbostic 		X(BTF_RDONLY,	"RDONLY");
5850990Sbostic 		X(BTF_RECNO,	"RECNO");
5950990Sbostic 		X(BTF_SEQINIT,	"SEQINIT");
6050990Sbostic 		X(BTF_METADIRTY,"METADIRTY");
6150990Sbostic 		(void)fprintf(stderr, ")\n");
6250990Sbostic 	}
6350990Sbostic #undef X
6450990Sbostic 
6550990Sbostic 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
6650990Sbostic 		__bt_dpage(h);
6750990Sbostic 		(void)mpool_put(t->bt_mp, h, 0);
6850990Sbostic 	}
6950990Sbostic }
7050990Sbostic 
7150990Sbostic /*
7251102Sbostic  * BT_DMPAGE -- Dump the meta page
7351102Sbostic  *
7451102Sbostic  * Parameters:
7551102Sbostic  *	h:	pointer to the PAGE
7651102Sbostic  */
7751102Sbostic void
7851102Sbostic __bt_dmpage(h)
7951102Sbostic 	PAGE *h;
8051102Sbostic {
8151102Sbostic 	BTMETA *m;
8251102Sbostic 	char *sep;
8351102Sbostic 
8451102Sbostic 	m = (BTMETA *)h;
8551102Sbostic 	(void)fprintf(stderr, "magic %lx\n", m->m_magic);
8651102Sbostic 	(void)fprintf(stderr, "version %lu\n", m->m_version);
8751102Sbostic 	(void)fprintf(stderr, "psize %lu\n", m->m_psize);
8851102Sbostic 	(void)fprintf(stderr, "free %lu\n", m->m_free);
8951102Sbostic 	(void)fprintf(stderr, "nrecs %lu\n", m->m_nrecs);
9051102Sbostic 	(void)fprintf(stderr, "flags %lu", m->m_flags);
9151102Sbostic #undef X
9251102Sbostic #define	X(flag, name) \
9351102Sbostic 	if (m->m_flags & flag) { \
9451102Sbostic 		(void)fprintf(stderr, "%s%s", sep, name); \
9551102Sbostic 		sep = ", "; \
9651102Sbostic 	}
9751102Sbostic 	if (m->m_flags) {
9851102Sbostic 		sep = " (";
9951102Sbostic 		X(BTF_NODUPS,	"NODUPS");
10051102Sbostic 		X(BTF_RECNO,	"RECNO");
10151102Sbostic 		(void)fprintf(stderr, ")");
10251102Sbostic 	}
10351102Sbostic 	(void)fprintf(stderr, "\nlorder %lu\n", m->m_lorder);
10451102Sbostic }
10551102Sbostic 
10651102Sbostic /*
10750990Sbostic  * BT_DPAGE -- Dump the page
10850990Sbostic  *
10950990Sbostic  * Parameters:
11050990Sbostic  *	h:	pointer to the PAGE
11150990Sbostic  */
11250990Sbostic void
11350990Sbostic __bt_dpage(h)
11450990Sbostic 	PAGE *h;
11550990Sbostic {
11650990Sbostic 	BINTERNAL *bi;
11750990Sbostic 	BLEAF *bl;
11850990Sbostic 	RINTERNAL *ri;
11950990Sbostic 	RLEAF *rl;
12050990Sbostic 	index_t cur, top;
12150990Sbostic 	char *sep;
12250990Sbostic 
12350990Sbostic 	(void)fprintf(stderr, "    page %d: (", h->pgno);
12451102Sbostic #undef X
12550990Sbostic #define	X(flag, name) \
12650990Sbostic 	if (h->flags & flag) { \
12750990Sbostic 		(void)fprintf(stderr, "%s%s", sep, name); \
12850990Sbostic 		sep = ", "; \
12950990Sbostic 	}
13050990Sbostic 	sep = "";
13150990Sbostic 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
13250990Sbostic 	X(P_BLEAF,	"BLEAF")
13350990Sbostic 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
13450990Sbostic 	X(P_RLEAF,	"RLEAF")
13550990Sbostic 	X(P_OVERFLOW,	"OVERFLOW")
13650990Sbostic 	X(P_PRESERVE,	"PRESERVE");
13750990Sbostic 	(void)fprintf(stderr, ")\n");
13850990Sbostic #undef X
13950990Sbostic 
14050990Sbostic 	(void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
14150990Sbostic 	if (h->flags & P_OVERFLOW)
14250990Sbostic 		return;
14350990Sbostic 
14450990Sbostic 	top = NEXTINDEX(h);
14550990Sbostic 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
14650990Sbostic 	    h->lower, h->upper, top);
14750990Sbostic 	for (cur = 0; cur < top; cur++) {
14850990Sbostic 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
14950990Sbostic 		switch(h->flags & P_TYPE) {
15050990Sbostic 		case P_BINTERNAL:
15150990Sbostic 			bi = GETBINTERNAL(h, cur);
15250990Sbostic 			(void)fprintf(stderr,
15350990Sbostic 			    "size %2d pgno %2d", bi->ksize, bi->pgno);
15450990Sbostic 			if (bi->flags & P_BIGKEY)
15550990Sbostic 				(void)fprintf(stderr, " (indirect)");
15650990Sbostic 			else if (bi->ksize)
15750990Sbostic 				(void)fprintf(stderr, " {%s}", bi->bytes);
15850990Sbostic 			break;
15950990Sbostic 		case P_RINTERNAL:
16050990Sbostic 			ri = GETRINTERNAL(h, cur);
16150990Sbostic 			(void)fprintf(stderr, "entries %2d pgno %2d",
16250990Sbostic 				ri->nrecs, ri->pgno);
16350990Sbostic 			break;
16450990Sbostic 		case P_BLEAF:
16550990Sbostic 			bl = GETBLEAF(h, cur);
16650990Sbostic 			if (bl->flags & P_BIGKEY)
16750990Sbostic 				(void)fprintf(stderr,
16850990Sbostic 				    "big key page %lu size %u/",
16950990Sbostic 				    *(pgno_t *)bl->bytes,
17050990Sbostic 				    *(size_t *)(bl->bytes + sizeof(pgno_t)));
17150990Sbostic 			else if (bl->ksize)
17250990Sbostic 				(void)fprintf(stderr, "%s/", bl->bytes);
17350990Sbostic 			if (bl->flags & P_BIGDATA)
17450990Sbostic 				(void)fprintf(stderr,
17550990Sbostic 				    "big data page %lu size %u",
17650990Sbostic 				    *(pgno_t *)(bl->bytes + bl->ksize),
17750990Sbostic 				    *(size_t *)(bl->bytes + bl->ksize +
17850990Sbostic 				    sizeof(pgno_t)));
17950990Sbostic 			else if (bl->dsize)
18050990Sbostic 				(void)fprintf(stderr,
18150990Sbostic 				    "%s", bl->bytes + bl->ksize);
18250990Sbostic 			break;
18350990Sbostic 		case P_RLEAF:
18450990Sbostic 			rl = GETRLEAF(h, cur);
18550990Sbostic 			if (rl->flags & P_BIGDATA)
18650990Sbostic 				(void)fprintf(stderr,
18750990Sbostic 				    "big data page %lu size %u",
18850990Sbostic 				    *(pgno_t *)rl->bytes,
18950990Sbostic 				    *(size_t *)(rl->bytes + sizeof(pgno_t)));
19050990Sbostic 			else if (rl->dsize)
19150990Sbostic 				(void)fprintf(stderr, "%s", rl->bytes);
19250990Sbostic 			break;
19350990Sbostic 		}
19450990Sbostic 		(void)fprintf(stderr, "\n");
19550990Sbostic 	}
19650990Sbostic }
19750990Sbostic #endif
19850990Sbostic 
19950990Sbostic #ifdef STATISTICS
20050990Sbostic /*
20150990Sbostic  * BT_STAT -- Gather/print the tree statistics
20250990Sbostic  *
20350990Sbostic  * Parameters:
20450990Sbostic  *	dbp:	pointer to the DB
20550990Sbostic  */
20650990Sbostic void
20750990Sbostic __bt_stat(dbp)
20850990Sbostic 	DB *dbp;
20950990Sbostic {
21050990Sbostic 	extern u_long bt_cache_hit, bt_cache_miss;
21150990Sbostic 	extern u_long bt_rootsplit, bt_split, bt_sortsplit;
21250990Sbostic 	extern u_long bt_pfxsaved;
21350990Sbostic 	BTREE *t;
21450990Sbostic 	PAGE *h;
21550990Sbostic 	pgno_t i, pcont, pinternal, pleaf;
21650990Sbostic 	u_long ifree, lfree, nkeys;
21750990Sbostic 	int levels;
21850990Sbostic 
21950990Sbostic 	t = dbp->internal;
22050990Sbostic 	pcont = pinternal = pleaf = 0;
22150990Sbostic 	nkeys = ifree = lfree = 0;
22250990Sbostic 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
22350990Sbostic 		switch(h->flags & P_TYPE) {
22450990Sbostic 		case P_BINTERNAL:
22550990Sbostic 		case P_RINTERNAL:
22650990Sbostic 			++pinternal;
22750990Sbostic 			ifree += h->upper - h->lower;
22850990Sbostic 			break;
22950990Sbostic 		case P_BLEAF:
23050990Sbostic 		case P_RLEAF:
23150990Sbostic 			++pleaf;
23250990Sbostic 			lfree += h->upper - h->lower;
23350990Sbostic 			nkeys += NEXTINDEX(h);
23450990Sbostic 			break;
23550990Sbostic 		case P_OVERFLOW:
23650990Sbostic 			++pcont;
23750990Sbostic 			break;
23850990Sbostic 		}
23950990Sbostic 		(void)mpool_put(t->bt_mp, h, 0);
24050990Sbostic 	}
24150990Sbostic 
24250990Sbostic 	/* Count the levels of the tree. */
24350990Sbostic 	for (i = P_ROOT, levels = 0 ;; ++levels) {
24450990Sbostic 		h = mpool_get(t->bt_mp, i, 0);
24550990Sbostic 		if (h->flags & (P_BLEAF|P_RLEAF)) {
24650990Sbostic 			if (levels == 0)
24750990Sbostic 				levels = 1;
24850990Sbostic 			(void)mpool_put(t->bt_mp, h, 0);
24950990Sbostic 			break;
25050990Sbostic 		}
25150990Sbostic 		i = ISSET(t, BTF_RECNO) ?
25250990Sbostic 		    GETRINTERNAL(h, 0)->pgno :
25350990Sbostic 		    GETBINTERNAL(h, 0)->pgno;
25450990Sbostic 		(void)mpool_put(t->bt_mp, h, 0);
25550990Sbostic 	}
25650990Sbostic 
25750990Sbostic 	(void)fprintf(stderr, "%d level%s with %ld keys",
25850990Sbostic 	    levels, levels == 1 ? "" : "s", nkeys);
25950990Sbostic 	if (ISSET(t, BTF_RECNO))
26050990Sbostic 		(void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
26150990Sbostic 	(void)fprintf(stderr,
26250990Sbostic 	    "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
26350990Sbostic 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
26450990Sbostic 	(void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
26550990Sbostic 	    bt_cache_hit, bt_cache_miss);
26650990Sbostic 	(void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
26750990Sbostic 	    bt_split, bt_rootsplit, bt_sortsplit);
26850990Sbostic 	pleaf *= t->bt_psize - BTDATAOFF;
26950990Sbostic 	if (pleaf)
27050990Sbostic 		(void)fprintf(stderr,
27150990Sbostic 		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
27250990Sbostic 		    ((double)(pleaf - lfree) / pleaf) * 100,
27350990Sbostic 		    pleaf - lfree, lfree);
27450990Sbostic 	pinternal *= t->bt_psize - BTDATAOFF;
27550990Sbostic 	if (pinternal)
27650990Sbostic 		(void)fprintf(stderr,
27750990Sbostic 		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
27850990Sbostic 		    ((double)(pinternal - ifree) / pinternal) * 100,
27950990Sbostic 		    pinternal - ifree, ifree);
28050990Sbostic 	if (bt_pfxsaved)
28150990Sbostic 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
28250990Sbostic 		    bt_pfxsaved);
28350990Sbostic }
28450990Sbostic #endif
285