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