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