Skip to content

Commit

Permalink
Add pool_size method to MemoryPool (#218)
Browse files Browse the repository at this point in the history
* Add pool_size method to MemoryPool

* Fix

* Fmt
  • Loading branch information
Dandandan authored and joroKr21 committed Apr 5, 2024
1 parent 1fa25ae commit 050e402
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion datafusion/execution/src/memory_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ pub trait MemoryPool: Send + Sync + std::fmt::Debug {

/// Return the total amount of memory reserved
fn reserved(&self) -> usize;

/// Return the configured pool size (if any)
fn pool_size(&self) -> Option<usize>;
}

/// A memory consumer is a named allocation traced by a particular
Expand Down Expand Up @@ -321,7 +324,9 @@ mod tests {

#[test]
fn test_memory_pool_underflow() {
let pool = Arc::new(GreedyMemoryPool::new(50)) as _;
let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(50)) as _;
assert_eq!(pool.pool_size(), Some(50));

let mut a1 = MemoryConsumer::new("a1").register(&pool);
assert_eq!(pool.reserved(), 0);

Expand Down
15 changes: 14 additions & 1 deletion datafusion/execution/src/memory_pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl MemoryPool for UnboundedMemoryPool {
fn reserved(&self) -> usize {
self.used.load(Ordering::Relaxed)
}

fn pool_size(&self) -> Option<usize> {
None
}
}

/// A [`MemoryPool`] that implements a greedy first-come first-serve limit.
Expand Down Expand Up @@ -96,6 +100,10 @@ impl MemoryPool for GreedyMemoryPool {
fn reserved(&self) -> usize {
self.used.load(Ordering::Relaxed)
}

fn pool_size(&self) -> Option<usize> {
Some(self.pool_size)
}
}

/// A [`MemoryPool`] that prevents spillable reservations from using more than
Expand Down Expand Up @@ -229,6 +237,10 @@ impl MemoryPool for FairSpillPool {
let state = self.state.lock();
state.spillable + state.unspillable
}

fn pool_size(&self) -> Option<usize> {
Some(self.pool_size)
}
}

fn insufficient_capacity_err(
Expand All @@ -246,7 +258,8 @@ mod tests {

#[test]
fn test_fair() {
let pool = Arc::new(FairSpillPool::new(100)) as _;
let pool: Arc<dyn MemoryPool> = Arc::new(FairSpillPool::new(100)) as _;
assert_eq!(pool.pool_size(), Some(100));

let mut r1 = MemoryConsumer::new("unspillable").register(&pool);
// Can grow beyond capacity of pool
Expand Down

0 comments on commit 050e402

Please sign in to comment.