From d26e3ff22bb5024793a8a03ad3303723cd390c66 Mon Sep 17 00:00:00 2001 From: HaoranYi Date: Thu, 10 Aug 2023 13:42:09 -0500 Subject: [PATCH] Extract read-only cache stats (#32789) * extract read-only cache stat * rename stats --------- Co-authored-by: HaoranYi --- accounts-db/src/read_only_accounts_cache.rs | 58 +++++++++++++-------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/accounts-db/src/read_only_accounts_cache.rs b/accounts-db/src/read_only_accounts_cache.rs index bd1720b760..b416531881 100644 --- a/accounts-db/src/read_only_accounts_cache.rs +++ b/accounts-db/src/read_only_accounts_cache.rs @@ -26,6 +26,32 @@ struct ReadOnlyAccountCacheEntry { index: AtomicU32, // Index of the entry in the eviction queue. } +#[derive(Default, Debug)] +struct ReadOnlyCacheStats { + hits: AtomicU64, + misses: AtomicU64, + evicts: AtomicU64, + load_us: AtomicU64, +} + +impl ReadOnlyCacheStats { + fn reset(&self) { + self.hits.store(0, Ordering::Relaxed); + self.misses.store(0, Ordering::Relaxed); + self.evicts.store(0, Ordering::Relaxed); + self.load_us.store(0, Ordering::Relaxed); + } + + fn get_and_reset_stats(&self) -> (u64, u64, u64, u64) { + let hits = self.hits.swap(0, Ordering::Relaxed); + let misses = self.misses.swap(0, Ordering::Relaxed); + let evicts = self.evicts.swap(0, Ordering::Relaxed); + let load_us = self.load_us.swap(0, Ordering::Relaxed); + + (hits, misses, evicts, load_us) + } +} + #[derive(Debug)] pub struct ReadOnlyAccountsCache { cache: DashMap, @@ -37,10 +63,9 @@ pub struct ReadOnlyAccountsCache { queue: Mutex>, max_data_size: usize, data_size: AtomicUsize, - hits: AtomicU64, - misses: AtomicU64, - evicts: AtomicU64, - load_us: AtomicU64, + + // Performance statistics + stats: ReadOnlyCacheStats, } impl ReadOnlyAccountsCache { @@ -50,10 +75,7 @@ impl ReadOnlyAccountsCache { cache: DashMap::default(), queue: Mutex::>::default(), data_size: AtomicUsize::default(), - hits: AtomicU64::default(), - misses: AtomicU64::default(), - evicts: AtomicU64::default(), - load_us: AtomicU64::default(), + stats: ReadOnlyCacheStats::default(), } } @@ -63,10 +85,7 @@ impl ReadOnlyAccountsCache { self.cache.clear(); self.queue.lock().unwrap().clear(); self.data_size.store(0, Ordering::Relaxed); - self.hits.store(0, Ordering::Relaxed); - self.misses.store(0, Ordering::Relaxed); - self.evicts.store(0, Ordering::Relaxed); - self.load_us.store(0, Ordering::Relaxed); + self.stats.reset(); } /// true if pubkey is in cache at slot @@ -78,7 +97,7 @@ impl ReadOnlyAccountsCache { let (account, load_us) = measure_us!({ let key = (pubkey, slot); let Some(entry) = self.cache.get(&key) else { - self.misses.fetch_add(1, Ordering::Relaxed); + self.stats.misses.fetch_add(1, Ordering::Relaxed); return None; }; // Move the entry to the end of the queue. @@ -91,10 +110,10 @@ impl ReadOnlyAccountsCache { } let account = entry.account.clone(); drop(entry); - self.hits.fetch_add(1, Ordering::Relaxed); + self.stats.hits.fetch_add(1, Ordering::Relaxed); Some(account) }); - self.load_us.fetch_add(load_us, Ordering::Relaxed); + self.stats.load_us.fetch_add(load_us, Ordering::Relaxed); account } @@ -135,7 +154,7 @@ impl ReadOnlyAccountsCache { num_evicts += 1; self.remove(pubkey, slot); } - self.evicts.fetch_add(num_evicts, Ordering::Relaxed); + self.stats.evicts.fetch_add(num_evicts, Ordering::Relaxed); } pub fn remove(&self, pubkey: Pubkey, slot: Slot) -> Option { @@ -158,12 +177,7 @@ impl ReadOnlyAccountsCache { } pub(crate) fn get_and_reset_stats(&self) -> (u64, u64, u64, u64) { - let hits = self.hits.swap(0, Ordering::Relaxed); - let misses = self.misses.swap(0, Ordering::Relaxed); - let evicts = self.evicts.swap(0, Ordering::Relaxed); - let load_us = self.load_us.swap(0, Ordering::Relaxed); - - (hits, misses, evicts, load_us) + self.stats.get_and_reset_stats() } }