1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 
7 #pragma ident	"%Z%%M%	%I%	%E% SMI"
8 
9 /*-
10  * Copyright (c) 1990, 1993, 1994, 1995
11  *	The Regents of the University of California.  All rights reserved.
12  *
13  * This code is derived from software contributed to Berkeley by
14  * Mike Olson.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  */
44 
45 #if defined(LIBC_SCCS) && !defined(lint)
46 static char sccsid[] = "@(#)bt_debug.c	8.6 (Berkeley) 1/9/95";
47 #endif /* LIBC_SCCS and not lint */
48 
49 #include <sys/param.h>
50 
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #include "db-int.h"
56 #include "btree.h"
57 
58 #if defined(DEBUG) || defined(STATISTICS)
59 
60 static FILE *tracefp;
61 
62 /*
63  * __bt_dinit --
64  *	initialize debugging.
65  */
66 static void
67 __bt_dinit()
68 {
69 	static int first = 1;
70 	char buf[1024];
71 
72 	if (!first)
73 		return;
74 	first = 0;
75 
76 #ifndef TRACE_TO_STDERR
77 	if ((tracefp = fopen("/tmp/__bt_debug", "wF")) != NULL)
78 		return;
79 #endif
80 	tracefp = stderr;
81 }
82 #endif
83 
84 #ifdef DEBUG
85 /*
86  * __bt_dump --
87  *	dump the tree
88  *
89  * Parameters:
90  *	dbp:	pointer to the DB
91  */
92 int
93 __bt_dump(dbp)
94 	DB *dbp;
95 {
96 	BTREE *t;
97 	PAGE *h;
98 	db_pgno_t i;
99 	char *sep;
100 
101 	__bt_dinit();
102 
103 	t = dbp->internal;
104 	(void)fprintf(tracefp, "%s: pgsz %d",
105 	    F_ISSET(t, B_INMEM) ? "memory" : "disk", t->bt_psize);
106 	if (F_ISSET(t, R_RECNO))
107 		(void)fprintf(tracefp, " keys %lu", t->bt_nrecs);
108 #undef X
109 #define	X(flag, name) \
110 	if (F_ISSET(t, flag)) { \
111 		(void)fprintf(tracefp, "%s%s", sep, name); \
112 		sep = ", "; \
113 	}
114 	if (t->flags != 0) {
115 		sep = " flags (";
116 		X(R_FIXLEN,	"FIXLEN");
117 		X(B_INMEM,	"INMEM");
118 		X(B_NODUPS,	"NODUPS");
119 		X(B_RDONLY,	"RDONLY");
120 		X(R_RECNO,	"RECNO");
121 		X(B_METADIRTY,"METADIRTY");
122 		(void)fprintf(tracefp, ")\n");
123 	}
124 #undef X
125 	for (i = P_ROOT; i < t->bt_mp->npages &&
126 	    (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
127 		__bt_dpage(dbp, h);
128 	(void)fflush(tracefp);
129 	return (0);
130 }
131 
132 /*
133  * BT_DMPAGE -- Dump the meta page
134  *
135  * Parameters:
136  *	h:	pointer to the PAGE
137  */
138 int
139 __bt_dmpage(h)
140 	PAGE *h;
141 {
142 	BTMETA *m;
143 	char *sep;
144 
145 	__bt_dinit();
146 
147 	m = (BTMETA *)h;
148 	(void)fprintf(tracefp, "magic %lx\n", m->magic);
149 	(void)fprintf(tracefp, "version %lu\n", m->version);
150 	(void)fprintf(tracefp, "psize %lu\n", m->psize);
151 	(void)fprintf(tracefp, "free %lu\n", m->free);
152 	(void)fprintf(tracefp, "nrecs %lu\n", m->nrecs);
153 	(void)fprintf(tracefp, "flags %lu", m->flags);
154 #undef X
155 #define	X(flag, name) \
156 	if (m->flags & flag) { \
157 		(void)fprintf(tracefp, "%s%s", sep, name); \
158 		sep = ", "; \
159 	}
160 	if (m->flags) {
161 		sep = " (";
162 		X(B_NODUPS,	"NODUPS");
163 		X(R_RECNO,	"RECNO");
164 		(void)fprintf(tracefp, ")");
165 	}
166 	(void)fprintf(tracefp, "\n");
167 	(void)fflush(tracefp);
168 	return (0);
169 }
170 
171 /*
172  * BT_DNPAGE -- Dump the page
173  *
174  * Parameters:
175  *	n:	page number to dump.
176  */
177 int
178 __bt_dnpage(dbp, pgno)
179 	DB *dbp;
180 	db_pgno_t pgno;
181 {
182 	BTREE *t;
183 	PAGE *h;
184 
185 	__bt_dinit();
186 
187 	t = dbp->internal;
188 	if ((h = mpool_get(t->bt_mp, pgno, MPOOL_IGNOREPIN)) != NULL)
189 		__bt_dpage(dbp, h);
190 	(void)fflush(tracefp);
191 	return (0);
192 }
193 
194 /*
195  * BT_DPAGE -- Dump the page
196  *
197  * Parameters:
198  *	h:	pointer to the PAGE
199  */
200 void
201 __bt_dpage(dbp, h)
202 	DB *dbp;
203 	PAGE *h;
204 {
205 	BINTERNAL *bi;
206 	BLEAF *bl;
207 	RINTERNAL *ri;
208 	RLEAF *rl;
209 	u_long pgsize;
210 	indx_t cur, top, lim;
211 	char *sep;
212 
213 	__bt_dinit();
214 
215 	(void)fprintf(tracefp, "    page %d: (", h->pgno);
216 #undef X
217 #define	X(flag, name)							\
218 	if (h->flags & flag) {						\
219 		(void)fprintf(tracefp, "%s%s", sep, name);		\
220 		sep = ", ";						\
221 	}
222 	sep = "";
223 	X(P_BINTERNAL,	"BINTERNAL")		/* types */
224 	X(P_BLEAF,	"BLEAF")
225 	X(P_RINTERNAL,	"RINTERNAL")		/* types */
226 	X(P_RLEAF,	"RLEAF")
227 	X(P_OVERFLOW,	"OVERFLOW")
228 	X(P_PRESERVE,	"PRESERVE");
229 	(void)fprintf(tracefp, ")\n");
230 #undef X
231 
232 	(void)fprintf(tracefp, "\tprev %2d next %2d", h->prevpg, h->nextpg);
233 	if (h->flags & P_OVERFLOW)
234 		return;
235 
236 	pgsize = ((BTREE *)dbp->internal)->bt_mp->pagesize;
237 	lim = (pgsize - BTDATAOFF) / sizeof(indx_t);
238 	top = NEXTINDEX(h);
239 	lim = top > lim ? lim : top;
240 	(void)fprintf(tracefp, " lower %3d upper %3d nextind %d\n",
241 	    h->lower, h->upper, top);
242 	for (cur = 0; cur < lim; cur++) {
243 		(void)fprintf(tracefp, "\t[%03d] %4d ", cur, h->linp[cur]);
244 		switch (h->flags & P_TYPE) {
245 		case P_BINTERNAL:
246 			bi = GETBINTERNAL(h, cur);
247 			(void)fprintf(tracefp,
248 			    "size %03d pgno %03d", bi->ksize, bi->pgno);
249 			if (bi->flags & P_BIGKEY)
250 				(void)fprintf(tracefp, " (indirect)");
251 			else if (bi->ksize)
252 				(void)fprintf(tracefp,
253 				    " {%.*s}", (int)bi->ksize, bi->bytes);
254 			break;
255 		case P_RINTERNAL:
256 			ri = GETRINTERNAL(h, cur);
257 			(void)fprintf(tracefp, "entries %03d pgno %03d",
258 				ri->nrecs, ri->pgno);
259 			break;
260 		case P_BLEAF:
261 			bl = GETBLEAF(h, cur);
262 			if (bl->flags & P_BIGKEY)
263 				(void)fprintf(tracefp,
264 				    "big key page %lu size %u/",
265 				    *(db_pgno_t *)bl->bytes,
266 				    *(u_int32_t *)(bl->bytes + sizeof(db_pgno_t)));
267 			else if (bl->ksize)
268 				(void)fprintf(tracefp, "%s/", bl->bytes);
269 			if (bl->flags & P_BIGDATA)
270 				(void)fprintf(tracefp,
271 				    "big data page %lu size %u",
272 				    *(db_pgno_t *)(bl->bytes + bl->ksize),
273 				    *(u_int32_t *)(bl->bytes + bl->ksize +
274 				    sizeof(db_pgno_t)));
275 			else if (bl->dsize)
276 				(void)fprintf(tracefp, "%.*s",
277 				    (int)bl->dsize, bl->bytes + bl->ksize);
278 			break;
279 		case P_RLEAF:
280 			rl = GETRLEAF(h, cur);
281 			if (rl->flags & P_BIGDATA)
282 				(void)fprintf(tracefp,
283 				    "big data page %lu size %u",
284 				    *(db_pgno_t *)rl->bytes,
285 				    *(u_int32_t *)(rl->bytes + sizeof(db_pgno_t)));
286 			else if (rl->dsize)
287 				(void)fprintf(tracefp,
288 				    "%.*s", (int)rl->dsize, rl->bytes);
289 			break;
290 		}
291 		(void)fprintf(tracefp, "\n");
292 	}
293 	(void)fflush(tracefp);
294 	return;
295 }
296 #endif
297 
298 #ifdef STATISTICS
299 /*
300  * bt_stat --
301  *	Gather/print the tree statistics
302  *
303  * Parameters:
304  *	dbp:	pointer to the DB
305  */
306 int
307 __bt_stat(dbp)
308 	DB *dbp;
309 {
310 	extern u_long bt_cache_hit, bt_cache_miss, bt_pfxsaved, bt_rootsplit;
311 	extern u_long bt_sortsplit, bt_split;
312 	BTREE *t;
313 	PAGE *h;
314 	db_pgno_t i, pcont, pinternal, pleaf;
315 	u_long ifree, lfree, nkeys;
316 	int levels;
317 
318 	__bt_dinit();
319 
320 	t = dbp->internal;
321 	pcont = pinternal = pleaf = 0;
322 	nkeys = ifree = lfree = 0;
323 	for (i = P_ROOT; i < t->bt_mp->npages &&
324 	    (h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN)) != NULL; ++i)
325 		switch (h->flags & P_TYPE) {
326 		case P_BINTERNAL:
327 		case P_RINTERNAL:
328 			++pinternal;
329 			ifree += h->upper - h->lower;
330 			break;
331 		case P_BLEAF:
332 		case P_RLEAF:
333 			++pleaf;
334 			lfree += h->upper - h->lower;
335 			nkeys += NEXTINDEX(h);
336 			break;
337 		case P_OVERFLOW:
338 			++pcont;
339 			break;
340 		}
341 
342 	/* Count the levels of the tree. */
343 	for (i = P_ROOT, levels = 0 ;; ++levels) {
344 		h = mpool_get(t->bt_mp, i, MPOOL_IGNOREPIN);
345 		if (h->flags & (P_BLEAF|P_RLEAF)) {
346 			if (levels == 0)
347 				levels = 1;
348 			break;
349 		}
350 		i = F_ISSET(t, R_RECNO) ?
351 		    GETRINTERNAL(h, 0)->pgno :
352 		    GETBINTERNAL(h, 0)->pgno;
353 	}
354 
355 	(void)fprintf(tracefp, "%d level%s with %ld keys",
356 	    levels, levels == 1 ? "" : "s", nkeys);
357 	if (F_ISSET(t, R_RECNO))
358 		(void)fprintf(tracefp, " (%ld header count)", t->bt_nrecs);
359 	(void)fprintf(tracefp,
360 	    "\n%lu pages (leaf %ld, internal %ld, overflow %ld)\n",
361 	    pinternal + pleaf + pcont, pleaf, pinternal, pcont);
362 	(void)fprintf(tracefp, "%ld cache hits, %ld cache misses\n",
363 	    bt_cache_hit, bt_cache_miss);
364 	(void)fprintf(tracefp,
365 	    "%ld splits (%ld root splits, %ld sort splits)\n",
366 	    bt_split, bt_rootsplit, bt_sortsplit);
367 	pleaf *= t->bt_psize - BTDATAOFF;
368 	if (pleaf)
369 		(void)fprintf(tracefp,
370 		    "%.0f%% leaf fill (%ld bytes used, %ld bytes free)\n",
371 		    ((double)(pleaf - lfree) / pleaf) * 100,
372 		    pleaf - lfree, lfree);
373 	pinternal *= t->bt_psize - BTDATAOFF;
374 	if (pinternal)
375 		(void)fprintf(tracefp,
376 		    "%.0f%% internal fill (%ld bytes used, %ld bytes free\n",
377 		    ((double)(pinternal - ifree) / pinternal) * 100,
378 		    pinternal - ifree, ifree);
379 	if (bt_pfxsaved)
380 		(void)fprintf(tracefp, "prefix checking removed %lu bytes.\n",
381 		    bt_pfxsaved);
382 	(void)fflush(tracefp);
383 	return (0);
384 }
385 #endif
386