MySQL 8.0.39
Source Code Documentation
trx0i_s.cc File Reference

INFORMATION SCHEMA innodb_trx, innodb_locks and innodb_lock_waits tables fetch code. More...

#include <sql_class.h>
#include <stdio.h>
#include "buf0buf.h"
#include "dict0dict.h"
#include "ha0storage.h"
#include "ha_prototypes.h"
#include "hash0hash.h"
#include "lock0iter.h"
#include "lock0lock.h"
#include "mem0mem.h"
#include "mysql/plugin.h"
#include "page0page.h"
#include "rem0rec.h"
#include "row0row.h"
#include "srv0srv.h"
#include "sync0rw.h"
#include "sync0sync.h"
#include "trx0i_s.h"
#include "trx0sys.h"
#include "trx0trx.h"
#include "ut0mem.h"
#include "storage/perfschema/pfs_data_lock.h"

Classes

struct  i_s_mem_chunk_t
 Memory for each table in the intermediate buffer is allocated in separate chunks. More...
 
struct  i_s_table_cache_t
 This represents one table's cache. More...
 
struct  trx_i_s_cache_t
 This structure describes the intermediate buffer. More...
 

Macros

#define MAX_ALLOWED_FOR_STORAGE(cache)   (TRX_I_S_MEM_LIMIT - (cache)->mem_allocd)
 Memory limit passed to ha_storage_put_memlim(). More...
 
#define MAX_ALLOWED_FOR_ALLOC(cache)
 Memory limit in table_cache_create_empty_row(). More...
 

Functions

static ulint wait_lock_get_heap_no (const lock_t *lock)
 For a record lock that is in waiting state retrieves the only bit that is set, for a table lock returns ULINT_UNDEFINED. More...
 
static void table_cache_init (i_s_table_cache_t *table_cache, size_t row_size)
 Initializes the members of a table cache. More...
 
static void table_cache_free (i_s_table_cache_t *table_cache)
 Frees a table cache. More...
 
static void * table_cache_create_empty_row (i_s_table_cache_t *table_cache, trx_i_s_cache_t *cache)
 Returns an empty row from a table cache. More...
 
static bool i_s_locks_row_validate (const i_s_locks_row_t *row)
 Validates a row in the locks cache. More...
 
static bool fill_trx_row (i_s_trx_row_t *row, const trx_t *trx, const i_s_locks_row_t *requested_lock_row, trx_i_s_cache_t *cache)
 Fills i_s_trx_row_t object. More...
 
static ulint put_nth_field (char *buf, ulint buf_size, ulint n, const dict_index_t *index, const rec_t *rec, const ulint *offsets)
 Format the nth field of "rec" and put it in "buf". More...
 
void p_s_fill_lock_data (const char **lock_data, const lock_t *lock, ulint heap_no, PSI_server_data_lock_container *container)
 Fill performance schema lock data. More...
 
void fill_locks_row (i_s_locks_row_t *row, const lock_t *lock, ulint heap_no)
 Fills i_s_locks_row_t object with data about the lock. More...
 
static i_s_locks_row_tadd_lock_to_cache (trx_i_s_cache_t *cache, const lock_t *lock, ulint heap_no)
 Adds new element to the locks cache, enlarging it if necessary. More...
 
static bool add_trx_relevant_locks_to_cache (trx_i_s_cache_t *cache, const trx_t *trx, i_s_locks_row_t **requested_lock_row)
 Adds transaction's relevant (important) locks to cache. More...
 
static bool can_cache_be_updated (trx_i_s_cache_t *cache)
 Checks if the cache can safely be updated. More...
 
static void trx_i_s_cache_clear (trx_i_s_cache_t *cache)
 Declare a cache empty, preparing it to be filled up. More...
 
template<typename Trx_list >
static void fetch_data_into_cache_low (trx_i_s_cache_t *cache, Trx_list *trx_list)
 Fetches the data needed to fill the 3 INFORMATION SCHEMA tables into the table cache buffer. More...
 
static void fetch_data_into_cache (trx_i_s_cache_t *cache)
 Fetches the data needed to fill the 3 INFORMATION SCHEMA tables into the table cache buffer. More...
 
