FAQ
Frequently asked questions about hotpath-rs.
What is the overhead of profiling with hotpath?
With the hotpath Cargo feature disabled, there is no overhead at all. Every macro compiles to a no-op, and none of the library dependencies are built.
With the feature enabled, the overhead is a few tens of nanoseconds per instrumented operation. For example, #[hotpath::measure] adds ~40 ns per function call, and lock and channel wrappers add 30-90 ns per operation. Events are recorded into lock-free per-thread queues and processed by background workers, so the instrumented code never waits on a lock.
For very hot code paths, you can lower the cost further with time sampling. It measures durations for only a fraction of calls and keeps the call counts exact. See Profiling overhead for full benchmark numbers and methodology.
What types of metrics can be profiled?
hotpath-rs can profile:
- Functions - execution time, call counts, percentiles, and memory allocations of sync and async functions.
- CPU usage - sampling-based CPU profiling powered by samply.
- Instruction counts - per-function instruction counts with Valgrind’s Callgrind, highly repeatable for deterministic workloads, for low-noise regression checks in CI.
- Threads - per-thread CPU usage and memory statistics.
- Async data flow - channel throughput and queue depth, streams, and futures.
- Locks - wait and hold times of mutexes and read-write locks.
- SQL queries - query timing for sqlx, Diesel, and Toasty.
- HTTP requests - outgoing requests made with reqwest or ureq, grouped by endpoint.
- HTTP server routes - response times and error counts per axum route.
- I/O - operation counts, bytes, transfer rates, and latency of files, sockets, and other streams.
- Tokio runtime - worker thread utilization, task scheduling, and queue depths.
All of these metrics can also be exported to Prometheus and Grafana.
How do I profile async Rust and Tokio applications?
Add #[hotpath::measure] to an async fn to record its wall-clock time and allocations, just like a sync function. Use #[hotpath::measure(future = true)] to also track the future itself: poll counts, poll durations, and whether each call completed or was cancelled. See Async functions for details.
Async function profiling works on any async runtime, including Tokio and smol, with no runtime-specific feature flag. Enable the tokio feature only for Tokio-specific integrations:
- Tokio runtime metrics - call
hotpath::tokio_runtime!()in your async main to monitor worker thread utilization, task scheduling, and queue depths. - Channels - wrap Tokio channels with
channel!to track throughput and queue depth. - Locks - wrap
tokio::sync::MutexandRwLockto measure wait and hold times.
You can also monitor streams and futures with the stream! and future! macros.
How do I profile memory allocations?
Enable the hotpath-alloc feature:
cargo run --features='hotpath-alloc'
hotpath then installs a tracking global allocator and reports allocation counts and bytes for every instrumented function. Async functions are measured per poll, so allocations are attributed correctly even when a future moves between Tokio worker threads.
By default, each function reports only its own allocations, excluding nested instrumented calls. Set HOTPATH_ALLOC_CUMULATIVE=true to include them. If your program uses a custom allocator like jemalloc or mimalloc, pass it with #[hotpath::main(allocator = ...)]. See Memory and allocations profiling for details.
The threads view complements per-function stats with per-thread allocated and deallocated bytes and process RSS. It’s useful for spotting memory leaks in long-running applications.
Is it safe to profile production applications?
Yes. Every instrumented subsystem is covered by an integration test suite, and the profiling pipeline is designed to keep a low and predictable overhead.
To profile production safely, follow these guidelines:
- Run your test suite with profiling enabled. Use
cargo test --features=hotpathto confirm that your application behaves the same with instrumentation turned on. - Don’t instrument very short, very hot functions. A function that runs in the low microseconds and is called millions of times contributes a large share of the total processing time, so even ~40 ns per call can add up. Instrument larger units of work instead, or use time sampling to reduce the cost.
- Review the metrics server. By default, a profiled application exposes an HTTP metrics server on
localhost:6770, which feeds the TUI console. SetHOTPATH_METRICS_SERVER_OFF=trueto disable it,HOTPATH_METRICS_PORTto change the port, orHOTPATH_METRICS_AUTH_TOKENto require a token on every request. See Configuration for all options.
How is hotpath-rs different from Criterion.rs?
Criterion.rs is a statistics-driven benchmarking library for Rust. The two tools answer different questions about the same benchmark: Criterion.rs tells you how fast it runs, and hotpath-rs shows where the CPU instructions, time and allocations go inside it. They work together, and since hotpath can profile Criterion benchmarks directly, you don’t have to choose between them.
Criterion.rs runs the code you choose in a dedicated benchmark harness (cargo bench), many times over, and applies statistical analysis to the execution time. Its strengths:
- Precise measurements with warmup, outlier detection, and confidence intervals.
- Detects whether, and by how much, performance changed since the previous run.
- Benchmarks anything from a single function to larger workloads, including async code on Tokio, smol, and other executors.
- HTML reports and plots, runs on stable Rust, and benchmarks live in
benches/, so your application code stays untouched.
Criterion.rs measures the workload as a whole: it tells you the benchmark got slower, not which function inside it did, or what it allocated. By default it measures wall-clock time, so results on shared CI runners are affected by CPU frequency scaling, scheduling, and other workloads.
hotpath-rs instruments the code and reports what happens while a workload runs. Its strengths:
- Profiles many functions at once and shows which of them dominate the total execution time and the allocations.
- Collects signals beyond timing: memory allocations, SQL queries, HTTP calls, lock contention, channel queues, and I/O. Counts like executed queries or allocated bytes usually stay the same between runs of the same workload, even when timings are noisy.
- Instruction counts in CI. Under Valgrind’s Callgrind, the
hotpath-callgrindfeature reports the instructions each function executed instead of elapsed time. For a deterministic workload the counts repeat closely between runs (not guaranteed identical), so small changes stay visible on shared GitHub Actions runners. Timing under Callgrind is meaningless, so a merged report takes its timing from a separate native run of the same build and shows it next to the counts.
Its trade-offs are a small per-call overhead and no statistical analysis of timings. Callgrind slows the program down substantially and runs on x86_64 and ARM64 Linux only; ARM64 support is highly experimental: it needs a patched Valgrind, because stock Valgrind counts instructions incorrectly there, which is a proof of concept likely to return invalid counts, and hotpath 0.31.0 or newer. Counts measure executed instructions, not elapsed time: fewer instructions don’t necessarily mean faster code, time spent waiting on I/O or locks is not counted, and counts from different architectures are not comparable. hotpath’s background collector and sampler threads are excluded from the counts, but the instrumentation executed on application threads still adds to them.
The two fit together: hotpath::criterion profiles existing Criterion benchmarks directly, so a Criterion benchmark can be the workload of hotpath’s native and Callgrind runs. Converting one usually takes small import and dependency changes, not a rewrite, and plain cargo bench, with Criterion’s statistical analysis, keeps working unchanged whenever HOTPATH_CRITERION_ITERATIONS is unset. For unbiased Criterion timings, build those runs without hotpath’s profiling features: with them on, instrumentation overhead still applies. Keep your existing Criterion benchmarks, and use hotpath to understand what happens inside them; the next question has the details.
Gungraun (formerly Iai-Callgrind) is an alternative, dedicated Callgrind benchmarking framework, while hotpath integrates instruction profiling into existing Criterion benchmarks.
Can I use hotpath with my existing Criterion benchmarks?
Yes. hotpath’s Criterion adapter supports Criterion 0.5, 0.6, 0.7 and 0.8, each behind its own cargo feature (criterion-0-5 to criterion-0-8), for sync and async (to_async) benchmarks:
- Switch the imports and macros of a benchmark file to
hotpath::criterion(Criterion,BenchmarkId,criterion_group!,criterion_main!); what the benchmark measures stays the same. - Setting
HOTPATH_CRITERION_ITERATIONSturns on fixed-iteration profiling: the selected benchmark’s routine runs exactly that many times and writes one hotpath report. Each run selects one benchmark, in Criterion’s test mode. WithHOTPATH_CRITERION_ITERATIONSunset, Criterion runs normally. For unbiased Criterion timing measurements, build without hotpath’s profiling features. - A native run reports timing and allocations, a run under Callgrind reports instruction counts, and the two merge into one report for hotpath-cloud.
- The report records the benchmark id, the iteration count and the Criterion version in
meta.criterion. It needs hotpath0.31.0or newer.
Criterion benchmarks has the setup, the supported iter* methods and the native and Callgrind commands.
How is hotpath-rs different from hyperfine?
hyperfine is a command-line benchmarking tool that measures the performance of whole commands.
hyperfine treats the program as a black box. Its strengths:
- Works with any command or binary, in any language, with no code changes.
- Runs each command multiple times with statistical analysis and outlier detection, and supports warmup runs and
--preparecommands for cold-cache benchmarks. - Compares several commands side by side, scans over parameters, and exports results to JSON, CSV, and Markdown.
- Since version 2.0, reports peak memory usage alongside wall-clock time by default, and can optionally measure hardware performance counters such as CPU cycles and instructions, where the platform supports them.
hyperfine measures whole-command performance and doesn’t attribute costs to the functions, queries, or locks inside the program.
hotpath-rs looks inside the program. It reports per-function timing and allocations, along with SQL queries, HTTP calls, locks, channels, and I/O, so you can see where the time is spent and what changed between two versions. In CI, it can also count executed instructions per function under Callgrind, without hardware performance counters. In exchange, it requires adding instrumentation macros to your code, works only for Rust programs, and adds a small overhead to every instrumented call.
hyperfine is a great fit for end-to-end benchmarks of CLI tools and whole programs, and hotpath-rs helps explain the numbers it produces. The allocation overhead benchmarks of hotpath-rs itself are timed with hyperfine.
How is hotpath-rs different from tokio-console?
tokio-console is the official diagnostics and debugging tool for async Rust applications running on Tokio. The two tools answer different questions, and they work well side by side.
tokio-console looks at the runtime from the inside. It shows the lifecycle of individual Tokio tasks, with poll counts and busy, scheduled, and idle time, plus waker activity and Tokio resources such as timers, semaphores, and tokio::sync primitives. It also warns about patterns like lost wakers, excessive self-wakes, and tasks that never yield. It’s an interactive tool built for live inspection, and it requires the console-subscriber crate, building with --cfg tokio_unstable, and the tokio-console client connected to the running process.
hotpath-rs looks at application-level performance. It measures function timings and allocations, channel throughput and queue depth, lock wait and hold times, poll counts and lifecycle of instrumented futures and streams, as well as SQL queries, HTTP calls, and I/O. Channel and lock instrumentation isn’t limited to Tokio, and also covers std, crossbeam, flume, and others. Async function profiling works on any runtime, and Tokio runtime metrics add worker and queue statistics. It’s enabled with a cargo feature and is a no-op when the feature is disabled. It prints a static report on exit, can be monitored live in the TUI console, and its JSON reports can be compared between commits in CI. It only sees the code you instrument, and it doesn’t provide tokio-console’s per-task lifecycle and waker diagnostics.
Some examples of which tool answers which question:
| Question | Tool |
|---|---|
| Which task never gets woken up again? | tokio-console |
| Which task blocks a worker thread with a long poll? | tokio-console |
| Which channel’s queue keeps growing, and how long do messages wait? | hotpath-rs |
| Which lock do threads wait on the longest, and how long is it held? | hotpath-rs |
| Did this PR make an endpoint slower or add SQL queries? | hotpath-rs |
Use tokio-console when you suspect the runtime itself (starved or stuck tasks, misbehaving wakers), and use hotpath-rs to find out which part of your application code is slow, congested, or contended. Both can run in the same application.