Skip to content

Commit 3ffc99b

Browse files
atish2196inikep
authored andcommitted
Track RAM usage in temptable shared block
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
1 parent 85b93d8 commit 3ffc99b

File tree

10 files changed

+223
-18
lines changed

10 files changed

+223
-18
lines changed

mysql-test/r/mysqld--help-notwin.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,9 @@ The following options may be given as the first argument:
25852585
Maximum amount of memory (in bytes) the TempTable storage
25862586
engine is allowed to allocate from the main memory (RAM)
25872587
before starting to store data on disk.
2588+
--temptable-track-shared-block-ram
2589+
Track memory consumption of TLS shared block in temptable
2590+
(Defaults to on; use --skip-temptable-track-shared-block-ram to disable.)
25882591
--temptable-use-mmap
25892592
Use mmap files for temptables. This variable is
25902593
deprecated and will be removed in a future release.
@@ -3485,6 +3488,7 @@ tablespace-definition-cache 256
34853488
tc-heuristic-recover OFF
34863489
temptable-max-mmap 1073741824
34873490
temptable-max-ram 1073741824
3491+
temptable-track-shared-block-ram TRUE
34883492
temptable-use-mmap TRUE
34893493
terminology-use-previous NONE
34903494
thread-cache-size 9

mysql-test/r/temptable_basic.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,23 +497,23 @@ truncate performance_schema.memory_summary_global_by_event_name;
497497
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
498498
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
499499
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
500-
memory/temptable/physical_ram 1 0 1048608 0 1 1 1 1048608 1048608 1048608
500+
memory/temptable/physical_ram 0 0 0 0 0 0 0 0 0 0
501501
# conn1
502502
show variables like '%tmp_mem_storage%';
503503
Variable_name Value
504504
internal_tmp_mem_storage_engine TempTable
505505
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
506506
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
507507
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
508-
memory/temptable/physical_ram 2 0 2097216 0 1 2 2 1048608 2097216 2097216
508+
memory/temptable/physical_ram 1 1 1048608 1048608 0 0 1 0 0 1048608
509509
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
510510
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
511511
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
512-
memory/temptable/physical_ram 2 0 2097216 0 1 2 2 1048608 2097216 2097216
512+
memory/temptable/physical_ram 1 1 1048608 1048608 0 0 1 0 0 1048608
513513
select * from performance_schema.memory_summary_global_by_event_name where event_name like 'memory/temptable%';
514514
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
515515
memory/temptable/physical_disk 0 0 0 0 0 0 0 0 0 0
516-
memory/temptable/physical_ram 2 1 2097216 1048608 1 1 2 1048608 1048608 2097216
516+
memory/temptable/physical_ram 3 3 3145824 3145824 0 0 2 0 0 2097216
517517
#
518518
# Bug #31091089 TEMPTABLE: ASSERTION `P - M_MYSQL_BUF < M_LENGTH' FAILED.
519519
#
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
select @@global.temptable_track_shared_block_ram;
2+
@@global.temptable_track_shared_block_ram
3+
1
4+
select @@session.temptable_track_shared_block_ram;
5+
ERROR HY000: Variable 'temptable_track_shared_block_ram' is a GLOBAL variable
6+
show global variables like 'temptable_track_shared_block_ram';
7+
Variable_name Value
8+
temptable_track_shared_block_ram ON
9+
show session variables like 'temptable_track_shared_block_ram';
10+
Variable_name Value
11+
temptable_track_shared_block_ram ON
12+
select * from performance_schema.global_variables where variable_name='temptable_track_shared_block_ram';
13+
VARIABLE_NAME VARIABLE_VALUE
14+
temptable_track_shared_block_ram ON
15+
select * from performance_schema.session_variables where variable_name='temptable_track_shared_block_ram';
16+
VARIABLE_NAME VARIABLE_VALUE
17+
temptable_track_shared_block_ram ON
18+
set global temptable_track_shared_block_ram=1;
19+
ERROR HY000: Variable 'temptable_track_shared_block_ram' is a read only variable
20+
set session temptable_track_shared_block_ram=1;
21+
ERROR HY000: Variable 'temptable_track_shared_block_ram' is a read only variable
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# only global
3+
#
4+
--source include/have_log_bin.inc
5+
select @@global.temptable_track_shared_block_ram;
6+
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
7+
select @@session.temptable_track_shared_block_ram;
8+
show global variables like 'temptable_track_shared_block_ram';
9+
show session variables like 'temptable_track_shared_block_ram';
10+
--disable_warnings
11+
select * from performance_schema.global_variables where variable_name='temptable_track_shared_block_ram';
12+
select * from performance_schema.session_variables where variable_name='temptable_track_shared_block_ram';
13+
--enable_warnings
14+
15+
#
16+
# show that it's read-only
17+
#
18+
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
19+
set global temptable_track_shared_block_ram=1;
20+
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
21+
set session temptable_track_shared_block_ram=1;

