| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2016, 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 handler/p_s.cc |
| 29 |
InnoDB performance_schema tables interface to MySQL. |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#include "storage/innobase/handler/p_s.h" |
| 34 |
|
| 35 |
#include <type_traits> |
| 36 |
|
| 37 |
#include "dict0dd.h" |
| 38 |
#include "lock0iter.h" |
| 39 |
#include "lock0lock.h" |
| 40 |
#include "sql_table.h" |
| 41 |
#include "trx0i_s.h" |
| 42 |
|
| 43 |
/** |
| 44 |
@page PAGE_INNODB_PFS Innodb data lock instrumentation |
| 45 |
Innodb Performance Schema data lock instrumentation |
| 46 |
|
| 47 |
@section data_lock_iterators Data lock iterators |
| 48 |
|
| 49 |
To provide content to the performance_schema.data_locks table, |
| 50 |
innodb implements #Innodb_data_lock_iterator. |
| 51 |
|
| 52 |
Likewise, table performance_schema.data_wait_locks |
| 53 |
is populated with #Innodb_data_lock_wait_iterator. |
| 54 |
|
| 55 |
Both these iterators need to return the data present |
| 56 |
in the innodb engine memory, |
| 57 |
which imply to take the proper mutex locks when inspecting it. |
| 58 |
The structure to inspect here is the transaction list (#trx_sys) |
| 59 |
|
| 60 |
How to implement this scan is critical for performances. |
| 61 |
|
| 62 |
@subsection no_full_scan No full scan |
| 63 |
|
| 64 |
Consider this implementation: |
| 65 |
- Take all necessary locks |
| 66 |
- Scan all the innodb internal locks |
| 67 |
- Report all of them to the performance schema |
| 68 |
- Release all the locks taken |
| 69 |
|
| 70 |
This implementation materializes the entire table. |
| 71 |
|
| 72 |
The benefits with this approach are: |
| 73 |
- The materialized table is consistent |
| 74 |
|
| 75 |
The problems with this approach are: |
| 76 |
- The innodb engine is frozen for the entire duration, |
| 77 |
for a time that is unpredictable. |
| 78 |
- Memory consumption spikes, without bounds |
| 79 |
- Materializing all rows upfront is incompatible with supporting an index |
| 80 |
|
| 81 |
For example with N = 10,000 transactions, |
| 82 |
a single scan reports all 10,000 transaction locks. |
| 83 |
|
| 84 |
This alternative is rejected. |
| 85 |
|
| 86 |
@subsection no_single_row_scan No single row scan |
| 87 |
|
| 88 |
Consider this implementation: |
| 89 |
- Take all necessary locks |
| 90 |
- Resume the scan on innodb internal locks for 1 record |
| 91 |
- Report this record to the performance schema |
| 92 |
- Release all the locks taken |
| 93 |
|
| 94 |
This implementation returns a row for a single transaction, |
| 95 |
or even a single lock, at a time. |
| 96 |
|
| 97 |
The benefits with this approach are: |
| 98 |
- Memory consumption is well bounded, and low. |
| 99 |
|
| 100 |
The problems with this approach are: |
| 101 |
- Data reported can be very inconsistent. |
| 102 |
- Implementing a restartable scan, on a very dynamic structure, |
| 103 |
without holding any lock, is complex. |
| 104 |
- Even assuming how to implement a scan is resolved, |
| 105 |
looping N times to find element i, i+1, i+2 ... in a list |
| 106 |
ends up having a complexity in O(N^2), consuming CPU. |
| 107 |
|
| 108 |
For example with N = 10,000 transactions, |
| 109 |
the trx_list would be scanned 10,000 times |
| 110 |
to return 1 record each time. |
| 111 |
The total number of operations on the list is 100 Millions. |
| 112 |
|
| 113 |
This alternative is rejected. |
| 114 |
|
| 115 |
@subsection restartable_batch_scan Restartable batch scan |
| 116 |
|
| 117 |
What is implemented is: |
| 118 |
- As lock sys data structures are sharded with each shard having own latch, we |
| 119 |
inspect the shards one by one to avoid latching whole lock system |
| 120 |
- We first process table locks, then record locks |
| 121 |
- Table locks are processed one table at a time |
| 122 |
- Record locks are processed one internal hash table bucket at a time |
| 123 |
|
| 124 |
This is a compromise, with the following properties: |
| 125 |
- Memory consumption is bounded, |
| 126 |
by the number of locks in each bucket and on each table. |
| 127 |
- The duration of mutex locks on innodb structures is bounded |
| 128 |
by the number of locks in each bucket and on each table. |
| 129 |
- The data returned is not consistent, |
| 130 |
but at least it is "consistent by chunks" |
| 131 |
|
| 132 |
*/ |
| 133 |
|
| 134 |
static const char *g_engine = "INNODB"; |
| 135 |
static const size_t g_engine_length = 6; |
| 136 |
namespace { |
| 137 |
struct parsed_table_path { |
| 138 |
std::string_view schema; |
| 139 |
std::string_view table; |
| 140 |
std::string_view partition; |
| 141 |
std::string_view subpartition; |
| 142 |
}; |
| 143 |
} // namespace |
| 144 |
/** Searches for the lock_t object which has the specified engine_lock_id (@see |
| 145 |
print_lock_id), and if it exists, executes provided f on it, under protection |
| 146 |
of shard-specific latch. The type of the lock (LOCK_REC or LOCK_TABLE) is also |
| 147 |
provided to f. For LOCK_REC multiple lock requests can share the same lock_t |
| 148 |
object, thus f will be fed the specific heap_no. For LOCK_TABLE this third |
| 149 |
argument will be always ULINT_UNDEFINED. |
| 150 |
@param[in] engine_lock_id |
| 151 |
The string used in ENGINE_LOCK_ID column to uniquely |
| 152 |
identify the lock request. |
| 153 |
@param[in] f |
| 154 |
A callable, for which f(lock, type, heap_no) will be |
| 155 |
executed, if lock with specified engine_lock_id exists. */ |
| 156 |
template <typename F> |
| 157 |
static void find_lock_and_execute(const char *engine_lock_id, F &&f) { |
| 158 |
i_s_locks_row_t row; |
| 159 |
const int lock_type = trx_i_s_parse_lock_id(engine_lock_id, &row); |
| 160 |
if (0 == lock_type) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if (lock_type == LOCK_REC) { |
| 165 |
const page_id_t page_id{row.lock_space, row.lock_page}; |
| 166 |
locksys::Shard_latch_guard guard{UT_LOCATION_HERE, page_id}; |
| 167 |
|
| 168 |
const lock_t *lock = lock_find_record_lock_by_guid(page_id, row.lock_guid); |
| 169 |
|
| 170 |
if (lock != nullptr) { |
| 171 |
std::forward<F>(f)(*lock, lock_type, row.lock_rec); |
| 172 |
} |
| 173 |
|
| 174 |
} else { |
| 175 |
ut_ad(lock_type == LOCK_TABLE); |
| 176 |
locksys::find_on_table(row.lock_table_id, [&](const lock_t &lock) { |
| 177 |
if (lock_guid_t(lock) == row.lock_guid) { |
| 178 |
std::forward<F>(f)(lock, lock_type, ULINT_UNDEFINED); |
| 179 |
return true; |
| 180 |
} |
| 181 |
return false; |
| 182 |
}); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** Inspect data locks for the innodb storage engine. */ |
| 187 |
class Innodb_data_lock_iterator : public PSI_engine_data_lock_iterator { |
| 188 |
public: |
| 189 |
bool scan(PSI_server_data_lock_container *container, |
| 190 |
bool with_lock_data) override; |
| 191 |
|
| 192 |
void fetch(PSI_server_data_lock_container *container, |
| 193 |
const char *engine_lock_id, size_t engine_lock_id_length, |
| 194 |
bool with_lock_data) override; |
| 195 |
|
| 196 |
private: |
| 197 |
All_locks_iterator m_all_locks_iterator; |
| 198 |
|
| 199 |
/** For a given lock it will inform container about each lock request it |
| 200 |
represents (which can be more than one in case of LOCK_RECORD as there can be |
| 201 |
multiple lock requests differing only by heap_no compressed into single lock |
| 202 |
object), subject to filtering defined for the container, and |
| 203 |
optionally by filter_heap_no if with_filter is true. |
| 204 |
The with_filter is used to fetch just a lock request, the one specified by its |
| 205 |
heap_no. Please note, that there is no guarantee that the lock with this heap |
| 206 |
no is still in the lock sys. |
| 207 |
@param[in,out] parsed_paths The cache of table path parsing results |
| 208 |
@param[in,out] container The container to fill. Serves as both visitor |
| 209 |
and filter |
| 210 |
@param[in] lock The lock to inspect and report upon |
| 211 |
@param[in] with_lock_data True if column LOCK_DATA is required. |
| 212 |
@param[in] with_filter If true then filter_heap_no has |
| 213 |
to be passed, and means that only the lock |
| 214 |
request for that heap number (if still present |
| 215 |
and matches the container's filters) should be |
| 216 |
reported to the container. If false then all |
| 217 |
lock requests represented for this lock are |
| 218 |
considered for reporting. |
| 219 |
@param[in] filter_heap_no if with_filter is true it further narrows the |
| 220 |
set of reported lock requests to just the one |
| 221 |
with given immutable id. |
| 222 |
@return number of locks reported to the container */ |
| 223 |
static size_t report( |
| 224 |
std::unordered_map<const char *, parsed_table_path> &parsed_paths, |
| 225 |
PSI_server_data_lock_container &container, const lock_t &lock, |
| 226 |
bool with_lock_data, bool with_filter, |
| 227 |
ulint filter_heap_no = ULINT_UNDEFINED); |
| 228 |
}; |
| 229 |
|
| 230 |
/** Inspect data lock waits for the innodb storage engine. */ |
| 231 |
class Innodb_data_lock_wait_iterator |
| 232 |
: public PSI_engine_data_lock_wait_iterator { |
| 233 |
public: |
| 234 |
bool scan(PSI_server_data_lock_wait_container *container) override; |
| 235 |
|
| 236 |
void fetch(PSI_server_data_lock_wait_container *container, |
| 237 |
const char *requesting_engine_lock_id, |
| 238 |
size_t requesting_engine_lock_id_length, |
| 239 |
const char *blocking_engine_lock_id, |
| 240 |
size_t blocking_engine_lock_id_length) override; |
| 241 |
|
| 242 |
private: |
| 243 |
All_locks_iterator m_all_locks_iterator; |
| 244 |
|
| 245 |
/** For a given wait_lock it will inform container about each lock which is |
| 246 |
blocking this wait_lock, subject to filtering defined for the container, and |
| 247 |
optionally by filter_blocking_lock_immutable_id if with_filter is true. |
| 248 |
The with_filter is used to fetch just a single blocking lock, the one |
| 249 |
specified by its immutable id. Please note, that there is no guarantee that |
| 250 |
this blocking lock is still in the lock sys. |
| 251 |
@param[in,out] container The container to fill. Serves as both visitor and |
| 252 |
filter |
| 253 |
@param[in] wait_lock The requesting lock which has to wait |
| 254 |
@param[in] with_filter If true then filter_blocking_lock_immutable_id has |
| 255 |
to be passed, and means that only the lock with |
| 256 |
that immutable id (if still present and matches |
| 257 |
the container's filters) should be reported to the |
| 258 |
container. If false then all locks blocking the |
| 259 |
wait_lock are considered for reporting. |
| 260 |
@param[in] filter_blocking_lock_guid if with_filter is true it |
| 261 |
further narrows the set of reported locks to just |
| 262 |
the one with given guid. |
| 263 |
@return number of locks reported to the container */ |
| 264 |
static size_t report( |
| 265 |
PSI_server_data_lock_wait_container &container, const lock_t &wait_lock, |
| 266 |
bool with_filter, |
| 267 |
const lock_guid_t &filter_blocking_lock_guid = lock_guid_t()); |
| 268 |
}; |
| 269 |
|
| 270 |
PSI_engine_data_lock_iterator * |
| 271 |
Innodb_data_lock_inspector::create_data_lock_iterator() { |
| 272 |
return new Innodb_data_lock_iterator(); |
| 273 |
} |
| 274 |
|
| 275 |
PSI_engine_data_lock_wait_iterator * |
| 276 |
Innodb_data_lock_inspector::create_data_lock_wait_iterator() { |
| 277 |
return new Innodb_data_lock_wait_iterator(); |
| 278 |
} |
| 279 |
|
| 280 |
void Innodb_data_lock_inspector::destroy_data_lock_iterator( |
| 281 |
PSI_engine_data_lock_iterator *it) { |
| 282 |
delete it; |
| 283 |
} |
| 284 |
|
| 285 |
void Innodb_data_lock_inspector::destroy_data_lock_wait_iterator( |
| 286 |
PSI_engine_data_lock_wait_iterator *it) { |
| 287 |
delete it; |
| 288 |
} |
| 289 |
|
| 290 |
namespace { |
| 291 |
/** Allocate identifier in performance schema container. The string is allocated |
| 292 |
in the cache of the container and is owned by that cache. Therefore the |
| 293 |
returned std::string_view is only valid for as long as the cache is not cleared |
| 294 |
by a call to PFS_data_cache::clear() which occurs in: |
| 295 |
- PFS_data_lock_wait_container::clear() |
| 296 |
- PFS_data_lock_wait_container::shrink() |
| 297 |
- PFS_data_lock_container::clear() |
| 298 |
- PFS_data_lock_wait_container::shrink() |
| 299 |
@param[in] container The container to fill |
| 300 |
@param[in] kind The identifier kind |
| 301 |
@param[in] id_str The identifier string |
| 302 |
@return view of the cached string */ |
| 303 |
std::string_view alloc_identifier(PSI_server_data_lock_container *container, |
| 304 |
PSI_identifier kind, |
| 305 |
const std::string &id_str) { |
| 306 |
if (id_str.length() > 0) { |
| 307 |
const char *cached_id; |
| 308 |
size_t cached_id_length; |
| 309 |
container->cache_identifier(kind, id_str.c_str(), id_str.length(), |
| 310 |
&cached_id, &cached_id_length); |
| 311 |
return {cached_id, cached_id_length}; |
| 312 |
} else { |
| 313 |
return {}; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/** Parse a table path string. |
| 318 |
Isolate the table schema, name, partition and sub partition |
| 319 |
from a table path string. |
| 320 |
Convert these strings and store them in the performance schema container. |
| 321 |
@note String returned are not zero terminated. |
| 322 |
@param[in] container The container to fill |
| 323 |
@param[in] table_path The table path string |
| 324 |
@return parsed fragments of the table name pointing to cached strings |
| 325 |
*/ |
| 326 |
parsed_table_path parse_table_path(PSI_server_data_lock_container *container, |
| 327 |
const char *table_path) { |
| 328 |
std::string dict_table(table_path); |
| 329 |
|
| 330 |
/* Get schema and table name in system cs. */ |
| 331 |
std::string schema; |
| 332 |
std::string table; |
| 333 |
std::string partition; |
| 334 |
bool is_tmp; |
| 335 |
dict_name::get_table(dict_table, true, schema, table, partition, is_tmp); |
| 336 |
|
| 337 |
std::string part; |
| 338 |
std::string sub_part; |
| 339 |
if (!partition.empty()) { |
| 340 |
ut_ad(dict_name::is_partition(dict_table)); |
| 341 |
/* Get schema partition and sub-partition name in system cs. */ |
| 342 |
dict_name::get_partition(partition, true, part, sub_part); |
| 343 |
} |
| 344 |
parsed_table_path parsed; |
| 345 |
parsed.schema = alloc_identifier(container, PSI_IDENTIFIER_SCHEMA, schema); |
| 346 |
parsed.table = alloc_identifier(container, PSI_IDENTIFIER_TABLE, table); |
| 347 |
parsed.partition = |
| 348 |
alloc_identifier(container, PSI_IDENTIFIER_PARTITION, part); |
| 349 |
parsed.subpartition = |
| 350 |
alloc_identifier(container, PSI_IDENTIFIER_SUBPARTITION, sub_part); |
| 351 |
return parsed; |
| 352 |
} |
| 353 |
} // namespace |
| 354 |
|
| 355 |
/** Print a table lock id. |
| 356 |
@param[in] lock The lock to print |
| 357 |
@param[in,out] lock_id Printing buffer |
| 358 |
@param[in] lock_id_size Printing buffer length |
| 359 |
*/ |
| 360 |
static void print_table_lock_id(const lock_t &lock, char *lock_id, |
| 361 |
size_t lock_id_size) { |
| 362 |
/* We try to be backward compatible with INFORMATION_SCHEMA so that one can |
| 363 |
join with INFORMATION_SCHEMA.innodb_trx.trx_requested_lock_id column */ |
| 364 |
i_s_locks_row_t row; |
| 365 |
fill_locks_row(&row, &lock, ULINT_UNDEFINED); |
| 366 |
trx_i_s_create_lock_id(row, lock_id, lock_id_size); |
| 367 |
} |
| 368 |
|
| 369 |
/** Print a record lock id. |
| 370 |
@param[in] lock The lock to print |
| 371 |
@param[in] heap_no Lock heap number |
| 372 |
@param[in,out] lock_id Printing buffer |
| 373 |
@param[in] lock_id_size Printing buffer length |
| 374 |
*/ |
| 375 |
static void print_record_lock_id(const lock_t &lock, ulint heap_no, |
| 376 |
char *lock_id, size_t lock_id_size) { |
| 377 |
/* We try to be backward compatible with INFORMATION_SCHEMA so that one can |
| 378 |
join with INFORMATION_SCHEMA.innodb_trx.trx_requested_lock_id column */ |
| 379 |
i_s_locks_row_t row; |
| 380 |
fill_locks_row(&row, &lock, heap_no); |
| 381 |
trx_i_s_create_lock_id(row, lock_id, lock_id_size); |
| 382 |
} |
| 383 |
|
| 384 |
/** Print a lock id. |
| 385 |
@param[in] lock The lock to print |
| 386 |
@param[in] heap_no Lock heap number if lock's type is LOCK_REC, |
| 387 |
ignored otherwise. |
| 388 |
@param[in,out] lock_id Printing buffer |
| 389 |
@param[in] lock_id_size Printing buffer length |
| 390 |
*/ |
| 391 |
static void print_lock_id(const lock_t &lock, ulint heap_no, char *lock_id, |
| 392 |
size_t lock_id_size) { |
| 393 |
switch (lock_get_type(&lock)) { |
| 394 |
case LOCK_TABLE: |
| 395 |
print_table_lock_id(lock, lock_id, lock_id_size); |
| 396 |
break; |
| 397 |
case LOCK_REC: |
| 398 |
print_record_lock_id(lock, heap_no, lock_id, lock_id_size); |
| 399 |
break; |
| 400 |
default: |
| 401 |
ut_error; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
bool Innodb_data_lock_iterator::scan(PSI_server_data_lock_container *container, |
| 406 |
bool with_lock_data) { |
| 407 |
if (!container->accept_engine(g_engine, g_engine_length)) { |
| 408 |
return true; |
| 409 |
} |
| 410 |
std::unordered_map<const char *, parsed_table_path> parsed_paths; |
| 411 |
for (size_t found = 0; 0 == found;) { |
| 412 |
if (m_all_locks_iterator.iterate_over_next_batch([&](const lock_t &lock) { |
| 413 |
found += |
| 414 |
report(parsed_paths, *container, lock, with_lock_data, false); |
| 415 |
})) { |
| 416 |
return true; |
| 417 |
} |
| 418 |
} |
| 419 |
return false; |
| 420 |
} |
| 421 |
|
| 422 |
void Innodb_data_lock_iterator::fetch(PSI_server_data_lock_container *container, |
| 423 |
const char *engine_lock_id, |
| 424 |
size_t engine_lock_id_length, |
| 425 |
bool with_lock_data) { |
| 426 |
ut_ad(strnlen(engine_lock_id, engine_lock_id_length + 1) == |
| 427 |
engine_lock_id_length); |
| 428 |
|
| 429 |
if (!container->accept_engine(g_engine, g_engine_length)) { |
| 430 |
return; |
| 431 |
} |
| 432 |
std::unordered_map<const char *, parsed_table_path> parsed_paths; |
| 433 |
find_lock_and_execute(engine_lock_id, |
| 434 |
[&](const lock_t &lock, int lock_type, ulint heap_no) { |
| 435 |
report(parsed_paths, *container, lock, with_lock_data, |
| 436 |
lock_type == LOCK_REC, heap_no); |
| 437 |
}); |
| 438 |
} |
| 439 |
|
| 440 |
size_t Innodb_data_lock_iterator::report( |
| 441 |
std::unordered_map<const char *, parsed_table_path> &parsed_paths, |
| 442 |
PSI_server_data_lock_container &container, const lock_t &lock, |
| 443 |
bool with_lock_data, bool with_filter, ulint filter_heap_no) { |
| 444 |
size_t found = 0; |
| 445 |
ulonglong thread_id; |
| 446 |
ulonglong event_id; |
| 447 |
const char *index_name; |
| 448 |
size_t index_name_length; |
| 449 |
const void *identity; |
| 450 |
const char *lock_mode_str; |
| 451 |
const char *lock_type_str; |
| 452 |
const char *lock_status_str; |
| 453 |
const char *lock_data_str; |
| 454 |
char engine_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1]; |
| 455 |
size_t engine_lock_id_length; |
| 456 |
ulint heap_no; |
| 457 |
ut_ad(locksys::owns_lock_shard(&lock)); |
| 458 |
const auto trx_id = lock_get_trx_id(&lock); |
| 459 |
|
| 460 |
if (!container.accept_transaction_id(trx_id)) { |
| 461 |
return found; |
| 462 |
} |
| 463 |
|
| 464 |
lock_get_psi_event(&lock, &thread_id, &event_id); |
| 465 |
|
| 466 |
if (!container.accept_thread_id_event_id(thread_id, event_id)) { |
| 467 |
return found; |
| 468 |
} |
| 469 |
const char *table_path = lock_get_table_name(&lock).m_name; |
| 470 |
auto parsed_it = parsed_paths.find(table_path); |
| 471 |
if (parsed_it == parsed_paths.end()) { |
| 472 |
parsed_it = |
| 473 |
parsed_paths |
| 474 |
.emplace(table_path, parse_table_path(&container, table_path)) |
| 475 |
.first; |
| 476 |
} |
| 477 |
const auto parsed = parsed_it->second; |
| 478 |
|
| 479 |
if (!container.accept_object( |
| 480 |
parsed.schema.data(), parsed.schema.size(), parsed.table.data(), |
| 481 |
parsed.table.size(), parsed.partition.data(), parsed.partition.size(), |
| 482 |
parsed.subpartition.data(), parsed.subpartition.size())) { |
| 483 |
return found; |
| 484 |
} |
| 485 |
|
| 486 |
identity = &lock; |
| 487 |
lock_mode_str = lock_get_mode_str(&lock); |
| 488 |
lock_type_str = lock_get_type_str(&lock); |
| 489 |
lock_status_str = lock_is_waiting(lock) ? "WAITING" : "GRANTED"; |
| 490 |
|
| 491 |
switch (lock_get_type(&lock)) { |
| 492 |
case LOCK_TABLE: |
| 493 |
print_table_lock_id(lock, engine_lock_id, sizeof(engine_lock_id)); |
| 494 |
engine_lock_id_length = strlen(engine_lock_id); |
| 495 |
|
| 496 |
if (container.accept_lock_id(engine_lock_id, engine_lock_id_length)) { |
| 497 |
container.add_lock_row( |
| 498 |
g_engine, g_engine_length, engine_lock_id, engine_lock_id_length, |
| 499 |
trx_id, thread_id, event_id, parsed.schema.data(), |
| 500 |
parsed.schema.size(), parsed.table.data(), parsed.table.size(), |
| 501 |
parsed.partition.data(), parsed.partition.size(), |
| 502 |
parsed.subpartition.data(), parsed.subpartition.size(), nullptr, 0, |
| 503 |
identity, lock_mode_str, lock_type_str, lock_status_str, nullptr); |
| 504 |
found++; |
| 505 |
} |
| 506 |
break; |
| 507 |
case LOCK_REC: |
| 508 |
index_name = lock_rec_get_index_name(&lock); |
| 509 |
index_name_length = strlen(index_name); |
| 510 |
|
| 511 |
heap_no = lock_rec_find_set_bit(&lock); |
| 512 |
|
| 513 |
while (heap_no != ULINT_UNDEFINED) { |
| 514 |
if (!with_filter || (heap_no == filter_heap_no)) { |
| 515 |
print_record_lock_id(lock, heap_no, engine_lock_id, |
| 516 |
sizeof(engine_lock_id)); |
| 517 |
engine_lock_id_length = strlen(engine_lock_id); |
| 518 |
|
| 519 |
if (container.accept_lock_id(engine_lock_id, engine_lock_id_length)) { |
| 520 |
if (with_lock_data) { |
| 521 |
p_s_fill_lock_data(&lock_data_str, &lock, heap_no, &container); |
| 522 |
} else { |
| 523 |
lock_data_str = nullptr; |
| 524 |
} |
| 525 |
|
| 526 |
container.add_lock_row( |
| 527 |
g_engine, g_engine_length, engine_lock_id, |
| 528 |
engine_lock_id_length, trx_id, thread_id, event_id, |
| 529 |
parsed.schema.data(), parsed.schema.size(), parsed.table.data(), |
| 530 |
parsed.table.size(), parsed.partition.data(), |
| 531 |
parsed.partition.size(), parsed.subpartition.data(), |
| 532 |
parsed.subpartition.size(), index_name, index_name_length, |
| 533 |
identity, lock_mode_str, lock_type_str, lock_status_str, |
| 534 |
lock_data_str); |
| 535 |
found++; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
heap_no = lock_rec_find_next_set_bit(&lock, heap_no); |
| 540 |
} |
| 541 |
break; |
| 542 |
default: |
| 543 |
ut_error; |
| 544 |
} |
| 545 |
|
| 546 |
return found; |
| 547 |
} |
| 548 |
|
| 549 |
bool Innodb_data_lock_wait_iterator::scan( |
| 550 |
PSI_server_data_lock_wait_container *container) { |
| 551 |
if (!container->accept_engine(g_engine, g_engine_length)) { |
| 552 |
return true; |
| 553 |
} |
| 554 |
|
| 555 |
for (size_t found = 0; 0 == found;) { |
| 556 |
if (m_all_locks_iterator.iterate_over_next_batch([&](const lock_t &lock) { |
| 557 |
found += report(*container, lock, false); |
| 558 |
})) { |
| 559 |
return true; |
| 560 |
} |
| 561 |
} |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
void Innodb_data_lock_wait_iterator::fetch( |
| 566 |
PSI_server_data_lock_wait_container *container, |
| 567 |
const char *requesting_engine_lock_id, |
| 568 |
size_t requesting_engine_lock_id_length, |
| 569 |
const char *blocking_engine_lock_id, |
| 570 |
size_t blocking_engine_lock_id_length) { |
| 571 |
ut_ad(strnlen(requesting_engine_lock_id, |
| 572 |
requesting_engine_lock_id_length + 1) == |
| 573 |
requesting_engine_lock_id_length); |
| 574 |
ut_ad(strnlen(blocking_engine_lock_id, blocking_engine_lock_id_length + 1) == |
| 575 |
blocking_engine_lock_id_length); |
| 576 |
|
| 577 |
if (!container->accept_engine(g_engine, g_engine_length)) { |
| 578 |
return; |
| 579 |
} |
| 580 |
|
| 581 |
i_s_locks_row_t blocking_lock_row; |
| 582 |
const int blocking_lock_type = |
| 583 |
trx_i_s_parse_lock_id(blocking_engine_lock_id, &blocking_lock_row); |
| 584 |
if (0 == blocking_lock_type) { |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
find_lock_and_execute( |
| 589 |
requesting_engine_lock_id, |
| 590 |
[&](const lock_t &lock, int lock_type, ulint heap_no) { |
| 591 |
ut_ad(lock_type != LOCK_REC || heap_no == lock_rec_find_set_bit(&lock)); |
| 592 |
report(*container, lock, true, blocking_lock_row.lock_guid); |
| 593 |
}); |
| 594 |
return; |
| 595 |
} |
| 596 |
|
| 597 |
size_t Innodb_data_lock_wait_iterator::report( |
| 598 |
PSI_server_data_lock_wait_container &container, const lock_t &wait_lock, |
| 599 |
bool with_filter, const lock_guid_t &filter_blocking_lock_guid) { |
| 600 |
ut_ad(locksys::owns_lock_shard(&wait_lock)); |
| 601 |
if (!lock_is_waiting(wait_lock)) { |
| 602 |
return 0; |
| 603 |
} |
| 604 |
|
| 605 |
ulonglong requesting_thread_id; |
| 606 |
ulonglong requesting_event_id; |
| 607 |
const void *requesting_identity; |
| 608 |
char requesting_engine_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1]; |
| 609 |
size_t requesting_engine_lock_id_length; |
| 610 |
ulonglong blocking_trx_id; |
| 611 |
ulonglong blocking_thread_id; |
| 612 |
ulonglong blocking_event_id; |
| 613 |
const void *blocking_identity; |
| 614 |
char blocking_engine_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1]; |
| 615 |
size_t blocking_engine_lock_id_length; |
| 616 |
|
| 617 |
size_t found = 0; |
| 618 |
|
| 619 |
const auto requesting_record_type = lock_get_type(&wait_lock); |
| 620 |
const auto requesting_trx_id = lock_get_trx_id(&wait_lock); |
| 621 |
if (!container.accept_requesting_transaction_id(requesting_trx_id)) { |
| 622 |
return found; |
| 623 |
} |
| 624 |
|
| 625 |
lock_get_psi_event(&wait_lock, &requesting_thread_id, &requesting_event_id); |
| 626 |
if (!container.accept_requesting_thread_id_event_id(requesting_thread_id, |
| 627 |
requesting_event_id)) { |
| 628 |
return found; |
| 629 |
} |
| 630 |
|
| 631 |
const ulint heap_no = requesting_record_type == LOCK_REC |
| 632 |
? lock_rec_find_set_bit(&wait_lock) |
| 633 |
: ULINT_UNDEFINED; |
| 634 |
|
| 635 |
print_lock_id(wait_lock, heap_no, requesting_engine_lock_id, |
| 636 |
sizeof(requesting_engine_lock_id)); |
| 637 |
requesting_engine_lock_id_length = strlen(requesting_engine_lock_id); |
| 638 |
if (!container.accept_requesting_lock_id(requesting_engine_lock_id, |
| 639 |
requesting_engine_lock_id_length)) { |
| 640 |
return found; |
| 641 |
} |
| 642 |
|
| 643 |
requesting_identity = &wait_lock; |
| 644 |
locksys::find_blockers(wait_lock, [&](const lock_t &curr_lock) { |
| 645 |
ut_ad(locksys::owns_lock_shard(&curr_lock)); |
| 646 |
if (with_filter && lock_guid_t(curr_lock) != filter_blocking_lock_guid) { |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
blocking_trx_id = lock_get_trx_id(&curr_lock); |
| 651 |
if (!container.accept_blocking_transaction_id(blocking_trx_id)) { |
| 652 |
return false; |
| 653 |
} |
| 654 |
|
| 655 |
lock_get_psi_event(&curr_lock, &blocking_thread_id, &blocking_event_id); |
| 656 |
if (!container.accept_blocking_thread_id_event_id(blocking_thread_id, |
| 657 |
blocking_event_id)) { |
| 658 |
return false; |
| 659 |
} |
| 660 |
|
| 661 |
blocking_identity = &curr_lock; |
| 662 |
print_lock_id(curr_lock, heap_no, blocking_engine_lock_id, |
| 663 |
sizeof(blocking_engine_lock_id)); |
| 664 |
blocking_engine_lock_id_length = strlen(blocking_engine_lock_id); |
| 665 |
if (!container.accept_blocking_lock_id(blocking_engine_lock_id, |
| 666 |
blocking_engine_lock_id_length)) { |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
container.add_lock_wait_row( |
| 671 |
g_engine, g_engine_length, requesting_engine_lock_id, |
| 672 |
requesting_engine_lock_id_length, requesting_trx_id, |
| 673 |
requesting_thread_id, requesting_event_id, requesting_identity, |
| 674 |
blocking_engine_lock_id, blocking_engine_lock_id_length, |
| 675 |
blocking_trx_id, blocking_thread_id, blocking_event_id, |
| 676 |
blocking_identity); |
| 677 |
found++; |
| 678 |
return false; |
| 679 |
}); |
| 680 |
|
| 681 |
return found; |
| 682 |
} |