use oxicuda_driver::loader::try_driver;
use crate::error::{CudaRtError, CudaRtResult};
pub fn profiler_start() -> CudaRtResult<()> {
let api = try_driver().map_err(|_| CudaRtError::DriverNotAvailable)?;
let f = api.cu_profiler_start.ok_or(CudaRtError::NotSupported)?;
let rc = unsafe { f() };
if rc != 0 {
return Err(CudaRtError::from_code(rc).unwrap_or(CudaRtError::ProfilerDisabled));
}
Ok(())
}
pub fn profiler_stop() -> CudaRtResult<()> {
let api = try_driver().map_err(|_| CudaRtError::DriverNotAvailable)?;
let f = api.cu_profiler_stop.ok_or(CudaRtError::NotSupported)?;
let rc = unsafe { f() };
if rc != 0 {
return Err(CudaRtError::from_code(rc).unwrap_or(CudaRtError::ProfilerDisabled));
}
Ok(())
}
pub struct ProfilerGuard {
active: bool,
}
impl ProfilerGuard {
pub fn new() -> CudaRtResult<Self> {
profiler_start()?;
Ok(Self { active: true })
}
pub fn stop(&mut self) {
if self.active {
let _ = profiler_stop();
self.active = false;
}
}
}
impl Drop for ProfilerGuard {
fn drop(&mut self) {
self.stop();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profiler_start_without_gpu_returns_error() {
let _ = profiler_start();
}
#[test]
fn profiler_stop_without_gpu_returns_error() {
let _ = profiler_stop();
}
}