| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 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 |
#ifndef lock0latches_h |
| 28 |
#define lock0latches_h |
| 29 |
|
| 30 |
#include "dict0types.h" |
| 31 |
#include "sync0sharded_rw.h" |
| 32 |
#include "ut0cpu_cache.h" |
| 33 |
#include "ut0mutex.h" |
| 34 |
|
| 35 |
/* Forward declarations */ |
| 36 |
struct dict_table_t; |
| 37 |
class page_id_t; |
| 38 |
|
| 39 |
namespace locksys { |
| 40 |
/** |
| 41 |
The class which handles the logic of latching of lock_sys queues themselves. |
| 42 |
The lock requests for table locks and record locks are stored in queues, and to |
| 43 |
allow concurrent operations on these queues, we need a mechanism to latch these |
| 44 |
queues in safe and quick fashion. |
| 45 |
In the past we had a single latch which protected access to all of them. |
| 46 |
Now, we use more granular approach. |
| 47 |
In extreme, one could imagine protecting each queue with a separate latch. |
| 48 |
To avoid having too many latch objects, and having to create and remove them on |
| 49 |
demand, we use a more conservative approach. |
| 50 |
The queues are grouped into a fixed number of shards, and each shard is |
| 51 |
protected by its own mutex. |
| 52 |
|
| 53 |
However, there are several rare events in which we need to "stop the world" - |
| 54 |
latch all queues, to prevent any activity inside lock-sys. |
| 55 |
One way to accomplish this would be to simply latch all the shards one by one, |
| 56 |
but it turns out to be way too slow in debug runs, where such "stop the world" |
| 57 |
events are very frequent due to lock_sys validation. |
| 58 |
|
| 59 |
To allow for efficient latching of everything, we've introduced a global_latch, |
| 60 |
which is a read-write latch. |
| 61 |
Most of the time, we operate on one or two shards, in which case it is |
| 62 |
sufficient to s-latch the global_latch and then latch shard's mutex. |
| 63 |
For the "stop the world" operations, we x-latch the global_latch, which prevents |
| 64 |
any other thread from latching any shard. |
| 65 |
|
| 66 |
However, it turned out that on ARM architecture, the default implementation of |
| 67 |
read-write latch (rw_lock_t) is too slow because increments and decrements of |
| 68 |
the number of s-latchers is implemented as read-update-try-to-write loop, which |
| 69 |
means multiple threads try to modify the same cache line disrupting each other. |
| 70 |
Therefore, we use a sharded version of read-write latch (Sharded_rw_lock), which |
| 71 |
internally uses multiple instances of rw_lock_t, spreading the load over several |
| 72 |
cache lines. Note that this sharding is a technical internal detail of the |
| 73 |
global_latch, which for all other purposes can be treated as a single entity. |
| 74 |
|
| 75 |
This his how this conceptually looks like: |
| 76 |
``` |
| 77 |
[ global latch ] |
| 78 |
| |
| 79 |
v |
| 80 |
[table shard 1] ... [table shard 512] [page shard 1] ... [page shard 512] |
| 81 |
|
| 82 |
``` |
| 83 |
|
| 84 |
So, for example access two queues for two records involves following steps: |
| 85 |
1. s-latch the global_latch |
| 86 |
2. identify the 2 pages to which the records belong |
| 87 |
3. identify the lock_sys 2 hash cells which contain the queues for given pages |
| 88 |
4. identify the 2 shard ids which contain these two cells |
| 89 |
5. latch mutexes for the two shards in the order of their addresses |
| 90 |
|
| 91 |
All of the steps above (except 2, as we usually know the page already) are |
| 92 |
accomplished with the help of single line: |
| 93 |
|
| 94 |
locksys::Shard_latches_guard guard{*block_a, *block_b}; |
| 95 |
|
| 96 |
And to "stop the world" one can simply x-latch the global latch by using: |
| 97 |
|
| 98 |
locksys::Global_exclusive_latch_guard guard{}; |
| 99 |
|
| 100 |
This class does not expose too many public functions, as the intention is to |
| 101 |
rather use friend guard classes, like the Shard_latches_guard demonstrated. |
| 102 |
*/ |
| 103 |
class Latches { |
| 104 |
private: |
| 105 |
using Lock_mutex = ib_mutex_t; |
| 106 |
|
| 107 |
/** A helper wrapper around Shared_rw_lock which simplifies: |
| 108 |
- lifecycle by providing constructor and destructor, and |
| 109 |
- s-latching and s-unlatching by keeping track of the shard id used for |
| 110 |
spreading the contention. |
| 111 |
There must be at most one instance of this class (the one in the lock_sys), as |
| 112 |
it uses thread_local-s to remember which shard of sharded rw lock was used by |
| 113 |
this thread to perform s-latching (so, hypothetical other instances would |
| 114 |
share this field, overwriting it and leading to errors). */ |
| 115 |
class Unique_sharded_rw_lock { |
| 116 |
/** The actual rw_lock implementation doing the heavy lifting */ |
| 117 |
Sharded_rw_lock rw_lock; |
| 118 |
|
| 119 |
/** The value used for m_shard_id to indicate that current thread did not |
| 120 |
s-latch any of the rw_lock's shards */ |
| 121 |
static constexpr size_t NOT_IN_USE = std::numeric_limits<size_t>::max(); |
| 122 |
|
| 123 |
/** The id of the rw_lock's shard which this thread has s-latched, or |
| 124 |
NOT_IN_USE if it has not s-latched any*/ |
| 125 |
static thread_local size_t m_shard_id; |
| 126 |
|
| 127 |
public: |
| 128 |
Unique_sharded_rw_lock(); |
| 129 |
~Unique_sharded_rw_lock(); |
| 130 |
bool try_x_lock(ut::Location location) { |
| 131 |
return rw_lock.try_x_lock(location); |
| 132 |
} |
| 133 |
/** Checks if there is a thread requesting an x-latch waiting for our |
| 134 |
thread to release its s-latch. |
| 135 |
Must be called while holding an s-latch. |
| 136 |
@return true iff there is an x-latcher blocked by our s-latch. */ |
| 137 |
bool is_x_blocked_by_our_s() { |
| 138 |
ut_ad(m_shard_id != NOT_IN_USE); |
| 139 |
return rw_lock.is_x_blocked_by_s(m_shard_id); |
| 140 |
} |
| 141 |
void x_lock(ut::Location location) { rw_lock.x_lock(location); } |
| 142 |
void x_unlock() { rw_lock.x_unlock(); } |
| 143 |
void s_lock(ut::Location location) { |
| 144 |
ut_ad(m_shard_id == NOT_IN_USE); |
| 145 |
m_shard_id = rw_lock.s_lock(location); |
| 146 |
} |
| 147 |
void s_unlock() { |
| 148 |
ut_ad(m_shard_id != NOT_IN_USE); |
| 149 |
rw_lock.s_unlock(m_shard_id); |
| 150 |
m_shard_id = NOT_IN_USE; |
| 151 |
} |
| 152 |
#ifdef UNIV_DEBUG |
| 153 |
bool x_own() const { return rw_lock.x_own(); } |
| 154 |
bool s_own() const { |
| 155 |
return m_shard_id != NOT_IN_USE && rw_lock.s_own(m_shard_id); |
| 156 |
} |
| 157 |
#endif |
| 158 |
}; |
| 159 |
|
| 160 |
using Padded_mutex = ut::Cacheline_padded<Lock_mutex>; |
| 161 |
|
| 162 |
public: |
| 163 |
/** Number of page shards, and also number of table shards. |
| 164 |
Must be a power of two */ |
| 165 |
static constexpr size_t SHARDS_COUNT = 512; |
| 166 |
|
| 167 |
private: |
| 168 |
/* |
| 169 |
Functions related to sharding by page (containing records to lock). |
| 170 |
|
| 171 |
This must be done in such a way that two pages which share a single lock |
| 172 |
queue fall into the same shard. We accomplish this by reusing hash function |
| 173 |
used to determine lock queue, and then group multiple queues into single |
| 174 |
shard. |
| 175 |
*/ |
| 176 |
class Page_shards { |
| 177 |
/** Each shard is protected by a separate mutex. Mutexes are padded to avoid |
| 178 |
false sharing issues with cache. */ |
| 179 |
Padded_mutex mutexes[SHARDS_COUNT]; |
| 180 |
/** |
| 181 |
Identifies the page shard which contains record locks for records from the |
| 182 |
given page. |
| 183 |
@param[in] page_id The space_id and page_no of the page |
| 184 |
@return Integer in the range [0..lock_sys_t::SHARDS_COUNT) |
| 185 |
*/ |
| 186 |
static size_t get_shard(const page_id_t &page_id); |
| 187 |
|
| 188 |
public: |
| 189 |
Page_shards(); |
| 190 |
~Page_shards(); |
| 191 |
|
| 192 |
/** |
| 193 |
Returns the mutex which (together with the global latch) protects the page |
| 194 |
shard which contains record locks for records from the given page. |
| 195 |
@param[in] page_id The space_id and page_no of the page |
| 196 |
@return The mutex responsible for the shard containing the page |
| 197 |
*/ |
| 198 |
const Lock_mutex &get_mutex(const page_id_t &page_id) const; |
| 199 |
|
| 200 |
/** |
| 201 |
Returns the mutex which (together with the global latch) protects the page |
| 202 |
shard which contains record locks for records from the given page. |
| 203 |
@param[in] page_id The space_id and page_no of the page |
| 204 |
@return The mutex responsible for the shard containing the page |
| 205 |
*/ |
| 206 |
Lock_mutex &get_mutex(const page_id_t &page_id); |
| 207 |
|
| 208 |
/** |
| 209 |
Returns the mutex which (together with the global latch) protects the page |
| 210 |
shard which contains record locks from given cell of hash tables. |
| 211 |
@param[in] cell_id The cell_id of the hash table |
| 212 |
@return The mutex responsible for the shard containing the page |
| 213 |
*/ |
| 214 |
Lock_mutex &get_mutex(const uint64_t cell_id); |
| 215 |
}; |
| 216 |
|
| 217 |
/* |
| 218 |
Functions related to sharding by table |
| 219 |
|
| 220 |
We identify tables by their id. Each table has its own lock queue, so we |
| 221 |
simply group several such queues into single shard. |
| 222 |
*/ |
| 223 |
class Table_shards { |
| 224 |
/** Each shard is protected by a separate mutex. Mutexes are padded to avoid |
| 225 |
false sharing issues with cache. */ |
| 226 |
Padded_mutex mutexes[SHARDS_COUNT]; |
| 227 |
/** |
| 228 |
Identifies the table shard which contains locks for the given table. |
| 229 |
@param[in] table_id The id of the table |
| 230 |
@return Integer in the range [0..lock_sys_t::SHARDS_COUNT) |
| 231 |
*/ |
| 232 |
static size_t get_shard(const table_id_t table_id); |
| 233 |
|
| 234 |
public: |
| 235 |
Table_shards(); |
| 236 |
~Table_shards(); |
| 237 |
|
| 238 |
/** Returns the mutex which (together with the global latch) protects the |
| 239 |
table shard which contains table locks for the given table. |
| 240 |
@param[in] table_id The id of the table |
| 241 |
@return The mutex responsible for the shard containing the table |
| 242 |
*/ |
| 243 |
Lock_mutex &get_mutex(const table_id_t table_id); |
| 244 |
|
| 245 |
/** Returns the mutex which (together with the global latch) protects the |
| 246 |
table shard which contains table locks for the given table. |
| 247 |
@param[in] table_id The id of the table |
| 248 |
@return The mutex responsible for the shard containing the table |
| 249 |
*/ |
| 250 |
const Lock_mutex &get_mutex(const table_id_t table_id) const; |
| 251 |
|
| 252 |
/** Returns the mutex which (together with the global latch) protects the |
| 253 |
table shard which contains table locks for the given table. |
| 254 |
@param[in] table The table |
| 255 |
@return The mutex responsible for the shard containing the table |
| 256 |
*/ |
| 257 |
const Lock_mutex &get_mutex(const dict_table_t &table) const; |
| 258 |
}; |
| 259 |
|
| 260 |
/** padding to prevent other memory update hotspots from residing on the same |
| 261 |
memory cache line */ |
| 262 |
char pad1[ut::INNODB_CACHE_LINE_SIZE] = {}; |
| 263 |
|
| 264 |
Unique_sharded_rw_lock global_latch; |
| 265 |
|
| 266 |
Page_shards page_shards; |
| 267 |
|
| 268 |
Table_shards table_shards; |
| 269 |
|
| 270 |
public: |
| 271 |
/* You should use following RAII guards to modify the state of Latches. */ |
| 272 |
friend class Global_exclusive_latch_guard; |
| 273 |
friend class Global_exclusive_try_latch; |
| 274 |
friend class Global_shared_latch_guard; |
| 275 |
friend class Shard_naked_latch_guard; |
| 276 |
friend class Shard_naked_latches_guard; |
| 277 |
|
| 278 |
/** You should not use this functionality in new code. |
| 279 |
Instead use Global_exclusive_latch_guard. |
| 280 |
This is intended only to be use within lock0* module, thus this class is only |
| 281 |
accessible through lock0priv.h. |
| 282 |
It is only used by lock_rec_fetch_page() as a workaround. */ |
| 283 |
friend class Unsafe_global_latch_manipulator; |
| 284 |
|
| 285 |
/** For some reason clang 6.0.0 and 7.0.0 (but not 8.0.0) fail at linking |
| 286 |
stage if we completely omit the destructor declaration, or use |
| 287 |
|
| 288 |
~Latches() = default; |
| 289 |
|
| 290 |
This might be somehow connected to one of these: |
| 291 |
https://bugs.llvm.org/show_bug.cgi?id=28280 |
| 292 |
https://github.com/android/ndk/issues/143 |
| 293 |
https://reviews.llvm.org/D45898 |
| 294 |
So, this declaration is just to make clang 6.0.0 and 7.0.0 happy. |
| 295 |
*/ |
| 296 |
#if defined(__clang__) && (__clang_major__ < 8) |
| 297 |
~Latches() {} // NOLINT(modernize-use-equals-default) |
| 298 |
#else |
| 299 |
~Latches() = default; |
| 300 |
#endif |
| 301 |
|
| 302 |
#ifdef UNIV_DEBUG |
| 303 |
/** |
| 304 |
Tests if lock_sys latch is exclusively owned by the current thread. |
| 305 |
@return true iff the current thread owns exclusive global lock_sys latch |
| 306 |
*/ |
| 307 |
bool owns_exclusive_global_latch() const { return global_latch.x_own(); } |
| 308 |
|
| 309 |
/** |
| 310 |
Tests if lock_sys latch is owned in shared mode by the current thread. |
| 311 |
@return true iff the current thread owns shared global lock_sys latch |
| 312 |
*/ |
| 313 |
bool owns_shared_global_latch() const { return global_latch.s_own(); } |
| 314 |
|
| 315 |
/** |
| 316 |
Tests if given page shard can be safely accessed by the current thread. |
| 317 |
@param[in] page_id The space_id and page_no of the page |
| 318 |
@return true iff the current thread owns exclusive global lock_sys latch or |
| 319 |
both a shared global lock_sys latch and mutex protecting the page shard |
| 320 |
*/ |
| 321 |
bool owns_page_shard(const page_id_t &page_id) const { |
| 322 |
return owns_exclusive_global_latch() || |
| 323 |
(page_shards.get_mutex(page_id).is_owned() && |
| 324 |
owns_shared_global_latch()); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
Tests if given table shard can be safely accessed by the current thread. |
| 329 |
@param table the table |
| 330 |
@return true iff the current thread owns exclusive global lock_sys latch or |
| 331 |
both a shared global lock_sys latch and mutex protecting the table shard |
| 332 |
*/ |
| 333 |
bool owns_table_shard(const dict_table_t &table) const { |
| 334 |
return owns_exclusive_global_latch() || |
| 335 |
(table_shards.get_mutex(table).is_owned() && |
| 336 |
owns_shared_global_latch()); |
| 337 |
} |
| 338 |
#endif /* UNIV_DEBUG */ |
| 339 |
}; |
| 340 |
} // namespace locksys |
| 341 |
|
| 342 |
#endif /* lock0latches_h */ |