baracuda_driver/profiler.rs
1//! Thin wrappers over `cuProfilerStart` / `cuProfilerStop`. These tell
2//! external profilers (Nsight, CUPTI) which sections of a program are
3//! interesting; they do not produce output themselves.
4//!
5//! Wrap a region with `start()` / `stop()` around the work you want
6//! profiled, or use [`section`] which drops an RAII guard:
7//!
8//! ```no_run
9//! # fn demo() -> baracuda_driver::Result<()> {
10//! let _guard = baracuda_driver::profiler::section()?;
11//! // ... profiled work ...
12//! # Ok(())
13//! # }
14//! ```
15
16use baracuda_cuda_sys::driver;
17
18use crate::error::{check, Result};
19
20/// Request that the external profiler begin sampling now.
21pub fn start() -> Result<()> {
22 let d = driver()?;
23 let cu = d.cu_profiler_start()?;
24 check(unsafe { cu() })
25}
26
27/// Request that the external profiler stop sampling now.
28pub fn stop() -> Result<()> {
29 let d = driver()?;
30 let cu = d.cu_profiler_stop()?;
31 check(unsafe { cu() })
32}
33
34/// Convenience guard: starts profiling on construction, stops on drop.
35pub fn section() -> Result<Section> {
36 start()?;
37 Ok(Section { _nonzst: () })
38}
39
40#[derive(Debug)]
41pub struct Section {
42 _nonzst: (),
43}
44
45impl Drop for Section {
46 fn drop(&mut self) {
47 let _ = stop();
48 }
49}