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::TemporaryMemoryManager Class Reference

Public Member Functions

unique_ptr< TemporaryMemoryState > Register (ClientContext &context)
 Register a TemporaryMemoryState.

Static Public Member Functions

static TemporaryMemoryManager & Get (ClientContext &context)
 Get the TemporaryMemoryManager.

Private Member Functions

unique_lock< mutex > Lock ()
 Locks the TemporaryMemoryManager.
idx_t DefaultMinimumReservation () const
 Get the default minimum reservation.
void Unregister (TemporaryMemoryState &temporary_memory_state)
 Unregister a TemporaryMemoryState (called by the destructor of TemporaryMemoryState).
void UpdateConfiguration (ClientContext &context)
 Update memory_limit, has_temporary_directory, and num_threads (must hold the lock).
void UpdateState (ClientContext &context, TemporaryMemoryState &temporary_memory_state)
 Update the TemporaryMemoryState to the new remaining size, and updates the reservation (must hold the lock).
void SetRemainingSize (TemporaryMemoryState &temporary_memory_state, idx_t new_remaining_size)
 Set the remaining size of a TemporaryMemoryState (must hold the lock).
void SetReservation (TemporaryMemoryState &temporary_memory_state, idx_t new_reservation)
 Set the reservation of a TemporaryMemoryState (must hold the lock).
idx_t ComputeReservation (const TemporaryMemoryState &temporary_memory_state) const
 Computes optimal reservation of a TemporaryMemoryState based on a cost function.
void Verify () const
 Verify internal counts (must hold the lock).

Private Attributes

mutex lock
 Lock because TemporaryMemoryManager is used concurrently.
idx_t memory_limit = DConstants::INVALID_INDEX
 Memory limit of the buffer pool.
bool has_temporary_directory = false
 Whether there is a temporary directory that we can offload blocks to.
idx_t num_threads = DConstants::INVALID_INDEX
 Number of threads.
idx_t num_connections = DConstants::INVALID_INDEX
 Number of active connections.
idx_t query_max_memory = DConstants::INVALID_INDEX
 Max memory per query.
reference_set_t< TemporaryMemoryState > active_states
 Currently active states.
idx_t reservation
 The sum of reservations of all active states.
idx_t remaining_size
 The sum of the remaining size of all active states.

Static Private Attributes

static constexpr idx_t MINIMUM_RESERVATION_PER_STATE_PER_THREAD = 512ULL * DEFAULT_BLOCK_ALLOC_SIZE
 512 blocks per state per thread, which is 0.125GB per thread for DEFAULT_BLOCK_ALLOC_SIZE.
static constexpr idx_t MINIMUM_RESERVATION_MEMORY_LIMIT_DIVISOR = 16ULL
 1/16th of the available main memory.
static constexpr double MAXIMUM_MEMORY_LIMIT_RATIO = 0.9
 The maximum ratio of the memory limit that we reserve using the TemporaryMemoryManager.
static constexpr double MAXIMUM_FREE_MEMORY_RATIO = 0.9
 The maximum ratio of the remaining memory that we reserve per TemporaryMemoryState.
static constexpr idx_t MINIMUM_REMAINING_STATE_RESERVATIONS = 8ULL
 Or, we leave room for between this many minimum reservations (if it is less than MAXIMUM_FREE_MEMORY_RATIO).
static constexpr idx_t MAXIMUM_REMAINING_STATE_RESERVATIONS = 32ULL

Friends

class TemporaryMemoryState

Detailed Description

TemporaryMemoryManager is a one-of class owned by the buffer pool that tries to dynamically assign memory to concurrent states, such that their combined memory usage does not exceed the limit

Constructor & Destructor Documentation

◆ TemporaryMemoryManager()

duckdb::TemporaryMemoryManager::TemporaryMemoryManager ( )
443513 : reservation(0), remaining_size(0) {
443514}
idx_t reservation
The sum of reservations of all active states.
Definition duckdb.cpp:103108
idx_t remaining_size
The sum of the remaining size of all active states.
Definition duckdb.cpp:103110

Member Function Documentation

◆ Get()

TemporaryMemoryManager & duckdb::TemporaryMemoryManager::Get ( ClientContext & context)
static

Get the TemporaryMemoryManager.

