2N/A/*-
2N/A * Copyright (c) 1990, 1993, 1994, 1995
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * This code is derived from software contributed to Berkeley by
2N/A * Mike Olson.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic char sccsid[] = "@(#)bt_debug.c 8.6 (Berkeley) 1/9/95";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A#include <sys/param.h>
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A#include "db-int.h"
2N/A#include "btree.h"
2N/A
2N/A#if defined(DEBUG) || defined(STATISTICS)
2N/A
2N/Astatic FILE *tracefp;
2N/A
2N/A/*
2N/A * __bt_dinit --
2N/A * initialize debugging.
2N/A */
2N/Astatic void
2N/A__bt_dinit()
2N/A{
2N/A static int first = 1;
2N/A char buf[1024];
2N/A
2N/A if (!first)
2N/A return;
2N/A first = 0;
2N/A
2N/A#ifndef TRACE_TO_STDERR
2N/A /* Solaris Kerberos */
2N/A if ((tracefp = fopen("/tmp/__bt_debug", "wF")) != NULL)
2N/A return;
2N/A#endif
2N/A tracefp = stderr;
2N/A}
2N/A#endif
2N/A
2N/A#ifdef DEBUG
2N/A/*
2N/A * __bt_dump --
2N/A * dump the tree
2N/A *
2N/A * Parameters:
2N/A * dbp: pointer to the DB
2N/A */
2N/Aint
2N/A__bt_dump(dbp)
2N/A DB *dbp;
2N/A{
2N/A BTREE *t;
2N/A PAGE *h;
2N/A db_pgno_t i;
2N/A char *sep;
2N/A
2N/A __bt_dinit();
2N/A
2N/A t = dbp->internal;
2N/A (void)fprintf(tracefp, "%s: pgsz %d",
2N/A F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
2N/A if (F_ISSET(t, R_RECNO))
2N/A (void)fprintf(tracefp, " keys %lu", t->bt_nrecs);
2N/A#undef X
2N/A#define X(flag, name) \
2N/A if (F_ISSET(t, flag)) { \
2N/A (void)fprintf(tracefp, "%s%s", sep, name); \
2N/A sep = ", "; \
2N/A }
2N/A if (t->flags != 0) {
2N/A sep = " flags (";
2N/A X(R_FIXLEN, "FIXLEN");
2N/A X(B_INMEM, "INMEM");
2N/A X(B_NODUPS, "NODUPS");
2N/A X(B_RDONLY, "RDONLY");
2N/A X(R_RECNO, "RECNO");
2N/A X(B_METADIRTY,"METADIRTY");
2N/A (void)fprintf(tracefp, ")\n");
2N/A }
2N/A#undef X
2N/A for (i = P_ROOT; i < t->bt_mp->npages &&
2N/A (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
2N/A __bt_dpage(dbp, h);
2N/A (void)fflush(tracefp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * BT_DMPAGE -- Dump the meta page
2N/A *
2N/A * Parameters:
2N/A * h: pointer to the PAGE
2N/A */
2N/Aint
2N/A__bt_dmpage(h)
2N/A PAGE *h;
2N/A{
2N/A BTMETA *m;
2N/A char *sep;
2N/A
2N/A __bt_dinit();
2N/A
2N/A m = (BTMETA *)h;
2N/A (void)fprintf(tracefp, "magic %lx\n", m->magic);
2N/A (void)fprintf(tracefp, "version %lu\n", m->version);
2N/A (void)fprintf(tracefp, "psize %lu\n", m->psize);
2N/A (void)fprintf(tracefp, "free %lu\n", m->free);
2N/A (void)fprintf(tracefp, "nrecs %lu\n", m->nrecs);
2N/A (void)fprintf(tracefp, "flags %lu", m->flags);
2N/A#undef X
2N/A#define X(flag, name) \
2N/A if (m->flags & flag) { \
2N/A (void)fprintf(tracefp, "%s%s", sep, name); \
2N/A sep = ", "; \
2N/A }
2N/A if (m->flags) {
2N/A sep = " (";
2N/A X(B_NODUPS, "NODUPS");
2N/A X(R_RECNO, "RECNO");
2N/A (void)fprintf(tracefp, ")");
2N/A }
2N/A (void)fprintf(tracefp, "\n");
2N/A (void)fflush(tracefp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * BT_DNPAGE -- Dump the page
2N/A *
2N/A * Parameters:
2N/A * n: page number to dump.
2N/A */
2N/Aint
2N/A__bt_dnpage(dbp, pgno)
2N/A DB *dbp;
2N/A db_pgno_t pgno;
2N/A{
2N/A BTREE *t;
2N/A PAGE *h;
2N/A
2N/A __bt_dinit();
2N/A
2N/A t = dbp->internal;
2N/A if ((h = mpool_get(t->bt_mp, pgno, MPOOL_IGNOREPIN)) != NULL)
2N/A __bt_dpage(dbp, h);
2N/A (void)fflush(tracefp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * BT_DPAGE -- Dump the page
2N/A *
2N/A * Parameters:
2N/A * h: pointer to the PAGE
2N/A */
2N/Aint
2N/A__bt_dpage(dbp, h)
2N/A DB *dbp;
2N/A PAGE *h;
2N/A{
2N/A BINTERNAL *bi;
2N/A BLEAF *bl;
2N/A RINTERNAL *ri;
2N/A RLEAF *rl;
2N/A u_long pgsize;
2N/A indx_t cur, top, lim;
2N/A char *sep;
2N/A
2N/A __bt_dinit();
2N/A
2N/A (void)fprintf(tracefp, " page %d: (", h->pgno);
2N/A#undef X
2N/A#define X(flag, name) \
2N/A if (h->flags & flag) { \
2N/A (void)fprintf(tracefp, "%s%s", sep, name); \
2N/A sep = ", "; \
2N/A }
2N/A sep = "";
2N/A X(P_BINTERNAL, "BINTERNAL") /* types */
2N/A X(P_BLEAF, "BLEAF")
2N/A X(P_RINTERNAL, "RINTERNAL") /* types */
2N/A X(P_RLEAF, "RLEAF")
2N/A X(P_OVERFLOW, "OVERFLOW")
2N/A X(P_PRESERVE, "PRESERVE");
2N/A (void)fprintf(tracefp, ")\n");
2N/A#undef X
2N/A
2N/A (void)fprintf(tracefp, "\tprev %2d next %2d", h->prevpg, h->nextpg);
2N/A if (h->flags & P_OVERFLOW)
2N/A return;
2N/A
2N/A pgsize = ((BTREE *)dbp->internal)->bt_mp->pagesize;
2N/A lim = (pgsize - BTDATAOFF) / sizeof(indx_t);
2N/A top = NEXTINDEX(h);
2N/A lim = top > lim ? lim : top;
2N/A (void)fprintf(tracefp, " lower %3d upper %3d nextind %d\n",
2N/A h->lower, h->upper, top);
2N/A for (cur = 0; cur < lim; cur++) {
2N/A (void)fprintf(tracefp, "\t[%03d] %4d ", cur, h->linp[cur]);
2N/A switch (h->flags & P_TYPE) {
2N/A case P_BINTERNAL:
2N/A bi = GETBINTERNAL(h, cur);
2N/A (void)fprintf(tracefp,
2N/A "size %03d pgno %03d", bi->ksize, bi->pgno);
2N/A if (bi->flags & P_BIGKEY)
2N/A (void)fprintf(tracefp, " (indirect)");
2N/A else if (bi->ksize)
2N/A (void)fprintf(tracefp,
2N/A " {%.*s}", (int)bi->ksize, bi->bytes);
2N/A break;
2N/A case P_RINTERNAL:
2N/A ri = GETRINTERNAL(h, cur);
2N/A (void)fprintf(tracefp, "entries %03d pgno %03d",
2N/A ri->nrecs, ri->pgno);
2N/A break;
2N/A case P_BLEAF:
2N/A bl = GETBLEAF(h, cur);
2N/A if (bl->flags & P_BIGKEY)
2N/A (void)fprintf(tracefp,
2N/A "big key page %lu size %u/",
2N/A *(db_pgno_t *)bl->bytes,
2N/A *(u_int32_t *)(bl->bytes + sizeof(db_pgno_t)));
2N/A else if (bl->ksize)
2N/A (void)fprintf(tracefp, "%.*s/",
2N/A (int)bl->ksize, bl->bytes);
2N/A if (bl->flags & P_BIGDATA)
2N/A (void)fprintf(tracefp,
2N/A "big data page %lu size %u",
2N/A *(db_pgno_t *)(bl->bytes + bl->ksize),
2N/A *(u_int32_t *)(bl->bytes + bl->ksize +
2N/A sizeof(db_pgno_t)));
2N/A else if (bl->dsize)
2N/A (void)fprintf(tracefp, "%.*s",
2N/A (int)bl->dsize, bl->bytes + bl->ksize);
2N/A break;
2N/A case P_RLEAF:
2N/A rl = GETRLEAF(h, cur);
2N/A if (rl->flags & P_BIGDATA)
2N/A (void)fprintf(tracefp,
2N/A "big data page %lu size %u",
2N/A *(db_pgno_t *)rl->bytes,
2N/A *(u_int32_t *)(rl->bytes + sizeof(db_pgno_t)));
2N/A else if (rl->dsize)
2N/A (void)fprintf(tracefp,
2N/A "%.*s", (int)rl->dsize, rl->bytes);
2N/A break;
2N/A }
2N/A (void)fprintf(tracefp, "\n");
2N/A }
2N/A (void)fflush(tracefp);
2N/A return (0);
2N/A}
2N/A#endif
2N/A
2N/A#ifdef STATISTICS
2N/A/*
2N/A * bt_stat --
2N/A * Gather/print the tree statistics
2N/A *
2N/A * Parameters:
2N/A * dbp: pointer to the DB
2N/A */
2N/Aint
2N/A__bt_stat(dbp)
2N/A DB *dbp;
2N/A{
2N/A extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
2N/A extern u_long bt_sortsplit, bt_split;
2N/A BTREE *t;
2N/A PAGE *h;
2N/A db_pgno_t i, pcont, pinternal, pleaf;
2N/A u_long ifree, lfree, nkeys;
2N/A int levels;
2N/A
2N/A __bt_dinit();
2N/A
2N/A t = dbp->internal;
2N/A pcont = pinternal = pleaf = 0;
2N/A nkeys = ifree = lfree = 0;
2N/A for (i = P_ROOT; i < t->bt_mp->npages &&
2N/A (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
2N/A switch (h->flags & P_TYPE) {
2N/A case P_BINTERNAL:
2N/A case P_RINTERNAL:
2N/A ++pinternal;
2N/A ifree += h->upper - h->lower;
2N/A break;
2N/A case P_BLEAF:
2N/A case P_RLEAF:
2N/A ++pleaf;
2N/A lfree += h->upper - h->lower;
2N/A nkeys += NEXTINDEX(h);
2N/A break;
2N/A case P_OVERFLOW:
2N/A ++pcont;
2N/A break;
2N/A }
2N/A
2N/A /* Count the levels of the tree. */
2N/A for (i = P_ROOT, levels = 0 ;; ++levels) {
2N/A h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN);
2N/A if (h->flags & (P_BLEAF|P_RLEAF)) {
2N/A if (levels == 0)
2N/A levels = 1;
2N/A break;
2N/A }
2N/A i = F_ISSET(t, R_RECNO) ?
2N/A GETRINTERNAL(h, 0)->pgno :
2N/A GETBINTERNAL(h, 0)->pgno;
2N/A }
2N/A
2N/A (void)fprintf(tracefp, "%d level%s with %ld keys",
2N/A levels, levels == 1 ? "" : "s", nkeys);
2N/A if (F_ISSET(t, R_RECNO))
2N/A (void)fprintf(tracefp, " (%ld header count)", t->bt_nrecs);
2N/A (void)fprintf(tracefp,
2N/A "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
2N/A pinternal + pleaf + pcont, pleaf, pinternal, pcont);
2N/A (void)fprintf(tracefp, "%ld cache hits, %ld cache misses\n",
2N/A bt_cache_hit, bt_cache_miss);
2N/A (void)fprintf(tracefp,
2N/A "%ld splits (%ld root splits, %ld sort splits)\n",
2N/A bt_split, bt_rootsplit, bt_sortsplit);
2N/A pleaf *= t->bt_psize - BTDATAOFF;
2N/A if (pleaf)
2N/A (void)fprintf(tracefp,
2N/A "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
2N/A ((double)(pleaf - lfree) / pleaf) * 100,
2N/A pleaf - lfree, lfree);
2N/A pinternal *= t->bt_psize - BTDATAOFF;
2N/A if (pinternal)
2N/A (void)fprintf(tracefp,
2N/A "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
2N/A ((double)(pinternal - ifree) / pinternal) * 100,
2N/A pinternal - ifree, ifree);
2N/A if (bt_pfxsaved)
2N/A (void)fprintf(tracefp, "prefix checking removed %lu bytes.\n",
2N/A bt_pfxsaved);
2N/A (void)fflush(tracefp);
2N/A return (0);
2N/A}
2N/A#endif