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 3ea1f9f commit 7331d46
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 207 deletions.
10 changes: 5 additions & 5 deletions accounts-db/benches/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ fn bench_delete_dependencies(bencher: &mut Bencher) {
for i in 0..1000 {
let pubkey = solana_sdk::pubkey::new_rand();
let account = AccountSharedData::new(i + 1, 0, AccountSharedData::default().owner());
accounts.store_slow_uncached(i, &pubkey, &account, None);
accounts.store_slow_uncached(i, &old_pubkey, &zero_account, None);
accounts.store_slow_uncached(i, &pubkey, &account);
accounts.store_slow_uncached(i, &old_pubkey, &zero_account);
old_pubkey = pubkey;
accounts.add_root(i);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ where
.take(num_keys)
.collect();
let storable_accounts: Vec<_> = pubkeys.iter().zip(accounts_data.iter()).collect();
accounts.store_accounts_cached((slot, storable_accounts.as_slice()), None);
accounts.store_accounts_cached((slot, storable_accounts.as_slice()));
accounts.add_root(slot);
accounts
.accounts_db
Expand All @@ -176,7 +176,7 @@ where
// Write to a different slot than the one being read from. Because
// there's a new account pubkey being written to every time, will
// compete for the accounts index lock on every store
accounts.store_accounts_cached((slot + 1, new_storable_accounts.as_slice()), None);
accounts.store_accounts_cached((slot + 1, new_storable_accounts.as_slice()));
});
}

