Skip to content

Commit dee4f24

Browse files
ebin-mathewsmergify[bot]
authored andcommitted
CostTracker: Add a getter to expose cost by writable accounts (#7920)
* Summary This change adds a new method to the CostTracker that provides a breakdown of compute unit costs for each writable account during block processing. Problem Currently, CostTracker aggregates compute unit costs but doesn't expose a detailed breakdown of costs per account. This makes it difficult to analyze resource consumption patterns and identify which accounts are the biggest resource consumers within a block. Solution A new method, get_cost_by_writable_accounts(), has been added to the CostTracker to address this issue. This method returns a mapping of each writable account to its specific compute unit costs, providing a granular view of resource usage. Testing ✅ The new method was tested to ensure it correctly tracks and returns per-account costs. ✅ Existing CostTracker functionality remains unchanged and unaffected by this addition. ✅ The change was validated to confirm it correctly captures the cost distribution among writable accounts. Type of Change [ ] Bug fix (non-breaking change which fixes an issue) [x] New feature (non-breaking change which adds functionality) [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) [ ] Documentation update Checklist [x] Code compiles without errors [x] New tests have been added and pass [x] No breaking changes [x] Follows existing code style * Use a trait to expose the heavy method * gate behind a feature * Revert "gate behind a feature" This reverts commit b927337. (cherry picked from commit f4598b1)
1 parent 9c181a4 commit dee4f24

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

cost-model/src/cost_tracker.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
//! - add_transaction_cost(&tx_cost), mutable function to accumulate tx_cost to tracker.
55
//!
66
use {
7-
crate::{block_cost_limits::*, transaction_cost::TransactionCost},
7+
crate::{
8+
block_cost_limits::*, cost_tracker_post_analysis::CostTrackerPostAnalysis,
9+
transaction_cost::TransactionCost,
10+
},
811
solana_metrics::datapoint_info,
912
solana_pubkey::Pubkey,
1013
solana_runtime_transaction::transaction_with_meta::TransactionWithMeta,
@@ -418,6 +421,15 @@ impl CostTracker {
418421
}
419422
}
420423

424+
/// Implement the trait for the cost tracker
425+
/// This is only used for post-analysis to avoid lock contention
426+
/// Do not use in the hot path
427+
impl CostTrackerPostAnalysis for CostTracker {
428+
fn get_cost_by_writable_accounts(&self) -> &HashMap<Pubkey, u64, ahash::RandomState> {
429+
&self.cost_by_writable_accounts
430+
}
431+
}
432+
421433
#[cfg(test)]
422434
mod tests {
423435
use {
@@ -983,4 +995,20 @@ mod tests {
983995
assert_eq!(0, cost_tracker.vote_cost);
984996
assert_eq!(0, cost_tracker.allocated_accounts_data_size.0);
985997
}
998+
999+
#[test]
1000+
fn test_get_cost_by_writable_accounts_post_analysis() {
1001+
let mut cost_tracker = CostTracker::default();
1002+
let cost = 100u64;
1003+
let transaction = WritableKeysTransaction(vec![Pubkey::new_unique()]);
1004+
let tx_cost = simple_transaction_cost(&transaction, cost);
1005+
cost_tracker.add_transaction_cost(&tx_cost);
1006+
let cost_by_writable_accounts = cost_tracker.get_cost_by_writable_accounts();
1007+
assert_eq!(1, cost_by_writable_accounts.len());
1008+
assert_eq!(cost, *cost_by_writable_accounts.values().next().unwrap());
1009+
assert_eq!(
1010+
*cost_by_writable_accounts,
1011+
cost_tracker.cost_by_writable_accounts
1012+
);
1013+
}
9861014
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use {solana_pubkey::Pubkey, std::collections::HashMap};
2+
3+
/// Trait to help with post-analysis of a given block
4+
pub trait CostTrackerPostAnalysis {
5+
/// Only use in post-analyze to avoid lock contention
6+
/// Do not use in the hot path
7+
fn get_cost_by_writable_accounts(&self) -> &HashMap<Pubkey, u64, ahash::RandomState>;
8+
}

cost-model/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pub mod block_cost_limits;
55
pub mod cost_model;
66
pub mod cost_tracker;
7+
pub mod cost_tracker_post_analysis;
78
pub mod transaction_cost;
89

910
#[cfg_attr(feature = "frozen-abi", macro_use)]

0 commit comments

Comments
 (0)