Skip to content

Commit

Permalink
add range start/end macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Zimmerman, Kevin authored and Zimmerman, Kevin committed Jun 23, 2023
1 parent 81130fd commit b1f91e0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/arbitrary_range/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::thread::{self, sleep};
use std::time::Duration;

use nvtx::{range_end, range_start};

/// Create an arbitrary concurrency range on the NVTX layer. </br>
/// This program should be ran from a profiling application like NVIDIA Nsight
/// Systems!
fn main() {
let id = thread::spawn(|| {
let id = range_start!("My Range");
sleep(Duration::from_millis(100)); // Expensive operation here
id
})
.join()
.unwrap();

range_end!(id);
}
10 changes: 10 additions & 0 deletions nvtx-sys/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ int ffi_range_pop()
return nvtxRangePop();
}

int ffi_range_start(const char *message)
{
return nvtxRangeStartA(message);
}

void ffi_range_end(int id)
{
return nvtxRangeEnd(id);
}

void ffi_mark(const char *message)
{
return nvtxMarkA(message);
Expand Down
24 changes: 24 additions & 0 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ pub fn _range_pop() -> i32 {
unsafe { ffi_range_pop() }
}

#[doc(hidden)]
pub fn _range_start<M: Display>(message: M) -> i32 {
#[link(name = "nvtx")]
extern "C" {
fn ffi_range_start(
message: *const ::core::ffi::c_char,
) -> ::core::ffi::c_int;
}
let message: CString = CString::new(message.to_string())
.expect("message contains null terminator");
// SAFETY: calling foreign function is unsafe
unsafe { ffi_range_start(message.as_ptr()) }
}

#[doc(hidden)]
pub fn _range_end(id: i32) -> i32 {
#[link(name = "nvtx")]
extern "C" {
fn ffi_range_end(id: ::core::ffi::c_int) -> ::core::ffi::c_int;
}
// SAFETY: calling foreign function is unsafe
unsafe { ffi_range_end(id) }
}

#[doc(hidden)]
pub fn _mark<M: Display>(message: M) {
#[link(name = "nvtx")]
Expand Down
44 changes: 44 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,50 @@ macro_rules! range_pop {
};
}

/// Starts a range that can occur on a different thread than the end.
///
/// # Arguments
///
/// * `message` - The event message associated to this range event.
///
/// # Returns
///
/// * returns the `id` of the range.
///
/// # Examples
///
/// ```
/// use nvtx::{range_end, range_start};
/// let id = range_start!("Hello World!");
/// range_end!(id);
/// ```
#[macro_export]
macro_rules! range_start{
($($tt:tt)*) => {
$crate::__private::_range_start(::core::format_args!($($tt)*))
};
}

/// Ends a range that can occur on a different thread than the start.
///
/// # Arguments
///
/// * `id` - The event id associated to this range event.
///
/// # Examples
///
/// ```
/// use nvtx::{range_end, range_start};
/// let id = range_start!("Hello World!");
/// range_end!(id);
/// ```
#[macro_export]
macro_rules! range_end {
($tt:tt) => {
$crate::__private::_range_end($tt)
};
}

/// Annotate an OS thread with a name.
///
/// # Examples
Expand Down

0 comments on commit b1f91e0

Please sign in to comment.