📄 db_stat.c
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 1996-2002 * Sleepycat Software. All rights reserved. */#include "db_config.h"#ifndef lintstatic const char copyright[] = "Copyright (c) 1996-2002\nSleepycat Software Inc. All rights reserved.\n";static const char revid[] = "$Id: db_stat.c,v 11.125 2002/08/08 15:26:15 bostic Exp $";#endif#ifndef NO_SYSTEM_INCLUDES#include <sys/types.h>#if TIME_WITH_SYS_TIME#include <sys/time.h>#include <time.h>#else#if HAVE_SYS_TIME_H#include <sys/time.h>#else#include <time.h>#endif#endif#include <ctype.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#endif#include "db_int.h"#include "dbinc/db_page.h"#define PCT(f, t, pgsize) \ ((t) == 0 ? 0 : \ (((double)(((t) * (pgsize)) - (f)) / ((t) * (pgsize))) * 100))typedef enum { T_NOTSET, T_DB, T_ENV, T_LOCK, T_LOG, T_MPOOL, T_REP, T_TXN } test_t;int argcheck __P((char *, const char *));int btree_stats __P((DB_ENV *, DB *, DB_BTREE_STAT *, int));int db_init __P((DB_ENV *, char *, test_t, u_int32_t, int *));void dl __P((const char *, u_long));void dl_bytes __P((const char *, u_long, u_long, u_long));int env_stats __P((DB_ENV *, u_int32_t));int hash_stats __P((DB_ENV *, DB *, int));int lock_stats __P((DB_ENV *, char *, u_int32_t));int log_stats __P((DB_ENV *, u_int32_t));int main __P((int, char *[]));int mpool_stats __P((DB_ENV *, char *, u_int32_t));void prflags __P((u_int32_t, const FN *));int queue_stats __P((DB_ENV *, DB *, int));int rep_stats __P((DB_ENV *, u_int32_t));int txn_compare __P((const void *, const void *));int txn_stats __P((DB_ENV *, u_int32_t));int usage __P((void));int version_check __P((const char *));intmain(argc, argv) int argc; char *argv[];{ extern char *optarg; extern int optind; const char *progname = "db_stat"; DB_ENV *dbenv; DB_BTREE_STAT *sp; DB *alt_dbp, *dbp; test_t ttype; u_int32_t cache; int ch, checked, d_close, e_close, exitval, fast, flags; int nflag, private, resize, ret; char *db, *home, *internal, *passwd, *subdb; if ((ret = version_check(progname)) != 0) return (ret); dbp = NULL; ttype = T_NOTSET; cache = MEGABYTE; d_close = e_close = exitval = fast = flags = nflag = private = 0; db = home = internal = passwd = subdb = NULL; while ((ch = getopt(argc, argv, "C:cd:efh:lM:mNP:rs:tVZ")) != EOF) switch (ch) { case 'C': if (ttype != T_NOTSET) goto argcombo; ttype = T_LOCK; if (!argcheck(internal = optarg, "Aclmop")) return (usage()); break; case 'c': if (ttype != T_NOTSET) goto argcombo; ttype = T_LOCK; break; case 'd': if (ttype != T_DB && ttype != T_NOTSET) goto argcombo; ttype = T_DB; db = optarg; break; case 'e': if (ttype != T_NOTSET) goto argcombo; ttype = T_ENV; break; case 'f': fast = DB_FAST_STAT; break; case 'h': home = optarg; break; case 'l': if (ttype != T_NOTSET) goto argcombo; ttype = T_LOG; break; case 'M': if (ttype != T_NOTSET) goto argcombo; ttype = T_MPOOL; if (!argcheck(internal = optarg, "Ahm")) return (usage()); break; case 'm': if (ttype != T_NOTSET) goto argcombo; ttype = T_MPOOL; break; case 'N': nflag = 1; break; case 'P': passwd = strdup(optarg); memset(optarg, 0, strlen(optarg)); if (passwd == NULL) { fprintf(stderr, "%s: strdup: %s\n", progname, strerror(errno)); return (EXIT_FAILURE); } break; case 'r': if (ttype != T_NOTSET) goto argcombo; ttype = T_REP; break; case 's': if (ttype != T_DB && ttype != T_NOTSET) goto argcombo; ttype = T_DB; subdb = optarg; break; case 't': if (ttype != T_NOTSET) {argcombo: fprintf(stderr, "%s: illegal option combination\n", progname); return (EXIT_FAILURE); } ttype = T_TXN; break; case 'V': printf("%s\n", db_version(NULL, NULL, NULL)); return (EXIT_SUCCESS); case 'Z': flags |= DB_STAT_CLEAR; break; case '?': default: return (usage()); } argc -= optind; argv += optind; switch (ttype) { case T_DB: if (db == NULL) return (usage()); break; case T_NOTSET: return (usage()); /* NOTREACHED */ default: if (fast != 0) return (usage()); break; } /* Handle possible interruptions. */ __db_util_siginit(); /* * Create an environment object and initialize it for error * reporting. */retry: if ((ret = db_env_create(&dbenv, 0)) != 0) { fprintf(stderr, "%s: db_env_create: %s\n", progname, db_strerror(ret)); goto shutdown; } e_close = 1; dbenv->set_errfile(dbenv, stderr); dbenv->set_errpfx(dbenv, progname); if (nflag) { if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) { dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING"); goto shutdown; } if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) { dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC"); goto shutdown; } } if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) { dbenv->err(dbenv, ret, "set_passwd"); goto shutdown; } /* Initialize the environment. */ if (db_init(dbenv, home, ttype, cache, &private) != 0) goto shutdown; switch (ttype) { case T_DB: /* Create the DB object and open the file. */ if (flags != 0) return (usage()); if ((ret = db_create(&dbp, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "db_create"); goto shutdown; } d_close = 1; if ((ret = dbp->open(dbp, NULL, db, subdb, DB_UNKNOWN, DB_RDONLY, 0)) != 0) { dbp->err(dbp, ret, "DB->open: %s", db); goto shutdown; } /* Check if cache is too small for this DB's pagesize. */ if (private) { if ((ret = __db_util_cache(dbenv, dbp, &cache, &resize)) != 0) goto shutdown; if (resize) { (void)dbp->close(dbp, 0); d_close = 0; (void)dbenv->close(dbenv, 0); e_close = 0; goto retry; } } /* * See if we can open this db read/write to update counts. * If its a master-db then we cannot. So check to see, * if its btree then it might be. */ checked = 0; if (subdb == NULL && dbp->type == DB_BTREE) { if ((ret = dbp->stat(dbp, &sp, DB_FAST_STAT)) != 0) { dbp->err(dbp, ret, "DB->stat"); goto shutdown; } checked = 1; } if (subdb != NULL || dbp->type != DB_BTREE || (sp->bt_metaflags & BTM_SUBDB) == 0) { if ((ret = db_create(&alt_dbp, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "db_create"); goto shutdown; } if ((ret = dbp->open(alt_dbp, NULL, db, subdb, DB_UNKNOWN, 0, 0)) != 0) { dbenv->err(dbenv, ret, "DB->open: %s:%s", db, subdb); (void)alt_dbp->close(alt_dbp, 0); goto shutdown; } (void)dbp->close(dbp, 0); dbp = alt_dbp; /* Need to run again to update counts */ checked = 0; } switch (dbp->type) { case DB_BTREE: case DB_RECNO: if (btree_stats( dbenv, dbp, checked == 1 ? sp : NULL, fast)) goto shutdown; break; case DB_HASH: if (hash_stats(dbenv, dbp, fast)) goto shutdown; break; case DB_QUEUE: if (queue_stats(dbenv, dbp, fast)) goto shutdown; break; case DB_UNKNOWN: dbenv->errx(dbenv, "Unknown database type."); goto shutdown; } break; case T_ENV: if (env_stats(dbenv, flags)) goto shutdown; break; case T_LOCK: if (lock_stats(dbenv, internal, flags)) goto shutdown; break; case T_LOG: if (log_stats(dbenv, flags)) goto shutdown; break; case T_MPOOL: if (mpool_stats(dbenv, internal, flags)) goto shutdown; break; case T_REP: if (rep_stats(dbenv, flags)) goto shutdown; break; case T_TXN: if (txn_stats(dbenv, flags)) goto shutdown; break; case T_NOTSET: dbenv->errx(dbenv, "Unknown statistics flag."); goto shutdown; } if (0) {shutdown: exitval = 1; } if (d_close && (ret = dbp->close(dbp, 0)) != 0) { exitval = 1; dbenv->err(dbenv, ret, "close"); } if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) { exitval = 1; fprintf(stderr, "%s: dbenv->close: %s\n", progname, db_strerror(ret)); } /* Resend any caught signal. */ __db_util_sigresend(); return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);}/* * env_stats -- * Display environment statistics. */intenv_stats(dbenv, flags) DB_ENV *dbenv; u_int32_t flags;{ REGENV renv; REGION *rp, regs[1024]; int n, ret; const char *lable; n = sizeof(regs) / sizeof(regs[0]); if ((ret = __db_e_stat(dbenv, &renv, regs, &n, flags)) != 0) { dbenv->err(dbenv, ret, "__db_e_stat"); return (1); } printf("%d.%d.%d\tEnvironment version.\n", renv.majver, renv.minver, renv.patch); printf("%lx\tMagic number.\n", (u_long)renv.magic); printf("%d\tPanic value.\n", renv.envpanic); /* Adjust the reference count for us... */ printf("%d\tReferences.\n", renv.refcnt - 1); dl("Locks granted without waiting.\n", (u_long)renv.mutex.mutex_set_nowait); dl("Locks granted after waiting.\n", (u_long)renv.mutex.mutex_set_wait); while (n > 0) { printf("%s\n", DB_LINE); rp = ®s[--n]; switch (rp->type) { case REGION_TYPE_ENV: lable = "Environment"; break; case REGION_TYPE_LOCK: lable = "Lock"; break; case REGION_TYPE_LOG: lable = "Log"; break; case REGION_TYPE_MPOOL: lable = "Mpool"; break; case REGION_TYPE_MUTEX: lable = "Mutex";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -