bt_debug.c 8.5 KB

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