27#ifndef TEMPTABLE_ALLOCATOR_H
28#define TEMPTABLE_ALLOCATOR_H
65 assert(
ram <= std::numeric_limits<
decltype(bytes)>::max() - bytes);
66 return ram.fetch_add(bytes) + bytes;
74 return ram.fetch_sub(bytes) - bytes;
92 assert(
mmap <= std::numeric_limits<
decltype(bytes)>::max() - bytes);
93 return mmap.fetch_add(bytes) + bytes;
100 assert(
mmap >= bytes);
101 return mmap.fetch_sub(bytes) - bytes;
122 static std::atomic<size_t>
ram;
123 static std::atomic<size_t>
mmap;
139 std::numeric_limits<
decltype(bytes)>::max() - bytes);
195template <
typename Block_size_policy,
typename Block_source_policy>
198 return Block_source_policy::block_source(
block_size);
200 static size_t block_size(
size_t number_of_blocks,
size_t n_bytes_requested) {
201 return Block_size_policy::block_size(number_of_blocks, n_bytes_requested);
286 static size_t block_size(
size_t number_of_blocks,
size_t n_bytes_requested) {
287 size_t block_size_hint;
289 block_size_hint = (1ULL << number_of_blocks) * 1_MiB;
324template <
class AllocationScheme>
358 const size_t block_size =
361 Block(block_size, AllocationScheme::block_source(block_size));
390 AllocationScheme::block_freed(block.size(), block.type());
447 "T's with alignment-requirement larger than "
448 "Block::ALIGN_TO are not supported.");
449 static_assert(
sizeof(T) > 0,
"Zero sized objects are not supported");
522 template <
class U,
class... Args>
544 std::shared_ptr<AllocatorState<AllocationScheme>>
m_state;
560template <
class T,
class AllocationScheme>
564 m_shared_block(shared_block),
565 m_table_resource_monitor(table_resource_monitor) {}
567template <
class T,
class AllocationScheme>
570 : m_state(other.m_state),
571 m_shared_block(other.m_shared_block),
572 m_table_resource_monitor(other.m_table_resource_monitor) {}
574template <
class T,
class AllocationScheme>
577 : m_state(std::move(other.m_state)),
578 m_shared_block(other.m_shared_block),
579 m_table_resource_monitor(other.m_table_resource_monitor) {}
581template <
class T,
class AllocationScheme>
584template <
class T,
class AllocationScheme>
591template <
class T,
class AllocationScheme>
595 return !(*
this == rhs);
598template <
class T,
class AllocationScheme>
600 assert(n_elements <= std::numeric_limits<size_type>::max() /
sizeof(T));
605 const size_t n_bytes_requested = n_elements *
sizeof(T);
606 if (n_bytes_requested == 0) {
612 if (m_shared_block && m_shared_block->is_empty()) {
613 const size_t block_size =
614 AllocationScheme::block_size(0, n_bytes_requested);
616 Block(block_size, AllocationScheme::block_source(block_size));
617 block = m_shared_block;
618 }
else if (m_shared_block &&
619 m_shared_block->can_accommodate(n_bytes_requested)) {
620 block = m_shared_block;
622 block = m_state->get_block_for_new_allocation(n_bytes_requested);
637 if (m_table_resource_monitor.consumption() + n_bytes_requested >
638 m_table_resource_monitor.threshold()) {
647 m_table_resource_monitor.increase(n_bytes_requested);
650 reinterpret_cast<T *
>(block->
allocate(n_bytes_requested).
data());
651 assert(
reinterpret_cast<uintptr_t
>(chunk_data) %
alignof(T) == 0);
655template <
class T,
class AllocationScheme>
658 assert(
reinterpret_cast<uintptr_t
>(chunk_data) %
alignof(T) == 0);
660 if (chunk_data ==
nullptr) {
664 const size_t n_bytes_requested = n_elements *
sizeof(T);
667 const auto remaining_chunks =
668 block.deallocate(
Chunk(chunk_data), n_bytes_requested);
669 if (remaining_chunks == 0) {
670 if (m_shared_block && (block == *m_shared_block)) {
673 m_state->block_is_not_used_anymore(block);
676 m_table_resource_monitor.decrease(n_bytes_requested);
679template <
class T,
class AllocationScheme>
680template <
class U,
class... Args>
682 new (
mem)
U(std::forward<Args>(args)...);
685template <
class T,
class AllocationScheme>
691template <
class T,
class AllocationScheme>
Block abstraction for temptable-allocator.
Chunk abstraction for temptable Block allocator.
void inc_status_count_hit_tmp_table_size()
Definition: sql_class.cc:2468
Shared state between all instances of a given allocator.
Definition: allocator.h:325
Block * get_block_for_new_allocation(size_t n_bytes_requested)
Gets a Block from which a new allocation of the specified size should be performed.
Definition: allocator.h:346
void block_is_not_used_anymore(Block block) noexcept
Informs the state object of a block that has no data allocated inside of it anymore for garbage colle...
Definition: allocator.h:372
size_t number_of_blocks
Number of created blocks so far (by this Allocator object).
Definition: allocator.h:403
Block current_block
Current not-yet-full block to feed allocations from.
Definition: allocator.h:396
void free_block(Block &block) noexcept
Frees the specified block and takes care of all accounting.
Definition: allocator.h:389
~AllocatorState() noexcept
Destroys the state, deallocate the current_block if it was left empty.
Definition: allocator.h:330
Custom memory allocator.
Definition: allocator.h:445
Block * m_shared_block
A block of memory which is a state external to this allocator and can be shared among different insta...
Definition: allocator.h:551
void construct(U *mem, Args &&...args)
Construct one object of type U on an already allocated chunk of memory, which must be large enough to...
Definition: allocator.h:681
T & reference
Definition: allocator.h:454
const T * const_pointer
Definition: allocator.h:453
void deallocate(T *ptr, size_t n_elements)
Free a memory allocated by allocate().
Definition: allocator.h:656
const T & const_reference
Definition: allocator.h:455
void operator=(const Allocator< U > &&)=delete
Move operator, not used, thus disabled.
Allocator(const Allocator &)=default
Allocator(Block *shared_block, TableResourceMonitor &table_resource_monitor)
Constructor.
Definition: allocator.h:561
T * pointer
Definition: allocator.h:448
T * allocate(size_t n_elements)
Allocate memory for storing n_elements number of elements.
Definition: allocator.h:599
size_t size_type
Definition: allocator.h:457
void destroy(U *p)
Destroy an object of type U.
Definition: allocator.h:687
static void init()
Initialize necessary structures.
Definition: allocator.h:692
bool operator!=(const Allocator< U > &rhs) const
Inequality operator.
Definition: allocator.h:593
TableResourceMonitor & m_table_resource_monitor
Table resource monitor control mechanism that limits the amount of resources that can be consumed at ...
Definition: allocator.h:555
bool operator==(const Allocator< U > &rhs) const
Equality operator.
Definition: allocator.h:586
ptrdiff_t difference_type
Definition: allocator.h:458
std::shared_ptr< AllocatorState< AllocationScheme > > m_state
Shared state between all the copies and rebinds of this allocator.
Definition: allocator.h:544
T value_type
Definition: allocator.h:456
void operator=(const Allocator< U > &)=delete
Assignment operator, not used, thus disabled.
Memory-block abstraction whose purpose is to serve as a building block for custom memory-allocator im...
Definition: block.h:163
bool is_empty() const
Check if Block is empty (not holding any data).
Definition: block.h:399
bool can_accommodate(size_t chunk_size) const
Check if Block can fit (allocate) a Chunk of given size.
Definition: block.h:403
size_t number_of_used_chunks() const
Get current number of Chunks allocated by the Block.
Definition: block.h:427
static constexpr size_t ALIGN_TO
Block will self-adjust all requested allocation-sizes to the multiple of this value.
Definition: block.h:167
Chunk allocate(size_t chunk_size) noexcept
Allocate a Chunk from a Block.
Definition: block.h:351
static size_t size_hint(size_t n_bytes)
For given size, how much memory will Block with single Chunk actually occupy.
Definition: block.h:448
Chunk is an abstraction with the purpose of representing a smallest logical memory-unit within the Bl...
Definition: chunk.h:68
uint8_t * data() const
Get the pointer to the data section which will be provided to the end-user.
Definition: chunk.h:158
Definition: allocator.h:132
TableResourceMonitor(size_t threshold)
Definition: allocator.h:134
size_t m_total_bytes
Definition: allocator.h:153
size_t increase(size_t bytes)
Definition: allocator.h:137
size_t decrease(size_t bytes)
Definition: allocator.h:143
size_t m_threshold
Definition: allocator.h:152
size_t threshold()
Definition: allocator.h:148
size_t consumption()
Definition: allocator.h:149
const char * p
Definition: ctype-mb.cc:1225
#define U
Definition: ctype-tis620.cc:74
thread_local THD * current_thd
Definition: current_thd.cc:26
Memory utilities for temptable-allocator.
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:171
Common header for many mysys elements.
ulonglong temptable_max_ram
Definition: mysqld.cc:1191
bool temptable_use_mmap
Definition: mysqld.cc:1193
ulonglong temptable_max_mmap
Definition: mysqld.cc:1192
Definition: gcs_xcom_synode.h:64
Definition: allocator.h:48
constexpr size_t ALLOCATOR_MAX_BLOCK_MB_EXP
log2(allocator max block size in MiB).
Definition: constants.h:60
void Block_PSI_init()
Initialize the PSI memory engine.
Definition: block.cc:74
std::atomic_uint64_t count_hit_max_ram
Status variable that counts the memory limit breaches.
Definition: plugin.cc:57
constexpr size_t ALLOCATOR_MAX_BLOCK_BYTES
Limit on the size of a block created by Allocator (in bytes).
Definition: constants.h:65
Source
Type of memory allocated.
Definition: memutils.h:68
@ MMAP_FILE
Memory is allocated on disk, using mmap()'ed file.
@ RAM
Memory is allocated from RAM, using malloc() for example.
std::enable_if_t<!std::is_array< T >::value, std::shared_ptr< T > > make_shared(Args &&...args)
Dynamically allocates storage for an object of type T.
Definition: ut0new.h:2592
static MEM_ROOT mem
Definition: sql_servers.cc:100
Definition: allocator.h:196
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Definition: allocator.h:200
static Source block_source(size_t block_size)
Definition: allocator.h:197
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:203
Definition: allocator.h:461
Allocator< U, AllocationScheme > other
Definition: allocator.h:462
Definition: allocator.h:278
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Given the current number of allocated blocks by the allocator, and number of bytes actually requested...
Definition: allocator.h:286
Definition: allocator.h:86
static size_t increase(size_t bytes)
Log increments of MMAP-backed memory consumption.
Definition: allocator.h:91
static size_t threshold()
Get MMAP-backed memory threshold level.
Definition: allocator.h:107
static size_t decrease(size_t bytes)
Log decrements of MMAP-backed memory consumption.
Definition: allocator.h:99
static size_t consumption()
Get current level of MMAP-backed memory consumption.
Definition: allocator.h:117
Definition: allocator.h:59
static size_t consumption()
Get current level of heap-memory consumption.
Definition: allocator.h:83
static size_t decrease(size_t bytes)
Log decrements of heap-memory consumption.
Definition: allocator.h:72
static size_t increase(size_t bytes)
Log increments of heap-memory consumption.
Definition: allocator.h:64
static size_t threshold()
Get heap-memory threshold level.
Definition: allocator.h:79
Definition: allocator.h:58
static std::atomic< size_t > mmap
Definition: allocator.h:123
static std::atomic< size_t > ram
Total bytes allocated so far by all threads in RAM/MMAP.
Definition: allocator.h:122
Definition: allocator.h:220
static Source block_source(uint32_t block_size)
Definition: allocator.h:221
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:247