Debian Codegraph @

storage/innobase/lock/lock0iter.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2007, 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 lock/lock0iter.cc
29 Lock queue iterator. Can iterate over table and record
30 lock queues.
31
32 Created July 16, 2007 Vasil Dimov
33 *******************************************************/
34
35 #define LOCK_MODULE_IMPLEMENTATION
36
37 #include "lock0iter.h"
38
39 #include "dict0dd.h"
40 #include "lock0lock.h"
41 #include "lock0priv.h"
42 #include "univ.i"
43
44 template <typename F>
45 bool All_locks_iterator::iterate_over_current_table(F &&f) {
46 if (m_table_ids.size() == m_bucket_id) {
47 return false;
48 }
49 ut_ad(m_bucket_id < m_table_ids.size());
50 const table_id_t table_id = m_table_ids[m_bucket_id];
51 locksys::find_on_table(table_id, [&](const lock_t &lock) {
52 std::forward<F>(f)(lock);
53 return false;
54 });
55 m_bucket_id++;
56 return true;
57 }
58
59 template <typename F>
60 bool All_locks_iterator::iterate_over_current_cell(Locks_hashtable &hash_table,
61 F &&f) {
62 locksys::Global_shared_latch_guard shared_latch_guard{UT_LOCATION_HERE};
63
64 if (m_bucket_id == 0) {
65 m_lock_sys_n_resizes_at_the_beginning = lock_sys->n_resizes;
66 }
67 /*
68 Current implementation does not crash in case of lock_sys_resize() executed
69 concurrently with iterating over locks, instead returning incomplete data.
70 This is better than reporting some locks twice, which would violate primary
71 key constraint, and could happen if we blindly report all locks from
72 m_bucket_id-th cell, without first checking if hash table was resized
73 causing reshuffling of entries among cells.
74 For now, the only use of this iterator is in performance_schema.data_locks
75 and performance_schema.data_wait_locks which both provide no guarantee that
76 the view of the locks is consistent. I consider current implementation a
77 good trade-off between simplicity of implementation and correctness, as any
78 problems can only occur during dynamically resizing the buffer pool (which
79 causes resize of lock-sys hash tables) and the only manifestation will be
80 that some locks are not reported (which is always possible anyway given
81 that we don't hold any latch permanently).
82 A more complicated solution would be to have a dedicated rwlock x-acquired
83 for lock_sys_resize() and s-acquired by the iterator constructor and
84 released in the destructor.
85 Having long-lasting latches, and non-trivial life-cycle of this class seems
86 to be introducing too much complexity to me (for one thing, reasoning about
87 latching order is very complicated then).
88 */
89 if (m_lock_sys_n_resizes_at_the_beginning != lock_sys->n_resizes ||
90 hash_table.get_n_cells() <= m_bucket_id) {
91 return false;
92 }
93 const size_t shard_id = m_bucket_id % locksys::Latches::SHARDS_COUNT;
94 /* We need to latch the shard of lock-sys which contains the locks from
95 hash_get_nth_cell(hash_table, m_bucket_id). We know that they must be in a
96 single shard, as otherwise lock-sys wouldn't be able to iterate over bucket.*/
97 locksys::Shard_naked_latch_guard shard_guard{UT_LOCATION_HERE, nullptr,
98 m_bucket_id};
99 m_bucket_id = hash_table.find_set_in_this_shard(m_bucket_id);
100 if (m_bucket_id < hash_table.get_n_cells()) {
101 hash_table.find_in_cell(m_bucket_id, [&](lock_t *lock) {
102 std::forward<F>(f)(*lock);
103 return false;
104 });
105
106 m_bucket_id += locksys::Latches::SHARDS_COUNT;
107 }
108 if (m_bucket_id < hash_table.get_n_cells()) {
109 return true;
110 }
111 m_bucket_id = shard_id + 1;
112 return m_bucket_id != locksys::Latches::SHARDS_COUNT;
113 }
114
115 bool All_locks_iterator::iterate_over_next_batch(
116 const std::function<void(const lock_t &lock)> &f) {
117 /*
118 We want to report at least one lock.
119 We will search for it in:
120 - table locks, one table at a time
121 - predicate page locks, one hash table cell at a time
122 - predicate locks, one hash table cell at a time
123 - record locks, one hash table cell at a time
124 When inspecting each of this places, we report all locks found there.
125 We stop as soon as we found something.
126 */
127 bool found_at_least_one_lock = false;
128
129 auto report_lock = [&found_at_least_one_lock, &f](const lock_t &lock) {
130 f(lock);
131 found_at_least_one_lock = true;
132 };
133
134 while (!found_at_least_one_lock && m_stage != stage_t::DONE) {
135 bool is_stage_finished;
136
137 switch (m_stage) {
138 case stage_t::NOT_STARTED: {
139 m_table_ids = dict_get_all_table_ids();
140 is_stage_finished = true;
141 break;
142 }
143
144 case stage_t::TABLE_LOCKS: {
145 is_stage_finished = !iterate_over_current_table(report_lock);
146 break;
147 }
148
149 case stage_t::PRDT_PAGE_LOCKS: {
150 is_stage_finished =
151 !iterate_over_current_cell(lock_sys->prdt_page_hash, report_lock);
152 break;
153 }
154
155 case stage_t::PRDT_LOCKS: {
156 is_stage_finished =
157 !iterate_over_current_cell(lock_sys->prdt_hash, report_lock);
158 break;
159 }
160
161 case stage_t::REC_LOCKS: {
162 is_stage_finished =
163 !iterate_over_current_cell(lock_sys->rec_hash, report_lock);
164
165 if (found_at_least_one_lock) {
166 DEBUG_SYNC_C("all_locks_iterator_found_record_lock");
167 }
168 break;
169 }
170
171 default:
172 ut_error;
173 }
174
175 if (is_stage_finished) {
176 m_stage = static_cast<stage_t>(to_int(m_stage) + 1);
177 m_bucket_id = 0;
178 }
179 }
180
181 return m_stage == stage_t::DONE;
182 }
183
184 namespace locksys {
185 const lock_t *find_blockers(const lock_t &wait_lock,
186 std::function<bool(const lock_t &)> visitor) {
187 ut_ad(locksys::owns_lock_shard(&wait_lock));
188 ut_a(wait_lock.is_waiting());
189 locksys::Trx_locks_cache wait_lock_cache{};
190 if (lock_get_type_low(&wait_lock) == LOCK_REC) {
191 const uint16_t heap_no = lock_rec_find_set_bit(&wait_lock);
192 const auto found = wait_lock.hash_table().find_on_record(
193 RecID{&wait_lock, heap_no}, [&](lock_t *lock) {
194 if (lock == &wait_lock) {
195 return true;
196 }
197 if (locksys::has_to_wait(&wait_lock, lock, wait_lock_cache)) {
198 return visitor(*lock);
199 }
200 return false;
201 });
202 return found == &wait_lock ? nullptr : found;
203 }
204 for (auto lock : wait_lock.tab_lock.table->locks) {
205 if (lock == &wait_lock) {
206 return nullptr;
207 }
208 if (locksys::has_to_wait(&wait_lock, lock, wait_lock_cache)) {
209 if (visitor(*lock)) {
210 return lock;
211 }
212 }
213 }
214 return nullptr;
215 }
216
217 void find_on_table(const table_id_t table_id,
218 std::function<bool(const lock_t &)> visitor) {
219 /* A thread which is dropping the table is not expecting the n_ref_count
220 to be above 0 (or 1 if we count the thread itself), because it holds exclusive
221 MDL on the table, and so nobody else should be accessing it. Yet, this method
222 here will call dd_table_open_on_id_in_mem(..) which is incrementing
223 n_ref_count. However, before releasing dict_sys->mutex it will decrement
224 n_ref_count again by calling table->release(), so the dropping thread should
225 not observe the state with elevated n_ref_count, because it checks it while
226 holding the dict_sys->mutex. Because visitor might be quite heavy, we do so,
227 before calling visitor. This, however, means visitor is called without holding
228 dict_sys->mutex nor even n_ref_count protection, thus it is crucial, that we
229 ensure table is not freed by other means: namely we verified that there is at
230 least one lock in table->locks, and we keep the shard mutex which prevents
231 anyone from releasing it - this is sufficient, and a technique already used in
232 other parts of the lock_sys, because one can not free a dict_table_t unless
233 all locks are released first. One drawback of this approach is that we acquire
234 lock_sys shard mutex, while holding dict_sys->mutex. To minimize the impact of
235 doing so, we only do so if table->locks appears non-empty - which we can check
236 without any mutex because it is an atomic. Of course, because we don't hold
237 any mutex, there's nothing preventing anyone from adding or removing locks on
238 this table, but this is fine, as we do not promise at which exact moment
239 during the call table->locks are inspected, so we can pick a convenient
240 linearization moment, or even check it twice, and the caller can't complain.
241 There are important reasons we use table->release() instead of a more regular
242 dict_table_close or dd_table_close:
243 - The dict_table_close, as a side effect might call dict_stats_deinit()
244 when the n_ref_count drops to zero, which could interfere with
245 create_table_info_t::create_table_update_dict() which is calling
246 dict_stats_update() without properly bumping the n_ref_count.
247 In worst case, our thread could deinitialize the stats just before
248 dict_stats_save() is called from dict_stats_update() to persist them.
249 - The dict_table_close() calls table->lock() which would violate latching
250 order if done while holding lock sys shard mutex.
251 - It's a bit cheaper and we try to not hog dict_sys->mutex */
252 dict_sys_mutex_enter();
253 dict_table_t *table = dd_table_open_on_id_in_mem(table_id, true);
254 if (table != nullptr) {
255 /* avoid waiting for shard mutex if there are no locks to report */
256 if (table->locks.get_length()) {
257 Shard_latch_guard table_latch_guard{UT_LOCATION_HERE, *table};
258 const bool any_lock_exists = (0 < table->locks.get_length());
259 table->release();
260 dict_sys_mutex_exit();
261 if (any_lock_exists) {
262 for (auto lock : table->locks) {
263 if (visitor(*lock)) {
264 return;
265 }
266 }
267 } /* else: table might be dangling, so do not even try to access it! */
268 return;
269 }
270 table->release();
271 }
272 dict_sys_mutex_exit();
273 }
274 } // namespace locksys