int trx_i_s_possibly_fetch_data_into_cache (trx_i_s_cache_t *cache)
 Update the transactions cache if it has not been read for some time. More...
 
bool trx_i_s_cache_is_truncated (trx_i_s_cache_t *cache)
 Returns true if the data in the cache is truncated due to the memory limit posed by TRX_I_S_MEM_LIMIT. More...
 
void trx_i_s_cache_init (trx_i_s_cache_t *cache)
 Initialize INFORMATION SCHEMA trx related cache. More...
 
void trx_i_s_cache_free (trx_i_s_cache_t *cache)
 Free the INFORMATION SCHEMA trx related cache. More...
 
void trx_i_s_cache_start_read (trx_i_s_cache_t *cache)
 Issue a shared/read lock on the tables cache. More...
 
void trx_i_s_cache_end_read (trx_i_s_cache_t *cache)
 Release a shared/read lock on the tables cache. More...
 
void trx_i_s_cache_start_write (trx_i_s_cache_t *cache)
 Issue an exclusive/write lock on the tables cache. More...
 
void trx_i_s_cache_end_write (trx_i_s_cache_t *cache)
 Release an exclusive/write lock on the tables cache. More...
 
static i_s_table_cache_tcache_select_table (trx_i_s_cache_t *cache, enum i_s_table table)
 Selects a INFORMATION SCHEMA table cache from the whole cache. More...
 
ulint trx_i_s_cache_get_rows_used (trx_i_s_cache_t *cache, enum i_s_table table)
 Retrieves the number of used rows in the cache for a given INFORMATION SCHEMA table. More...
 
void * trx_i_s_cache_get_nth_row (trx_i_s_cache_t *cache, enum i_s_table table, ulint n)
 Retrieves the nth row (zero-based) in the cache for a given INFORMATION SCHEMA table. More...
 
char * trx_i_s_create_lock_id (const i_s_locks_row_t *row, char *lock_id, ulint lock_id_size)
 Crafts a lock id string from a i_s_locks_row_t object. More...
 
int trx_i_s_parse_lock_id (const char *lock_id, i_s_locks_row_t *row)
 Parses lock id into row. More...
 

Variables

constexpr uint32_t TABLE_CACHE_INITIAL_ROWSNUM = 1024
 Initial number of rows in the table cache. More...
 
constexpr uint32_t MEM_CHUNKS_IN_TABLE_CACHE = 39
 The maximum number of chunks to allocate for a table cache. More...
 
constexpr uint32_t CACHE_STORAGE_INITIAL_SIZE = 1024
 Initial size of the cache storage. More...
 
constexpr uint32_t CACHE_STORAGE_HASH_CELLS = 2048
 Number of hash cells in the cache storage. More...
 
static trx_i_s_cache_t trx_i_s_cache_static
 This is the intermediate buffer where data needed to fill the INFORMATION SCHEMA tables is fetched and later retrieved by the C++ code in handler/i_s.cc. More...
 
trx_i_s_cache_ttrx_i_s_cache = &trx_i_s_cache_static
 This is the intermediate buffer where data needed to fill the INFORMATION SCHEMA tables is fetched and later retrieved by the C++ code in handler/i_s.cc. More...
 
constexpr const char * LOCK_RECORD_ID_FORMAT
 
constexpr const char * LOCK_TABLE_ID_FORMAT = UINT64PF ":" UINT64PF ":" UINT64PF
 

Detailed Description

INFORMATION SCHEMA innodb_trx, innodb_locks and innodb_lock_waits tables fetch code.

The code below fetches information needed to fill those 3 dynamic tables and uploads it into a "transactions table cache" for later retrieval.

Created July 17, 2007 Vasil Dimov

Macro Definition Documentation

◆ MAX_ALLOWED_FOR_ALLOC

#define MAX_ALLOWED_FOR_ALLOC (   cache)
Value:
(TRX_I_S_MEM_LIMIT - (cache)->mem_allocd - \
ha_storage_get_size((cache)->storage))
static ulint ha_storage_get_size(const ha_storage_t *storage)
Gets the size of the memory used by a storage.
constexpr uint32_t TRX_I_S_MEM_LIMIT
The maximum amount of memory that can be consumed by innodb_trx, innodb_locks and innodb_lock_waits i...
Definition: trx0i_s.h:49

