]> git.saurik.com Git - apple/libc.git/blob - db/btree/bt_debug.c
ffef39e57d8fddec02668c642c9235bc3570e8b8
[apple/libc.git] / db / btree / bt_debug.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*-
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Mike Olson.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid[] = "@(#)bt_debug.c 8.5 (Berkeley) 8/17/94";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
65
66 #include <sys/param.h>
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 #include <db.h>
73 #include "btree.h"
74
75 #ifdef DEBUG
76 /*
77 * BT_DUMP -- Dump the tree
78 *
79 * Parameters:
80 * dbp: pointer to the DB
81 */
82 void
83 __bt_dump(dbp)
84 DB *dbp;
85 {
86 BTREE *t;
87 PAGE *h;
88 pgno_t i;
89 char *sep;
90
91 t = dbp->internal;
92 (void)fprintf(stderr, "%s: pgsz %d",
93 F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
94 if (F_ISSET(t, R_RECNO))
95 (void)fprintf(stderr, " keys %lu", t->bt_nrecs);
96 #undef X
97 #define X(flag, name) \
98 if (F_ISSET(t, flag)) { \
99 (void)fprintf(stderr, "%s%s", sep, name); \
100 sep = ", "; \
101 }
102 if (t->flags != 0) {
103 sep = " flags (";
104 X(R_FIXLEN, "FIXLEN");
105 X(B_INMEM, "INMEM");
106 X(B_NODUPS, "NODUPS");
107 X(B_RDONLY, "RDONLY");
108 X(R_RECNO, "RECNO");
109 X(B_METADIRTY,"METADIRTY");
110 (void)fprintf(stderr, ")\n");
111 }
112 #undef X
113
114 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
115 __bt_dpage(h);
116 (void)mpool_put(t->bt_mp, h, 0);
117 }
118 }
119
120 /*
121 * BT_DMPAGE -- Dump the meta page
122 *
123 * Parameters:
124 * h: pointer to the PAGE
125 */
126 void
127 __bt_dmpage(h)
128 PAGE *h;
129 {
130 BTMETA *m;
131 char *sep;
132
133 m = (BTMETA *)h;
134 (void)fprintf(stderr, "magic %lx\n", m->magic);
135 (void)fprintf(stderr, "version %lu\n", m->version);
136 (void)fprintf(stderr, "psize %lu\n", m->psize);
137 (void)fprintf(stderr, "free %lu\n", m->free);
138 (void)fprintf(stderr, "nrecs %lu\n", m->nrecs);
139 (void)fprintf(stderr, "flags %lu", m->flags);
140 #undef X
141 #define X(flag, name) \
142 if (m->flags & flag) { \
143 (void)fprintf(stderr, "%s%s", sep, name); \
144 sep = ", "; \
145 }
146 if (m->flags) {
147 sep = " (";
148 X(B_NODUPS, "NODUPS");
149 X(R_RECNO, "RECNO");
150 (void)fprintf(stderr, ")");
151 }
152 }
153
154 /*
155 * BT_DNPAGE -- Dump the page
156 *
157 * Parameters:
158 * n: page number to dump.
159 */
160 void
161 __bt_dnpage(dbp, pgno)
162 DB *dbp;
163 pgno_t pgno;
164 {
165 BTREE *t;
166 PAGE *h;
167
168 t = dbp->internal;
169 if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
170 __bt_dpage(h);
171 (void)mpool_put(t->bt_mp, h, 0);
172 }
173 }
174
175 /*
176 * BT_DPAGE -- Dump the page
177 *
178 * Parameters:
179 * h: pointer to the PAGE
180 */
181 void
182 __bt_dpage(h)
183 PAGE *h;
184 {
185 BINTERNAL *bi;
186 BLEAF *bl;
187 RINTERNAL *ri;
188 RLEAF *rl;
189 indx_t cur, top;
190 char *sep;
191
192 (void)fprintf(stderr, " page %d: (", h->pgno);
193 #undef X
194 #define X(flag, name) \
195 if (h->flags & flag) { \
196 (void)fprintf(stderr, "%s%s", sep, name); \
197 sep = ", "; \
198 }
199 sep = "";
200 X(P_BINTERNAL, "BINTERNAL") /* types */
201 X(P_BLEAF, "BLEAF")
202 X(P_RINTERNAL, "RINTERNAL") /* types */
203 X(P_RLEAF, "RLEAF")
204 X(P_OVERFLOW, "OVERFLOW")
205 X(P_PRESERVE, "PRESERVE");
206 (void)fprintf(stderr, ")\n");
207 #undef X
208
209 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
210 if (h->flags & P_OVERFLOW)
211 return;
212
213 top = NEXTINDEX(h);
214 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
215 h->lower, h->upper, top);
216 for (cur = 0; cur < top; cur++) {
217 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
218 switch (h->flags & P_TYPE) {
219 case P_BINTERNAL:
220 bi = GETBINTERNAL(h, cur);
221 (void)fprintf(stderr,
222 "size %03d pgno %03d", bi->ksize, bi->pgno);
223 if (bi->flags & P_BIGKEY)
224 (void)fprintf(stderr, " (indirect)");
225 else if (bi->ksize)
226 (void)fprintf(stderr,
227 " {%.*s}", (int)bi->ksize, bi->bytes);
228 break;
229 case P_RINTERNAL:
230 ri = GETRINTERNAL(h, cur);
231 (void)fprintf(stderr, "entries %03d pgno %03d",
232 ri->nrecs, ri->pgno);
233 break;
234 case P_BLEAF:
235 bl = GETBLEAF(h, cur);
236 if (bl->flags & P_BIGKEY)
237 (void)fprintf(stderr,
238 "big key page %lu size %u/",
239 *(pgno_t *)bl->bytes,
240 *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
241 else if (bl->ksize)
242 (void)fprintf(stderr, "%s/", bl->bytes);
243 if (bl->flags & P_BIGDATA)
244 (void)fprintf(stderr,
245 "big data page %lu size %u",
246 *(pgno_t *)(bl->bytes + bl->ksize),
247 *(u_int32_t *)(bl->bytes + bl->ksize +
248 sizeof(pgno_t)));
249 else if (bl->dsize)
250 (void)fprintf(stderr, "%.*s",
251 (int)bl->dsize, bl->bytes + bl->ksize);
252 break;
253 case P_RLEAF:
254 rl = GETRLEAF(h, cur);
255 if (rl->flags & P_BIGDATA)
256 (void)fprintf(stderr,
257 "big data page %lu size %u",
258 *(pgno_t *)rl->bytes,
259 *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
260 else if (rl->dsize)
261 (void)fprintf(stderr,
262 "%.*s", (int)rl->dsize, rl->bytes);
263 break;
264 }
265 (void)fprintf(stderr, "\n");
266 }
267 }
268 #endif
269
270 #ifdef STATISTICS
271 /*
272 * BT_STAT -- Gather/print the tree statistics
273 *
274 * Parameters:
275 * dbp: pointer to the DB
276 */
277 void
278 __bt_stat(dbp)
279 DB *dbp;
280 {
281 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
282 extern u_long bt_sortsplit, bt_split;
283 BTREE *t;
284 PAGE *h;
285 pgno_t i, pcont, pinternal, pleaf;
286 u_long ifree, lfree, nkeys;
287 int levels;
288
289 t = dbp->internal;
290 pcont = pinternal = pleaf = 0;
291 nkeys = ifree = lfree = 0;
292 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
293 switch (h->flags & P_TYPE) {
294 case P_BINTERNAL:
295 case P_RINTERNAL:
296 ++pinternal;
297 ifree += h->upper - h->lower;
298 break;
299 case P_BLEAF:
300 case P_RLEAF:
301 ++pleaf;
302 lfree += h->upper - h->lower;
303 nkeys += NEXTINDEX(h);
304 break;
305 case P_OVERFLOW:
306 ++pcont;
307 break;
308 }
309 (void)mpool_put(t->bt_mp, h, 0);
310 }
311
312 /* Count the levels of the tree. */
313 for (i = P_ROOT, levels = 0 ;; ++levels) {
314 h = mpool_get(t->bt_mp, i, 0);
315 if (h->flags & (P_BLEAF|P_RLEAF)) {
316 if (levels == 0)
317 levels = 1;
318 (void)mpool_put(t->bt_mp, h, 0);
319 break;
320 }
321 i = F_ISSET(t, R_RECNO) ?
322 GETRINTERNAL(h, 0)->pgno :
323 GETBINTERNAL(h, 0)->pgno;
324 (void)mpool_put(t->bt_mp, h, 0);
325 }
326
327 (void)fprintf(stderr, "%d level%s with %ld keys",
328 levels, levels == 1 ? "" : "s", nkeys);
329 if (F_ISSET(t, R_RECNO))
330 (void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
331 (void)fprintf(stderr,
332 "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
333 pinternal + pleaf + pcont, pleaf, pinternal, pcont);
334 (void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
335 bt_cache_hit, bt_cache_miss);
336 (void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
337 bt_split, bt_rootsplit, bt_sortsplit);
338 pleaf *= t->bt_psize - BTDATAOFF;
339 if (pleaf)
340 (void)fprintf(stderr,
341 "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
342 ((double)(pleaf - lfree) / pleaf) * 100,
343 pleaf - lfree, lfree);
344 pinternal *= t->bt_psize - BTDATAOFF;
345 if (pinternal)
346 (void)fprintf(stderr,
347 "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
348 ((double)(pinternal - ifree) / pinternal) * 100,
349 pinternal - ifree, ifree);
350 if (bt_pfxsaved)
351 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
352 bt_pfxsaved);
353 }
354 #endif