hotpath 0.31.1

One profiler for CPU, time, memory, SQL, and async code - quickly find and debug performance bottlenecks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Deterministic per-function instruction counts (`hotpath-callgrind`).
//!
//! The feature only changes what the measure macros emit: each measured body
//! runs inside a nested `#[inline(never)] fn __hotpath_cg_<hash>` (sync) or is
//! polled through one (async, via [`CallgrindFuture`]), registered here
//! against the measured name. Whether a run reports instructions is decided
//! at runtime: under `valgrind --tool=callgrind` the session zeroes Callgrind's
//! counters at guard creation, and the guard drop writes a dump through a
//! client request, stops instrumenting and attributes the dump's inclusive
//! `Ir` to measured functions by marker (after the `before_shutdown`
//! callback, so its measured calls count too, and after every collector
//! worker's final sweep, so the total does not depend on how far behind the
//! workers were). Every background worker (collectors and samplers) turns
//! Callgrind collection off for itself in each session, so the total counts
//! application threads only. Natively the markers are one extra call per
//! measured invocation and nothing else happens.

use std::collections::HashMap;
use std::future::Future;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{LazyLock, OnceLock};
use std::task::{Context, Poll};

use pin_project_lite::pin_project;

use crate::json::{JsonFunctionEntry, JsonFunctionsList};
use crate::lib_on::functions::FunctionStatsConfig;
use crate::output::ProfilingMode;

pub(crate) mod parser;
pub(crate) mod request;

/// Marker ident -> measured names registered for it. More than one name
/// means `macro_rules!` expanded several measured fns from one source
/// position; their counts cannot be told apart.
static MARKERS: OnceLock<crate::lib_on::MetaRwLock<HashMap<&'static str, Vec<&'static str>>>> =
    OnceLock::new();

/// Maps a marker ident emitted by `#[measure]` to the measured name.
#[doc(hidden)]
pub fn register_callgrind_marker(marker: &'static str, name: &'static str) {
    let _suspend = crate::lib_on::SuspendAllocTracking::new();
    let map =
        MARKERS.get_or_init(|| crate::lib_on::meta_rw_lock!("callgrind_markers", HashMap::new()));
    if let Ok(mut w) = map.write() {
        let names = w.entry(marker).or_default();
        if !names.contains(&name) {
            names.push(name);
        }
    }
}

fn markers() -> HashMap<&'static str, Vec<&'static str>> {
    MARKERS
        .get()
        .and_then(|m| m.read().ok().map(|g| g.clone()))
        .unwrap_or_default()
}

pin_project! {
    /// Polls a measured async body through the call-site marker function, so
    /// the body's poll has a symbol of its own instead of being inlined into
    /// the executor.
    #[doc(hidden)]
    pub struct CallgrindFuture<F, P> {
        #[pin]
        fut: F,
        poll_fn: P,
    }
}

