xref: /csrg-svn/lib/libc/db/btree/bt_debug.c (revision 50990)
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.1 (Berkeley) 09/04/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 #define	X(flag, name) \
44 	if (ISSET(t, flag)) { \
45 		(void)fprintf(stderr, "%s%s", sep, name); \
46 		sep = ", "; \
47 	}
48 	if (t->bt_flags) {
49 		sep = " flags (";
50 		X(BTF_DELCRSR,	"DELCRSR");
51 		X(BTF_FIXEDLEN,	"FIXEDLEN");
52 		X(BTF_INMEM,	"INMEM");
53 		X(BTF_NODUPS,	"NODUPS");
54 		X(BTF_RDONLY,	"RDONLY");
55 		X(BTF_RECNO,	"RECNO");
56 		X(BTF_SEQINIT,	"SEQINIT");
57 		X(BTF_METADIRTY,"METADIRTY");
58 		(void)fprintf(stderr, ")\n");
59 	}
60 #undef X
61 
62 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
63 		__bt_dpage(h);
64 		(void)mpool_put(t->bt_mp, h, 0);
65 	}
66 }
67 
68 /*
69  * BT_DPAGE -- Dump the page
70  *
71  * Parameters:
72  *	h:	pointer to the PAGE
73  */
74 void
75 __bt_dpage(h)
76 	PAGE *h;
77 {
78 	BINTERNAL *bi;
79 	BLEAF *bl;
80 	RINTERNAL *ri;
81 	RLEAF *rl;
82 	index_t cur, top;
83 	char *sep;
84 
85 	(void)fprintf(stderr, "    page %d: (", h->pgno);
86 #define	X(flag, name) \
87 	if (h->flags & flag) { \
88 		(void)fprintf(stderr, "%s%s", sep, name); \
89 		sep = ", "; \
90 	}
91 	sep = "";
92 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
93 	X(P_BLEAF,	"BLEAF")
94 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
95 	X(P_RLEAF,	"RLEAF")
96 	X(P_OVERFLOW,	"OVERFLOW")
97 	X(P_PRESERVE,	"PRESERVE");
98 	(void)fprintf(stderr, ")\n");
99 #undef X
100 
101 	(void)fprintf(stderr, "\tprev %2d next %2d", h->prevpg, h->nextpg);
102 	if (h->flags & P_OVERFLOW)
103 		return;
104 
105 	top = NEXTINDEX(h);
106 	(void)fprintf(stderr, " lower %3d upper %3d nextind %d\n",
107 	    h->lower, h->upper, top);
108 	for (cur = 0; cur < top; cur++) {
109 		(void)fprintf(stderr, "\t[%03d] %4d ", cur, h->linp[cur]);
110 		switch(h->flags & P_TYPE) {
111 		case P_BINTERNAL:
112 			bi = GETBINTERNAL(h, cur);
113 			(void)fprintf(stderr,
114 			    "size %2d pgno %2d", bi->ksize, bi->pgno);
115 			if (bi->flags & P_BIGKEY)
116 				(void)fprintf(stderr, " (indirect)");
117 			else if (bi->ksize)
118 				(void)fprintf(stderr, " {%s}", bi->bytes);
119 			break;
120 		case P_RINTERNAL:
121 			ri = GETRINTERNAL(h, cur);
122 			(void)fprintf(stderr, "entries %2d pgno %2d",
123 				ri->nrecs, ri->pgno);
124 			break;
125 		case P_BLEAF:
126 			bl = GETBLEAF(h, cur);
127 			if (bl->flags & P_BIGKEY)
128 				(void)fprintf(stderr,
129 				    "big key page %lu size %u/",
130 				    *(pgno_t *)bl->bytes,
131 				    *(size_t *)(bl->bytes + sizeof(pgno_t)));
132 			else if (bl->ksize)
133 				(void)fprintf(stderr, "%s/", bl->bytes);
134 			if (bl->flags & P_BIGDATA)
135 				(void)fprintf(stderr,
136 				    "big data page %lu size %u",
137 				    *(pgno_t *)(bl->bytes + bl->ksize),
138 				    *(size_t *)(bl->bytes + bl->ksize +
139 				    sizeof(pgno_t)));
140 			else if (bl->dsize)
141 				(void)fprintf(stderr,
142 				    "%s", bl->bytes + bl->ksize);
143 			break;
144 		case P_RLEAF:
145 			rl = GETRLEAF(h, cur);
146 			if (rl->flags & P_BIGDATA)
147 				(void)fprintf(stderr,
148 				    "big data page %lu size %u",
149 				    *(pgno_t *)rl->bytes,
150 				    *(size_t *)(rl->bytes + sizeof(pgno_t)));
151 			else if (rl->dsize)
152 				(void)fprintf(stderr, "%s", rl->bytes);
153 			break;
154 		}
155 		(void)fprintf(stderr, "\n");
156 	}
157 }
158 #endif
159 
160 #ifdef STATISTICS
161 /*
162  * BT_STAT -- Gather/print the tree statistics
163  *
164  * Parameters:
165  *	dbp:	pointer to the DB
166  */
167 void
168 __bt_stat(dbp)
169 	DB *dbp;
170 {
171 	extern u_long bt_cache_hit, bt_cache_miss;
172 	extern u_long bt_rootsplit, bt_split, bt_sortsplit;
173 	extern u_long bt_pfxsaved;
174 	BTREE *t;
175 	PAGE *h;
176 	pgno_t i, pcont, pinternal, pleaf;
177 	u_long ifree, lfree, nkeys;
178 	int levels;
179 
180 	t = dbp->internal;
181 	pcont = pinternal = pleaf = 0;
182 	nkeys = ifree = lfree = 0;
183 	for (i = P_ROOT; (h = mpool_get(t->bt_mp, i, 0)) != NULL; ++i) {
184 		switch(h->flags & P_TYPE) {
185 		case P_BINTERNAL:
186 		case P_RINTERNAL:
187 			++pinternal;
188 			ifree += h->upper - h->lower;
189 			break;
190 		case P_BLEAF:
191 		case P_RLEAF:
192 			++pleaf;
193 			lfree += h->upper - h->lower;
194 			nkeys += NEXTINDEX(h);
195 			break;
196 		case P_OVERFLOW:
197 			++pcont;
198 			break;
199 		}
200 		(void)mpool_put(t->bt_mp, h, 0);
201 	}
202 
203 	/* Count the levels of the tree. */
204 	for (i = P_ROOT, levels = 0 ;; ++levels) {
205 		h = mpool_get(t->bt_mp, i, 0);
206 		if (h->flags & (P_BLEAF|P_RLEAF)) {
207 			if (levels == 0)
208 				levels = 1;
209 			(void)mpool_put(t->bt_mp, h, 0);
210 			break;
211 		}
212 		i = ISSET(t, BTF_RECNO) ?
213 		    GETRINTERNAL(h, 0)->pgno :
214 		    GETBINTERNAL(h, 0)->pgno;
215 		(void)mpool_put(t->bt_mp, h, 0);
216 	}
217 
218 	(void)fprintf(stderr, "%d level%s with %ld keys",
219 	    levels, levels == 1 ? "" : "s", nkeys);
220 	if (ISSET(t, BTF_RECNO))
221 		(void)fprintf(stderr, " (%ld header count)", t->bt_nrecs);
222 	(void)fprintf(stderr,
223 	    "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
224 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
225 	(void)fprintf(stderr, "%ld cache hits, %ld cache misses\n",
226 	    bt_cache_hit, bt_cache_miss);
227 	(void)fprintf(stderr, "%ld splits (%ld root splits, %ld sort splits)\n",
228 	    bt_split, bt_rootsplit, bt_sortsplit);
229 	pleaf *= t->bt_psize - BTDATAOFF;
230 	if (pleaf)
231 		(void)fprintf(stderr,
232 		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
233 		    ((double)(pleaf - lfree) / pleaf) * 100,
234 		    pleaf - lfree, lfree);
235 	pinternal *= t->bt_psize - BTDATAOFF;
236 	if (pinternal)
237 		(void)fprintf(stderr,
238 		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
239 		    ((double)(pinternal - ifree) / pinternal) * 100,
240 		    pinternal - ifree, ifree);
241 	if (bt_pfxsaved)
242 		(void)fprintf(stderr, "prefix checking removed %lu bytes.\n",
243 		    bt_pfxsaved);
244 }
245 #endif
246