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