Skip to main content

cu_profiler_core/parser/
compute_budget.rs

1//! Compute-budget heuristics derived from logs.
2//!
3//! The Solana runtime does not log the requested compute-unit limit directly.
4//! What it *does* expose is the budget figure `Y` in
5//! `consumed X of Y compute units`; for the outermost invocation that equals the
6//! transaction's available budget. We use the maximum observed budget as the
7//! best log-only estimate of the requested limit, and flag it as an estimate.
8
9use crate::parser::solana_logs::LogEvent;
10
11/// The Compute Budget program ID.
12pub const COMPUTE_BUDGET_PROGRAM_ID: &str = "ComputeBudget111111111111111111111111111111";
13
14/// Best log-derived estimate of the requested compute-unit limit, if any
15/// `consumed … of Y …` line was seen.
16#[must_use]
17pub fn estimated_requested_limit(events: &[LogEvent]) -> Option<u64> {
18    events
19        .iter()
20        .filter_map(|e| match e {
21            LogEvent::Consumed { budget, .. } => Some(*budget),
22            _ => None,
23        })
24        .max()
25}
26
27/// Whether the Compute Budget program was invoked in this transaction.
28#[must_use]
29pub fn used_compute_budget_program(events: &[LogEvent]) -> bool {
30    events.iter().any(|e| match e {
31        LogEvent::Invoke { program_id, .. } => program_id == COMPUTE_BUDGET_PROGRAM_ID,
32        _ => false,
33    })
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::parser::solana_logs::lex;
40
41    #[test]
42    fn estimates_limit_from_max_budget() {
43        let lines = vec![
44            "Program A invoke [1]".to_string(),
45            "Program B invoke [2]".to_string(),
46            "Program B consumed 3000 of 197000 compute units".to_string(),
47            "Program A consumed 12000 of 200000 compute units".to_string(),
48        ];
49        let events: Vec<LogEvent> = lex(&lines).events().cloned().collect();
50        assert_eq!(estimated_requested_limit(&events), Some(200_000));
51    }
52}