443547 {
443548 return BufferManager::GetBufferManager(context).GetTemporaryMemoryManager();
443549}

◆ Register()

unique_ptr< TemporaryMemoryState > duckdb::TemporaryMemoryManager::Register ( ClientContext & context)

Register a TemporaryMemoryState.

443551 {
443552 auto guard = Lock();
443553 UpdateConfiguration(context);
443554
443555 auto result = unique_ptr<TemporaryMemoryState>(new TemporaryMemoryState(*this, DefaultMinimumReservation()));
443556 SetRemainingSize(*result, result->GetMinimumReservation());
443557 SetReservation(*result, result->GetMinimumReservation());
443558 active_states.insert(*result);
443559
443560 Verify();
443561 return result;
443562}
unique_lock< mutex > Lock()
Locks the TemporaryMemoryManager.
Definition duckdb.cpp:443516
void Verify() const
Verify internal counts (must hold the lock).
Definition duckdb.cpp:443782
reference_set_t< TemporaryMemoryState > active_states
Currently active states.
Definition duckdb.cpp:103106
void UpdateConfiguration(ClientContext &context)
Update memory_limit, has_temporary_directory, and num_threads (must hold the lock).
Definition duckdb.cpp:443535
void SetRemainingSize(TemporaryMemoryState &temporary_memory_state, idx_t new_remaining_size)
Set the remaining size of a TemporaryMemoryState (must hold the lock).
Definition duckdb.cpp:443607
void SetReservation(TemporaryMemoryState &temporary_memory_state, idx_t new_reservation)
Set the reservation of a TemporaryMemoryState (must hold the lock).
Definition duckdb.cpp:443614
idx_t DefaultMinimumReservation() const
Get the default minimum reservation.
Definition duckdb.cpp:443520
friend class TemporaryMemoryState
Definition duckdb.cpp:103042
Here is the call graph for this function:

◆ Lock()

unique_lock< mutex > duckdb::TemporaryMemoryManager::Lock ( )
private

Locks the TemporaryMemoryManager.

443516 {
443517 return unique_lock<mutex>(lock);
443518}
mutex lock
Lock because TemporaryMemoryManager is used concurrently.
Definition duckdb.cpp:103092
Here is the caller graph for this function:

◆ DefaultMinimumReservation()

idx_t duckdb::TemporaryMemoryManager::DefaultMinimumReservation ( ) const
private

Get the default minimum reservation.

443520 {
443523}
static constexpr idx_t MINIMUM_RESERVATION_MEMORY_LIMIT_DIVISOR
1/16th of the available main memory.
Definition duckdb.cpp:103054
idx_t memory_limit
Memory limit of the buffer pool.
Definition duckdb.cpp:103095
idx_t num_threads
Number of threads.
Definition duckdb.cpp:103099
static constexpr idx_t MINIMUM_RESERVATION_PER_STATE_PER_THREAD
512 blocks per state per thread, which is 0.125GB per thread for DEFAULT_BLOCK_ALLOC_SIZE.
Definition duckdb.cpp:103052
Here is the caller graph for this function:

◆ Unregister()

void duckdb::TemporaryMemoryManager::Unregister ( TemporaryMemoryState & temporary_memory_state)
private

Unregister a TemporaryMemoryState (called by the destructor of TemporaryMemoryState).

443525 {
443526 auto guard = Lock();
443527
443528 SetReservation(temporary_memory_state, 0);
443529 SetRemainingSize(temporary_memory_state, 0);
443530 active_states.erase(temporary_memory_state);
443531
443532 Verify();
443533}
Here is the call graph for this function:

◆ UpdateConfiguration()

void duckdb::TemporaryMemoryManager::UpdateConfiguration ( ClientContext & context)
private

Update memory_limit, has_temporary_directory, and num_threads (must hold the lock).

