1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Olson. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)bt_debug.c 5.3 (Berkeley) 11/13/92"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/param.h> 16 17 #include <db.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 22 #include "btree.h" 23 24 #ifdef DEBUG 25 /* 26 * BT_DUMP -- Dump the tree 27 * 28 * Parameters: 29 * dbp: pointer to the DB 30 */ 31 void 32 __bt_dump(dbp) 33 DB *dbp; 34 { 35 BTREE *t; 36 PAGE *h; 37 pgno_t i; 38 char *sep; 39 40 t = dbp->internal; 41 (void)fprintf(stderr, "%s: pgsz %d", 42 ISSET(t, BTF_INMEM) ? "memory" : "disk", t->bt_psize); 43 if (ISSET(t, BTF_RECNO)) 44 (void)fprintf(stderr, " keys %lu", t->bt_nrecs); 45 #undef X 46 #define X(flag, name) \ 47 if (ISSET(t, flag)) { \ 48 (void)fprintf(stderr, "%s%s", sep, name); \ 49 sep = ", "; \ 50 } 51 if (t->bt_flags) { 52 sep = " flags ("; 53 X(BTF_DELCRSR, "DELCRSR"); 54 X(BTF_FIXEDLEN, "FIXEDLEN"); 55 X(BTF_INMEM, "INMEM"); 56 X(BTF_NODUPS, "NODUPS"); 57 X(BTF_RDONLY, "RDONLY"); 58 X(BTF_RECNO, "RECNO"); 59 X(BTF_SEQINIT, "SEQINIT"); 60 X(BTF_METADIRTY,"METADIRTY"); 61 (void)fprintf(stderr, ")\n"); 62 } 63 #undef X 64 65 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 66 __bt_dpage(h); 67 (void)mpool_put(t->bt_mp, h, 0); 68 } 69 } 70 71 /* 72 * BT_DMPAGE -- Dump the meta page 73 * 74 * Parameters: 75 * h: pointer to the PAGE 76 */ 77 void 78 __bt_dmpage(h) 79 PAGE *h; 80 { 81 BTMETA *m; 82 char *sep; 83 84 m = (BTMETA *)h; 85 (void)fprintf(stderr, "magic %lx\n", m->m_magic); 86 (void)fprintf(stderr, "version %lu\n", m->m_version); 87 (void)fprintf(stderr, "psize %lu\n", m->m_psize); 88 (void)fprintf(stderr, "free %lu\n", m->m_free); 89 (void)fprintf(stderr, "nrecs %lu\n", m->m_nrecs); 90 (void)fprintf(stderr, "flags %lu", m->m_flags); 91 #undef X 92 #define X(flag, name) \ 93 if (m->m_flags & flag) { \ 94 (void)fprintf(stderr, "%s%s", sep, name); \ 95 sep = ", "; \ 96 } 97 if (m->m_flags) { 98 sep = " ("; 99 X(BTF_NODUPS, "NODUPS"); 100 X(BTF_RECNO, "RECNO"); 101 (void)fprintf(stderr, ")"); 102 } 103 (void)fprintf(stderr, "\nlorder %lu\n", m->m_lorder); 104 } 105 106 /* 107 * BT_DPAGE -- Dump the page 108 * 109 * Parameters: 110 * h: pointer to the PAGE 111 */ 112 void 113 __bt_dpage(h) 114 PAGE *h; 115 { 116 BINTERNAL *bi; 117 BLEAF *bl; 118 RINTERNAL *ri; 119 RLEAF *rl; 120 index_t cur, top; 121 char *sep; 122 123 (void)fprintf(stderr, " page %d: (", h->pgno); 124 #undef X 125 #define X(flag, name) \ 126 if (h->flags & flag) { \ 127 (void)fprintf(stderr, "%s%s", sep, name); \ 128 sep = ", "; \ 129 } 130 sep = ""; 131 X(P_BINTERNAL, "BINTERNAL") /* types */ 132 X(P_BLEAF, "BLEAF") 133 X(P_RINTERNAL, "RINTERNAL") /* types */ 134 X(P_RLEAF, "RLEAF") 135 X(P_OVERFLOW, "OVERFLOW") 136 X(P_PRESERVE, "PRESERVE"); 137 (void)fprintf(stderr, ")\n"); 138 #undef X 139 140 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg); 141 if (h->flags & P_OVERFLOW) 142 return; 143 144 top = NEXTINDEX(h); 145 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n", 146 h->lower, h->upper, top); 147 for (cur = 0; cur < top; cur++) { 148 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]); 149 switch(h->flags & P_TYPE) { 150 case P_BINTERNAL: 151 bi = GETBINTERNAL(h, cur); 152 (void)fprintf(stderr, 153 "size %2d pgno %2d", bi->ksize, bi->pgno); 154 if (bi->flags & P_BIGKEY) 155 (void)fprintf(stderr, " (indirect)"); 156 else if (bi->ksize) 157 (void)fprintf(stderr, " {%s}", bi->bytes); 158 break; 159 case P_RINTERNAL: 160 ri = GETRINTERNAL(h, cur); 161 (void)fprintf(stderr, "entries %2d pgno %2d", 162 ri->nrecs, ri->pgno); 163 break; 164 case P_BLEAF: 165 bl = GETBLEAF(h, cur); 166 if (bl->flags & P_BIGKEY) 167 (void)fprintf(stderr, 168 "big key page %lu size %u/", 169 *(pgno_t *)bl->bytes, 170 *(size_t *)(bl->bytes + sizeof(pgno_t))); 171 else if (bl->ksize) 172 (void)fprintf(stderr, "%s/", bl->bytes); 173 if (bl->flags & P_BIGDATA) 174 (void)fprintf(stderr, 175 "big data page %lu size %u", 176 *(pgno_t *)(bl->bytes + bl->ksize), 177 *(size_t *)(bl->bytes + bl->ksize + 178 sizeof(pgno_t))); 179 else if (bl->dsize) 180 (void)fprintf(stderr, 181 "%s", bl->bytes + bl->ksize); 182 break; 183 case P_RLEAF: 184 rl = GETRLEAF(h, cur); 185 if (rl->flags & P_BIGDATA) 186 (void)fprintf(stderr, 187 "big data page %lu size %u", 188 *(pgno_t *)rl->bytes, 189 *(size_t *)(rl->bytes + sizeof(pgno_t))); 190 else if (rl->dsize) 191 (void)fprintf(stderr, "%s", rl->bytes); 192 break; 193 } 194 (void)fprintf(stderr, "\n"); 195 } 196 } 197 #endif 198 199 #ifdef STATISTICS 200 /* 201 * BT_STAT -- Gather/print the tree statistics 202 * 203 * Parameters: 204 * dbp: pointer to the DB 205 */ 206 void 207 __bt_stat(dbp) 208 DB *dbp; 209 { 210 extern u_long bt_cache_hit, bt_cache_miss; 211 extern u_long bt_rootsplit, bt_split, bt_sortsplit; 212 extern u_long bt_pfxsaved; 213 BTREE *t; 214 PAGE *h; 215 pgno_t i, pcont, pinternal, pleaf; 216 u_long ifree, lfree, nkeys; 217 int levels; 218 219 t = dbp->internal; 220 pcont = pinternal = pleaf = 0; 221 nkeys = ifree = lfree = 0; 222 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 223 switch(h->flags & P_TYPE) { 224 case P_BINTERNAL: 225 case P_RINTERNAL: 226 ++pinternal; 227 ifree += h->upper - h->lower; 228 break; 229 case P_BLEAF: 230 case P_RLEAF: 231 ++pleaf; 232 lfree += h->upper - h->lower; 233 nkeys += NEXTINDEX(h); 234 break; 235 case P_OVERFLOW: 236 ++pcont; 237 break; 238 } 239 (void)mpool_put(t->bt_mp, h, 0); 240 } 241 242 /* Count the levels of the tree. */ 243 for (i = P_ROOT, levels = 0 ;; ++levels) { 244 h = mpool_get(t->bt_mp, i, 0); 245 if (h->flags & (P_BLEAF|P_RLEAF)) { 246 if (levels == 0) 247 levels = 1; 248 (void)mpool_put(t->bt_mp, h, 0); 249 break; 250 } 251 i = ISSET(t, BTF_RECNO) ? 252 GETRINTERNAL(h, 0)->pgno : 253 GETBINTERNAL(h, 0)->pgno; 254 (void)mpool_put(t->bt_mp, h, 0); 255 } 256 257 (void)fprintf(stderr, "%d level%s with %ld keys", 258 levels, levels == 1 ? "" : "s", nkeys); 259 if (ISSET(t, BTF_RECNO)) 260 (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs); 261 (void)fprintf(stderr, 262 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n", 263 pinternal + pleaf + pcont, pleaf, pinternal, pcont); 264 (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n", 265 bt_cache_hit, bt_cache_miss); 266 (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n", 267 bt_split, bt_rootsplit, bt_sortsplit); 268 pleaf *= t->bt_psize - BTDATAOFF; 269 if (pleaf) 270 (void)fprintf(stderr, 271 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n", 272 ((double)(pleaf - lfree) / pleaf) * 100, 273 pleaf - lfree, lfree); 274 pinternal *= t->bt_psize - BTDATAOFF; 275 if (pinternal) 276 (void)fprintf(stderr, 277 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n", 278 ((double)(pinternal - ifree) / pinternal) * 100, 279 pinternal - ifree, ifree); 280 if (bt_pfxsaved) 281 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n", 282 bt_pfxsaved); 283 } 284 #endif 285