Skip to main content

baracuda_runtime/
profiler.rs

1//! Runtime-side profiler start / stop — bracket the region you want
2//! Nsight Systems / nvprof to capture.
3
4use baracuda_cuda_sys::runtime::runtime;
5
6use crate::error::{check, Result};
7
8/// `cudaProfilerStart`.
9pub fn start() -> Result<()> {
10    let r = runtime()?;
11    let cu = r.cuda_profiler_start()?;
12    check(unsafe { cu() })
13}
14
15/// `cudaProfilerStop`.
16pub fn stop() -> Result<()> {
17    let r = runtime()?;
18    let cu = r.cuda_profiler_stop()?;
19    check(unsafe { cu() })
20}
21
22/// Run `f` with profiling enabled; always stops on return (even on
23/// panic unwind via the guard's drop).
24pub fn with_profiling<F, R>(f: F) -> Result<R>
25where
26    F: FnOnce() -> R,
27{
28    start()?;
29    struct Guard;
30    impl Drop for Guard {
31        fn drop(&mut self) {
32            let _ = stop();
33        }
34    }
35    let _g = Guard;
36    Ok(f())
37}