Memory limit in table_cache_create_empty_row().

Parameters
cachehash storage
Returns
maximum allowed allocation size

◆ MAX_ALLOWED_FOR_STORAGE

#define MAX_ALLOWED_FOR_STORAGE (   cache)    (TRX_I_S_MEM_LIMIT - (cache)->mem_allocd)

Memory limit passed to ha_storage_put_memlim().

Parameters
cachehash storage
Returns
maximum allowed allocation size

Function Documentation

◆ add_lock_to_cache()

static i_s_locks_row_t * add_lock_to_cache ( trx_i_s_cache_t cache,
const lock_t lock,
ulint  heap_no 
)
static

Adds new element to the locks cache, enlarging it if necessary.

Returns a pointer to the added row. If the row is already present then no row is added and a pointer to the existing row is returned. If row can not be allocated then NULL is returned.

Returns
row
Parameters
cachein/out: cache
lockin: the element to add
heap_noin: lock's record number or ULINT_UNDEFINED if the lock is a table lock

◆ add_trx_relevant_locks_to_cache()

static bool add_trx_relevant_locks_to_cache ( trx_i_s_cache_t cache,
const trx_t trx,
i_s_locks_row_t **  requested_lock_row 
)
static

Adds transaction's relevant (important) locks to cache.

If the transaction is waiting, then the wait lock is added to innodb_locks and a pointer to the added row is returned in requested_lock_row, otherwise requested_lock_row is set to NULL. If rows can not be allocated then false is returned and the value of requested_lock_row is undefined.

Returns
false if allocation fails
Parameters
cachein/out: cache
trxin: transaction
requested_lock_rowout: pointer to the requested lock row, or NULL or undefined

◆ cache_select_table()

static i_s_table_cache_t * cache_select_table ( trx_i_s_cache_t cache,
enum i_s_table  table 
)
static

Selects a INFORMATION SCHEMA table cache from the whole cache.

Returns
table cache
Parameters
cachein: whole cache
tablein: which table

◆ can_cache_be_updated()

static bool can_cache_be_updated ( trx_i_s_cache_t cache)
static

Checks if the cache can safely be updated.

Returns
true if can be updated

The minimum time that a cache must not be updated after it has been read for the last time. We use this technique to ensure that SELECTs which join several INFORMATION SCHEMA tables read the same version of the cache.

Parameters
cachein: cache

◆ fetch_data_into_cache()

static void fetch_data_into_cache ( trx_i_s_cache_t cache)
static

Fetches the data needed to fill the 3 INFORMATION SCHEMA tables into the table cache buffer.

Cache must be locked for write.

Parameters
cachein/out: cache

◆ fetch_data_into_cache_low()

template<typename Trx_list >
static void fetch_data_into_cache_low ( trx_i_s_cache_t cache,
Trx_list *  trx_list 
)
static

Fetches the data needed to fill the 3 INFORMATION SCHEMA tables into the table cache buffer.

Cache must be locked for write.

Parameters
[in,out]cachethe cache
[in]trx_listthe list to scan

◆ fill_locks_row()

void fill_locks_row ( i_s_locks_row_t row,
const lock_t lock,
ulint  heap_no 
)

Fills i_s_locks_row_t object with data about the lock.

Parameters
[out]rowResult object that's filled
[in]lockLock to get data from
[in]heap_noLock's record number or ULINT_UNDEFINED if the lock is a table lock

◆ fill_trx_row()

static bool fill_trx_row ( i_s_trx_row_t row,
const trx_t trx,
const i_s_locks_row_t requested_lock_row,
trx_i_s_cache_t cache 
)
static

Fills i_s_trx_row_t object.

If memory can not be allocated then false is returned.

Returns
false if allocation fails
Parameters
rowout: result object that's filled
trxin: transaction to get data from
requested_lock_rowin: pointer to the corresponding row in innodb_locks if trx is waiting or NULL if trx is not waiting
cachein/out: cache into which to copy volatile strings

◆ i_s_locks_row_validate()

static bool i_s_locks_row_validate ( const i_s_locks_row_t row)
static

