1 /*****************************************************************************
2
3 Copyright (c) 1995, 2021, 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
13 it under the terms of the GNU General Public License, version 2.0,
14 as published by the Free Software Foundation.
15
16 This program is also distributed 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 included with MySQL.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License, version 2.0, for more details.
27
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
31
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 #include "sync0rw.h"
43 #include "sync0sync.h"
44
45 /** Keeps count of number of Performance Schema keys defined. */
46 unsigned int mysql_pfs_key_t::s_count;
47
48 #ifdef UNIV_PFS_MUTEX
49 /* Key to register autoinc_mutex with performance schema */
50 mysql_pfs_key_t autoinc_mutex_key;
51 #ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK
52 mysql_pfs_key_t buffer_block_mutex_key;
53 #endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */
54 mysql_pfs_key_t buf_pool_chunks_mutex_key;
55 mysql_pfs_key_t buf_pool_flush_state_mutex_key;
56 mysql_pfs_key_t buf_pool_LRU_list_mutex_key;
57 mysql_pfs_key_t buf_pool_free_list_mutex_key;
58 mysql_pfs_key_t buf_pool_zip_free_mutex_key;
59 mysql_pfs_key_t buf_pool_zip_hash_mutex_key;
60 mysql_pfs_key_t buf_pool_zip_mutex_key;
61 mysql_pfs_key_t cache_last_read_mutex_key;
62 mysql_pfs_key_t dict_foreign_err_mutex_key;
63 mysql_pfs_key_t dict_sys_mutex_key;
64 mysql_pfs_key_t file_format_max_mutex_key;
65 mysql_pfs_key_t fil_system_mutex_key;
66 mysql_pfs_key_t flush_list_mutex_key;
67 mysql_pfs_key_t fts_bg_threads_mutex_key;
68 mysql_pfs_key_t fts_delete_mutex_key;
69 mysql_pfs_key_t fts_optimize_mutex_key;
70 mysql_pfs_key_t fts_doc_id_mutex_key;
71 mysql_pfs_key_t fts_pll_tokenize_mutex_key;
72 mysql_pfs_key_t hash_table_mutex_key;
73 mysql_pfs_key_t ibuf_bitmap_mutex_key;
74 mysql_pfs_key_t ibuf_mutex_key;
75 mysql_pfs_key_t ibuf_pessimistic_insert_mutex_key;
76 mysql_pfs_key_t log_sys_mutex_key;
77 mysql_pfs_key_t log_sys_write_mutex_key;
78 mysql_pfs_key_t log_cmdq_mutex_key;
79 mysql_pfs_key_t log_flush_order_mutex_key;
80 mysql_pfs_key_t mutex_list_mutex_key;
81 mysql_pfs_key_t recalc_pool_mutex_key;
82 mysql_pfs_key_t page_cleaner_mutex_key;
83 mysql_pfs_key_t purge_sys_pq_mutex_key;
84 mysql_pfs_key_t recv_sys_mutex_key;
85 mysql_pfs_key_t redo_rseg_mutex_key;
86 mysql_pfs_key_t noredo_rseg_mutex_key;
87 mysql_pfs_key_t page_zip_stat_per_index_mutex_key;
88 # ifdef UNIV_DEBUG
89 mysql_pfs_key_t rw_lock_debug_mutex_key;
90 # endif /* UNIV_DEBUG */
91 mysql_pfs_key_t rtr_active_mutex_key;
92 mysql_pfs_key_t rtr_match_mutex_key;
93 mysql_pfs_key_t rtr_path_mutex_key;
94 mysql_pfs_key_t rtr_ssn_mutex_key;
95 mysql_pfs_key_t rw_lock_list_mutex_key;
96 mysql_pfs_key_t rw_lock_mutex_key;
97 mysql_pfs_key_t srv_dict_tmpfile_mutex_key;
98 mysql_pfs_key_t srv_innodb_monitor_mutex_key;
99 mysql_pfs_key_t srv_misc_tmpfile_mutex_key;
100 mysql_pfs_key_t srv_monitor_file_mutex_key;
101 # ifdef UNIV_DEBUG
102 mysql_pfs_key_t sync_thread_mutex_key;
103 # endif /* UNIV_DEBUG */
104 mysql_pfs_key_t buf_dblwr_mutex_key;
105 mysql_pfs_key_t trx_undo_mutex_key;
106 mysql_pfs_key_t trx_mutex_key;
107 mysql_pfs_key_t trx_pool_mutex_key;
108 mysql_pfs_key_t trx_pool_manager_mutex_key;
109 mysql_pfs_key_t lock_mutex_key;
110 mysql_pfs_key_t lock_wait_mutex_key;
111 mysql_pfs_key_t trx_sys_mutex_key;
112 mysql_pfs_key_t srv_sys_mutex_key;
113 mysql_pfs_key_t srv_threads_mutex_key;
114 # ifndef PFS_SKIP_EVENT_MUTEX
115 mysql_pfs_key_t event_mutex_key;
116 mysql_pfs_key_t event_manager_mutex_key;
117 # endif /* !PFS_SKIP_EVENT_MUTEX */
118 mysql_pfs_key_t sync_array_mutex_key;
119 mysql_pfs_key_t thread_mutex_key;
120 mysql_pfs_key_t zip_pad_mutex_key;
121 mysql_pfs_key_t row_drop_list_mutex_key;
122 mysql_pfs_key_t master_key_id_mutex_key;
123 mysql_pfs_key_t analyze_index_mutex_key;
124
125 #endif /* UNIV_PFS_MUTEX */
126
127 #ifdef UNIV_PFS_RWLOCK
128 mysql_pfs_key_t btr_search_latch_key;
129 # ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK
130 mysql_pfs_key_t buf_block_lock_key;
131 # endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */
132 # ifdef UNIV_DEBUG
133 mysql_pfs_key_t buf_block_debug_latch_key;
134 # endif /* UNIV_DEBUG */
135 mysql_pfs_key_t checkpoint_lock_key;
136 mysql_pfs_key_t dict_operation_lock_key;
137 mysql_pfs_key_t dict_table_stats_key;
138 mysql_pfs_key_t hash_table_locks_key;
139 mysql_pfs_key_t index_tree_rw_lock_key;
140 mysql_pfs_key_t index_online_log_key;
141 mysql_pfs_key_t fil_space_latch_key;
142 mysql_pfs_key_t fts_cache_rw_lock_key;
143 mysql_pfs_key_t fts_cache_init_rw_lock_key;
144 mysql_pfs_key_t trx_i_s_cache_lock_key;
145 mysql_pfs_key_t trx_purge_latch_key;
146 #endif /* UNIV_PFS_RWLOCK */
147
148 /* There are mutexes/rwlocks that we want to exclude from instrumentation
149 even if their corresponding performance schema define is set. And this
150 PFS_NOT_INSTRUMENTED is used as the key value to identify those objects that
151 would be excluded from instrumentation.*/
152 mysql_pfs_key_t PFS_NOT_INSTRUMENTED(ULINT32_UNDEFINED);
153
154 /** For monitoring active mutexes */
155 MutexMonitor* mutex_monitor;
156
157 /**
158 Prints wait info of the sync system.
159 @param file - where to print */
160 static
161 void
sync_print_wait_info(FILE * file)162 sync_print_wait_info(FILE* file)
163 {
164 // Sum counter values once
165 int64_t rw_s_spin_wait_count_val
166 = static_cast<int64_t>(rw_lock_stats.rw_s_spin_wait_count);
167 int64_t rw_s_spin_round_count_val
168 = static_cast<int64_t>(rw_lock_stats.rw_s_spin_round_count);
169 int64_t rw_s_os_wait_count_val
170 = static_cast<int64_t>(rw_lock_stats.rw_s_os_wait_count);
171 int64_t rw_x_spin_wait_count_val
172 = static_cast<int64_t>(rw_lock_stats.rw_x_spin_wait_count);
173 int64_t rw_x_spin_round_count_val
174 = static_cast<int64_t>(rw_lock_stats.rw_x_spin_round_count);
175 int64_t rw_x_os_wait_count_val
176 = static_cast<int64_t>(rw_lock_stats.rw_x_os_wait_count);
177 int64_t rw_sx_spin_wait_count_val
178 = static_cast<int64_t>(rw_lock_stats.rw_sx_spin_wait_count);
179 int64_t rw_sx_spin_round_count_val
180 = static_cast<int64_t>(rw_lock_stats.rw_sx_spin_round_count);
181 int64_t rw_sx_os_wait_count_val
182 = static_cast<int64_t>(rw_lock_stats.rw_sx_os_wait_count);
183
184 fprintf(file,
185 "RW-shared spins " INT64PF ", rounds " INT64PF ","
186 " OS waits " INT64PF "\n"
187 "RW-excl spins " INT64PF ", rounds " INT64PF ","
188 " OS waits " INT64PF "\n"
189 "RW-sx spins " INT64PF ", rounds " INT64PF ","
190 " OS waits " INT64PF "\n",
191 rw_s_spin_wait_count_val, rw_s_spin_round_count_val,
192 rw_s_os_wait_count_val,
193 rw_x_spin_wait_count_val, rw_x_spin_round_count_val,
194 rw_x_os_wait_count_val,
195 rw_sx_spin_wait_count_val, rw_sx_spin_round_count_val,
196 rw_sx_os_wait_count_val);
197
198 fprintf(file,
199 "Spin rounds per wait: %.2f RW-shared,"
200 " %.2f RW-excl, %.2f RW-sx\n",
201 (double) rw_s_spin_round_count_val /
202 (rw_s_spin_wait_count_val ? rw_s_spin_wait_count_val : 1),
203 (double) rw_x_spin_round_count_val /
204 (rw_x_spin_wait_count_val ? rw_x_spin_wait_count_val : 1),
205 (double) rw_sx_spin_round_count_val /
206 (rw_sx_spin_wait_count_val ? rw_sx_spin_wait_count_val : 1));
207 }
208
209 /**
210 Prints info of the sync system.
211 @param file - where to print */
212 void
sync_print(FILE * file)213 sync_print(FILE* file)
214 {
215 #ifdef UNIV_DEBUG
216 rw_lock_list_print_info(file);
217 #endif /* UNIV_DEBUG */
218
219 sync_array_print(file);
220
221 sync_print_wait_info(file);
222 }
223
224 /** Print the filename "basename" e.g., p = "/a/b/c/d/e.cc" -> p = "e.cc"
225 @param[in] filename Name from where to extract the basename
226 @return the basename */
227 const char*
sync_basename(const char * filename)228 sync_basename(const char* filename)
229 {
230 const char* ptr = filename + strlen(filename) - 1;
231
232 while (ptr > filename && *ptr != '/' && *ptr != '\\') {
233 --ptr;
234 }
235
236 ++ptr;
237
238 return(ptr);
239 }
240
241 /** String representation of the filename and line number where the
242 latch was created
243 @param[in] id Latch ID
244 @param[in] created Filename and line number where it was crated
245 @return the string representation */
246 std::string
sync_mutex_to_string(latch_id_t id,const std::string & created)247 sync_mutex_to_string(
248 latch_id_t id,
249 const std::string& created)
250 {
251 std::ostringstream msg;
252
253 msg << "Mutex " << sync_latch_get_name(id) << " "
254 << "created " << created;
255
256 return(msg.str());
257 }
258
259 /** Enable the mutex monitoring */
260 void
enable()261 MutexMonitor::enable()
262 {
263 /** Note: We don't add any latch meta-data after startup. Therefore
264 there is no need to use a mutex here. */
265
266 LatchMetaData::iterator end = latch_meta.end();
267
268 for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
269
270 if (*it != NULL) {
271 (*it)->get_counter()->enable();
272 }
273 }
274 }
275
276 /** Disable the mutex monitoring */
277 void
disable()278 MutexMonitor::disable()
279 {
280 /** Note: We don't add any latch meta-data after startup. Therefore
281 there is no need to use a mutex here. */
282
283 LatchMetaData::iterator end = latch_meta.end();
284
285 for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
286
287 if (*it != NULL) {
288 (*it)->get_counter()->disable();
289 }
290 }
291 }
292
293 /** Reset the mutex monitoring counters */
294 void
reset()295 MutexMonitor::reset()
296 {
297 /** Note: We don't add any latch meta-data after startup. Therefore
298 there is no need to use a mutex here. */
299
300 LatchMetaData::iterator end = latch_meta.end();
301
302 for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
303
304 if (*it != NULL) {
305 (*it)->get_counter()->reset();
306 }
307 }
308
309 mutex_enter(&rw_lock_list_mutex);
310
311 for (rw_lock_t* rw_lock = UT_LIST_GET_FIRST(rw_lock_list);
312 rw_lock != NULL;
313 rw_lock = UT_LIST_GET_NEXT(list, rw_lock)) {
314
315 rw_lock->count_os_wait = 0;
316 }
317
318 mutex_exit(&rw_lock_list_mutex);
319 }
320