bt_debug.c 9.5 KB

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