Validates a row in the locks cache.

Returns
true if valid
Parameters
rowin: row to validate

◆ p_s_fill_lock_data()

void p_s_fill_lock_data ( const char **  lock_data,
const lock_t lock,
ulint  heap_no,
PSI_server_data_lock_container container 
)

Fill performance schema lock data.

Create a string that represents the LOCK_DATA column, for a given lock record.

Parameters
[out]lock_dataLock data string
[in]lockLock to inspect
[in]heap_noLock heap number
[in]containerData container to fill

◆ put_nth_field()

static ulint put_nth_field ( char *  buf,
ulint  buf_size,
ulint  n,
const dict_index_t index,
const rec_t rec,
const ulint offsets 
)
static

Format the nth field of "rec" and put it in "buf".

The result is always NUL-terminated. Returns the number of bytes that were written to "buf" (including the terminating NUL).

Returns
end of the result
Parameters
bufout: buffer
buf_sizein: buffer size in bytes
nin: number of field
indexin: index
recin: record
offsetsin: record offsets, returned by rec_get_offsets()

◆ table_cache_create_empty_row()

static void * table_cache_create_empty_row ( i_s_table_cache_t table_cache,
trx_i_s_cache_t cache 
)
static

Returns an empty row from a table cache.

The row is allocated if no more empty rows are available. The number of used rows is incremented. If the memory limit is hit then NULL is returned and nothing is allocated.

Returns
empty row, or NULL if out of memory
Parameters
table_cachein/out: table cache
cachein/out: cache to record how many bytes are allocated

◆ table_cache_free()

static void table_cache_free ( i_s_table_cache_t table_cache)
static

Frees a table cache.

Parameters
table_cachein/out: table cache

◆ table_cache_init()

static void table_cache_init ( i_s_table_cache_t table_cache,
size_t  row_size 
)
static

Initializes the members of a table cache.

Parameters
table_cacheout: table cache
row_sizein: the size of a row

◆ trx_i_s_cache_clear()

static void trx_i_s_cache_clear ( trx_i_s_cache_t cache)
static

Declare a cache empty, preparing it to be filled up.

Not all resources are freed because they can be reused.

Parameters
cacheout: cache to clear

◆ trx_i_s_cache_end_read()

void trx_i_s_cache_end_read ( trx_i_s_cache_t cache)

Release a shared/read lock on the tables cache.

in: cache

Parameters
cachein: cache

◆ trx_i_s_cache_end_write()

void trx_i_s_cache_end_write ( trx_i_s_cache_t cache)

Release an exclusive/write lock on the tables cache.

in: cache

Parameters
cachein: cache

◆ trx_i_s_cache_free()

void trx_i_s_cache_free ( trx_i_s_cache_t cache)

Free the INFORMATION SCHEMA trx related cache.

in/out: cache to free

Parameters
cachein, own: cache to free

◆ trx_i_s_cache_get_nth_row()

void * trx_i_s_cache_get_nth_row ( trx_i_s_cache_t cache,
enum i_s_table  table,
ulint  n 
)

Retrieves the nth row (zero-based) in the cache for a given INFORMATION SCHEMA table.

Retrieves the nth row in the cache for a given INFORMATION SCHEMA table.

Returns
row
Parameters
cachein: cache
tablein: which table
nin: row number

◆ trx_i_s_cache_get_rows_used()

ulint trx_i_s_cache_get_rows_used ( trx_i_s_cache_t cache,
enum i_s_table  table 
)

Retrieves the number of used rows in the cache for a given INFORMATION SCHEMA table.

Returns
number of rows
Parameters
cachein: cache
tablein: which table

◆ trx_i_s_cache_init()

void trx_i_s_cache_init ( trx_i_s_cache_t cache)

Initialize INFORMATION SCHEMA trx related cache.

out: cache to init

Parameters
cacheout: cache to init

◆ trx_i_s_cache_is_truncated()

bool trx_i_s_cache_is_truncated ( trx_i_s_cache_t cache)

Returns true if the data in the cache is truncated due to the memory limit posed by TRX_I_S_MEM_LIMIT.

Parameters
[in]cacheThe cache
Returns
true if truncated

◆ trx_i_s_cache_start_read()

