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