#[cfg(feature = "nvtx")]
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(feature = "nvtx")]
static NVTX_ENABLED: AtomicBool = AtomicBool::new(false);
pub fn init() {
#[cfg(feature = "nvtx")]
{
let enabled = std::env::var("DYN_ENABLE_RUST_NVTX")
.map(|v| matches!(v.to_lowercase().as_str(), "1" | "true" | "yes" | "on"))
.unwrap_or(false);
NVTX_ENABLED.store(enabled, Ordering::Relaxed);
if enabled {
tracing::info!("NVTX annotations enabled (DYN_ENABLE_RUST_NVTX)");
}
}
}
#[inline(always)]
pub fn enabled() -> bool {
#[cfg(feature = "nvtx")]
{
return NVTX_ENABLED.load(Ordering::Relaxed);
}
#[allow(unreachable_code)]
false
}
#[inline(always)]
pub fn push_impl(name: &str) {
#[cfg(feature = "nvtx")]
{
if NVTX_ENABLED.load(Ordering::Relaxed) {
cudarc::nvtx::result::range_push(name);
}
}
let _ = name;
}
#[inline(always)]
pub fn pop_impl() {
#[cfg(feature = "nvtx")]
{
if NVTX_ENABLED.load(Ordering::Relaxed) {
cudarc::nvtx::result::range_pop();
}
}
}
#[inline(always)]
pub fn name_current_thread_impl(name: &str) {
#[cfg(feature = "nvtx")]
{
if NVTX_ENABLED.load(Ordering::Relaxed) {
#[cfg(target_os = "linux")]
let tid = unsafe { libc::syscall(libc::SYS_gettid) as u32 };
#[cfg(not(target_os = "linux"))]
let tid = 0u32;
cudarc::nvtx::result::name_os_thread(tid, name);
}
}
let _ = name;
}
#[cfg(feature = "nvtx")]
pub struct NvtxRangeGuard {
active: bool,
}
#[cfg(not(feature = "nvtx"))]
pub struct NvtxRangeGuard;
impl NvtxRangeGuard {
#[doc(hidden)]
pub fn new(name: &str) -> Self {
#[cfg(feature = "nvtx")]
{
let active = NVTX_ENABLED.load(Ordering::Relaxed);
if active {
cudarc::nvtx::result::range_push(name);
}
return NvtxRangeGuard { active };
}
#[cfg(not(feature = "nvtx"))]
{
let _ = name;
NvtxRangeGuard {}
}
}
}
#[cfg(feature = "nvtx")]
impl Drop for NvtxRangeGuard {
fn drop(&mut self) {
if self.active {
cudarc::nvtx::result::range_pop();
}
}
}
#[cfg(not(feature = "nvtx"))]
impl Drop for NvtxRangeGuard {
fn drop(&mut self) {}
}
#[macro_export]
macro_rules! dynamo_nvtx_push {
($name:expr) => {
$crate::nvtx::push_impl($name)
};
}
#[macro_export]
macro_rules! dynamo_nvtx_pop {
() => {
$crate::nvtx::pop_impl()
};
}
#[macro_export]
macro_rules! dynamo_nvtx_range {
($name:expr) => {
$crate::nvtx::NvtxRangeGuard::new($name)
};
}
#[macro_export]
macro_rules! dynamo_nvtx_name_thread {
($name:expr) => {
$crate::nvtx::name_current_thread_impl($name)
};
}