netstack3_trace/
lib.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Tracing abstractions for Netstack3.
6//!
7//! This crate provides a tracing frontend that can be compiled with and without
8//! fuchsia dependencies for netstack3-core. It encodes the common patterns and
9//! trace categories used by netstack3.
10
11#![no_std]
12#![warn(missing_docs, unreachable_patterns, clippy::useless_conversion, clippy::redundant_clone)]
13
14mod id;
15
16/// The trace category used by netstack3.
17pub const CATEGORY: &'static core::ffi::CStr = c"net";
18
19/// Internal implementation.
20#[cfg(target_os = "fuchsia")]
21pub mod __inner {
22    use super::CATEGORY;
23
24    pub use fuchsia_trace::{duration, instant, ArgValue, Scope};
25    use fuchsia_trace::{trace_site_t, TraceCategoryContext};
26
27    /// A single trace site cache that is used in the macro expansions.
28    ///
29    /// Given this crate always forces the same category [`crate::CATEGORY`] for
30    /// trace points, we can have a single entry instead of one per call-site.
31    static CACHE: trace_site_t = trace_site_t::new(0);
32
33    /// Checks for [`CACHE`] to see if the trace category is enabled.
34    #[inline]
35    pub fn category_context() -> Option<TraceCategoryContext> {
36        TraceCategoryContext::acquire_cached(CATEGORY, &CACHE)
37    }
38
39    /// Equivalent of [`fuchsia_trace::duration!`].
40    ///
41    /// Always uses [`crate::CATEGORY`].
42    #[macro_export]
43    macro_rules! trace_duration {
44        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
45            let mut args;
46            let _scope = {
47                if $crate::__inner::category_context().is_some() {
48                    args = [$($crate::__inner::ArgValue::of($key, $val)),*];
49                    Some($crate::__inner::duration($crate::CATEGORY, $name, &args))
50                } else {
51                    None
52                }
53            };
54        }
55    }
56
57    /// Equivalent of [`fuchsia_trace::instant!`].
58    ///
59    /// Always uses [`crate::CATEGORY`].
60    #[macro_export]
61    macro_rules! trace_instant {
62        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
63            if let Some(context) = $crate::__inner::category_context() {
64                let args = [$($crate::__inner::ArgValue::of($key, $val)),*];
65                $crate::__inner::instant(
66                    &context,
67                    $name,
68                    $crate::__inner::Scope::Thread,
69                    &args
70                );
71            }
72        }
73    }
74}
75
76/// Internal implementation.
77#[cfg(not(target_os = "fuchsia"))]
78#[allow(missing_docs)]
79pub mod __inner {
80    #[macro_export]
81    macro_rules! trace_duration {
82        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
83            $(
84                let _ = $key;
85                let _ = $val;
86            )*
87        }
88    }
89
90    #[macro_export]
91    macro_rules! trace_instant {
92        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
93            $(
94                let _ = $key;
95                let _ = $val;
96            )*
97        }
98    }
99}
100
101pub use id::TraceResourceId;