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.7 (Berkeley) 02/14/93"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/param.h> 16 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 21 #include <db.h> 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_DNPAGE -- Dump the page 108 * 109 * Parameters: 110 * n: page number to dump. 111 */ 112 void 113 __bt_dnpage(dbp, pgno) 114 DB *dbp; 115 pgno_t pgno; 116 { 117 BTREE *t; 118 PAGE *h; 119 120 t = dbp->internal; 121 if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) { 122 __bt_dpage(h); 123 (void)mpool_put(t->bt_mp, h, 0); 124 } 125 } 126 127 /* 128 * BT_DPAGE -- Dump the page 129 * 130 * Parameters: 131 * h: pointer to the PAGE 132 */ 133 void 134 __bt_dpage(h) 135 PAGE *h; 136 { 137 BINTERNAL *bi; 138 BLEAF *bl; 139 RINTERNAL *ri; 140 RLEAF *rl; 141 indx_t cur, top; 142 char *sep; 143 144 (void)fprintf(stderr, " page %d: (", h->pgno); 145 #undef X 146 #define X(flag, name) \ 147 if (h->flags & flag) { \ 148 (void)fprintf(stderr, "%s%s", sep, name); \ 149 sep = ", "; \ 150 } 151 sep = ""; 152 X(P_BINTERNAL, "BINTERNAL") /* types */ 153 X(P_BLEAF, "BLEAF") 154 X(P_RINTERNAL, "RINTERNAL") /* types */ 155 X(P_RLEAF, "RLEAF") 156 X(P_OVERFLOW, "OVERFLOW") 157 X(P_PRESERVE, "PRESERVE"); 158 (void)fprintf(stderr, ")\n"); 159 #undef X 160 161 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg); 162 if (h->flags & P_OVERFLOW) 163 return; 164 165 top = NEXTINDEX(h); 166 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n", 167 h->lower, h->upper, top); 168 for (cur = 0; cur < top; cur++) { 169 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]); 170 switch(h->flags & P_TYPE) { 171 case P_BINTERNAL: 172 bi = GETBINTERNAL(h, cur); 173 (void)fprintf(stderr, 174 "size %03d pgno %03d", bi->ksize, bi->pgno); 175 if (bi->flags & P_BIGKEY) 176 (void)fprintf(stderr, " (indirect)"); 177 else if (bi->ksize) 178 (void)fprintf(stderr, 179 " {%.*s}", bi->ksize, bi->bytes); 180 break; 181 case P_RINTERNAL: 182 ri = GETRINTERNAL(h, cur); 183 (void)fprintf(stderr, "entries %03d pgno %03d", 184 ri->nrecs, ri->pgno); 185 break; 186 case P_BLEAF: 187 bl = GETBLEAF(h, cur); 188 if (bl->flags & P_BIGKEY) 189 (void)fprintf(stderr, 190 "big key page %lu size %u/", 191 *(pgno_t *)bl->bytes, 192 *(size_t *)(bl->bytes + sizeof(pgno_t))); 193 else if (bl->ksize) 194 (void)fprintf(stderr, "%s/", bl->bytes); 195 if (bl->flags & P_BIGDATA) 196 (void)fprintf(stderr, 197 "big data page %lu size %u", 198 *(pgno_t *)(bl->bytes + bl->ksize), 199 *(size_t *)(bl->bytes + bl->ksize + 200 sizeof(pgno_t))); 201 else if (bl->dsize) 202 (void)fprintf(stderr, 203 "%.*s", bl->dsize, bl->bytes + bl->ksize); 204 break; 205 case P_RLEAF: 206 rl = GETRLEAF(h, cur); 207 if (rl->flags & P_BIGDATA) 208 (void)fprintf(stderr, 209 "big data page %lu size %u", 210 *(pgno_t *)rl->bytes, 211 *(size_t *)(rl->bytes + sizeof(pgno_t))); 212 else if (rl->dsize) 213 (void)fprintf(stderr, 214 "%.*s", rl->dsize, rl->bytes); 215 break; 216 } 217 (void)fprintf(stderr, "\n"); 218 } 219 } 220 #endif 221 222 #ifdef STATISTICS 223 /* 224 * BT_STAT -- Gather/print the tree statistics 225 * 226 * Parameters: 227 * dbp: pointer to the DB 228 */ 229 void 230 __bt_stat(dbp) 231 DB *dbp; 232 { 233 extern u_long bt_cache_hit, bt_cache_miss; 234 extern u_long bt_rootsplit, bt_split, bt_sortsplit; 235 extern u_long bt_pfxsaved; 236 BTREE *t; 237 PAGE *h; 238 pgno_t i, pcont, pinternal, pleaf; 239 u_long ifree, lfree, nkeys; 240 int levels; 241 242 t = dbp->internal; 243 pcont = pinternal = pleaf = 0; 244 nkeys = ifree = lfree = 0; 245 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 246 switch(h->flags & P_TYPE) { 247 case P_BINTERNAL: 248 case P_RINTERNAL: 249 ++pinternal; 250 ifree += h->upper - h->lower; 251 break; 252 case P_BLEAF: 253 case P_RLEAF: 254 ++pleaf; 255 lfree += h->upper - h->lower; 256 nkeys += NEXTINDEX(h); 257 break; 258 case P_OVERFLOW: 259 ++pcont; 260 break; 261 } 262 (void)mpool_put(t->bt_mp, h, 0); 263 } 264 265 /* Count the levels of the tree. */ 266 for (i = P_ROOT, levels = 0 ;; ++levels) { 267 h = mpool_get(t->bt_mp, i, 0); 268 if (h->flags & (P_BLEAF|P_RLEAF)) { 269 if (levels == 0) 270 levels = 1; 271 (void)mpool_put(t->bt_mp, h, 0); 272 break; 273 } 274 i = ISSET(t, BTF_RECNO) ? 275 GETRINTERNAL(h, 0)->pgno : 276 GETBINTERNAL(h, 0)->pgno; 277 (void)mpool_put(t->bt_mp, h, 0); 278 } 279 280 (void)fprintf(stderr, "%d level%s with %ld keys", 281 levels, levels == 1 ? "" : "s", nkeys); 282 if (ISSET(t, BTF_RECNO)) 283 (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs); 284 (void)fprintf(stderr, 285 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n", 286 pinternal + pleaf + pcont, pleaf, pinternal, pcont); 287 (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n", 288 bt_cache_hit, bt_cache_miss); 289 (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n", 290 bt_split, bt_rootsplit, bt_sortsplit); 291 pleaf *= t->bt_psize - BTDATAOFF; 292 if (pleaf) 293 (void)fprintf(stderr, 294 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n", 295 ((double)(pleaf - lfree) / pleaf) * 100, 296 pleaf - lfree, lfree); 297 pinternal *= t->bt_psize - BTDATAOFF; 298 if (pinternal) 299 (void)fprintf(stderr, 300 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n", 301 ((double)(pinternal - ifree) / pinternal) * 100, 302 pinternal - ifree, ifree); 303 if (bt_pfxsaved) 304 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n", 305 bt_pfxsaved); 306 } 307 #endif 308