1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
use crate::{adjust_stack_depth, Client};
use std::ffi::CString;
/// A handle representing a span of execution.
///
/// The trace span will be ended when this type is dropped.
pub struct Span {
#[cfg(feature = "enable")]
client: Client,
#[cfg(feature = "enable")]
zone: sys::___tracy_c_zone_context,
#[cfg(feature = "enable")]
_no_send_sync: std::marker::PhantomData<*mut sys::___tracy_c_zone_context>,
#[cfg(not(feature = "enable"))]
_no_send_sync: std::marker::PhantomData<*mut ()>,
}
/// A statically allocated location information for a span.
///
/// Construct with the [`span_location!`](crate::span_location) macro.
pub struct SpanLocation {
#[cfg(feature = "enable")]
pub(crate) _function_name: CString,
#[cfg(feature = "enable")]
pub(crate) data: sys::___tracy_source_location_data,
#[cfg(not(feature = "enable"))]
pub(crate) _internal: (),
}
unsafe impl Send for SpanLocation {}
unsafe impl Sync for SpanLocation {}
/// Instrumentation for timed regions, spans or zones of execution.
impl Client {
/// Start a new Tracy span/zone.
///
/// In order to obtain a [`SpanLocation`] value to provide to this function use the
/// [`span_location!`](crate::span_location) macro.
///
/// Specifying a non-zero `callstack_depth` will enable collection of callstack for this
/// message. The number provided will limit the number of call frames collected. Note that
/// enabling callstack collection introduces a non-trivial amount of overhead to this call. On
/// some systems this value may be clamped to a maximum value supported by the target.
///
/// The [`span!`](crate::span!) macro is a convenience wrapper over this method.
///
/// # Example
///
/// In the following example the span is created with the location at which the
/// `span_location!` macro appears and will measure the execution of the 100ms long sleep.
///
/// ```rust
/// use tracy_client::{Client, span_location};
/// let client = Client::start();
/// {
/// let _span = client.span(span_location!("sleeping"), 100);
/// std::thread::sleep(std::time::Duration::from_millis(100));
/// } // _span ends
/// ```
#[inline]
#[must_use]
pub fn span(self, loc: &'static SpanLocation, callstack_depth: u16) -> Span {
#[cfg(feature = "enable")]
unsafe {
let zone = if callstack_depth == 0 {
sys::___tracy_emit_zone_begin(&loc.data, 1)
} else {
let stack_depth = adjust_stack_depth(callstack_depth).into();
sys::___tracy_emit_zone_begin_callstack(&loc.data, stack_depth, 1)
};
Span {
client: self,
zone,
_no_send_sync: std::marker::PhantomData,
}
}
#[cfg(not(feature = "enable"))]
Span {
_no_send_sync: std::marker::PhantomData,
}
}
/// Start a new Tracy span/zone.
///
/// This function allocates the span information on the heap until it is read out by the
/// profiler. Prefer the [`Client::span`] as a allocation-free and faster alternative when
/// possible.
///
/// Specifying a non-zero `callstack_depth` will enable collection of callstack for this
/// message. The number provided will limit the number of call frames collected. Note that
/// enabling callstack collection introduces a non-trivial amount of overhead to this call. On
/// some systems this value may be clamped to a maximum value supported by the target.
///
/// # Example
///
/// In the following example the span is created with custom span source data and will measure
/// the execution of the 100ms long sleep.
///
/// ```rust
/// use tracy_client::Client;
/// let client = Client::start();
/// {
/// let _span = client.span_alloc(Some("hello"), "my_function", "hello.rs", 42, 100);
/// std::thread::sleep(std::time::Duration::from_millis(100));
/// } // _span ends
/// ```
#[inline]
#[must_use]
pub fn span_alloc(
self,
name: Option<&str>,
function: &str,
file: &str,
line: u32,
callstack_depth: u16,
) -> Span {
#[cfg(feature = "enable")]
unsafe {
let loc = sys::___tracy_alloc_srcloc_name(
line,
file.as_ptr().cast(),
file.len(),
function.as_ptr().cast(),
function.len(),
name.map_or(std::ptr::null(), |n| n.as_ptr().cast()),
name.unwrap_or("").len(),
0
);
let zone = if callstack_depth == 0 {
sys::___tracy_emit_zone_begin_alloc(loc, 1)
} else {
let stack_depth = adjust_stack_depth(callstack_depth).into();
sys::___tracy_emit_zone_begin_alloc_callstack(loc, stack_depth, 1)
};
Span {
client: self,
zone,
_no_send_sync: std::marker::PhantomData,
}
}
#[cfg(not(feature = "enable"))]
Span {
_no_send_sync: std::marker::PhantomData,
}
}
}
impl Span {
/// Emit a numeric value associated with this span.
pub fn emit_value(&self, value: u64) {
#[cfg(feature = "enable")]
unsafe {
// SAFE: the only way to construct `Span` is by creating a valid tracy zone context.
let () = sys::___tracy_emit_zone_value(self.zone, value);
}
}
/// Emit some text associated with this span.
pub fn emit_text(&self, text: &str) {
#[cfg(feature = "enable")]
unsafe {
// SAFE: the only way to construct `Span` is by creating a valid tracy zone context.
let () = sys::___tracy_emit_zone_text(self.zone, text.as_ptr().cast(), text.len());
}
}
/// Emit a color associated with this span.
pub fn emit_color(&self, color: u32) {
#[cfg(feature = "enable")]
unsafe {
// SAFE: the only way to construct `Span` is by creating a valid tracy zone context.
// TODO: verify if we need to shift by 8 or not...?
let () = sys::___tracy_emit_zone_color(self.zone, color);
}
}
}
impl Drop for Span {
fn drop(&mut self) {
#[cfg(feature = "enable")]
unsafe {
// SAFE: The only way to construct `Span` is by creating a valid tracy zone context. We
// also still have an owned Client handle.
let () = sys::___tracy_emit_zone_end(self.zone);
std::convert::identity(&self.client);
}
}
}
/// Construct a <code>&'static [SpanLocation]</code>.
///
/// The returned `SpanLocation` is allocated statically and is cached between invocations. This
/// `SpanLocation` will refer to the file and line at which this macro has been invoked, as well as
/// to the item containing this macro invocation.
///
/// The resulting value may be used as an argument for the [`Client::span`] method.
///
/// # Example
///
/// ```rust
/// let location: &'static tracy_client::SpanLocation = tracy_client::span_location!("some name");
/// ```
#[macro_export]
macro_rules! span_location {
() => {{
struct S;
// String processing in `const` when, Oli?
static LOC: $crate::internal::Lazy<$crate::internal::SpanLocation> =
$crate::internal::Lazy::new(|| {
$crate::internal::make_span_location(
$crate::internal::type_name::<S>(),
$crate::internal::null(),
concat!(file!(), "\0").as_ptr(),
line!(),
)
});
&*LOC
}};
($name: expr) => {{
struct S;
// String processing in `const` when, Oli?
static LOC: $crate::internal::Lazy<$crate::internal::SpanLocation> =
$crate::internal::Lazy::new(|| {
$crate::internal::make_span_location(
$crate::internal::type_name::<S>(),
concat!($name, "\0").as_ptr(),
concat!(file!(), "\0").as_ptr(),
line!(),
)
});
&*LOC
}};
}
/// Start a new Tracy span with function, file, and line determined automatically.
///
/// # Panics
///
/// `span!` will panic if the Client isn't running at the time this macro is invoked.
///
/// # Examples
///
/// Begin a span region, which will be terminated once `_span` goes out of scope:
///
/// ```
/// use tracy_client::{Client, span};
/// # let _client = tracy_client::Client::start();
/// let _span = span!("some span");
/// ```
///
/// It is also possible to enable collection of the callstack by specifying a limit of call stack
/// frames to record:
///
/// ```
/// use tracy_client::span;
/// # let _client = tracy_client::Client::start();
/// let _span = span!("some span", 32);
/// ```
///
/// Note, however, that collecting callstack introduces a non-trivial overhead at the point of
/// instrumentation.
#[macro_export]
macro_rules! span {
() => {
$crate::Client::running()
.expect("span! without a running Client")
.span($crate::span_location!(), 0)
};
($name: expr) => {
$crate::span!($name, 0)
};
($name: expr, $callstack_depth: expr) => {{
let location = $crate::span_location!($name);
$crate::Client::running()
.expect("span! without a running Client")
.span(location, $callstack_depth)
}};
}