Skip to content

Commit

Permalink
Track RAM usage in temptable shared block
Browse files Browse the repository at this point in the history
Summary:
When decreasing temptable_max_ram system variable, I am not
seeing decrease in memory when running workloads.
create_tmp_table is still taking significant amount of memory in
temptable APIs. This is because temptable_max_ram limit does not
apply to shared block. The shared block is a TLS variable. In
multithreaded environment, this takes a significant amount of
memory (>150 MB). The shared block is unconditionally allocated from
RAM.  Also the shared_block is never deallocated once allocated in a
thread.
The fix introduces a system variable temptable_track_shared_block_ram.
When enabled, the shared block memory allocation is also tracked.
This tracking enables shared block to be allocated from mmap files
in case the total allocated memory by temptable exceeds
temptable_max_ram. Also with the fix, shared_block is deallocated
after all the chunks are destroyed. This allows us to reclaim memory
space more proactively.
With this fix, I am seeing savings of >150 MB in our workloads.

Reviewed By: yizhang82

Differential Revision: D27520132
  • Loading branch information
atish2196 authored and inikep committed Aug 6, 2024
1 parent 437b161 commit b5de4e0
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 16 deletions.
4 changes: 4 additions & 0 deletions mysql-test/r/mysqld--help-notwin.result
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,9 @@ The following options may be given as the first argument:
Maximum amount of memory (in bytes) the TempTable storage
engine is allowed to allocate from the main memory (RAM)
before starting to store data on disk.
--temptable-track-shared-block-ram
Track memory consumption of TLS shared block in temptable
(Defaults to on; use --skip-temptable-track-shared-block-ram to disable.)
--temptable-use-mmap
Use mmap files for temptables. This variable is
deprecated and will be removed in a future release.
Expand Down Expand Up @@ -3450,6 +3453,7 @@ tablespace-definition-cache 256
tc-heuristic-recover OFF
temptable-max-mmap 1073741824
temptable-max-ram 1073741824
temptable-track-shared-block-ram TRUE
temptable-use-mmap TRUE
terminology-use-previous NONE
thread-cache-size 9
Expand Down
8 changes: 4 additions & 4 deletions mysql-test/r/temptable_basic.result
Original file line number Diff line number Diff line change
Expand Up @@ -497,23 +497,23 @@ truncate performance_schema.memory_summary_global_by_event_name;
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
EVENT_NAME COUNT_ALLOC COUNT_FREE SUM_NUMBER_OF_BYTES_ALLOC SUM_NUMBER_OF_BYTES_FREE LOW_COUNT_USED CURRENT_COUNT_USED HIGH_COUNT_USED LOW_NUMBER_OF_BYTES_USED CURRENT_NUMBER_OF_BYTES_USED HIGH_NUMBER_OF_BYTES_USED
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
memory/temptable/physical_ram 1 0 1048608 0 1 1 1 1048608 1048608 1048608
memory/temptable/physical_ram 0 0 0 0 0 0 0 0 0 0
# conn1
show variables like '%tmp_mem_storage%';
Variable_name Value
internal_tmp_mem_storage_engine TempTable
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
EVENT_NAME COUNT_ALLOC COUNT_FREE SUM_NUMBER_OF_BYTES_ALLOC SUM_NUMBER_OF_BYTES_FREE LOW_COUNT_USED CURRENT_COUNT_USED HIGH_COUNT_USED LOW_NUMBER_OF_BYTES_USED CURRENT_NUMBER_OF_BYTES_USED HIGH_NUMBER_OF_BYTES_USED
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
memory/temptable/physical_ram 2 0 2097216 0 1 2 2 1048608 2097216 2097216
memory/temptable/physical_ram 1 1 1048608 1048608 0 0 1 0 0 1048608
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
EVENT_NAME COUNT_ALLOC COUNT_FREE SUM_NUMBER_OF_BYTES_ALLOC SUM_NUMBER_OF_BYTES_FREE LOW_COUNT_USED CURRENT_COUNT_USED HIGH_COUNT_USED LOW_NUMBER_OF_BYTES_USED CURRENT_NUMBER_OF_BYTES_USED HIGH_NUMBER_OF_BYTES_USED
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
memory/temptable/physical_ram 2 0 2097216 0 1 2 2 1048608 2097216 2097216
memory/temptable/physical_ram 1 1 1048608 1048608 0 0 1 0 0 1048608
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
EVENT_NAME COUNT_ALLOC COUNT_FREE SUM_NUMBER_OF_BYTES_ALLOC SUM_NUMBER_OF_BYTES_FREE LOW_COUNT_USED CURRENT_COUNT_USED HIGH_COUNT_USED LOW_NUMBER_OF_BYTES_USED CURRENT_NUMBER_OF_BYTES_USED HIGH_NUMBER_OF_BYTES_USED
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
memory/temptable/physical_ram 2 1 2097216 1048608 1 1 2 1048608 1048608 2097216
memory/temptable/physical_ram 3 3 3145824 3145824 0 0 2 0 0 2097216
#
# Bug #31091089 TEMPTABLE: ASSERTION `P - M_MYSQL_BUF < M_LENGTH' FAILED.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
select @@global.temptable_track_shared_block_ram;
@@global.temptable_track_shared_block_ram
1
select @@session.temptable_track_shared_block_ram;
ERROR HY000: Variable 'temptable_track_shared_block_ram' is a GLOBAL variable
show global variables like 'temptable_track_shared_block_ram';
Variable_name Value
temptable_track_shared_block_ram ON
show session variables like 'temptable_track_shared_block_ram';
Variable_name Value
temptable_track_shared_block_ram ON
select * from performance_schema.global_variables where variable_name='temptable_track_shared_block_ram';
VARIABLE_NAME VARIABLE_VALUE
temptable_track_shared_block_ram ON
select * from performance_schema.session_variables where variable_name='temptable_track_shared_block_ram';
VARIABLE_NAME VARIABLE_VALUE
temptable_track_shared_block_ram ON
set global temptable_track_shared_block_ram=1;
ERROR HY000: Variable 'temptable_track_shared_block_ram' is a read only variable
set session temptable_track_shared_block_ram=1;
ERROR HY000: Variable 'temptable_track_shared_block_ram' is a read only variable
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# only global
#
--source include/have_log_bin.inc
select @@global.temptable_track_shared_block_ram;
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
select @@session.temptable_track_shared_block_ram;
show global variables like 'temptable_track_shared_block_ram';
show session variables like 'temptable_track_shared_block_ram';
--disable_warnings
select * from performance_schema.global_variables where variable_name='temptable_track_shared_block_ram';
select * from performance_schema.session_variables where variable_name='temptable_track_shared_block_ram';
--enable_warnings

