Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::BlockAllocator Class Reference
Collaboration diagram for duckdb::BlockAllocator:

Public Member Functions

 BlockAllocator (Allocator &allocator, idx_t block_size, idx_t virtual_memory_size, idx_t physical_memory_size)
void Resize (idx_t new_physical_memory_size)
 Resize physical memory (can only be increased).
data_ptr_t AllocateData (idx_t size) const
 Allocation functions (same API as Allocator).
void FreeData (data_ptr_t pointer, idx_t size) const
data_ptr_t ReallocateData (data_ptr_t pointer, idx_t old_size, idx_t new_size) const
bool SupportsFlush () const
 Flush outstanding allocations.
void ThreadFlush (bool allocator_background_threads, idx_t threshold, idx_t thread_count) const
void FlushAll (optional_idx extra_memory=optional_idx()) const

Static Public Member Functions

static BlockAllocator & Get (DatabaseInstance &db)
static BlockAllocator & Get (AttachedDatabase &db)

Private Member Functions

bool IsActive () const
bool IsEnabled () const
bool IsInPool (data_ptr_t pointer) const
idx_t ModuloBlockSize (idx_t n) const
idx_t DivBlockSize (idx_t n) const
uint32_t GetBlockID (data_ptr_t pointer) const
data_ptr_t GetPointer (uint32_t block_id) const
void VerifyBlockID (uint32_t block_id) const
void FreeInternal (idx_t extra_memory) const
void FreeContiguousBlocks (uint32_t block_id_start, uint32_t block_id_end_including) const

Private Attributes

const hugeint_t uuid
 Identifier.
Allocator & allocator
 Fallback allocator.
const idx_t block_size
 Block size (power of two).
const idx_t block_size_div_shift
 Shift for dividing by block size.
const idx_t virtual_memory_size
 Size of the virtual memory.
atomic< data_ptr_t > virtual_memory_space
 Pointer to the start of the virtual memory.
mutex physical_memory_lock
 Mutex for modifying physical memory size.
atomic< idx_t > physical_memory_size
 Size of the physical memory.
unsafe_unique_ptr< BlockQueue > untouched
 Untouched block IDs.
unsafe_unique_ptr< BlockQueue > touched
 Touched by block IDs.

Friends

class BlockAllocatorThreadLocalState

Constructor & Destructor Documentation

◆ BlockAllocator()

duckdb::BlockAllocator::BlockAllocator ( Allocator & allocator,
idx_t block_size,
idx_t virtual_memory_size,
idx_t physical_memory_size )
386579 : uuid(UUID::GenerateRandomUUID()), allocator(allocator_p), block_size(block_size_p),
386580 block_size_div_shift(CountZeros<idx_t>::Trailing(block_size)),
386581 virtual_memory_size(AlignValue(virtual_memory_size_p, block_size)), virtual_memory_space(nullptr),
386582 physical_memory_size(0), untouched(make_unsafe_uniq<BlockQueue>()), touched(make_unsafe_uniq<BlockQueue>()) {
386583 D_ASSERT(IsPowerOfTwo(block_size));
386584 Resize(physical_memory_size_p);
386585}
unsafe_unique_ptr< BlockQueue > touched
Touched by block IDs.
Definition duckdb.cpp:72781
const idx_t virtual_memory_size
Size of the virtual memory.
Definition duckdb.cpp:72769
void Resize(idx_t new_physical_memory_size)
Resize physical memory (can only be increased).
Definition duckdb.cpp:386606
const idx_t block_size_div_shift
Shift for dividing by block size.
Definition duckdb.cpp:72766
atomic< idx_t > physical_memory_size
Size of the physical memory.
Definition duckdb.cpp:72776
Allocator & allocator
Fallback allocator.
Definition duckdb.cpp:72761
atomic< data_ptr_t > virtual_memory_space
Pointer to the start of the virtual memory.
Definition duckdb.cpp:72771
const hugeint_t uuid
Identifier.
Definition duckdb.cpp:72759
unsafe_unique_ptr< BlockQueue > untouched
Untouched block IDs.
Definition duckdb.cpp:72779
const idx_t block_size
Block size (power of two).
Definition duckdb.cpp:72764
static hugeint_t GenerateRandomUUID(RandomEngine &engine)
Generate a random UUID v4 value.
Definition duckdb.cpp:131021
static T AlignValue(T n)
Align value (ceiling) (not for pointer types).
Definition duckdb.hpp:2126

◆ ~BlockAllocator()

duckdb::BlockAllocator::~BlockAllocator ( )
386587 {
386588 GetBlockAllocatorThreadLocalState(*this).Clear();
386589 if (IsActive()) {
386590 try {
386591 FreeVirtualMemory(virtual_memory_space, virtual_memory_size);
386592 } catch (...) {
386593 // Not allowed to throw in destructor!
386594 }
386595 }
386596}

