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