| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 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 |
/**************************************************/ /** |
| 29 |
@file include/log0write.h |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#ifndef log0write_h |
| 34 |
#define log0write_h |
| 35 |
|
| 36 |
#ifndef UNIV_HOTBACKUP |
| 37 |
|
| 38 |
/* std::memory_order_X */ |
| 39 |
#include <atomic> |
| 40 |
|
| 41 |
/* log.write_to_file_requests_interval */ |
| 42 |
#include "log0sys.h" |
| 43 |
|
| 44 |
/* log_t&, lsn_t */ |
| 45 |
#include "log0types.h" |
| 46 |
|
| 47 |
/* srv_threads */ |
| 48 |
#include "srv0srv.h" |
| 49 |
|
| 50 |
/**************************************************/ /** |
| 51 |
|
| 52 |
@name Log - waiting for redo written to disk. |
| 53 |
|
| 54 |
*******************************************************/ |
| 55 |
|
| 56 |
/** @{ */ |
| 57 |
|
| 58 |
/** Waits until the redo log is written up to a provided lsn. |
| 59 |
@param[in] log redo log |
| 60 |
@param[in] lsn lsn to wait for |
| 61 |
@param[in] flush_to_disk true: wait until it is flushed |
| 62 |
@return statistics about waiting inside */ |
| 63 |
Wait_stats log_write_up_to(log_t &log, lsn_t lsn, bool flush_to_disk); |
| 64 |
|
| 65 |
/** Total number of redo log flushes (fsyncs) that have been started since |
| 66 |
the redo log system (log_sys) became initialized (@see log_sys_init). |
| 67 |
@return total number of fsyncs or 0 if the redo log system is uninitialized */ |
| 68 |
uint64_t log_total_flushes(); |
| 69 |
|
| 70 |
/** Number of currently pending redo log flushes (fsyncs in-progress). |
| 71 |
@return number of pending fsyncs or 0 if the redo log system is uninitialized */ |
| 72 |
uint64_t log_pending_flushes(); |
| 73 |
|
| 74 |
/** Checks if the redo log writer exited extra margin. To minimize flipping |
| 75 |
of log.m_writer_inside_extra_margin, the check assumes the very pessimistic |
| 76 |
scenario in which a next write of the log_writer thread, would be executed |
| 77 |
up to the current lsn. |
| 78 |
|
| 79 |
Requirement: log.writer_mutex acquired and log.m_writer_inside_extra_margin |
| 80 |
is true, before calling this function. |
| 81 |
|
| 82 |
@remarks |
| 83 |
This method is supposed to be used by the log_checkpointer thread to detect |
| 84 |
situation in which the redo log writer has actually exited the extra_margin, |
| 85 |
because of advanced log.last_checkpoint_lsn, but the log_writer thread didn't |
| 86 |
notice it because it has not been active since then (e.g. because there is |
| 87 |
nothing more to write, ie. log.write_lsn == current lsn). |
| 88 |
|
| 89 |
@param[in,out] log redo log */ |
| 90 |
void log_writer_check_if_exited_extra_margin(log_t &log); |
| 91 |
|
| 92 |
/** @} */ |
| 93 |
|
| 94 |
/**************************************************/ /** |
| 95 |
|
| 96 |
@name Log - the log write threads. |
| 97 |
|
| 98 |
*******************************************************/ |
| 99 |
|
| 100 |
/** @{ */ |
| 101 |
|
| 102 |
/** Pause / resume the log writer, the log flusher, the log write notifier |
| 103 |
and the log flush notifier threads based on innodb_log_writer_threads value. |
| 104 |
@note Calls to this function should be protected externally by some mutex. |
| 105 |
The caller innodb_log_writer_threads_update() is protected |
| 106 |
by LOCK_global_system_variables in mysqld. */ |
| 107 |
void log_control_writer_threads(log_t &log); |
| 108 |
|
| 109 |
/** The log writer thread routine. |
| 110 |
@param[in,out] log_ptr pointer to redo log */ |
| 111 |
void log_writer(log_t *log_ptr); |
| 112 |
|
| 113 |
/** The log flusher thread routine. |
| 114 |
@param[in,out] log_ptr pointer to redo log */ |
| 115 |
void log_flusher(log_t *log_ptr); |
| 116 |
|
| 117 |
/** The log flush notifier thread routine. |
| 118 |
@param[in,out] log_ptr pointer to redo log */ |
| 119 |
void log_flush_notifier(log_t *log_ptr); |
| 120 |
|
| 121 |
/** The log write notifier thread routine. |
| 122 |
@param[in,out] log_ptr pointer to redo log */ |
| 123 |
void log_write_notifier(log_t *log_ptr); |
| 124 |
|
| 125 |
/** Validates that the log writer thread is active. |
| 126 |
Used only to assert, that the state is correct. */ |
| 127 |
void log_writer_thread_active_validate(); |
| 128 |
|
| 129 |
/** Validates that the log writer, flusher threads are active. |
| 130 |
Used only to assert, that the state is correct. |
| 131 |
@param[in] log redo log */ |
| 132 |
void log_background_write_threads_active_validate(const log_t &log); |
| 133 |
|
| 134 |
#define log_flusher_mutex_enter(log) mutex_enter(&((log).flusher_mutex)) |
| 135 |
|
| 136 |
#define log_flusher_mutex_enter_nowait(log) \ |
| 137 |
mutex_enter_nowait(&((log).flusher_mutex)) |
| 138 |
|
| 139 |
#define log_flusher_mutex_exit(log) mutex_exit(&((log).flusher_mutex)) |
| 140 |
|
| 141 |
#define log_flusher_mutex_own(log) \ |
| 142 |
(mutex_own(&((log).flusher_mutex)) || !log_flusher_is_active()) |
| 143 |
|
| 144 |
#define log_flush_notifier_mutex_enter(log) \ |
| 145 |
mutex_enter(&((log).flush_notifier_mutex)) |
| 146 |
|
| 147 |
#define log_flush_notifier_mutex_exit(log) \ |
| 148 |
mutex_exit(&((log).flush_notifier_mutex)) |
| 149 |
|
| 150 |
#define log_flush_notifier_mutex_own(log) \ |
| 151 |
(mutex_own(&((log).flush_notifier_mutex)) || !log_flush_notifier_is_active()) |
| 152 |
|
| 153 |
#define log_writer_mutex_enter(log) mutex_enter(&((log).writer_mutex)) |
| 154 |
|
| 155 |
#define log_writer_mutex_enter_nowait(log) \ |
| 156 |
mutex_enter_nowait(&((log).writer_mutex)) |
| 157 |
|
| 158 |
#define log_writer_mutex_exit(log) mutex_exit(&((log).writer_mutex)) |
| 159 |
|
| 160 |
#define log_writer_mutex_own(log) \ |
| 161 |
(mutex_own(&((log).writer_mutex)) || !log_writer_is_active()) |
| 162 |
|
| 163 |
#define log_write_notifier_mutex_enter(log) \ |
| 164 |
mutex_enter(&((log).write_notifier_mutex)) |
| 165 |
|
| 166 |
#define log_write_notifier_mutex_exit(log) \ |
| 167 |
mutex_exit(&((log).write_notifier_mutex)) |
| 168 |
|
| 169 |
#define log_write_notifier_mutex_own(log) \ |
| 170 |
(mutex_own(&((log).write_notifier_mutex)) || !log_write_notifier_is_active()) |
| 171 |
|
| 172 |
/** Checks if log writer thread is active. |
| 173 |
@return true if and only if the log writer thread is active */ |
| 174 |
inline bool log_writer_is_active() { |
| 175 |
return srv_thread_is_active(srv_threads.m_log_writer); |
| 176 |
} |
| 177 |
|
| 178 |
/** Checks if log write notifier thread is active. |
| 179 |
@return true if and only if the log write notifier thread is active */ |
| 180 |
inline bool log_write_notifier_is_active() { |
| 181 |
return srv_thread_is_active(srv_threads.m_log_write_notifier); |
| 182 |
} |
| 183 |
|
| 184 |
/** Checks if log flusher thread is active. |
| 185 |
@return true if and only if the log flusher thread is active */ |
| 186 |
inline bool log_flusher_is_active() { |
| 187 |
return srv_thread_is_active(srv_threads.m_log_flusher); |
| 188 |
} |
| 189 |
|
| 190 |
/** Checks if log flush notifier thread is active. |
| 191 |
@return true if and only if the log flush notifier thread is active */ |
| 192 |
inline bool log_flush_notifier_is_active() { |
| 193 |
return srv_thread_is_active(srv_threads.m_log_flush_notifier); |
| 194 |
} |
| 195 |
|
| 196 |
/** Checks if requests to write redo log buffer to disk are frequent |
| 197 |
(which means that there is at least one request per 1ms in average). |
| 198 |
@param[in] interval how often in average requests happen |
| 199 |
@return true iff requests are considered frequent */ |
| 200 |
inline bool log_write_to_file_requests_are_frequent( |
| 201 |
std::chrono::microseconds interval) { |
| 202 |
return interval < std::chrono::milliseconds{1}; |
| 203 |
} |
| 204 |
|
| 205 |
/** Checks if requests to write redo log buffer to disk are frequent |
| 206 |
(which means that there is at least one request per 1ms in average). |
| 207 |
@param[in] log redo log |
| 208 |
@return true iff requests are considered frequent */ |
| 209 |
inline bool log_write_to_file_requests_are_frequent(const log_t &log) { |
| 210 |
return log_write_to_file_requests_are_frequent( |
| 211 |
log.write_to_file_requests_interval.load(std::memory_order_relaxed)); |
| 212 |
} |
| 213 |
|
| 214 |
/** @} */ |
| 215 |
|
| 216 |
#else |
| 217 |
|
| 218 |
#define log_writer_mutex_own(log) true |
| 219 |
|
| 220 |
#endif /* UNIV_HOTBACKUP */ |
| 221 |
|
| 222 |
#endif /* !log0write_h */ |