]>
git.saurik.com Git - apple/libc.git/blob - db.subproj/btree.subproj/bt_debug.c
e64187c9471353cb6416135544b4a0bd9be4275c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
70 * BT_DUMP -- Dump the tree
73 * dbp: pointer to the DB
85 (void)fprintf(stderr
, "%s: pgsz %d",
86 ISSET(t
, B_INMEM
) ? "memory" : "disk", t
->bt_psize
);
87 if (ISSET(t
, R_RECNO
))
88 (void)fprintf(stderr
, " keys %lu", t
->bt_nrecs
);
90 #define X(flag, name) \
91 if (ISSET(t, flag)) { \
92 (void)fprintf(stderr, "%s%s", sep, name); \
97 X(B_DELCRSR
, "DELCRSR");
98 X(R_FIXLEN
, "FIXLEN");
100 X(B_NODUPS
, "NODUPS");
101 X(B_RDONLY
, "RDONLY");
103 X(B_SEQINIT
, "SEQINIT");
104 X(B_METADIRTY
,"METADIRTY");
105 (void)fprintf(stderr
, ")\n");
109 for (i
= P_ROOT
; (h
= mpool_get(t
->bt_mp
, i
, 0)) != NULL
; ++i
) {
111 (void)mpool_put(t
->bt_mp
, h
, 0);
116 * BT_DMPAGE -- Dump the meta page
119 * h: pointer to the PAGE
129 (void)fprintf(stderr
, "magic %lx\n", m
->m_magic
);
130 (void)fprintf(stderr
, "version %lu\n", m
->m_version
);
131 (void)fprintf(stderr
, "psize %lu\n", m
->m_psize
);
132 (void)fprintf(stderr
, "free %lu\n", m
->m_free
);
133 (void)fprintf(stderr
, "nrecs %lu\n", m
->m_nrecs
);
134 (void)fprintf(stderr
, "flags %lu", m
->m_flags
);
136 #define X(flag, name) \
137 if (m->m_flags & flag) { \
138 (void)fprintf(stderr, "%s%s", sep, name); \
143 X(B_NODUPS
, "NODUPS");
145 (void)fprintf(stderr
, ")");
150 * BT_DNPAGE -- Dump the page
153 * n: page number to dump.
156 __bt_dnpage(dbp
, pgno
)
164 if ((h
= mpool_get(t
->bt_mp
, pgno
, 0)) != NULL
) {
166 (void)mpool_put(t
->bt_mp
, h
, 0);
171 * BT_DPAGE -- Dump the page
174 * h: pointer to the PAGE
187 (void)fprintf(stderr
, " page %d: (", h
->pgno
);
189 #define X(flag, name) \
190 if (h->flags & flag) { \
191 (void)fprintf(stderr, "%s%s", sep, name); \
195 X(P_BINTERNAL
, "BINTERNAL") /* types */
197 X(P_RINTERNAL
, "RINTERNAL") /* types */
199 X(P_OVERFLOW
, "OVERFLOW")
200 X(P_PRESERVE
, "PRESERVE");
201 (void)fprintf(stderr
, ")\n");
204 (void)fprintf(stderr
, "\tprev %2d next %2d", h
->prevpg
, h
->nextpg
);
205 if (h
->flags
& P_OVERFLOW
)
209 (void)fprintf(stderr
, " lower %3d upper %3d nextind %d\n",
210 h
->lower
, h
->upper
, top
);
211 for (cur
= 0; cur
< top
; cur
++) {
212 (void)fprintf(stderr
, "\t[%03d] %4d ", cur
, h
->linp
[cur
]);
213 switch(h
->flags
& P_TYPE
) {
215 bi
= GETBINTERNAL(h
, cur
);
216 (void)fprintf(stderr
,
217 "size %03d pgno %03d", bi
->ksize
, bi
->pgno
);
218 if (bi
->flags
& P_BIGKEY
)
219 (void)fprintf(stderr
, " (indirect)");
221 (void)fprintf(stderr
,
222 " {%.*s}", (int)bi
->ksize
, bi
->bytes
);
225 ri
= GETRINTERNAL(h
, cur
);
226 (void)fprintf(stderr
, "entries %03d pgno %03d",
227 ri
->nrecs
, ri
->pgno
);
230 bl
= GETBLEAF(h
, cur
);
231 if (bl
->flags
& P_BIGKEY
)
232 (void)fprintf(stderr
,
233 "big key page %lu size %u/",
234 *(pgno_t
*)bl
->bytes
,
235 *(size_t *)(bl
->bytes
+ sizeof(pgno_t
)));
237 (void)fprintf(stderr
, "%s/", bl
->bytes
);
238 if (bl
->flags
& P_BIGDATA
)
239 (void)fprintf(stderr
,
240 "big data page %lu size %u",
241 *(pgno_t
*)(bl
->bytes
+ bl
->ksize
),
242 *(size_t *)(bl
->bytes
+ bl
->ksize
+
245 (void)fprintf(stderr
, "%.*s",
246 (int)bl
->dsize
, bl
->bytes
+ bl
->ksize
);
249 rl
= GETRLEAF(h
, cur
);
250 if (rl
->flags
& P_BIGDATA
)
251 (void)fprintf(stderr
,
252 "big data page %lu size %u",
253 *(pgno_t
*)rl
->bytes
,
254 *(size_t *)(rl
->bytes
+ sizeof(pgno_t
)));
256 (void)fprintf(stderr
,
257 "%.*s", (int)rl
->dsize
, rl
->bytes
);
260 (void)fprintf(stderr
, "\n");
267 * BT_STAT -- Gather/print the tree statistics
270 * dbp: pointer to the DB
276 extern u_long bt_cache_hit
, bt_cache_miss
, bt_pfxsaved
, bt_rootsplit
;
277 extern u_long bt_sortsplit
, bt_split
;
280 pgno_t i
, pcont
, pinternal
, pleaf
;
281 u_long ifree
, lfree
, nkeys
;
285 pcont
= pinternal
= pleaf
= 0;
286 nkeys
= ifree
= lfree
= 0;
287 for (i
= P_ROOT
; (h
= mpool_get(t
->bt_mp
, i
, 0)) != NULL
; ++i
) {
288 switch(h
->flags
& P_TYPE
) {
292 ifree
+= h
->upper
- h
->lower
;
297 lfree
+= h
->upper
- h
->lower
;
298 nkeys
+= NEXTINDEX(h
);
304 (void)mpool_put(t
->bt_mp
, h
, 0);
307 /* Count the levels of the tree. */
308 for (i
= P_ROOT
, levels
= 0 ;; ++levels
) {
309 h
= mpool_get(t
->bt_mp
, i
, 0);
310 if (h
->flags
& (P_BLEAF
|P_RLEAF
)) {
313 (void)mpool_put(t
->bt_mp
, h
, 0);
316 i
= ISSET(t
, R_RECNO
) ?
317 GETRINTERNAL(h
, 0)->pgno
:
318 GETBINTERNAL(h
, 0)->pgno
;
319 (void)mpool_put(t
->bt_mp
, h
, 0);
322 (void)fprintf(stderr
, "%d level%s with %ld keys",
323 levels
, levels
== 1 ? "" : "s", nkeys
);
324 if (ISSET(t
, R_RECNO
))
325 (void)fprintf(stderr
, " (%ld header count)", t
->bt_nrecs
);
326 (void)fprintf(stderr
,
327 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
328 pinternal
+ pleaf
+ pcont
, pleaf
, pinternal
, pcont
);
329 (void)fprintf(stderr
, "%ld cache hits, %ld cache misses\n",
330 bt_cache_hit
, bt_cache_miss
);
331 (void)fprintf(stderr
, "%ld splits (%ld root splits, %ld sort splits)\n",
332 bt_split
, bt_rootsplit
, bt_sortsplit
);
333 pleaf
*= t
->bt_psize
- BTDATAOFF
;
335 (void)fprintf(stderr
,
336 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
337 ((double)(pleaf
- lfree
) / pleaf
) * 100,
338 pleaf
- lfree
, lfree
);
339 pinternal
*= t
->bt_psize
- BTDATAOFF
;
341 (void)fprintf(stderr
,
342 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
343 ((double)(pinternal
- ifree
) / pinternal
) * 100,
344 pinternal
- ifree
, ifree
);
346 (void)fprintf(stderr
, "prefix checking removed %lu bytes.\n",