Expand Down Expand Up @@ -326,7 +326,7 @@ fn bench_load_largest_accounts(b: &mut Bencher) {
let lamports = rng.gen();
let pubkey = Pubkey::new_unique();
let account = AccountSharedData::new(lamports, 0, &Pubkey::default());
accounts.store_slow_uncached(0, &pubkey, &account, None);
accounts.store_slow_uncached(0, &pubkey, &account);
}
let ancestors = Ancestors::from(vec![0]);
let bank_id = 0;
Expand Down
40 changes: 13 additions & 27 deletions accounts-db/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use {
crate::{
account_locks::{validate_account_locks, AccountLocks},
accounts_db::{
stats::AtomicBankHashStats, AccountStorageEntry, AccountsAddRootTiming, AccountsDb,
LoadHint, LoadedAccount, ScanAccountStorageData, ScanStorageResult,
VerifyAccountsHashAndLamportsConfig,
AccountStorageEntry, AccountsAddRootTiming, AccountsDb, LoadHint, LoadedAccount,
ScanAccountStorageData, ScanStorageResult, VerifyAccountsHashAndLamportsConfig,
},
accounts_index::{IndexKey, ScanConfig, ScanError, ScanResult},
ancestors::Ancestors,
Expand Down Expand Up @@ -552,15 +551,8 @@ impl Accounts {
/// Slow because lock is held for 1 operation instead of many.
/// WARNING: This noncached version is only to be used for tests/benchmarking
/// as bypassing the cache in general is not supported
pub fn store_slow_uncached(
&self,
slot: Slot,
pubkey: &Pubkey,
account: &AccountSharedData,
bank_hash_stats: Option<&AtomicBankHashStats>,
) {
self.accounts_db
.store_uncached(slot, &[(pubkey, account)], bank_hash_stats);
pub fn store_slow_uncached(&self, slot: Slot, pubkey: &Pubkey, account: &AccountSharedData) {
self.accounts_db.store_uncached(slot, &[(pubkey, account)]);
}

/// This function will prevent multiple threads from modifying the same account state at the
Expand Down Expand Up @@ -641,19 +633,13 @@ impl Accounts {
&self,
accounts: impl StorableAccounts<'a>,
transactions: Option<&'a [&'a SanitizedTransaction]>,
bank_hash_stats: Option<&AtomicBankHashStats>,
) {
self.accounts_db
.store_cached_inline_update_index(accounts, transactions, bank_hash_stats);
.store_cached_inline_update_index(accounts, transactions);
}

pub fn store_accounts_cached<'a>(
&self,
accounts: impl StorableAccounts<'a>,
bank_hash_stats: Option<&AtomicBankHashStats>,
) {
self.accounts_db
.store_cached(accounts, None, bank_hash_stats)
pub fn store_accounts_cached<'a>(&self, accounts: impl StorableAccounts<'a>) {
self.accounts_db.store_cached(accounts, None)
}

/// Add a slot to root. Root slots cannot be purged
Expand Down Expand Up @@ -784,7 +770,7 @@ mod tests {
let invalid_table_key = Pubkey::new_unique();
let mut invalid_table_account = AccountSharedData::default();
invalid_table_account.set_lamports(1);
accounts.store_slow_uncached(0, &invalid_table_key, &invalid_table_account, None);
accounts.store_slow_uncached(0, &invalid_table_key, &invalid_table_account);

let address_table_lookup = MessageAddressTableLookup {
account_key: invalid_table_key,
Expand All @@ -811,7 +797,7 @@ mod tests {
let invalid_table_key = Pubkey::new_unique();
let invalid_table_account =
AccountSharedData::new(1, 0, &address_lookup_table::program::id());
accounts.store_slow_uncached(0, &invalid_table_key, &invalid_table_account, None);
accounts.store_slow_uncached(0, &invalid_table_key, &invalid_table_account);

let address_table_lookup = MessageAddressTableLookup {
account_key: invalid_table_key,
Expand Down Expand Up @@ -850,7 +836,7 @@ mod tests {
0,
)
};
accounts.store_slow_uncached(0, &table_key, &table_account, None);
accounts.store_slow_uncached(0, &table_key, &table_account);

let address_table_lookup = MessageAddressTableLookup {
account_key: table_key,
Expand Down Expand Up @@ -882,13 +868,13 @@ mod tests {
// Load accounts owned by various programs into AccountsDb
let pubkey0 = solana_sdk::pubkey::new_rand();
let account0 = AccountSharedData::new(1, 0, &Pubkey::from([2; 32]));
accounts.store_slow_uncached(0, &pubkey0, &account0, None);
accounts.store_slow_uncached(0, &pubkey0, &account0);
let pubkey1 = solana_sdk::pubkey::new_rand();
let account1 = AccountSharedData::new(1, 0, &Pubkey::from([2; 32]));
accounts.store_slow_uncached(0, &pubkey1, &account1, None);
accounts.store_slow_uncached(0, &pubkey1, &account1);
let pubkey2 = solana_sdk::pubkey::new_rand();
let account2 = AccountSharedData::new(1, 0, &Pubkey::from([3; 32]));
accounts.store_slow_uncached(0, &pubkey2, &account2, None);
accounts.store_slow_uncached(0, &pubkey2, &account2);

let loaded = accounts.load_by_program_slot(0, Some(&Pubkey::from([2; 32])));
assert_eq!(loaded.len(), 2);
Expand Down
23 changes: 6 additions & 17 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use {
},
accounts_cache::{AccountsCache, CachedAccount, SlotCache},
accounts_db::stats::{
AccountsStats, AtomicBankHashStats, BankHashStats, CleanAccountsStats, FlushStats,
PurgeStats, ShrinkAncientStats, ShrinkStats, ShrinkStatsSub, StoreAccountsTiming,
AccountsStats, CleanAccountsStats, FlushStats, PurgeStats, ShrinkAncientStats,
ShrinkStats, ShrinkStatsSub, StoreAccountsTiming,
},
accounts_file::{
AccountsFile, AccountsFileError, AccountsFileProvider, MatchAccountOwnerError,
Expand Down Expand Up @@ -1865,7 +1865,7 @@ impl solana_frozen_abi::abi_example::AbiExample for AccountsDb {
let some_data_len = 5;
let some_slot: Slot = 0;
let account = AccountSharedData::new(1, some_data_len, &key);
accounts_db.store_uncached(some_slot, &[(&key, &account)], None);
accounts_db.store_uncached(some_slot, &[(&key, &account)]);
accounts_db.add_root(0);

accounts_db
Expand Down Expand Up @@ -8033,50 +8033,40 @@ impl AccountsDb {
&self,
accounts: impl StorableAccounts<'a>,
transactions: Option<&'a [&'a SanitizedTransaction]>,
bank_hash_stat: Option<&AtomicBankHashStats>,
) {
self.store(
accounts,
&StoreTo::Cache,
transactions,
StoreReclaims::Default,
UpdateIndexThreadSelection::PoolWithThreshold,
bank_hash_stat,
);
}

pub(crate) fn store_cached_inline_update_index<'a>(
&self,
accounts: impl StorableAccounts<'a>,
transactions: Option<&'a [&'a SanitizedTransaction]>,
bank_hash_stat: Option<&AtomicBankHashStats>,
) {
self.store(
accounts,
&StoreTo::Cache,
transactions,
StoreReclaims::Default,
UpdateIndexThreadSelection::Inline,
bank_hash_stat,
);
}

/// Store the account update.
/// only called by tests
pub fn store_uncached(
&self,
slot: Slot,
accounts: &[(&Pubkey, &AccountSharedData)],
bank_hash_stat: Option<&AtomicBankHashStats>,
) {
pub fn store_uncached(&self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)]) {
let storage = self.find_storage_candidate(slot);
self.store(
(slot, accounts),
&StoreTo::Storage(&storage),
None,
StoreReclaims::Default,
UpdateIndexThreadSelection::PoolWithThreshold,
bank_hash_stat,
);
}

Expand Down Expand Up @@ -9379,7 +9369,6 @@ impl AccountsDb {
None,
StoreReclaims::Default,
UpdateIndexThreadSelection::PoolWithThreshold,
None,
);
}

Expand Down Expand Up @@ -9572,7 +9561,7 @@ pub mod test_utils {
data_size,
AccountSharedData::default().owner(),
);
accounts.store_slow_uncached(slot, &pubkey, &account, None);
accounts.store_slow_uncached(slot, &pubkey, &account);
pubkeys.push(pubkey);
}
}
Expand All @@ -9583,7 +9572,7 @@ pub mod test_utils {
for pubkey in pubkeys {
let amount = thread_rng().gen_range(0..10);
let account = AccountSharedData::new(amount, 0, AccountSharedData::default().owner());
accounts.store_slow_uncached(slot, pubkey, &account, None);
accounts.store_slow_uncached(slot, pubkey, &account);
}
}
}
22 changes: 11 additions & 11 deletions accounts-db/src/accounts_db/geyser_plugin_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,19 @@ pub mod tests {
let account1 =
AccountSharedData::new(account1_lamports, 1, AccountSharedData::default().owner());
let slot0 = 0;
accounts.store_uncached(slot0, &[(&key1, &account1)], None);
accounts.store_uncached(slot0, &[(&key1, &account1)]);

account1_lamports = 2;
let account1 = AccountSharedData::new(account1_lamports, 1, account1.owner());
accounts.store_uncached(slot0, &[(&key1, &account1)], None);
accounts.store_uncached(slot0, &[(&key1, &account1)]);
let notifier = GeyserTestPlugin::default();

let key2 = solana_sdk::pubkey::new_rand();
let account2_lamports: u64 = 100;
let account2 =
AccountSharedData::new(account2_lamports, 1, AccountSharedData::default().owner());

accounts.store_uncached(slot0, &[(&key2, &account2)], None);
accounts.store_uncached(slot0, &[(&key2, &account2)]);

let notifier = Arc::new(notifier);
accounts.set_geyser_plugin_notifer(Some(notifier.clone()));
Expand Down Expand Up @@ -284,25 +284,25 @@ pub mod tests {
let account1 =
AccountSharedData::new(account1_lamports, 1, AccountSharedData::default().owner());
let slot0 = 0;
accounts.store_uncached(slot0, &[(&key1, &account1)], None);
accounts.store_uncached(slot0, &[(&key1, &account1)]);

let key2 = solana_sdk::pubkey::new_rand();
let account2_lamports: u64 = 200;
let account2 =
AccountSharedData::new(account2_lamports, 1, AccountSharedData::default().owner());
accounts.store_uncached(slot0, &[(&key2, &account2)], None);
accounts.store_uncached(slot0, &[(&key2, &account2)]);

account1_lamports = 2;
let slot1 = 1;
let account1 = AccountSharedData::new(account1_lamports, 1, account1.owner());
accounts.store_uncached(slot1, &[(&key1, &account1)], None);
accounts.store_uncached(slot1, &[(&key1, &account1)]);
let notifier = GeyserTestPlugin::default();

let key3 = solana_sdk::pubkey::new_rand();
let account3_lamports: u64 = 300;
let account3 =
AccountSharedData::new(account3_lamports, 1, AccountSharedData::default().owner());
accounts.store_uncached(slot1, &[(&key3, &account3)], None);
accounts.store_uncached(slot1, &[(&key3, &account3)]);

let notifier = Arc::new(notifier);
accounts.set_geyser_plugin_notifer(Some(notifier.clone()));
Expand Down Expand Up @@ -353,24 +353,24 @@ pub mod tests {
let account1 =
AccountSharedData::new(account1_lamports1, 1, AccountSharedData::default().owner());
let slot0 = 0;
accounts.store_cached((slot0, &[(&key1, &account1)][..]), None, None);
accounts.store_cached((slot0, &[(&key1, &account1)][..]), None);

let key2 = solana_sdk::pubkey::new_rand();
let account2_lamports: u64 = 200;
let account2 =
AccountSharedData::new(account2_lamports, 1, AccountSharedData::default().owner());
accounts.store_cached((slot0, &[(&key2, &account2)][..]), None, None);
accounts.store_cached((slot0, &[(&key2, &account2)][..]), None);

let account1_lamports2 = 2;
let slot1 = 1;
let account1 = AccountSharedData::new(account1_lamports2, 1, account1.owner());
accounts.store_cached((slot1, &[(&key1, &account1)][..]), None, None);
accounts.store_cached((slot1, &[(&key1, &account1)][..]), None);

let key3 = solana_sdk::pubkey::new_rand();
let account3_lamports: u64 = 300;
let account3 =
AccountSharedData::new(account3_lamports, 1, AccountSharedData::default().owner());
accounts.store_cached((slot1, &[(&key3, &account3)][..]), None, None);
accounts.store_cached((slot1, &[(&key3, &account3)][..]), None);

assert_eq!(notifier.accounts_notified.get(&key1).unwrap().len(), 2);
assert_eq!(
Expand Down
44 changes: 0 additions & 44 deletions accounts-db/src/accounts_db/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,6 @@ impl BankHashStats {
}
}

#[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, Ordering::Relaxed);
self.num_removed_accounts
.fetch_add(other.num_removed_accounts, Ordering::Relaxed);
self.total_data_len
.fetch_add(other.total_data_len, Ordering::Relaxed);
self.num_lamports_stored
.fetch_add(other.num_lamports_stored, Ordering::Relaxed);
self.num_executable_accounts
.fetch_add(other.num_executable_accounts, Ordering::Relaxed);
}

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

#[derive(Debug, Default)]
pub struct AccountsStats {
pub delta_hash_scan_time_total_us: AtomicU64,
Expand Down
Loading

0 comments on commit 7331d46

Please sign in to comment.