hotpath/function_sink.rs
1//! Public types of the function sink: a callback that receives every
2//! `#[measure]` / `measure_block!` scope as it is entered and exited, so an
3//! external recorder (a timeline viewer, a tracing bridge) can see individual
4//! calls instead of only the aggregated report.
5
6/// How a measured scope relates to the thread that entered it.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum ScopeKind {
10 /// A sync function body. `exit` runs on the thread that called `enter`
11 /// and sync scopes on one thread are strictly nested (LIFO).
12 Sync,
13 /// An `async fn`, a `measure_block!` or the `#[hotpath::main]` wrapper.
14 /// The scope may suspend across `.await` points, so `exit` can run on
15 /// another thread and interleave with other scopes; use the token returned
16 /// by `enter` to pair the two calls.
17 Async,
18}
19
20/// Receives every measured function scope while it runs.
21///
22/// Install one per process with [`set_function_sink`](crate::set_function_sink),
23/// which only reports scopes when the `hotpath-sink` feature is enabled.
24/// Both methods run inline on the instrumented thread, so they must be cheap
25/// and must never block. They are called whether or not a
26/// [`HotpathGuard`](crate::HotpathGuard) is alive and regardless of time
27/// sampling; scopes filtered out by `HOTPATH_FOCUS` are not reported.
28///
29/// ```
30/// use std::sync::atomic::{AtomicU64, Ordering};
31///
32/// struct CountCalls(AtomicU64);
33///
34/// impl hotpath::FunctionSink for CountCalls {
35/// fn enter(&self, _name: &'static str, _kind: hotpath::ScopeKind) -> u64 {
36/// self.0.fetch_add(1, Ordering::Relaxed)
37/// }
38///
39/// fn exit(&self, _name: &'static str, _kind: hotpath::ScopeKind, _token: u64) {}
40/// }
41///
42/// hotpath::set_function_sink(CountCalls(AtomicU64::new(0))).ok();
43/// ```
44pub trait FunctionSink: Send + Sync + 'static {
45 /// A scope named `name` (`module_path::function` or the block label)
46 /// started. The returned token is handed back to the matching
47 /// [`exit`](FunctionSink::exit).
48 fn enter(&self, name: &'static str, kind: ScopeKind) -> u64;
49
50 /// The scope started by the `enter` call that returned `token` finished.
51 fn exit(&self, name: &'static str, kind: ScopeKind, token: u64);
52}
53
54/// Returned by [`set_function_sink`](crate::set_function_sink) when a sink is
55/// already installed. The sink cannot be replaced for the lifetime of the
56/// process.
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub struct FunctionSinkAlreadySet;
59
60impl std::fmt::Display for FunctionSinkAlreadySet {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 f.write_str("a hotpath function sink is already installed")
63 }
64}
65
66impl std::error::Error for FunctionSinkAlreadySet {}
67
68/// No-op without the `hotpath-sink` feature: no scope is reported, so the sink
69/// is dropped and never called. The signature matches the enabled
70/// implementation, so callers need no feature gate of their own.
71///
72/// # Errors
73///
74/// Never fails.
75#[cfg(not(feature = "hotpath-sink"))]
76#[inline]
77pub fn set_function_sink(_sink: impl FunctionSink) -> Result<(), FunctionSinkAlreadySet> {
78 Ok(())
79}