starnix_logging/
trace.rs

1// Copyright 2023 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
5pub use fuchsia_trace::Scope as TraceScope;
6
7// This needs to be available to the macros in this module without clients having to depend on
8// fuchsia_trace themselves.
9#[doc(hidden)]
10pub use fuchsia_trace as __fuchsia_trace;
11
12// The trace category used for starnix-related traces.
13pub const CATEGORY_STARNIX: &'static str = "starnix";
14
15// The trace category used for memory manager related traces.
16pub const CATEGORY_STARNIX_MM: &'static str = "starnix:mm";
17
18// The trace category used for security related traces.
19pub const CATEGORY_STARNIX_SECURITY: &'static str = "starnix:security";
20
21// The name used to track the duration in Starnix while executing a task.
22pub const NAME_RUN_TASK: &'static str = "RunTask";
23
24// The trace category used for atrace events generated within starnix.
25pub const CATEGORY_ATRACE: &'static str = "starnix:atrace";
26
27// The trace category used for trace events about emitting trace events.
28pub const CATEGORY_TRACE_META: &'static str = "trace_meta";
29
30// The name used to identify blob records from the container's Perfetto daemon.
31pub const NAME_PERFETTO_BLOB: &'static str = "starnix_perfetto";
32
33// The name used to track the duration of creating a container.
34pub const NAME_CREATE_CONTAINER: &'static str = "CreateContainer";
35
36// The name used to track the start time of the starnix kernel.
37pub const NAME_START_KERNEL: &'static str = "StartKernel";
38
39// The name used to track when a thread was kicked.
40pub const NAME_RESTRICTED_KICK: &'static str = "RestrictedKick";
41
42// The name used to track the duration for inline exception handling.
43pub const NAME_HANDLE_EXCEPTION: &'static str = "HandleException";
44
45// The names used to track durations for restricted state I/O.
46pub const NAME_READ_RESTRICTED_STATE: &'static str = "ReadRestrictedState";
47pub const NAME_WRITE_RESTRICTED_STATE: &'static str = "WriteRestrictedState";
48pub const NAME_MAP_RESTRICTED_STATE: &'static str = "MapRestrictedState";
49
50// The name used to track the duration of checking whether the task loop should exit.
51pub const NAME_CHECK_TASK_EXIT: &'static str = "CheckTaskExit";
52
53pub const ARG_NAME: &'static str = "name";
54
55#[inline]
56pub fn regular_trace_category_enabled(category: &'static str) -> bool {
57    fuchsia_trace::category_enabled(category)
58}
59
60#[macro_export]
61macro_rules! trace_instant {
62    ($category:expr, $name:expr, $scope:expr $(, $key:expr => $val:expr)*) => {
63        $crate::__fuchsia_trace::instant!($category, $name, $scope $(, $key => $val)*);
64    };
65}
66
67#[macro_export]
68macro_rules! firehose_trace_instant {
69    ($category:expr, $name:expr, $scope:expr $(, $key:expr => $val:expr)*) => {
70        $crate::trace_instant!($category, $name, $scope $(, $key => $val)*);
71    }
72}
73
74// The `trace_duration` macro defines a `_scope` instead of executing a statement because the
75// lifetime of the `_scope` variable corresponds to the duration.
76#[macro_export]
77macro_rules! trace_duration {
78    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
79        let args;
80        let _scope = {
81            static CACHE: $crate::__fuchsia_trace::trace_site_t = $crate::__fuchsia_trace::trace_site_t::new(0);
82            if let Some(_context) =
83                    $crate::__fuchsia_trace::TraceCategoryContext::acquire_cached($category, &CACHE) {
84                args = [$($crate::__fuchsia_trace::ArgValue::of($key, $val)),*];
85                Some($crate::__fuchsia_trace::duration($category, $name, &args))
86            } else {
87                None
88            }
89        };
90    }
91}
92
93#[macro_export]
94macro_rules! firehose_trace_duration {
95    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
96        $crate::trace_duration!($category, $name $(, $key => $val)*);
97    }
98}
99
100#[macro_export]
101macro_rules! trace_duration_begin {
102    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
103        $crate::__fuchsia_trace::duration_begin!($category, $name $(, $key => $val)*);
104    };
105}
106
107#[macro_export]
108macro_rules! firehose_trace_duration_begin {
109    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
110        $crate::trace_duration_begin!($category, $name $(, $key => $val)*);
111    }
112}
113
114#[macro_export]
115macro_rules! trace_duration_end {
116    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
117        $crate::__fuchsia_trace::duration_end!($category, $name $(, $key => $val)*);
118    };
119}
120
121#[macro_export]
122macro_rules! firehose_trace_duration_end {
123    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
124        $crate::trace_duration_end!($category, $name $(, $key => $val)*);
125    }
126}
127
128#[macro_export]
129macro_rules! trace_flow_begin {
130    ($category:expr, $name:expr, $flow_id:expr $(, $key:expr => $val:expr)*) => {
131        let _flow_id: $crate::__fuchsia_trace::Id = $flow_id;
132        $crate::__fuchsia_trace::flow_begin!($category, $name, _flow_id $(, $key => $val)*);
133    };
134}
135
136#[macro_export]
137macro_rules! trace_flow_step {
138    ($category:expr, $name:expr, $flow_id:expr $(, $key:expr => $val:expr)*) => {
139        let _flow_id: $crate::__fuchsia_trace::Id = $flow_id;
140        $crate::__fuchsia_trace::flow_step!($category, $name, _flow_id $(, $key => $val)*);
141    };
142}
143
144#[macro_export]
145macro_rules! trace_flow_end {
146    ($category:expr, $name:expr, $flow_id:expr $(, $key:expr => $val:expr)*) => {
147        let _flow_id: $crate::__fuchsia_trace::Id = $flow_id;
148        $crate::__fuchsia_trace::flow_end!($category, $name, _flow_id $(, $key => $val)*);
149    };
150}
151
152#[macro_export]
153macro_rules! trace_instaflow_begin {
154    (
155        $category:expr,
156        $flow_name:expr,
157        $step_name:expr,
158        $flow_id:expr
159        $(, $key:expr => $val:expr)*
160    ) => {
161        {
162            let _flow_id: $crate::__fuchsia_trace::Id = $flow_id;
163            $crate::__fuchsia_trace::instaflow_begin!(
164                $category,
165                $flow_name,
166                $step_name,
167                _flow_id
168                $(, $key => $val)*
169            );
170        }
171    };
172}
173
174#[macro_export]
175macro_rules! trace_instaflow_end {
176    (
177        $category:expr,
178        $flow_name:expr,
179        $step_name:expr,
180        $flow_id:expr
181        $(, $key:expr => $val:expr)*
182    ) => {
183        {
184            let _flow_id: $crate::__fuchsia_trace::Id = $flow_id;
185            $crate::__fuchsia_trace::instaflow_end!(
186                $category,
187                $flow_name,
188                $step_name,
189                _flow_id
190                $(, $key => $val)*
191            );
192        }
193    };
194}
195
196#[macro_export]
197macro_rules! trace_instaflow_step {
198    (
199        $category:expr,
200        $flow_name:expr,
201        $step_name:expr,
202        $flow_id:expr
203        $(, $key:expr => $val:expr)*
204    ) => {
205        {
206            let _flow_id: $crate::__fuchsia_trace::Id = $flow_id;
207            $crate::__fuchsia_trace::instaflow_step!(
208                $category,
209                $flow_name,
210                $step_name,
211                _flow_id
212                $(, $key => $val)*
213            );
214        }
215    };
216}