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