1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1996,2008 Oracle. All rights reserved. 5 * 6 * $Id: db_stat.c,v 12.23 2008/01/08 20:58:16 bostic Exp $ 7 */ 8 9#include "db_config.h" 10 11#include "db_int.h" 12 13#ifndef lint 14static const char copyright[] = 15 "Copyright (c) 1996,2008 Oracle. All rights reserved.\n"; 16#endif 17 18typedef enum { T_NOTSET, T_DB, 19 T_ENV, T_LOCK, T_LOG, T_MPOOL, T_MUTEX, T_REP, T_TXN } test_t; 20 21int db_stat_db_init __P((DB_ENV *, char *, test_t, u_int32_t, int *)); 22int db_stat_main __P((int, char *[])); 23int db_stat_usage __P((void)); 24int db_stat_version_check __P((void)); 25 26const char *progname; 27 28int 29db_stat(args) 30 char *args; 31{ 32 int argc; 33 char **argv; 34 35 __db_util_arg("db_stat", args, &argc, &argv); 36 return (db_stat_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS); 37} 38 39#include <stdio.h> 40#define ERROR_RETURN ERROR 41 42int 43db_stat_main(argc, argv) 44 int argc; 45 char *argv[]; 46{ 47 extern char *optarg; 48 extern int optind, __db_getopt_reset; 49 DB_ENV *dbenv; 50 DB *dbp; 51 test_t ttype; 52 u_int32_t cache, flags; 53 int ch, exitval; 54 int nflag, private, resize, ret; 55 char *db, *home, *p, *passwd, *subdb; 56 57 if ((progname = __db_rpath(argv[0])) == NULL) 58 progname = argv[0]; 59 else 60 ++progname; 61 62 if ((ret = db_stat_version_check()) != 0) 63 return (ret); 64 65 dbenv = NULL; 66 dbp = NULL; 67 ttype = T_NOTSET; 68 cache = MEGABYTE; 69 exitval = flags = nflag = private = 0; 70 db = home = passwd = subdb = NULL; 71 72 __db_getopt_reset = 1; 73 while ((ch = getopt(argc, 74 argv, "C:cd:Eefgh:L:lM:mNP:R:rs:tVxX:Z")) != EOF) 75 switch (ch) { 76 case 'C': case 'c': 77 if (ttype != T_NOTSET && ttype != T_LOCK) 78 goto argcombo; 79 ttype = T_LOCK; 80 if (ch != 'c') 81 for (p = optarg; *p; ++p) 82 switch (*p) { 83 case 'A': 84 LF_SET(DB_STAT_ALL); 85 break; 86 case 'c': 87 LF_SET(DB_STAT_LOCK_CONF); 88 break; 89 case 'l': 90 LF_SET(DB_STAT_LOCK_LOCKERS); 91 break; 92 case 'm': /* Backward compatible. */ 93 break; 94 case 'o': 95 LF_SET(DB_STAT_LOCK_OBJECTS); 96 break; 97 case 'p': 98 LF_SET(DB_STAT_LOCK_PARAMS); 99 break; 100 default: 101 return (db_stat_usage()); 102 } 103 break; 104 case 'd': 105 if (ttype != T_NOTSET && ttype != T_DB) 106 goto argcombo; 107 ttype = T_DB; 108 db = optarg; 109 break; 110 case 'E': case 'e': 111 if (ttype != T_NOTSET && ttype != T_ENV) 112 goto argcombo; 113 ttype = T_ENV; 114 LF_SET(DB_STAT_SUBSYSTEM); 115 if (ch == 'E') 116 LF_SET(DB_STAT_ALL); 117 break; 118 case 'f': 119 if (ttype != T_NOTSET && ttype != T_DB) 120 goto argcombo; 121 ttype = T_DB; 122 LF_SET(DB_FAST_STAT); 123 break; 124 case 'h': 125 home = optarg; 126 break; 127 case 'L': case 'l': 128 if (ttype != T_NOTSET && ttype != T_LOG) 129 goto argcombo; 130 ttype = T_LOG; 131 if (ch != 'l') 132 for (p = optarg; *p; ++p) 133 switch (*p) { 134 case 'A': 135 LF_SET(DB_STAT_ALL); 136 break; 137 default: 138 return (db_stat_usage()); 139 } 140 break; 141 case 'M': case 'm': 142 if (ttype != T_NOTSET && ttype != T_MPOOL) 143 goto argcombo; 144 ttype = T_MPOOL; 145 if (ch != 'm') 146 for (p = optarg; *p; ++p) 147 switch (*p) { 148 case 'A': 149 LF_SET(DB_STAT_ALL); 150 break; 151 case 'h': 152 LF_SET(DB_STAT_MEMP_HASH); 153 break; 154 case 'm': /* Backward compatible. */ 155 break; 156 default: 157 return (db_stat_usage()); 158 } 159 break; 160 case 'N': 161 nflag = 1; 162 break; 163 case 'P': 164 passwd = strdup(optarg); 165 memset(optarg, 0, strlen(optarg)); 166 if (passwd == NULL) { 167 fprintf(stderr, "%s: strdup: %s\n", 168 progname, strerror(errno)); 169 return (EXIT_FAILURE); 170 } 171 break; 172 case 'R': case 'r': 173 if (ttype != T_NOTSET && ttype != T_REP) 174 goto argcombo; 175 ttype = T_REP; 176 if (ch != 'r') 177 for (p = optarg; *p; ++p) 178 switch (*p) { 179 case 'A': 180 LF_SET(DB_STAT_ALL); 181 break; 182 default: 183 return (db_stat_usage()); 184 } 185 break; 186 case 's': 187 if (ttype != T_NOTSET && ttype != T_DB) 188 goto argcombo; 189 ttype = T_DB; 190 subdb = optarg; 191 break; 192 case 't': 193 if (ttype != T_NOTSET) { 194argcombo: fprintf(stderr, 195 "%s: illegal option combination\n", 196 progname); 197 return (db_stat_usage()); 198 } 199 ttype = T_TXN; 200 break; 201 case 'V': 202 printf("%s\n", db_version(NULL, NULL, NULL)); 203 return (EXIT_SUCCESS); 204 case 'X': case 'x': 205 if (ttype != T_NOTSET && ttype != T_MUTEX) 206 goto argcombo; 207 ttype = T_MUTEX; 208 if (ch != 'x') 209 for (p = optarg; *p; ++p) 210 switch (*p) { 211 case 'A': 212 LF_SET(DB_STAT_ALL); 213 break; 214 default: 215 return (db_stat_usage()); 216 } 217 break; 218 case 'Z': 219 LF_SET(DB_STAT_CLEAR); 220 break; 221 case '?': 222 default: 223 return (db_stat_usage()); 224 } 225 argc -= optind; 226 argv += optind; 227 228 switch (ttype) { 229 case T_DB: 230 if (db == NULL) 231 return (db_stat_usage()); 232 break; 233 case T_ENV: 234 case T_LOCK: 235 case T_LOG: 236 case T_MPOOL: 237 case T_MUTEX: 238 case T_REP: 239 case T_TXN: 240 break; 241 case T_NOTSET: 242 return (db_stat_usage()); 243 } 244 245 /* Handle possible interruptions. */ 246 __db_util_siginit(); 247 248 /* 249 * Create an environment object and initialize it for error 250 * reporting. 251 */ 252retry: if ((ret = db_env_create(&dbenv, 0)) != 0) { 253 fprintf(stderr, 254 "%s: db_env_create: %s\n", progname, db_strerror(ret)); 255 goto err; 256 } 257 258 dbenv->set_errfile(dbenv, stderr); 259 dbenv->set_errpfx(dbenv, progname); 260 261 if (nflag) { 262 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) { 263 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING"); 264 goto err; 265 } 266 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) { 267 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC"); 268 goto err; 269 } 270 } 271 272 if (passwd != NULL && 273 (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) { 274 dbenv->err(dbenv, ret, "set_passwd"); 275 goto err; 276 } 277 278 /* Initialize the environment. */ 279 if (db_stat_db_init(dbenv, home, ttype, cache, &private) != 0) 280 goto err; 281 282 switch (ttype) { 283 case T_DB: 284 /* Create the DB object and open the file. */ 285 if ((ret = db_create(&dbp, dbenv, 0)) != 0) { 286 dbenv->err(dbenv, ret, "db_create"); 287 goto err; 288 } 289 290 /* 291 * We open the database for writing so we can update the cached 292 * statistics, but it's OK to fail, we can open read-only and 293 * proceed. 294 * 295 * Turn off error messages for now -- we can't open lots of 296 * databases read-write (for example, master databases and 297 * hash databases for which we don't know the hash function). 298 */ 299 dbenv->set_errfile(dbenv, NULL); 300 ret = dbp->open(dbp, NULL, db, subdb, DB_UNKNOWN, 0, 0); 301 dbenv->set_errfile(dbenv, stderr); 302 if (ret != 0 && (ret = dbp->open( 303 dbp, NULL, db, subdb, DB_UNKNOWN, DB_RDONLY, 0)) != 0) { 304 dbenv->err(dbenv, ret, "DB->open: %s", db); 305 goto err; 306 } 307 308 /* Check if cache is too small for this DB's pagesize. */ 309 if (private) { 310 if ((ret = __db_util_cache(dbp, &cache, &resize)) != 0) 311 goto err; 312 if (resize) { 313 (void)dbp->close(dbp, DB_NOSYNC); 314 dbp = NULL; 315 316 (void)dbenv->close(dbenv, 0); 317 dbenv = NULL; 318 goto retry; 319 } 320 } 321 322 if (dbp->stat_print(dbp, flags)) 323 goto err; 324 break; 325 case T_ENV: 326 if (dbenv->stat_print(dbenv, flags)) 327 goto err; 328 break; 329 case T_LOCK: 330 if (dbenv->lock_stat_print(dbenv, flags)) 331 goto err; 332 break; 333 case T_LOG: 334 if (dbenv->log_stat_print(dbenv, flags)) 335 goto err; 336 break; 337 case T_MPOOL: 338 if (dbenv->memp_stat_print(dbenv, flags)) 339 goto err; 340 break; 341 case T_MUTEX: 342 if (dbenv->mutex_stat_print(dbenv, flags)) 343 goto err; 344 break; 345 case T_REP: 346#ifdef HAVE_REPLICATION_THREADS 347 if (dbenv->repmgr_stat_print(dbenv, flags)) 348 goto err; 349#endif 350 if (dbenv->rep_stat_print(dbenv, flags)) 351 goto err; 352 break; 353 case T_TXN: 354 if (dbenv->txn_stat_print(dbenv, flags)) 355 goto err; 356 break; 357 case T_NOTSET: 358 dbenv->errx(dbenv, "Unknown statistics flag"); 359 goto err; 360 } 361 362 if (0) { 363err: exitval = 1; 364 } 365 if (dbp != NULL && (ret = dbp->close(dbp, DB_NOSYNC)) != 0) { 366 exitval = 1; 367 dbenv->err(dbenv, ret, "close"); 368 } 369 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) { 370 exitval = 1; 371 fprintf(stderr, 372 "%s: dbenv->close: %s\n", progname, db_strerror(ret)); 373 } 374 375 if (passwd != NULL) 376 free(passwd); 377 378 /* Resend any caught signal. */ 379 __db_util_sigresend(); 380 381 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 382} 383 384/* 385 * db_init -- 386 * Initialize the environment. 387 */ 388int 389db_stat_db_init(dbenv, home, ttype, cache, is_private) 390 DB_ENV *dbenv; 391 char *home; 392 test_t ttype; 393 u_int32_t cache; 394 int *is_private; 395{ 396 u_int32_t oflags; 397 int ret; 398 399 /* 400 * If our environment open fails, and we're trying to look at a 401 * shared region, it's a hard failure. 402 * 403 * We will probably just drop core if the environment we join does 404 * not include a memory pool. This is probably acceptable; trying 405 * to use an existing environment that does not contain a memory 406 * pool to look at a database can be safely construed as operator 407 * error, I think. 408 */ 409 *is_private = 0; 410 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) == 0) 411 return (0); 412 if (ret == DB_VERSION_MISMATCH) 413 goto err; 414 if (ttype != T_DB && ttype != T_LOG) { 415 dbenv->err(dbenv, ret, "DB_ENV->open%s%s", 416 home == NULL ? "" : ": ", home == NULL ? "" : home); 417 return (1); 418 } 419 420 /* 421 * We're looking at a database or set of log files and no environment 422 * exists. Create one, but make it private so no files are actually 423 * created. Declare a reasonably large cache so that we don't fail 424 * when reporting statistics on large databases. 425 * 426 * An environment is required to look at databases because we may be 427 * trying to look at databases in directories other than the current 428 * one. 429 */ 430 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) { 431 dbenv->err(dbenv, ret, "set_cachesize"); 432 return (1); 433 } 434 *is_private = 1; 435 oflags = DB_CREATE | DB_PRIVATE | DB_USE_ENVIRON; 436 if (ttype == T_DB) 437 oflags |= DB_INIT_MPOOL; 438 if (ttype == T_LOG) 439 oflags |= DB_INIT_LOG; 440 if ((ret = dbenv->open(dbenv, home, oflags, 0)) == 0) 441 return (0); 442 443 /* An environment is required. */ 444err: dbenv->err(dbenv, ret, "DB_ENV->open"); 445 return (1); 446} 447 448int 449db_stat_usage() 450{ 451 fprintf(stderr, "usage: %s %s\n", progname, 452 "-d file [-fN] [-h home] [-P password] [-s database]"); 453 fprintf(stderr, "usage: %s %s\n\t%s\n", progname, 454 "[-cEelmNrtVxZ] [-C Aclop]", 455 "[-h home] [-L A] [-M A] [-P password] [-R A] [-X A]"); 456 return (EXIT_FAILURE); 457} 458 459int 460db_stat_version_check() 461{ 462 int v_major, v_minor, v_patch; 463 464 /* Make sure we're loaded with the right version of the DB library. */ 465 (void)db_version(&v_major, &v_minor, &v_patch); 466 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { 467 fprintf(stderr, 468 "%s: version %d.%d doesn't match library version %d.%d\n", 469 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR, 470 v_major, v_minor); 471 return (EXIT_FAILURE); 472 } 473 return (0); 474} 475