2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid[] = "@(#)bt_stat.c 10.14 (Sleepycat) 10/25/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
26 static void __bam_add_rstat __P((DB_BTREE_LSTAT *, DB_BTREE_STAT *));
30 * Gather/print the btree statistics
32 * PUBLIC: int __bam_stat __P((DB *, void *, void *(*)(size_t), int));
35 __bam_stat(argdbp, spp, db_malloc, flags)
38 void *(*db_malloc) __P((size_t));
47 db_pgno_t lastpgno, pgno;
50 DEBUG_LWRITE(argdbp, NULL, "bam_stat", NULL, NULL, flags);
52 /* Check for invalid flags. */
53 if ((ret = __db_statchk(argdbp, flags)) != 0)
59 GETHANDLE(argdbp, NULL, &dbp, ret);
62 /* Allocate and clear the structure. */
63 if ((sp = db_malloc == NULL ?
64 (DB_BTREE_STAT *)__db_malloc(sizeof(*sp)) :
65 (DB_BTREE_STAT *)db_malloc(sizeof(*sp))) == NULL) {
69 memset(sp, 0, sizeof(*sp));
71 /* If the app just wants the record count, make it fast. */
72 if (LF_ISSET(DB_RECORDCOUNT)) {
74 if ((ret = __bam_lget(dbp, 0, pgno, DB_LOCK_READ, &lock)) != 0)
76 if ((ret = __bam_pget(dbp, (PAGE **)&h, &pgno, 0)) != 0)
79 sp->bt_nrecs = RE_NREC(h);
81 (void)memp_fput(dbp->mpf, h, 0);
82 (void)__BT_LPUT(dbp, lock);
86 /* Get the meta-data page. */
88 if ((ret = __bam_lget(dbp, 0, pgno, DB_LOCK_READ, &lock)) != 0)
90 if ((ret = __bam_pget(dbp, (PAGE **)&meta, &pgno, 0)) != 0)
93 /* Translate the metadata flags. */
94 if (F_ISSET(meta, BTM_DUP))
95 sp->bt_flags |= DB_DUP;
96 if (F_ISSET(meta, BTM_FIXEDLEN))
97 sp->bt_flags |= DB_FIXEDLEN;
98 if (F_ISSET(meta, BTM_RECNUM))
99 sp->bt_flags |= DB_RECNUM;
100 if (F_ISSET(meta, BTM_RENUMBER))
101 sp->bt_flags |= DB_RENUMBER;
103 /* Get the remaining metadata fields. */
104 sp->bt_minkey = meta->minkey;
105 sp->bt_maxkey = meta->maxkey;
106 sp->bt_re_len = meta->re_len;
107 sp->bt_re_pad = meta->re_pad;
108 sp->bt_magic = meta->magic;
109 sp->bt_version = meta->version;
111 /* Get the page size from the DB. */
112 sp->bt_pagesize = dbp->pgsize;
114 /* Initialize counters with the meta-data page information. */
115 __bam_add_rstat(&meta->stat, sp);
118 * Add in the local information from this handle.
121 * This is a bit odd, but it gets us closer to the truth.
123 __bam_add_rstat(&t->lstat, sp);
125 /* Walk the free list, counting pages. */
126 for (sp->bt_free = 0, pgno = meta->free; pgno != PGNO_INVALID;) {
129 if ((ret = __bam_pget(dbp, &h, &pgno, 0)) != 0) {
130 (void)memp_fput(dbp->mpf, meta, 0);
131 (void)__BT_TLPUT(dbp, lock);
135 (void)memp_fput(dbp->mpf, h, 0);
138 /* Discard the meta-data page. */
139 (void)memp_fput(dbp->mpf, meta, 0);
140 (void)__BT_TLPUT(dbp, lock);
142 /* Determine the last page of the database. */
143 if ((ret = memp_fget(dbp->mpf, &lastpgno, DB_MPOOL_LAST, &h)) != 0)
145 (void)memp_fput(dbp->mpf, h, 0);
147 /* Get the root page. */
149 if ((ret = __bam_lget(dbp, 0, PGNO_ROOT, DB_LOCK_READ, &lock)) != 0)
151 if ((ret = __bam_pget(dbp, &h, &pgno, 0)) != 0) {
152 (void)__BT_LPUT(dbp, lock);
156 /* Get the levels from the root page. */
157 sp->bt_levels = h->level;
159 /* Walk the page list, counting things. */
167 sp->bt_int_pgfree += HOFFSET(h) - LOFFSET(h);
171 sp->bt_leaf_pgfree += HOFFSET(h) - LOFFSET(h);
172 sp->bt_nrecs += NUM_ENT(h) / P_INDX;
176 sp->bt_leaf_pgfree += HOFFSET(h) - LOFFSET(h);
177 sp->bt_nrecs += NUM_ENT(h);
181 /* XXX MARGO: sp->bt_dup_pgfree; */
185 /* XXX MARGO: sp->bt_over_pgfree; */
188 (void)memp_fput(dbp->mpf, h, 0);
189 (void)__BT_LPUT(dbp, lock);
190 return (__db_pgfmt(dbp, pgno));
193 (void)memp_fput(dbp->mpf, h, 0);
194 (void)__BT_LPUT(dbp, lock);
196 if (++pgno > lastpgno)
198 if (__bam_lget(dbp, 0, pgno, DB_LOCK_READ, &lock))
200 if (memp_fget(dbp->mpf, &pgno, 0, &h) != 0) {
201 (void)__BT_LPUT(dbp, lock);
206 done: *(DB_BTREE_STAT **)spp = sp;
215 * Add the local statistics to the meta-data page statistics.
217 * PUBLIC: void __bam_add_mstat __P((DB_BTREE_LSTAT *, DB_BTREE_LSTAT *));
220 __bam_add_mstat(from, to)
221 DB_BTREE_LSTAT *from;
224 to->bt_freed += from->bt_freed;
225 to->bt_pfxsaved += from->bt_pfxsaved;
226 to->bt_split += from->bt_split;
227 to->bt_rootsplit += from->bt_rootsplit;
228 to->bt_fastsplit += from->bt_fastsplit;
229 to->bt_added += from->bt_added;
230 to->bt_deleted += from->bt_deleted;
231 to->bt_get += from->bt_get;
232 to->bt_cache_hit += from->bt_cache_hit;
233 to->bt_cache_miss += from->bt_cache_miss;
238 * Add the local statistics to the returned statistics.
241 __bam_add_rstat(from, to)
242 DB_BTREE_LSTAT *from;
245 to->bt_freed += from->bt_freed;
246 to->bt_pfxsaved += from->bt_pfxsaved;
247 to->bt_split += from->bt_split;
248 to->bt_rootsplit += from->bt_rootsplit;
249 to->bt_fastsplit += from->bt_fastsplit;
250 to->bt_added += from->bt_added;
251 to->bt_deleted += from->bt_deleted;
252 to->bt_get += from->bt_get;
253 to->bt_cache_hit += from->bt_cache_hit;
254 to->bt_cache_miss += from->bt_cache_miss;