2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)bt_stat.c 10.17 (Sleepycat) 4/26/98";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
25 static void __bam_add_rstat
__P((DB_BTREE_LSTAT
*, DB_BTREE_STAT
*));
29 * Gather/print the btree statistics
31 * PUBLIC: int __bam_stat __P((DB *, void *, void *(*)(size_t), u_int32_t));
34 __bam_stat(argdbp
, spp
, db_malloc
, flags
)
37 void *(*db_malloc
) __P((size_t));
46 db_pgno_t lastpgno
, pgno
;
49 DEBUG_LWRITE(argdbp
, NULL
, "bam_stat", NULL
, NULL
, flags
);
51 /* Check for invalid flags. */
52 if ((ret
= __db_statchk(argdbp
, flags
)) != 0)
58 GETHANDLE(argdbp
, NULL
, &dbp
, ret
);
61 /* Allocate and clear the structure. */
62 if ((sp
= db_malloc
== NULL
?
63 (DB_BTREE_STAT
*)__db_malloc(sizeof(*sp
)) :
64 (DB_BTREE_STAT
*)db_malloc(sizeof(*sp
))) == NULL
) {
68 memset(sp
, 0, sizeof(*sp
));
70 /* If the app just wants the record count, make it fast. */
71 if (LF_ISSET(DB_RECORDCOUNT
)) {
73 if ((ret
= __bam_lget(dbp
, 0, pgno
, DB_LOCK_READ
, &lock
)) != 0)
75 if ((ret
= __bam_pget(dbp
, (PAGE
**)&h
, &pgno
, 0)) != 0)
78 sp
->bt_nrecs
= RE_NREC(h
);
80 (void)memp_fput(dbp
->mpf
, h
, 0);
81 (void)__BT_LPUT(dbp
, lock
);
85 /* Get the meta-data page. */
87 if ((ret
= __bam_lget(dbp
, 0, pgno
, DB_LOCK_READ
, &lock
)) != 0)
89 if ((ret
= __bam_pget(dbp
, (PAGE
**)&meta
, &pgno
, 0)) != 0)
92 /* Translate the metadata flags. */
93 if (F_ISSET(meta
, BTM_DUP
))
94 sp
->bt_flags
|= DB_DUP
;
95 if (F_ISSET(meta
, BTM_FIXEDLEN
))
96 sp
->bt_flags
|= DB_FIXEDLEN
;
97 if (F_ISSET(meta
, BTM_RECNUM
))
98 sp
->bt_flags
|= DB_RECNUM
;
99 if (F_ISSET(meta
, BTM_RENUMBER
))
100 sp
->bt_flags
|= DB_RENUMBER
;
102 /* Get the remaining metadata fields. */
103 sp
->bt_minkey
= meta
->minkey
;
104 sp
->bt_maxkey
= meta
->maxkey
;
105 sp
->bt_re_len
= meta
->re_len
;
106 sp
->bt_re_pad
= meta
->re_pad
;
107 sp
->bt_magic
= meta
->magic
;
108 sp
->bt_version
= meta
->version
;
110 /* Get the page size from the DB. */
111 sp
->bt_pagesize
= dbp
->pgsize
;
113 /* Initialize counters with the meta-data page information. */
114 __bam_add_rstat(&meta
->stat
, sp
);
117 * Add in the local information from this handle.
120 * This is a bit odd, but it gets us closer to the truth.
122 __bam_add_rstat(&t
->lstat
, sp
);
124 /* Walk the free list, counting pages. */
125 for (sp
->bt_free
= 0, pgno
= meta
->free
; pgno
!= PGNO_INVALID
;) {
128 if ((ret
= __bam_pget(dbp
, &h
, &pgno
, 0)) != 0) {
129 (void)memp_fput(dbp
->mpf
, meta
, 0);
130 (void)__BT_TLPUT(dbp
, lock
);
134 (void)memp_fput(dbp
->mpf
, h
, 0);
137 /* Discard the meta-data page. */
138 (void)memp_fput(dbp
->mpf
, meta
, 0);
139 (void)__BT_TLPUT(dbp
, lock
);
141 /* Determine the last page of the database. */
142 if ((ret
= memp_fget(dbp
->mpf
, &lastpgno
, DB_MPOOL_LAST
, &h
)) != 0)
144 (void)memp_fput(dbp
->mpf
, h
, 0);
146 /* Get the root page. */
148 if ((ret
= __bam_lget(dbp
, 0, PGNO_ROOT
, DB_LOCK_READ
, &lock
)) != 0)
150 if ((ret
= __bam_pget(dbp
, &h
, &pgno
, 0)) != 0) {
151 (void)__BT_LPUT(dbp
, lock
);
155 /* Get the levels from the root page. */
156 sp
->bt_levels
= h
->level
;
158 /* Walk the page list, counting things. */
166 sp
->bt_int_pgfree
+= HOFFSET(h
) - LOFFSET(h
);
170 sp
->bt_leaf_pgfree
+= HOFFSET(h
) - LOFFSET(h
);
171 sp
->bt_nrecs
+= NUM_ENT(h
) / P_INDX
;
175 sp
->bt_leaf_pgfree
+= HOFFSET(h
) - LOFFSET(h
);
176 sp
->bt_nrecs
+= NUM_ENT(h
);
180 /* XXX MARGO: sp->bt_dup_pgfree; */
184 /* XXX MARGO: sp->bt_over_pgfree; */
187 (void)memp_fput(dbp
->mpf
, h
, 0);
188 (void)__BT_LPUT(dbp
, lock
);
189 return (__db_pgfmt(dbp
, pgno
));
192 (void)memp_fput(dbp
->mpf
, h
, 0);
193 (void)__BT_LPUT(dbp
, lock
);
195 if (++pgno
> lastpgno
)
197 if (__bam_lget(dbp
, 0, pgno
, DB_LOCK_READ
, &lock
))
199 if (memp_fget(dbp
->mpf
, &pgno
, 0, &h
) != 0) {
200 (void)__BT_LPUT(dbp
, lock
);
205 done
: *(DB_BTREE_STAT
**)spp
= sp
;
214 * Add the local statistics to the meta-data page statistics.
216 * PUBLIC: void __bam_add_mstat __P((DB_BTREE_LSTAT *, DB_BTREE_LSTAT *));
219 __bam_add_mstat(from
, to
)
220 DB_BTREE_LSTAT
*from
;
223 to
->bt_freed
+= from
->bt_freed
;
224 to
->bt_pfxsaved
+= from
->bt_pfxsaved
;
225 to
->bt_split
+= from
->bt_split
;
226 to
->bt_rootsplit
+= from
->bt_rootsplit
;
227 to
->bt_fastsplit
+= from
->bt_fastsplit
;
228 to
->bt_added
+= from
->bt_added
;
229 to
->bt_deleted
+= from
->bt_deleted
;
230 to
->bt_get
+= from
->bt_get
;
231 to
->bt_cache_hit
+= from
->bt_cache_hit
;
232 to
->bt_cache_miss
+= from
->bt_cache_miss
;
237 * Add the local statistics to the returned statistics.
240 __bam_add_rstat(from
, to
)
241 DB_BTREE_LSTAT
*from
;
244 to
->bt_freed
+= from
->bt_freed
;
245 to
->bt_pfxsaved
+= from
->bt_pfxsaved
;
246 to
->bt_split
+= from
->bt_split
;
247 to
->bt_rootsplit
+= from
->bt_rootsplit
;
248 to
->bt_fastsplit
+= from
->bt_fastsplit
;
249 to
->bt_added
+= from
->bt_added
;
250 to
->bt_deleted
+= from
->bt_deleted
;
251 to
->bt_get
+= from
->bt_get
;
252 to
->bt_cache_hit
+= from
->bt_cache_hit
;
253 to
->bt_cache_miss
+= from
->bt_cache_miss
;