void trx_i_s_cache_start_read ( trx_i_s_cache_t cache)

Issue a shared/read lock on the tables cache.

in: cache

Parameters
cachein: cache

◆ trx_i_s_cache_start_write()

void trx_i_s_cache_start_write ( trx_i_s_cache_t cache)

Issue an exclusive/write lock on the tables cache.

in: cache

Parameters
cachein: cache

◆ trx_i_s_create_lock_id()

char * trx_i_s_create_lock_id ( const i_s_locks_row_t row,
char *  lock_id,
ulint  lock_id_size 
)

Crafts a lock id string from a i_s_locks_row_t object.

Returns its second argument. This function aborts if there is not enough space in lock_id. Be sure to provide at least TRX_I_S_LOCK_ID_MAX_LEN + 1 if you want to be 100% sure that it will not abort.

Returns
resulting lock id
Parameters
rowin: innodb_locks row
lock_idout: resulting lock_id
lock_id_sizein: size of the lock id buffer

◆ trx_i_s_parse_lock_id()

int trx_i_s_parse_lock_id ( const char *  lock_id,
i_s_locks_row_t row 
)

Parses lock id into row.

Parameters
[in]lock_idLock id generated with trx_i_s_create_lock_id
[out]rowRow to be filled in with data
Returns
LOCK_REC, LOCK_TABLE or 0 if failed to parse

◆ trx_i_s_possibly_fetch_data_into_cache()

int trx_i_s_possibly_fetch_data_into_cache ( trx_i_s_cache_t cache)

Update the transactions cache if it has not been read for some time.

Called from handler/i_s.cc.

Returns
0 - fetched, 1 - not
Parameters
cachein/out: cache

◆ wait_lock_get_heap_no()

static ulint wait_lock_get_heap_no ( const lock_t lock)
static

For a record lock that is in waiting state retrieves the only bit that is set, for a table lock returns ULINT_UNDEFINED.

Returns
record number within the heap
Parameters
lockin: lock

Variable Documentation

◆ CACHE_STORAGE_HASH_CELLS

constexpr uint32_t CACHE_STORAGE_HASH_CELLS = 2048
constexpr

Number of hash cells in the cache storage.

◆ CACHE_STORAGE_INITIAL_SIZE

constexpr uint32_t CACHE_STORAGE_INITIAL_SIZE = 1024
constexpr

Initial size of the cache storage.

◆ LOCK_RECORD_ID_FORMAT

constexpr const char* LOCK_RECORD_ID_FORMAT
constexpr
Initial value:
=
#define UINT64PF
Definition: univ.i:396
#define PAGE_NO_PF
Definition: univ.i:453
#define ULINTPF
Definition: univ.i:411
#define SPACE_ID_PF
Definition: univ.i:451

◆ LOCK_TABLE_ID_FORMAT

constexpr const char* LOCK_TABLE_ID_FORMAT = UINT64PF ":" UINT64PF ":" UINT64PF
constexpr

◆ MEM_CHUNKS_IN_TABLE_CACHE

constexpr uint32_t MEM_CHUNKS_IN_TABLE_CACHE = 39
constexpr

The maximum number of chunks to allocate for a table cache.

The rows of a table cache are stored in a set of chunks. When a new row is added a new chunk is allocated if necessary. Assuming that the first one is 1024 rows (TABLE_CACHE_INITIAL_ROWSNUM) and each subsequent is N/2 where N is the number of rows we have allocated till now, then 39th chunk would accommodate 1677416425 rows and all chunks would accommodate 3354832851 rows.

◆ TABLE_CACHE_INITIAL_ROWSNUM

constexpr uint32_t TABLE_CACHE_INITIAL_ROWSNUM = 1024
constexpr

Initial number of rows in the table cache.

◆ trx_i_s_cache

This is the intermediate buffer where data needed to fill the INFORMATION SCHEMA tables is fetched and later retrieved by the C++ code in handler/i_s.cc.

◆ trx_i_s_cache_static

trx_i_s_cache_t trx_i_s_cache_static
static

This is the intermediate buffer where data needed to fill the INFORMATION SCHEMA tables is fetched and later retrieved by the C++ code in handler/i_s.cc.