#
# show that it's read-only
#
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
set global temptable_track_shared_block_ram=1;
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
set session temptable_track_shared_block_ram=1;
1 change: 1 addition & 0 deletions mysql-test/t/all_persisted_variables.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ let $total_excluded_vars=`SELECT COUNT(*) FROM performance_schema.global_variabl
'per_user_session_var_user_name_delimiter',
'query_cache_size',
'query_cache_type',
'temptable_track_shared_block_ram',
'tx_isolation',
'tx_read_only',
'sql_wsenv_tenant',
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ const char *default_storage_engine;
const char *default_tmp_storage_engine;
ulonglong temptable_max_ram;
ulonglong temptable_max_mmap;
bool temptable_track_shared_block_ram = false;
bool temptable_use_mmap;
static char compiled_default_collation_name[] = MYSQL_DEFAULT_COLLATION_NAME;
static bool binlog_format_used = false;
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ extern const char *default_storage_engine;
extern const char *default_tmp_storage_engine;
extern ulonglong temptable_max_ram;
extern ulonglong temptable_max_mmap;
extern bool temptable_track_shared_block_ram;
extern bool temptable_use_mmap;
extern bool using_udf_functions;
extern bool locked_in_memory;
Expand Down
6 changes: 6 additions & 0 deletions sql/sys_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5773,6 +5773,12 @@ static Sys_var_ulonglong Sys_temptable_max_mmap(
GLOBAL_VAR(temptable_max_mmap), CMD_LINE(REQUIRED_ARG),
VALID_RANGE(0, ULLONG_MAX), DEFAULT(1 << 30 /* 1 GiB */), BLOCK_SIZE(1));

static Sys_var_bool Sys_temptable_track_shared_block_ram(
"temptable_track_shared_block_ram",
"Track memory consumption of TLS shared block in temptable",
READ_ONLY NON_PERSIST GLOBAL_VAR(temptable_track_shared_block_ram),
CMD_LINE(OPT_ARG), DEFAULT(true));

static Sys_var_bool Sys_temptable_use_mmap(
"temptable_use_mmap",
"Use mmap files for temptables. "
Expand Down
14 changes: 11 additions & 3 deletions storage/temptable/include/temptable/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,13 @@ inline T *Allocator<T, AllocationScheme>::allocate(size_t n_elements) {
}

Block *block;

if (m_shared_block && m_shared_block->is_empty()) {
const size_t block_size =
AllocationScheme::block_size(0, n_bytes_requested);
*m_shared_block =
Block(block_size, AllocationScheme::block_source(block_size));
Block(block_size, temptable_track_shared_block_ram
? AllocationScheme::block_source(block_size)
: Source::RAM);
block = m_shared_block;
} else if (m_shared_block &&
m_shared_block->can_accommodate(n_bytes_requested)) {
Expand Down Expand Up @@ -578,7 +579,14 @@ inline void Allocator<T, AllocationScheme>::deallocate(T *chunk_data,
block.deallocate(Chunk(chunk_data), n_bytes_requested);
if (remaining_chunks == 0) {
if (m_shared_block && (block == *m_shared_block)) {
// Do nothing. Keep the last block alive.
// Do nothing. Keep the last block alive unless
// we are tracking RAM consumption of shared block.
if (temptable_track_shared_block_ram) {
if (block.type() == Source::RAM) {
MemoryMonitor::RAM::decrease(block.size());
}
m_shared_block->destroy();
}
} else {
assert(m_state->number_of_blocks > 0);
if (block.type() == Source::RAM) {
Expand Down
Loading

0 comments on commit b5de4e0

Please sign in to comment.