accounts index stats (#19797)
This commit is contained in:
committed by
GitHub
parent
ab152f1319
commit
3617d43e76
@@ -1,44 +1,80 @@
|
||||
use crate::accounts_index::{AccountMapEntry, IsCached, WriteAccountMapEntry};
|
||||
use crate::accounts_index_storage::AccountsIndexStorage;
|
||||
use crate::bucket_map_holder::BucketMapHolder;
|
||||
use crate::bucket_map_holder_stats::BucketMapHolderStats;
|
||||
use solana_measure::measure::Measure;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use std::collections::{
|
||||
hash_map::{Entry, Keys},
|
||||
HashMap,
|
||||
};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
type K = Pubkey;
|
||||
|
||||
// one instance of this represents one bin of the accounts index.
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct InMemAccountsIndex<T: IsCached> {
|
||||
// backing store
|
||||
map: HashMap<Pubkey, AccountMapEntry<T>>,
|
||||
storage: Arc<BucketMapHolder>,
|
||||
}
|
||||
|
||||
impl<T: IsCached> InMemAccountsIndex<T> {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(storage: &AccountsIndexStorage) -> Self {
|
||||
Self {
|
||||
map: HashMap::new(),
|
||||
storage: storage.storage().clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_bucket_map_holder() -> Arc<BucketMapHolder> {
|
||||
Arc::new(BucketMapHolder::new())
|
||||
}
|
||||
|
||||
pub fn entry(&mut self, pubkey: Pubkey) -> Entry<K, AccountMapEntry<T>> {
|
||||
self.map.entry(pubkey)
|
||||
let m = Measure::start("entry");
|
||||
let result = self.map.entry(pubkey);
|
||||
let stats = &self.storage.stats;
|
||||
let (count, time) = if matches!(result, Entry::Occupied(_)) {
|
||||
(&stats.gets_from_mem, &stats.get_mem_us)
|
||||
} else {
|
||||
(&stats.gets_missing, &stats.get_missing_us)
|
||||
};
|
||||
Self::update_time_stat(time, m);
|
||||
Self::update_stat(count, 1);
|
||||
result
|
||||
}
|
||||
|
||||
pub fn items(&self) -> Vec<(K, AccountMapEntry<T>)> {
|
||||
Self::update_stat(&self.stats().items, 1);
|
||||
self.map.iter().map(|(k, v)| (*k, v.clone())).collect()
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> Keys<K, AccountMapEntry<T>> {
|
||||
Self::update_stat(&self.stats().keys, 1);
|
||||
self.map.keys()
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &K) -> Option<AccountMapEntry<T>> {
|
||||
self.map.get(key).cloned()
|
||||
let m = Measure::start("get");
|
||||
let result = self.map.get(key).cloned();
|
||||
let stats = self.stats();
|
||||
let (count, time) = if result.is_some() {
|
||||
(&stats.gets_from_mem, &stats.get_mem_us)
|
||||
} else {
|
||||
(&stats.gets_missing, &stats.get_missing_us)
|
||||
};
|
||||
Self::update_time_stat(time, m);
|
||||
Self::update_stat(count, 1);
|
||||
result
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, key: &K) {
|
||||
Self::update_stat(&self.stats().deletes, 1);
|
||||
self.map.remove(key);
|
||||
}
|
||||
|
||||
@@ -83,4 +119,20 @@ impl<T: IsCached> InMemAccountsIndex<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stats(&self) -> &BucketMapHolderStats {
|
||||
&self.storage.stats
|
||||
}
|
||||
|
||||
fn update_stat(stat: &AtomicU64, value: u64) {
|
||||
if value != 0 {
|
||||
stat.fetch_add(value, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_time_stat(stat: &AtomicU64, mut m: Measure) {
|
||||
m.stop();
|
||||
let value = m.as_us();
|
||||
Self::update_stat(stat, value);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user