443535 {
443536 auto &buffer_manager = BufferManager::GetBufferManager(context);
443537 auto &task_scheduler = TaskScheduler::GetScheduler(context);
443538
443539 memory_limit =
443540 LossyNumericCast<idx_t>(MAXIMUM_MEMORY_LIMIT_RATIO * static_cast<double>(buffer_manager.GetMaxMemory()));
443541 has_temporary_directory = buffer_manager.HasTemporaryDirectory();
443542 num_threads = NumericCast<idx_t>(task_scheduler.NumberOfThreads());
443543 num_connections = ConnectionManager::Get(context).GetConnectionCount();
443544 query_max_memory = buffer_manager.GetQueryMaxMemory();
443545}
bool has_temporary_directory
Whether there is a temporary directory that we can offload blocks to.
Definition duckdb.cpp:103097
static constexpr double MAXIMUM_MEMORY_LIMIT_RATIO
The maximum ratio of the memory limit that we reserve using the TemporaryMemoryManager.
Definition duckdb.cpp:103057
idx_t num_connections
Number of active connections.
Definition duckdb.cpp:103101
idx_t query_max_memory
Max memory per query.
Definition duckdb.cpp:103103
Here is the caller graph for this function:

◆ UpdateState()

void duckdb::TemporaryMemoryManager::UpdateState ( ClientContext & context,
TemporaryMemoryState & temporary_memory_state )
private

Update the TemporaryMemoryState to the new remaining size, and updates the reservation (must hold the lock).

443564 {
443565 UpdateConfiguration(context);
443566
443567 // The lower bound for the reservation of this state is either the minimum reservation or the remaining size
443568 const auto lower_bound =
443569 MinValue(temporary_memory_state.GetMinimumReservation(), temporary_memory_state.GetRemainingSize());
443570
443571 if (temporary_memory_state.GetRemainingSize() == 0) {
443572 // Sometimes set to 0 to denote end of state (before actually deleting the state)
443573 SetReservation(temporary_memory_state, 0);
443574 } else if (context.config.force_external) {
443575 // We're forcing external processing. Give it the minimum
443576 SetReservation(temporary_memory_state, lower_bound);
443577 } else if (!has_temporary_directory) {
443578 // We cannot offload, so we cannot limit memory usage. Set reservation equal to the remaining size
443579 SetReservation(temporary_memory_state, temporary_memory_state.GetRemainingSize());
443580 } else if (reservation - temporary_memory_state.GetReservation() + lower_bound >= memory_limit) {
443581 // We overshot. Set reservation equal to the minimum
443582 SetReservation(temporary_memory_state, lower_bound);
443583 } else {
443584 // The upper bound for the reservation of this state is the minimum of:
443585 // 1. Remaining size of the state
443586 // 2. The max memory per query
443587 // 3. MAXIMUM_FREE_MEMORY_RATIO * free memory
443588 auto upper_bound = MinValue(temporary_memory_state.GetRemainingSize(), query_max_memory);
443589 const auto free_memory = memory_limit - (reservation - temporary_memory_state.GetReservation());
443590 upper_bound = MinValue(upper_bound,
443591 LossyNumericCast<idx_t>(MAXIMUM_FREE_MEMORY_RATIO * static_cast<double>(free_memory)));
443592 upper_bound = MinValue(upper_bound, free_memory);
443593
443594 idx_t new_reservation;
443595 if (lower_bound >= upper_bound) {
443596 new_reservation = lower_bound;
443597 } else {
443598 new_reservation = remaining_size > memory_limit ? ComputeReservation(temporary_memory_state) : upper_bound;
443599 }
443600
443601 SetReservation(temporary_memory_state, new_reservation);
443602 }
443603
443604 Verify();
443605}
static constexpr double MAXIMUM_FREE_MEMORY_RATIO
The maximum ratio of the remaining memory that we reserve per TemporaryMemoryState.
Definition duckdb.cpp:103059
idx_t ComputeReservation(const TemporaryMemoryState &temporary_memory_state) const
Computes optimal reservation of a TemporaryMemoryState based on a cost function.
Definition duckdb.cpp:443663
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ SetRemainingSize()

void duckdb::TemporaryMemoryManager::SetRemainingSize ( TemporaryMemoryState & temporary_memory_state,
idx_t new_remaining_size )
private

Set the remaining size of a TemporaryMemoryState (must hold the lock).

443607 {
443608 D_ASSERT(this->remaining_size >= temporary_memory_state.GetRemainingSize());
443609 this->remaining_size -= temporary_memory_state.GetRemainingSize();
443610 temporary_memory_state.remaining_size = new_remaining_size;
443611 this->remaining_size += temporary_memory_state.GetRemainingSize();
443612}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetReservation()

void duckdb::TemporaryMemoryManager::SetReservation ( TemporaryMemoryState & temporary_memory_state,
idx_t new_reservation )
private

