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::EvictionQueue Struct Reference
Collaboration diagram for duckdb::EvictionQueue:

Public Member Functions

 EvictionQueue (const vector< FileBufferType > &file_buffer_types_p)
bool AddToEvictionQueue (BufferEvictionNode &&node)
bool TryDequeueWithLock (BufferEvictionNode &node)
 Tries to dequeue an element from the eviction queue, but only after acquiring the purge queue lock.
void Purge ()
 Garbage collect dead nodes in the eviction queue.
template<typename FN>
void IterateUnloadableBlocks (FN fn)
void IncrementDeadNodes ()
 Increment the dead node counter in the purge queue.
void DecrementDeadNodes ()
 Decrement the dead node counter in the purge queue.
bool HasFileBufferType (const FileBufferType &type) const

Public Attributes

const vector< FileBufferType > file_buffer_types
 The type of the buffers in this queue and helper function (both for verification only).
eviction_queue_t q
 The concurrent queue.
atomic< idx_t > debug_eviction_queue_sleep
 Debug-only: atomic holding the eviction queue sleep setting.

Private Member Functions

void PurgeIteration (const idx_t purge_size)
 Bulk purge dead nodes from the eviction queue. Then, enqueue those that are still alive.

Private Attributes

atomic< idx_t > evict_queue_insertions
 Total number of insertions into the eviction queue. This guides the schedule for calling PurgeQueue.
atomic< idx_t > total_dead_nodes
mutex purge_lock
vector< BufferEvictionNode > purge_nodes
 A pre-allocated vector of eviction nodes. We reuse this to keep the allocation overhead of purges small.

Static Private Attributes

static constexpr idx_t INSERT_INTERVAL = 4096
 We trigger a purge of the eviction queue every INSERT_INTERVAL insertions.
static constexpr idx_t PURGE_SIZE_MULTIPLIER = 2
 We multiply the base purge size by this value.
static constexpr idx_t EARLY_OUT_MULTIPLIER = 4
static constexpr idx_t ALIVE_NODE_MULTIPLIER = 4

Constructor & Destructor Documentation

◆ EvictionQueue()

duckdb::EvictionQueue::EvictionQueue ( const vector< FileBufferType > & file_buffer_types_p)
inlineexplicit
387289 total_dead_nodes(0) {
387290 }
atomic< idx_t > evict_queue_insertions
Total number of insertions into the eviction queue. This guides the schedule for calling PurgeQueue.
Definition duckdb.cpp:387341
atomic< idx_t > total_dead_nodes
Definition duckdb.cpp:387344
const vector< FileBufferType > file_buffer_types
The type of the buffers in this queue and helper function (both for verification only).
Definition duckdb.cpp:387321
atomic< idx_t > debug_eviction_queue_sleep
Debug-only: atomic holding the eviction queue sleep setting.
Definition duckdb.cpp:387325

Member Function Documentation

◆ AddToEvictionQueue()

bool duckdb::EvictionQueue::AddToEvictionQueue ( BufferEvictionNode && node)

Add a buffer handle to the eviction queue. Returns true, if the queue is ready to be purged, and false otherwise.

387353 {
387354 q.enqueue(std::move(node));
387355 return ++evict_queue_insertions % INSERT_INTERVAL == 0;
387356}
static constexpr idx_t INSERT_INTERVAL
We trigger a purge of the eviction queue every INSERT_INTERVAL insertions.
Definition duckdb.cpp:387329
eviction_queue_t q
The concurrent queue.
Definition duckdb.cpp:387323

◆ TryDequeueWithLock()

bool duckdb::EvictionQueue::TryDequeueWithLock ( BufferEvictionNode & node)

Tries to dequeue an element from the eviction queue, but only after acquiring the purge queue lock.

387358 {
387359 lock_guard<mutex> lock(purge_lock);
387360 return q.try_dequeue(node);
387361}
mutex purge_lock
Definition duckdb.cpp:387348

◆ Purge()

void duckdb::EvictionQueue::Purge ( )

Garbage collect dead nodes in the eviction queue.

