Home | History | Annotate | Line # | Download | only in btree
bt_debug.c revision 1.6
      1 /*	$NetBSD: bt_debug.c,v 1.6 1996/05/03 21:50:41 cgd 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #if defined(LIBC_SCCS) && !defined(lint)
     40 #if 0
     41 static char sccsid[] = "@(#)bt_debug.c	8.5 (Berkeley) 8/17/94";
     42 #else
     43 static char rcsid[] = "$NetBSD: bt_debug.c,v 1.6 1996/05/03 21:50:41 cgd Exp $";
     44 #endif
     45 #endif /* LIBC_SCCS and not lint */
     46 
     47 #include <sys/param.h>
     48 
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 
     53 #include <db.h>
     54 #include "btree.h"
     55 
     56 #ifdef DEBUG
     57 /*
     58  * BT_DUMP -- Dump the tree
     59  *
     60  * Parameters:
     61  *	dbp:	pointer to the DB
     62  */
     63 void
     64 __bt_dump(dbp)
     65 	DB *dbp;
     66 {
     67 	BTREE *t;
     68 	PAGE *h;
     69 	pgno_t i;
     70 	char *sep;
     71 
     72 	t = dbp->internal;
     73 	(void)fprintf(stderr, "%s: pgsz %d",
     74 	    F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
     75 	if (F_ISSET(t, R_RECNO))
     76 		(void)fprintf(stderr, " keys %lu", t->bt_nrecs);
     77 #undef X
     78 #define	X(flag, name) \
     79 	if (F_ISSET(t, flag)) { \
     80 		(void)fprintf(stderr, "%s%s", sep, name); \
     81 		sep = ", "; \
     82 	}
     83 	if (t->flags != 0) {
     84 		sep = " flags (";
     85 		X(R_FIXLEN,	"FIXLEN");
     86 		X(B_INMEM,	"INMEM");
     87 		X(B_NODUPS,	"NODUPS");
     88 		X(B_RDONLY,	"RDONLY");
     89 		X(R_RECNO,	"RECNO");
     90 		X(B_METADIRTY,"METADIRTY");
     91 		(void)fprintf(stderr, ")\n");
     92 	}
     93 #undef X
     94 
     95 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
     96 		__bt_dpage(h);
     97 		(void)mpool_put(t->bt_mp, h, 0);
     98 	}
     99 }
    100 
    101 /*
    102  * BT_DMPAGE -- Dump the meta page
    103  *
    104  * Parameters:
    105  *	h:	pointer to the PAGE
    106  */
    107 void
    108 __bt_dmpage(h)
    109 	PAGE *h;
    110 {
    111 	BTMETA *m;
    112 	char *sep;
    113 
    114 	m = (BTMETA *)h;
    115 	(void)fprintf(stderr, "magic %lx\n", m->magic);
    116 	(void)fprintf(stderr, "version %lu\n", m->version);
    117 	(void)fprintf(stderr, "psize %lu\n", m->psize);
    118 	(void)fprintf(stderr, "free %lu\n", m->free);
    119 	(void)fprintf(stderr, "nrecs %lu\n", m->nrecs);
    120 	(void)fprintf(stderr, "flags %lu", m->flags);
    121 #undef X
    122 #define	X(flag, name) \
    123 	if (m->flags & flag) { \
    124 		(void)fprintf(stderr, "%s%s", sep, name); \
    125 		sep = ", "; \
    126 	}
    127 	if (m->flags) {
    128 		sep = " (";
    129 		X(B_NODUPS,	"NODUPS");
    130 		X(R_RECNO,	"RECNO");
    131 		(void)fprintf(stderr, ")");
    132 	}
    133 }
    134 
    135 /*
    136  * BT_DNPAGE -- Dump the page
    137  *
    138  * Parameters:
    139  *	n:	page number to dump.
    140  */
    141 void
    142 __bt_dnpage(dbp, pgno)
    143 	DB *dbp;
    144 	pgno_t pgno;
    145 {
    146 	BTREE *t;
    147 	PAGE *h;
    148 
    149 	t = dbp->internal;
    150 	if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
    151 		__bt_dpage(h);
    152 		(void)mpool_put(t->bt_mp, h, 0);
    153 	}
    154 }
    155 
    156 /*
    157  * BT_DPAGE -- Dump the page
    158  *
    159  * Parameters:
    160  *	h:	pointer to the PAGE
    161  */
    162 void
    163 __bt_dpage(h)
    164 	PAGE *h;
    165 {
    166 	BINTERNAL *bi;
    167 	BLEAF *bl;
    168 	RINTERNAL *ri;
    169 	RLEAF *rl;
    170 	indx_t cur, top;
    171 	char *sep;
    172 
    173 	(void)fprintf(stderr, "    page %d: (", h->pgno);
    174 #undef X
    175 #define	X(flag, name) \
    176 	if (h->flags & flag) { \
    177 		(void)fprintf(stderr, "%s%s", sep, name); \
    178 		sep = ", "; \
    179 	}
    180 	sep = "";
    181 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
    182 	X(P_BLEAF,	"BLEAF")
    183 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
    184 	X(P_RLEAF,	"RLEAF")
    185 	X(P_OVERFLOW,	"OVERFLOW")
    186 	X(P_PRESERVE,	"PRESERVE");
    187 	(void)fprintf(stderr, ")\n");
    188 #undef X
    189 
    190 	(void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
    191 	if (h->flags & P_OVERFLOW)
    192 		return;
    193 
    194 	top = NEXTINDEX(h);
    195 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
    196 	    h->lower, h->upper, top);
    197 	for (cur = 0; cur < top; cur++) {
    198 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
    199 		switch (h->flags & P_TYPE) {
    200 		case P_BINTERNAL:
    201 			bi = GETBINTERNAL(h, cur);
    202 			(void)fprintf(stderr,
    203 			    "size %03d pgno %03d", bi->ksize, bi->pgno);
    204 			if (bi->flags & P_BIGKEY)
    205 				(void)fprintf(stderr, " (indirect)");
    206 			else if (bi->ksize)
    207 				(void)fprintf(stderr,
    208 				    " {%.*s}", (int)bi->ksize, bi->bytes);
    209 			break;
    210 		case P_RINTERNAL:
    211 			ri = GETRINTERNAL(h, cur);
    212 			(void)fprintf(stderr, "entries %03d pgno %03d",
    213 				ri->nrecs, ri->pgno);
    214 			break;
    215 		case P_BLEAF:
    216 			bl = GETBLEAF(h, cur);
    217 			if (bl->flags & P_BIGKEY)
    218 				(void)fprintf(stderr,
    219 				    "big key page %lu size %u/",
    220 				    *(pgno_t *)bl->bytes,
    221 				    *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
    222 			else if (bl->ksize)
    223 				(void)fprintf(stderr, "%s/", bl->bytes);
    224 			if (bl->flags & P_BIGDATA)
    225 				(void)fprintf(stderr,
    226 				    "big data page %lu size %u",
    227 				    *(pgno_t *)(bl->bytes + bl->ksize),
    228 				    *(u_int32_t *)(bl->bytes + bl->ksize +
    229 				    sizeof(pgno_t)));
    230 			else if (bl->dsize)
    231 				(void)fprintf(stderr, "%.*s",
    232 				    (int)bl->dsize, bl->bytes + bl->ksize);
    233 			break;
    234 		case P_RLEAF:
    235 			rl = GETRLEAF(h, cur);
    236 			if (rl->flags & P_BIGDATA)
    237 				(void)fprintf(stderr,
    238 				    "big data page %lu size %u",
    239 				    *(pgno_t *)rl->bytes,
    240 				    *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
    241 			else if (rl->dsize)
    242 				(void)fprintf(stderr,
    243 				    "%.*s", (int)rl->dsize, rl->bytes);
    244 			break;
    245 		}
    246 		(void)fprintf(stderr, "\n");
    247 	}
    248 }
    249 #endif
    250 
    251 #ifdef STATISTICS
    252 /*
    253  * BT_STAT -- Gather/print the tree statistics
    254  *
    255  * Parameters:
    256  *	dbp:	pointer to the DB
    257  */
    258 void
    259 __bt_stat(dbp)
    260 	DB *dbp;
    261 {
    262 	extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
    263 	extern u_long bt_sortsplit, bt_split;
    264 	BTREE *t;
    265 	PAGE *h;
    266 	pgno_t i, pcont, pinternal, pleaf;
    267 	u_long ifree, lfree, nkeys;
    268 	int levels;
    269 
    270 	t = dbp->internal;
    271 	pcont = pinternal = pleaf = 0;
    272 	nkeys = ifree = lfree = 0;
    273 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
    274 		switch (h->flags & P_TYPE) {
    275 		case P_BINTERNAL:
    276 		case P_RINTERNAL:
    277 			++pinternal;
    278 			ifree += h->upper - h->lower;
    279 			break;
    280 		case P_BLEAF:
    281 		case P_RLEAF:
    282 			++pleaf;
    283 			lfree += h->upper - h->lower;
    284 			nkeys += NEXTINDEX(h);
    285 			break;
    286 		case P_OVERFLOW:
    287 			++pcont;
    288 			break;
    289 		}
    290 		(void)mpool_put(t->bt_mp, h, 0);
    291 	}
    292 
    293 	/* Count the levels of the tree. */
    294 	for (i = P_ROOT, levels = 0 ;; ++levels) {
    295 		h = mpool_get(t->bt_mp, i, 0);
    296 		if (h->flags & (P_BLEAF|P_RLEAF)) {
    297 			if (levels == 0)
    298 				levels = 1;
    299 			(void)mpool_put(t->bt_mp, h, 0);
    300 			break;
    301 		}
    302 		i = F_ISSET(t, R_RECNO) ?
    303 		    GETRINTERNAL(h, 0)->pgno :
    304 		    GETBINTERNAL(h, 0)->pgno;
    305 		(void)mpool_put(t->bt_mp, h, 0);
    306 	}
    307 
    308 	(void)fprintf(stderr, "%d level%s with %ld keys",
    309 	    levels, levels == 1 ? "" : "s", nkeys);
    310 	if (F_ISSET(t, R_RECNO))
    311 		(void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
    312 	(void)fprintf(stderr,
    313 	    "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
    314 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
    315 	(void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
    316 	    bt_cache_hit, bt_cache_miss);
    317 	(void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
    318 	    bt_split, bt_rootsplit, bt_sortsplit);
    319 	pleaf *= t->bt_psize - BTDATAOFF;
    320 	if (pleaf)
    321 		(void)fprintf(stderr,
    322 		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
    323 		    ((double)(pleaf - lfree) / pleaf) * 100,
    324 		    pleaf - lfree, lfree);
    325 	pinternal *= t->bt_psize - BTDATAOFF;
    326 	if (pinternal)
    327 		(void)fprintf(stderr,
    328 		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
    329 		    ((double)(pinternal - ifree) / pinternal) * 100,
    330 		    pinternal - ifree, ifree);
    331 	if (bt_pfxsaved)
    332 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
    333 		    bt_pfxsaved);
    334 }
    335 #endif
    336