impl<F, P> CallgrindFuture<F, P>
where
    F: Future,
    P: FnMut(Pin<&mut F>, &mut Context<'_>) -> Poll<F::Output>,
{
    #[doc(hidden)]
    pub fn new(fut: F, poll_fn: P) -> Self {
        Self { fut, poll_fn }
    }
}

impl<F, P> Future for CallgrindFuture<F, P>
where
    F: Future,
    P: FnMut(Pin<&mut F>, &mut Context<'_>) -> Poll<F::Output>,
{
    type Output = F::Output;

    #[inline]
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> {
        let this = self.project();
        (this.poll_fn)(this.fut, cx)
    }
}

/// Whether this run counts instructions (the process runs under Valgrind).
pub(crate) fn active() -> bool {
    *crate::lib_on::valgrind::ON_VALGRIND
}

/// `HOTPATH_CALLGRIND_OUT_FILE`: the `--callgrind-out-file` value Valgrind
/// was started with (`%p` and `%q{VAR}` expanded like Valgrind does), default
/// `callgrind.out.%p`. Relative paths resolve against the working directory
/// at the first guard creation, like Valgrind's against its startup cwd.
static OUT_FILE: LazyLock<PathBuf> = LazyLock::new(|| {
    let template = std::env::var("HOTPATH_CALLGRIND_OUT_FILE")
        .ok()
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "callgrind.out.%p".to_string());
    let path = PathBuf::from(expand_out_file(&template, std::process::id()));
    if path.is_absolute() {
        path
    } else {
        std::env::current_dir()
            .map(|cwd| cwd.join(&path))
            .unwrap_or(path)
    }
});

fn expand_out_file(template: &str, pid: u32) -> String {
    let mut out = String::with_capacity(template.len());
    let mut rest = template;
    while let Some(pos) = rest.find('%') {
        out.push_str(&rest[..pos]);
        rest = &rest[pos..];
        if let Some(after) = rest.strip_prefix("%p") {
            out.push_str(&pid.to_string());
            rest = after;
        } else if let Some((var, after)) = rest.strip_prefix("%q{").and_then(|r| r.split_once('}'))
        {
            out.push_str(&std::env::var(var).unwrap_or_default());
            rest = after;
        } else if let Some(after) = rest.strip_prefix("%%") {
            out.push('%');
            rest = after;
        } else {
            out.push('%');
            rest = &rest[1..];
        }
    }
    out.push_str(rest);
    out
}

/// Client-request dumps this process wrote; Callgrind numbers them `.1`,
/// `.2`, ... in the same order.
static DUMPS: AtomicU32 = AtomicU32::new(0);

/// Session state under Valgrind: odd while a session is live, even
/// otherwise. It moves to the next odd value right after START (which resets
/// every thread to collecting) and to the next even value right after STOP,
/// so a collector that sees a new odd value knows it is collecting and one
/// toggle turns it off. One atomic, so a worker never pairs a stale session
/// number with a fresh "live" state. Only the guard thread writes it (one
/// guard at a time). Stays 0 natively, so collectors never toggle there.
static SESSION: AtomicU32 = AtomicU32::new(0);

/// Called at guard creation.
pub(crate) fn start_session() {
    if active() {
        LazyLock::force(&OUT_FILE);
        request::start_instrumentation();
        // Next odd value; skips one if a previous session never finished.
        let state = SESSION.load(Ordering::Relaxed);
        SESSION.store(state + 1 + (state & 1), Ordering::Release);
        request::zero_stats();
    }
}

/// Keeps the owning collector thread out of the session's instruction
/// counts. Toggling is per thread and relative, and every START/STOP resets
/// all threads, so the worker re-toggles once per session it observes. Not
/// `Send`: the state it tracks belongs to the thread that created it.
pub(crate) struct CollectorExclusion {
    seen: u32,
    _thread: std::marker::PhantomData<*const ()>,
}

impl CollectorExclusion {
    pub(crate) fn new() -> Self {
        let mut this = Self {
            seen: 0,
            _thread: std::marker::PhantomData,
        };
        this.refresh();
        this
    }

    /// Call before each sweep. Natively it is one atomic load.
    #[inline]
    pub(crate) fn refresh(&mut self) {
        let state = SESSION.load(Ordering::Acquire);
        // Between sessions (even) there is nothing to exclude, and the next
        // START resets this thread anyway.
        if state & 1 == 1 && state != self.seen {
            self.seen = state;
            request::toggle_collect();
        }
    }
}

pub(crate) struct IcountReport {
    pub(crate) total_ir: u64,
    /// Inclusive `Ir` per measured name.
    pub(crate) by_name: HashMap<&'static str, u64>,
    /// `creator:` of the dump, e.g. `callgrind-3.24.0`.
    pub(crate) tool: Option<String>,
}

/// Called at guard drop once the workers are drained. `None` when not under Valgrind.
#[cfg_attr(feature = "hotpath-meta", hotpath_meta::measure)]
pub(crate) fn finish_session() -> Option<Result<IcountReport, String>> {
    if !active() {
        return None;
    }
    let dump_no = DUMPS.fetch_add(1, Ordering::Relaxed) + 1;
    let tag = format!("hotpath-{}-{dump_no}", std::process::id());
    let tag_c = std::ffi::CString::new(tag.as_str()).expect("tag has no NUL");
    request::dump_and_stop(&tag_c);
    // STOP reset every thread; the session is over (next even value).
    let state = SESSION.load(Ordering::Relaxed);
    SESSION.store(state + (state & 1), Ordering::Release);

    let mut path = OUT_FILE.clone().into_os_string();
    path.push(format!(".{dump_no}"));
    let path = PathBuf::from(path);
    Some(read_report(&path, &tag))
}

fn read_report(path: &std::path::Path, tag: &str) -> Result<IcountReport, String> {
    let input = std::fs::read_to_string(path).map_err(|e| {
        format!(
            "failed to read Callgrind dump {}: {e} (is valgrind running with \
             --tool=callgrind? With a custom --callgrind-out-file, set \
             HOTPATH_CALLGRIND_OUT_FILE to the same value)",
            path.display()
        )
    })?;
    let dump = parser::parse(&input)
        .map_err(|e| format!("failed to parse Callgrind dump {}: {e}", path.display()))?;

    let expected = format!("Client Request: {tag}");
    if dump.trigger.as_deref() != Some(expected.as_str()) {
        return Err(format!(
            "Callgrind dump {} was not written by hotpath (trigger {:?}); other dumps \
             (--dump-every-bb, --dump-before, client requests) shift Callgrind's numbering",
            path.display(),
            dump.trigger.as_deref().unwrap_or("none"),
        ));
    }

    let markers = markers();
    let mut by_name: HashMap<&'static str, u64> = HashMap::new();
    for (marker, ir) in &dump.inclusive {
        match markers.get(marker.as_str()).map(Vec::as_slice) {
            Some([name]) => *by_name.entry(name).or_insert(0) += ir,
            Some(names) if !names.is_empty() => {
                eprintln!(
                    "[hotpath] no instruction counts for {}: generated from one source \
                     position, so their counts cannot be told apart",
                    names.join(", ")
                );
            }
            _ => {}
        }
    }

    Ok(IcountReport {
        total_ir: dump.total_ir,
        by_name,
        tool: dump.creator,
    })
}

/// Calls and id per measured name, from the timing or alloc stats.
pub(crate) type CallCounts = HashMap<&'static str, (u32, u64)>;

#[cfg_attr(feature = "hotpath-meta", hotpath_meta::measure)]
pub(crate) fn build_icount_list(
    report: &IcountReport,
    calls: &CallCounts,
    config: &FunctionStatsConfig,
    current_elapsed_ns: u64,
) -> JsonFunctionsList {
    let precision = crate::output::Precision::for_cloud(config.cloud);

    let mut entries: Vec<(&'static str, u64, u32, u64)> = report
        .by_name
        .iter()
        .filter_map(|(&name, &ir)| {
            // Unfocused or never-called functions have no stats entry.
            let &(id, count) = calls.get(name)?;
            Some((name, ir, id, count))
        })
        .collect();
    entries.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(b.0)));

    let total_count = entries.len();
    if config.limit > 0 {
        entries.truncate(config.limit);
    }
    let included_count = entries.len();

    let data = entries
        .into_iter()
        .map(|(name, ir, id, count)| {
            let percentage = if report.total_ir > 0 {
                (ir as f64 / report.total_ir as f64) * 100.0
            } else {
                0.0
            };
            JsonFunctionEntry {
                id,
                name: name.to_string(),
                calls: count,
                sampled_calls: count,
                // Hundredths: a whole-number average of a short hot function
                // (~33 per call) moves by 3% when the total moves by 1%.
                // Rounded in u128; an average past the u64 hundredths range
                // (an unreachable instruction count) prints as unknown.
                avg: if count > 0 {
                    let hundredths =
                        (u128::from(ir) * 100 + u128::from(count) / 2) / u128::from(count);
                    u64::try_from(hundredths)
                        .map_or_else(|_| "-".to_string(), crate::output::format_icount)
                } else {
                    "-".to_string()
                },
                percentiles: HashMap::new(),
                total: ir.to_string(),
                percent_total: format!("{percentage:.2}%"),
                location: crate::lib_on::locations::lookup_location(name),
            }
        })
        .collect();

    JsonFunctionsList {
        profiling_mode: ProfilingMode::Icount,
        time_elapsed: precision.duration(config.total_elapsed.as_nanos() as u64),
        total_elapsed_ns: current_elapsed_ns,
        total_allocated: None,
        total_icount: Some(report.total_ir),
        description: "Inclusive instructions executed per function (Callgrind Ir).".to_string(),
        caller_name: config.caller_name.to_string(),
        percentiles: Vec::new(),
        data,
        total_count,
        included_count,
        tool: report.tool.clone(),
    }
}

