| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted by |
| 7 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 8 |
briefly in the InnoDB documentation. The contributions by Google are |
| 9 |
incorporated with their permission, and subject to the conditions contained in |
| 10 |
the file COPYING.Google. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it under |
| 13 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 14 |
Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 27 |
for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License along with |
| 30 |
this program; if not, write to the Free Software Foundation, Inc., |
| 31 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
/** @file sync/sync0sync.cc |
| 36 |
Mutex, the basic synchronization primitive |
| 37 |
|
| 38 |
Created 9/5/1995 Heikki Tuuri |
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
#include "univ.i" |
| 42 |
|
| 43 |
#include "sync0rw.h" |
| 44 |
#include "sync0sync.h" |
| 45 |
|
| 46 |
#ifdef HAVE_PSI_INTERFACE |
| 47 |
/** To keep count of number of PS keys defined. */ |
| 48 |
unsigned int mysql_pfs_key_t::s_count = 0; |
| 49 |
#endif /* HAVE_PSI_INTERFACE */ |
| 50 |
|
| 51 |
#ifdef UNIV_PFS_MUTEX |
| 52 |
mysql_pfs_key_t alter_stage_mutex_key; |
| 53 |
/* Key to register autoinc_mutex with performance schema */ |
| 54 |
mysql_pfs_key_t autoinc_mutex_key; |
| 55 |
mysql_pfs_key_t autoinc_persisted_mutex_key; |
| 56 |
#ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK |
| 57 |
mysql_pfs_key_t buffer_block_mutex_key; |
| 58 |
#endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */ |
| 59 |
mysql_pfs_key_t buf_pool_chunks_mutex_key; |
| 60 |
mysql_pfs_key_t buf_pool_flush_state_mutex_key; |
| 61 |
mysql_pfs_key_t buf_pool_LRU_list_mutex_key; |
| 62 |
mysql_pfs_key_t buf_pool_free_list_mutex_key; |
| 63 |
mysql_pfs_key_t buf_pool_zip_free_mutex_key; |
| 64 |
mysql_pfs_key_t buf_pool_zip_hash_mutex_key; |
| 65 |
mysql_pfs_key_t buf_pool_zip_mutex_key; |
| 66 |
mysql_pfs_key_t ddl_autoinc_mutex_key; |
| 67 |
mysql_pfs_key_t dict_foreign_err_mutex_key; |
| 68 |
mysql_pfs_key_t dict_persist_dirty_tables_mutex_key; |
| 69 |
mysql_pfs_key_t dict_sys_mutex_key; |
| 70 |
mysql_pfs_key_t dict_table_mutex_key; |
| 71 |
mysql_pfs_key_t parser_mutex_key; |
| 72 |
mysql_pfs_key_t fil_system_mutex_key; |
| 73 |
mysql_pfs_key_t flush_list_mutex_key; |
| 74 |
mysql_pfs_key_t fts_bg_threads_mutex_key; |
| 75 |
mysql_pfs_key_t fts_delete_mutex_key; |
| 76 |
mysql_pfs_key_t fts_optimize_mutex_key; |
| 77 |
mysql_pfs_key_t fts_doc_id_mutex_key; |
| 78 |
mysql_pfs_key_t fts_pll_tokenize_mutex_key; |
| 79 |
mysql_pfs_key_t hash_table_mutex_key; |
| 80 |
mysql_pfs_key_t ibuf_bitmap_mutex_key; |
| 81 |
mysql_pfs_key_t ibuf_mutex_key; |
| 82 |
mysql_pfs_key_t ibuf_pessimistic_insert_mutex_key; |
| 83 |
mysql_pfs_key_t lock_free_hash_mutex_key; |
| 84 |
mysql_pfs_key_t log_checkpointer_mutex_key; |
| 85 |
mysql_pfs_key_t log_closer_mutex_key; |
| 86 |
mysql_pfs_key_t log_writer_mutex_key; |
| 87 |
mysql_pfs_key_t log_flusher_mutex_key; |
| 88 |
mysql_pfs_key_t log_write_notifier_mutex_key; |
| 89 |
mysql_pfs_key_t log_flush_notifier_mutex_key; |
| 90 |
mysql_pfs_key_t log_limits_mutex_key; |
| 91 |
mysql_pfs_key_t log_files_mutex_key; |
| 92 |
mysql_pfs_key_t log_cmdq_mutex_key; |
| 93 |
mysql_pfs_key_t log_sn_lock_key; |
| 94 |
mysql_pfs_key_t log_sn_mutex_key; |
| 95 |
mysql_pfs_key_t log_sys_arch_mutex_key; |
| 96 |
mysql_pfs_key_t page_sys_arch_mutex_key; |
| 97 |
mysql_pfs_key_t page_sys_arch_oper_mutex_key; |
| 98 |
mysql_pfs_key_t page_sys_arch_client_mutex_key; |
| 99 |
mysql_pfs_key_t mutex_list_mutex_key; |
| 100 |
mysql_pfs_key_t recalc_pool_mutex_key; |
| 101 |
mysql_pfs_key_t page_cleaner_mutex_key; |
| 102 |
mysql_pfs_key_t purge_sys_pq_mutex_key; |
| 103 |
mysql_pfs_key_t recv_sys_mutex_key; |
| 104 |
mysql_pfs_key_t recv_writer_mutex_key; |
| 105 |
mysql_pfs_key_t temp_space_rseg_mutex_key; |
| 106 |
mysql_pfs_key_t undo_space_rseg_mutex_key; |
| 107 |
mysql_pfs_key_t trx_sys_rseg_mutex_key; |
| 108 |
mysql_pfs_key_t page_zip_stat_per_index_mutex_key; |
| 109 |
#ifdef UNIV_DEBUG |
| 110 |
mysql_pfs_key_t rw_lock_debug_mutex_key; |
| 111 |
#endif /* UNIV_DEBUG */ |
| 112 |
mysql_pfs_key_t rtr_active_mutex_key; |
| 113 |
mysql_pfs_key_t rtr_match_mutex_key; |
| 114 |
mysql_pfs_key_t rtr_path_mutex_key; |
| 115 |
mysql_pfs_key_t rtr_ssn_mutex_key; |
| 116 |
mysql_pfs_key_t rw_lock_list_mutex_key; |
| 117 |
mysql_pfs_key_t rw_lock_mutex_key; |
| 118 |
mysql_pfs_key_t srv_innodb_monitor_mutex_key; |
| 119 |
mysql_pfs_key_t srv_misc_tmpfile_mutex_key; |
| 120 |
mysql_pfs_key_t srv_monitor_file_mutex_key; |
| 121 |
#ifdef UNIV_DEBUG |
| 122 |
mysql_pfs_key_t sync_thread_mutex_key; |
| 123 |
#endif /* UNIV_DEBUG */ |
| 124 |
mysql_pfs_key_t trx_undo_mutex_key; |
| 125 |
mysql_pfs_key_t trx_mutex_key; |
| 126 |
mysql_pfs_key_t trx_pool_mutex_key; |
| 127 |
mysql_pfs_key_t trx_pool_manager_mutex_key; |
| 128 |
mysql_pfs_key_t temp_pool_manager_mutex_key; |
| 129 |
mysql_pfs_key_t lock_sys_table_mutex_key; |
| 130 |
mysql_pfs_key_t lock_sys_page_mutex_key; |
| 131 |
mysql_pfs_key_t lock_wait_mutex_key; |
| 132 |
mysql_pfs_key_t trx_sys_mutex_key; |
| 133 |
mysql_pfs_key_t trx_sys_shard_mutex_key; |
| 134 |
mysql_pfs_key_t trx_sys_serialisation_mutex_key; |
| 135 |
mysql_pfs_key_t srv_sys_mutex_key; |
| 136 |
mysql_pfs_key_t srv_threads_mutex_key; |
| 137 |
#ifndef PFS_SKIP_EVENT_MUTEX |
| 138 |
mysql_pfs_key_t event_mutex_key; |
| 139 |
mysql_pfs_key_t event_manager_mutex_key; |
| 140 |
#endif /* !PFS_SKIP_EVENT_MUTEX */ |
| 141 |
mysql_pfs_key_t sync_array_mutex_key; |
| 142 |
mysql_pfs_key_t zip_pad_mutex_key; |
| 143 |
mysql_pfs_key_t row_drop_list_mutex_key; |
| 144 |
mysql_pfs_key_t file_open_mutex_key; |
| 145 |
mysql_pfs_key_t master_key_id_mutex_key; |
| 146 |
mysql_pfs_key_t clone_sys_mutex_key; |
| 147 |
mysql_pfs_key_t clone_task_mutex_key; |
| 148 |
mysql_pfs_key_t clone_snapshot_mutex_key; |
| 149 |
mysql_pfs_key_t parallel_read_mutex_key; |
| 150 |
mysql_pfs_key_t dblwr_mutex_key; |
| 151 |
mysql_pfs_key_t ahi_enabled_mutex_key; |
| 152 |
|
| 153 |
#endif /* UNIV_PFS_MUTEX */ |
| 154 |
|
| 155 |
#ifdef UNIV_PFS_RWLOCK |
| 156 |
mysql_pfs_key_t btr_search_latch_key; |
| 157 |
#ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK |
| 158 |
mysql_pfs_key_t buf_block_lock_key; |
| 159 |
#endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */ |
| 160 |
#ifdef UNIV_DEBUG |
| 161 |
mysql_pfs_key_t buf_block_debug_latch_key; |
| 162 |
#endif /* UNIV_DEBUG */ |
| 163 |
mysql_pfs_key_t undo_spaces_lock_key; |
| 164 |
mysql_pfs_key_t rsegs_lock_key; |
| 165 |
mysql_pfs_key_t lock_sys_global_rw_lock_key; |
| 166 |
mysql_pfs_key_t dict_operation_lock_key; |
| 167 |
mysql_pfs_key_t dict_table_stats_key; |
| 168 |
mysql_pfs_key_t hash_table_locks_key; |
| 169 |
mysql_pfs_key_t index_tree_rw_lock_key; |
| 170 |
mysql_pfs_key_t index_online_log_key; |
| 171 |
mysql_pfs_key_t fil_space_latch_key; |
| 172 |
mysql_pfs_key_t fts_cache_rw_lock_key; |
| 173 |
mysql_pfs_key_t fts_cache_init_rw_lock_key; |
| 174 |
mysql_pfs_key_t trx_i_s_cache_lock_key; |
| 175 |
mysql_pfs_key_t trx_purge_latch_key; |
| 176 |
#endif /* UNIV_PFS_RWLOCK */ |
| 177 |
|
| 178 |
/* There are mutexes/rwlocks that we want to exclude from instrumentation |
| 179 |
even if their corresponding performance schema define is set. And this |
| 180 |
PFS_NOT_INSTRUMENTED is used as the key value to identify those objects that |
| 181 |
would be excluded from instrumentation.*/ |
| 182 |
mysql_pfs_key_t PFS_NOT_INSTRUMENTED(0); |
| 183 |
|
| 184 |
/** For monitoring active mutexes */ |
| 185 |
MutexMonitor *mutex_monitor; |
| 186 |
|
| 187 |
/** |
| 188 |
Prints wait info of the sync system. |
| 189 |
Note: The instrumental counters are deprecated |
| 190 |
and prints all 0 for compatibility. |
| 191 |
@param file - where to print */ |
| 192 |
static void sync_print_wait_info(FILE *file) { |
| 193 |
fprintf(file, |
| 194 |
"RW-shared spins 0, rounds 0, OS waits 0\n" |
| 195 |
"RW-excl spins 0, rounds 0, OS waits 0\n" |
| 196 |
"RW-sx spins 0, rounds 0, OS waits 0\n"); |
| 197 |
|
| 198 |
fprintf(file, |
| 199 |
"Spin rounds per wait: 0.00 RW-shared," |
| 200 |
" 0.00 RW-excl, 0.00 RW-sx\n"); |
| 201 |
} |
| 202 |
|
| 203 |
/** Prints info of the sync system. |
| 204 |
@param[in] file where to print */ |
| 205 |
void sync_print(FILE *file) { |
| 206 |
#ifdef UNIV_DEBUG |
| 207 |
rw_lock_list_print_info(file); |
| 208 |
#endif /* UNIV_DEBUG */ |
| 209 |
|
| 210 |
sync_array_print(file); |
| 211 |
|
| 212 |
sync_print_wait_info(file); |
| 213 |
} |
| 214 |
|
| 215 |
/** Print the filename "basename" e.g., p = "/a/b/c/d/e.cc" -> p = "e.cc" |
| 216 |
@param[in] filename Name from where to extract the basename |
| 217 |
@return the basename */ |
| 218 |
const char *sync_basename(const char *filename) { |
| 219 |
const char *ptr = filename + strlen(filename) - 1; |
| 220 |
|
| 221 |
while (ptr > filename && *ptr != '/' && *ptr != '\\') { |
| 222 |
--ptr; |
| 223 |
} |
| 224 |
|
| 225 |
++ptr; |
| 226 |
|
| 227 |
return (ptr); |
| 228 |
} |
| 229 |
|
| 230 |
/** String representation of the filename and line number where the |
| 231 |
latch was created |
| 232 |
@param[in] id Latch ID |
| 233 |
@param[in] created Filename and line number where it was created |
| 234 |
@return the string representation */ |
| 235 |
std::string sync_mutex_to_string(latch_id_t id, const std::string &created) { |
| 236 |
std::ostringstream msg; |
| 237 |
|
| 238 |
msg << "Mutex " << sync_latch_get_name(id) << " " |
| 239 |
<< "created " << created; |
| 240 |
|
| 241 |
return (msg.str()); |
| 242 |
} |
| 243 |
|
| 244 |
/** Enable the mutex monitoring */ |
| 245 |
void MutexMonitor::enable() { |
| 246 |
/** Note: We don't add any latch meta-data after startup. Therefore |
| 247 |
there is no need to use a mutex here. */ |
| 248 |
|
| 249 |
LatchMetaData::iterator end = latch_meta.end(); |
| 250 |
|
| 251 |
for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) { |
| 252 |
if (*it != NULL) { |
| 253 |
(*it)->get_counter()->enable(); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** Disable the mutex monitoring */ |
| 259 |
void MutexMonitor::disable() { |
| 260 |
/** Note: We don't add any latch meta-data after startup. Therefore |
| 261 |
there is no need to use a mutex here. */ |
| 262 |
|
| 263 |
LatchMetaData::iterator end = latch_meta.end(); |
| 264 |
|
| 265 |
for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) { |
| 266 |
if (*it != NULL) { |
| 267 |
(*it)->get_counter()->disable(); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** Reset the mutex monitoring counters */ |
| 273 |
void MutexMonitor::reset() { |
| 274 |
/** Note: We don't add any latch meta-data after startup. Therefore |
| 275 |
there is no need to use a mutex here. */ |
| 276 |
|
| 277 |
LatchMetaData::iterator end = latch_meta.end(); |
| 278 |
|
| 279 |
for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) { |
| 280 |
if (*it != NULL) { |
| 281 |
(*it)->get_counter()->reset(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
mutex_enter(&rw_lock_list_mutex); |
| 286 |
|
| 287 |
for (auto rw_lock : rw_lock_list) { |
| 288 |
rw_lock->count_os_wait = 0; |
| 289 |
} |
| 290 |
|
| 291 |
mutex_exit(&rw_lock_list_mutex); |
| 292 |
} |