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