#[cfg(test)]
mod tests {
    use crate::lib_on::functions::callgrind::{
        build_icount_list, expand_out_file, CallCounts, IcountReport,
    };
    use crate::lib_on::functions::FunctionStatsConfig;
    use std::collections::HashMap;
    use std::time::Duration;

    #[test]
    fn icount_json_is_exact_and_ungrouped_while_the_table_groups_it() {
        let report = IcountReport {
            total_ir: 500_000_000,
            by_name: HashMap::from([("produce", 429_926_497), ("push", 33_047_735)]),
            tool: None,
        };
        let calls: CallCounts = HashMap::from([("produce", (1, 4)), ("push", (2, 1_000_000))]);
        let config = FunctionStatsConfig {
            total_elapsed: Duration::from_secs(1),
            percentiles: Vec::new(),
            caller_name: "main",
            limit: 0,
            cloud: true,
        };
        let list = build_icount_list(&report, &calls, &config, 0);
        assert_eq!(list.total_icount, Some(500_000_000));
        let cells: Vec<_> = list
            .data
            .iter()
            .map(|e| (e.name.as_str(), e.avg.as_str(), e.total.as_str()))
            .collect();
        // 33.047735 per call rounds to the hundredth, not truncates.
        assert_eq!(
            cells,
            [
                ("produce", "107481624.25", "429926497"),
                ("push", "33.05", "33047735"),
            ]
        );

        let mut table = Vec::new();
        crate::output_on::display_functions_table_to(&mut table, &list);
        let table = String::from_utf8(table).unwrap();
        for shown in [
            "107,481,624",
            "429,926,497",
            "33.05",
            "33,047,735",
            "1,000,000",
            "Total: 500,000,000",
        ] {
            assert!(table.contains(shown), "{shown} missing from:\n{table}");
        }
        // From 1,000 up the table rounds to a whole instruction.
        assert!(!table.contains("107,481,624.25"), "{table}");
    }

    #[test]
    fn out_file_expands_like_valgrind() {
        assert_eq!(expand_out_file("callgrind.out.%p", 42), "callgrind.out.42");
        assert_eq!(
            expand_out_file("/tmp/%q{HOTPATH_CG_UNSET_VAR}cg.%p.%%x%z%", 7),
            "/tmp/cg.7.%x%z%"
        );
    }
}