1 /* $NetBSD: bt_debug.c,v 1.16 2011/07/17 20:47:39 christos Exp $ */
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 HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: bt_debug.c,v 1.16 2011/07/17 20:47:39 christos Exp $");
52 * BT_DUMP -- Dump the tree
55 * dbp: pointer to the DB
66 (void)fprintf(stderr
, "%s: pgsz %d",
67 F_ISSET(t
, B_INMEM
) ? "memory" : "disk", t
->bt_psize
);
68 if (F_ISSET(t
, R_RECNO
))
69 (void)fprintf(stderr
, " keys %lu", (unsigned long) 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");
88 for (i
= P_ROOT
; (h
= mpool_get(t
->bt_mp
, i
, 0)) != NULL
; ++i
) {
90 (void)mpool_put(t
->bt_mp
, h
, 0);
95 * BT_DMPAGE -- Dump the meta page
98 * h: pointer to the PAGE
106 m
= (BTMETA
*)(void *)h
;
107 (void)fprintf(stderr
, "magic %lx\n", (unsigned long) m
->magic
);
108 (void)fprintf(stderr
, "version %lu\n", (unsigned long) m
->version
);
109 (void)fprintf(stderr
, "psize %lu\n", (unsigned long) m
->psize
);
110 (void)fprintf(stderr
, "free %lu\n", (unsigned long) m
->free
);
111 (void)fprintf(stderr
, "nrecs %lu\n", (unsigned long) m
->nrecs
);
112 (void)fprintf(stderr
, "flags %lu", (unsigned long) 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_pgno_t(const void *m
)
131 memcpy(&r
, m
, sizeof(r
));
136 __bt_uint32_t(const void *m
)
139 memcpy(&r
, m
, sizeof(r
));
144 * BT_DNPAGE -- Dump the page
147 * n: page number to dump.
150 __bt_dnpage(DB
*dbp
, pgno_t pgno
)
156 if ((h
= mpool_get(t
->bt_mp
, pgno
, 0)) != NULL
) {
158 (void)mpool_put(t
->bt_mp
, h
, 0);
163 * BT_DPAGE -- Dump the page
166 * h: pointer to the PAGE
178 (void)fprintf(stderr
, " page %d: (", h
->pgno
);
180 #define X(flag, name) \
181 if (h->flags & flag) { \
182 (void)fprintf(stderr, "%s%s", sep, name); \
186 X(P_BINTERNAL
, "BINTERNAL") /* types */
188 X(P_RINTERNAL
, "RINTERNAL") /* types */
190 X(P_OVERFLOW
, "OVERFLOW")
191 X(P_PRESERVE
, "PRESERVE");
192 (void)fprintf(stderr
, ")\n");
195 (void)fprintf(stderr
, "\tprev %2d next %2d", h
->prevpg
, h
->nextpg
);
196 if (h
->flags
& P_OVERFLOW
)
200 (void)fprintf(stderr
, " lower %3d upper %3d nextind %d\n",
201 h
->lower
, h
->upper
, top
);
202 for (cur
= 0; cur
< top
; cur
++) {
203 (void)fprintf(stderr
, "\t[%03d] %4d ", cur
, h
->linp
[cur
]);
204 switch (h
->flags
& P_TYPE
) {
206 bi
= GETBINTERNAL(h
, cur
);
207 (void)fprintf(stderr
,
208 "size %03d pgno %03d", bi
->ksize
, bi
->pgno
);
209 if (bi
->flags
& P_BIGKEY
)
210 (void)fprintf(stderr
, " (indirect)");
212 (void)fprintf(stderr
,
213 " {%.*s}", (int)bi
->ksize
, bi
->bytes
);
216 ri
= GETRINTERNAL(h
, cur
);
217 (void)fprintf(stderr
, "entries %03d pgno %03d",
218 ri
->nrecs
, ri
->pgno
);
221 bl
= GETBLEAF(h
, cur
);
222 if (bl
->flags
& P_BIGKEY
)
223 (void)fprintf(stderr
,
224 "big key page %lu size %u/",
225 (unsigned long) __bt_pgno_t(bl
->bytes
),
226 __bt_uint32_t(bl
->bytes
+ sizeof(pgno_t
)));
228 (void)fprintf(stderr
, "%s/", bl
->bytes
);
229 if (bl
->flags
& P_BIGDATA
)
230 (void)fprintf(stderr
,
231 "big data page %lu size %u",
233 __bt_pgno_t(bl
->bytes
+ bl
->ksize
),
234 __bt_uint32_t(bl
->bytes
+ bl
->ksize
+
237 (void)fprintf(stderr
, "%.*s",
238 (int)bl
->dsize
, bl
->bytes
+ bl
->ksize
);
241 rl
= GETRLEAF(h
, cur
);
242 if (rl
->flags
& P_BIGDATA
)
243 (void)fprintf(stderr
,
244 "big data page %lu size %u",
245 (unsigned long) __bt_pgno_t(rl
->bytes
),
246 __bt_uint32_t(rl
->bytes
+ sizeof(pgno_t
)));
248 (void)fprintf(stderr
,
249 "%.*s", (int)rl
->dsize
, rl
->bytes
);
252 (void)fprintf(stderr
, "\n");
259 * BT_STAT -- Gather/print the tree statistics
262 * dbp: pointer to the DB
267 extern unsigned long bt_cache_hit
, bt_cache_miss
, bt_pfxsaved
, bt_rootsplit
;
268 extern unsigned long bt_sortsplit
, bt_split
;
271 pgno_t i
, pcont
, pinternal
, pleaf
;
272 unsigned long ifree
, lfree
, nkeys
;
276 pcont
= pinternal
= pleaf
= 0;
277 nkeys
= ifree
= lfree
= 0;
278 for (i
= P_ROOT
; (h
= mpool_get(t
->bt_mp
, i
, 0)) != NULL
; ++i
) {
279 switch (h
->flags
& P_TYPE
) {
283 ifree
+= h
->upper
- h
->lower
;
288 lfree
+= h
->upper
- h
->lower
;
289 nkeys
+= NEXTINDEX(h
);
295 (void)mpool_put(t
->bt_mp
, h
, 0);
298 /* Count the levels of the tree. */
299 for (i
= P_ROOT
, levels
= 0 ;; ++levels
) {
300 h
= mpool_get(t
->bt_mp
, i
, 0);
301 if (h
->flags
& (P_BLEAF
|P_RLEAF
)) {
304 (void)mpool_put(t
->bt_mp
, h
, 0);
307 i
= F_ISSET(t
, R_RECNO
) ?
308 GETRINTERNAL(h
, 0)->pgno
:
309 GETBINTERNAL(h
, 0)->pgno
;
310 (void)mpool_put(t
->bt_mp
, h
, 0);
313 (void)fprintf(stderr
, "%d level%s with %ld keys",
314 levels
, levels
== 1 ? "" : "s", nkeys
);
315 if (F_ISSET(t
, R_RECNO
))
316 (void)fprintf(stderr
, " (%ld header count)", (long)t
->bt_nrecs
);
317 (void)fprintf(stderr
,
318 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
319 (long)pinternal
+ pleaf
+ pcont
, (long)pleaf
, (long)pinternal
,
321 (void)fprintf(stderr
, "%ld cache hits, %ld cache misses\n",
322 bt_cache_hit
, bt_cache_miss
);
323 (void)fprintf(stderr
, "%ld splits (%ld root splits, %ld sort splits)\n",
324 bt_split
, bt_rootsplit
, bt_sortsplit
);
325 pleaf
*= t
->bt_psize
- BTDATAOFF
;
327 (void)fprintf(stderr
,
328 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
329 ((double)(pleaf
- lfree
) / pleaf
) * 100,
330 pleaf
- lfree
, lfree
);
331 pinternal
*= t
->bt_psize
- BTDATAOFF
;
333 (void)fprintf(stderr
,
334 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
335 ((double)(pinternal
- ifree
) / pinternal
) * 100,
336 pinternal
- ifree
, ifree
);
338 (void)fprintf(stderr
, "prefix checking removed %lu bytes.\n",