Skip to content

Commit

Permalink
add metric read_only_cache_load_us
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Jul 20, 2023
1 parent 979913e commit fa38a43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
9 changes: 7 additions & 2 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8403,8 +8403,12 @@ impl AccountsDb {

fn report_store_timings(&self) {
if self.stats.last_store_report.should_update(1000) {
let (read_only_cache_hits, read_only_cache_misses, read_only_cache_evicts) =
self.read_only_accounts_cache.get_and_reset_stats();
let (
read_only_cache_hits,
read_only_cache_misses,
read_only_cache_evicts,
read_only_cache_load_us,
) = self.read_only_accounts_cache.get_and_reset_stats();
datapoint_info!(
"accounts_db_store_timings",
(
Expand Down Expand Up @@ -8470,6 +8474,7 @@ impl AccountsDb {
read_only_cache_evicts,
i64
),
("read_only_cache_load_us", read_only_cache_load_us, i64),
(
"calc_stored_meta_us",
self.stats.calc_stored_meta.swap(0, Ordering::Relaxed),
Expand Down
45 changes: 28 additions & 17 deletions runtime/src/read_only_accounts_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use {
dashmap::{mapref::entry::Entry, DashMap},
index_list::{Index, IndexList},
solana_measure::measure_us,
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
clock::Slot,
Expand Down Expand Up @@ -39,6 +40,7 @@ pub(crate) struct ReadOnlyAccountsCache {
hits: AtomicU64,
misses: AtomicU64,
evicts: AtomicU64,
load_us: AtomicU64,
}

impl ReadOnlyAccountsCache {
Expand All @@ -51,6 +53,7 @@ impl ReadOnlyAccountsCache {
hits: AtomicU64::default(),
misses: AtomicU64::default(),
evicts: AtomicU64::default(),
load_us: AtomicU64::default(),
}
}

Expand All @@ -63,6 +66,7 @@ impl ReadOnlyAccountsCache {
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);
}

/// true if pubkey is in cache at slot
Expand All @@ -71,21 +75,27 @@ impl ReadOnlyAccountsCache {
}

pub(crate) fn load(&self, pubkey: Pubkey, slot: Slot) -> Option<AccountSharedData> {
let key = (pubkey, slot);
let Some(mut entry) = self.cache.get_mut(&key) else {
self.misses.fetch_add(1, Ordering::Relaxed);
return None;
};
self.hits.fetch_add(1, Ordering::Relaxed);
// Move the entry to the end of the queue.
// self.queue is modified while holding a reference to the cache entry;
// so that another thread cannot write to the same key.
{
let mut queue = self.queue.lock().unwrap();
queue.remove(entry.index);
entry.index = queue.insert_last(key);
}
Some(entry.account.clone())
let (account, load_us) = measure_us!({
let key = (pubkey, slot);
let Some(mut entry) = self.cache.get_mut(&key) else {
self.misses.fetch_add(1, Ordering::Relaxed);
return None;
};
// Move the entry to the end of the queue.
// self.queue is modified while holding a reference to the cache entry;
// so that another thread cannot write to the same key.
{
let mut queue = self.queue.lock().unwrap();
queue.remove(entry.index);
entry.index = queue.insert_last(key);
}
let account = entry.account.clone();
drop(entry);
self.hits.fetch_add(1, Ordering::Relaxed);
Some(account)
});
self.load_us.fetch_add(load_us, Ordering::Relaxed);
account
}

fn account_size(&self, account: &AccountSharedData) -> usize {
Expand Down Expand Up @@ -147,12 +157,13 @@ impl ReadOnlyAccountsCache {
self.data_size.load(Ordering::Relaxed)
}

pub(crate) fn get_and_reset_stats(&self) -> (u64, u64, u64) {
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)
(hits, misses, evicts, load_us)
}
}

Expand Down

0 comments on commit fa38a43

Please sign in to comment.