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(
13    missing_docs,
14    unreachable_patterns,
15    clippy::useless_conversion,
16    clippy::redundant_clone,
17    clippy::precedence
18)]
19
20mod id;
21
22/// The trace category used by netstack3.
23pub const CATEGORY: &'static core::ffi::CStr = c"net";
24
25/// Internal implementation.
26#[cfg(target_os = "fuchsia")]
27pub mod __inner {
28    use super::CATEGORY;
29
30    pub use fuchsia_trace::{duration, instant, ArgValue, Scope};
31    use fuchsia_trace::{trace_site_t, TraceCategoryContext};
32
33    /// A single trace site cache that is used in the macro expansions.
34    ///
35    /// Given this crate always forces the same category [`crate::CATEGORY`] for
36    /// trace points, we can have a single entry instead of one per call-site.
37    static CACHE: trace_site_t = trace_site_t::new(0);
38
39    /// Checks for [`CACHE`] to see if the trace category is enabled.
40    #[inline]
41    pub fn category_context() -> Option<TraceCategoryContext> {
42        TraceCategoryContext::acquire_cached(CATEGORY, &CACHE)
43    }
44
45    /// Equivalent of [`fuchsia_trace::duration!`].
46    ///
47    /// Always uses [`crate::CATEGORY`].
48    #[macro_export]
49    macro_rules! trace_duration {
50        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
51            let mut args;
52            let _scope = {
53                if $crate::__inner::category_context().is_some() {
54                    args = [$($crate::__inner::ArgValue::of($key, $val)),*];
55                    Some($crate::__inner::duration($crate::CATEGORY, $name, &args))
56                } else {
57                    None
58                }
59            };
60        }
61    }
62
63    /// Equivalent of [`fuchsia_trace::instant!`].
64    ///
65    /// Always uses [`crate::CATEGORY`].
66    #[macro_export]
67    macro_rules! trace_instant {
68        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
69            if let Some(context) = $crate::__inner::category_context() {
70                let args = [$($crate::__inner::ArgValue::of($key, $val)),*];
71                $crate::__inner::instant(
72                    &context,
73                    $name,
74                    $crate::__inner::Scope::Thread,
75                    &args
76                );
77            }
78        }
79    }
80}
81
82/// Internal implementation.
83#[cfg(not(target_os = "fuchsia"))]
84#[allow(missing_docs)]
85pub mod __inner {
86    #[macro_export]
87    macro_rules! trace_duration {
88        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
89            $(
90                let _ = $key;
91                let _ = $val;
92            )*
93        }
94    }
95
96    #[macro_export]
97    macro_rules! trace_instant {
98        ($name:expr $(, $key:expr => $val:expr)* $(,)?) => {
99            $(
100                let _ = $key;
101                let _ = $val;
102            )*
103        }
104    }
105}
106
107pub use id::TraceResourceId;