mysql-test/t/all_persisted_variables.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ let $total_excluded_vars=`SELECT COUNT(*) FROM performance_schema.global_variabl
6969
'per_user_session_var_user_name_delimiter',
7070
'query_cache_size',
7171
'query_cache_type',
72+
'temptable_track_shared_block_ram',
7273
'tx_isolation',
7374
'tx_read_only',
7475
'sql_wsenv_tenant',

sql/mysqld.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ const char *default_storage_engine;
11761176
const char *default_tmp_storage_engine;
11771177
ulonglong temptable_max_ram;
11781178
ulonglong temptable_max_mmap;
1179+
bool temptable_track_shared_block_ram = false;
11791180
bool temptable_use_mmap;
11801181
static char compiled_default_collation_name[] = MYSQL_DEFAULT_COLLATION_NAME;
11811182
static bool binlog_format_used = false;

sql/mysqld.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ extern const char *default_storage_engine;
271271
extern const char *default_tmp_storage_engine;
272272
extern ulonglong temptable_max_ram;
273273
extern ulonglong temptable_max_mmap;
274+
extern bool temptable_track_shared_block_ram;
274275
extern bool temptable_use_mmap;
275276
extern bool using_udf_functions;
276277
extern bool locked_in_memory;

sql/sys_vars.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,6 +5972,12 @@ static Sys_var_ulonglong Sys_temptable_max_mmap(
59725972
GLOBAL_VAR(temptable_max_mmap), CMD_LINE(REQUIRED_ARG),
59735973
VALID_RANGE(0, ULLONG_MAX), DEFAULT(1 << 30 /* 1 GiB */), BLOCK_SIZE(1));
59745974

5975+
static Sys_var_bool Sys_temptable_track_shared_block_ram(
5976+
"temptable_track_shared_block_ram",
5977+
"Track memory consumption of TLS shared block in temptable",
5978+
READ_ONLY NON_PERSIST GLOBAL_VAR(temptable_track_shared_block_ram),
5979+
CMD_LINE(OPT_ARG), DEFAULT(true));
5980+
59755981
static Sys_var_bool Sys_temptable_use_mmap(
59765982
"temptable_use_mmap",
59775983
"Use mmap files for temptables. "

storage/temptable/include/temptable/allocator.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,14 @@ inline T *Allocator<T, AllocationScheme>::allocate(size_t n_elements) {
552552
}
553553

554554
Block *block;
555-
556555
if (m_shared_block && m_shared_block->is_empty()) {
557556
const size_t block_size =
558557
AllocationScheme::block_size(0, n_bytes_requested);
559-
*m_shared_block = Block(
560-
block_size,
561-
AllocationScheme::block_source(block_size, &m_table_resource_monitor));
558+
*m_shared_block =
559+
Block(block_size, temptable_track_shared_block_ram
560+
? AllocationScheme::block_source(
561+
block_size, &m_table_resource_monitor)
562+
: Source::RAM);
562563
block = m_shared_block;
563564
} else if (m_shared_block &&
564565
m_shared_block->can_accommodate(n_bytes_requested)) {
@@ -600,7 +601,14 @@ inline void Allocator<T, AllocationScheme>::deallocate(T *chunk_data,
600601
block.deallocate(Chunk(chunk_data), n_bytes_requested);
601602
if (remaining_chunks == 0) {
602603
if (m_shared_block && (block == *m_shared_block)) {
603-
// Do nothing. Keep the last block alive.
604+
// Do nothing. Keep the last block alive unless
605+
// we are tracking RAM consumption of shared block.
606+
if (temptable_track_shared_block_ram) {
607+
if (block.type() == Source::RAM) {
608+
MemoryMonitor::RAM::decrease(block.size());
609+
}
610+
m_shared_block->destroy();
611+
}
604612
} else {
605613
assert(m_state->number_of_blocks > 0);
606614
if (block.type() == Source::RAM) {

0 commit comments

Comments
 (0)