xref: /original-bsd/lib/libc/db/btree/bt_debug.c (revision 02e832b2)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.4 (Berkeley) 12/04/92";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/param.h>
16 
17 #include <db.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
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
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, BTF_INMEM) ? "memory" : "disk", t->bt_psize);
43 	if (ISSET(t, BTF_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(BTF_DELCRSR,	"DELCRSR");
54 		X(BTF_FIXEDLEN,	"FIXEDLEN");
55 		X(BTF_INMEM,	"INMEM");
56 		X(BTF_NODUPS,	"NODUPS");
57 		X(BTF_RDONLY,	"RDONLY");
58 		X(BTF_RECNO,	"RECNO");
59 		X(BTF_SEQINIT,	"SEQINIT");
60 		X(BTF_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
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(BTF_NODUPS,	"NODUPS");
100 		X(BTF_RECNO,	"RECNO");
101 		(void)fprintf(stderr, ")");
102 	}
103 	(void)fprintf(stderr, "\nlorder %lu\n", m->m_lorder);
104 }
105 
106 /*
107  * BT_DNPAGE -- Dump the page
108  *
109  * Parameters:
110  *	n:	page number to dump.
111  */
112 void
113 __bt_dnpage(dbp, pgno)
114 	DB *dbp;
115 	pgno_t pgno;
116 {
117 	BTREE *t;
118 	PAGE *h;
119 
120 	t = dbp->internal;
121 	if ((h = mpool_get(t->bt_mp, pgno, 0)) != NULL) {
122 		__bt_dpage(h);
123 		(void)mpool_put(t->bt_mp, h, 0);
124 	}
125 }
126 
127 /*
128  * BT_DPAGE -- Dump the page
129  *
130  * Parameters:
131  *	h:	pointer to the PAGE
132  */
133 void
134 __bt_dpage(h)
135 	PAGE *h;
136 {
137 	BINTERNAL *bi;
138 	BLEAF *bl;
139 	RINTERNAL *ri;
140 	RLEAF *rl;
141 	index_t cur, top;
142 	char *sep;
143 
144 	(void)fprintf(stderr, "    page %d: (", h->pgno);
145 #undef X
146 #define	X(flag, name) \
147 	if (h->flags & flag) { \
148 		(void)fprintf(stderr, "%s%s", sep, name); \
149 		sep = ", "; \
150 	}
151 	sep = "";
152 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
153 	X(P_BLEAF,	"BLEAF")
154 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
155 	X(P_RLEAF,	"RLEAF")
156 	X(P_OVERFLOW,	"OVERFLOW")
157 	X(P_PRESERVE,	"PRESERVE");
158 	(void)fprintf(stderr, ")\n");
159 #undef X
160 
161 	(void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
162 	if (h->flags & P_OVERFLOW)
163 		return;
164 
165 	top = NEXTINDEX(h);
166 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
167 	    h->lower, h->upper, top);
168 	for (cur = 0; cur < top; cur++) {
169 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
170 		switch(h->flags & P_TYPE) {
171 		case P_BINTERNAL:
172 			bi = GETBINTERNAL(h, cur);
173 			(void)fprintf(stderr,
174 			    "size %2d pgno %2d", bi->ksize, bi->pgno);
175 			if (bi->flags & P_BIGKEY)
176 				(void)fprintf(stderr, " (indirect)");
177 			else if (bi->ksize)
178 				(void)fprintf(stderr, " {%s}", bi->bytes);
179 			break;
180 		case P_RINTERNAL:
181 			ri = GETRINTERNAL(h, cur);
182 			(void)fprintf(stderr, "entries %2d pgno %2d",
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,
202 				    "%s", 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, "%s", rl->bytes);
213 			break;
214 		}
215 		(void)fprintf(stderr, "\n");
216 	}
217 }
218 #endif
219 
220 #ifdef STATISTICS
221 /*
222  * BT_STAT -- Gather/print the tree statistics
223  *
224  * Parameters:
225  *	dbp:	pointer to the DB
226  */
227 void
228 __bt_stat(dbp)
229 	DB *dbp;
230 {
231 	extern u_long bt_cache_hit, bt_cache_miss;
232 	extern u_long bt_rootsplit, bt_split, bt_sortsplit;
233 	extern u_long bt_pfxsaved;
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, BTF_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, BTF_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