1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2001, 2010 Oracle and/or its affiliates. All rights reserved. 5 * 6 * $Id$ 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, 2010 Oracle and/or its affiliates. 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) { 303 /* Handles cannot be reused after a failed DB->open. */ 304 (void)dbp->close(dbp, 0); 305 if ((ret = db_create(&dbp, dbenv, 0)) != 0) { 306 dbenv->err(dbenv, ret, "db_create"); 307 goto err; 308 } 309 310 if ((ret = dbp->open(dbp, 311 NULL, db, subdb, DB_UNKNOWN, DB_RDONLY, 0)) != 0) { 312 dbenv->err(dbenv, ret, "DB->open: %s", db); 313 goto err; 314 } 315 } 316 317 /* Check if cache is too small for this DB's pagesize. */ 318 if (private) { 319 if ((ret = __db_util_cache(dbp, &cache, &resize)) != 0) 320 goto err; 321 if (resize) { 322 (void)dbp->close(dbp, DB_NOSYNC); 323 dbp = NULL; 324 325 (void)dbenv->close(dbenv, 0); 326 dbenv = NULL; 327 goto retry; 328 } 329 } 330 331 if (dbp->stat_print(dbp, flags)) 332 goto err; 333 break; 334 case T_ENV: 335 if (dbenv->stat_print(dbenv, flags)) 336 goto err; 337 break; 338 case T_LOCK: 339 if (dbenv->lock_stat_print(dbenv, flags)) 340 goto err; 341 break; 342 case T_LOG: 343 if (dbenv->log_stat_print(dbenv, flags)) 344 goto err; 345 break; 346 case T_MPOOL: 347 if (dbenv->memp_stat_print(dbenv, flags)) 348 goto err; 349 break; 350 case T_MUTEX: 351 if (dbenv->mutex_stat_print(dbenv, flags)) 352 goto err; 353 break; 354 case T_REP: 355#ifdef HAVE_REPLICATION_THREADS 356 if (dbenv->repmgr_stat_print(dbenv, flags)) 357 goto err; 358#endif 359 if (dbenv->rep_stat_print(dbenv, flags)) 360 goto err; 361 break; 362 case T_TXN: 363 if (dbenv->txn_stat_print(dbenv, flags)) 364 goto err; 365 break; 366 case T_NOTSET: 367 dbenv->errx(dbenv, "Unknown statistics flag"); 368 goto err; 369 } 370 371 if (0) { 372err: exitval = 1; 373 } 374 if (dbp != NULL && (ret = dbp->close(dbp, DB_NOSYNC)) != 0) { 375 exitval = 1; 376 dbenv->err(dbenv, ret, "close"); 377 } 378 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) { 379 exitval = 1; 380 fprintf(stderr, 381 "%s: dbenv->close: %s\n", progname, db_strerror(ret)); 382 } 383 384 if (passwd != NULL) 385 free(passwd); 386 387 /* Resend any caught signal. */ 388 __db_util_sigresend(); 389 390 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 391} 392 393/* 394 * db_init -- 395 * Initialize the environment. 396 */ 397int 398db_stat_db_init(dbenv, home, ttype, cache, is_private) 399 DB_ENV *dbenv; 400 char *home; 401 test_t ttype; 402 u_int32_t cache; 403 int *is_private; 404{ 405 u_int32_t oflags; 406 int ret; 407 408 /* 409 * If our environment open fails, and we're trying to look at a 410 * shared region, it's a hard failure. 411 * 412 * We will probably just drop core if the environment we join does 413 * not include a memory pool. This is probably acceptable; trying 414 * to use an existing environment that does not contain a memory 415 * pool to look at a database can be safely construed as operator 416 * error, I think. 417 */ 418 *is_private = 0; 419 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) == 0) 420 return (0); 421 if (ret == DB_VERSION_MISMATCH) 422 goto err; 423 if (ttype != T_DB && ttype != T_LOG) { 424 dbenv->err(dbenv, ret, "DB_ENV->open%s%s", 425 home == NULL ? "" : ": ", home == NULL ? "" : home); 426 return (1); 427 } 428 429 /* 430 * We're looking at a database or set of log files and no environment 431 * exists. Create one, but make it private so no files are actually 432 * created. Declare a reasonably large cache so that we don't fail 433 * when reporting statistics on large databases. 434 * 435 * An environment is required to look at databases because we may be 436 * trying to look at databases in directories other than the current 437 * one. 438 */ 439 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) { 440 dbenv->err(dbenv, ret, "set_cachesize"); 441 return (1); 442 } 443 *is_private = 1; 444 oflags = DB_CREATE | DB_PRIVATE | DB_USE_ENVIRON; 445 if (ttype == T_DB) 446 oflags |= DB_INIT_MPOOL; 447 if (ttype == T_LOG) 448 oflags |= DB_INIT_LOG; 449 if ((ret = dbenv->open(dbenv, home, oflags, 0)) == 0) 450 return (0); 451 452 /* An environment is required. */ 453err: dbenv->err(dbenv, ret, "DB_ENV->open"); 454 return (1); 455} 456 457int 458db_stat_usage() 459{ 460 fprintf(stderr, "usage: %s %s\n", progname, 461 "-d file [-fN] [-h home] [-P password] [-s database]"); 462 fprintf(stderr, "usage: %s %s\n\t%s\n", progname, 463 "[-cEelmNrtVxZ] [-C Aclop]", 464 "[-h home] [-L A] [-M A] [-P password] [-R A] [-X A]"); 465 return (EXIT_FAILURE); 466} 467 468int 469db_stat_version_check() 470{ 471 int v_major, v_minor, v_patch; 472 473 /* Make sure we're loaded with the right version of the DB library. */ 474 (void)db_version(&v_major, &v_minor, &v_patch); 475 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { 476 fprintf(stderr, 477 "%s: version %d.%d doesn't match library version %d.%d\n", 478 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR, 479 v_major, v_minor); 480 return (EXIT_FAILURE); 481 } 482 return (0); 483} 484