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