Extract read-only cache stats (#32789)

* extract read-only cache stat

* rename stats

---------

Co-authored-by: HaoranYi <haoran.yi@solana.com>
This commit is contained in:
HaoranYi 2023-08-10 13:42:09 -05:00 committed by GitHub
parent c9f7cb5b7e
commit d26e3ff22b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 22 deletions

View File

@ -26,6 +26,32 @@ struct ReadOnlyAccountCacheEntry {
index: AtomicU32, // Index of the entry in the eviction queue. 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)] #[derive(Debug)]
pub struct ReadOnlyAccountsCache { pub struct ReadOnlyAccountsCache {
cache: DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>, cache: DashMap<ReadOnlyCacheKey, ReadOnlyAccountCacheEntry>,
@ -37,10 +63,9 @@ pub struct ReadOnlyAccountsCache {
queue: Mutex<IndexList<ReadOnlyCacheKey>>, queue: Mutex<IndexList<ReadOnlyCacheKey>>,
max_data_size: usize, max_data_size: usize,
data_size: AtomicUsize, data_size: AtomicUsize,
hits: AtomicU64,
misses: AtomicU64, // Performance statistics
evicts: AtomicU64, stats: ReadOnlyCacheStats,
load_us: AtomicU64,
} }
impl ReadOnlyAccountsCache { impl ReadOnlyAccountsCache {
@ -50,10 +75,7 @@ impl ReadOnlyAccountsCache {
cache: DashMap::default(), cache: DashMap::default(),
queue: Mutex::<IndexList<ReadOnlyCacheKey>>::default(), queue: Mutex::<IndexList<ReadOnlyCacheKey>>::default(),
data_size: AtomicUsize::default(), data_size: AtomicUsize::default(),
hits: AtomicU64::default(), stats: ReadOnlyCacheStats::default(),
misses: AtomicU64::default(),
evicts: AtomicU64::default(),
load_us: AtomicU64::default(),
} }
} }
@ -63,10 +85,7 @@ impl ReadOnlyAccountsCache {
self.cache.clear(); self.cache.clear();
self.queue.lock().unwrap().clear(); self.queue.lock().unwrap().clear();
self.data_size.store(0, Ordering::Relaxed); self.data_size.store(0, Ordering::Relaxed);
self.hits.store(0, Ordering::Relaxed); self.stats.reset();
self.misses.store(0, Ordering::Relaxed);
self.evicts.store(0, Ordering::Relaxed);
self.load_us.store(0, Ordering::Relaxed);
} }
/// true if pubkey is in cache at slot /// true if pubkey is in cache at slot
@ -78,7 +97,7 @@ impl ReadOnlyAccountsCache {
let (account, load_us) = measure_us!({ let (account, load_us) = measure_us!({
let key = (pubkey, slot); let key = (pubkey, slot);
let Some(entry) = self.cache.get(&key) else { 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; return None;
}; };
// Move the entry to the end of the queue. // Move the entry to the end of the queue.
@ -91,10 +110,10 @@ impl ReadOnlyAccountsCache {
} }
let account = entry.account.clone(); let account = entry.account.clone();
drop(entry); drop(entry);
self.hits.fetch_add(1, Ordering::Relaxed); self.stats.hits.fetch_add(1, Ordering::Relaxed);
Some(account) Some(account)
}); });
self.load_us.fetch_add(load_us, Ordering::Relaxed); self.stats.load_us.fetch_add(load_us, Ordering::Relaxed);
account account
} }
@ -135,7 +154,7 @@ impl ReadOnlyAccountsCache {
num_evicts += 1; num_evicts += 1;
self.remove(pubkey, slot); 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<AccountSharedData> { pub fn remove(&self, pubkey: Pubkey, slot: Slot) -> Option<AccountSharedData> {
@ -158,12 +177,7 @@ impl ReadOnlyAccountsCache {
} }
pub(crate) fn get_and_reset_stats(&self) -> (u64, u64, u64, u64) { pub(crate) fn get_and_reset_stats(&self) -> (u64, u64, u64, u64) {
let hits = self.hits.swap(0, Ordering::Relaxed); self.stats.get_and_reset_stats()
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)
} }
} }