Skip to content

Commit

Permalink
Non-allocating span! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Apr 3, 2022
1 parent 15936ea commit f183050
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 11 deletions.
3 changes: 3 additions & 0 deletions tracy-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ description = """
High level bindings to the client libraries for the Tracy profiler
"""

[dependencies]
once_cell = "1.10"

[dependencies.tracy-client-sys]
path = "../tracy-client-sys"
version = ">=0.14.0, <0.17.0" # AUTO-UPDATE
Expand Down
96 changes: 85 additions & 11 deletions tracy-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,66 @@ use tracy_client_sys as sys;
#[macro_export]
macro_rules! span {
($name:expr) => {
$crate::span!($name, 62)
$crate::span_impl!($name, 62)
};
($name:expr, $callstack_depth:expr) => {
$crate::span_impl!($name, $callstack_depth)
};
}

#[macro_export]
#[doc(hidden)]
#[cfg(feature = "enable")]
macro_rules! span_impl {
($name:expr, $callstack_depth:expr) => {{
use std::ffi::CString;
use $crate::internal::once_cell::sync::Lazy;

struct S;
let func_name = std::any::type_name::<S>();
$crate::Span::new(
$name,
&func_name[..func_name.len() - 3],
file!(),
line!(),
$callstack_depth,
)
static SRC_LOC: Lazy<$crate::internal::SrcLoc> = Lazy::new(|| {
let function_name = std::any::type_name::<S>();
let function_name = CString::new(&function_name[..function_name.len() - 3]).unwrap();
$crate::internal::SrcLoc {
data: $crate::internal::sys::___tracy_source_location_data {
name: concat!($name, "\0").as_ptr().cast(),
function: function_name.as_ptr(),
file: concat!(file!(), "\0").as_ptr().cast(),
line: line!(),
color: 0,
},
_function_name: function_name,
}
});
unsafe { $crate::Span::from_src_loc(&SRC_LOC.data, $callstack_depth) }
}};
}

#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "enable"))]
macro_rules! span_impl {
($name:expr, $callstack_depth:expr) => {
$crate::Span::disabled()
};
}

#[doc(hidden)]
#[cfg(feature = "enable")]
pub mod internal {
use std::ffi::CString;

pub use once_cell;
pub use tracy_client_sys as sys;

pub struct SrcLoc {
pub _function_name: CString,
pub data: sys::___tracy_source_location_data,
}

unsafe impl Send for SrcLoc {}
unsafe impl Sync for SrcLoc {}
}

/// A handle representing a span of execution.
#[cfg(feature="enable")]
pub struct Span(
Expand All @@ -56,8 +101,6 @@ pub struct Span(
#[cfg(not(feature="enable"))]
pub struct Span(());



impl Span {
/// Start a new Tracy span.
///
Expand Down Expand Up @@ -100,6 +143,37 @@ impl Span {
}
}

#[inline]
#[doc(hidden)]
#[cfg(not(feature = "enable"))]
pub fn disabled() -> Self {
Self(())
}

#[inline]
#[doc(hidden)]
#[cfg(feature = "enable")]
pub unsafe fn from_src_loc(
loc: &'static sys::___tracy_source_location_data,
callstack_depth: u16,
) -> Self {
if callstack_depth == 0 {
Self(
sys::___tracy_emit_zone_begin(loc, 1),
std::marker::PhantomData,
)
} else {
Self(
sys::___tracy_emit_zone_begin_callstack(
loc,
adjust_stack_depth(callstack_depth).into(),
1,
),
std::marker::PhantomData,
)
}
}

/// Emit a numeric value associated with this span.
pub fn emit_value(&self, value: u64) {
// SAFE: the only way to construct `Span` is by creating a valid tracy zone context.
Expand Down

0 comments on commit f183050

Please sign in to comment.