Member Function Documentation

◆ Get() [1/2]

BlockAllocator & duckdb::BlockAllocator::Get ( DatabaseInstance & db)
static
386598 {
386599 return *db.config.block_allocator;
386600}
db

◆ Get() [2/2]

BlockAllocator & duckdb::BlockAllocator::Get ( AttachedDatabase & db)
static
386602 {
386603 return Get(db.GetDatabase());
386604}

◆ Resize()

void duckdb::BlockAllocator::Resize ( idx_t new_physical_memory_size)

Resize physical memory (can only be increased).

386606 {
386607 lock_guard<mutex> guard(physical_memory_lock);
386608
386609 if (new_physical_memory_size != 0 && !IsActive()) {
386610 virtual_memory_space = AllocateVirtualMemory(virtual_memory_size);
386611 if (!IsActive()) {
386612 return; // Failed to initialize
386613 }
386614 }
386615
386616 if (new_physical_memory_size < physical_memory_size) {
386617 throw InvalidInputException("The \"block_allocator_size\" setting cannot be reduced (current: %llu)",
386618 physical_memory_size.load());
386619 }
386620 if (new_physical_memory_size > virtual_memory_size) {
386621 throw InvalidInputException("The \"block_allocator_size\" setting cannot be greater than the virtual memory "
386622 "size (virtual memory size: %llu)",
386624 }
386625
386626 // Enqueue block IDs efficiently in batches
386627 uint32_t block_ids[STANDARD_VECTOR_SIZE];
386628 const auto start = NumericCast<uint32_t>(DivBlockSize(physical_memory_size));
386629 const auto end = NumericCast<uint32_t>(DivBlockSize(new_physical_memory_size));
386630 for (auto block_id = start; block_id < end; block_id += STANDARD_VECTOR_SIZE) {
386631 const auto next = MinValue<idx_t>(end - block_id, STANDARD_VECTOR_SIZE);
386632 for (uint32_t i = 0; i < next; i++) {
386633 block_ids[i] = block_id + i;
386634 }
386635 untouched->q.enqueue_bulk(block_ids, next);
386636 }
386637
386638 // Finally, update to the new size
386639 physical_memory_size = new_physical_memory_size;
386640}
mutex physical_memory_lock
Mutex for modifying physical memory size.
Definition duckdb.cpp:72774
::uint32_t uint32_t

◆ AllocateData()

data_ptr_t duckdb::BlockAllocator::AllocateData ( idx_t size) const

Allocation functions (same API as Allocator).

386681 {
386682 if (!IsActive() || !IsEnabled() || size != block_size) {
386683 return allocator.AllocateData(size);
386684 }
386685 return GetBlockAllocatorThreadLocalState(*this).Allocate();
386686}

◆ FreeData()

void duckdb::BlockAllocator::FreeData ( data_ptr_t pointer,
idx_t size ) const
386688 {
386689 if (!IsActive() || !IsInPool(pointer)) {
386690 return allocator.FreeData(pointer, size);
386691 }
386692 D_ASSERT(size == block_size);
386693 GetBlockAllocatorThreadLocalState(*this).Free(pointer);
386694}

◆ ReallocateData()

data_ptr_t duckdb::BlockAllocator::ReallocateData ( data_ptr_t pointer,
idx_t old_size,
idx_t new_size ) const
386696 {
386697 if (old_size == new_size) {
386698 return pointer;
386699 }
386700
386701 // If both the old and new allocation are not (or cannot be) in the pool, immediately use the fallback allocator
386702 if (!IsActive() || (!IsInPool(pointer) && new_size != block_size)) {
386703 return allocator.ReallocateData(pointer, old_size, new_size);
386704 }
386705
386706 // Either old or new can be in the pool: allocate, copy, and free
386707 const auto new_pointer = AllocateData(new_size);
386708 memcpy(new_pointer, pointer, MinValue(old_size, new_size));
386709 FreeData(pointer, old_size);
386710 return new_pointer;
386711}
data_ptr_t AllocateData(idx_t size) const
Allocation functions (same API as Allocator).
Definition duckdb.cpp:386681

◆ SupportsFlush()

bool duckdb::BlockAllocator::SupportsFlush ( ) const

Flush outstanding allocations.

386713 {
386714 return (IsActive() && IsEnabled()) || Allocator::SupportsFlush();
386715}
Here is the caller graph for this function:

◆ ThreadFlush()

void duckdb::BlockAllocator::ThreadFlush ( bool allocator_background_threads,
idx_t threshold,
idx_t thread_count ) const
386717 {
386718 if (IsActive() && IsEnabled()) {
386719 GetBlockAllocatorThreadLocalState(*this).Clear();
386720 }
386721 if (Allocator::SupportsFlush()) {
386722 Allocator::ThreadFlush(allocator_background_threads, threshold, thread_count);
386723 }
386724}

◆ FlushAll()

void duckdb::BlockAllocator::FlushAll ( optional_idx extra_memory = optional_idx()) const
386726 {
386727 if (IsActive() && IsEnabled() && extra_memory.IsValid()) {
386728 FreeInternal(extra_memory.GetIndex());
386729 }
386730 if (Allocator::SupportsFlush()) {
386731 Allocator::FlushAll();
386732 }
386733}

◆ IsActive()

bool duckdb::BlockAllocator::IsActive ( ) const
private
386642 {
386643 return virtual_memory_space.load(std::memory_order_relaxed);
386644}

◆ IsEnabled()

bool duckdb::BlockAllocator::IsEnabled ( ) const
private
386646 {
386647 return physical_memory_size.load(std::memory_order_relaxed) != 0;
386648}

◆ IsInPool()

bool duckdb::BlockAllocator::IsInPool ( data_ptr_t pointer) const
private
386650 {
386651 const auto virtual_memory_space_loaded = virtual_memory_space.load(std::memory_order_relaxed);
386652 return pointer >= virtual_memory_space_loaded && pointer < virtual_memory_space_loaded + virtual_memory_size;
386653}

◆ ModuloBlockSize()

idx_t duckdb::BlockAllocator::ModuloBlockSize ( idx_t n) const
private
386655 {
386656 return n & (block_size - 1);
386657}

◆ DivBlockSize()

idx_t duckdb::BlockAllocator::DivBlockSize ( idx_t n) const
private
386659 {
386660 return n >> block_size_div_shift;
386661}

◆ GetBlockID()

uint32_t duckdb::BlockAllocator::GetBlockID ( data_ptr_t pointer) const
private
386663 {
386664 D_ASSERT(IsInPool(pointer));
386665 const auto offset = NumericCast<idx_t>(pointer - virtual_memory_space.load(std::memory_order_relaxed));
386666 D_ASSERT(ModuloBlockSize(offset) == 0);
386667 const auto block_id = NumericCast<uint32_t>(DivBlockSize(offset));
386668 VerifyBlockID(block_id);
386669 return block_id;
386670}

◆ GetPointer()

data_ptr_t duckdb::BlockAllocator::GetPointer ( uint32_t block_id) const
private
386676 {
386677 VerifyBlockID(block_id);
386678 return virtual_memory_space.load(std::memory_order_relaxed) + NumericCast<idx_t>(block_id) * block_size;
386679}

◆ VerifyBlockID()

void duckdb::BlockAllocator::VerifyBlockID ( uint32_t block_id) const
private
386672 {
386673 D_ASSERT(block_id < NumericCast<uint32_t>(virtual_memory_size / block_size));
386674}

◆ FreeInternal()

void duckdb::BlockAllocator::FreeInternal ( idx_t extra_memory) const
private
386735 {
386736 auto count = DivBlockSize(extra_memory);
386737 unsafe_vector<uint32_t> to_free_buffer;
386738 to_free_buffer.resize(count);
386739 count = touched->q.try_dequeue_bulk(to_free_buffer.begin(), count);
386740 if (count == 0) {
386741 return;
386742 }
386743 to_free_buffer.resize(count);
386744
386745 // Sort so we can coalesce madvise calls
386746 std::sort(to_free_buffer.begin(), to_free_buffer.end());
386747
386748 // Coalesce and free
386749 uint32_t block_id_start = to_free_buffer[0];
386750 for (idx_t i = 1; i < to_free_buffer.size(); i++) {
386751 const auto &previous_block_id = to_free_buffer[i - 1];
386752 const auto &current_block_id = to_free_buffer[i];
386753 if (previous_block_id == current_block_id - 1) {
386754 continue; // Current is contiguous with previous block
386755 }
386756
386757 // Previous block is the last contiguous block starting from block_id_start, free them in one go
386758 FreeContiguousBlocks(block_id_start, previous_block_id);
386759
386760 // Continue coalescing from the current
386761 block_id_start = current_block_id;
386762 }
386763
386764 // Don't forget the last one
386765 FreeContiguousBlocks(block_id_start, to_free_buffer.back());
386766
386767 // Make freed blocks available to allocate again
386768 untouched->q.enqueue_bulk(to_free_buffer.begin(), to_free_buffer.size());
386769}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ FreeContiguousBlocks()

void duckdb::BlockAllocator::FreeContiguousBlocks ( uint32_t block_id_start,
uint32_t block_id_end_including ) const
private
386771 {
386772 const auto pointer = GetPointer(block_id_start);
386773 const auto num_blocks = block_id_end_including - block_id_start + 1;
386774 const auto size = num_blocks * block_size;
386775 OnDeallocation(pointer, size);
386776}
GOpaque< Size > size(const GMat &src)

The documentation for this class was generated from the following file: