introduce StorageSizeAndCount (#19137)

This commit is contained in:
Jeff Washington (jwash)
2021-08-09 22:20:03 -05:00
committed by GitHub
parent 2d9385a65c
commit f506da5e19

View File

@ -175,6 +175,12 @@ struct GenerateIndexTimings {
pub storage_size_accounts_map_flatten_us: u64, pub storage_size_accounts_map_flatten_us: u64,
} }
#[derive(Default, Debug, PartialEq)]
struct StorageSizeAndCount {
pub stored_size: usize,
pub count: usize,
}
impl GenerateIndexTimings { impl GenerateIndexTimings {
pub fn report(&self) { pub fn report(&self) {
datapoint_info!( datapoint_info!(
@ -6065,7 +6071,7 @@ impl AccountsDb {
fn calculate_storage_count_and_alive_bytes( fn calculate_storage_count_and_alive_bytes(
&self, &self,
timings: &mut GenerateIndexTimings, timings: &mut GenerateIndexTimings,
) -> HashMap<usize, (usize, usize)> { ) -> HashMap<AppendVecId, StorageSizeAndCount> {
// look at every account in the account index and calculate for each storage: stored_size and count // look at every account in the account index and calculate for each storage: stored_size and count
let mut storage_size_accounts_map_time = Measure::start("storage_size_accounts_map"); let mut storage_size_accounts_map_time = Measure::start("storage_size_accounts_map");
let mut maps = self let mut maps = self
@ -6083,9 +6089,9 @@ impl AccountsDb {
.for_each(|(_slot, account_entry)| { .for_each(|(_slot, account_entry)| {
let storage_entry_meta = stored_sizes_and_counts let storage_entry_meta = stored_sizes_and_counts
.entry(account_entry.store_id) .entry(account_entry.store_id)
.or_insert((0, 0)); .or_insert_with(StorageSizeAndCount::default);
storage_entry_meta.0 += account_entry.stored_size; storage_entry_meta.stored_size += account_entry.stored_size;
storage_entry_meta.1 += 1; storage_entry_meta.count += 1;
}) })
}); });
stored_sizes_and_counts stored_sizes_and_counts
@ -6100,9 +6106,11 @@ impl AccountsDb {
let mut stored_sizes_and_counts = maps.pop().unwrap_or_default(); let mut stored_sizes_and_counts = maps.pop().unwrap_or_default();
for map in maps { for map in maps {
for (store_id, meta) in map.into_iter() { for (store_id, meta) in map.into_iter() {
let storage_entry_meta = stored_sizes_and_counts.entry(store_id).or_insert((0, 0)); let storage_entry_meta = stored_sizes_and_counts
storage_entry_meta.0 += meta.0; .entry(store_id)
storage_entry_meta.1 += meta.1; .or_insert_with(StorageSizeAndCount::default);
storage_entry_meta.stored_size += meta.stored_size;
storage_entry_meta.count += meta.count;
} }
} }
storage_size_accounts_map_flatten_time.stop(); storage_size_accounts_map_flatten_time.stop();
@ -6113,7 +6121,7 @@ impl AccountsDb {
fn set_storage_count_and_alive_bytes( fn set_storage_count_and_alive_bytes(
&self, &self,
stored_sizes_and_counts: HashMap<usize, (usize, usize)>, stored_sizes_and_counts: HashMap<usize, StorageSizeAndCount>,
timings: &mut GenerateIndexTimings, timings: &mut GenerateIndexTimings,
) { ) {
// store count and size for each storage // store count and size for each storage
@ -6122,7 +6130,9 @@ impl AccountsDb {
for (id, store) in slot_stores.value().read().unwrap().iter() { for (id, store) in slot_stores.value().read().unwrap().iter() {
// Should be default at this point // Should be default at this point
assert_eq!(store.alive_bytes(), 0); assert_eq!(store.alive_bytes(), 0);
if let Some((stored_size, count)) = stored_sizes_and_counts.get(id) { if let Some(StorageSizeAndCount { stored_size, count }) =
stored_sizes_and_counts.get(id)
{
trace!("id: {} setting count: {} cur: {}", id, count, store.count(),); trace!("id: {} setting count: {} cur: {}", id, count, store.count(),);
store.count_and_status.write().unwrap().0 = *count; store.count_and_status.write().unwrap().0 = *count;
store.alive_bytes.store(*stored_size, Ordering::SeqCst); store.alive_bytes.store(*stored_size, Ordering::SeqCst);
@ -11971,7 +11981,16 @@ pub mod tests {
accounts.calculate_storage_count_and_alive_bytes(&mut GenerateIndexTimings::default()); accounts.calculate_storage_count_and_alive_bytes(&mut GenerateIndexTimings::default());
assert_eq!(result.len(), 1); assert_eq!(result.len(), 1);
for (k, v) in result.iter() { for (k, v) in result.iter() {
assert_eq!((k, v), (&0, &(144, 1))); assert_eq!(
(k, v),
(
&0,
&StorageSizeAndCount {
stored_size: 144,
count: 1
}
)
);
} }
} }
@ -12011,7 +12030,16 @@ pub mod tests {
accounts.calculate_storage_count_and_alive_bytes(&mut GenerateIndexTimings::default()); accounts.calculate_storage_count_and_alive_bytes(&mut GenerateIndexTimings::default());
assert_eq!(result.len(), 1); assert_eq!(result.len(), 1);
for (k, v) in result.iter() { for (k, v) in result.iter() {
assert_eq!((k, v), (&0, &(1280, 2))); assert_eq!(
(k, v),
(
&0,
&StorageSizeAndCount {
stored_size: 1280,
count: 2
}
)
);
} }
} }
@ -12034,7 +12062,13 @@ pub mod tests {
// populate based on made up hash data // populate based on made up hash data
let mut hashmap = HashMap::default(); let mut hashmap = HashMap::default();
hashmap.insert(0, (2, 3)); hashmap.insert(
0,
StorageSizeAndCount {
stored_size: 2,
count: 3,
},
);
accounts.set_storage_count_and_alive_bytes(hashmap, &mut GenerateIndexTimings::default()); accounts.set_storage_count_and_alive_bytes(hashmap, &mut GenerateIndexTimings::default());
assert_eq!(accounts.storage.0.len(), 1); assert_eq!(accounts.storage.0.len(), 1);
for slot_stores in accounts.storage.0.iter() { for slot_stores in accounts.storage.0.iter() {