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