Skip to content

Commit

Permalink
add unref_zero_count stat
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Aug 29, 2024
1 parent 39af57f commit 7cfae31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,7 @@ pub struct ShrinkStats {
accounts_loaded: AtomicU64,
purged_zero_lamports: AtomicU64,
accounts_not_found_in_index: AtomicU64,
pub(crate) unref_zero_count: AtomicU64,
}

impl ShrinkStats {
Expand Down Expand Up @@ -2143,6 +2144,11 @@ impl ShrinkStats {
self.accounts_not_found_in_index.swap(0, Ordering::Relaxed),
i64
),
(
"unref_zero_count",
self.unref_zero_count.swap(0, Ordering::Relaxed),
i64
),
);
}
}
Expand Down Expand Up @@ -3315,6 +3321,7 @@ impl AccountsDb {
.iter_mut()
.for_each(|(candidate_pubkey, candidate_info)| {
self.accounts_index.scan(
&self.shrink_stats,
[*candidate_pubkey].iter(),
|_candidate_pubkey, slot_list_and_ref_count, _entry| {
let mut useless = true;
Expand Down Expand Up @@ -3919,6 +3926,7 @@ impl AccountsDb {
let mut index_entries_being_shrunk = Vec::with_capacity(accounts.len());
let latest_full_snapshot_slot = self.latest_full_snapshot_slot();
self.accounts_index.scan(
&self.shrink_stats,
accounts.iter().map(|account| account.pubkey()),
|pubkey, slots_refs, entry| {
let mut result = AccountsIndexScanResult::OnlyKeepInMemoryIfDirty;
Expand Down Expand Up @@ -4188,6 +4196,7 @@ impl AccountsDb {
// we have to unref before we `purge_keys_exact`. Otherwise, we could race with the foreground with tx processing
// reviving this index entry and then we'd unref the revived version, which is a refcount bug.
self.accounts_index.scan(
&self.shrink_stats,
zero_lamport_single_ref_pubkeys.iter().cloned(),
|_pubkey, _slots_refs, _entry| AccountsIndexScanResult::Unref,
Some(AccountsIndexScanResult::Unref),
Expand Down Expand Up @@ -4296,6 +4305,7 @@ impl AccountsDb {
.fetch_add(1, Ordering::Relaxed);

self.accounts_index.scan(
&self.shrink_stats,
shrink_collect.unrefed_pubkeys.into_iter(),
|pubkey, _slot_refs, entry| {
// pubkeys in `unrefed_pubkeys` were unref'd in `shrink_collect` above under the assumption that we would shrink everything.
Expand Down Expand Up @@ -8144,6 +8154,7 @@ impl AccountsDb {
(0..batches).into_par_iter().for_each(|batch| {
let skip = batch * UNREF_ACCOUNTS_BATCH_SIZE;
self.accounts_index.scan(
&self.shrink_stats,
pubkeys
.clone()
.skip(skip)
Expand Down Expand Up @@ -9216,6 +9227,7 @@ impl AccountsDb {
let mut removed_rent_paying = 0;
let mut removed_top_off = 0;
self.accounts_index.scan(
&self.shrink_stats,
pubkeys.iter(),
|pubkey, slots_refs, _entry| {
if let Some((slot_list, _ref_count)) = slots_refs {
Expand Down
3 changes: 3 additions & 0 deletions accounts-db/src/accounts_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) mod in_mem_accounts_index;
use {
crate::{
accounts_db::ShrinkStats,
accounts_index_storage::{AccountsIndexStorage, Startup},
accounts_partition::RentPayingAccountsByPartition,
ancestors::Ancestors,
Expand Down Expand Up @@ -1395,6 +1396,7 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> AccountsIndex<T, U> {
/// None.
pub(crate) fn scan<'a, F, I>(
&self,
stat: &ShrinkStats,
pubkeys: I,
mut callback: F,
avoid_callback_result: Option<AccountsIndexScanResult>,
Expand Down Expand Up @@ -1437,6 +1439,7 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> AccountsIndex<T, U> {
AccountsIndexScanResult::Unref => {
if locked_entry.unref() {
info!("scan: refcount of item already at 0: {pubkey}");
stat.unref_zero_count.fetch_add(1, Ordering::Relaxed);
}
true
}
Expand Down
2 changes: 2 additions & 0 deletions accounts-db/src/ancient_append_vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ impl AccountsDb {
self.thread_pool_clean.install(|| {
accounts_to_combine.into_par_iter().for_each(|combine| {
self.accounts_index.scan(
&self.shrink_stats,
combine.unrefed_pubkeys.into_iter(),
|_pubkey, _slots_refs, entry| {
if let Some(entry) = entry {
Expand Down Expand Up @@ -3886,6 +3887,7 @@ pub mod tests {
};
db.addref_accounts_failed_to_shrink_ancient(accounts_to_combine.accounts_to_combine);
db.accounts_index.scan(
&db.shrink_stats,
unrefed_pubkeys.iter(),
|k, slot_refs, _entry| {
assert_eq!(expected_ref_counts.remove(k).unwrap(), slot_refs.unwrap().1);
Expand Down

0 comments on commit 7cfae31

Please sign in to comment.