Skip to main content

windjammer_runtime/profiling/
tracy.rs

1//! CPU zones for [Tracy](https://github.com/wolfpld/tracy).
2//!
3//! Windjammer maps `@profile("Name")` on functions to:
4//! `let _guard = windjammer_runtime::profiling::tracy_zone("Name");` at the start of the body.
5
6#[cfg(feature = "tracy")]
7use tracy_client::Client;
8
9/// Opaque guard that ends the Tracy zone when dropped.
10///
11/// When the `tracy` feature is off, this is a zero-sized no-op.
12pub struct TracyZoneGuard {
13    #[cfg(feature = "tracy")]
14    _span: tracy_client::Span,
15    #[cfg(not(feature = "tracy"))]
16    _no_copy: std::marker::PhantomData<()>,
17}
18
19/// Begin a named Tracy zone; drops at end of scope.
20///
21/// With `--features tracy` on `windjammer-runtime`, this forwards to
22/// [`tracy_client::Client::span_alloc`]. Without the feature, this compiles to an empty body and
23/// returns a zero-sized guard (no linked Tracy code).
24#[inline]
25pub fn tracy_zone(_name: &'static str) -> TracyZoneGuard {
26    #[cfg(feature = "tracy")]
27    {
28        let client = Client::start();
29        let span = client.span_alloc(Some(_name), "windjammer", file!(), line!(), 0);
30        TracyZoneGuard { _span: span }
31    }
32    #[cfg(not(feature = "tracy"))]
33    {
34        TracyZoneGuard {
35            _no_copy: std::marker::PhantomData,
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn tracy_zone_no_panic_without_feature() {
46        {
47            let _z = tracy_zone("unit_test_zone");
48        }
49    }
50}