Skip to content

Commit

Permalink
Add Database::cache_stats()
Browse files Browse the repository at this point in the history
When the "cache_metrics" feature is enable this exposes the number of
cache evictions
  • Loading branch information
cberner committed Dec 16, 2024
1 parent 0fbc63c commit ae8f08c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ impl<'a, K: Key + 'static, V: Key + 'static> Display for MultimapTableDefinition
}
}

/// Information regarding the usage of the in-memory cache
///
/// Note: these metrics are only collected when the "`cache_metrics`" feature is enabled
#[derive(Debug)]
pub struct CacheStats {
pub(crate) evictions: u64,
}

impl CacheStats {
/// Number of times that data has been evicted, due to the cache being full
///
/// To increase the cache size use [`Builder::set_cache_size`]
pub fn evictions(&self) -> u64 {
self.evictions
}
}

pub(crate) struct TransactionGuard {
transaction_tracker: Option<Arc<TransactionTracker>>,
transaction_id: Option<TransactionId>,
Expand Down Expand Up @@ -328,6 +345,13 @@ impl Database {
Self::builder().open(path)
}

/// Information regarding the usage of the in-memory cache
///
/// Note: these metrics are only collected when the "`cache_metrics`" feature is enabled
pub fn cache_stats(&self) -> CacheStats {
self.mem.cache_stats()
}

pub(crate) fn get_memory(&self) -> Arc<TransactionalMemory> {
self.mem.clone()
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
//! [design]: https://github.com/cberner/redb/blob/master/docs/design.md
pub use db::{
Builder, Database, MultimapTableDefinition, MultimapTableHandle, RepairSession, StorageBackend,
TableDefinition, TableHandle, UntypedMultimapTableHandle, UntypedTableHandle,
Builder, CacheStats, Database, MultimapTableDefinition, MultimapTableHandle, RepairSession,
StorageBackend, TableDefinition, TableHandle, UntypedMultimapTableHandle, UntypedTableHandle,
};
pub use error::{
CommitError, CompactionError, DatabaseError, Error, SavepointError, StorageError, TableError,
Expand Down
24 changes: 23 additions & 1 deletion src/tree_store/page_store/cached_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::tree_store::page_store::base::PageHint;
use crate::tree_store::page_store::lru_cache::LRUCache;
use crate::{DatabaseError, Result, StorageBackend, StorageError};
use crate::{CacheStats, DatabaseError, Result, StorageBackend, StorageError};
use std::ops::{Index, IndexMut};
use std::slice::SliceIndex;
#[cfg(feature = "cache_metrics")]
Expand Down Expand Up @@ -187,6 +187,8 @@ pub(super) struct PagedCachedFile {
reads_total: AtomicU64,
#[cfg(feature = "cache_metrics")]
reads_hits: AtomicU64,
#[cfg(feature = "cache_metrics")]
evictions: AtomicU64,
read_cache: Vec<RwLock<LRUCache<Arc<[u8]>>>>,
// TODO: maybe move this cache to WriteTransaction?
write_buffer: Arc<Mutex<LRUWriteCache>>,
Expand Down Expand Up @@ -214,11 +216,23 @@ impl PagedCachedFile {
reads_total: Default::default(),
#[cfg(feature = "cache_metrics")]
reads_hits: Default::default(),
#[cfg(feature = "cache_metrics")]
evictions: Default::default(),
read_cache,
write_buffer: Arc::new(Mutex::new(LRUWriteCache::new())),
})
}

#[allow(clippy::unused_self)]
pub(crate) fn cache_stats(&self) -> CacheStats {
CacheStats {
#[cfg(not(feature = "cache_metrics"))]
evictions: 0,
#[cfg(feature = "cache_metrics")]
evictions: self.evictions.load(Ordering::Acquire),
}
}

pub(crate) fn check_io_errors(&self) -> Result {
self.file.check_failure()
}
Expand Down Expand Up @@ -329,6 +343,10 @@ impl PagedCachedFile {
if cache_size + len > self.max_read_cache_bytes {
while removed < len {
if let Some((_, v)) = write_lock.pop_lowest_priority() {
#[cfg(feature = "cache_metrics")]
{
self.evictions.fetch_add(1, Ordering::Relaxed);
}
removed += v.len();
} else {
break;
Expand Down Expand Up @@ -415,6 +433,10 @@ impl PagedCachedFile {
result?;
self.write_buffer_bytes
.fetch_sub(removed_len, Ordering::Release);
#[cfg(feature = "cache_metrics")]
{
self.evictions.fetch_add(1, Ordering::Relaxed);
}
removed_bytes += removed_len;
} else {
break;
Expand Down
6 changes: 5 additions & 1 deletion src/tree_store/page_store/page_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::tree_store::page_store::layout::DatabaseLayout;
use crate::tree_store::page_store::region::{Allocators, RegionTracker};
use crate::tree_store::page_store::{hash128_with_seed, PageImpl, PageMut};
use crate::tree_store::{Page, PageNumber};
use crate::StorageBackend;
use crate::{CacheStats, StorageBackend};
use crate::{DatabaseError, Result, StorageError};
#[cfg(feature = "logging")]
use log::warn;
Expand Down Expand Up @@ -274,6 +274,10 @@ impl TransactionalMemory {
})
}

pub(crate) fn cache_stats(&self) -> CacheStats {
self.storage.cache_stats()
}

pub(crate) fn check_io_errors(&self) -> Result {
self.storage.check_io_errors()
}
Expand Down

0 comments on commit ae8f08c

Please sign in to comment.