home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / db / btree / bt_debug.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  9KB  |  338 lines

  1. /*    $NetBSD: bt_debug.c,v 1.5 1995/02/27 13:20:12 cgd 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. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. #if 0
  41. static char sccsid[] = "@(#)bt_debug.c    8.3 (Berkeley) 5/31/94";
  42. #else
  43. static char rcsid[] = "$NetBSD: bt_debug.c,v 1.5 1995/02/27 13:20:12 cgd Exp $";
  44. #endif
  45. #endif /* LIBC_SCCS and not lint */
  46.  
  47. #include <sys/param.h>
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52.  
  53. #include <db.h>
  54. #include "btree.h"
  55.  
  56. #ifdef DEBUG
  57. /*
  58.  * BT_DUMP -- Dump the tree
  59.  *
  60.  * Parameters:
  61.  *    dbp:    pointer to the DB
  62.  */
  63. void
  64. __bt_dump(dbp)
  65.     DB *dbp;
  66. {
  67.     BTREE *t;
  68.     PAGE *h;
  69.     pgno_t i;
  70.     char *sep;
  71.  
  72.     t = dbp->internal;
  73.     (void)fprintf(stderr, "%s: pgsz %d",
  74.         ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
  75.     if (ISSET(t, R_RECNO))
  76.         (void)fprintf(stderr, " keys %lu", t->bt_nrecs);
  77. #undef X
  78. #define    X(flag, name) \
  79.     if (ISSET(t, flag)) { \
  80.         (void)fprintf(stderr, "%s%s", sep, name); \
  81.         sep = ", "; \
  82.     }
  83.     if (t->bt_flags) {
  84.         sep = " flags (";
  85.         X(B_DELCRSR,    "DELCRSR");
  86.         X(R_FIXLEN,    "FIXLEN");
  87.         X(B_INMEM,    "INMEM");
  88.         X(B_NODUPS,    "NODUPS");
  89.         X(B_RDONLY,    "RDONLY");
  90.         X(R_RECNO,    "RECNO");
  91.         X(B_SEQINIT,    "SEQINIT");
  92.         X(B_METADIRTY,"METADIRTY");
  93.         (void)fprintf(stderr, ")\n");
  94.     }
  95. #undef X
  96.  
  97.     for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
  98.         __bt_dpage(h);
  99.         (void)mpool_put(t->bt_mp, h, 0);
  100.     }
  101. }
  102.  
  103. /*
  104.  * BT_DMPAGE -- Dump the meta page
  105.  *
  106.  * Parameters:
  107.  *    h:    pointer to the PAGE
  108.  */
  109. void
  110. __bt_dmpage(h)
  111.     PAGE *h;
  112. {
  113.     BTMETA *m;
  114.     char *sep;
  115.  
  116.     m = (BTMETA *)h;
  117.     (void)fprintf(stderr, "magic %lx\n", m->m_magic);
  118.     (void)fprintf(stderr, "version %lu\n", m->m_version);
  119.     (void)fprintf(stderr, "psize %lu\n", m->m_psize);
  120.     (void)fprintf(stderr, "free %lu\n", m->m_free);
  121.     (void)fprintf(stderr, "nrecs %lu\n", m->m_nrecs);
  122.     (void)fprintf(stderr, "flags %lu", m->m_flags);
  123. #undef X
  124. #define    X(flag, name) \
  125.     if (m->m_flags & flag) { \
  126.         (void)fprintf(stderr, "%s%s", sep, name); \
  127.         sep = ", "; \
  128.     }
  129.     if (m->m_flags) {
  130.         sep = " (";
  131.         X(B_NODUPS,    "NODUPS");
  132.         X(R_RECNO,    "RECNO");
  133.         (void)fprintf(stderr, ")");
  134.     }
  135. }
  136.  
  137. /*
  138.  * BT_DNPAGE -- Dump the page
  139.  *
  140.  * Parameters:
  141.  *    n:    page number to dump.
  142.  */
  143. void
  144. __bt_dnpage(dbp, pgno)
  145.     DB *dbp;
  146.     pgno_t pgno;
  147. {
  148.     BTREE *t;
  149.     PAGE *h;
  150.  
  151.     t = dbp->internal;
  152.     if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
  153.         __bt_dpage(h);
  154.         (void)mpool_put(t->bt_mp, h, 0);
  155.     }
  156. }
  157.  
  158. /*
  159.  * BT_DPAGE -- Dump the page
  160.  *
  161.  * Parameters:
  162.  *    h:    pointer to the PAGE
  163.  */
  164. void
  165. __bt_dpage(h)
  166.     PAGE *h;
  167. {
  168.     BINTERNAL *bi;
  169.     BLEAF *bl;
  170.     RINTERNAL *ri;
  171.     RLEAF *rl;
  172.     indx_t cur, top;
  173.     char *sep;
  174.  
  175.     (void)fprintf(stderr, "    page %d: (", h->pgno);
  176. #undef X
  177. #define    X(flag, name) \
  178.     if (h->flags & flag) { \
  179.         (void)fprintf(stderr, "%s%s", sep, name); \
  180.         sep = ", "; \
  181.     }
  182.     sep = "";
  183.     X(P_BINTERNAL,    "BINTERNAL")        /* types */
  184.     X(P_BLEAF,    "BLEAF")
  185.     X(P_RINTERNAL,    "RINTERNAL")        /* types */
  186.     X(P_RLEAF,    "RLEAF")
  187.     X(P_OVERFLOW,    "OVERFLOW")
  188.     X(P_PRESERVE,    "PRESERVE");
  189.     (void)fprintf(stderr, ")\n");
  190. #undef X
  191.  
  192.     (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
  193.     if (h->flags & P_OVERFLOW)
  194.         return;
  195.  
  196.     top = NEXTINDEX(h);
  197.     (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
  198.         h->lower, h->upper, top);
  199.     for (cur = 0; cur < top; cur++) {
  200.         (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
  201.         switch(h->flags & P_TYPE) {
  202.         case P_BINTERNAL:
  203.             bi = GETBINTERNAL(h, cur);
  204.             (void)fprintf(stderr,
  205.                 "size %03d pgno %03d", bi->ksize, bi->pgno);
  206.             if (bi->flags & P_BIGKEY)
  207.                 (void)fprintf(stderr, " (indirect)");
  208.             else if (bi->ksize)
  209.                 (void)fprintf(stderr,
  210.                     " {%.*s}", (int)bi->ksize, bi->bytes);
  211.             break;
  212.         case P_RINTERNAL:
  213.             ri = GETRINTERNAL(h, cur);
  214.             (void)fprintf(stderr, "entries %03d pgno %03d",
  215.                 ri->nrecs, ri->pgno);
  216.             break;
  217.         case P_BLEAF:
  218.             bl = GETBLEAF(h, cur);
  219.             if (bl->flags & P_BIGKEY)
  220.                 (void)fprintf(stderr,
  221.                     "big key page %lu size %u/",
  222.                     *(pgno_t *)bl->bytes,
  223.                     *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
  224.             else if (bl->ksize)
  225.                 (void)fprintf(stderr, "%s/", bl->bytes);
  226.             if (bl->flags & P_BIGDATA)
  227.                 (void)fprintf(stderr,
  228.                     "big data page %lu size %u",
  229.                     *(pgno_t *)(bl->bytes + bl->ksize),
  230.                     *(u_int32_t *)(bl->bytes + bl->ksize +
  231.                     sizeof(pgno_t)));
  232.             else if (bl->dsize)
  233.                 (void)fprintf(stderr, "%.*s",
  234.                     (int)bl->dsize, bl->bytes + bl->ksize);
  235.             break;
  236.         case P_RLEAF:
  237.             rl = GETRLEAF(h, cur);
  238.             if (rl->flags & P_BIGDATA)
  239.                 (void)fprintf(stderr,
  240.                     "big data page %lu size %u",
  241.                     *(pgno_t *)rl->bytes,
  242.                     *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
  243.             else if (rl->dsize)
  244.                 (void)fprintf(stderr,
  245.                     "%.*s", (int)rl->dsize, rl->bytes);
  246.             break;
  247.         }
  248.         (void)fprintf(stderr, "\n");
  249.     }
  250. }
  251. #endif
  252.  
  253. #ifdef STATISTICS
  254. /*
  255.  * BT_STAT -- Gather/print the tree statistics
  256.  *
  257.  * Parameters:
  258.  *    dbp:    pointer to the DB
  259.  */
  260. void
  261. __bt_stat(dbp)
  262.     DB *dbp;
  263. {
  264.     extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
  265.     extern u_long bt_sortsplit, bt_split;
  266.     BTREE *t;
  267.     PAGE *h;
  268.     pgno_t i, pcont, pinternal, pleaf;
  269.     u_long ifree, lfree, nkeys;
  270.     int levels;
  271.  
  272.     t = dbp->internal;
  273.     pcont = pinternal = pleaf = 0;
  274.     nkeys = ifree = lfree = 0;
  275.     for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
  276.         switch(h->flags & P_TYPE) {
  277.         case P_BINTERNAL:
  278.         case P_RINTERNAL:
  279.             ++pinternal;
  280.             ifree += h->upper - h->lower;
  281.             break;
  282.         case P_BLEAF:
  283.         case P_RLEAF:
  284.             ++pleaf;
  285.             lfree += h->upper - h->lower;
  286.             nkeys += NEXTINDEX(h);
  287.             break;
  288.         case P_OVERFLOW:
  289.             ++pcont;
  290.             break;
  291.         }
  292.         (void)mpool_put(t->bt_mp, h, 0);
  293.     }
  294.  
  295.     /* Count the levels of the tree. */
  296.     for (i = P_ROOT, levels = 0 ;; ++levels) {
  297.         h = mpool_get(t->bt_mp, i, 0);
  298.         if (h->flags & (P_BLEAF|P_RLEAF)) {
  299.             if (levels == 0)
  300.                 levels = 1;
  301.             (void)mpool_put(t->bt_mp, h, 0);
  302.             break;
  303.         }
  304.         i = ISSET(t, R_RECNO) ?
  305.             GETRINTERNAL(h, 0)->pgno :
  306.             GETBINTERNAL(h, 0)->pgno;
  307.         (void)mpool_put(t->bt_mp, h, 0);
  308.     }
  309.  
  310.     (void)fprintf(stderr, "%d level%s with %ld keys",
  311.         levels, levels == 1 ? "" : "s", nkeys);
  312.     if (ISSET(t, R_RECNO))
  313.         (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
  314.     (void)fprintf(stderr,
  315.         "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
  316.         pinternal + pleaf + pcont, pleaf, pinternal, pcont);
  317.     (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
  318.         bt_cache_hit, bt_cache_miss);
  319.     (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
  320.         bt_split, bt_rootsplit, bt_sortsplit);
  321.     pleaf *= t->bt_psize - BTDATAOFF;
  322.     if (pleaf)
  323.         (void)fprintf(stderr,
  324.             "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
  325.             ((double)(pleaf - lfree) / pleaf) * 100,
  326.             pleaf - lfree, lfree);
  327.     pinternal *= t->bt_psize - BTDATAOFF;
  328.     if (pinternal)
  329.         (void)fprintf(stderr,
  330.             "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
  331.             ((double)(pinternal - ifree) / pinternal) * 100,
  332.             pinternal - ifree, ifree);
  333.     if (bt_pfxsaved)
  334.         (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
  335.             bt_pfxsaved);
  336. }
  337. #endif
  338.