150990Sbostic /*- 250990Sbostic * Copyright (c) 1990 The Regents of the University of California. 350990Sbostic * All rights reserved. 450990Sbostic * 550990Sbostic * This code is derived from software contributed to Berkeley by 650990Sbostic * Mike Olson. 750990Sbostic * 850990Sbostic * %sccs.include.redist.c% 950990Sbostic */ 1050990Sbostic 1150990Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*56994Sbostic static char sccsid[] = "@(#)bt_debug.c 5.4 (Berkeley) 12/04/92"; 1350990Sbostic #endif /* LIBC_SCCS and not lint */ 1450990Sbostic 1550990Sbostic #include <sys/param.h> 1656739Sbostic 1750990Sbostic #include <db.h> 1850990Sbostic #include <stdio.h> 1950990Sbostic #include <stdlib.h> 2050990Sbostic #include <string.h> 2156739Sbostic 2250990Sbostic #include "btree.h" 2350990Sbostic 2450990Sbostic #ifdef DEBUG 2550990Sbostic /* 2650990Sbostic * BT_DUMP -- Dump the tree 2750990Sbostic * 2850990Sbostic * Parameters: 2950990Sbostic * dbp: pointer to the DB 3050990Sbostic */ 3150990Sbostic void 3250990Sbostic __bt_dump(dbp) 3350990Sbostic DB *dbp; 3450990Sbostic { 3550990Sbostic BTREE *t; 3650990Sbostic PAGE *h; 3750990Sbostic pgno_t i; 3850990Sbostic char *sep; 3950990Sbostic 4050990Sbostic t = dbp->internal; 4151102Sbostic (void)fprintf(stderr, "%s: pgsz %d", 4250990Sbostic ISSET(t, BTF_INMEM) ? "memory" : "disk", t->bt_psize); 4350990Sbostic if (ISSET(t, BTF_RECNO)) 4450990Sbostic (void)fprintf(stderr, " keys %lu", t->bt_nrecs); 4551102Sbostic #undef X 4650990Sbostic #define X(flag, name) \ 4750990Sbostic if (ISSET(t, flag)) { \ 4850990Sbostic (void)fprintf(stderr, "%s%s", sep, name); \ 4950990Sbostic sep = ", "; \ 5050990Sbostic } 5150990Sbostic if (t->bt_flags) { 5250990Sbostic sep = " flags ("; 5350990Sbostic X(BTF_DELCRSR, "DELCRSR"); 5450990Sbostic X(BTF_FIXEDLEN, "FIXEDLEN"); 5550990Sbostic X(BTF_INMEM, "INMEM"); 5650990Sbostic X(BTF_NODUPS, "NODUPS"); 5750990Sbostic X(BTF_RDONLY, "RDONLY"); 5850990Sbostic X(BTF_RECNO, "RECNO"); 5950990Sbostic X(BTF_SEQINIT, "SEQINIT"); 6050990Sbostic X(BTF_METADIRTY,"METADIRTY"); 6150990Sbostic (void)fprintf(stderr, ")\n"); 6250990Sbostic } 6350990Sbostic #undef X 6450990Sbostic 6550990Sbostic for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 6650990Sbostic __bt_dpage(h); 6750990Sbostic (void)mpool_put(t->bt_mp, h, 0); 6850990Sbostic } 6950990Sbostic } 7050990Sbostic 7150990Sbostic /* 7251102Sbostic * BT_DMPAGE -- Dump the meta page 7351102Sbostic * 7451102Sbostic * Parameters: 7551102Sbostic * h: pointer to the PAGE 7651102Sbostic */ 7751102Sbostic void 7851102Sbostic __bt_dmpage(h) 7951102Sbostic PAGE *h; 8051102Sbostic { 8151102Sbostic BTMETA *m; 8251102Sbostic char *sep; 8351102Sbostic 8451102Sbostic m = (BTMETA *)h; 8551102Sbostic (void)fprintf(stderr, "magic %lx\n", m->m_magic); 8651102Sbostic (void)fprintf(stderr, "version %lu\n", m->m_version); 8751102Sbostic (void)fprintf(stderr, "psize %lu\n", m->m_psize); 8851102Sbostic (void)fprintf(stderr, "free %lu\n", m->m_free); 8951102Sbostic (void)fprintf(stderr, "nrecs %lu\n", m->m_nrecs); 9051102Sbostic (void)fprintf(stderr, "flags %lu", m->m_flags); 9151102Sbostic #undef X 9251102Sbostic #define X(flag, name) \ 9351102Sbostic if (m->m_flags & flag) { \ 9451102Sbostic (void)fprintf(stderr, "%s%s", sep, name); \ 9551102Sbostic sep = ", "; \ 9651102Sbostic } 9751102Sbostic if (m->m_flags) { 9851102Sbostic sep = " ("; 9951102Sbostic X(BTF_NODUPS, "NODUPS"); 10051102Sbostic X(BTF_RECNO, "RECNO"); 10151102Sbostic (void)fprintf(stderr, ")"); 10251102Sbostic } 10351102Sbostic (void)fprintf(stderr, "\nlorder %lu\n", m->m_lorder); 10451102Sbostic } 10551102Sbostic 10651102Sbostic /* 107*56994Sbostic * BT_DNPAGE -- Dump the page 108*56994Sbostic * 109*56994Sbostic * Parameters: 110*56994Sbostic * n: page number to dump. 111*56994Sbostic */ 112*56994Sbostic void 113*56994Sbostic __bt_dnpage(dbp, pgno) 114*56994Sbostic DB *dbp; 115*56994Sbostic pgno_t pgno; 116*56994Sbostic { 117*56994Sbostic BTREE *t; 118*56994Sbostic PAGE *h; 119*56994Sbostic 120*56994Sbostic t = dbp->internal; 121*56994Sbostic if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) { 122*56994Sbostic __bt_dpage(h); 123*56994Sbostic (void)mpool_put(t->bt_mp, h, 0); 124*56994Sbostic } 125*56994Sbostic } 126*56994Sbostic 127*56994Sbostic /* 12850990Sbostic * BT_DPAGE -- Dump the page 12950990Sbostic * 13050990Sbostic * Parameters: 13150990Sbostic * h: pointer to the PAGE 13250990Sbostic */ 13350990Sbostic void 13450990Sbostic __bt_dpage(h) 13550990Sbostic PAGE *h; 13650990Sbostic { 13750990Sbostic BINTERNAL *bi; 13850990Sbostic BLEAF *bl; 13950990Sbostic RINTERNAL *ri; 14050990Sbostic RLEAF *rl; 14150990Sbostic index_t cur, top; 14250990Sbostic char *sep; 14350990Sbostic 14450990Sbostic (void)fprintf(stderr, " page %d: (", h->pgno); 14551102Sbostic #undef X 14650990Sbostic #define X(flag, name) \ 14750990Sbostic if (h->flags & flag) { \ 14850990Sbostic (void)fprintf(stderr, "%s%s", sep, name); \ 14950990Sbostic sep = ", "; \ 15050990Sbostic } 15150990Sbostic sep = ""; 15250990Sbostic X(P_BINTERNAL, "BINTERNAL") /* types */ 15350990Sbostic X(P_BLEAF, "BLEAF") 15450990Sbostic X(P_RINTERNAL, "RINTERNAL") /* types */ 15550990Sbostic X(P_RLEAF, "RLEAF") 15650990Sbostic X(P_OVERFLOW, "OVERFLOW") 15750990Sbostic X(P_PRESERVE, "PRESERVE"); 15850990Sbostic (void)fprintf(stderr, ")\n"); 15950990Sbostic #undef X 16050990Sbostic 16150990Sbostic (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg); 16250990Sbostic if (h->flags & P_OVERFLOW) 16350990Sbostic return; 16450990Sbostic 16550990Sbostic top = NEXTINDEX(h); 16650990Sbostic (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n", 16750990Sbostic h->lower, h->upper, top); 16850990Sbostic for (cur = 0; cur < top; cur++) { 16950990Sbostic (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]); 17050990Sbostic switch(h->flags & P_TYPE) { 17150990Sbostic case P_BINTERNAL: 17250990Sbostic bi = GETBINTERNAL(h, cur); 17350990Sbostic (void)fprintf(stderr, 17450990Sbostic "size %2d pgno %2d", bi->ksize, bi->pgno); 17550990Sbostic if (bi->flags & P_BIGKEY) 17650990Sbostic (void)fprintf(stderr, " (indirect)"); 17750990Sbostic else if (bi->ksize) 17850990Sbostic (void)fprintf(stderr, " {%s}", bi->bytes); 17950990Sbostic break; 18050990Sbostic case P_RINTERNAL: 18150990Sbostic ri = GETRINTERNAL(h, cur); 18250990Sbostic (void)fprintf(stderr, "entries %2d pgno %2d", 18350990Sbostic ri->nrecs, ri->pgno); 18450990Sbostic break; 18550990Sbostic case P_BLEAF: 18650990Sbostic bl = GETBLEAF(h, cur); 18750990Sbostic if (bl->flags & P_BIGKEY) 18850990Sbostic (void)fprintf(stderr, 18950990Sbostic "big key page %lu size %u/", 19050990Sbostic *(pgno_t *)bl->bytes, 19150990Sbostic *(size_t *)(bl->bytes + sizeof(pgno_t))); 19250990Sbostic else if (bl->ksize) 19350990Sbostic (void)fprintf(stderr, "%s/", bl->bytes); 19450990Sbostic if (bl->flags & P_BIGDATA) 19550990Sbostic (void)fprintf(stderr, 19650990Sbostic "big data page %lu size %u", 19750990Sbostic *(pgno_t *)(bl->bytes + bl->ksize), 19850990Sbostic *(size_t *)(bl->bytes + bl->ksize + 19950990Sbostic sizeof(pgno_t))); 20050990Sbostic else if (bl->dsize) 20150990Sbostic (void)fprintf(stderr, 20250990Sbostic "%s", bl->bytes + bl->ksize); 20350990Sbostic break; 20450990Sbostic case P_RLEAF: 20550990Sbostic rl = GETRLEAF(h, cur); 20650990Sbostic if (rl->flags & P_BIGDATA) 20750990Sbostic (void)fprintf(stderr, 20850990Sbostic "big data page %lu size %u", 20950990Sbostic *(pgno_t *)rl->bytes, 21050990Sbostic *(size_t *)(rl->bytes + sizeof(pgno_t))); 21150990Sbostic else if (rl->dsize) 21250990Sbostic (void)fprintf(stderr, "%s", rl->bytes); 21350990Sbostic break; 21450990Sbostic } 21550990Sbostic (void)fprintf(stderr, "\n"); 21650990Sbostic } 21750990Sbostic } 21850990Sbostic #endif 21950990Sbostic 22050990Sbostic #ifdef STATISTICS 22150990Sbostic /* 22250990Sbostic * BT_STAT -- Gather/print the tree statistics 22350990Sbostic * 22450990Sbostic * Parameters: 22550990Sbostic * dbp: pointer to the DB 22650990Sbostic */ 22750990Sbostic void 22850990Sbostic __bt_stat(dbp) 22950990Sbostic DB *dbp; 23050990Sbostic { 23150990Sbostic extern u_long bt_cache_hit, bt_cache_miss; 23250990Sbostic extern u_long bt_rootsplit, bt_split, bt_sortsplit; 23350990Sbostic extern u_long bt_pfxsaved; 23450990Sbostic BTREE *t; 23550990Sbostic PAGE *h; 23650990Sbostic pgno_t i, pcont, pinternal, pleaf; 23750990Sbostic u_long ifree, lfree, nkeys; 23850990Sbostic int levels; 23950990Sbostic 24050990Sbostic t = dbp->internal; 24150990Sbostic pcont = pinternal = pleaf = 0; 24250990Sbostic nkeys = ifree = lfree = 0; 24350990Sbostic for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 24450990Sbostic switch(h->flags & P_TYPE) { 24550990Sbostic case P_BINTERNAL: 24650990Sbostic case P_RINTERNAL: 24750990Sbostic ++pinternal; 24850990Sbostic ifree += h->upper - h->lower; 24950990Sbostic break; 25050990Sbostic case P_BLEAF: 25150990Sbostic case P_RLEAF: 25250990Sbostic ++pleaf; 25350990Sbostic lfree += h->upper - h->lower; 25450990Sbostic nkeys += NEXTINDEX(h); 25550990Sbostic break; 25650990Sbostic case P_OVERFLOW: 25750990Sbostic ++pcont; 25850990Sbostic break; 25950990Sbostic } 26050990Sbostic (void)mpool_put(t->bt_mp, h, 0); 26150990Sbostic } 26250990Sbostic 26350990Sbostic /* Count the levels of the tree. */ 26450990Sbostic for (i = P_ROOT, levels = 0 ;; ++levels) { 26550990Sbostic h = mpool_get(t->bt_mp, i, 0); 26650990Sbostic if (h->flags & (P_BLEAF|P_RLEAF)) { 26750990Sbostic if (levels == 0) 26850990Sbostic levels = 1; 26950990Sbostic (void)mpool_put(t->bt_mp, h, 0); 27050990Sbostic break; 27150990Sbostic } 27250990Sbostic i = ISSET(t, BTF_RECNO) ? 27350990Sbostic GETRINTERNAL(h, 0)->pgno : 27450990Sbostic GETBINTERNAL(h, 0)->pgno; 27550990Sbostic (void)mpool_put(t->bt_mp, h, 0); 27650990Sbostic } 27750990Sbostic 27850990Sbostic (void)fprintf(stderr, "%d level%s with %ld keys", 27950990Sbostic levels, levels == 1 ? "" : "s", nkeys); 28050990Sbostic if (ISSET(t, BTF_RECNO)) 28150990Sbostic (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs); 28250990Sbostic (void)fprintf(stderr, 28350990Sbostic "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n", 28450990Sbostic pinternal + pleaf + pcont, pleaf, pinternal, pcont); 28550990Sbostic (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n", 28650990Sbostic bt_cache_hit, bt_cache_miss); 28750990Sbostic (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n", 28850990Sbostic bt_split, bt_rootsplit, bt_sortsplit); 28950990Sbostic pleaf *= t->bt_psize - BTDATAOFF; 29050990Sbostic if (pleaf) 29150990Sbostic (void)fprintf(stderr, 29250990Sbostic "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n", 29350990Sbostic ((double)(pleaf - lfree) / pleaf) * 100, 29450990Sbostic pleaf - lfree, lfree); 29550990Sbostic pinternal *= t->bt_psize - BTDATAOFF; 29650990Sbostic if (pinternal) 29750990Sbostic (void)fprintf(stderr, 29850990Sbostic "%.0f%% internal fill (%ld bytes used, %ld bytes free\n", 29950990Sbostic ((double)(pinternal - ifree) / pinternal) * 100, 30050990Sbostic pinternal - ifree, ifree); 30150990Sbostic if (bt_pfxsaved) 30250990Sbostic (void)fprintf(stderr, "prefix checking removed %lu bytes.\n", 30350990Sbostic bt_pfxsaved); 30450990Sbostic } 30550990Sbostic #endif 306