home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / btree / bt_debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  8.6 KB  |  337 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  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. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)bt_debug.c    8.1 (Berkeley) 6/4/93";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  42. #include "OurMalloc.h"
  43. #endif
  44.  
  45. #include <sys/param.h>
  46.  
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50.  
  51. #include <db.h>
  52. #include "btree.h"
  53.  
  54. #ifdef DEBUG
  55. /*
  56.  * BT_DUMP -- Dump the tree
  57.  *
  58.  * Parameters:
  59.  *    dbp:    pointer to the DB
  60.  */
  61. void
  62. __bt_dump(dbp)
  63.     DB *dbp;
  64. {
  65.     BTREE *t;
  66.     PAGE *h;
  67.     pgno_t i;
  68.     char *sep;
  69.  
  70.     t = dbp->internal;
  71.     (void)fprintf(stderr, "%s: pgsz %d",
  72.         ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
  73.     if (ISSET(t, R_RECNO))
  74.         (void)fprintf(stderr, " keys %lu", t->bt_nrecs);
  75. #undef X
  76. #define    X(flag, name) \
  77.     if (ISSET(t, flag)) { \
  78.         (void)fprintf(stderr, "%s%s", sep, name); \
  79.         sep = ", "; \
  80.     }
  81.     if (t->bt_flags) {
  82.         sep = " flags (";
  83.         X(B_DELCRSR,    "DELCRSR");
  84.         X(R_FIXLEN,    "FIXLEN");
  85.         X(B_INMEM,    "INMEM");
  86.         X(B_NODUPS,    "NODUPS");
  87.         X(B_RDONLY,    "RDONLY");
  88.         X(R_RECNO,    "RECNO");
  89.         X(B_SEQINIT,    "SEQINIT");
  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->m_magic);
  116.     (void)fprintf(stderr, "version %lu\n", m->m_version);
  117.     (void)fprintf(stderr, "psize %lu\n", m->m_psize);
  118.     (void)fprintf(stderr, "free %lu\n", m->m_free);
  119.     (void)fprintf(stderr, "nrecs %lu\n", m->m_nrecs);
  120.     (void)fprintf(stderr, "flags %lu", m->m_flags);
  121. #undef X
  122. #define    X(flag, name) \
  123.     if (m->m_flags & flag) { \
  124.         (void)fprintf(stderr, "%s%s", sep, name); \
  125.         sep = ", "; \
  126.     }
  127.     if (m->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.                     *(size_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.                     *(size_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.                     *(size_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;
  263.     extern u_long bt_rootsplit, bt_split, bt_sortsplit;
  264.     extern u_long bt_pfxsaved;
  265.     BTREE *t;
  266.     PAGE *h;
  267.     pgno_t i, pcont, pinternal, pleaf;
  268.     u_long ifree, lfree, nkeys;
  269.     int levels;
  270.  
  271.     t = dbp->internal;
  272.     pcont = pinternal = pleaf = 0;
  273.     nkeys = ifree = lfree = 0;
  274.     for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
  275.         switch(h->flags & P_TYPE) {
  276.         case P_BINTERNAL:
  277.         case P_RINTERNAL:
  278.             ++pinternal;
  279.             ifree += h->upper - h->lower;
  280.             break;
  281.         case P_BLEAF:
  282.         case P_RLEAF:
  283.             ++pleaf;
  284.             lfree += h->upper - h->lower;
  285.             nkeys += NEXTINDEX(h);
  286.             break;
  287.         case P_OVERFLOW:
  288.             ++pcont;
  289.             break;
  290.         }
  291.         (void)mpool_put(t->bt_mp, h, 0);
  292.     }
  293.  
  294.     /* Count the levels of the tree. */
  295.     for (i = P_ROOT, levels = 0 ;; ++levels) {
  296.         h = mpool_get(t->bt_mp, i, 0);
  297.         if (h->flags & (P_BLEAF|P_RLEAF)) {
  298.             if (levels == 0)
  299.                 levels = 1;
  300.             (void)mpool_put(t->bt_mp, h, 0);
  301.             break;
  302.         }
  303.         i = ISSET(t, R_RECNO) ?
  304.             GETRINTERNAL(h, 0)->pgno :
  305.             GETBINTERNAL(h, 0)->pgno;
  306.         (void)mpool_put(t->bt_mp, h, 0);
  307.     }
  308.  
  309.     (void)fprintf(stderr, "%d level%s with %ld keys",
  310.         levels, levels == 1 ? "" : "s", nkeys);
  311.     if (ISSET(t, R_RECNO))
  312.         (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
  313.     (void)fprintf(stderr,
  314.         "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
  315.         pinternal + pleaf + pcont, pleaf, pinternal, pcont);
  316.     (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
  317.         bt_cache_hit, bt_cache_miss);
  318.     (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
  319.         bt_split, bt_rootsplit, bt_sortsplit);
  320.     pleaf *= t->bt_psize - BTDATAOFF;
  321.     if (pleaf)
  322.         (void)fprintf(stderr,
  323.             "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
  324.             ((double)(pleaf - lfree) / pleaf) * 100,
  325.             pleaf - lfree, lfree);
  326.     pinternal *= t->bt_psize - BTDATAOFF;
  327.     if (pinternal)
  328.         (void)fprintf(stderr,
  329.             "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
  330.             ((double)(pinternal - ifree) / pinternal) * 100,
  331.             pinternal - ifree, ifree);
  332.     if (bt_pfxsaved)
  333.         (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
  334.             bt_pfxsaved);
  335. }
  336. #endif
  337.