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