1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2008, Google Inc.
5 Copyright (c) 2020, MariaDB Corporation.
6 
7 Portions of this file contain modifications contributed and copyrighted by
8 Google, Inc. Those modifications are gratefully acknowledged and are described
9 briefly in the InnoDB documentation. The contributions by Google are
10 incorporated with their permission, and subject to the conditions contained in
11 the file COPYING.Google.
12 
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; version 2 of the License.
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 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file sync/sync0sync.cc
29 Mutex, the basic synchronization primitive
30 
31 Created 9/5/1995 Heikki Tuuri
32 *******************************************************/
33 
34 #include "sync0rw.h"
35 #include "sync0sync.h"
36 
37 #ifdef UNIV_PFS_MUTEX
38 /* Key to register autoinc_mutex with performance schema */
39 mysql_pfs_key_t	autoinc_mutex_key;
40 mysql_pfs_key_t	buffer_block_mutex_key;
41 mysql_pfs_key_t	buf_pool_mutex_key;
42 mysql_pfs_key_t	buf_pool_zip_mutex_key;
43 mysql_pfs_key_t	cache_last_read_mutex_key;
44 mysql_pfs_key_t	dict_foreign_err_mutex_key;
45 mysql_pfs_key_t	dict_sys_mutex_key;
46 mysql_pfs_key_t	fil_system_mutex_key;
47 mysql_pfs_key_t	flush_list_mutex_key;
48 mysql_pfs_key_t	fts_delete_mutex_key;
49 mysql_pfs_key_t	fts_doc_id_mutex_key;
50 mysql_pfs_key_t	fts_pll_tokenize_mutex_key;
51 mysql_pfs_key_t	hash_table_mutex_key;
52 mysql_pfs_key_t	ibuf_bitmap_mutex_key;
53 mysql_pfs_key_t	ibuf_mutex_key;
54 mysql_pfs_key_t	ibuf_pessimistic_insert_mutex_key;
55 mysql_pfs_key_t	log_sys_mutex_key;
56 mysql_pfs_key_t	log_sys_write_mutex_key;
57 mysql_pfs_key_t	log_cmdq_mutex_key;
58 mysql_pfs_key_t	log_flush_order_mutex_key;
59 mysql_pfs_key_t	mutex_list_mutex_key;
60 mysql_pfs_key_t	recalc_pool_mutex_key;
61 mysql_pfs_key_t	page_cleaner_mutex_key;
62 mysql_pfs_key_t	purge_sys_pq_mutex_key;
63 mysql_pfs_key_t	recv_sys_mutex_key;
64 mysql_pfs_key_t	recv_writer_mutex_key;
65 mysql_pfs_key_t	redo_rseg_mutex_key;
66 mysql_pfs_key_t	noredo_rseg_mutex_key;
67 mysql_pfs_key_t page_zip_stat_per_index_mutex_key;
68 # ifdef UNIV_DEBUG
69 mysql_pfs_key_t	rw_lock_debug_mutex_key;
70 # endif /* UNIV_DEBUG */
71 mysql_pfs_key_t rtr_active_mutex_key;
72 mysql_pfs_key_t	rtr_match_mutex_key;
73 mysql_pfs_key_t	rtr_path_mutex_key;
74 mysql_pfs_key_t	rw_lock_list_mutex_key;
75 mysql_pfs_key_t	rw_lock_mutex_key;
76 mysql_pfs_key_t	srv_innodb_monitor_mutex_key;
77 mysql_pfs_key_t	srv_misc_tmpfile_mutex_key;
78 mysql_pfs_key_t	srv_monitor_file_mutex_key;
79 mysql_pfs_key_t	buf_dblwr_mutex_key;
80 mysql_pfs_key_t	trx_mutex_key;
81 mysql_pfs_key_t	trx_pool_mutex_key;
82 mysql_pfs_key_t	trx_pool_manager_mutex_key;
83 mysql_pfs_key_t	lock_mutex_key;
84 mysql_pfs_key_t	lock_wait_mutex_key;
85 mysql_pfs_key_t	trx_sys_mutex_key;
86 mysql_pfs_key_t	srv_sys_mutex_key;
87 mysql_pfs_key_t	srv_threads_mutex_key;
88 mysql_pfs_key_t	event_mutex_key;
89 mysql_pfs_key_t	event_manager_mutex_key;
90 mysql_pfs_key_t	sync_array_mutex_key;
91 mysql_pfs_key_t	thread_mutex_key;
92 mysql_pfs_key_t zip_pad_mutex_key;
93 mysql_pfs_key_t row_drop_list_mutex_key;
94 mysql_pfs_key_t	rw_trx_hash_element_mutex_key;
95 #endif /* UNIV_PFS_MUTEX */
96 #ifdef UNIV_PFS_RWLOCK
97 mysql_pfs_key_t	btr_search_latch_key;
98 mysql_pfs_key_t	buf_block_lock_key;
99 # ifdef UNIV_DEBUG
100 mysql_pfs_key_t	buf_block_debug_latch_key;
101 # endif /* UNIV_DEBUG */
102 mysql_pfs_key_t	checkpoint_lock_key;
103 mysql_pfs_key_t	dict_operation_lock_key;
104 mysql_pfs_key_t	hash_table_locks_key;
105 mysql_pfs_key_t	index_tree_rw_lock_key;
106 mysql_pfs_key_t	index_online_log_key;
107 mysql_pfs_key_t	fil_space_latch_key;
108 mysql_pfs_key_t	fts_cache_rw_lock_key;
109 mysql_pfs_key_t	fts_cache_init_rw_lock_key;
110 mysql_pfs_key_t trx_i_s_cache_lock_key;
111 mysql_pfs_key_t	trx_purge_latch_key;
112 #endif /* UNIV_PFS_RWLOCK */
113 
114 /** For monitoring active mutexes */
115 MutexMonitor	mutex_monitor;
116 
117 /**
118 Prints wait info of the sync system.
119 @param file - where to print */
120 static
121 void
sync_print_wait_info(FILE * file)122 sync_print_wait_info(FILE* file)
123 {
124 	fprintf(file,
125 		"RW-shared spins " UINT64PF ", rounds " UINT64PF ","
126 		" OS waits " UINT64PF "\n"
127 		"RW-excl spins " UINT64PF ", rounds " UINT64PF ","
128 		" OS waits " UINT64PF "\n"
129 		"RW-sx spins " UINT64PF ", rounds " UINT64PF ","
130 		" OS waits " UINT64PF "\n",
131 		(ib_uint64_t) rw_lock_stats.rw_s_spin_wait_count,
132 		(ib_uint64_t) rw_lock_stats.rw_s_spin_round_count,
133 		(ib_uint64_t) rw_lock_stats.rw_s_os_wait_count,
134 		(ib_uint64_t) rw_lock_stats.rw_x_spin_wait_count,
135 		(ib_uint64_t) rw_lock_stats.rw_x_spin_round_count,
136 		(ib_uint64_t) rw_lock_stats.rw_x_os_wait_count,
137 		(ib_uint64_t) rw_lock_stats.rw_sx_spin_wait_count,
138 		(ib_uint64_t) rw_lock_stats.rw_sx_spin_round_count,
139 		(ib_uint64_t) rw_lock_stats.rw_sx_os_wait_count);
140 
141 	fprintf(file,
142 		"Spin rounds per wait: %.2f RW-shared,"
143 		" %.2f RW-excl, %.2f RW-sx\n",
144 		(double) rw_lock_stats.rw_s_spin_round_count /
145 		(rw_lock_stats.rw_s_spin_wait_count
146 		 ? rw_lock_stats.rw_s_spin_wait_count : 1LL),
147 		(double) rw_lock_stats.rw_x_spin_round_count /
148 		(rw_lock_stats.rw_x_spin_wait_count
149 		 ? rw_lock_stats.rw_x_spin_wait_count : 1LL),
150 		(double) rw_lock_stats.rw_sx_spin_round_count /
151 		(rw_lock_stats.rw_sx_spin_wait_count
152 		 ? rw_lock_stats.rw_sx_spin_wait_count : 1LL));
153 }
154 
155 /**
156 Prints info of the sync system.
157 @param file - where to print */
158 void
sync_print(FILE * file)159 sync_print(FILE* file)
160 {
161 #ifdef UNIV_DEBUG
162 	rw_lock_list_print_info(file);
163 #endif /* UNIV_DEBUG */
164 
165 	sync_array_print(file);
166 
167 	sync_print_wait_info(file);
168 }
169 
170 /** Print the filename "basename" e.g., p = "/a/b/c/d/e.cc" -> p = "e.cc"
171 @param[in]	filename	Name from where to extract the basename
172 @return the basename */
173 const char*
sync_basename(const char * filename)174 sync_basename(const char* filename)
175 {
176 	const char*	ptr = filename + strlen(filename) - 1;
177 
178 	while (ptr > filename && *ptr != '/' && *ptr != '\\') {
179 		--ptr;
180 	}
181 
182 	++ptr;
183 
184 	return(ptr);
185 }
186 
187 /** String representation of the filename and line number where the
188 latch was created
189 @param[in]	id		Latch ID
190 @param[in]	created		Filename and line number where it was crated
191 @return the string representation */
192 std::string
sync_mutex_to_string(latch_id_t id,const std::string & created)193 sync_mutex_to_string(
194 	latch_id_t		id,
195 	const std::string&	created)
196 {
197 	std::ostringstream msg;
198 
199 	msg << "Mutex " << sync_latch_get_name(id) << " "
200 	    << "created " << created;
201 
202 	return(msg.str());
203 }
204 
205 /** Enable the mutex monitoring */
206 void
enable()207 MutexMonitor::enable()
208 {
209 	/** Note: We don't add any latch meta-data after startup. Therefore
210 	there is no need to use a mutex here. */
211 
212 	LatchMetaData::iterator	end = latch_meta.end();
213 
214 	for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
215 
216 		if (*it != NULL) {
217 			(*it)->get_counter()->enable();
218 		}
219 	}
220 }
221 
222 /** Disable the mutex monitoring */
223 void
disable()224 MutexMonitor::disable()
225 {
226 	/** Note: We don't add any latch meta-data after startup. Therefore
227 	there is no need to use a mutex here. */
228 
229 	LatchMetaData::iterator	end = latch_meta.end();
230 
231 	for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
232 
233 		if (*it != NULL) {
234 			(*it)->get_counter()->disable();
235 		}
236 	}
237 }
238 
239 /** Reset the mutex monitoring counters */
240 void
reset()241 MutexMonitor::reset()
242 {
243 	/** Note: We don't add any latch meta-data after startup. Therefore
244 	there is no need to use a mutex here. */
245 
246 	LatchMetaData::iterator	end = latch_meta.end();
247 
248 	for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
249 
250 		if (*it != NULL) {
251 			(*it)->get_counter()->reset();
252 		}
253 	}
254 
255 	mutex_enter(&rw_lock_list_mutex);
256 
257 	for (rw_lock_t* rw_lock = UT_LIST_GET_FIRST(rw_lock_list);
258 	     rw_lock != NULL;
259 	     rw_lock = UT_LIST_GET_NEXT(list, rw_lock)) {
260 
261 		rw_lock->count_os_wait = 0;
262 	}
263 
264 	mutex_exit(&rw_lock_list_mutex);
265 }
266