Skip to content

Commit b5de4e0

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 437b161 commit b5de4e0

File tree

10 files changed

+221
-16
lines changed

10 files changed

+221
-16
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,6 +2549,9 @@ The following options may be given as the first argument:
25492549
Maximum amount of memory (in bytes) the TempTable storage
25502550
engine is allowed to allocate from the main memory (RAM)
25512551
before starting to store data on disk.
2552+
--temptable-track-shared-block-ram
2553+
Track memory consumption of TLS shared block in temptable
2554+
(Defaults to on; use --skip-temptable-track-shared-block-ram to disable.)
25522555
--temptable-use-mmap
25532556
Use mmap files for temptables. This variable is
25542557
deprecated and will be removed in a future release.
@@ -3450,6 +3453,7 @@ tablespace-definition-cache 256
34503453
tc-heuristic-recover OFF
34513454
temptable-max-mmap 1073741824
34523455
temptable-max-ram 1073741824
3456+
temptable-track-shared-block-ram TRUE
34533457
temptable-use-mmap TRUE
34543458
terminology-use-previous NONE
34553459
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
@@ -79,6 +79,7 @@ let $total_excluded_vars=`SELECT COUNT(*) FROM performance_schema.global_variabl
7979
'per_user_session_var_user_name_delimiter',
8080
'query_cache_size',
8181
'query_cache_type',
82+
'temptable_track_shared_block_ram',
8283
'tx_isolation',
8384
'tx_read_only',
8485
'sql_wsenv_tenant',

sql/mysqld.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ const char *default_storage_engine;
11931193
const char *default_tmp_storage_engine;
11941194
ulonglong temptable_max_ram;
11951195
ulonglong temptable_max_mmap;
1196+
bool temptable_track_shared_block_ram = false;
11961197
bool temptable_use_mmap;
11971198
static char compiled_default_collation_name[] = MYSQL_DEFAULT_COLLATION_NAME;
11981199
static bool binlog_format_used = false;

sql/mysqld.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ extern const char *default_storage_engine;
259259
extern const char *default_tmp_storage_engine;
260260
extern ulonglong temptable_max_ram;
261261
extern ulonglong temptable_max_mmap;
262+
extern bool temptable_track_shared_block_ram;
262263
extern bool temptable_use_mmap;
263264
extern bool using_udf_functions;
264265
extern bool locked_in_memory;

sql/sys_vars.cc

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

5776+
static Sys_var_bool Sys_temptable_track_shared_block_ram(
5777+
"temptable_track_shared_block_ram",
5778+
"Track memory consumption of TLS shared block in temptable",
5779+
READ_ONLY NON_PERSIST GLOBAL_VAR(temptable_track_shared_block_ram),
5780+
CMD_LINE(OPT_ARG), DEFAULT(true));
5781+
57765782
static Sys_var_bool Sys_temptable_use_mmap(
57775783
"temptable_use_mmap",
57785784
"Use mmap files for temptables. "

storage/temptable/include/temptable/allocator.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,13 @@ inline T *Allocator<T, AllocationScheme>::allocate(size_t n_elements) {
516516
}
517517

518518
Block *block;
519-
520519
if (m_shared_block && m_shared_block->is_empty()) {
521520
const size_t block_size =
522521
AllocationScheme::block_size(0, n_bytes_requested);
523522
*m_shared_block =
524-
Block(block_size, AllocationScheme::block_source(block_size));
523+
Block(block_size, temptable_track_shared_block_ram
524+
? AllocationScheme::block_source(block_size)
525+
: Source::RAM);
525526
block = m_shared_block;
526527
} else if (m_shared_block &&
527528
m_shared_block->can_accommodate(n_bytes_requested)) {
@@ -578,7 +579,14 @@ inline void Allocator<T, AllocationScheme>::deallocate(T *chunk_data,
578579
block.deallocate(Chunk(chunk_data), n_bytes_requested);
579580
if (remaining_chunks == 0) {
580581
if (m_shared_block && (block == *m_shared_block)) {
581-
// Do nothing. Keep the last block alive.
582+
// Do nothing. Keep the last block alive unless
583+
// we are tracking RAM consumption of shared block.
584+
if (temptable_track_shared_block_ram) {
585+
if (block.type() == Source::RAM) {
586+
MemoryMonitor::RAM::decrease(block.size());
587+
}
588+
m_shared_block->destroy();
589+
}
582590
} else {
583591
assert(m_state->number_of_blocks > 0);
584592
if (block.type() == Source::RAM) {

0 commit comments

Comments
 (0)