bt_debug.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993, 1994
00003  * The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Mike Olson.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  * This product includes software developed by the University of
00019  * California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #if defined(LIBC_SCCS) && !defined(lint)
00038 static char sccsid[] = "@(#)bt_debug.c 8.5 (Berkeley) 8/17/94";
00039 #endif /* LIBC_SCCS and not lint */
00040 
00041 #ifdef DEBUG
00042 #include <sys/param.h>
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 
00048 #include "../include/db.h"
00049 #include "btree.h"
00050 
00051 /*
00052  * BT_DUMP -- Dump the tree
00053  *
00054  * Parameters:
00055  * dbp:  pointer to the DB
00056  */
00057 void
00058 __bt_dump(dbp)
00059    DB *dbp;
00060 {
00061    BTREE *t;
00062    PAGE *h;
00063    pgno_t i;
00064    char *sep;
00065 
00066    t = dbp->internal;
00067    (void)fprintf(stderr, "%s: pgsz %d",
00068        F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
00069    if (F_ISSET(t, R_RECNO))
00070       (void)fprintf(stderr, " keys %lu", t->bt_nrecs);
00071 #undef X
00072 #define  X(flag, name) \
00073    if (F_ISSET(t, flag)) { \
00074       (void)fprintf(stderr, "%s%s", sep, name); \
00075       sep = ", "; \
00076    }
00077    if (t->flags != 0) {
00078       sep = " flags (";
00079       X(R_FIXLEN, "FIXLEN");
00080       X(B_INMEM,  "INMEM");
00081       X(B_NODUPS, "NODUPS");
00082       X(B_RDONLY, "RDONLY");
00083       X(R_RECNO,  "RECNO");
00084       X(B_METADIRTY,"METADIRTY");
00085       (void)fprintf(stderr, ")\n");
00086    }
00087 #undef X
00088 
00089    for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
00090       __bt_dpage(h);
00091       (void)mpool_put(t->bt_mp, h, 0);
00092    }
00093 }
00094 
00095 /*
00096  * BT_DMPAGE -- Dump the meta page
00097  *
00098  * Parameters:
00099  * h: pointer to the PAGE
00100  */
00101 void
00102 __bt_dmpage(h)
00103    PAGE *h;
00104 {
00105    BTMETA *m;
00106    char *sep;
00107 
00108    m = (BTMETA *)h;
00109    (void)fprintf(stderr, "magic %lx\n", m->magic);
00110    (void)fprintf(stderr, "version %lu\n", m->version);
00111    (void)fprintf(stderr, "psize %lu\n", m->psize);
00112    (void)fprintf(stderr, "free %lu\n", m->free);
00113    (void)fprintf(stderr, "nrecs %lu\n", m->nrecs);
00114    (void)fprintf(stderr, "flags %lu", m->flags);
00115 #undef X
00116 #define  X(flag, name) \
00117    if (m->flags & flag) { \
00118       (void)fprintf(stderr, "%s%s", sep, name); \
00119       sep = ", "; \
00120    }
00121    if (m->flags) {
00122       sep = " (";
00123       X(B_NODUPS, "NODUPS");
00124       X(R_RECNO,  "RECNO");
00125       (void)fprintf(stderr, ")");
00126    }
00127 }
00128 
00129 /*
00130  * BT_DNPAGE -- Dump the page
00131  *
00132  * Parameters:
00133  * n: page number to dump.
00134  */
00135 void
00136 __bt_dnpage(dbp, pgno)
00137    DB *dbp;
00138    pgno_t pgno;
00139 {
00140    BTREE *t;
00141    PAGE *h;
00142 
00143    t = dbp->internal;
00144    if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
00145       __bt_dpage(h);
00146       (void)mpool_put(t->bt_mp, h, 0);
00147    }
00148 }
00149 
00150 /*
00151  * BT_DPAGE -- Dump the page
00152  *
00153  * Parameters:
00154  * h: pointer to the PAGE
00155  */
00156 void
00157 __bt_dpage(h)
00158    PAGE *h;
00159 {
00160    BINTERNAL *bi;
00161    BLEAF *bl;
00162    RINTERNAL *ri;
00163    RLEAF *rl;
00164    indx_t cur, top;
00165    char *sep;
00166 
00167    (void)fprintf(stderr, "    page %d: (", h->pgno);
00168 #undef X
00169 #define  X(flag, name) \
00170    if (h->flags & flag) { \
00171       (void)fprintf(stderr, "%s%s", sep, name); \
00172       sep = ", "; \
00173    }
00174    sep = "";
00175    X(P_BINTERNAL, "BINTERNAL")      /* types */
00176    X(P_BLEAF,  "BLEAF")
00177    X(P_RINTERNAL, "RINTERNAL")      /* types */
00178    X(P_RLEAF,  "RLEAF")
00179    X(P_OVERFLOW,  "OVERFLOW")
00180    X(P_PRESERVE,  "PRESERVE");
00181    (void)fprintf(stderr, ")\n");
00182 #undef X
00183 
00184    (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
00185    if (h->flags & P_OVERFLOW)
00186       return;
00187 
00188    top = NEXTINDEX(h);
00189    (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
00190        h->lower, h->upper, top);
00191    for (cur = 0; cur < top; cur++) {
00192       (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
00193       switch (h->flags & P_TYPE) {
00194       case P_BINTERNAL:
00195          bi = GETBINTERNAL(h, cur);
00196          (void)fprintf(stderr,
00197              "size %03d pgno %03d", bi->ksize, bi->pgno);
00198          if (bi->flags & P_BIGKEY)
00199             (void)fprintf(stderr, " (indirect)");
00200          else if (bi->ksize)
00201             (void)fprintf(stderr,
00202                 " {%.*s}", (int)bi->ksize, bi->bytes);
00203          break;
00204       case P_RINTERNAL:
00205          ri = GETRINTERNAL(h, cur);
00206          (void)fprintf(stderr, "entries %03d pgno %03d",
00207             ri->nrecs, ri->pgno);
00208          break;
00209       case P_BLEAF:
00210          bl = GETBLEAF(h, cur);
00211          if (bl->flags & P_BIGKEY)
00212             (void)fprintf(stderr,
00213                 "big key page %lu size %u/",
00214                 *(pgno_t *)bl->bytes,
00215                 *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
00216          else if (bl->ksize)
00217             (void)fprintf(stderr, "%s/", bl->bytes);
00218          if (bl->flags & P_BIGDATA)
00219             (void)fprintf(stderr,
00220                 "big data page %lu size %u",
00221                 *(pgno_t *)(bl->bytes + bl->ksize),
00222                 *(u_int32_t *)(bl->bytes + bl->ksize +
00223                 sizeof(pgno_t)));
00224          else if (bl->dsize)
00225             (void)fprintf(stderr, "%.*s",
00226                 (int)bl->dsize, bl->bytes + bl->ksize);
00227          break;
00228       case P_RLEAF:
00229          rl = GETRLEAF(h, cur);
00230          if (rl->flags & P_BIGDATA)
00231             (void)fprintf(stderr,
00232                 "big data page %lu size %u",
00233                 *(pgno_t *)rl->bytes,
00234                 *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
00235          else if (rl->dsize)
00236             (void)fprintf(stderr,
00237                 "%.*s", (int)rl->dsize, rl->bytes);
00238          break;
00239       }
00240       (void)fprintf(stderr, "\n");
00241    }
00242 }
00243 #endif
00244 
00245 #ifdef STATISTICS
00246 /*
00247  * BT_STAT -- Gather/print the tree statistics
00248  *
00249  * Parameters:
00250  * dbp:  pointer to the DB
00251  */
00252 void
00253 __bt_stat(dbp)
00254    DB *dbp;
00255 {
00256    extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
00257    extern u_long bt_sortsplit, bt_split;
00258    BTREE *t;
00259    PAGE *h;
00260    pgno_t i, pcont, pinternal, pleaf;
00261    u_long ifree, lfree, nkeys;
00262    int levels;
00263 
00264    t = dbp->internal;
00265    pcont = pinternal = pleaf = 0;
00266    nkeys = ifree = lfree = 0;
00267    for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
00268       switch (h->flags & P_TYPE) {
00269       case P_BINTERNAL:
00270       case P_RINTERNAL:
00271          ++pinternal;
00272          ifree += h->upper - h->lower;
00273          break;
00274       case P_BLEAF:
00275       case P_RLEAF:
00276          ++pleaf;
00277          lfree += h->upper - h->lower;
00278          nkeys += NEXTINDEX(h);
00279          break;
00280       case P_OVERFLOW:
00281          ++pcont;
00282          break;
00283       }
00284       (void)mpool_put(t->bt_mp, h, 0);
00285    }
00286 
00287    /* Count the levels of the tree. */
00288    for (i = P_ROOT, levels = 0 ;; ++levels) {
00289       h = mpool_get(t->bt_mp, i, 0);
00290       if (h->flags & (P_BLEAF|P_RLEAF)) {
00291          if (levels == 0)
00292             levels = 1;
00293          (void)mpool_put(t->bt_mp, h, 0);
00294          break;
00295       }
00296       i = F_ISSET(t, R_RECNO) ?
00297           GETRINTERNAL(h, 0)->pgno :
00298           GETBINTERNAL(h, 0)->pgno;
00299       (void)mpool_put(t->bt_mp, h, 0);
00300    }
00301 
00302    (void)fprintf(stderr, "%d level%s with %ld keys",
00303        levels, levels == 1 ? "" : "s", nkeys);
00304    if (F_ISSET(t, R_RECNO))
00305       (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
00306    (void)fprintf(stderr,
00307        "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
00308        pinternal + pleaf + pcont, pleaf, pinternal, pcont);
00309    (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
00310        bt_cache_hit, bt_cache_miss);
00311    (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
00312        bt_split, bt_rootsplit, bt_sortsplit);
00313    pleaf *= t->bt_psize - BTDATAOFF;
00314    if (pleaf)
00315       (void)fprintf(stderr,
00316           "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
00317           ((double)(pleaf - lfree) / pleaf) * 100,
00318           pleaf - lfree, lfree);
00319    pinternal *= t->bt_psize - BTDATAOFF;
00320    if (pinternal)
00321       (void)fprintf(stderr,
00322           "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
00323           ((double)(pinternal - ifree) / pinternal) * 100,
00324           pinternal - ifree, ifree);
00325    if (bt_pfxsaved)
00326       (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
00327           bt_pfxsaved);
00328 }
00329 #endif

Generated on Thu Apr 16 06:27:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6