Skip to content

Commit

Permalink
Add tracing support
Browse files Browse the repository at this point in the history
Signed-off-by: Will Killian <[email protected]>
  • Loading branch information
willkill07 committed Mar 29, 2024
1 parent 2a22940 commit 687c0d0
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 21 deletions.
116 changes: 114 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ publish.workspace = true
license.workspace = true

[features]
default = ["color-name", "name-current-thread", "cuda", "cuda_runtime"]
default = ["color-name", "name-current-thread", "cuda", "cuda_runtime", "tracing"]
color-name = ["dep:color-name"]
name-current-thread = ["dep:gettid"]
cuda = ["nvtx-sys/cuda"]
cuda_runtime = ["nvtx-sys/cuda_runtime"]
tracing = ["dep:tracing", "dep:tracing-subscriber", "dep:color-name" ]

[dependencies]
color-name = { version = "1.1.0", optional = true }
gettid = { version = "0.1.2", optional = true }
nvtx-sys = { path = "crates/nvtx-sys" }
tracing = { version = "0.1.40", optional = true, features = ["attributes"] }
tracing-subscriber = { version = "0.3.18", optional = true }
widestring = "1.0.2"

[target.'cfg(unix)'.dependencies]
Expand All @@ -59,3 +62,7 @@ required-features = ["name-current-thread"]
[[example]]
name = "domain_threads"
required-features = ["name-current-thread"]

[[example]]
name = "tracing"
required-features = ["tracing"]
35 changes: 35 additions & 0 deletions examples/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use tracing::{info, instrument};
use tracing_subscriber::prelude::*;

#[instrument(fields(color = "goldenrod", category = 1, payload = i))]
fn foo(i: u64) {
for j in 1..=i {
bar(j);
std::thread::sleep(std::time::Duration::from_millis(10 * i));
}
}

#[instrument(fields(color = "plum", category = 1, payload = j))]
fn bar(j: u64) {
for k in 1..=j {
baz(k);
std::thread::sleep(std::time::Duration::from_millis(10 * j));
}
}

#[instrument(fields(color = "salmon", category = 2, payload = k))]
fn baz(k: u64) {
std::thread::sleep(std::time::Duration::from_millis(10 * k));
}

fn main() {
let layer = nvtx::tracing::NvtxLayer::new("nvtx");
tracing_subscriber::registry().with(layer).init();

info!(
message = "At the beginning of the program",
color = "blue",
category = 2
);
foo(10);
}
2 changes: 1 addition & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl TypeValueEncodable for Color {
type Value = u32;

fn encode(&self) -> (Self::Type, Self::Value) {
let as_u32 = u32::from_ne_bytes([self.a, self.r, self.g, self.b]);
let as_u32 = u32::from_be_bytes([self.a, self.r, self.g, self.b]);
(Self::Type::NVTX_COLOR_ARGB, as_u32)
}

Expand Down
4 changes: 2 additions & 2 deletions src/domain/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ use crate::Domain;
/// Represents a category for use with event and range grouping. See [`Domain::register_category`], [`Domain::register_categories`]
#[derive(Debug, Clone, Copy)]
pub struct Category<'a> {
pub(super) id: u32,
pub(super) domain: &'a Domain,
pub(crate) id: u32,
pub(crate) domain: &'a Domain,
}
8 changes: 4 additions & 4 deletions src/domain/event_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::{Color, Domain, Payload, TypeValueEncodable};
/// All attributes that are associated with marks and ranges
#[derive(Debug, Clone)]
pub struct EventAttributes<'a> {
pub(super) category: Option<Category<'a>>,
pub(super) color: Option<Color>,
pub(super) payload: Option<Payload>,
pub(super) message: Option<Message<'a>>,
pub(crate) category: Option<Category<'a>>,
pub(crate) color: Option<Color>,
pub(crate) payload: Option<Payload>,
pub(crate) message: Option<Message<'a>>,
}

impl<'a> EventAttributes<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/local_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::EventArgument;
use crate::Domain;
use std::marker::PhantomData;

/// A RAII-like object for modeling callstack Ranges within a Domain
/// A RAII-like object for modeling callstack (thread-local) Ranges within a Domain
#[derive(Debug)]
pub struct LocalRange<'a> {
pub(super) domain: &'a Domain,
Expand Down
14 changes: 14 additions & 0 deletions src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ impl Domain {
Range::new(arg, self)
}

/// Internal function for starting a range and returning a raw Range Id
pub(crate) fn range_start<'a>(&self, arg: impl Into<EventArgument<'a>>) -> u64 {
let arg = match arg.into() {
EventArgument::Attributes(attr) => attr,
EventArgument::Message(m) => m.into(),
};
nvtx_sys::domain_range_start_ex(self.handle, &arg.encode())
}

/// Internal function for ending a range given a raw Range Id
pub(crate) fn range_end(&self, range_id: u64) {
nvtx_sys::domain_range_end(self.handle, range_id);
}

/// Name a resource
///
/// ```
Expand Down
14 changes: 6 additions & 8 deletions src/domain/range.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::EventArgument;
use crate::Domain;

/// A RAII-like object for modeling start/end Ranges within a Domain
/// A RAII-like object for modeling process-wide Ranges within a Domain
#[derive(Debug)]
pub struct Range<'a> {
pub(super) id: nvtx_sys::RangeId,
Expand All @@ -10,18 +10,16 @@ pub struct Range<'a> {

impl<'a> Range<'a> {
pub(super) fn new(arg: impl Into<EventArgument<'a>>, domain: &'a Domain) -> Range<'a> {
let arg = match arg.into() {
EventArgument::Attributes(attr) => attr,
EventArgument::Message(m) => m.into(),
};
let id = nvtx_sys::domain_range_start_ex(domain.handle, &arg.encode());
Range { id, domain }
Range {
id: domain.range_start(arg),
domain,
}
}
}

impl<'a> Drop for Range<'a> {
fn drop(&mut self) {
nvtx_sys::domain_range_end(self.domain.handle, self.id)
self.domain.range_end(self.id);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
//! runtime resources such as Devices, Events, and Streams. The feature also adds `CudaRuntimeIdentifier`
//! to the [`crate::domain`] module to provide an alternative naming mechanism via [`crate::Domain::name_resource`].
//!
//! * **tracing** -
//! When enabled,
//!
//! ## Platform-specific types
//!
//! * **PThread Resource Naming** -
Expand All @@ -46,12 +49,17 @@
/// color support
pub mod color;

/// specialized types for use within a domain context
pub mod domain;

/// platform native types
pub mod native_types;

#[cfg(feature = "tracing")]
/// tracing support
pub mod tracing;

mod category;
mod event_argument;
mod event_attributes;
Expand Down
2 changes: 1 addition & 1 deletion src/local_range.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{EventArgument, Message};
use std::marker::PhantomData;

/// A RAII-like object for modeling callstack Ranges
/// A RAII-like object for modeling callstack (thread-local) Ranges
#[derive(Debug)]
pub struct LocalRange {
// prevent Sync + Send
Expand Down
2 changes: 1 addition & 1 deletion src/range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{EventArgument, Message};

/// A RAII-like object for modeling start/end Ranges
/// A RAII-like object for modeling process-wide Ranges
#[derive(Debug)]
pub struct Range {
id: nvtx_sys::RangeId,
Expand Down
Loading

0 comments on commit 687c0d0

Please sign in to comment.