Expand description
NVTX timeline-annotation helpers for Nsight Systems profiling.
Backed by the nvtx crate, which vendors NVIDIA’s header-only NVTX v3 C API
and compiles it in with cc. There is no link-time dependency on any NVTX shared
library: the v3 headers compile to inert stubs that lazily dlopen the profiler’s
injection library named by NVTX_INJECTION64_PATH the first time an annotation
fires. With no profiler attached, nothing is loaded at all.
This is why v3 matters on CUDA 13: CUDA 13 removed libnvToolsExt.so, which the
older v2 C API (and cudarc’s binding to it) resolved by dlopen — a hard failure
when the file is absent. v3 has no such library to find, so markers work on CUDA 13
natively — the same mechanism TensorRT-LLM’s own Python markers use, which puts both
on one timeline.
§Gating (two-level)
Cargo feature nvtx | DYN_NVTX env | Effect |
|---|---|---|
| off (default) | any | macros compile to nothing; zero overhead |
| on | unset | inlined Relaxed load + branch per site |
| on | 1/true/on/yes | two allocations + an FFI call per annotation |
The disabled-but-compiled-in row is not free, only cheap. Each macro tests
enabled() in the caller, which inlines to a relaxed load of a static AtomicBool
and a branch that predicts perfectly; the name and its core::fmt::Arguments are
built only on the taken side. Verified by disassembling a release build with the
repo’s profiling profile: the disabled path is a load, a test, and a jump to the
function tail, with no call and no Arguments construction.
The enabled path is not cheap: the nvtx crate renders the name with
to_string() and then copies it into a CString, so each annotation costs two heap
allocations on top of the injection-library call. Measured at 4096 concurrent
streams on mock workers, a range costs about 0.1 µs with no profiler attached and
about 1.3 µs under nsys -t nvtx. Keep the second figure in mind when reading a
profile of a per-chunk path: the observer effect is real.
§Caveats inherited from the nvtx crate
-
Range ids are narrowed to
i32.nvtx-sys/export.cdeclaresint ffi_range_start(...)/void ffi_range_end(int), but NVTX’snvtxRangeId_tisuint64_t. If an injection library ever handed back an id with bits above 31 set, the id passed tonvtxRangeEndwould differ from the one returned and the range would never close — an unterminated bar on the timeline.Measured against Nsight Systems 2026.4.1: ids are allocated sequentially from 1, so a 300,000-range capture (200k raw start/end pairs plus 100k guard-managed ranges) saw a maximum id of 200,000, zero negative ids, and all 300,000 ranges closed. The narrowing has ~2^31 of headroom per process at that allocation rate and does not bite in practice. Re-check if the injection library ever changes its id scheme; widening it means a local
ccshim over the v3 headers, since the narrowing is in the vendored C, not in the Rust declarations [ffi] corrects. -
Interior NULs in names are sanitized, not fatal. The upstream crate builds its
CStringwithCString::new(..).expect(..), so an interpolated name carrying a NUL would panic the annotated request path. [ffi] below owns that conversion instead and substitutes U+FFFD, because a profiling aid must never be able to abort the work it is measuring. -
The upstream
ffi_range_endbinding does not match its C definition. Innvtx1.3.0,nvtx-sys/export.cdefinesvoid ffi_range_end(int)whilesrc/bindings.rsdeclares it-> c_int. Every guarded range end would cross that mismatched ABI, so [ffi] declares the whole surface itself, matchingexport.cexactly.
§Usage
let _r = dynamo_nvtx_range!("preprocess.tokenize"); // RAII — ends at scope end
let _r = dynamo_nvtx_range!("request.{}", request_id); // format args also work
dynamo_nvtx_push!("codec.encode");
dynamo_nvtx_pop!();
dynamo_nvtx_mark!("stream.first_token");
dynamo_nvtx_name_thread!("tokio-worker-0");§Range flavors
[dynamo_nvtx_range!] opens an NVTX start/end range, which carries an explicit
id and is therefore correct in async code: the guard may be created on one tokio
worker thread and dropped on another after an .await. [dynamo_nvtx_push!] /
[dynamo_nvtx_pop!] use the thread-local nested range stack and must be paired on
the same thread with no .await in between.
§Name arguments
A string literal is treated as a format string, so a literal containing {} or
{name} interpolates rather than appearing verbatim. dynamo_nvtx_range!("map{}")
fails to compile; dynamo_nvtx_range!("a{b}") silently captures a local b. Use
dynamo_nvtx_range!("{}", "map{}") for a literal brace.
§Build
cargo build --profile profiling --features dynamo-runtime/nvtxPrefer dynamo-runtime/nvtx over a bare --features nvtx at the workspace root:
the latter also selects the nvtx feature of kvbm-engine, whose markers have no
DYN_NVTX runtime switch and would fire unconditionally.
Structs§
- Nvtx
Range Guard - Zero-sized no-op guard used when the
nvtxfeature is off.
Constants§
- ENV_VAR
- Name of the runtime switch. Shared verbatim with the Python layer
(
dynamo.common.utils.nvtx_utils), which parses it with the same vocabulary.
Functions§
- enabled
- Returns
truewhen thenvtxfeature is compiled in andDYN_NVTXis set. - init
- Initialise the NVTX subsystem from the
ENV_VARenvironment variable. Must be called once at runtime startup before any annotation macros fire. - mark_
impl - Record an instantaneous NVTX marker on the calling thread’s timeline.
No-op (compiled out) when the
nvtxfeature is off. - name_
current_ thread_ impl - Name the current OS thread in the Nsight Systems timeline.
No-op (compiled out) when the
nvtxfeature is off. - pop_
impl - Pop the innermost NVTX range from the calling thread’s stack.
No-op (compiled out) when the
nvtxfeature is off. - push_
impl - Push an NVTX range onto the calling thread’s stack.
No-op (compiled out) when the
nvtxfeature is off.