canic_core/macros/
perf.rs

1/// Log elapsed instruction counts since the last `perf!` invocation in this thread.
2///
3/// - Uses a thread-local `PERF_LAST` snapshot.
4/// - Computes `delta = now - last`.
5/// - Prints a human-readable line for debugging.
6///
7/// Intended usage:
8/// - Long-running maintenance tasks where you want *checkpoints* in a single call.
9///
10/// Note: `perf!` is independent of endpoint perf scopes and does not touch the
11/// endpoint stack used by dispatch.
12///
13/// Notes:
14/// - On non-wasm targets, `perf_counter()` returns 0, so this becomes a no-op-ish
15///   counter (still records 0 deltas); this keeps unit tests compiling cleanly.
16#[macro_export]
17macro_rules! perf {
18    ($($label:tt)*) => {{
19        $crate::perf::PERF_LAST.with(|last| {
20            // Use the wrapper so non-wasm builds compile.
21            let now = $crate::perf::perf_counter();
22            let then = *last.borrow();
23            let delta = now.saturating_sub(then);
24
25            // Update last checkpoint.
26            *last.borrow_mut() = now;
27
28            // Format label + pretty-print counters.
29            let label = format!($($label)*);
30            let delta_fmt = $crate::utils::instructions::format_instructions(delta);
31            let now_fmt = $crate::utils::instructions::format_instructions(now);
32
33            // ❌ NO structured recording here
34            // ✔️ Debug log only
35            $crate::log!(
36                Info,
37                Topic::Perf,
38                "{}: '{}' used {}i since last (total: {}i)",
39                module_path!(),
40                label,
41                delta_fmt,
42                now_fmt
43            );
44        });
45    }};
46}