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;
 
  116  static std::atomic<size_t> 
ram;
 
  117  static std::atomic<size_t> 
mmap;
 
  133           std::numeric_limits<
decltype(bytes)>::
max() - bytes);
 
  189template <
typename Block_size_policy, 
typename Block_source_policy>
 
  192    return Block_source_policy::block_source(
block_size);
 
  194  static size_t block_size(
size_t number_of_blocks, 
size_t n_bytes_requested) {
 
  195    return Block_size_policy::block_size(number_of_blocks, n_bytes_requested);
 
  280  static size_t block_size(
size_t number_of_blocks, 
size_t n_bytes_requested) {
 
  281    size_t block_size_hint;
 
  283      block_size_hint = (1ULL << number_of_blocks) * 1_MiB;
 
  318template <
class AllocationScheme>
 
  352      const size_t block_size =
 
  355          Block(block_size, AllocationScheme::block_source(block_size));
 
  384    AllocationScheme::block_freed(block.size(), block.type());
 
  441                "T's with alignment-requirement larger than " 
  442                "Block::ALIGN_TO are not supported.");
 
  443  static_assert(
sizeof(
T) > 0, 
"Zero sized objects are not supported");
 
  516  template <
class U, 
class... Args>
 
  538  std::shared_ptr<AllocatorState<AllocationScheme>> 
m_state;
 
  554template <
class T, 
class AllocationScheme>
 
  558      m_shared_block(shared_block),
 
  559      m_table_resource_monitor(table_resource_monitor) {}
 
  561template <
class T, 
class AllocationScheme>
 
  564    : m_state(other.m_state),
 
  565      m_shared_block(other.m_shared_block),
 
  566      m_table_resource_monitor(other.m_table_resource_monitor) {}
 
  568template <
class T, 
class AllocationScheme>
 
  571    : m_state(std::move(other.m_state)),
 
  572      m_shared_block(other.m_shared_block),
 
  573      m_table_resource_monitor(other.m_table_resource_monitor) {}
 
  575template <
class T, 
class AllocationScheme>
 
  578template <
class T, 
class AllocationScheme>
 
  585template <
class T, 
class AllocationScheme>
 
  589  return !(*
this == rhs);
 
  592template <
class T, 
class AllocationScheme>
 
  599  const size_t n_bytes_requested = n_elements * 
sizeof(
T);
 
  600  if (n_bytes_requested == 0) {
 
  606  if (m_shared_block && m_shared_block->is_empty()) {
 
  607    const size_t block_size =
 
  608        AllocationScheme::block_size(0, n_bytes_requested);
 
  610        Block(block_size, AllocationScheme::block_source(block_size));
 
  611    block = m_shared_block;
 
  612  } 
else if (m_shared_block &&
 
  613             m_shared_block->can_accommodate(n_bytes_requested)) {
 
  614    block = m_shared_block;
 
  616    block = m_state->get_block_for_new_allocation(n_bytes_requested);
 
  631  if (m_table_resource_monitor.consumption() + n_bytes_requested >
 
  632      m_table_resource_monitor.threshold()) {
 
  641  m_table_resource_monitor.increase(n_bytes_requested);
 
  644      reinterpret_cast<T *
>(block->
allocate(n_bytes_requested).
data());
 
  645  assert(
reinterpret_cast<uintptr_t
>(chunk_data) % 
alignof(
T) == 0);
 
  649template <
class T, 
class AllocationScheme>
 
  652  assert(
reinterpret_cast<uintptr_t
>(chunk_data) % 
alignof(
T) == 0);
 
  654  if (chunk_data == 
nullptr) {
 
  658  const size_t n_bytes_requested = n_elements * 
sizeof(
T);
 
  661  const auto remaining_chunks =
 
  662      block.deallocate(
Chunk(chunk_data), n_bytes_requested);
 
  663  if (remaining_chunks == 0) {
 
  664    if (m_shared_block && (block == *m_shared_block)) {
 
  667      m_state->block_is_not_used_anymore(block);
 
  670  m_table_resource_monitor.decrease(n_bytes_requested);
 
  673template <
class T, 
class AllocationScheme>
 
  674template <
class U, 
class... Args>
 
  676  new (
mem) 
U(std::forward<Args>(args)...);
 
  679template <
class T, 
class AllocationScheme>
 
  685template <
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:2503
 
Shared state between all instances of a given allocator.
Definition: allocator.h:319
 
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:340
 
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:366
 
size_t number_of_blocks
Number of created blocks so far (by this Allocator object).
Definition: allocator.h:397
 
Block current_block
Current not-yet-full block to feed allocations from.
Definition: allocator.h:390
 
void free_block(Block &block) noexcept
Frees the specified block and takes care of all accounting.
Definition: allocator.h:383
 
~AllocatorState() noexcept
Destroys the state, deallocate the current_block if it was left empty.
Definition: allocator.h:324
 
Custom memory allocator.
Definition: allocator.h:439
 
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:545
 
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:675
 
T & reference
Definition: allocator.h:448
 
const T * const_pointer
Definition: allocator.h:447
 
void deallocate(T *ptr, size_t n_elements)
Free a memory allocated by allocate().
Definition: allocator.h:650
 
const T & const_reference
Definition: allocator.h:449
 
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:555
 
T * pointer
Definition: allocator.h:442
 
T * allocate(size_t n_elements)
Allocate memory for storing n_elements number of elements.
Definition: allocator.h:593
 
size_t size_type
Definition: allocator.h:451
 
void destroy(U *p)
Destroy an object of type U.
Definition: allocator.h:681
 
static void init()
Initialize necessary structures.
Definition: allocator.h:686
 
bool operator!=(const Allocator< U > &rhs) const
Inequality operator.
Definition: allocator.h:587
 
TableResourceMonitor & m_table_resource_monitor
Table resource monitor control mechanism that limits the amount of resources that can be consumed at ...
Definition: allocator.h:549
 
bool operator==(const Allocator< U > &rhs) const
Equality operator.
Definition: allocator.h:580
 
ptrdiff_t difference_type
Definition: allocator.h:452
 
std::shared_ptr< AllocatorState< AllocationScheme > > m_state
Shared state between all the copies and rebinds of this allocator.
Definition: allocator.h:538
 
T value_type
Definition: allocator.h:450
 
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:126
 
TableResourceMonitor(size_t threshold)
Definition: allocator.h:128
 
size_t m_total_bytes
Definition: allocator.h:147
 
size_t increase(size_t bytes)
Definition: allocator.h:131
 
size_t decrease(size_t bytes)
Definition: allocator.h:137
 
size_t m_threshold
Definition: allocator.h:146
 
size_t threshold()
Definition: allocator.h:142
 
size_t consumption()
Definition: allocator.h:143
 
const char * p
Definition: ctype-mb.cc:1227
 
#define U
Definition: ctype-tis620.cc:73
 
thread_local THD * current_thd
Definition: current_thd.cc:26
 
#define T
Definition: jit_executor_value.cc:373
 
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:1208
 
ulonglong temptable_max_mmap
Definition: mysqld.cc:1209
 
ValueType max(X &&first)
Definition: gtid.h:103
 
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:56
 
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:2596
 
static MEM_ROOT mem
Definition: sql_servers.cc:100
 
Definition: allocator.h:190
 
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Definition: allocator.h:194
 
static Source block_source(size_t block_size)
Definition: allocator.h:191
 
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:197
 
Definition: allocator.h:455
 
Allocator< U, AllocationScheme > other
Definition: allocator.h:456
 
Definition: allocator.h:272
 
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:280
 
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:111
 
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:117
 
static std::atomic< size_t > ram
Total bytes allocated so far by all threads in RAM/MMAP.
Definition: allocator.h:116
 
Definition: allocator.h:214
 
static Source block_source(uint32_t block_size)
Definition: allocator.h:215
 
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:241