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 * 3. 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
32 * @(#)bt_debug.c 8.5 (Berkeley) 8/17/94
33 * $DragonFly: src/lib/libc/db/btree/bt_debug.c,v 1.3 2005/09/19 09:20:37 asmodai Exp $
36 #include <sys/param.h>
47 * BT_DUMP -- Dump the tree
50 * dbp: pointer to the DB
62 (void)fprintf(stderr, "%s: pgsz %d",
63 F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
64 if (F_ISSET(t, R_RECNO))
65 (void)fprintf(stderr, " keys %lu", t->bt_nrecs);
67 #define X(flag, name) \
68 if (F_ISSET(t, flag)) { \
69 (void)fprintf(stderr, "%s%s", sep, name); \
74 X(R_FIXLEN, "FIXLEN");
76 X(B_NODUPS, "NODUPS");
77 X(B_RDONLY, "RDONLY");
79 X(B_METADIRTY,"METADIRTY");
80 (void)fprintf(stderr, ")\n");
84 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
86 (void)mpool_put(t->bt_mp, h, 0);
91 * BT_DMPAGE -- Dump the meta page
94 * h: pointer to the PAGE
104 (void)fprintf(stderr, "magic %lx\n", m->magic);
105 (void)fprintf(stderr, "version %lu\n", m->version);
106 (void)fprintf(stderr, "psize %lu\n", m->psize);
107 (void)fprintf(stderr, "free %lu\n", m->free);
108 (void)fprintf(stderr, "nrecs %lu\n", m->nrecs);
109 (void)fprintf(stderr, "flags %lu", m->flags);
111 #define X(flag, name) \
112 if (m->flags & flag) { \
113 (void)fprintf(stderr, "%s%s", sep, name); \
118 X(B_NODUPS, "NODUPS");
120 (void)fprintf(stderr, ")");
125 * BT_DNPAGE -- Dump the page
128 * n: page number to dump.
131 __bt_dnpage(dbp, pgno)
139 if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
141 (void)mpool_put(t->bt_mp, h, 0);
146 * BT_DPAGE -- Dump the page
149 * h: pointer to the PAGE
162 (void)fprintf(stderr, " page %d: (", h->pgno);
164 #define X(flag, name) \
165 if (h->flags & flag) { \
166 (void)fprintf(stderr, "%s%s", sep, name); \
170 X(P_BINTERNAL, "BINTERNAL") /* types */
172 X(P_RINTERNAL, "RINTERNAL") /* types */
174 X(P_OVERFLOW, "OVERFLOW")
175 X(P_PRESERVE, "PRESERVE");
176 (void)fprintf(stderr, ")\n");
179 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
180 if (h->flags & P_OVERFLOW)
184 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
185 h->lower, h->upper, top);
186 for (cur = 0; cur < top; cur++) {
187 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
188 switch (h->flags & P_TYPE) {
190 bi = GETBINTERNAL(h, cur);
191 (void)fprintf(stderr,
192 "size %03d pgno %03d", bi->ksize, bi->pgno);
193 if (bi->flags & P_BIGKEY)
194 (void)fprintf(stderr, " (indirect)");
196 (void)fprintf(stderr,
197 " {%.*s}", (int)bi->ksize, bi->bytes);
200 ri = GETRINTERNAL(h, cur);
201 (void)fprintf(stderr, "entries %03d pgno %03d",
202 ri->nrecs, ri->pgno);
205 bl = GETBLEAF(h, cur);
206 if (bl->flags & P_BIGKEY)
207 (void)fprintf(stderr,
208 "big key page %lu size %u/",
209 *(pgno_t *)bl->bytes,
210 *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
212 (void)fprintf(stderr, "%s/", bl->bytes);
213 if (bl->flags & P_BIGDATA)
214 (void)fprintf(stderr,
215 "big data page %lu size %u",
216 *(pgno_t *)(bl->bytes + bl->ksize),
217 *(u_int32_t *)(bl->bytes + bl->ksize +
220 (void)fprintf(stderr, "%.*s",
221 (int)bl->dsize, bl->bytes + bl->ksize);
224 rl = GETRLEAF(h, cur);
225 if (rl->flags & P_BIGDATA)
226 (void)fprintf(stderr,
227 "big data page %lu size %u",
228 *(pgno_t *)rl->bytes,
229 *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
231 (void)fprintf(stderr,
232 "%.*s", (int)rl->dsize, rl->bytes);
235 (void)fprintf(stderr, "\n");
242 * BT_STAT -- Gather/print the tree statistics
245 * dbp: pointer to the DB
251 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
252 extern u_long bt_sortsplit, bt_split;
255 pgno_t i, pcont, pinternal, pleaf;
256 u_long ifree, lfree, nkeys;
260 pcont = pinternal = pleaf = 0;
261 nkeys = ifree = lfree = 0;
262 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
263 switch (h->flags & P_TYPE) {
267 ifree += h->upper - h->lower;
272 lfree += h->upper - h->lower;
273 nkeys += NEXTINDEX(h);
279 (void)mpool_put(t->bt_mp, h, 0);
282 /* Count the levels of the tree. */
283 for (i = P_ROOT, levels = 0 ;; ++levels) {
284 h = mpool_get(t->bt_mp, i, 0);
285 if (h->flags & (P_BLEAF|P_RLEAF)) {
288 (void)mpool_put(t->bt_mp, h, 0);
291 i = F_ISSET(t, R_RECNO) ?
292 GETRINTERNAL(h, 0)->pgno :
293 GETBINTERNAL(h, 0)->pgno;
294 (void)mpool_put(t->bt_mp, h, 0);
297 (void)fprintf(stderr, "%d level%s with %ld keys",
298 levels, levels == 1 ? "" : "s", nkeys);
299 if (F_ISSET(t, R_RECNO))
300 (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
301 (void)fprintf(stderr,
302 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
303 pinternal + pleaf + pcont, pleaf, pinternal, pcont);
304 (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
305 bt_cache_hit, bt_cache_miss);
306 (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
307 bt_split, bt_rootsplit, bt_sortsplit);
308 pleaf *= t->bt_psize - BTDATAOFF;
310 (void)fprintf(stderr,
311 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
312 ((double)(pleaf - lfree) / pleaf) * 100,
313 pleaf - lfree, lfree);
314 pinternal *= t->bt_psize - BTDATAOFF;
316 (void)fprintf(stderr,
317 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
318 ((double)(pinternal - ifree) / pinternal) * 100,
319 pinternal - ifree, ifree);
321 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",