1 /* $OpenBSD: bt_debug.c,v 1.10 2015/01/16 16:48:51 deraadt 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 <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <db.h>
40 #include "btree.h"
41
42 #ifdef DEBUG
43 /*
44 * BT_DUMP -- Dump the tree
45 *
46 * Parameters:
47 * dbp: pointer to the DB
48 */
49 void
__bt_dump(DB * dbp)50 __bt_dump(DB *dbp)
51 {
52 BTREE *t;
53 PAGE *h;
54 pgno_t i;
55 char *sep;
56
57 t = dbp->internal;
58 (void)fprintf(stderr, "%s: pgsz %u",
59 F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
60 if (F_ISSET(t, R_RECNO))
61 (void)fprintf(stderr, " keys %u", t->bt_nrecs);
62 #undef X
63 #define X(flag, name) \
64 if (F_ISSET(t, flag)) { \
65 (void)fprintf(stderr, "%s%s", sep, name); \
66 sep = ", "; \
67 }
68 if (t->flags != 0) {
69 sep = " flags (";
70 X(R_FIXLEN, "FIXLEN");
71 X(B_INMEM, "INMEM");
72 X(B_NODUPS, "NODUPS");
73 X(B_RDONLY, "RDONLY");
74 X(R_RECNO, "RECNO");
75 X(B_METADIRTY,"METADIRTY");
76 (void)fprintf(stderr, ")\n");
77 }
78 #undef X
79
80 for (i = P_ROOT;
81 (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
82 __bt_dpage(h);
83 }
84
85 /*
86 * BT_DMPAGE -- Dump the meta page
87 *
88 * Parameters:
89 * h: pointer to the PAGE
90 */
91 void
__bt_dmpage(PAGE * h)92 __bt_dmpage(PAGE *h)
93 {
94 BTMETA *m;
95 char *sep;
96
97 m = (BTMETA *)h;
98 (void)fprintf(stderr, "magic %x\n", m->magic);
99 (void)fprintf(stderr, "version %u\n", m->version);
100 (void)fprintf(stderr, "psize %u\n", m->psize);
101 (void)fprintf(stderr, "free %u\n", m->free);
102 (void)fprintf(stderr, "nrecs %u\n", m->nrecs);
103 (void)fprintf(stderr, "flags %u", m->flags);
104 #undef X
105 #define X(flag, name) \
106 if (m->flags & flag) { \
107 (void)fprintf(stderr, "%s%s", sep, name); \
108 sep = ", "; \
109 }
110 if (m->flags) {
111 sep = " (";
112 X(B_NODUPS, "NODUPS");
113 X(R_RECNO, "RECNO");
114 (void)fprintf(stderr, ")");
115 }
116 }
117
118 /*
119 * BT_DNPAGE -- Dump the page
120 *
121 * Parameters:
122 * n: page number to dump.
123 */
124 void
__bt_dnpage(DB * dbp,pgno_t pgno)125 __bt_dnpage(DB *dbp, pgno_t pgno)
126 {
127 BTREE *t;
128 PAGE *h;
129
130 t = dbp->internal;
131 if ((h = mpool_get(t->bt_mp, pgno, MPOOL_IGNOREPIN)) != NULL)
132 __bt_dpage(h);
133 }
134
135 /*
136 * BT_DPAGE -- Dump the page
137 *
138 * Parameters:
139 * h: pointer to the PAGE
140 */
141 void
__bt_dpage(PAGE * h)142 __bt_dpage(PAGE *h)
143 {
144 BINTERNAL *bi;
145 BLEAF *bl;
146 RINTERNAL *ri;
147 RLEAF *rl;
148 indx_t cur, top;
149 char *sep;
150
151 (void)fprintf(stderr, " page %u: (", h->pgno);
152 #undef X
153 #define X(flag, name) \
154 if (h->flags & flag) { \
155 (void)fprintf(stderr, "%s%s", sep, name); \
156 sep = ", "; \
157 }
158 sep = "";
159 X(P_BINTERNAL, "BINTERNAL") /* types */
160 X(P_BLEAF, "BLEAF")
161 X(P_RINTERNAL, "RINTERNAL") /* types */
162 X(P_RLEAF, "RLEAF")
163 X(P_OVERFLOW, "OVERFLOW")
164 X(P_PRESERVE, "PRESERVE");
165 (void)fprintf(stderr, ")\n");
166 #undef X
167
168 (void)fprintf(stderr, "\tprev %2u next %2u", h->prevpg, h->nextpg);
169 if (h->flags & P_OVERFLOW)
170 return;
171
172 top = NEXTINDEX(h);
173 (void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
174 h->lower, h->upper, top);
175 for (cur = 0; cur < top; cur++) {
176 (void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
177 switch (h->flags & P_TYPE) {
178 case P_BINTERNAL:
179 bi = GETBINTERNAL(h, cur);
180 (void)fprintf(stderr,
181 "size %03d pgno %03d", bi->ksize, bi->pgno);
182 if (bi->flags & P_BIGKEY)
183 (void)fprintf(stderr, " (indirect)");
184 else if (bi->ksize)
185 (void)fprintf(stderr,
186 " {%.*s}", (int)bi->ksize, bi->bytes);
187 break;
188 case P_RINTERNAL:
189 ri = GETRINTERNAL(h, cur);
190 (void)fprintf(stderr, "entries %03d pgno %03d",
191 ri->nrecs, ri->pgno);
192 break;
193 case P_BLEAF:
194 bl = GETBLEAF(h, cur);
195 if (bl->flags & P_BIGKEY)
196 (void)fprintf(stderr,
197 "big key page %u size %u/",
198 *(pgno_t *)bl->bytes,
199 *(u_int32_t *)(bl->bytes + sizeof(pgno_t)));
200 else if (bl->ksize)
201 (void)fprintf(stderr, "%s/", bl->bytes);
202 if (bl->flags & P_BIGDATA)
203 (void)fprintf(stderr,
204 "big data page %u size %u",
205 *(pgno_t *)(bl->bytes + bl->ksize),
206 *(u_int32_t *)(bl->bytes + bl->ksize +
207 sizeof(pgno_t)));
208 else if (bl->dsize)
209 (void)fprintf(stderr, "%.*s",
210 (int)bl->dsize, bl->bytes + bl->ksize);
211 break;
212 case P_RLEAF:
213 rl = GETRLEAF(h, cur);
214 if (rl->flags & P_BIGDATA)
215 (void)fprintf(stderr,
216 "big data page %u size %u",
217 *(pgno_t *)rl->bytes,
218 *(u_int32_t *)(rl->bytes + sizeof(pgno_t)));
219 else if (rl->dsize)
220 (void)fprintf(stderr,
221 "%.*s", (int)rl->dsize, rl->bytes);
222 break;
223 }
224 (void)fprintf(stderr, "\n");
225 }
226 }
227 #endif
228
229 #ifdef STATISTICS
230 /*
231 * BT_STAT -- Gather/print the tree statistics
232 *
233 * Parameters:
234 * dbp: pointer to the DB
235 */
236 void
__bt_stat(DB * dbp)237 __bt_stat(DB *dbp)
238 {
239 extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
240 extern u_long bt_sortsplit, bt_split;
241 BTREE *t;
242 PAGE *h;
243 pgno_t i, pcont, pinternal, pleaf;
244 u_long ifree, lfree, nkeys;
245 int levels;
246
247 t = dbp->internal;
248 pcont = pinternal = pleaf = 0;
249 nkeys = ifree = lfree = 0;
250 for (i = P_ROOT;
251 (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
252 switch (h->flags & P_TYPE) {
253 case P_BINTERNAL:
254 case P_RINTERNAL:
255 ++pinternal;
256 ifree += h->upper - h->lower;
257 break;
258 case P_BLEAF:
259 case P_RLEAF:
260 ++pleaf;
261 lfree += h->upper - h->lower;
262 nkeys += NEXTINDEX(h);
263 break;
264 case P_OVERFLOW:
265 ++pcont;
266 break;
267 }
268
269 /* Count the levels of the tree. */
270 for (i = P_ROOT, levels = 0 ;; ++levels) {
271 h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN);
272 if (h->flags & (P_BLEAF|P_RLEAF)) {
273 if (levels == 0)
274 levels = 1;
275 break;
276 }
277 i = F_ISSET(t, R_RECNO) ?
278 GETRINTERNAL(h, 0)->pgno :
279 GETBINTERNAL(h, 0)->pgno;
280 }
281
282 (void)fprintf(stderr, "%d level%s with %lu keys",
283 levels, levels == 1 ? "" : "s", nkeys);
284 if (F_ISSET(t, R_RECNO))
285 (void)fprintf(stderr, " (%u header count)", t->bt_nrecs);
286 (void)fprintf(stderr,
287 "\n%u pages (leaf %u, internal %u, overflow %u)\n",
288 pinternal + pleaf + pcont, pleaf, pinternal, pcont);
289 (void)fprintf(stderr, "%lu cache hits, %lu cache misses\n",
290 bt_cache_hit, bt_cache_miss);
291 (void)fprintf(stderr, "%lu splits (%lu root splits, %lu sort splits)\n",
292 bt_split, bt_rootsplit, bt_sortsplit);
293 pleaf *= t->bt_psize - BTDATAOFF;
294 if (pleaf)
295 (void)fprintf(stderr,
296 "%.0f%% leaf fill (%lu bytes used, %lu bytes free)\n",
297 ((double)(pleaf - lfree) / pleaf) * 100,
298 pleaf - lfree, lfree);
299 pinternal *= t->bt_psize - BTDATAOFF;
300 if (pinternal)
301 (void)fprintf(stderr,
302 "%.0f%% internal fill (%lu bytes used, %lu bytes free\n",
303 ((double)(pinternal - ifree) / pinternal) * 100,
304 pinternal - ifree, ifree);
305 if (bt_pfxsaved)
306 (void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
307 bt_pfxsaved);
308 }
309 #endif
310