MySQL  8.0.12
Source Code Documentation
ut0dbg.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 1994, 2018, Oracle and/or its affiliates. All Rights Reserved.
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24  The lines above are intentionally left blank
25 */
26 
27 /** @file include/ut0dbg.h
28  Debug utilities for Innobase
29 
30  Created 1/30/1994 Heikki Tuuri
31  **********************************************************************/
32 
33 #ifndef ut0dbg_h
34 #define ut0dbg_h
35 
36 /* Do not include univ.i because univ.i includes this. */
37 
38 #include "os0thread.h"
39 
40 /** Report a failed assertion. */
41 [[noreturn]] void ut_dbg_assertion_failed(
42  const char *expr, /*!< in: the failed assertion */
43  const char *file, /*!< in: source file containing the assertion */
44  ulint line); /*!< in: line number of the assertion */
45 
46 /** Abort execution if EXPR does not evaluate to nonzero.
47 @param EXPR assertion expression that should hold */
48 #define ut_a(EXPR) \
49  do { \
50  if (UNIV_UNLIKELY(!(ulint)(EXPR))) { \
51  ut_dbg_assertion_failed(#EXPR, __FILE__, (ulint)__LINE__); \
52  } \
53  } while (0)
54 
55 /** Abort execution. */
56 #define ut_error ut_dbg_assertion_failed(0, __FILE__, (ulint)__LINE__)
57 
58 #ifdef UNIV_DEBUG
59 /** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
60 #define ut_ad(EXPR) ut_a(EXPR)
61 /** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
62 #define ut_d(EXPR) EXPR
63 #else
64 /** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
65 #define ut_ad(EXPR)
66 /** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
67 #define ut_d(EXPR)
68 #endif
69 
70 /** Debug crash point */
71 #ifdef UNIV_DEBUG
72 #define DBUG_INJECT_CRASH(prefix, count) \
73  do { \
74  char buf[64]; \
75  snprintf(buf, sizeof buf, prefix "_%u", count); \
76  DBUG_EXECUTE_IF(buf, DBUG_SUICIDE();); \
77  } while (0)
78 #else
79 #define DBUG_INJECT_CRASH(prefix, count)
80 #endif
81 
82 /** Silence warnings about an unused variable by doing a null assignment.
83 @param A the unused variable */
84 #define UT_NOT_USED(A) A = A
85 
86 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
87 
88 #define HAVE_UT_CHRONO_T
89 
90 #include <sys/resource.h>
91 #include <sys/time.h>
92 #include <sys/types.h>
93 
94 /** A "chronometer" used to clock snippets of code.
95 Example usage:
96  ut_chrono_t ch("this loop");
97  for (;;) { ... }
98  ch.show();
99 would print the timings of the for() loop, prefixed with "this loop:" */
100 class ut_chrono_t {
101  public:
102  /** Constructor.
103  @param[in] name chrono's name, used when showing the values */
104  ut_chrono_t(const char *name) : m_name(name), m_show_from_destructor(true) {
105  reset();
106  }
107 
108  /** Resets the chrono (records the current time in it). */
109  void reset() {
110  gettimeofday(&m_tv, NULL);
111 
112  getrusage(RUSAGE_SELF, &m_ru);
113  }
114 
115  /** Shows the time elapsed and usage statistics since the last reset. */
116  void show() {
117  struct rusage ru_now;
118  struct timeval tv_now;
119  struct timeval tv_diff;
120 
121  getrusage(RUSAGE_SELF, &ru_now);
122 
123  gettimeofday(&tv_now, NULL);
124 
125 #ifndef timersub
126 #define timersub(a, b, r) \
127  do { \
128  (r)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
129  (r)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
130  if ((r)->tv_usec < 0) { \
131  (r)->tv_sec--; \
132  (r)->tv_usec += 1000000; \
133  } \
134  } while (0)
135 #endif /* timersub */
136 
137 #define CHRONO_PRINT(type, tvp) \
138  fprintf(stderr, "%s: %s% 5ld.%06ld sec\n", m_name, type, \
139  static_cast<long>((tvp)->tv_sec), static_cast<long>((tvp)->tv_usec))
140 
141  timersub(&tv_now, &m_tv, &tv_diff);
142  CHRONO_PRINT("real", &tv_diff);
143 
144  timersub(&ru_now.ru_utime, &m_ru.ru_utime, &tv_diff);
145  CHRONO_PRINT("user", &tv_diff);
146 
147  timersub(&ru_now.ru_stime, &m_ru.ru_stime, &tv_diff);
148  CHRONO_PRINT("sys ", &tv_diff);
149  }
150 
151  /** Cause the timings not to be printed from the destructor. */
152  void end() { m_show_from_destructor = false; }
153 
154  /** Destructor. */
155  ~ut_chrono_t() {
156  if (m_show_from_destructor) {
157  show();
158  }
159  }
160 
161  private:
162  /** Name of this chronometer. */
163  const char *m_name;
164 
165  /** True if the current timings should be printed by the destructor. */
166  bool m_show_from_destructor;
167 
168  /** getrusage() result as of the last reset(). */
169  struct rusage m_ru;
170 
171  /** gettimeofday() result as of the last reset(). */
172  struct timeval m_tv;
173 };
174 
175 #endif /* HAVE_SYS_TIME_H && HAVE_SYS_RESOURCE_H */
176 
177 #endif
#define timersub(a, b, r)
const string name("\ame\)
The interface to the operating system process and thread control primitives.
Definition: win32.h:60
#define RUSAGE_SELF
Definition: win32.h:40
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
struct timeval ru_stime
Definition: win32.h:64
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
int getrusage(int who, struct rusage *rusage)
Definition: win32.c:225
struct timeval ru_utime
Definition: win32.h:62
#define CHRONO_PRINT(type, tvp)
void ut_dbg_assertion_failed(const char *expr, const char *file, ulint line)
Report a failed assertion.
Definition: ut0dbg.cc:45