150990Sbostic /*- 2*61194Sbostic * Copyright (c) 1990, 1993 3*61194Sbostic * The Regents of the University of California. 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*61194Sbostic static char sccsid[] = "@(#)bt_debug.c 8.1 (Berkeley) 06/04/93"; 1350990Sbostic #endif /* LIBC_SCCS and not lint */ 1450990Sbostic 1550990Sbostic #include <sys/param.h> 1656739Sbostic 1750990Sbostic #include <stdio.h> 1850990Sbostic #include <stdlib.h> 1950990Sbostic #include <string.h> 2056739Sbostic 2157932Sbostic #include <db.h> 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", 4260049Sbostic ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize); 4360049Sbostic if (ISSET(t, R_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 ("; 5360049Sbostic X(B_DELCRSR, "DELCRSR"); 5460049Sbostic X(R_FIXLEN, "FIXLEN"); 5560049Sbostic X(B_INMEM, "INMEM"); 5660049Sbostic X(B_NODUPS, "NODUPS"); 5760049Sbostic X(B_RDONLY, "RDONLY"); 5860049Sbostic X(R_RECNO, "RECNO"); 5960049Sbostic X(B_SEQINIT, "SEQINIT"); 6060049Sbostic X(B_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 = " ("; 9960049Sbostic X(B_NODUPS, "NODUPS"); 10060049Sbostic X(R_RECNO, "RECNO"); 10151102Sbostic (void)fprintf(stderr, ")"); 10251102Sbostic } 10351102Sbostic } 10451102Sbostic 10551102Sbostic /* 10656994Sbostic * BT_DNPAGE -- Dump the page 10756994Sbostic * 10856994Sbostic * Parameters: 10956994Sbostic * n: page number to dump. 11056994Sbostic */ 11156994Sbostic void 11256994Sbostic __bt_dnpage(dbp, pgno) 11356994Sbostic DB *dbp; 11456994Sbostic pgno_t pgno; 11556994Sbostic { 11656994Sbostic BTREE *t; 11756994Sbostic PAGE *h; 11856994Sbostic 11956994Sbostic t = dbp->internal; 12056994Sbostic if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) { 12156994Sbostic __bt_dpage(h); 12256994Sbostic (void)mpool_put(t->bt_mp, h, 0); 12356994Sbostic } 12456994Sbostic } 12556994Sbostic 12656994Sbostic /* 12750990Sbostic * BT_DPAGE -- Dump the page 12850990Sbostic * 12950990Sbostic * Parameters: 13050990Sbostic * h: pointer to the PAGE 13150990Sbostic */ 13250990Sbostic void 13350990Sbostic __bt_dpage(h) 13450990Sbostic PAGE *h; 13550990Sbostic { 13650990Sbostic BINTERNAL *bi; 13750990Sbostic BLEAF *bl; 13850990Sbostic RINTERNAL *ri; 13950990Sbostic RLEAF *rl; 14057989Sbostic indx_t cur, top; 14150990Sbostic char *sep; 14250990Sbostic 14350990Sbostic (void)fprintf(stderr, " page %d: (", h->pgno); 14451102Sbostic #undef X 14550990Sbostic #define X(flag, name) \ 14650990Sbostic if (h->flags & flag) { \ 14750990Sbostic (void)fprintf(stderr, "%s%s", sep, name); \ 14850990Sbostic sep = ", "; \ 14950990Sbostic } 15050990Sbostic sep = ""; 15150990Sbostic X(P_BINTERNAL, "BINTERNAL") /* types */ 15250990Sbostic X(P_BLEAF, "BLEAF") 15350990Sbostic X(P_RINTERNAL, "RINTERNAL") /* types */ 15450990Sbostic X(P_RLEAF, "RLEAF") 15550990Sbostic X(P_OVERFLOW, "OVERFLOW") 15650990Sbostic X(P_PRESERVE, "PRESERVE"); 15750990Sbostic (void)fprintf(stderr, ")\n"); 15850990Sbostic #undef X 15950990Sbostic 16050990Sbostic (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg); 16150990Sbostic if (h->flags & P_OVERFLOW) 16250990Sbostic return; 16350990Sbostic 16450990Sbostic top = NEXTINDEX(h); 16550990Sbostic (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n", 16650990Sbostic h->lower, h->upper, top); 16750990Sbostic for (cur = 0; cur < top; cur++) { 16850990Sbostic (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]); 16950990Sbostic switch(h->flags & P_TYPE) { 17050990Sbostic case P_BINTERNAL: 17150990Sbostic bi = GETBINTERNAL(h, cur); 17250990Sbostic (void)fprintf(stderr, 17357446Sbostic "size %03d pgno %03d", bi->ksize, bi->pgno); 17450990Sbostic if (bi->flags & P_BIGKEY) 17550990Sbostic (void)fprintf(stderr, " (indirect)"); 17650990Sbostic else if (bi->ksize) 17757446Sbostic (void)fprintf(stderr, 17860049Sbostic " {%.*s}", (int)bi->ksize, bi->bytes); 17950990Sbostic break; 18050990Sbostic case P_RINTERNAL: 18150990Sbostic ri = GETRINTERNAL(h, cur); 18257446Sbostic (void)fprintf(stderr, "entries %03d pgno %03d", 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) 20160049Sbostic (void)fprintf(stderr, "%.*s", 20260049Sbostic (int)bl->dsize, 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) 21257446Sbostic (void)fprintf(stderr, 21360049Sbostic "%.*s", (int)rl->dsize, rl->bytes); 21450990Sbostic break; 21550990Sbostic } 21650990Sbostic (void)fprintf(stderr, "\n"); 21750990Sbostic } 21850990Sbostic } 21950990Sbostic #endif 22050990Sbostic 22150990Sbostic #ifdef STATISTICS 22250990Sbostic /* 22350990Sbostic * BT_STAT -- Gather/print the tree statistics 22450990Sbostic * 22550990Sbostic * Parameters: 22650990Sbostic * dbp: pointer to the DB 22750990Sbostic */ 22850990Sbostic void 22950990Sbostic __bt_stat(dbp) 23050990Sbostic DB *dbp; 23150990Sbostic { 23250990Sbostic extern u_long bt_cache_hit, bt_cache_miss; 23350990Sbostic extern u_long bt_rootsplit, bt_split, bt_sortsplit; 23450990Sbostic extern u_long bt_pfxsaved; 23550990Sbostic BTREE *t; 23650990Sbostic PAGE *h; 23750990Sbostic pgno_t i, pcont, pinternal, pleaf; 23850990Sbostic u_long ifree, lfree, nkeys; 23950990Sbostic int levels; 24050990Sbostic 24150990Sbostic t = dbp->internal; 24250990Sbostic pcont = pinternal = pleaf = 0; 24350990Sbostic nkeys = ifree = lfree = 0; 24450990Sbostic for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 24550990Sbostic switch(h->flags & P_TYPE) { 24650990Sbostic case P_BINTERNAL: 24750990Sbostic case P_RINTERNAL: 24850990Sbostic ++pinternal; 24950990Sbostic ifree += h->upper - h->lower; 25050990Sbostic break; 25150990Sbostic case P_BLEAF: 25250990Sbostic case P_RLEAF: 25350990Sbostic ++pleaf; 25450990Sbostic lfree += h->upper - h->lower; 25550990Sbostic nkeys += NEXTINDEX(h); 25650990Sbostic break; 25750990Sbostic case P_OVERFLOW: 25850990Sbostic ++pcont; 25950990Sbostic break; 26050990Sbostic } 26150990Sbostic (void)mpool_put(t->bt_mp, h, 0); 26250990Sbostic } 26350990Sbostic 26450990Sbostic /* Count the levels of the tree. */ 26550990Sbostic for (i = P_ROOT, levels = 0 ;; ++levels) { 26650990Sbostic h = mpool_get(t->bt_mp, i, 0); 26750990Sbostic if (h->flags & (P_BLEAF|P_RLEAF)) { 26850990Sbostic if (levels == 0) 26950990Sbostic levels = 1; 27050990Sbostic (void)mpool_put(t->bt_mp, h, 0); 27150990Sbostic break; 27250990Sbostic } 27360049Sbostic i = ISSET(t, R_RECNO) ? 27450990Sbostic GETRINTERNAL(h, 0)->pgno : 27550990Sbostic GETBINTERNAL(h, 0)->pgno; 27650990Sbostic (void)mpool_put(t->bt_mp, h, 0); 27750990Sbostic } 27850990Sbostic 27950990Sbostic (void)fprintf(stderr, "%d level%s with %ld keys", 28050990Sbostic levels, levels == 1 ? "" : "s", nkeys); 28160049Sbostic if (ISSET(t, R_RECNO)) 28250990Sbostic (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs); 28350990Sbostic (void)fprintf(stderr, 28450990Sbostic "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n", 28550990Sbostic pinternal + pleaf + pcont, pleaf, pinternal, pcont); 28650990Sbostic (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n", 28750990Sbostic bt_cache_hit, bt_cache_miss); 28850990Sbostic (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n", 28950990Sbostic bt_split, bt_rootsplit, bt_sortsplit); 29050990Sbostic pleaf *= t->bt_psize - BTDATAOFF; 29150990Sbostic if (pleaf) 29250990Sbostic (void)fprintf(stderr, 29350990Sbostic "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n", 29450990Sbostic ((double)(pleaf - lfree) / pleaf) * 100, 29550990Sbostic pleaf - lfree, lfree); 29650990Sbostic pinternal *= t->bt_psize - BTDATAOFF; 29750990Sbostic if (pinternal) 29850990Sbostic (void)fprintf(stderr, 29950990Sbostic "%.0f%% internal fill (%ld bytes used, %ld bytes free\n", 30050990Sbostic ((double)(pinternal - ifree) / pinternal) * 100, 30150990Sbostic pinternal - ifree, ifree); 30250990Sbostic if (bt_pfxsaved) 30350990Sbostic (void)fprintf(stderr, "prefix checking removed %lu bytes.\n", 30450990Sbostic bt_pfxsaved); 30550990Sbostic } 30650990Sbostic #endif 307