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