2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)bt_debug.c 8.5 (Berkeley) 8/17/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
52 * BT_DUMP -- Dump the tree
55 * dbp: pointer to the DB
66 (void)fprintf(stderr, "%s: pgsz %u",
67 F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
68 if (F_ISSET(t, R_RECNO))
69 (void)fprintf(stderr, " keys %u", t->bt_nrecs);
71 #define X(flag, name) \
72 if (F_ISSET(t, flag)) { \
73 (void)fprintf(stderr, "%s%s", sep, name); \
78 X(R_FIXLEN, "FIXLEN");
80 X(B_NODUPS, "NODUPS");
81 X(B_RDONLY, "RDONLY");
83 X(B_METADIRTY,"METADIRTY");
84 (void)fprintf(stderr, ")\n");
89 (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
94 * BT_DMPAGE -- Dump the meta page
97 * h: pointer to the PAGE
106 (void)fprintf(stderr, "magic %x\n", m->magic);
107 (void)fprintf(stderr, "version %u\n", m->version);
108 (void)fprintf(stderr, "psize %u\n", m->psize);
109 (void)fprintf(stderr, "free %u\n", m->free);
110 (void)fprintf(stderr, "nrecs %u\n", m->nrecs);
111 (void)fprintf(stderr, "flags %u", m->flags);
113 #define X(flag, name) \
114 if (m->flags & flag) { \
115 (void)fprintf(stderr, "%s%s", sep, name); \
120 X(B_NODUPS, "NODUPS");
122 (void)fprintf(stderr, ")");
127 * BT_DNPAGE -- Dump the page
130 * n: page number to dump.
133 __bt_dnpage(DB *dbp, pgno_t pgno)
139 if ((h = mpool_get(t->bt_mp, pgno, MPOOL_IGNOREPIN)) != NULL)
144 * BT_DPAGE -- Dump the page
147 * h: pointer to the PAGE
159 (void)fprintf(stderr, " page %u: (", h->pgno);
161 #define X(flag, name) \
162 if (h->flags & flag) { \
163 (void)fprintf(stderr, "%s%s", sep, name); \
167 X(P_BINTERNAL, "BINTERNAL") /* types */
169 X(P_RINTERNAL, "RINTERNAL") /* types */
171 X(P_OVERFLOW, "OVERFLOW")
172 X(P_PRESERVE, "PRESERVE");
173 (void)fprintf(stderr, ")\n");
176 (void)fprintf(stderr, "\tprev %2u next %2u", h->prevpg, h->nextpg);
177 if (h->flags & P_OVERFLOW)
181 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
182 h->lower, h->upper, top);
183 for (cur = 0; cur < top; cur++) {
184 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
185 switch (h->flags & P_TYPE) {
187 bi = GETBINTERNAL(h, cur);
188 (void)fprintf(stderr,
189 "size %03d pgno %03d", bi->ksize, bi->pgno);
190 if (bi->flags & P_BIGKEY)
191 (void)fprintf(stderr, " (indirect)");
193 (void)fprintf(stderr,
194 " {%.*s}", (int)bi->ksize, bi->bytes);
197 ri = GETRINTERNAL(h, cur);
198 (void)fprintf(stderr, "entries %03d pgno %03d",
199 ri->nrecs, ri->pgno);
202 bl = GETBLEAF(h, cur);
203 if (bl->flags & P_BIGKEY)
204 (void)fprintf(stderr,
205 "big key page %u size %u/",
206 *(pgno_t *)bl->bytes,
207 *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
209 (void)fprintf(stderr, "%.*s/",
210 bl->ksize, bl->bytes);
211 if (bl->flags & P_BIGDATA)
212 (void)fprintf(stderr,
213 "big data page %u size %u",
214 *(pgno_t *)(bl->bytes + bl->ksize),
215 *(u_int32_t *)(bl->bytes + bl->ksize +
218 (void)fprintf(stderr, "%.*s",
219 (int)bl->dsize, bl->bytes + bl->ksize);
222 rl = GETRLEAF(h, cur);
223 if (rl->flags & P_BIGDATA)
224 (void)fprintf(stderr,
225 "big data page %u size %u",
226 *(pgno_t *)rl->bytes,
227 *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
229 (void)fprintf(stderr,
230 "%.*s", (int)rl->dsize, rl->bytes);
233 (void)fprintf(stderr, "\n");
240 * BT_STAT -- Gather/print the tree statistics
243 * dbp: pointer to the DB
248 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
249 extern u_long bt_sortsplit, bt_split;
252 pgno_t i, pcont, pinternal, pleaf;
253 u_long ifree, lfree, nkeys;
257 pcont = pinternal = pleaf = 0;
258 nkeys = ifree = lfree = 0;
260 (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
261 switch (h->flags & P_TYPE) {
265 ifree += h->upper - h->lower;
270 lfree += h->upper - h->lower;
271 nkeys += NEXTINDEX(h);
278 /* Count the levels of the tree. */
279 for (i = P_ROOT, levels = 0 ;; ++levels) {
280 h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN);
281 if (h->flags & (P_BLEAF|P_RLEAF)) {
286 i = F_ISSET(t, R_RECNO) ?
287 GETRINTERNAL(h, 0)->pgno :
288 GETBINTERNAL(h, 0)->pgno;
291 (void)fprintf(stderr, "%d level%s with %lu keys",
292 levels, levels == 1 ? "" : "s", nkeys);
293 if (F_ISSET(t, R_RECNO))
294 (void)fprintf(stderr, " (%u header count)", t->bt_nrecs);
295 (void)fprintf(stderr,
296 "\n%u pages (leaf %u, internal %u, overflow %u)\n",
297 pinternal + pleaf + pcont, pleaf, pinternal, pcont);
298 (void)fprintf(stderr, "%lu cache hits, %lu cache misses\n",
299 bt_cache_hit, bt_cache_miss);
300 (void)fprintf(stderr, "%lu splits (%lu root splits, %lu sort splits)\n",
301 bt_split, bt_rootsplit, bt_sortsplit);
302 pleaf *= t->bt_psize - BTDATAOFF;
304 (void)fprintf(stderr,
305 "%.0f%% leaf fill (%lu bytes used, %lu bytes free)\n",
306 ((double)(pleaf - lfree) / pleaf) * 100,
307 pleaf - lfree, lfree);
308 pinternal *= t->bt_psize - BTDATAOFF;
310 (void)fprintf(stderr,
311 "%.0f%% internal fill (%lu bytes used, %lu bytes free\n",
312 ((double)(pinternal - ifree) / pinternal) * 100,
313 pinternal - ifree, ifree);
315 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",