db_deadlock.c
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1996-2002
  5.  * Sleepycat Software.  All rights reserved.
  6.  */
  7. #include "db_config.h"
  8. #ifndef lint
  9. static const char copyright[] =
  10.     "Copyright (c) 1996-2002nSleepycat Software Inc.  All rights reserved.n";
  11. static const char revid[] =
  12.     "$Id: db_deadlock.c,v 11.38 2002/08/08 03:50:32 bostic Exp $";
  13. #endif
  14. #ifndef NO_SYSTEM_INCLUDES
  15. #include <sys/types.h>
  16. #if TIME_WITH_SYS_TIME
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #else
  20. #if HAVE_SYS_TIME_H
  21. #include <sys/time.h>
  22. #else
  23. #include <time.h>
  24. #endif
  25. #endif
  26. #include <limits.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #endif
  32. #include "db_int.h"
  33. int main __P((int, char *[]));
  34. int usage __P((void));
  35. int version_check __P((const char *));
  36. int
  37. main(argc, argv)
  38. int argc;
  39. char *argv[];
  40. {
  41. extern char *optarg;
  42. extern int optind;
  43. const char *progname = "db_deadlock";
  44. DB_ENV  *dbenv;
  45. u_int32_t atype;
  46. time_t now;
  47. long secs, usecs;
  48. int ch, e_close, exitval, ret, verbose;
  49. char *home, *logfile, *str;
  50. if ((ret = version_check(progname)) != 0)
  51. return (ret);
  52. atype = DB_LOCK_DEFAULT;
  53. home = logfile = NULL;
  54. secs = usecs = 0;
  55. e_close = exitval = verbose = 0;
  56. while ((ch = getopt(argc, argv, "a:h:L:t:Vvw")) != EOF)
  57. switch (ch) {
  58. case 'a':
  59. switch (optarg[0]) {
  60. case 'e':
  61. atype = DB_LOCK_EXPIRE;
  62. break;
  63. case 'm':
  64. atype = DB_LOCK_MAXLOCKS;
  65. break;
  66. case 'n':
  67. atype = DB_LOCK_MINLOCKS;
  68. break;
  69. case 'o':
  70. atype = DB_LOCK_OLDEST;
  71. break;
  72. case 'w':
  73. atype = DB_LOCK_MINWRITE;
  74. break;
  75. case 'y':
  76. atype = DB_LOCK_YOUNGEST;
  77. break;
  78. default:
  79. return (usage());
  80. /* NOTREACHED */
  81. }
  82. if (optarg[1] != '')
  83. return (usage());
  84. break;
  85. case 'h':
  86. home = optarg;
  87. break;
  88. case 'L':
  89. logfile = optarg;
  90. break;
  91. case 't':
  92. if ((str = strchr(optarg, '.')) != NULL) {
  93. *str++ = '';
  94. if (*str != '' && __db_getlong(
  95.     NULL, progname, str, 0, LONG_MAX, &usecs))
  96. return (EXIT_FAILURE);
  97. }
  98. if (*optarg != '' && __db_getlong(
  99.     NULL, progname, optarg, 0, LONG_MAX, &secs))
  100. return (EXIT_FAILURE);
  101. if (secs == 0 && usecs == 0)
  102. return (usage());
  103. break;
  104. case 'V':
  105. printf("%sn", db_version(NULL, NULL, NULL));
  106. return (EXIT_SUCCESS);
  107. case 'v':
  108. verbose = 1;
  109. break;
  110. case 'w': /* Undocumented. */
  111. /* Detect every 100ms (100000 us) when polling. */
  112. secs = 0;
  113. usecs = 100000;
  114. break;
  115. case '?':
  116. default:
  117. return (usage());
  118. }
  119. argc -= optind;
  120. argv += optind;
  121. if (argc != 0)
  122. return (usage());
  123. /* Handle possible interruptions. */
  124. __db_util_siginit();
  125. /* Log our process ID. */
  126. if (logfile != NULL && __db_util_logset(progname, logfile))
  127. goto shutdown;
  128. /*
  129.  * Create an environment object and initialize it for error
  130.  * reporting.
  131.  */
  132. if ((ret = db_env_create(&dbenv, 0)) != 0) {
  133. fprintf(stderr,
  134.     "%s: db_env_create: %sn", progname, db_strerror(ret));
  135. goto shutdown;
  136. }
  137. e_close = 1;
  138. dbenv->set_errfile(dbenv, stderr);
  139. dbenv->set_errpfx(dbenv, progname);
  140. if (verbose) {
  141. (void)dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
  142. (void)dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, 1);
  143. }
  144. /* An environment is required. */
  145. if ((ret = dbenv->open(dbenv, home,
  146.     DB_JOINENV | DB_USE_ENVIRON, 0)) != 0) {
  147. dbenv->err(dbenv, ret, "open");
  148. goto shutdown;
  149. }
  150. while (!__db_util_interrupted()) {
  151. if (verbose) {
  152. (void)time(&now);
  153. dbenv->errx(dbenv, "running at %.24s", ctime(&now));
  154. }
  155. if ((ret = dbenv->lock_detect(dbenv, 0, atype, NULL)) != 0) {
  156. dbenv->err(dbenv, ret, "DB_ENV->lock_detect");
  157. goto shutdown;
  158. }
  159. /* Make a pass every "secs" secs and "usecs" usecs. */
  160. if (secs == 0 && usecs == 0)
  161. break;
  162. (void)__os_sleep(dbenv, secs, usecs);
  163. }
  164. if (0) {
  165. shutdown: exitval = 1;
  166. }
  167. /* Clean up the logfile. */
  168. if (logfile != NULL)
  169. remove(logfile);
  170. /* Clean up the environment. */
  171. if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
  172. exitval = 1;
  173. fprintf(stderr,
  174.     "%s: dbenv->close: %sn", progname, db_strerror(ret));
  175. }
  176. /* Resend any caught signal. */
  177. __db_util_sigresend();
  178. return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  179. }
  180. int
  181. usage()
  182. {
  183. (void)fprintf(stderr, "%snt%sn",
  184.     "usage: db_deadlock [-Vv]",
  185.     "[-a e | m | n | o | w | y] [-h home] [-L file] [-t sec.usec]");
  186. return (EXIT_FAILURE);
  187. }
  188. int
  189. version_check(progname)
  190. const char *progname;
  191. {
  192. int v_major, v_minor, v_patch;
  193. /* Make sure we're loaded with the right version of the DB library. */
  194. (void)db_version(&v_major, &v_minor, &v_patch);
  195. if (v_major != DB_VERSION_MAJOR ||
  196.     v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
  197. fprintf(stderr,
  198. "%s: version %d.%d.%d doesn't match library version %d.%d.%dn",
  199.     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
  200.     DB_VERSION_PATCH, v_major, v_minor, v_patch);
  201. return (EXIT_FAILURE);
  202. }
  203. return (0);
  204. }