]> git.saurik.com Git - apple/libc.git/blob - db.subproj/btree.subproj/bt_debug.c
e64187c9471353cb6416135544b4a0bd9be4275c
[apple/libc.git] / db.subproj / btree.subproj / bt_debug.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * This code is derived from software contributed to Berkeley by
27 * Mike Olson.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 */
57
58
59 #include <sys/param.h>
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include <db.h>
66 #include "btree.h"
67
68 #ifdef DEBUG
69 /*
70 * BT_DUMP -- Dump the tree
71 *
72 * Parameters:
73 * dbp: pointer to the DB
74 */
75 void
76 __bt_dump(dbp)
77 DB *dbp;
78 {
79 BTREE *t;
80 PAGE *h;
81 pgno_t i;
82 char *sep;
83
84 t = dbp->internal;
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);
89 #undef X
90 #define X(flag, name) \
91 if (ISSET(t, flag)) { \
92 (void)fprintf(stderr, "%s%s", sep, name); \
93 sep = ", "; \
94 }
95 if (t->bt_flags) {
96 sep = " flags (";
97 X(B_DELCRSR, "DELCRSR");
98 X(R_FIXLEN, "FIXLEN");
99 X(B_INMEM, "INMEM");
100 X(B_NODUPS, "NODUPS");
101 X(B_RDONLY, "RDONLY");
102 X(R_RECNO, "RECNO");
103 X(B_SEQINIT, "SEQINIT");
104 X(B_METADIRTY,"METADIRTY");
105 (void)fprintf(stderr, ")\n");
106 }
107 #undef X
108
109 for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
110 __bt_dpage(h);
111 (void)mpool_put(t->bt_mp, h, 0);
112 }
113 }
114
115 /*
116 * BT_DMPAGE -- Dump the meta page
117 *
118 * Parameters:
119 * h: pointer to the PAGE
120 */
121 void
122 __bt_dmpage(h)
123 PAGE *h;
124 {
125 BTMETA *m;
126 char *sep;
127
128 m = (BTMETA *)h;
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);
135 #undef X
136 #define X(flag, name) \
137 if (m->m_flags & flag) { \
138 (void)fprintf(stderr, "%s%s", sep, name); \
139 sep = ", "; \
140 }
141 if (m->m_flags) {
142 sep = " (";
143 X(B_NODUPS, "NODUPS");
144 X(R_RECNO, "RECNO");
145 (void)fprintf(stderr, ")");
146 }
147 }
148
149 /*
150 * BT_DNPAGE -- Dump the page
151 *
152 * Parameters:
153 * n: page number to dump.
154 */
155 void
156 __bt_dnpage(dbp, pgno)
157 DB *dbp;
158 pgno_t pgno;
159 {
160 BTREE *t;
161 PAGE *h;
162
163 t = dbp->internal;
164 if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
165 __bt_dpage(h);
166 (void)mpool_put(t->bt_mp, h, 0);
167 }
168 }
169
170 /*
171 * BT_DPAGE -- Dump the page
172 *
173 * Parameters:
174 * h: pointer to the PAGE
175 */
176 void
177 __bt_dpage(h)
178 PAGE *h;
179 {
180 BINTERNAL *bi;
181 BLEAF *bl;
182 RINTERNAL *ri;
183 RLEAF *rl;
184 indx_t cur, top;
185 char *sep;
186
187 (void)fprintf(stderr, " page %d: (", h->pgno);
188 #undef X
189 #define X(flag, name) \
190 if (h->flags & flag) { \
191 (void)fprintf(stderr, "%s%s", sep, name); \
192 sep = ", "; \
193 }
194 sep = "";
195 X(P_BINTERNAL, "BINTERNAL") /* types */
196 X(P_BLEAF, "BLEAF")
197 X(P_RINTERNAL, "RINTERNAL") /* types */
198 X(P_RLEAF, "RLEAF")
199 X(P_OVERFLOW, "OVERFLOW")
200 X(P_PRESERVE, "PRESERVE");
201 (void)fprintf(stderr, ")\n");
202 #undef X
203
204 (void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
205 if (h->flags & P_OVERFLOW)
206 return;
207
208 top = NEXTINDEX(h);
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) {
214 case P_BINTERNAL:
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)");
220 else if (bi->ksize)
221 (void)fprintf(stderr,
222 " {%.*s}", (int)bi->ksize, bi->bytes);
223 break;
224 case P_RINTERNAL:
225 ri = GETRINTERNAL(h, cur);
226 (void)fprintf(stderr, "entries %03d pgno %03d",
227 ri->nrecs, ri->pgno);
228 break;
229 case P_BLEAF:
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)));
236 else if (bl->ksize)
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 +
243 sizeof(pgno_t)));
244 else if (bl->dsize)
245 (void)fprintf(stderr, "%.*s",
246 (int)bl->dsize, bl->bytes + bl->ksize);
247 break;
248 case P_RLEAF:
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)));
255 else if (rl->dsize)
256 (void)fprintf(stderr,
257 "%.*s", (int)rl->dsize, rl->bytes);
258 break;
259 }
260 (void)fprintf(stderr, "\n");
261 }
262 }
263 #endif
264
265 #ifdef STATISTICS
266 /*
267 * BT_STAT -- Gather/print the tree statistics
268 *
269 * Parameters:
270 * dbp: pointer to the DB
271 */
272 void
273 __bt_stat(dbp)
274 DB *dbp;
275 {
276 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
277 extern u_long bt_sortsplit, bt_split;
278 BTREE *t;
279 PAGE *h;
280 pgno_t i, pcont, pinternal, pleaf;
281 u_long ifree, lfree, nkeys;
282 int levels;
283
284 t = dbp->internal;
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) {
289 case P_BINTERNAL:
290 case P_RINTERNAL:
291 ++pinternal;
292 ifree += h->upper - h->lower;
293 break;
294 case P_BLEAF:
295 case P_RLEAF:
296 ++pleaf;
297 lfree += h->upper - h->lower;
298 nkeys += NEXTINDEX(h);
299 break;
300 case P_OVERFLOW:
301 ++pcont;
302 break;
303 }
304 (void)mpool_put(t->bt_mp, h, 0);
305 }
306
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)) {
311 if (levels == 0)
312 levels = 1;
313 (void)mpool_put(t->bt_mp, h, 0);
314 break;
315 }
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);
320 }
321
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;
334 if (pleaf)
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;
340 if (pinternal)
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);
345 if (bt_pfxsaved)
346 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
347 bt_pfxsaved);
348 }
349 #endif