Skip to main content

singe_cuda/
profile.rs

1//! CUDA profiler control helpers.
2
3use singe_cuda_sys::profiler;
4
5use crate::{error::Result, try_ffi};
6
7#[derive(Debug)]
8pub struct Profiler {
9    active: bool,
10}
11
12impl Profiler {
13    /// Starts profiler collection and returns a guard that stops it on drop.
14    pub fn create() -> Result<Self> {
15        start()?;
16        Ok(Self { active: true })
17    }
18
19    pub const fn is_active(&self) -> bool {
20        self.active
21    }
22
23    /// Stops profiler collection before the guard is dropped.
24    pub fn stop(mut self) -> Result<()> {
25        if self.active {
26            stop()?;
27            self.active = false;
28        }
29        Ok(())
30    }
31}
32
33impl Drop for Profiler {
34    fn drop(&mut self) {
35        if self.active {
36            let _ = stop();
37            self.active = false;
38        }
39    }
40}
41
42pub fn start() -> Result<()> {
43    unsafe {
44        try_ffi!(profiler::cuProfilerStart())?;
45    }
46    Ok(())
47}
48
49pub fn stop() -> Result<()> {
50    unsafe {
51        try_ffi!(profiler::cuProfilerStop())?;
52    }
53    Ok(())
54}