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