Set the reservation of a TemporaryMemoryState (must hold the lock).

443614 {
443615 D_ASSERT(this->reservation >= temporary_memory_state.GetReservation());
443616 this->reservation -= temporary_memory_state.GetReservation();
443617 temporary_memory_state.reservation = new_reservation;
443618 this->reservation += temporary_memory_state.GetReservation();
443619}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeReservation()

idx_t duckdb::TemporaryMemoryManager::ComputeReservation ( const TemporaryMemoryState & temporary_memory_state) const
private

Computes optimal reservation of a TemporaryMemoryState based on a cost function.

443663 {
443664 static constexpr idx_t OPTIMIZATION_ITERATIONS_MULTIPLIER = 5;
443665
443666 // Use vectors for ease
443667 optional_idx state_index;
443668 vector<reference<const TemporaryMemoryState>> states;
443669 vector<idx_t> res;
443670 vector<double> der;
443671 idx_t sum_of_initial_res = 0;
443672
443673 idx_t i = 0;
443674 for (auto &state : active_states) {
443675 const auto initial_reservation = ComputeInitialReservation(state);
443676 sum_of_initial_res += initial_reservation;
443677 if (RefersToSameObject(state.get(), temporary_memory_state)) {
443678 state_index = i;
443679 } else if (initial_reservation >= state.get().GetRemainingSize()) {
443680 continue;
443681 }
443682 states.emplace_back(state);
443683 res.push_back(initial_reservation);
443684 der.push_back(NumericLimits<double>::Maximum());
443685 i++;
443686 }
443687 const idx_t n = i;
443688
443689 if (sum_of_initial_res >= memory_limit) {
443690 return res[state_index.GetIndex()];
443691 }
443692 const auto free_memory = memory_limit - sum_of_initial_res;
443693
443694 // Distribute memory in OPTIMIZATION_ITERATIONS
443695 idx_t remaining_memory = free_memory;
443696 const idx_t optimization_iterations = OPTIMIZATION_ITERATIONS_MULTIPLIER * n;
443697 for (idx_t opt_idx = 0; opt_idx < optimization_iterations; opt_idx++) {
443698 D_ASSERT(remaining_memory != 0);
443699 ComputeDerivatives(states, res, der, n);
443700
443701 // Find the index of the state with the lowest derivative
443702 idx_t min_idx = 0;
443703 double min_der = NumericLimits<double>::Maximum();
443704 for (i = 0; i < n; i++) {
443705 auto &state = states[i].get();
443706 if (res[i] >= state.GetRemainingSize()) {
443707 continue; // We can't increase the reservation of "maxed" states, so we skip these
443708 }
443709 if (der[i] < min_der) {
443710 min_idx = i;
443711 min_der = der[i];
443712 }
443713 }
443714 auto &min_state = states[min_idx].get();
443715
443716 // This is how much memory we will distribute in this round
443717 const auto iter_memory = ExactNumericCast<idx_t>(
443718 std::ceil(static_cast<double>(remaining_memory) / static_cast<double>(optimization_iterations - opt_idx)));
443719
443720 // Compute how much we can add
443721 const auto state_room = min_state.GetRemainingSize() - res[min_idx];
443722 const auto delta = MinValue(iter_memory, state_room);
443723 D_ASSERT(delta <= remaining_memory);
443724
443725 // Update counts
443726 res[min_idx] += delta;
443727 remaining_memory -= delta;
443728
443729 // If we aren't able to assign the remaining memory to the lowest-derivative state in the last iteration,
443730 // we'll exit this loop without assigning all free memory. This adds another iteration
443731 if (opt_idx == optimization_iterations - 1 && delta < iter_memory) {
443732 opt_idx--;
443733 }
443734 }
443735 D_ASSERT(remaining_memory == 0);
443736
443737 // We computed how the memory should be assigned to the states,
443738 // but we did not yet take into account the upper bound of MAXIMUM_FREE_MEMORY_RATIO * free_memory.
443739 // We don't simply bound this state's reservation because order matters,
443740 // and this function is called in arbitrary order for all active states.
443741 // So, we order the states by derivative (as if they got the max reservation),
443742 // and assign the states with the lowest derivate first.
443743 // This way, order does not matter _as much_, and the "best" states will _likely_ get more memory
443744 vector<idx_t> idxs(n, 0);
443745 vector<idx_t> max_res(n, 0);
443746 for (i = 0; i < n; i++) {
443747 idxs[i] = i;
443748 max_res[i] = states[i].get().GetRemainingSize() - 1;
443749 }
443750 ComputeDerivatives(states, max_res, der, n);
443751 std::sort(idxs.begin(), idxs.end(), [&](const idx_t &lhs, const idx_t &rhs) { return der[lhs] < der[rhs]; });
443752
443753 // Loop through sorted indices until we encounter the state index
443754 remaining_memory = free_memory;
443755 for (const auto idx : idxs) {
443756 auto &state = states[idx].get();
443757 D_ASSERT(res[idx] <= state.GetRemainingSize());
443758 const auto initial_state_reservation = ComputeInitialReservation(state);
443759 // Bound by the ratio
443760 const auto state_remaining = initial_state_reservation + remaining_memory;
443761 auto upper_bound = LossyNumericCast<idx_t>(MAXIMUM_FREE_MEMORY_RATIO * static_cast<double>(state_remaining));
443762 // Or bound by leaving a number of minimum reservations for other states
443763 auto num_other_states = MinValue(MAXIMUM_REMAINING_STATE_RESERVATIONS, num_connections);
443764 num_other_states = MaxValue(num_other_states, MINIMUM_REMAINING_STATE_RESERVATIONS);
443765 upper_bound = MinValue(upper_bound, num_other_states * DefaultMinimumReservation());
443766 auto state_reservation = MinValue(res[idx], upper_bound);
443767 // But make sure it's never less than the initial reservation
443768 state_reservation = MaxValue(state_reservation, initial_state_reservation);
443769 // If this is the current state, we can just return
443770 if (idx == state_index.GetIndex()) {
443771 return state_reservation;
443772 }
443773 // Decrement the remaining memory
443774 const auto delta = state_reservation - initial_state_reservation;
443775 remaining_memory -= delta;
443776 }
443777
443778 // We cannot have gotten through the loop above without finding the current state
443779 throw InternalException("Did not find state_index in ComputeOptimalReservation");
443780}
static constexpr idx_t MINIMUM_REMAINING_STATE_RESERVATIONS
Or, we leave room for between this many minimum reservations (if it is less than MAXIMUM_FREE_MEMORY_...
Definition duckdb.cpp:103061
static idx_t ComputeInitialReservation(const TemporaryMemoryState &temporary_memory_state)
Compute initial reservation for use in ComputeReservation.
Definition duckdb.cpp:443622
bool RefersToSameObject(const reference< T > &a, const reference< T > &b)
Returns whether or not two reference wrappers refer to the same object.
Definition duckdb.hpp:2191
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Verify()

void duckdb::TemporaryMemoryManager::Verify ( ) const
private

Verify internal counts (must hold the lock).

443782 {
443783#ifdef DEBUG
443784 idx_t total_reservation = 0;
443785 idx_t total_remaining_size = 0;
443786 for (auto &active_state : active_states) {
443787 total_reservation += active_state.get().GetReservation();
443788 total_remaining_size += active_state.get().GetRemainingSize();
443789 }
443790 D_ASSERT(total_reservation == this->reservation);
443791 D_ASSERT(total_remaining_size == this->remaining_size);
443792#endif
443793}
Here is the caller graph for this function:

◆ TemporaryMemoryState

friend class TemporaryMemoryState
friend

TemporaryMemoryState is a friend class so it can access the private methods of this class, but it should not access the private fields!

Member Data Documentation

◆ MINIMUM_RESERVATION_PER_STATE_PER_THREAD

idx_t duckdb::TemporaryMemoryManager::MINIMUM_RESERVATION_PER_STATE_PER_THREAD = 512ULL * DEFAULT_BLOCK_ALLOC_SIZE
staticconstexprprivate

512 blocks per state per thread, which is 0.125GB per thread for DEFAULT_BLOCK_ALLOC_SIZE.

TemporaryMemoryState is initialized with a minimum reservation guarantee, which is the minimum of MINIMUM_RESERVATION_PER_STATE_PER_THREAD and MINIMUM_RESERVATION_MEMORY_LIMIT_DIVISOR.


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