2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)bt_debug.c 8.5 (Berkeley) 8/17/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
50 * BT_DUMP -- Dump the tree
53 * dbp: pointer to the DB
65 (void)fprintf(stderr, "%s: pgsz %d",
66 F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
67 if (F_ISSET(t, R_RECNO))
68 (void)fprintf(stderr, " keys %u", t->bt_nrecs);
70 #define X(flag, name) \
71 if (F_ISSET(t, flag)) { \
72 (void)fprintf(stderr, "%s%s", sep, name); \
77 X(R_FIXLEN, "FIXLEN");
79 X(B_NODUPS, "NODUPS");
80 X(B_RDONLY, "RDONLY");
82 X(B_METADIRTY,"METADIRTY");
83 (void)fprintf(stderr, ")\n");
87 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
89 (void)mpool_put(t->bt_mp, h, 0);
94 * BT_DMPAGE -- Dump the meta page
97 * h: pointer to the PAGE
107 (void)fprintf(stderr, "magic %x\n", m->magic);
108 (void)fprintf(stderr, "version %u\n", m->version);
109 (void)fprintf(stderr, "psize %u\n", m->psize);
110 (void)fprintf(stderr, "free %u\n", m->free);
111 (void)fprintf(stderr, "nrecs %u\n", m->nrecs);
112 (void)fprintf(stderr, "flags %u", m->flags);
114 #define X(flag, name) \
115 if (m->flags & flag) { \
116 (void)fprintf(stderr, "%s%s", sep, name); \
121 X(B_NODUPS, "NODUPS");
123 (void)fprintf(stderr, ")");
128 * BT_DNPAGE -- Dump the page
131 * n: page number to dump.
134 __bt_dnpage(dbp, pgno)
142 if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
144 (void)mpool_put(t->bt_mp, h, 0);
149 * BT_DPAGE -- Dump the page
152 * h: pointer to the PAGE
165 (void)fprintf(stderr, " page %d: (", h->pgno);
167 #define X(flag, name) \
168 if (h->flags & flag) { \
169 (void)fprintf(stderr, "%s%s", sep, name); \
173 X(P_BINTERNAL, "BINTERNAL") /* types */
175 X(P_RINTERNAL, "RINTERNAL") /* types */
177 X(P_OVERFLOW, "OVERFLOW")
178 X(P_PRESERVE, "PRESERVE");
179 (void)fprintf(stderr, ")\n");
182 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
183 if (h->flags & P_OVERFLOW)
187 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
188 h->lower, h->upper, top);
189 for (cur = 0; cur < top; cur++) {
190 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
191 switch (h->flags & P_TYPE) {
193 bi = GETBINTERNAL(h, cur);
194 (void)fprintf(stderr,
195 "size %03d pgno %03d", bi->ksize, bi->pgno);
196 if (bi->flags & P_BIGKEY)
197 (void)fprintf(stderr, " (indirect)");
199 (void)fprintf(stderr,
200 " {%.*s}", (int)bi->ksize, bi->bytes);
203 ri = GETRINTERNAL(h, cur);
204 (void)fprintf(stderr, "entries %03d pgno %03d",
205 ri->nrecs, ri->pgno);
208 bl = GETBLEAF(h, cur);
209 if (bl->flags & P_BIGKEY)
210 (void)fprintf(stderr,
211 "big key page %u size %u/",
212 *(pgno_t *)bl->bytes,
213 *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
215 (void)fprintf(stderr, "%.*s/",
216 bl->ksize, bl->bytes);
217 if (bl->flags & P_BIGDATA)
218 (void)fprintf(stderr,
219 "big data page %u size %u",
220 *(pgno_t *)(bl->bytes + bl->ksize),
221 *(u_int32_t *)(bl->bytes + bl->ksize +
224 (void)fprintf(stderr, "%.*s",
225 (int)bl->dsize, bl->bytes + bl->ksize);
228 rl = GETRLEAF(h, cur);
229 if (rl->flags & P_BIGDATA)
230 (void)fprintf(stderr,
231 "big data page %u size %u",
232 *(pgno_t *)rl->bytes,
233 *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
235 (void)fprintf(stderr,
236 "%.*s", (int)rl->dsize, rl->bytes);
239 (void)fprintf(stderr, "\n");
246 * BT_STAT -- Gather/print the tree statistics
249 * dbp: pointer to the DB
255 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
256 extern u_long bt_sortsplit, bt_split;
259 pgno_t i, pcont, pinternal, pleaf;
260 u_long ifree, lfree, nkeys;
264 pcont = pinternal = pleaf = 0;
265 nkeys = ifree = lfree = 0;
266 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
267 switch (h->flags & P_TYPE) {
271 ifree += h->upper - h->lower;
276 lfree += h->upper - h->lower;
277 nkeys += NEXTINDEX(h);
283 (void)mpool_put(t->bt_mp, h, 0);
286 /* Count the levels of the tree. */
287 for (i = P_ROOT, levels = 0 ;; ++levels) {
288 h = mpool_get(t->bt_mp, i, 0);
289 if (h->flags & (P_BLEAF|P_RLEAF)) {
292 (void)mpool_put(t->bt_mp, h, 0);
295 i = F_ISSET(t, R_RECNO) ?
296 GETRINTERNAL(h, 0)->pgno :
297 GETBINTERNAL(h, 0)->pgno;
298 (void)mpool_put(t->bt_mp, h, 0);
301 (void)fprintf(stderr, "%d level%s with %ld keys",
302 levels, levels == 1 ? "" : "s", nkeys);
303 if (F_ISSET(t, R_RECNO))
304 (void)fprintf(stderr, " (%d header count)", t->bt_nrecs);
305 (void)fprintf(stderr,
306 "\n%u pages (leaf %d, internal %d, overflow %d)\n",
307 pinternal + pleaf + pcont, pleaf, pinternal, pcont);
308 (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
309 bt_cache_hit, bt_cache_miss);
310 (void)fprintf(stderr, "%lu splits (%lu root splits, %lu sort splits)\n",
311 bt_split, bt_rootsplit, bt_sortsplit);
312 pleaf *= t->bt_psize - BTDATAOFF;
314 (void)fprintf(stderr,
315 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
316 ((double)(pleaf - lfree) / pleaf) * 100,
317 pleaf - lfree, lfree);
318 pinternal *= t->bt_psize - BTDATAOFF;
320 (void)fprintf(stderr,
321 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
322 ((double)(pinternal - ifree) / pinternal) * 100,
323 pinternal - ifree, ifree);
325 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",