| 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 trx/trx0i_s.cc |
| 29 |
INFORMATION SCHEMA innodb_trx, innodb_locks and |
| 30 |
innodb_lock_waits tables fetch code. |
| 31 |
|
| 32 |
The code below fetches information needed to fill those |
| 33 |
3 dynamic tables and uploads it into a "transactions |
| 34 |
table cache" for later retrieval. |
| 35 |
|
| 36 |
Created July 17, 2007 Vasil Dimov |
| 37 |
*******************************************************/ |
| 38 |
|
| 39 |
/* Found during the build of 5.5.3 on Linux 2.4 and early 2.6 kernels: |
| 40 |
The includes "univ.i" -> "my_global.h" cause a different path |
| 41 |
to be taken further down with pthread functions and types, |
| 42 |
so they must come first. |
| 43 |
From the symptoms, this is related to bug#46587 in the MySQL bug DB. |
| 44 |
*/ |
| 45 |
|
| 46 |
#include <sql_class.h> |
| 47 |
#include <stdio.h> |
| 48 |
|
| 49 |
#include "buf0buf.h" |
| 50 |
#include "dict0dict.h" |
| 51 |
#include "ha0storage.h" |
| 52 |
#include "ha_prototypes.h" |
| 53 |
#include "hash0hash.h" |
| 54 |
#include "lock0iter.h" |
| 55 |
#include "lock0lock.h" |
| 56 |
#include "mem0mem.h" |
| 57 |
#include "mysql/plugin.h" |
| 58 |
#include "page0page.h" |
| 59 |
#include "rem0rec.h" |
| 60 |
#include "row0row.h" |
| 61 |
#include "srv0srv.h" |
| 62 |
#include "sync0rw.h" |
| 63 |
#include "sync0sync.h" |
| 64 |
#include "trx0i_s.h" |
| 65 |
#include "trx0sys.h" |
| 66 |
#include "trx0trx.h" |
| 67 |
#include "ut0mem.h" |
| 68 |
|
| 69 |
#include "storage/perfschema/pfs_data_lock.h" |
| 70 |
static_assert(pk_pos_data_lock::max_len > TRX_I_S_LOCK_ID_MAX_LEN, |
| 71 |
"pk_pos_data_lock::m_engine_lock_id must be able to hold " |
| 72 |
"engine_lock_id which has TRX_I_S_LOCK_ID_MAX_LEN chars"); |
| 73 |
static_assert(pk_pos_data_lock_wait::max_len > TRX_I_S_LOCK_ID_MAX_LEN, |
| 74 |
"pk_pos_data_lock_wait::m_requesting_engine_lock_id must be able " |
| 75 |
"to hold engine_lock_id which has TRX_I_S_LOCK_ID_MAX_LEN chars"); |
| 76 |
static_assert(pk_pos_data_lock_wait::max_len > TRX_I_S_LOCK_ID_MAX_LEN, |
| 77 |
"pk_pos_data_lock_wait::m_blocking_engine_lock_id must be able " |
| 78 |
"to hold engine_lock_id which has TRX_I_S_LOCK_ID_MAX_LEN chars"); |
| 79 |
|
| 80 |
/** Initial number of rows in the table cache */ |
| 81 |
constexpr uint32_t TABLE_CACHE_INITIAL_ROWSNUM = 1024; |
| 82 |
|
| 83 |
/** @brief The maximum number of chunks to allocate for a table cache. |
| 84 |
|
| 85 |
The rows of a table cache are stored in a set of chunks. When a new |
| 86 |
row is added a new chunk is allocated if necessary. Assuming that the |
| 87 |
first one is 1024 rows (TABLE_CACHE_INITIAL_ROWSNUM) and each |
| 88 |
subsequent is N/2 where N is the number of rows we have allocated till |
| 89 |
now, then 39th chunk would accommodate 1677416425 rows and all chunks |
| 90 |
would accommodate 3354832851 rows. */ |
| 91 |
constexpr uint32_t MEM_CHUNKS_IN_TABLE_CACHE = 39; |
| 92 |
|
| 93 |
/** Memory limit passed to ha_storage_put_memlim(). |
| 94 |
@param cache hash storage |
| 95 |
@return maximum allowed allocation size */ |
| 96 |
#define MAX_ALLOWED_FOR_STORAGE(cache) (TRX_I_S_MEM_LIMIT - (cache)->mem_allocd) |
| 97 |
|
| 98 |
/** Memory limit in table_cache_create_empty_row(). |
| 99 |
@param cache hash storage |
| 100 |
@return maximum allowed allocation size */ |
| 101 |
#define MAX_ALLOWED_FOR_ALLOC(cache) \ |
| 102 |
(TRX_I_S_MEM_LIMIT - (cache)->mem_allocd - \ |
| 103 |
ha_storage_get_size((cache)->storage)) |
| 104 |
|
| 105 |
/** Memory for each table in the intermediate buffer is allocated in |
| 106 |
separate chunks. These chunks are considered to be concatenated to |
| 107 |
represent one flat array of rows. */ |
| 108 |
struct i_s_mem_chunk_t { |
| 109 |
ulint offset; /*!< offset, in number of rows */ |
| 110 |
ulint rows_allocd; /*!< the size of this chunk, in number |
| 111 |
of rows */ |
| 112 |
void *base; /*!< start of the chunk */ |
| 113 |
}; |
| 114 |
|
| 115 |
/** This represents one table's cache. */ |
| 116 |
struct i_s_table_cache_t { |
| 117 |
ulint rows_used; /*!< number of used rows */ |
| 118 |
ulint rows_allocd; /*!< number of allocated rows */ |
| 119 |
ulint row_size; /*!< size of a single row */ |
| 120 |
i_s_mem_chunk_t chunks[MEM_CHUNKS_IN_TABLE_CACHE]; /*!< array of |
| 121 |
memory chunks that stores the |
| 122 |
rows */ |
| 123 |
}; |
| 124 |
|
| 125 |
/** Initial size of the cache storage */ |
| 126 |
constexpr uint32_t CACHE_STORAGE_INITIAL_SIZE = 1024; |
| 127 |
/** Number of hash cells in the cache storage */ |
| 128 |
constexpr uint32_t CACHE_STORAGE_HASH_CELLS = 2048; |
| 129 |
|
| 130 |
/** This structure describes the intermediate buffer */ |
| 131 |
struct trx_i_s_cache_t { |
| 132 |
rw_lock_t *rw_lock; /*!< read-write lock protecting |
| 133 |
the rest of this structure */ |
| 134 |
std::atomic<std::chrono::steady_clock::time_point> last_read{ |
| 135 |
std::chrono::steady_clock::time_point{}}; /*!< last time the cache was |
| 136 |
read; */ |
| 137 |
static_assert(decltype(last_read)::is_always_lock_free); |
| 138 |
|
| 139 |
i_s_table_cache_t innodb_trx; /*!< innodb_trx table */ |
| 140 |
i_s_table_cache_t innodb_locks; /*!< innodb_locks table */ |
| 141 |
|
| 142 |
/** storage for external volatile data that may become unavailable when we |
| 143 |
release exclusive global locksys latch or trx_sys->mutex */ |
| 144 |
ha_storage_t *storage; |
| 145 |
|
| 146 |
/** the amount of memory allocated with mem_alloc*() */ |
| 147 |
ulint mem_allocd; |
| 148 |
|
| 149 |
/** this is true if the memory limit was hit and thus the data in the cache is |
| 150 |
truncated */ |
| 151 |
bool is_truncated; |
| 152 |
}; |
| 153 |
|
| 154 |
/** This is the intermediate buffer where data needed to fill the |
| 155 |
INFORMATION SCHEMA tables is fetched and later retrieved by the C++ |
| 156 |
code in handler/i_s.cc. */ |
| 157 |
static trx_i_s_cache_t trx_i_s_cache_static; |
| 158 |
/** This is the intermediate buffer where data needed to fill the |
| 159 |
INFORMATION SCHEMA tables is fetched and later retrieved by the C++ |
| 160 |
code in handler/i_s.cc. */ |
| 161 |
trx_i_s_cache_t *trx_i_s_cache = &trx_i_s_cache_static; |
| 162 |
|
| 163 |
/** For a record lock that is in waiting state retrieves the only bit that |
| 164 |
is set, for a table lock returns ULINT_UNDEFINED. |
| 165 |
@return record number within the heap */ |
| 166 |
static ulint wait_lock_get_heap_no(const lock_t *lock) /*!< in: lock */ |
| 167 |
{ |
| 168 |
ulint ret; |
| 169 |
|
| 170 |
switch (lock_get_type(lock)) { |
| 171 |
case LOCK_REC: |
| 172 |
ret = lock_rec_find_set_bit(lock); |
| 173 |
ut_a(ret != ULINT_UNDEFINED); |
| 174 |
break; |
| 175 |
case LOCK_TABLE: |
| 176 |
ret = ULINT_UNDEFINED; |
| 177 |
break; |
| 178 |
default: |
| 179 |
ut_error; |
| 180 |
} |
| 181 |
|
| 182 |
return (ret); |
| 183 |
} |
| 184 |
|
| 185 |
/** Initializes the members of a table cache. */ |
| 186 |
static void table_cache_init( |
| 187 |
i_s_table_cache_t *table_cache, /*!< out: table cache */ |
| 188 |
size_t row_size) /*!< in: the size of a |
| 189 |
row */ |
| 190 |
{ |
| 191 |
ulint i; |
| 192 |
|
| 193 |
table_cache->rows_used = 0; |
| 194 |
table_cache->rows_allocd = 0; |
| 195 |
table_cache->row_size = row_size; |
| 196 |
|
| 197 |
for (i = 0; i < MEM_CHUNKS_IN_TABLE_CACHE; i++) { |
| 198 |
/* the memory is actually allocated in |
| 199 |
table_cache_create_empty_row() */ |
| 200 |
table_cache->chunks[i].base = nullptr; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** Frees a table cache. */ |
| 205 |
static void table_cache_free( |
| 206 |
i_s_table_cache_t *table_cache) /*!< in/out: table cache */ |
| 207 |
{ |
| 208 |
ulint i; |
| 209 |
|
| 210 |
for (i = 0; i < MEM_CHUNKS_IN_TABLE_CACHE; i++) { |
| 211 |
/* the memory is actually allocated in |
| 212 |
table_cache_create_empty_row() */ |
| 213 |
if (table_cache->chunks[i].base) { |
| 214 |
ut::free(table_cache->chunks[i].base); |
| 215 |
table_cache->chunks[i].base = nullptr; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** Returns an empty row from a table cache. The row is allocated if no more |
| 221 |
empty rows are available. The number of used rows is incremented. |
| 222 |
If the memory limit is hit then NULL is returned and nothing is |
| 223 |
allocated. |
| 224 |
@return empty row, or NULL if out of memory */ |
| 225 |
static void *table_cache_create_empty_row( |
| 226 |
i_s_table_cache_t *table_cache, /*!< in/out: table cache */ |
| 227 |
trx_i_s_cache_t *cache) /*!< in/out: cache to record |
| 228 |
how many bytes are |
| 229 |
allocated */ |
| 230 |
{ |
| 231 |
ulint i; |
| 232 |
void *row; |
| 233 |
|
| 234 |
ut_a(table_cache->rows_used <= table_cache->rows_allocd); |
| 235 |
|
| 236 |
if (table_cache->rows_used == table_cache->rows_allocd) { |
| 237 |
/* rows_used == rows_allocd means that new chunk needs |
| 238 |
to be allocated: either no more empty rows in the |
| 239 |
last allocated chunk or nothing has been allocated yet |
| 240 |
(rows_num == rows_allocd == 0); */ |
| 241 |
|
| 242 |
i_s_mem_chunk_t *chunk; |
| 243 |
ulint req_bytes; |
| 244 |
ulint got_bytes; |
| 245 |
ulint req_rows; |
| 246 |
ulint got_rows; |
| 247 |
|
| 248 |
/* find the first not allocated chunk */ |
| 249 |
for (i = 0; i < MEM_CHUNKS_IN_TABLE_CACHE; i++) { |
| 250 |
if (table_cache->chunks[i].base == nullptr) { |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/* i == MEM_CHUNKS_IN_TABLE_CACHE means that all chunks |
| 256 |
have been allocated :-X */ |
| 257 |
ut_a(i < MEM_CHUNKS_IN_TABLE_CACHE); |
| 258 |
|
| 259 |
/* allocate the chunk we just found */ |
| 260 |
|
| 261 |
if (i == 0) { |
| 262 |
/* first chunk, nothing is allocated yet */ |
| 263 |
req_rows = TABLE_CACHE_INITIAL_ROWSNUM; |
| 264 |
} else { |
| 265 |
/* Memory is increased by the formula |
| 266 |
new = old + old / 2; We are trying not to be |
| 267 |
aggressive here (= using the common new = old * 2) |
| 268 |
because the allocated memory will not be freed |
| 269 |
until InnoDB exit (it is reused). So it is better |
| 270 |
to once allocate the memory in more steps, but |
| 271 |
have less unused/wasted memory than to use less |
| 272 |
steps in allocation (which is done once in a |
| 273 |
lifetime) but end up with lots of unused/wasted |
| 274 |
memory. */ |
| 275 |
req_rows = table_cache->rows_allocd / 2; |
| 276 |
} |
| 277 |
req_bytes = req_rows * table_cache->row_size; |
| 278 |
|
| 279 |
if (req_bytes > MAX_ALLOWED_FOR_ALLOC(cache)) { |
| 280 |
return (nullptr); |
| 281 |
} |
| 282 |
|
| 283 |
chunk = &table_cache->chunks[i]; |
| 284 |
|
| 285 |
got_bytes = req_bytes; |
| 286 |
chunk->base = ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, req_bytes); |
| 287 |
|
| 288 |
got_rows = got_bytes / table_cache->row_size; |
| 289 |
|
| 290 |
cache->mem_allocd += got_bytes; |
| 291 |
|
| 292 |
#if 0 |
| 293 |
printf("allocating chunk %d req bytes=%lu, got bytes=%lu," |
| 294 |
" row size=%lu," |
| 295 |
" req rows=%lu, got rows=%lu\n", |
| 296 |
i, req_bytes, got_bytes, |
| 297 |
table_cache->row_size, |
| 298 |
req_rows, got_rows); |
| 299 |
#endif |
| 300 |
|
| 301 |
chunk->rows_allocd = got_rows; |
| 302 |
|
| 303 |
table_cache->rows_allocd += got_rows; |
| 304 |
|
| 305 |
/* adjust the offset of the next chunk */ |
| 306 |
if (i < MEM_CHUNKS_IN_TABLE_CACHE - 1) { |
| 307 |
table_cache->chunks[i + 1].offset = chunk->offset + chunk->rows_allocd; |
| 308 |
} |
| 309 |
|
| 310 |
/* return the first empty row in the newly allocated |
| 311 |
chunk */ |
| 312 |
row = chunk->base; |
| 313 |
} else { |
| 314 |
char *chunk_start; |
| 315 |
ulint offset; |
| 316 |
|
| 317 |
/* there is an empty row, no need to allocate new |
| 318 |
chunks */ |
| 319 |
|
| 320 |
/* find the first chunk that contains allocated but |
| 321 |
empty/unused rows */ |
| 322 |
for (i = 0; i < MEM_CHUNKS_IN_TABLE_CACHE; i++) { |
| 323 |
if (table_cache->chunks[i].offset + table_cache->chunks[i].rows_allocd > |
| 324 |
table_cache->rows_used) { |
| 325 |
break; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/* i == MEM_CHUNKS_IN_TABLE_CACHE means that all chunks |
| 330 |
are full, but |
| 331 |
table_cache->rows_used != table_cache->rows_allocd means |
| 332 |
exactly the opposite - there are allocated but |
| 333 |
empty/unused rows :-X */ |
| 334 |
ut_a(i < MEM_CHUNKS_IN_TABLE_CACHE); |
| 335 |
|
| 336 |
chunk_start = (char *)table_cache->chunks[i].base; |
| 337 |
offset = table_cache->rows_used - table_cache->chunks[i].offset; |
| 338 |
|
| 339 |
row = chunk_start + offset * table_cache->row_size; |
| 340 |
} |
| 341 |
|
| 342 |
table_cache->rows_used++; |
| 343 |
|
| 344 |
return (row); |
| 345 |
} |
| 346 |
|
| 347 |
#ifdef UNIV_DEBUG |
| 348 |
/** Validates a row in the locks cache. |
| 349 |
@return true if valid */ |
| 350 |
static bool i_s_locks_row_validate( |
| 351 |
const i_s_locks_row_t *row) /*!< in: row to validate */ |
| 352 |
{ |
| 353 |
ut_ad(row->lock_guid); |
| 354 |
ut_ad(row->lock_guid.m_trx_guid); |
| 355 |
ut_ad(row->lock_table_id != 0); |
| 356 |
|
| 357 |
if (row->lock_space == SPACE_UNKNOWN) { |
| 358 |
ut_ad(row->lock_page == FIL_NULL); |
| 359 |
ut_ad(row->lock_rec == ULINT_UNDEFINED); |
| 360 |
} else { |
| 361 |
ut_ad(row->lock_page != FIL_NULL); |
| 362 |
ut_ad(row->lock_rec != ULINT_UNDEFINED); |
| 363 |
} |
| 364 |
|
| 365 |
return true; |
| 366 |
} |
| 367 |
#endif /* UNIV_DEBUG */ |
| 368 |
|
| 369 |
/** Fills i_s_trx_row_t object. |
| 370 |
If memory can not be allocated then false is returned. |
| 371 |
@return false if allocation fails */ |
| 372 |
static bool fill_trx_row( |
| 373 |
i_s_trx_row_t *row, /*!< out: result object |
| 374 |
that's filled */ |
| 375 |
const trx_t *trx, /*!< in: transaction to |
| 376 |
get data from */ |
| 377 |
const i_s_locks_row_t *requested_lock_row, /*!< in: pointer to the |
| 378 |
corresponding row in |
| 379 |
innodb_locks if trx is |
| 380 |
waiting or NULL if trx |
| 381 |
is not waiting */ |
| 382 |
trx_i_s_cache_t *cache) /*!< in/out: cache into |
| 383 |
which to copy volatile |
| 384 |
strings */ |
| 385 |
{ |
| 386 |
size_t stmt_len; |
| 387 |
const char *s; |
| 388 |
|
| 389 |
/* We are going to read various trx->lock fields protected by trx->mutex */ |
| 390 |
ut_ad(trx_mutex_own(trx)); |
| 391 |
/* We are going to read TRX_WEIGHT, lock_number_of_rows_locked() and |
| 392 |
lock_number_of_tables_locked() which requires latching the lock_sys. |
| 393 |
Also, we need it to avoid reading temporary NULL value set to wait_lock by a |
| 394 |
B-tree page reorganization. */ |
| 395 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 396 |
|
| 397 |
row->trx_id = trx_get_id_for_print(trx); |
| 398 |
row->trx_started = trx->start_time.load(std::memory_order_relaxed); |
| 399 |
row->trx_state = trx_get_que_state_str(trx); |
| 400 |
row->requested_lock_row = requested_lock_row; |
| 401 |
ut_ad(requested_lock_row == nullptr || |
| 402 |
i_s_locks_row_validate(requested_lock_row)); |
| 403 |
|
| 404 |
if (trx->lock.wait_lock != nullptr) { |
| 405 |
ut_a(requested_lock_row != nullptr); |
| 406 |
row->trx_wait_started = trx->lock.wait_started; |
| 407 |
} else { |
| 408 |
ut_a(requested_lock_row == nullptr); |
| 409 |
row->trx_wait_started = {}; |
| 410 |
} |
| 411 |
|
| 412 |
row->trx_weight = static_cast<uintmax_t>(TRX_WEIGHT(trx)); |
| 413 |
if (trx->lock.que_state == TRX_QUE_LOCK_WAIT) { |
| 414 |
row->trx_schedule_weight.second = trx->lock.schedule_weight.load(); |
| 415 |
row->trx_schedule_weight.first = true; |
| 416 |
} else { |
| 417 |
row->trx_schedule_weight.first = false; |
| 418 |
} |
| 419 |
|
| 420 |
if (trx->mysql_thd == nullptr) { |
| 421 |
/* For internal transactions e.g., purge and transactions |
| 422 |
being recovered at startup there is no associated MySQL |
| 423 |
thread data structure. */ |
| 424 |
row->trx_mysql_thread_id = 0; |
| 425 |
row->trx_query = nullptr; |
| 426 |
goto thd_done; |
| 427 |
} |
| 428 |
|
| 429 |
row->trx_mysql_thread_id = thd_get_thread_id(trx->mysql_thd); |
| 430 |
|
| 431 |
char query[TRX_I_S_TRX_QUERY_MAX_LEN + 1]; |
| 432 |
stmt_len = innobase_get_stmt_safe(trx->mysql_thd, query, sizeof(query)); |
| 433 |
|
| 434 |
if (stmt_len > 0) { |
| 435 |
row->trx_query = static_cast<const char *>(ha_storage_put_memlim( |
| 436 |
cache->storage, query, stmt_len + 1, MAX_ALLOWED_FOR_STORAGE(cache))); |
| 437 |
|
| 438 |
row->trx_query_cs = innobase_get_charset(trx->mysql_thd); |
| 439 |
|
| 440 |
if (row->trx_query == nullptr) { |
| 441 |
return false; |
| 442 |
} |
| 443 |
} else { |
| 444 |
row->trx_query = nullptr; |
| 445 |
} |
| 446 |
|
| 447 |
thd_done: |
| 448 |
s = trx->op_info; |
| 449 |
|
| 450 |
if (s != nullptr && s[0] != '\0') { |
| 451 |
TRX_I_S_STRING_COPY(s, row->trx_operation_state, |
| 452 |
TRX_I_S_TRX_OP_STATE_MAX_LEN, cache); |
| 453 |
|
| 454 |
if (row->trx_operation_state == nullptr) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
} else { |
| 458 |
row->trx_operation_state = nullptr; |
| 459 |
} |
| 460 |
|
| 461 |
row->trx_tables_in_use = trx->n_mysql_tables_in_use; |
| 462 |
|
| 463 |
row->trx_tables_locked = lock_number_of_tables_locked(trx); |
| 464 |
|
| 465 |
row->trx_lock_structs = UT_LIST_GET_LEN(trx->lock.trx_locks); |
| 466 |
|
| 467 |
row->trx_lock_memory_bytes = mem_heap_get_size(trx->lock.lock_heap); |
| 468 |
|
| 469 |
row->trx_rows_locked = lock_number_of_rows_locked(&trx->lock); |
| 470 |
|
| 471 |
row->trx_rows_modified = trx->undo_no; |
| 472 |
|
| 473 |
row->trx_concurrency_tickets = trx->n_tickets_to_enter_innodb; |
| 474 |
|
| 475 |
switch (trx->isolation_level) { |
| 476 |
case TRX_ISO_READ_UNCOMMITTED: |
| 477 |
row->trx_isolation_level = "READ UNCOMMITTED"; |
| 478 |
break; |
| 479 |
case TRX_ISO_READ_COMMITTED: |
| 480 |
row->trx_isolation_level = "READ COMMITTED"; |
| 481 |
break; |
| 482 |
case TRX_ISO_REPEATABLE_READ: |
| 483 |
row->trx_isolation_level = "REPEATABLE READ"; |
| 484 |
break; |
| 485 |
case TRX_ISO_SERIALIZABLE: |
| 486 |
row->trx_isolation_level = "SERIALIZABLE"; |
| 487 |
break; |
| 488 |
/* Should not happen as TRX_ISO_READ_COMMITTED is default */ |
| 489 |
default: |
| 490 |
row->trx_isolation_level = "UNKNOWN"; |
| 491 |
} |
| 492 |
|
| 493 |
row->trx_unique_checks = trx->check_unique_secondary; |
| 494 |
|
| 495 |
row->trx_foreign_key_checks = trx->check_foreigns; |
| 496 |
|
| 497 |
s = trx->detailed_error; |
| 498 |
|
| 499 |
if (s != nullptr && s[0] != '\0') { |
| 500 |
TRX_I_S_STRING_COPY(s, row->trx_foreign_key_error, |
| 501 |
TRX_I_S_TRX_FK_ERROR_MAX_LEN, cache); |
| 502 |
|
| 503 |
if (row->trx_foreign_key_error == nullptr) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
} else { |
| 507 |
row->trx_foreign_key_error = nullptr; |
| 508 |
} |
| 509 |
|
| 510 |
row->trx_has_search_latch = trx->has_search_latch; |
| 511 |
|
| 512 |
row->trx_is_read_only = trx->read_only; |
| 513 |
|
| 514 |
row->trx_is_autocommit_non_locking = trx_is_autocommit_non_locking(trx); |
| 515 |
|
| 516 |
return true; |
| 517 |
} |
| 518 |
|
| 519 |
/** Format the nth field of "rec" and put it in "buf". The result is always |
| 520 |
NUL-terminated. Returns the number of bytes that were written to "buf" |
| 521 |
(including the terminating NUL). |
| 522 |
@return end of the result */ |
| 523 |
static ulint put_nth_field( |
| 524 |
char *buf, /*!< out: buffer */ |
| 525 |
ulint buf_size, /*!< in: buffer size in bytes */ |
| 526 |
ulint n, /*!< in: number of field */ |
| 527 |
const dict_index_t *index, /*!< in: index */ |
| 528 |
const rec_t *rec, /*!< in: record */ |
| 529 |
const ulint *offsets) /*!< in: record offsets, returned |
| 530 |
by rec_get_offsets() */ |
| 531 |
{ |
| 532 |
const byte *data; |
| 533 |
ulint data_len; |
| 534 |
dict_field_t *dict_field; |
| 535 |
ulint ret; |
| 536 |
|
| 537 |
ut_ad(rec_offs_validate(rec, nullptr, offsets)); |
| 538 |
|
| 539 |
if (buf_size == 0) { |
| 540 |
return (0); |
| 541 |
} |
| 542 |
|
| 543 |
ret = 0; |
| 544 |
|
| 545 |
if (n > 0) { |
| 546 |
/* we must append ", " before the actual data */ |
| 547 |
|
| 548 |
if (buf_size < 3) { |
| 549 |
buf[0] = '\0'; |
| 550 |
return (1); |
| 551 |
} |
| 552 |
|
| 553 |
memcpy(buf, ", ", 3); |
| 554 |
|
| 555 |
buf += 2; |
| 556 |
buf_size -= 2; |
| 557 |
ret += 2; |
| 558 |
} |
| 559 |
|
| 560 |
/* now buf_size >= 1 */ |
| 561 |
|
| 562 |
/* Here any field must be part of index key, which should not be |
| 563 |
added instantly, so no default value */ |
| 564 |
ut_ad(!rec_offs_nth_default(index, offsets, n)); |
| 565 |
|
| 566 |
data = rec_get_nth_field(index, rec, offsets, n, &data_len); |
| 567 |
|
| 568 |
dict_field = index->get_field(n); |
| 569 |
|
| 570 |
ret += |
| 571 |
row_raw_format((const char *)data, data_len, dict_field, buf, buf_size); |
| 572 |
|
| 573 |
return (ret); |
| 574 |
} |
| 575 |
|
| 576 |
/** Fill performance schema lock data. |
| 577 |
Create a string that represents the LOCK_DATA |
| 578 |
column, for a given lock record. |
| 579 |
@param[out] lock_data Lock data string |
| 580 |
@param[in] lock Lock to inspect |
| 581 |
@param[in] heap_no Lock heap number |
| 582 |
@param[in] container Data container to fill |
| 583 |
*/ |
| 584 |
void p_s_fill_lock_data(const char **lock_data, const lock_t *lock, |
| 585 |
ulint heap_no, |
| 586 |
PSI_server_data_lock_container *container) { |
| 587 |
ut_a(lock_get_type(lock) == LOCK_REC); |
| 588 |
|
| 589 |
switch (heap_no) { |
| 590 |
case PAGE_HEAP_NO_INFIMUM: |
| 591 |
*lock_data = "infimum pseudo-record"; |
| 592 |
return; |
| 593 |
case PAGE_HEAP_NO_SUPREMUM: |
| 594 |
*lock_data = "supremum pseudo-record"; |
| 595 |
return; |
| 596 |
} |
| 597 |
|
| 598 |
mtr_t mtr; |
| 599 |
|
| 600 |
const buf_block_t *block; |
| 601 |
const page_t *page; |
| 602 |
const rec_t *rec; |
| 603 |
const dict_index_t *index; |
| 604 |
ulint n_fields; |
| 605 |
char buf[TRX_I_S_LOCK_DATA_MAX_LEN]; |
| 606 |
ulint buf_used; |
| 607 |
ulint i; |
| 608 |
Rec_offsets rec_offsets; |
| 609 |
|
| 610 |
mtr_start(&mtr); |
| 611 |
|
| 612 |
block = buf_page_try_get(lock_rec_get_page_id(lock), UT_LOCATION_HERE, &mtr); |
| 613 |
|
| 614 |
if (block == nullptr) { |
| 615 |
*lock_data = nullptr; |
| 616 |
|
| 617 |
mtr_commit(&mtr); |
| 618 |
|
| 619 |
return; |
| 620 |
} |
| 621 |
|
| 622 |
page = reinterpret_cast<const page_t *>(buf_block_get_frame(block)); |
| 623 |
|
| 624 |
rec = page_find_rec_with_heap_no(page, heap_no); |
| 625 |
ut_a(rec != nullptr); |
| 626 |
|
| 627 |
index = lock_rec_get_index(lock); |
| 628 |
|
| 629 |
n_fields = dict_index_get_n_unique_in_tree(index); |
| 630 |
|
| 631 |
ut_a(n_fields > 0); |
| 632 |
|
| 633 |
const ulint *offsets = rec_offsets.compute(rec, index); |
| 634 |
|
| 635 |
/* format and store the data */ |
| 636 |
|
| 637 |
buf_used = 0; |
| 638 |
for (i = 0; i < n_fields; i++) { |
| 639 |
buf_used += put_nth_field(buf + buf_used, sizeof(buf) - buf_used, i, index, |
| 640 |
rec, offsets) - |
| 641 |
1; |
| 642 |
} |
| 643 |
|
| 644 |
*lock_data = container->cache_string(buf); |
| 645 |
|
| 646 |
mtr_commit(&mtr); |
| 647 |
} |
| 648 |
|
| 649 |
void fill_locks_row(i_s_locks_row_t *row, const lock_t *lock, ulint heap_no) { |
| 650 |
row->lock_guid = lock_guid_t(*lock); |
| 651 |
switch (lock_get_type(lock)) { |
| 652 |
case LOCK_REC: { |
| 653 |
const auto page_id = lock_rec_get_page_id(lock); |
| 654 |
row->lock_space = page_id.space(); |
| 655 |
row->lock_page = page_id.page_no(); |
| 656 |
row->lock_rec = heap_no; |
| 657 |
|
| 658 |
break; |
| 659 |
} |
| 660 |
case LOCK_TABLE: |
| 661 |
|
| 662 |
row->lock_space = SPACE_UNKNOWN; |
| 663 |
row->lock_page = FIL_NULL; |
| 664 |
row->lock_rec = ULINT_UNDEFINED; |
| 665 |
|
| 666 |
break; |
| 667 |
default: |
| 668 |
ut_error; |
| 669 |
} |
| 670 |
|
| 671 |
row->lock_table_id = lock_get_table_id(lock); |
| 672 |
|
| 673 |
ut_ad(i_s_locks_row_validate(row)); |
| 674 |
} |
| 675 |
|
| 676 |
/** Adds new element to the locks cache, enlarging it if necessary. |
| 677 |
Returns a pointer to the added row. If the row is already present then |
| 678 |
no row is added and a pointer to the existing row is returned. |
| 679 |
If row can not be allocated then NULL is returned. |
| 680 |
@return row */ |
| 681 |
static i_s_locks_row_t *add_lock_to_cache( |
| 682 |
trx_i_s_cache_t *cache, /*!< in/out: cache */ |
| 683 |
const lock_t *lock, /*!< in: the element to add */ |
| 684 |
ulint heap_no) /*!< in: lock's record number |
| 685 |
or ULINT_UNDEFINED if the lock |
| 686 |
is a table lock */ |
| 687 |
{ |
| 688 |
i_s_locks_row_t *dst_row; |
| 689 |
|
| 690 |
dst_row = (i_s_locks_row_t *)table_cache_create_empty_row( |
| 691 |
&cache->innodb_locks, cache); |
| 692 |
|
| 693 |
/* memory could not be allocated */ |
| 694 |
if (dst_row == nullptr) { |
| 695 |
return (nullptr); |
| 696 |
} |
| 697 |
|
| 698 |
fill_locks_row(dst_row, lock, heap_no); |
| 699 |
|
| 700 |
ut_ad(i_s_locks_row_validate(dst_row)); |
| 701 |
return (dst_row); |
| 702 |
} |
| 703 |
|
| 704 |
/** Adds transaction's relevant (important) locks to cache. |
| 705 |
If the transaction is waiting, then the wait lock is added to |
| 706 |
innodb_locks and a pointer to the added row is returned in |
| 707 |
requested_lock_row, otherwise requested_lock_row is set to NULL. |
| 708 |
If rows can not be allocated then false is returned and the value of |
| 709 |
requested_lock_row is undefined. |
| 710 |
@return false if allocation fails */ |
| 711 |
static bool add_trx_relevant_locks_to_cache( |
| 712 |
trx_i_s_cache_t *cache, /*!< in/out: cache */ |
| 713 |
const trx_t *trx, /*!< in: transaction */ |
| 714 |
i_s_locks_row_t **requested_lock_row) /*!< out: pointer to the |
| 715 |
requested lock row, or NULL or |
| 716 |
undefined */ |
| 717 |
{ |
| 718 |
/* We are about to iterate over locks for various tables/rows so we can not |
| 719 |
narrow the required latch to any specific shard, and thus require exclusive |
| 720 |
access to lock_sys. This is also needed to avoid observing NULL temporarily |
| 721 |
set to wait_lock during B-tree page reorganization. */ |
| 722 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 723 |
|
| 724 |
/* If transaction is waiting we add the wait lock and all locks |
| 725 |
from another transactions that are blocking the wait lock. */ |
| 726 |
if (trx->lock.que_state == TRX_QUE_LOCK_WAIT) { |
| 727 |
ulint wait_lock_heap_no; |
| 728 |
i_s_locks_row_t *blocking_lock_row; |
| 729 |
const lock_t *wait_lock = trx->lock.wait_lock; |
| 730 |
ut_a(wait_lock != nullptr); |
| 731 |
|
| 732 |
wait_lock_heap_no = wait_lock_get_heap_no(wait_lock); |
| 733 |
|
| 734 |
/* add the requested lock */ |
| 735 |
*requested_lock_row = |
| 736 |
add_lock_to_cache(cache, wait_lock, wait_lock_heap_no); |
| 737 |
|
| 738 |
/* memory could not be allocated */ |
| 739 |
if (*requested_lock_row == nullptr) { |
| 740 |
return false; |
| 741 |
} |
| 742 |
bool out_of_memory = false; |
| 743 |
/* then iterate over the locks before the wait lock and |
| 744 |
add the ones that are blocking it */ |
| 745 |
locksys::find_blockers(*wait_lock, [&](const lock_t &curr_lock) { |
| 746 |
blocking_lock_row = add_lock_to_cache(cache, &curr_lock, |
| 747 |
/* heap_no is the same |
| 748 |
for the wait and waited |
| 749 |
locks */ |
| 750 |
wait_lock_heap_no); |
| 751 |
|
| 752 |
/* memory could not be allocated */ |
| 753 |
if (blocking_lock_row == nullptr) { |
| 754 |
out_of_memory = true; |
| 755 |
return true; // stop iterating |
| 756 |
} |
| 757 |
return false; |
| 758 |
}); |
| 759 |
return !out_of_memory; |
| 760 |
} else { |
| 761 |
*requested_lock_row = nullptr; |
| 762 |
} |
| 763 |
|
| 764 |
return true; |
| 765 |
} |
| 766 |
|
| 767 |
/** Checks if the cache can safely be updated. |
| 768 |
@return true if can be updated */ |
| 769 |
static bool can_cache_be_updated(trx_i_s_cache_t *cache) /*!< in: cache */ |
| 770 |
{ |
| 771 |
/* Here we read cache->last_read without acquiring its mutex |
| 772 |
because last_read is only updated when a shared rw lock on the |
| 773 |
whole cache is being held (see trx_i_s_cache_end_read()) and |
| 774 |
we are currently holding an exclusive rw lock on the cache. |
| 775 |
So it is not possible for last_read to be updated while we are |
| 776 |
reading it. */ |
| 777 |
|
| 778 |
ut_ad(rw_lock_own(cache->rw_lock, RW_LOCK_X)); |
| 779 |
|
| 780 |
/** The minimum time that a cache must not be updated after it has been |
| 781 |
read for the last time. We use this technique to ensure that SELECTs which |
| 782 |
join several INFORMATION SCHEMA tables read the same version of the cache. */ |
| 783 |
constexpr std::chrono::milliseconds cache_min_idle_time{100}; |
| 784 |
|
| 785 |
return std::chrono::steady_clock::now() - cache->last_read.load() > |
| 786 |
cache_min_idle_time; |
| 787 |
} |
| 788 |
|
| 789 |
/** Declare a cache empty, preparing it to be filled up. Not all resources |
| 790 |
are freed because they can be reused. */ |
| 791 |
static void trx_i_s_cache_clear( |
| 792 |
trx_i_s_cache_t *cache) /*!< out: cache to clear */ |
| 793 |
{ |
| 794 |
cache->innodb_trx.rows_used = 0; |
| 795 |
cache->innodb_locks.rows_used = 0; |
| 796 |
|
| 797 |
ha_storage_empty(&cache->storage); |
| 798 |
} |
| 799 |
|
| 800 |
/** Fetches the data needed to fill the 3 INFORMATION SCHEMA tables into the |
| 801 |
table cache buffer. Cache must be locked for write. |
| 802 |
@param[in,out] cache the cache |
| 803 |
@param[in] trx_list the list to scan |
| 804 |
*/ |
| 805 |
template <typename Trx_list> |
| 806 |
static void fetch_data_into_cache_low(trx_i_s_cache_t *cache, |
| 807 |
Trx_list *trx_list) { |
| 808 |
/* We are going to iterate over many different shards of lock_sys so we need |
| 809 |
exclusive access */ |
| 810 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 811 |
constexpr bool rw_trx_list = |
| 812 |
std::is_same<Trx_list, decltype(trx_sys->rw_trx_list)>::value; |
| 813 |
|
| 814 |
static_assert( |
| 815 |
rw_trx_list || |
| 816 |
std::is_same<Trx_list, decltype(trx_sys->mysql_trx_list)>::value, |
| 817 |
"only rw_trx_list and mysql_trx_list are supported"); |
| 818 |
|
| 819 |
/* Iterate over the transaction list and add each one |
| 820 |
to innodb_trx's cache. We also add all locks that are relevant |
| 821 |
to each transaction into innodb_locks' and innodb_lock_waits' |
| 822 |
caches. */ |
| 823 |
|
| 824 |
for (auto trx : *trx_list) { |
| 825 |
i_s_trx_row_t *trx_row; |
| 826 |
i_s_locks_row_t *requested_lock_row; |
| 827 |
|
| 828 |
trx_mutex_enter(trx); |
| 829 |
|
| 830 |
/* Note: Read only transactions that modify temporary |
| 831 |
tables have a transaction ID. |
| 832 |
|
| 833 |
Note: auto-commit non-locking read-only transactions |
| 834 |
can have trx->state set from NOT_STARTED to ACTIVE and |
| 835 |
then from ACTIVE to NOT_STARTED with neither trx_sys->mutex |
| 836 |
nor trx->mutex acquired. However, as long as these transactions |
| 837 |
are members of mysql_trx_list they are not freed. For such |
| 838 |
transactions "trx_was_started(trx)" might be considered random, |
| 839 |
but whatever is its result, the code below handles that well |
| 840 |
(transaction won't release locks until its trx->mutex is acquired). |
| 841 |
|
| 842 |
Note: locking read-only transactions can have trx->state set from |
| 843 |
NOT_STARTED to ACTIVE with neither trx_sys->mutex nor trx->mutex |
| 844 |
acquired. However, such transactions need to be marked as COMMITTED |
| 845 |
before trx->state is set to NOT_STARTED and that is protected by the |
| 846 |
trx->mutex. Therefore the assertion assert_trx_nonlocking_or_in_list() |
| 847 |
should hold few lines below (note: the name of the assertion is wrong, |
| 848 |
because it actually checks if the transaction is autocommit nonlocking, |
| 849 |
whereas its name suggests that it only checks if the trx is nonlocking). */ |
| 850 |
if (!trx_was_started(trx) || |
| 851 |
(!rw_trx_list && trx->id != 0 && !trx->read_only)) { |
| 852 |
trx_mutex_exit(trx); |
| 853 |
continue; |
| 854 |
} |
| 855 |
|
| 856 |
assert_trx_nonlocking_or_in_list(trx); |
| 857 |
|
| 858 |
ut_ad(trx->in_rw_trx_list == rw_trx_list); |
| 859 |
|
| 860 |
if (!add_trx_relevant_locks_to_cache(cache, trx, &requested_lock_row)) { |
| 861 |
cache->is_truncated = true; |
| 862 |
trx_mutex_exit(trx); |
| 863 |
return; |
| 864 |
} |
| 865 |
|
| 866 |
trx_row = reinterpret_cast<i_s_trx_row_t *>( |
| 867 |
table_cache_create_empty_row(&cache->innodb_trx, cache)); |
| 868 |
|
| 869 |
/* memory could not be allocated */ |
| 870 |
if (trx_row == nullptr) { |
| 871 |
cache->is_truncated = true; |
| 872 |
trx_mutex_exit(trx); |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
if (!fill_trx_row(trx_row, trx, requested_lock_row, cache)) { |
| 877 |
/* memory could not be allocated */ |
| 878 |
--cache->innodb_trx.rows_used; |
| 879 |
cache->is_truncated = true; |
| 880 |
trx_mutex_exit(trx); |
| 881 |
return; |
| 882 |
} |
| 883 |
|
| 884 |
trx_mutex_exit(trx); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/** Fetches the data needed to fill the 3 INFORMATION SCHEMA tables into the |
| 889 |
table cache buffer. Cache must be locked for write. */ |
| 890 |
static void fetch_data_into_cache(trx_i_s_cache_t *cache) /*!< in/out: cache */ |
| 891 |
{ |
| 892 |
/* We are going to iterate over many different shards of lock_sys so we need |
| 893 |
exclusive access */ |
| 894 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 895 |
ut_ad(trx_sys_mutex_own()); |
| 896 |
|
| 897 |
trx_i_s_cache_clear(cache); |
| 898 |
|
| 899 |
/* Capture the state of the read-write transactions. This includes |
| 900 |
internal transactions too. They are not on mysql_trx_list */ |
| 901 |
fetch_data_into_cache_low(cache, &trx_sys->rw_trx_list); |
| 902 |
|
| 903 |
/* Capture the state of the read-only active transactions */ |
| 904 |
fetch_data_into_cache_low(cache, &trx_sys->mysql_trx_list); |
| 905 |
|
| 906 |
cache->is_truncated = false; |
| 907 |
} |
| 908 |
|
| 909 |
/** Update the transactions cache if it has not been read for some time. |
| 910 |
Called from handler/i_s.cc. |
| 911 |
@return 0 - fetched, 1 - not */ |
| 912 |
int trx_i_s_possibly_fetch_data_into_cache( |
| 913 |
trx_i_s_cache_t *cache) /*!< in/out: cache */ |
| 914 |
{ |
| 915 |
if (!can_cache_be_updated(cache)) { |
| 916 |
return (1); |
| 917 |
} |
| 918 |
|
| 919 |
{ |
| 920 |
/* We need to read trx_sys and record/table lock queues */ |
| 921 |
locksys::Global_exclusive_latch_guard guard{UT_LOCATION_HERE}; |
| 922 |
|
| 923 |
trx_sys_mutex_enter(); |
| 924 |
|
| 925 |
fetch_data_into_cache(cache); |
| 926 |
|
| 927 |
trx_sys_mutex_exit(); |
| 928 |
} |
| 929 |
|
| 930 |
return (0); |
| 931 |
} |
| 932 |
|
| 933 |
bool trx_i_s_cache_is_truncated(trx_i_s_cache_t *cache) { |
| 934 |
return (cache->is_truncated); |
| 935 |
} |
| 936 |
|
| 937 |
/** Initialize INFORMATION SCHEMA trx related cache. */ |
| 938 |
void trx_i_s_cache_init(trx_i_s_cache_t *cache) /*!< out: cache to init */ |
| 939 |
{ |
| 940 |
/* The latching is done in the following order: |
| 941 |
acquire trx_i_s_cache_t::rw_lock, X |
| 942 |
acquire locksys exclusive global latch |
| 943 |
acquire trx_sys mutex |
| 944 |
release trx_sys mutex |
| 945 |
release locksys exclusive global latch |
| 946 |
release trx_i_s_cache_t::rw_lock |
| 947 |
acquire trx_i_s_cache_t::rw_lock, S |
| 948 |
release trx_i_s_cache_t::rw_lock */ |
| 949 |
|
| 950 |
cache->rw_lock = static_cast<rw_lock_t *>( |
| 951 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*cache->rw_lock))); |
| 952 |
|
| 953 |
rw_lock_create(trx_i_s_cache_lock_key, cache->rw_lock, |
| 954 |
LATCH_ID_TRX_I_S_CACHE); |
| 955 |
|
| 956 |
cache->last_read = std::chrono::steady_clock::time_point{}; |
| 957 |
|
| 958 |
table_cache_init(&cache->innodb_trx, sizeof(i_s_trx_row_t)); |
| 959 |
table_cache_init(&cache->innodb_locks, sizeof(i_s_locks_row_t)); |
| 960 |
|
| 961 |
cache->storage = |
| 962 |
ha_storage_create(CACHE_STORAGE_INITIAL_SIZE, CACHE_STORAGE_HASH_CELLS); |
| 963 |
|
| 964 |
cache->mem_allocd = 0; |
| 965 |
|
| 966 |
cache->is_truncated = false; |
| 967 |
} |
| 968 |
|
| 969 |
/** Free the INFORMATION SCHEMA trx related cache. */ |
| 970 |
void trx_i_s_cache_free(trx_i_s_cache_t *cache) /*!< in, own: cache to free */ |
| 971 |
{ |
| 972 |
rw_lock_free(cache->rw_lock); |
| 973 |
ut::free(cache->rw_lock); |
| 974 |
cache->rw_lock = nullptr; |
| 975 |
|
| 976 |
ha_storage_free(cache->storage); |
| 977 |
table_cache_free(&cache->innodb_trx); |
| 978 |
table_cache_free(&cache->innodb_locks); |
| 979 |
} |
| 980 |
|
| 981 |
/** Issue a shared/read lock on the tables cache. */ |
| 982 |
void trx_i_s_cache_start_read(trx_i_s_cache_t *cache) /*!< in: cache */ |
| 983 |
{ |
| 984 |
rw_lock_s_lock(cache->rw_lock, UT_LOCATION_HERE); |
| 985 |
} |
| 986 |
|
| 987 |
/** Release a shared/read lock on the tables cache. */ |
| 988 |
void trx_i_s_cache_end_read(trx_i_s_cache_t *cache) /*!< in: cache */ |
| 989 |
{ |
| 990 |
ut_ad(rw_lock_own(cache->rw_lock, RW_LOCK_S)); |
| 991 |
|
| 992 |
/* update cache last read time */ |
| 993 |
cache->last_read.store(std::chrono::steady_clock::now()); |
| 994 |
|
| 995 |
rw_lock_s_unlock(cache->rw_lock); |
| 996 |
} |
| 997 |
|
| 998 |
/** Issue an exclusive/write lock on the tables cache. */ |
| 999 |
void trx_i_s_cache_start_write(trx_i_s_cache_t *cache) /*!< in: cache */ |
| 1000 |
{ |
| 1001 |
rw_lock_x_lock(cache->rw_lock, UT_LOCATION_HERE); |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** Release an exclusive/write lock on the tables cache. */ |
| 1005 |
void trx_i_s_cache_end_write(trx_i_s_cache_t *cache) /*!< in: cache */ |
| 1006 |
{ |
| 1007 |
ut_ad(rw_lock_own(cache->rw_lock, RW_LOCK_X)); |
| 1008 |
|
| 1009 |
rw_lock_x_unlock(cache->rw_lock); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/** Selects a INFORMATION SCHEMA table cache from the whole cache. |
| 1013 |
@return table cache */ |
| 1014 |
static i_s_table_cache_t *cache_select_table( |
| 1015 |
trx_i_s_cache_t *cache, /*!< in: whole cache */ |
| 1016 |
enum i_s_table table) /*!< in: which table */ |
| 1017 |
{ |
| 1018 |
i_s_table_cache_t *table_cache; |
| 1019 |
|
| 1020 |
ut_ad(rw_lock_own(cache->rw_lock, RW_LOCK_S) || |
| 1021 |
rw_lock_own(cache->rw_lock, RW_LOCK_X)); |
| 1022 |
|
| 1023 |
switch (table) { |
| 1024 |
case I_S_INNODB_TRX: |
| 1025 |
table_cache = &cache->innodb_trx; |
| 1026 |
break; |
| 1027 |
default: |
| 1028 |
ut_error; |
| 1029 |
} |
| 1030 |
|
| 1031 |
return (table_cache); |
| 1032 |
} |
| 1033 |
|
| 1034 |
/** Retrieves the number of used rows in the cache for a given |
| 1035 |
INFORMATION SCHEMA table. |
| 1036 |
@return number of rows */ |
| 1037 |
ulint trx_i_s_cache_get_rows_used(trx_i_s_cache_t *cache, /*!< in: cache */ |
| 1038 |
enum i_s_table table) /*!< in: which table */ |
| 1039 |
{ |
| 1040 |
i_s_table_cache_t *table_cache; |
| 1041 |
|
| 1042 |
table_cache = cache_select_table(cache, table); |
| 1043 |
|
| 1044 |
return (table_cache->rows_used); |
| 1045 |
} |
| 1046 |
|
| 1047 |
/** Retrieves the nth row (zero-based) in the cache for a given |
| 1048 |
INFORMATION SCHEMA table. |
| 1049 |
@return row */ |
| 1050 |
void *trx_i_s_cache_get_nth_row(trx_i_s_cache_t *cache, /*!< in: cache */ |
| 1051 |
enum i_s_table table, /*!< in: which table */ |
| 1052 |
ulint n) /*!< in: row number */ |
| 1053 |
{ |
| 1054 |
i_s_table_cache_t *table_cache; |
| 1055 |
ulint i; |
| 1056 |
void *row; |
| 1057 |
|
| 1058 |
table_cache = cache_select_table(cache, table); |
| 1059 |
|
| 1060 |
ut_a(n < table_cache->rows_used); |
| 1061 |
|
| 1062 |
row = nullptr; |
| 1063 |
|
| 1064 |
for (i = 0; i < MEM_CHUNKS_IN_TABLE_CACHE; i++) { |
| 1065 |
if (table_cache->chunks[i].offset + table_cache->chunks[i].rows_allocd > |
| 1066 |
n) { |
| 1067 |
row = (char *)table_cache->chunks[i].base + |
| 1068 |
(n - table_cache->chunks[i].offset) * table_cache->row_size; |
| 1069 |
break; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
ut_a(row != nullptr); |
| 1074 |
|
| 1075 |
return (row); |
| 1076 |
} |
| 1077 |
constexpr const char *LOCK_RECORD_ID_FORMAT = UINT64PF |
| 1078 |
":" UINT64PF ":" SPACE_ID_PF ":" PAGE_NO_PF ":" ULINTPF ":" UINT64PF; |
| 1079 |
constexpr const int LOCK_RECORD_ID_FORMAT_PARTS = 6; |
| 1080 |
constexpr const char *LOCK_TABLE_ID_FORMAT = |
| 1081 |
UINT64PF ":" UINT64PF ":" UINT64PF ":" UINT64PF; |
| 1082 |
constexpr const int LOCK_TABLE_ID_FORMAT_PARTS = 4; |
| 1083 |
|
| 1084 |
char *trx_i_s_create_lock_id(const i_s_locks_row_t &row, char *lock_id, |
| 1085 |
size_t lock_id_size) { |
| 1086 |
int res_len; |
| 1087 |
|
| 1088 |
/* Please adjust TRX_I_S_LOCK_ID_MAX_LEN if you change this. |
| 1089 |
The reason lock_guid.* fields are not being printed next to each other, |
| 1090 |
and thus the reason why printing them is not a member function of lock_guit_t, |
| 1091 |
is that we want the natural lexicographic order on LOCK_ENGINE_ID to be useful |
| 1092 |
for humans and it is much more useful if the rows are sorted by space, page |
| 1093 |
and heap_no, or by table, rather than by immutable id. */ |
| 1094 |
|
| 1095 |
if (row.lock_space != SPACE_UNKNOWN) { |
| 1096 |
/* record lock */ |
| 1097 |
res_len = |
| 1098 |
snprintf(lock_id, lock_id_size, LOCK_RECORD_ID_FORMAT, |
| 1099 |
row.lock_guid.m_trx_guid.m_immutable_id, |
| 1100 |
row.lock_guid.m_trx_guid.m_version, row.lock_space, |
| 1101 |
row.lock_page, row.lock_rec, row.lock_guid.m_immutable_id); |
| 1102 |
} else { |
| 1103 |
/* table lock */ |
| 1104 |
res_len = snprintf(lock_id, lock_id_size, LOCK_TABLE_ID_FORMAT, |
| 1105 |
row.lock_guid.m_trx_guid.m_immutable_id, |
| 1106 |
row.lock_guid.m_trx_guid.m_version, row.lock_table_id, |
| 1107 |
row.lock_guid.m_immutable_id); |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* the typecast is safe because snprintf(3) never returns |
| 1111 |
negative result */ |
| 1112 |
ut_a(res_len >= 0); |
| 1113 |
ut_a((size_t)res_len < lock_id_size); |
| 1114 |
|
| 1115 |
return lock_id; |
| 1116 |
} |
| 1117 |
|
| 1118 |
int trx_i_s_parse_lock_id(const char *lock_id, i_s_locks_row_t *row) { |
| 1119 |
if (sscanf(lock_id, LOCK_RECORD_ID_FORMAT, |
| 1120 |
&row->lock_guid.m_trx_guid.m_immutable_id, |
| 1121 |
&row->lock_guid.m_trx_guid.m_version, &row->lock_space, |
| 1122 |
&row->lock_page, &row->lock_rec, |
| 1123 |
&row->lock_guid.m_immutable_id) == LOCK_RECORD_ID_FORMAT_PARTS) { |
| 1124 |
return LOCK_REC; |
| 1125 |
} |
| 1126 |
if (sscanf(lock_id, LOCK_TABLE_ID_FORMAT, |
| 1127 |
&row->lock_guid.m_trx_guid.m_immutable_id, |
| 1128 |
&row->lock_guid.m_trx_guid.m_version, &row->lock_table_id, |
| 1129 |
&row->lock_guid.m_immutable_id) == LOCK_TABLE_ID_FORMAT_PARTS) { |
| 1130 |
return LOCK_TABLE; |
| 1131 |
} |
| 1132 |
static_assert(LOCK_TABLE != 0 && LOCK_REC != 0); |
| 1133 |
return 0; |
| 1134 |
} |