Procházet zdrojové kódy

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 b9273377b63b328eebf6fb13303fda51f40c2068.
ebin-mathews před 2 měsíci
rodič
revize
f4598b1256

+ 29 - 1
cost-model/src/cost_tracker.rs

@@ -4,7 +4,10 @@
 //! - add_transaction_cost(&tx_cost), mutable function to accumulate tx_cost to tracker.
 //!
 use {
-    crate::{block_cost_limits::*, transaction_cost::TransactionCost},
+    crate::{
+        block_cost_limits::*, cost_tracker_post_analysis::CostTrackerPostAnalysis,
+        transaction_cost::TransactionCost,
+    },
     solana_metrics::datapoint_info,
     solana_pubkey::Pubkey,
     solana_runtime_transaction::transaction_with_meta::TransactionWithMeta,
@@ -433,6 +436,15 @@ impl CostTracker {
     }
 }
 
+/// Implement the trait for the cost tracker
+/// This is only used for post-analysis to avoid lock contention
+/// Do not use in the hot path
+impl CostTrackerPostAnalysis for CostTracker {
+    fn get_cost_by_writable_accounts(&self) -> &HashMap<Pubkey, u64, ahash::RandomState> {
+        &self.cost_by_writable_accounts
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use {
@@ -998,4 +1010,20 @@ mod tests {
         assert_eq!(0, cost_tracker.vote_cost);
         assert_eq!(0, cost_tracker.allocated_accounts_data_size.0);
     }
+
+    #[test]
+    fn test_get_cost_by_writable_accounts_post_analysis() {
+        let mut cost_tracker = CostTracker::default();
+        let cost = 100u64;
+        let transaction = WritableKeysTransaction(vec![Pubkey::new_unique()]);
+        let tx_cost = simple_transaction_cost(&transaction, cost);
+        cost_tracker.add_transaction_cost(&tx_cost);
+        let cost_by_writable_accounts = cost_tracker.get_cost_by_writable_accounts();
+        assert_eq!(1, cost_by_writable_accounts.len());
+        assert_eq!(cost, *cost_by_writable_accounts.values().next().unwrap());
+        assert_eq!(
+            *cost_by_writable_accounts,
+            cost_tracker.cost_by_writable_accounts
+        );
+    }
 }

+ 8 - 0
cost-model/src/cost_tracker_post_analysis.rs

@@ -0,0 +1,8 @@
+use {solana_pubkey::Pubkey, std::collections::HashMap};
+
+/// Trait to help with post-analysis of a given block
+pub trait CostTrackerPostAnalysis {
+    /// Only use in post-analyze to avoid lock contention
+    /// Do not use in the hot path
+    fn get_cost_by_writable_accounts(&self) -> &HashMap<Pubkey, u64, ahash::RandomState>;
+}

+ 1 - 0
cost-model/src/lib.rs

@@ -4,6 +4,7 @@
 pub mod block_cost_limits;
 pub mod cost_model;
 pub mod cost_tracker;
+pub mod cost_tracker_post_analysis;
 pub mod transaction_cost;
 
 #[cfg_attr(feature = "frozen-abi", macro_use)]