| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 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 */ |
| 46 |
void 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. |
| 117 |
Example usage: |
| 118 |
ut_chrono_t ch("this loop"); |
| 119 |
for (;;) { ... } |
| 120 |
ch.show(); |
| 121 |
would print the timings of the for() loop, prefixed with "this loop:" */ |
| 122 |
class ut_chrono_t { |
| 123 |
public: |
| 124 |
/** Constructor. |
| 125 |
@param[in] name chrono's name, used when showing the values */ |
| 126 |
ut_chrono_t(const char *name) : m_name(name), m_show_from_destructor(true) { |
| 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. */ |
| 177 |
~ut_chrono_t() { |
| 178 |
if (m_show_from_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. */ |
| 188 |
bool m_show_from_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 |