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