387363 {
387364 // only one thread purges the queue, all other threads early-out
387365 unique_lock<mutex> guard(purge_lock, std::try_to_lock);
387366 if (!guard.owns_lock()) {
387367 return;
387368 }
387369
387370 // we purge INSERT_INTERVAL * PURGE_SIZE_MULTIPLIER nodes
387372
387373 // get an estimate of the queue size as-of now
387374 idx_t approx_q_size = q.size_approx();
387375
387376 // early-out, if the queue is not big enough to justify purging
387377 // - we want to keep the LRU characteristic alive
387378 if (approx_q_size < purge_size * EARLY_OUT_MULTIPLIER) {
387379 return;
387380 }
387381
387382 // There are two types of situations.
387383
387384 // For most scenarios, purging INSERT_INTERVAL * PURGE_SIZE_MULTIPLIER nodes is enough.
387385 // Purging more nodes than we insert also counters oscillation for scenarios where most nodes are dead.
387386 // If we always purge slightly more, we trigger a purge less often, as we purge below the trigger.
387387
387388 // However, if the pressure on the queue becomes too contested, we need to purge more aggressively,
387389 // i.e., we actively seek a specific number of dead nodes to purge. We use the total number of existing dead nodes.
387390 // We detect this situation by observing the queue's ratio between alive vs. dead nodes. If the ratio of alive vs.
387391 // dead nodes grows faster than we can purge, we keep purging until we hit one of the following conditions.
387392
387393 // 2.1. We're back at an approximate queue size less than purge_size * EARLY_OUT_MULTIPLIER.
387394 // 2.2. We're back at a ratio of 1*alive_node:ALIVE_NODE_MULTIPLIER*dead_nodes.
387395 // 2.3. We've purged the entire queue: max_purges is zero. This is a worst-case scenario,
387396 // guaranteeing that we always exit the loop.
387397
387398 idx_t max_purges = approx_q_size / purge_size;
387399 while (max_purges != 0) {
387400 PurgeIteration(purge_size);
387401
387402 // update relevant sizes and potentially early-out
387403 approx_q_size = q.size_approx();
387404
387405 // early-out according to (2.1)
387406 if (approx_q_size < purge_size * EARLY_OUT_MULTIPLIER) {
387407 break;
387408 }
387409
387410 idx_t approx_dead_nodes = total_dead_nodes;
387411 approx_dead_nodes = approx_dead_nodes > approx_q_size ? approx_q_size : approx_dead_nodes;
387412 idx_t approx_alive_nodes = approx_q_size - approx_dead_nodes;
387413
387414 // early-out according to (2.2)
387415 if (approx_alive_nodes * (ALIVE_NODE_MULTIPLIER - 1) > approx_dead_nodes) {
387416 break;
387417 }
387418
387419 max_purges--;
387420 }
387421}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static constexpr idx_t ALIVE_NODE_MULTIPLIER
Definition duckdb.cpp:387337
static constexpr idx_t EARLY_OUT_MULTIPLIER
Definition duckdb.cpp:387334
static constexpr idx_t PURGE_SIZE_MULTIPLIER
We multiply the base purge size by this value.
Definition duckdb.cpp:387331
void PurgeIteration(const idx_t purge_size)
Bulk purge dead nodes from the eviction queue. Then, enqueue those that are still alive.
Definition duckdb.cpp:387423
Here is the call graph for this function:

◆ IterateUnloadableBlocks()

template<typename FN>
void duckdb::EvictionQueue::IterateUnloadableBlocks ( FN fn)
387662 {
387663 auto debug_sleep_micros = debug_eviction_queue_sleep.load(std::memory_order_relaxed);
387664 for (;;) {
387665 // get a block to unpin from the queue
387666 BufferEvictionNode node;
387667 if (!q.try_dequeue(node)) {
387668 // we could not dequeue any eviction node, so we try one more time,
387669 // but more aggressively
387670 if (!TryDequeueWithLock(node)) {
387671 return;
387672 }
387673 }
387674
387675 // get a reference to the underlying block pointer
387676 auto handle = node.TryGetBlockMemory();
387677 if (debug_sleep_micros > 0) {
387678 // Debug race conditions regarding the ownership of the BlockMemory.
387679 // Note that for this to trigger we need at least one purge iteration with the setting active.
387680 ThreadUtil::SleepMicroSeconds(debug_sleep_micros);
387681 }
387682 if (!handle) {
387684 continue;
387685 }
387686
387687 // we might be able to free this block: grab the mutex and check if we can free it
387688 auto lock = handle->GetLock();
387689 if (!node.CanUnload(*handle)) {
387690 // something changed in the mean-time, bail out
387692 continue;
387693 }
387694
387695 if (!fn(node, handle, lock)) {
387696 break;
387697 }
387698 }
387699}
void DecrementDeadNodes()
Decrement the dead node counter in the purge queue.
Definition duckdb.cpp:387308
bool TryDequeueWithLock(BufferEvictionNode &node)
Tries to dequeue an element from the eviction queue, but only after acquiring the purge queue lock.
Definition duckdb.cpp:387358

