1 /* $NetBSD: bt_debug.c,v 1.13 2007/02/03 23:46:09 christos 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 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)bt_debug.c 8.5 (Berkeley) 8/17/94"; 43 #else 44 __RCSID("$NetBSD: bt_debug.c,v 1.13 2007/02/03 23:46:09 christos Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include <assert.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 53 #include <db.h> 54 #include "btree.h" 55 56 #ifdef DEBUG 57 /* 58 * BT_DUMP -- Dump the tree 59 * 60 * Parameters: 61 * dbp: pointer to the DB 62 */ 63 void 64 __bt_dump(DB *dbp) 65 { 66 BTREE *t; 67 PAGE *h; 68 pgno_t i; 69 const char *sep; 70 71 t = dbp->internal; 72 (void)fprintf(stderr, "%s: pgsz %d", 73 F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize); 74 if (F_ISSET(t, R_RECNO)) 75 (void)fprintf(stderr, " keys %lu", (u_long) t->bt_nrecs); 76 #undef X 77 #define X(flag, name) \ 78 if (F_ISSET(t, flag)) { \ 79 (void)fprintf(stderr, "%s%s", sep, name); \ 80 sep = ", "; \ 81 } 82 if (t->flags != 0) { 83 sep = " flags ("; 84 X(R_FIXLEN, "FIXLEN"); 85 X(B_INMEM, "INMEM"); 86 X(B_NODUPS, "NODUPS"); 87 X(B_RDONLY, "RDONLY"); 88 X(R_RECNO, "RECNO"); 89 X(B_METADIRTY,"METADIRTY"); 90 (void)fprintf(stderr, ")\n"); 91 } 92 #undef X 93 94 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 95 __bt_dpage(h); 96 (void)mpool_put(t->bt_mp, h, 0); 97 } 98 } 99 100 /* 101 * BT_DMPAGE -- Dump the meta page 102 * 103 * Parameters: 104 * h: pointer to the PAGE 105 */ 106 void 107 __bt_dmpage(PAGE *h) 108 { 109 BTMETA *m; 110 const char *sep; 111 112 m = (BTMETA *)(void *)h; 113 (void)fprintf(stderr, "magic %lx\n", (u_long) m->magic); 114 (void)fprintf(stderr, "version %lu\n", (u_long) m->version); 115 (void)fprintf(stderr, "psize %lu\n", (u_long) m->psize); 116 (void)fprintf(stderr, "free %lu\n", (u_long) m->free); 117 (void)fprintf(stderr, "nrecs %lu\n", (u_long) m->nrecs); 118 (void)fprintf(stderr, "flags %lu", (u_long) m->flags); 119 #undef X 120 #define X(flag, name) \ 121 if (m->flags & flag) { \ 122 (void)fprintf(stderr, "%s%s", sep, name); \ 123 sep = ", "; \ 124 } 125 if (m->flags) { 126 sep = " ("; 127 X(B_NODUPS, "NODUPS"); 128 X(R_RECNO, "RECNO"); 129 (void)fprintf(stderr, ")"); 130 } 131 } 132 133 /* 134 * BT_DNPAGE -- Dump the page 135 * 136 * Parameters: 137 * n: page number to dump. 138 */ 139 void 140 __bt_dnpage(DB *dbp, pgno_t pgno) 141 { 142 BTREE *t; 143 PAGE *h; 144 145 t = dbp->internal; 146 if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) { 147 __bt_dpage(h); 148 (void)mpool_put(t->bt_mp, h, 0); 149 } 150 } 151 152 /* 153 * BT_DPAGE -- Dump the page 154 * 155 * Parameters: 156 * h: pointer to the PAGE 157 */ 158 void 159 __bt_dpage(PAGE *h) 160 { 161 BINTERNAL *bi; 162 BLEAF *bl; 163 RINTERNAL *ri; 164 RLEAF *rl; 165 indx_t cur, top; 166 const char *sep; 167 168 (void)fprintf(stderr, " page %d: (", h->pgno); 169 #undef X 170 #define X(flag, name) \ 171 if (h->flags & flag) { \ 172 (void)fprintf(stderr, "%s%s", sep, name); \ 173 sep = ", "; \ 174 } 175 sep = ""; 176 X(P_BINTERNAL, "BINTERNAL") /* types */ 177 X(P_BLEAF, "BLEAF") 178 X(P_RINTERNAL, "RINTERNAL") /* types */ 179 X(P_RLEAF, "RLEAF") 180 X(P_OVERFLOW, "OVERFLOW") 181 X(P_PRESERVE, "PRESERVE"); 182 (void)fprintf(stderr, ")\n"); 183 #undef X 184 185 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg); 186 if (h->flags & P_OVERFLOW) 187 return; 188 189 top = NEXTINDEX(h); 190 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n", 191 h->lower, h->upper, top); 192 for (cur = 0; cur < top; cur++) { 193 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]); 194 switch (h->flags & P_TYPE) { 195 case P_BINTERNAL: 196 bi = GETBINTERNAL(h, cur); 197 (void)fprintf(stderr, 198 "size %03d pgno %03d", bi->ksize, bi->pgno); 199 if (bi->flags & P_BIGKEY) 200 (void)fprintf(stderr, " (indirect)"); 201 else if (bi->ksize) 202 (void)fprintf(stderr, 203 " {%.*s}", (int)bi->ksize, bi->bytes); 204 break; 205 case P_RINTERNAL: 206 ri = GETRINTERNAL(h, cur); 207 (void)fprintf(stderr, "entries %03d pgno %03d", 208 ri->nrecs, ri->pgno); 209 break; 210 case P_BLEAF: 211 bl = GETBLEAF(h, cur); 212 if (bl->flags & P_BIGKEY) 213 (void)fprintf(stderr, 214 "big key page %lu size %u/", 215 (u_long) *(pgno_t *)(void *)bl->bytes, 216 *(u_int32_t *)(void *)(bl->bytes + sizeof(pgno_t))); 217 else if (bl->ksize) 218 (void)fprintf(stderr, "%s/", bl->bytes); 219 if (bl->flags & P_BIGDATA) 220 (void)fprintf(stderr, 221 "big data page %lu size %u", 222 (u_long) *(pgno_t *)(void *)(bl->bytes + bl->ksize), 223 *(u_int32_t *)(void *)(bl->bytes + bl->ksize + 224 sizeof(pgno_t))); 225 else if (bl->dsize) 226 (void)fprintf(stderr, "%.*s", 227 (int)bl->dsize, bl->bytes + bl->ksize); 228 break; 229 case P_RLEAF: 230 rl = GETRLEAF(h, cur); 231 if (rl->flags & P_BIGDATA) 232 (void)fprintf(stderr, 233 "big data page %lu size %u", 234 (u_long) *(pgno_t *)(void *)rl->bytes, 235 *(u_int32_t *)(void *)(rl->bytes + sizeof(pgno_t))); 236 else if (rl->dsize) 237 (void)fprintf(stderr, 238 "%.*s", (int)rl->dsize, rl->bytes); 239 break; 240 } 241 (void)fprintf(stderr, "\n"); 242 } 243 } 244 #endif 245 246 #ifdef STATISTICS 247 /* 248 * BT_STAT -- Gather/print the tree statistics 249 * 250 * Parameters: 251 * dbp: pointer to the DB 252 */ 253 void 254 __bt_stat(DB *dbp) 255 { 256 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit; 257 extern u_long bt_sortsplit, bt_split; 258 BTREE *t; 259 PAGE *h; 260 pgno_t i, pcont, pinternal, pleaf; 261 u_long ifree, lfree, nkeys; 262 int levels; 263 264 t = dbp->internal; 265 pcont = pinternal = pleaf = 0; 266 nkeys = ifree = lfree = 0; 267 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) { 268 switch (h->flags & P_TYPE) { 269 case P_BINTERNAL: 270 case P_RINTERNAL: 271 ++pinternal; 272 ifree += h->upper - h->lower; 273 break; 274 case P_BLEAF: 275 case P_RLEAF: 276 ++pleaf; 277 lfree += h->upper - h->lower; 278 nkeys += NEXTINDEX(h); 279 break; 280 case P_OVERFLOW: 281 ++pcont; 282 break; 283 } 284 (void)mpool_put(t->bt_mp, h, 0); 285 } 286 287 /* Count the levels of the tree. */ 288 for (i = P_ROOT, levels = 0 ;; ++levels) { 289 h = mpool_get(t->bt_mp, i, 0); 290 if (h->flags & (P_BLEAF|P_RLEAF)) { 291 if (levels == 0) 292 levels = 1; 293 (void)mpool_put(t->bt_mp, h, 0); 294 break; 295 } 296 i = F_ISSET(t, R_RECNO) ? 297 GETRINTERNAL(h, 0)->pgno : 298 GETBINTERNAL(h, 0)->pgno; 299 (void)mpool_put(t->bt_mp, h, 0); 300 } 301 302 (void)fprintf(stderr, "%d level%s with %ld keys", 303 levels, levels == 1 ? "" : "s", nkeys); 304 if (F_ISSET(t, R_RECNO)) 305 (void)fprintf(stderr, " (%ld header count)", (long)t->bt_nrecs); 306 (void)fprintf(stderr, 307 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n", 308 (long)pinternal + pleaf + pcont, (long)pleaf, (long)pinternal, 309 (long)pcont); 310 (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n", 311 bt_cache_hit, bt_cache_miss); 312 (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n", 313 bt_split, bt_rootsplit, bt_sortsplit); 314 pleaf *= t->bt_psize - BTDATAOFF; 315 if (pleaf) 316 (void)fprintf(stderr, 317 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n", 318 ((double)(pleaf - lfree) / pleaf) * 100, 319 pleaf - lfree, lfree); 320 pinternal *= t->bt_psize - BTDATAOFF; 321 if (pinternal) 322 (void)fprintf(stderr, 323 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n", 324 ((double)(pinternal - ifree) / pinternal) * 100, 325 pinternal - ifree, ifree); 326 if (bt_pfxsaved) 327 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n", 328 bt_pfxsaved); 329 } 330 #endif 331