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