2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
10
static const char revid[] = "$Id$";
13
#ifndef NO_SYSTEM_INCLUDES
14
#include <sys/types.h>
22
#include "dbinc/db_page.h"
23
#include "dbinc/db_shash.h"
24
#include "dbinc/db_am.h"
27
static void __memp_dumpcache __P((DB_ENV *,
28
DB_MPOOL *, REGINFO *, size_t *, FILE *, u_int32_t));
29
static void __memp_pbh __P((DB_MPOOL *, BH *, size_t *, FILE *));
30
static void __memp_stat_wait __P((REGINFO *, MPOOL *, DB_MPOOL_STAT *, int));
34
* Display MPOOL statistics.
36
* PUBLIC: int __memp_stat
37
* PUBLIC: __P((DB_ENV *, DB_MPOOL_STAT **, DB_MPOOL_FSTAT ***, u_int32_t));
40
__memp_stat(dbenv, gspp, fspp, flags)
43
DB_MPOOL_FSTAT ***fspp;
47
DB_MPOOL_FSTAT **tfsp, *tstruct;
51
size_t len, nlen, pagesize;
57
ENV_REQUIRES_CONFIG(dbenv,
58
dbenv->mp_handle, "memp_stat", DB_INIT_MPOOL);
60
if ((ret = __db_fchk(dbenv,
61
"DB_ENV->memp_stat", flags, DB_STAT_CLEAR)) != 0)
64
dbmp = dbenv->mp_handle;
65
mp = dbmp->reginfo[0].primary;
67
/* Global statistics. */
71
if ((ret = __os_umalloc(dbenv, sizeof(**gspp), gspp)) != 0)
73
memset(*gspp, 0, sizeof(**gspp));
77
* Initialization and information that is not maintained on
80
c_mp = dbmp->reginfo[0].primary;
81
sp->st_gbytes = c_mp->stat.st_gbytes;
82
sp->st_bytes = c_mp->stat.st_bytes;
83
sp->st_ncache = dbmp->nreg;
84
sp->st_regsize = dbmp->reginfo[0].rp->size;
86
/* Walk the cache list and accumulate the global information. */
87
for (i = 0; i < mp->nreg; ++i) {
88
c_mp = dbmp->reginfo[i].primary;
90
sp->st_map += c_mp->stat.st_map;
91
sp->st_cache_hit += c_mp->stat.st_cache_hit;
92
sp->st_cache_miss += c_mp->stat.st_cache_miss;
93
sp->st_page_create += c_mp->stat.st_page_create;
94
sp->st_page_in += c_mp->stat.st_page_in;
95
sp->st_page_out += c_mp->stat.st_page_out;
96
sp->st_ro_evict += c_mp->stat.st_ro_evict;
97
sp->st_rw_evict += c_mp->stat.st_rw_evict;
98
sp->st_page_trickle += c_mp->stat.st_page_trickle;
99
sp->st_pages += c_mp->stat.st_pages;
101
* st_page_dirty calculated by __memp_stat_hash
102
* st_page_clean calculated here
105
&dbmp->reginfo[i], c_mp, &sp->st_page_dirty);
106
sp->st_page_clean = sp->st_pages - sp->st_page_dirty;
107
sp->st_hash_buckets += c_mp->stat.st_hash_buckets;
108
sp->st_hash_searches += c_mp->stat.st_hash_searches;
109
sp->st_hash_longest += c_mp->stat.st_hash_longest;
110
sp->st_hash_examined += c_mp->stat.st_hash_examined;
112
* st_hash_nowait calculated by __memp_stat_wait
115
__memp_stat_wait(&dbmp->reginfo[i], c_mp, sp, flags);
116
sp->st_region_nowait +=
117
dbmp->reginfo[i].rp->mutex.mutex_set_nowait;
118
sp->st_region_wait +=
119
dbmp->reginfo[i].rp->mutex.mutex_set_wait;
120
sp->st_alloc += c_mp->stat.st_alloc;
121
sp->st_alloc_buckets += c_mp->stat.st_alloc_buckets;
122
if (sp->st_alloc_max_buckets <
123
c_mp->stat.st_alloc_max_buckets)
124
sp->st_alloc_max_buckets =
125
c_mp->stat.st_alloc_max_buckets;
126
sp->st_alloc_pages += c_mp->stat.st_alloc_pages;
127
if (sp->st_alloc_max_pages <
128
c_mp->stat.st_alloc_max_pages)
129
sp->st_alloc_max_pages =
130
c_mp->stat.st_alloc_max_pages;
132
if (LF_ISSET(DB_STAT_CLEAR)) {
133
dbmp->reginfo[i].rp->mutex.mutex_set_wait = 0;
134
dbmp->reginfo[i].rp->mutex.mutex_set_nowait = 0;
135
pages = c_mp->stat.st_pages;
136
memset(&c_mp->stat, 0, sizeof(c_mp->stat));
137
c_mp->stat.st_hash_buckets = c_mp->htab_buckets;
138
c_mp->stat.st_pages = pages;
143
* We have duplicate statistics fields in per-file structures
144
* and the cache. The counters are only incremented in the
145
* per-file structures, except if a file is flushed from the
146
* mpool, at which time we copy its information into the cache
147
* statistics. We added the cache information above, now we
148
* add the per-file information.
150
R_LOCK(dbenv, dbmp->reginfo);
151
for (mfp = SH_TAILQ_FIRST(&mp->mpfq, __mpoolfile);
152
mfp != NULL; mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile)) {
153
sp->st_map += mfp->stat.st_map;
154
sp->st_cache_hit += mfp->stat.st_cache_hit;
155
sp->st_cache_miss += mfp->stat.st_cache_miss;
156
sp->st_page_create += mfp->stat.st_page_create;
157
sp->st_page_in += mfp->stat.st_page_in;
158
sp->st_page_out += mfp->stat.st_page_out;
159
if (fspp == NULL && LF_ISSET(DB_STAT_CLEAR)) {
160
pagesize = mfp->stat.st_pagesize;
161
memset(&mfp->stat, 0, sizeof(mfp->stat));
162
mfp->stat.st_pagesize = pagesize;
165
R_UNLOCK(dbenv, dbmp->reginfo);
168
/* Per-file statistics. */
172
/* Count the MPOOLFILE structures. */
173
R_LOCK(dbenv, dbmp->reginfo);
175
mfp = SH_TAILQ_FIRST(&mp->mpfq, __mpoolfile);
177
++i, mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile))
178
len += sizeof(DB_MPOOL_FSTAT *) +
179
sizeof(DB_MPOOL_FSTAT) +
180
strlen(__memp_fns(dbmp, mfp)) + 1;
181
len += sizeof(DB_MPOOL_FSTAT *); /* Trailing NULL */
182
R_UNLOCK(dbenv, dbmp->reginfo);
188
if ((ret = __os_umalloc(dbenv, len, fspp)) != 0)
192
* Build each individual entry. We assume that an array of
193
* pointers are aligned correctly to be followed by an array
194
* of structures, which should be safe (in this particular
195
* case, the first element of the structure is a pointer, so
196
* we're doubly safe). The array is followed by space for
197
* the text file names.
199
* Add 1 to i because we need to skip over the NULL.
202
tstruct = (DB_MPOOL_FSTAT *)(tfsp + i + 1);
203
tname = (char *)(tstruct + i);
206
* Files may have been opened since we counted, don't walk
207
* off the end of the allocated space.
209
R_LOCK(dbenv, dbmp->reginfo);
210
for (mfp = SH_TAILQ_FIRST(&mp->mpfq, __mpoolfile);
211
mfp != NULL && i-- > 0;
212
++tfsp, ++tstruct, tname += nlen,
213
mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile)) {
214
name = __memp_fns(dbmp, mfp);
215
nlen = strlen(name) + 1;
217
*tstruct = mfp->stat;
218
if (LF_ISSET(DB_STAT_CLEAR)) {
219
pagesize = mfp->stat.st_pagesize;
220
memset(&mfp->stat, 0, sizeof(mfp->stat));
221
mfp->stat.st_pagesize = pagesize;
223
tstruct->file_name = tname;
224
memcpy(tname, name, nlen);
226
R_UNLOCK(dbenv, dbmp->reginfo);
233
#define FMAP_ENTRIES 200 /* Files we map. */
235
#define MPOOL_DUMP_HASH 0x01 /* Debug hash chains. */
236
#define MPOOL_DUMP_MEM 0x04 /* Debug region memory. */
237
#define MPOOL_DUMP_ALL 0x07 /* Debug all. */
240
* __memp_dump_region --
241
* Display MPOOL structures.
243
* PUBLIC: int __memp_dump_region __P((DB_ENV *, char *, FILE *));
246
__memp_dump_region(dbenv, area, fp)
251
static const FN fn[] = {
252
{ MP_CAN_MMAP, "mmapped" },
253
{ MP_DEADFILE, "dead" },
254
{ MP_DIRECT, "no buffer" },
255
{ MP_EXTENT, "extent" },
256
{ MP_TEMP, "temporary" },
257
{ MP_UNLINK, "unlink" },
264
size_t fmap[FMAP_ENTRIES + 1];
270
ENV_REQUIRES_CONFIG(dbenv,
271
dbenv->mp_handle, "memp_dump_region", DB_INIT_MPOOL);
273
dbmp = dbenv->mp_handle;
275
/* Make it easy to call from the debugger. */
279
for (flags = 0; *area != '\0'; ++area)
282
LF_SET(MPOOL_DUMP_ALL);
285
LF_SET(MPOOL_DUMP_HASH);
288
LF_SET(MPOOL_DUMP_MEM);
292
mp = dbmp->reginfo[0].primary;
294
/* Display MPOOL structures. */
295
(void)fprintf(fp, "%s\nPool (region addr 0x%lx)\n",
296
DB_LINE, P_TO_ULONG(dbmp->reginfo[0].addr));
298
/* Display the MPOOLFILE structures. */
299
R_LOCK(dbenv, dbmp->reginfo);
300
for (cnt = 0, mfp = SH_TAILQ_FIRST(&mp->mpfq, __mpoolfile);
301
mfp != NULL; mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile), ++cnt) {
302
(void)fprintf(fp, "File #%d: %s: pagesize %lu\n", cnt + 1,
303
__memp_fns(dbmp, mfp), (u_long)mfp->stat.st_pagesize);
304
(void)fprintf(fp, "\t type %ld; ref %lu; blocks %lu; last %lu;",
305
(long)mfp->ftype, (u_long)mfp->mpf_cnt,
306
(u_long)mfp->block_cnt, (u_long)mfp->last_pgno);
307
__db_prflags(mfp->flags, fn, fp);
309
(void)fprintf(fp, "\n\t UID: ");
310
p = R_ADDR(dbmp->reginfo, mfp->fileid_off);
311
for (i = 0; i < DB_FILE_ID_LEN; ++i, ++p) {
312
(void)fprintf(fp, "%x", (u_int)*p);
313
if (i < DB_FILE_ID_LEN - 1)
314
(void)fprintf(fp, " ");
316
(void)fprintf(fp, "\n");
317
if (cnt < FMAP_ENTRIES)
318
fmap[cnt] = R_OFFSET(dbmp->reginfo, mfp);
320
R_UNLOCK(dbenv, dbmp->reginfo);
322
MUTEX_THREAD_LOCK(dbenv, dbmp->mutexp);
323
for (dbmfp = TAILQ_FIRST(&dbmp->dbmfq);
324
dbmfp != NULL; dbmfp = TAILQ_NEXT(dbmfp, q), ++cnt) {
325
(void)fprintf(fp, "File #%d: %s: per-process, %s\n",
326
cnt + 1, __memp_fn(dbmfp),
327
F_ISSET(dbmfp, MP_READONLY) ? "readonly" : "read/write");
328
if (cnt < FMAP_ENTRIES)
329
fmap[cnt] = R_OFFSET(dbmp->reginfo, mfp);
331
MUTEX_THREAD_UNLOCK(dbenv, dbmp->mutexp);
332
if (cnt < FMAP_ENTRIES)
333
fmap[cnt] = INVALID_ROFF;
335
fmap[FMAP_ENTRIES] = INVALID_ROFF;
337
/* Dump the memory pools. */
338
for (i = 0; i < mp->nreg; ++i) {
339
(void)fprintf(fp, "%s\nCache #%d:\n", DB_LINE, i + 1);
341
dbenv, dbmp, &dbmp->reginfo[i], fmap, fp, flags);
344
/* Flush in case we're debugging. */
351
* __memp_dumpcache --
352
* Display statistics for a cache.
355
__memp_dumpcache(dbenv, dbmp, reginfo, fmap, fp, flags)
368
c_mp = reginfo->primary;
370
/* Display the hash table list of BH's. */
371
if (LF_ISSET(MPOOL_DUMP_HASH)) {
373
"%s\nBH hash table (%lu hash slots)\nbucket (priority):\n",
374
DB_LINE, (u_long)c_mp->htab_buckets);
376
"\tpageno, file, ref, address [LSN] priority\n");
378
for (hp = R_ADDR(reginfo, c_mp->htab),
379
bucket = 0; bucket < c_mp->htab_buckets; ++hp, ++bucket) {
380
MUTEX_LOCK(dbenv, &hp->hash_mutex);
382
SH_TAILQ_FIRST(&hp->hash_bucket, __bh)) != NULL)
383
(void)fprintf(fp, "%lu (%u):\n",
384
(u_long)bucket, hp->hash_priority);
385
for (; bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, hq, __bh))
386
__memp_pbh(dbmp, bhp, fmap, fp);
387
MUTEX_UNLOCK(dbenv, &hp->hash_mutex);
391
/* Dump the memory pool. */
392
if (LF_ISSET(MPOOL_DUMP_MEM))
393
__db_shalloc_dump(reginfo->addr, fp);
398
* Display a BH structure.
401
__memp_pbh(dbmp, bhp, fmap, fp)
407
static const FN fn[] = {
408
{ BH_CALLPGIN, "callpgin" },
409
{ BH_DIRTY, "dirty" },
410
{ BH_DIRTY_CREATE, "created" },
411
{ BH_DISCARD, "discard" },
412
{ BH_LOCKED, "locked" },
413
{ BH_TRASH, "trash" },
418
for (i = 0; i < FMAP_ENTRIES; ++i)
419
if (fmap[i] == INVALID_ROFF || fmap[i] == bhp->mf_offset)
422
if (fmap[i] == INVALID_ROFF)
423
(void)fprintf(fp, "\t%5lu, %lu, %2lu, %8lu [%lu,%lu] %lu",
424
(u_long)bhp->pgno, (u_long)bhp->mf_offset,
425
(u_long)bhp->ref, (u_long)R_OFFSET(dbmp->reginfo, bhp),
426
(u_long)LSN(bhp->buf).file, (u_long)LSN(bhp->buf).offset,
427
(u_long)bhp->priority);
429
(void)fprintf(fp, "\t%5lu, #%d, %2lu, %8lu [%lu,%lu] %lu",
430
(u_long)bhp->pgno, i + 1,
431
(u_long)bhp->ref, (u_long)R_OFFSET(dbmp->reginfo, bhp),
432
(u_long)LSN(bhp->buf).file, (u_long)LSN(bhp->buf).offset,
433
(u_long)bhp->priority);
435
__db_prflags(bhp->flags, fn, fp);
437
(void)fprintf(fp, "\n");
441
* __memp_stat_hash --
442
* Total hash bucket stats (other than mutex wait) into the region.
444
* PUBLIC: void __memp_stat_hash __P((REGINFO *, MPOOL *, u_int32_t *));
447
__memp_stat_hash(reginfo, mp, dirtyp)
456
hp = R_ADDR(reginfo, mp->htab);
457
for (i = 0, dirty = 0; i < mp->htab_buckets; i++, hp++)
458
dirty += hp->hash_page_dirty;
463
* __memp_stat_wait --
464
* Total hash bucket wait stats into the region.
467
__memp_stat_wait(reginfo, mp, mstat, flags)
470
DB_MPOOL_STAT *mstat;
477
mstat->st_hash_max_wait = 0;
478
hp = R_ADDR(reginfo, mp->htab);
479
for (i = 0; i < mp->htab_buckets; i++, hp++) {
480
mutexp = &hp->hash_mutex;
481
mstat->st_hash_nowait += mutexp->mutex_set_nowait;
482
mstat->st_hash_wait += mutexp->mutex_set_wait;
483
if (mutexp->mutex_set_wait > mstat->st_hash_max_wait)
484
mstat->st_hash_max_wait = mutexp->mutex_set_wait;
486
if (LF_ISSET(DB_STAT_CLEAR)) {
487
mutexp->mutex_set_wait = 0;
488
mutexp->mutex_set_nowait = 0;