Skip to content

Commit

Permalink
pr
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Dec 2, 2024
1 parent 3680d89 commit 3ea1f9f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
7 changes: 0 additions & 7 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8087,31 +8087,24 @@ impl AccountsDb {
transactions: Option<&'a [&'a SanitizedTransaction]>,
reclaim: StoreReclaims,
update_index_thread_selection: UpdateIndexThreadSelection,
bank_hash_stats: Option<&AtomicBankHashStats>,
) {
// If all transactions in a batch are errored,
// it's possible to get a store with no accounts.
if accounts.is_empty() {
return;
}

let mut stats = BankHashStats::default();
let mut total_data = 0;
(0..accounts.len()).for_each(|index| {
accounts.account(index, |account| {
total_data += account.data().len();
stats.update(&account);
})
});

self.stats
.store_total_data
.fetch_add(total_data as u64, Ordering::Relaxed);

if let Some(bank_hash_stats) = bank_hash_stats {
bank_hash_stats.accumulate(&stats);
}

// we use default hashes for now since the same account may be stored to the cache multiple times
self.store_accounts_unfrozen(
accounts,
Expand Down
62 changes: 52 additions & 10 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ use {
account_locks::validate_account_locks,
accounts::{AccountAddressFilter, Accounts, PubkeyAccountSlot},
accounts_db::{
stats::{AtomicBankHashStats, BankHashStats},
AccountStorageEntry, AccountsDb, AccountsDbConfig, CalcAccountsHashDataSource,
DuplicatesLtHash, OldStoragesPolicy, PubkeyHashAccount,
stats::BankHashStats, AccountStorageEntry, AccountsDb, AccountsDbConfig,
CalcAccountsHashDataSource, DuplicatesLtHash, OldStoragesPolicy, PubkeyHashAccount,
VerifyAccountsHashAndLamportsConfig,
},
accounts_hash::{
Expand Down Expand Up @@ -1016,6 +1015,49 @@ pub struct ProcessedTransactionCounts {
pub signature_count: u64,
}

#[derive(Debug, Default)]
pub struct AtomicBankHashStats {
pub num_updated_accounts: AtomicU64,
pub num_removed_accounts: AtomicU64,
pub num_lamports_stored: AtomicU64,
pub total_data_len: AtomicU64,
pub num_executable_accounts: AtomicU64,
}

impl AtomicBankHashStats {
pub fn new(stat: &BankHashStats) -> Self {
AtomicBankHashStats {
num_updated_accounts: AtomicU64::new(stat.num_updated_accounts),
num_removed_accounts: AtomicU64::new(stat.num_removed_accounts),
num_lamports_stored: AtomicU64::new(stat.num_lamports_stored),
total_data_len: AtomicU64::new(stat.total_data_len),
num_executable_accounts: AtomicU64::new(stat.num_executable_accounts),
}
}

pub fn accumulate(&self, other: &BankHashStats) {
self.num_updated_accounts
.fetch_add(other.num_updated_accounts, Relaxed);
self.num_removed_accounts
.fetch_add(other.num_removed_accounts, Relaxed);
self.total_data_len.fetch_add(other.total_data_len, Relaxed);
self.num_lamports_stored
.fetch_add(other.num_lamports_stored, Relaxed);
self.num_executable_accounts
.fetch_add(other.num_executable_accounts, Relaxed);
}

pub fn load(&self) -> BankHashStats {
BankHashStats {
num_updated_accounts: self.num_updated_accounts.load(Relaxed),
num_removed_accounts: self.num_removed_accounts.load(Relaxed),
num_lamports_stored: self.num_lamports_stored.load(Relaxed),
total_data_len: self.total_data_len.load(Relaxed),
num_executable_accounts: self.num_executable_accounts.load(Relaxed),
}
}
}

impl Bank {
fn default_with_accounts(accounts: Accounts) -> Self {
let mut bank = Self {
Expand Down Expand Up @@ -2794,9 +2836,7 @@ impl Bank {
assert!(!self.freeze_started());
thread_pool.install(|| {
stake_rewards.par_chunks(512).for_each(|chunk| {
self.rc
.accounts
.store_accounts_cached((slot, chunk), Some(&self.bank_hash_stats));
self.rc.accounts.store_accounts_cached((slot, chunk));
})
});
metrics
Expand Down Expand Up @@ -4134,7 +4174,6 @@ impl Bank {
self.rc.accounts.store_cached(
(self.slot(), accounts_to_store.as_slice()),
transactions.as_deref(),
Some(&self.bank_hash_stats),
);
});

Expand Down Expand Up @@ -5102,18 +5141,21 @@ impl Bank {
assert!(!self.freeze_started());
let mut m = Measure::start("stakes_cache.check_and_store");
let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();

let mut stats = BankHashStats::default();
(0..accounts.len()).for_each(|i| {
accounts.account(i, |account| {
stats.update(&account);
self.stakes_cache.check_and_store(
account.pubkey(),
&account,
new_warmup_cooldown_rate_epoch,
)
})
});
self.rc
.accounts
.store_accounts_cached(accounts, Some(&self.bank_hash_stats));
self.bank_hash_stats.accumulate(&stats);

self.rc.accounts.store_accounts_cached(accounts);
m.stop();
self.rc
.accounts
Expand Down
1 change: 0 additions & 1 deletion runtime/src/bank/accounts_lt_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ mod tests {
[(&keypair5.pubkey(), &prev_account5.clone().unwrap())].as_slice(),
),
None,
None,
);

// freeze the bank to trigger update_accounts_lt_hash() to run
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13449,7 +13449,7 @@ fn test_rehash_bad() {
account.checked_add_lamports(ten_sol).unwrap();
bank.rc
.accounts
.store_accounts_cached((bank.slot(), [(&pubkey, &account)].as_slice()), None);
.store_accounts_cached((bank.slot(), [(&pubkey, &account)].as_slice()));

// let the show begin
bank.rehash();
Expand Down
4 changes: 2 additions & 2 deletions runtime/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_shrink_and_clean() {
for (pubkey, account) in alive_accounts.iter_mut() {
account.checked_sub_lamports(1).unwrap();

accounts.store_cached((current_slot, &[(&*pubkey, &*account)][..]), None, None);
accounts.store_cached((current_slot, &[(&*pubkey, &*account)][..]), None);
}
accounts.add_root(current_slot);
accounts.flush_accounts_cache(true, Some(current_slot));
Expand Down Expand Up @@ -127,7 +127,7 @@ fn test_bad_bank_hash() {
.iter()
.map(|idx| (&accounts_keys[*idx].0, &accounts_keys[*idx].1))
.collect();
db.store_cached((some_slot, &account_refs[..]), None, None);
db.store_cached((some_slot, &account_refs[..]), None);
for pass in 0..2 {
for (key, account) in &account_refs {
if pass == 1 {
Expand Down

0 comments on commit 3ea1f9f

Please sign in to comment.