MySQL 8.0.37
Source Code Documentation
ut0dbg.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1994, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ut0dbg.h
29 Debug utilities for Innobase
30
31 Created 1/30/1994 Heikki Tuuri
32 **********************************************************************/
33
34#ifndef ut0dbg_h
35#define ut0dbg_h
36
37#include "my_compiler.h"
38
39/* Do not include univ.i because univ.i includes this. */
40
41#include <cstdio>
42#include <functional>
43
44/** Set a callback function to be called before exiting.
45@param[in] callback user callback function */
46void ut_set_assert_callback(std::function<void()> &callback);
47
48/** Report a failed assertion.
49@param[in] expr The failed assertion
50@param[in] file Source file containing the assertion
51@param[in] line Line number of the assertion */
52[[noreturn]] void ut_dbg_assertion_failed(const char *expr, const char *file,
53 uint64_t line);
54
55/** Abort execution if EXPR does not evaluate to nonzero.
56@param EXPR assertion expression that should hold */
57#define ut_a(EXPR) \
58 do { \
59 if (unlikely(false == (bool)(EXPR))) { \
60 ut_dbg_assertion_failed(#EXPR, __FILE__, __LINE__); \
61 } \
62 } while (0)
63
64/** Abort execution. */
65#define ut_error ut_dbg_assertion_failed(0, __FILE__, __LINE__)
66
67#ifdef UNIV_DEBUG
68/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
69#define ut_ad(EXPR) ut_a(EXPR)
70/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
71#define ut_d(EXPR) EXPR
72/** Opposite of ut_d(). Does nothing if UNIV_DEBUG is defined. */
73#define ut_o(EXPR)
74#else
75/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
76#define ut_ad(EXPR)
77/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
78#define ut_d(EXPR)
79/** Opposite of ut_d(). Does nothing if UNIV_DEBUG is defined. */
80#define ut_o(EXPR) EXPR
81#endif
82
83/** Debug crash point */
84#ifdef UNIV_DEBUG
85#define DBUG_INJECT_CRASH(prefix, count) \
86 do { \
87 char buf[64]; \
88 snprintf(buf, sizeof buf, prefix "_%u", count); \
89 DBUG_EXECUTE_IF(buf, DBUG_SUICIDE();); \
90 } while (0)
91
92#define DBUG_INJECT_CRASH_WITH_LOG_FLUSH(prefix, count) \
93 do { \
94 char buf[64]; \
95 snprintf(buf, sizeof buf, prefix "_%u", count); \
96 DBUG_EXECUTE_IF(buf, log_buffer_flush_to_disk(); DBUG_SUICIDE();); \
97 } while (0)
98
99#else
100#define DBUG_INJECT_CRASH(prefix, count)
101#define DBUG_INJECT_CRASH_WITH_LOG_FLUSH(prefix, count)
102#endif
103
104/** Silence warnings about an unused variable by doing a null assignment.
105@param A the unused variable */
106#define UT_NOT_USED(A) std::ignore = A
107
108#if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
109
110#define HAVE_UT_CHRONO_T
111
112#include <sys/resource.h>
113#include <sys/time.h>
114#include <sys/types.h>
115
116/** A "chronometer" used to clock snippets of code.
117Example usage:
118 ut_chrono_t ch("this loop");
119 for (;;) { ... }
120 ch.show();
121would print the timings of the for() loop, prefixed with "this loop:" */
123 public:
124 /** Constructor.
125 @param[in] name chrono's name, used when showing the values */
127 reset();
128 }
129
130 /** Resets the chrono (records the current time in it). */
131 void reset() {
132 gettimeofday(&m_tv, nullptr);
133
134 getrusage(RUSAGE_SELF, &m_ru);
135 }
136
137 /** Shows the time elapsed and usage statistics since the last reset. */
138 void show() {
139 struct rusage ru_now;
140 struct timeval tv_now;
141 struct timeval tv_diff;
142
143 getrusage(RUSAGE_SELF, &ru_now);
144
145 gettimeofday(&tv_now, nullptr);
146
147#ifndef timersub
148#define timersub(a, b, r) \
149 do { \
150 (r)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
151 (r)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
152 if ((r)->tv_usec < 0) { \
153 (r)->tv_sec--; \
154 (r)->tv_usec += 1000000; \
155 } \
156 } while (0)
157#endif /* timersub */
158
159#define CHRONO_PRINT(type, tvp) \
160 fprintf(stderr, "%s: %s% 5ld.%06ld sec\n", m_name, type, \
161 static_cast<long>((tvp)->tv_sec), static_cast<long>((tvp)->tv_usec))
162
163 timersub(&tv_now, &m_tv, &tv_diff);
164 CHRONO_PRINT("real", &tv_diff);
165
166 timersub(&ru_now.ru_utime, &m_ru.ru_utime, &tv_diff);
167 CHRONO_PRINT("user", &tv_diff);
168
169 timersub(&ru_now.ru_stime, &m_ru.ru_stime, &tv_diff);
170 CHRONO_PRINT("sys ", &tv_diff);
171 }
172
173 /** Cause the timings not to be printed from the destructor. */
174 void end() { m_show_from_destructor = false; }
175
176 /** Destructor. */
179 show();
180 }
181 }
182
183 private:
184 /** Name of this chronometer. */
185 const char *m_name;
186
187 /** True if the current timings should be printed by the destructor. */
189
190 /** getrusage() result as of the last reset(). */
191 struct rusage m_ru;
192
193 /** gettimeofday() result as of the last reset(). */
194 struct timeval m_tv;
195};
196
197#endif /* HAVE_SYS_TIME_H && HAVE_SYS_RESOURCE_H */
198
199#endif
A "chronometer" used to clock snippets of code.
Definition: ut0dbg.h:122
void show()
Shows the time elapsed and usage statistics since the last reset.
Definition: ut0dbg.h:138
bool m_show_from_destructor
True if the current timings should be printed by the destructor.
Definition: ut0dbg.h:188
struct rusage m_ru
getrusage() result as of the last reset().
Definition: ut0dbg.h:191
ut_chrono_t(const char *name)
Constructor.
Definition: ut0dbg.h:126
const char * m_name
Name of this chronometer.
Definition: ut0dbg.h:185
struct timeval m_tv
gettimeofday() result as of the last reset().
Definition: ut0dbg.h:194
void reset()
Resets the chrono (records the current time in it).
Definition: ut0dbg.h:131
void end()
Cause the timings not to be printed from the destructor.
Definition: ut0dbg.h:174
~ut_chrono_t()
Destructor.
Definition: ut0dbg.h:177
Header for compiler-dependent features.
Definition: os0file.h:86
case opt name
Definition: sslopt-case.h:33
Include file for Sun RPC to compile out of the box.
#define CHRONO_PRINT(type, tvp)
void ut_dbg_assertion_failed(const char *expr, const char *file, uint64_t line)
Report a failed assertion.
Definition: ut0dbg.cc:56
#define timersub(a, b, r)
void ut_set_assert_callback(std::function< void()> &callback)
Set a callback function to be called before exiting.
Definition: ut0dbg.cc:48