◆ IncrementDeadNodes()

void duckdb::EvictionQueue::IncrementDeadNodes ( )
inline

Increment the dead node counter in the purge queue.

387304 {
387306 }
Here is the caller graph for this function:

◆ DecrementDeadNodes()

void duckdb::EvictionQueue::DecrementDeadNodes ( )
inline

Decrement the dead node counter in the purge queue.

387308 {
387310 }

◆ HasFileBufferType()

bool duckdb::EvictionQueue::HasFileBufferType ( const FileBufferType & type) const
inline
387311 {
387312 return std::find(file_buffer_types.begin(), file_buffer_types.end(), type) != file_buffer_types.end();
387313 }

◆ PurgeIteration()

void duckdb::EvictionQueue::PurgeIteration ( const idx_t purge_size)
private

Bulk purge dead nodes from the eviction queue. Then, enqueue those that are still alive.

387423 {
387424 // if this purge is significantly smaller or bigger than the previous purge, then
387425 // we need to resize the purge_nodes vector. Note that this barely happens, as we
387426 // purge queue_insertions * PURGE_SIZE_MULTIPLIER nodes
387427 idx_t previous_purge_size = purge_nodes.size();
387428 if (purge_size < previous_purge_size / 2 || purge_size > previous_purge_size) {
387429 purge_nodes.resize(purge_size);
387430 }
387431
387432 // bulk purge
387433 const idx_t actually_dequeued = q.try_dequeue_bulk(purge_nodes.begin(), purge_size);
387434
387435 // retrieve all alive nodes that have been wrongly dequeued
387436 idx_t alive_nodes = 0;
387437 auto debug_sleep_micros = debug_eviction_queue_sleep.load(std::memory_order_relaxed);
387438 for (idx_t i = 0; i < actually_dequeued; i++) {
387439 auto &node = purge_nodes[i];
387440 auto handle = node.TryGetBlockMemory();
387441 if (debug_sleep_micros > 0) {
387442 // Debug race conditions regarding the ownership of the BlockMemory.
387443 ThreadUtil::SleepMicroSeconds(debug_sleep_micros);
387444 }
387445 if (handle) {
387446 purge_nodes[alive_nodes++] = std::move(node);
387447 }
387448 }
387449
387450 // bulk re-add (TODO order them by timestamp to better retain the LRU behavior)
387451 q.enqueue_bulk(purge_nodes.begin(), alive_nodes);
387452
387453 total_dead_nodes -= actually_dequeued - alive_nodes;
387454}
vector< BufferEvictionNode > purge_nodes
A pre-allocated vector of eviction nodes. We reuse this to keep the allocation overhead of purges sma...
Definition duckdb.cpp:387350
Here is the caller graph for this function:

Member Data Documentation

◆ EARLY_OUT_MULTIPLIER

idx_t duckdb::EvictionQueue::EARLY_OUT_MULTIPLIER = 4
staticconstexprprivate

We multiply the purge size by this value to determine early-outs. This is the minimum queue size. We never purge below this point.

◆ ALIVE_NODE_MULTIPLIER

idx_t duckdb::EvictionQueue::ALIVE_NODE_MULTIPLIER = 4
staticconstexprprivate

We multiply the approximate alive nodes by this value to test whether our total dead nodes exceed their allowed ratio. Must be greater than 1.

◆ total_dead_nodes

atomic<idx_t> duckdb::EvictionQueue::total_dead_nodes
private

Total dead nodes in the eviction queue. There are two scenarios in which a node dies: (1) we destroy its block handle, or (2) we insert a newer version into the eviction queue.

◆ purge_lock

mutex duckdb::EvictionQueue::purge_lock
private

Locked, if a queue purge is currently active or we're trying to forcefully evict a node. Only lets a single thread enter the purge phase.


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