root/libdb/bt_debug.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __bt_dump
  2. __bt_dmpage
  3. __bt_dnpage
  4. __bt_dpage
  5. __bt_stat

   1 /*-
   2  * Copyright (c) 1990, 1993, 1994
   3  *      The Regents of the University of California.  All rights reserved.
   4  *
   5  * This code is derived from software contributed to Berkeley by
   6  * Mike Olson.
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions
  10  * are met:
  11  * 1. Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  * 2. Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in the
  15  *    documentation and/or other materials provided with the distribution.
  16  * 3. Neither the name of the University nor the names of its contributors
  17  *    may be used to endorse or promote products derived from this software
  18  *    without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30  * SUCH DAMAGE.
  31  */
  32 
  33 #if defined(LIBC_SCCS) && !defined(lint)
  34 static char sccsid[] = "@(#)bt_debug.c  8.5 (Berkeley) 8/17/94";
  35 #endif /* LIBC_SCCS and not lint */
  36 
  37 #ifdef HAVE_CONFIG_H
  38 #include <config.h>
  39 #endif
  40 #include <stdio.h>
  41 #ifdef STDC_HEADERS
  42 #include <stdlib.h>
  43 #endif
  44 #ifdef HAVE_STRING_H
  45 #include <string.h>
  46 #else
  47 #include <strings.h>
  48 #endif
  49 
  50 #include "db.h"
  51 #include "btree.h"
  52 
  53 /**
  54  * BT_DUMP -- Dump the tree
  55  *
  56  *      @param 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", (long unsigned int)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  *      @param h        pointer to the #PAGE
 100  */
 101 void
 102 __bt_dmpage(h)
 103         PAGE *h;
 104 {
 105         BTMETA *m;
 106         char *sep;
 107 
 108         m = (BTMETA *)h;
 109         (void)fprintf(stderr, "magic %lx\n", (long unsigned int)m->magic);
 110         (void)fprintf(stderr, "version %lu\n", (long unsigned int)m->version);
 111         (void)fprintf(stderr, "psize %lu\n", (long unsigned int)m->psize);
 112         (void)fprintf(stderr, "free %lu\n", (long unsigned int)m->free);
 113         (void)fprintf(stderr, "nrecs %lu\n", (long unsigned int)m->nrecs);
 114         (void)fprintf(stderr, "flags %lu", (long unsigned int)m->flags);
 115 #undef X
 116 #define X(flag, name) \
 117         if (m->flags & flag) { \
 118                 (void)fprintf(stderr, "%s%s", sep, name); \
 119                 sep = ", "; \
 120         }
 121         if (m->flags) {
 122                 sep = " (";
 123                 X(B_NODUPS,     "NODUPS");
 124                 X(R_RECNO,      "RECNO");
 125                 (void)fprintf(stderr, ")");
 126         }
 127 }
 128 
 129 /**
 130  * BT_DNPAGE -- Dump the page
 131  *
 132  *      @param dbp
 133  *      @param pgno     page number to dump.
 134  */
 135 void
 136 __bt_dnpage(dbp, pgno)
 137         DB *dbp;
 138         pgno_t pgno;
 139 {
 140         BTREE *t;
 141         PAGE *h;
 142 
 143         t = dbp->internal;
 144         if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
 145                 __bt_dpage(h);
 146                 (void)mpool_put(t->bt_mp, h, 0);
 147         }
 148 }
 149 
 150 /**
 151  * BT_DPAGE -- Dump the page
 152  *
 153  *      @param h        pointer to the #PAGE
 154  */
 155 void
 156 __bt_dpage(h)
 157         PAGE *h;
 158 {
 159         BINTERNAL *bi;
 160         BLEAF *bl;
 161         RINTERNAL *ri;
 162         RLEAF *rl;
 163         indx_t cur, top;
 164         char *sep;
 165 
 166         (void)fprintf(stderr, "    page %d: (", h->pgno);
 167 #undef X
 168 #define X(flag, name) \
 169         if (h->flags & flag) { \
 170                 (void)fprintf(stderr, "%s%s", sep, name); \
 171                 sep = ", "; \
 172         }
 173         sep = "";
 174         X(P_BINTERNAL,  "BINTERNAL")            /* types */
 175         X(P_BLEAF,      "BLEAF")
 176         X(P_RINTERNAL,  "RINTERNAL")            /* types */
 177         X(P_RLEAF,      "RLEAF")
 178         X(P_OVERFLOW,   "OVERFLOW")
 179         X(P_PRESERVE,   "PRESERVE");
 180         (void)fprintf(stderr, ")\n");
 181 #undef X
 182 
 183         (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
 184         if (h->flags & P_OVERFLOW)
 185                 return;
 186 
 187         top = NEXTINDEX(h);
 188         (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
 189             h->lower, h->upper, top);
 190         for (cur = 0; cur < top; cur++) {
 191                 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
 192                 switch (h->flags & P_TYPE) {
 193                 case P_BINTERNAL:
 194                         bi = GETBINTERNAL(h, cur);
 195                         (void)fprintf(stderr,
 196                             "size %03d pgno %03d", bi->ksize, bi->pgno);
 197                         if (bi->flags & P_BIGKEY)
 198                                 (void)fprintf(stderr, " (indirect)");
 199                         else if (bi->ksize)
 200                                 (void)fprintf(stderr,
 201                                     " {%.*s}", (int)bi->ksize, bi->bytes);
 202                         break;
 203                 case P_RINTERNAL:
 204                         ri = GETRINTERNAL(h, cur);
 205                         (void)fprintf(stderr, "entries %03d pgno %03d",
 206                                 ri->nrecs, ri->pgno);
 207                         break;
 208                 case P_BLEAF:
 209                         bl = GETBLEAF(h, cur);
 210                         if (bl->flags & P_BIGKEY)
 211                                 (void)fprintf(stderr,
 212                                     "big key page %lu size %u/",
 213                                     (long unsigned int)*(pgno_t *)bl->bytes,
 214                                     (unsigned int)*(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
 215                         else if (bl->ksize)
 216                                 (void)fprintf(stderr, "%s/", bl->bytes);
 217                         if (bl->flags & P_BIGDATA)
 218                                 (void)fprintf(stderr,
 219                                     "big data page %lu size %u",
 220                                     (long unsigned int)*(pgno_t *)(bl->bytes + bl->ksize),
 221                                     (unsigned int)*(u_int32_t *)(bl->bytes + bl->ksize +
 222                                     sizeof(pgno_t)));
 223                         else if (bl->dsize)
 224                                 (void)fprintf(stderr, "%.*s",
 225                                     (int)bl->dsize, bl->bytes + bl->ksize);
 226                         break;
 227                 case P_RLEAF:
 228                         rl = GETRLEAF(h, cur);
 229                         if (rl->flags & P_BIGDATA)
 230                                 (void)fprintf(stderr,
 231                                     "big data page %lu size %u",
 232                                     (long unsigned int)*(pgno_t *)rl->bytes,
 233                                     (unsigned int)*(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
 234                         else if (rl->dsize)
 235                                 (void)fprintf(stderr,
 236                                     "%.*s", (int)rl->dsize, rl->bytes);
 237                         break;
 238                 }
 239                 (void)fprintf(stderr, "\n");
 240         }
 241 }
 242 
 243 /**
 244  * BT_STAT -- Gather/print the tree statistics
 245  *
 246  *      @param dbp      pointer to the #DB
 247  */
 248 void
 249 __bt_stat(dbp)
 250         DB *dbp;
 251 {
 252         extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
 253         extern u_long bt_sortsplit, bt_split;
 254         BTREE *t;
 255         PAGE *h;
 256         pgno_t i, pcont, pinternal, pleaf;
 257         u_long ifree, lfree, nkeys;
 258         int levels;
 259 
 260         t = dbp->internal;
 261         pcont = pinternal = pleaf = 0;
 262         nkeys = ifree = lfree = 0;
 263         for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
 264                 switch (h->flags & P_TYPE) {
 265                 case P_BINTERNAL:
 266                 case P_RINTERNAL:
 267                         ++pinternal;
 268                         ifree += h->upper - h->lower;
 269                         break;
 270                 case P_BLEAF:
 271                 case P_RLEAF:
 272                         ++pleaf;
 273                         lfree += h->upper - h->lower;
 274                         nkeys += NEXTINDEX(h);
 275                         break;
 276                 case P_OVERFLOW:
 277                         ++pcont;
 278                         break;
 279                 }
 280                 (void)mpool_put(t->bt_mp, h, 0);
 281         }
 282 
 283         /* Count the levels of the tree. */
 284         for (i = P_ROOT, levels = 0 ;; ++levels) {
 285                 h = mpool_get(t->bt_mp, i, 0);
 286                 if (h->flags & (P_BLEAF|P_RLEAF)) {
 287                         if (levels == 0)
 288                                 levels = 1;
 289                         (void)mpool_put(t->bt_mp, h, 0);
 290                         break;
 291                 }
 292                 i = F_ISSET(t, R_RECNO) ?
 293                     GETRINTERNAL(h, 0)->pgno :
 294                     GETBINTERNAL(h, 0)->pgno;
 295                 (void)mpool_put(t->bt_mp, h, 0);
 296         }
 297 
 298         (void)fprintf(stderr, "%d level%s with %ld keys",
 299             levels, levels == 1 ? "" : "s", nkeys);
 300         if (F_ISSET(t, R_RECNO))
 301                 (void)fprintf(stderr, " (%ld header count)", (long int)t->bt_nrecs);
 302         (void)fprintf(stderr,
 303             "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
 304             (long unsigned int)(pinternal + pleaf + pcont), (long unsigned int)pleaf, (long unsigned int)pinternal, (long unsigned int)pcont);
 305         (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
 306             bt_cache_hit, bt_cache_miss);
 307         (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
 308             bt_split, bt_rootsplit, bt_sortsplit);
 309         pleaf *= t->bt_psize - BTDATAOFF;
 310         if (pleaf)
 311                 (void)fprintf(stderr,
 312                     "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
 313                     ((double)(pleaf - lfree) / pleaf) * 100,
 314                     pleaf - lfree, lfree);
 315         pinternal *= t->bt_psize - BTDATAOFF;
 316         if (pinternal)
 317                 (void)fprintf(stderr,
 318                     "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
 319                     ((double)(pinternal - ifree) / pinternal) * 100,
 320                     pinternal - ifree, ifree);
 321         if (bt_pfxsaved)
 322                 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
 323                     bt_pfxsaved);
 324 }

/* [previous][next][first][last][top][bottom][index][help] */