1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! CUDA profiler control helpers.
use singe_core::report_cleanup_failure;
use singe_cuda_sys::profiler;
use crate::{error::Result, try_ffi};
/// A guard that starts profiler collection and attempts to stop it when dropped.
///
/// Collection is controlled for CUDA's current context.
/// The guard tracks its own stop obligation; it does not isolate profiling from other guards or native profiler calls.
#[derive(Debug)]
pub struct Profiler {
active: bool,
}
impl Profiler {
/// Starts profiler collection and returns a guard that stops it on drop.
///
/// # Errors
///
/// Returns an error if CUDA cannot start profiler collection for the current context.
pub fn create() -> Result<Self> {
start()?;
Ok(Self { active: true })
}
/// Returns whether this guard still owes a profiler stop call.
///
/// This does not query the active profiling tool or observe profiler calls made elsewhere.
#[must_use]
pub const fn is_active(&self) -> bool {
self.active
}
/// Stops profiler collection before the guard is dropped.
///
/// # Errors
///
/// Returns an error if CUDA cannot stop profiler collection.
/// On failure, dropping the consumed guard attempts the stop call again and reports a second failure only in debug builds.
pub fn stop(mut self) -> Result<()> {
if self.active {
stop()?;
self.active = false;
}
Ok(())
}
}
impl Drop for Profiler {
fn drop(&mut self) {
if self.active {
if let Err(error) = stop() {
report_cleanup_failure("stop", "cuda profiler collection", &error);
}
self.active = false;
}
}
}
/// Enables collection by the active profiling tool for the current CUDA context.
///
/// Has no effect if profiling is already enabled.
///
/// # Errors
///
/// Returns an error if CUDA cannot enable profiler collection.
pub fn start() -> Result<()> {
// SAFETY: the profiler control functions take no arguments and only affect profiling state.
unsafe {
try_ffi!(profiler::cuProfilerStart())?;
}
Ok(())
}
/// Disables collection by the active profiling tool for the current CUDA context.
///
/// Has no effect if profiling is already disabled.
///
/// # Errors
///
/// Returns an error if CUDA cannot disable profiler collection.
pub fn stop() -> Result<()> {
// SAFETY: the profiler control functions take no arguments and only affect profiling state.
unsafe {
try_ffi!(profiler::cuProfilerStop())?;
}
Ok(())
}