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