Skip to main content Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. Rust
  4. pw_kernel
  5. src
  6. kernel/trace.rs

kernel/
trace.rs

1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15use crate::Kernel;
16use crate::scheduler::State;
17
18#[cfg(feature = "tracing")]
19#[expect(clippy::cast_possible_truncation)]
20fn get_tracepoint_timestamp<K: Kernel>() -> u32 {
21    use pw_time_core::Clock;
22    // TODO: https://pigweed.dev/issues/477681143 - Add target specific
23    // timestamp scaling.
24    (K::Clock::now().ticks() >> 8) as u32
25}
26
27/// Traces a context switch event.
28///
29/// This function records a context switch event to the kernel's trace buffer.
30pub fn trace_context_switch<K: Kernel>(
31    kernel: K,
32    current_thread_id: usize,
33    new_thread_id: usize,
34    current_thread_state: State,
35) {
36    #[cfg(not(feature = "tracing"))]
37    {
38        // Silence unused variable warnings when tracing is not enabled.
39        let _ = (
40            kernel,
41            current_thread_id,
42            new_thread_id,
43            current_thread_state,
44        );
45    }
46
47    #[cfg(feature = "tracing")]
48    #[allow(clippy::cast_possible_truncation)]
49    {
50        use pw_kernel_tracing::{ContextSwitchEvent, EventType};
51        kernel.get_state().trace_buffer.add_record(
52            get_tracepoint_timestamp::<K>(),
53            EventType::ContextSwitch,
54            ContextSwitchEvent {
55                // TODO: https://pigweed.dev/issues/477681354 - Support 64bit
56                // architectures and their 64 bit thread ids.
57                old_thread_id: current_thread_id as u32,
58                new_thread_id: new_thread_id as u32,
59                old_thread_state: current_thread_state as u8,
60            }
61            .encode(),
62        );
63    }
64}
65
66/// Starts a trace span with an optional message.
67///
68/// This function records a span start event to the kernel's trace buffer.
69pub fn trace_span_start<K: Kernel>(kernel: K, message: [u8; 12]) {
70    #[cfg(not(feature = "tracing"))]
71    {
72        // Silence unused variable warnings when tracing is not enabled.
73        let _ = (kernel, message);
74    }
75
76    #[cfg(feature = "tracing")]
77    {
78        use pw_kernel_tracing::{EventType, TraceSpanEvent};
79        kernel.get_state().trace_buffer.add_record(
80            get_tracepoint_timestamp::<K>(),
81            EventType::SpanStart,
82            TraceSpanEvent { message }.encode(),
83        );
84    }
85}
86
87/// Ends a trace span with an optional message.
88///
89/// This function records a span end event to the kernel's trace buffer.
90pub fn trace_span_end<K: Kernel>(kernel: K, message: [u8; 12]) {
91    #[cfg(not(feature = "tracing"))]
92    {
93        // Silence unused variable warnings when tracing is not enabled.
94        let _ = (kernel, message);
95    }
96
97    #[cfg(feature = "tracing")]
98    {
99        use pw_kernel_tracing::{EventType, TraceSpanEvent};
100        kernel.get_state().trace_buffer.add_record(
101            get_tracepoint_timestamp::<K>(),
102            EventType::SpanEnd,
103            TraceSpanEvent { message }.encode(),
104        );
105    }
106}
107
108/// Starts a trace span with an optional format string and format arguments.
109#[macro_export]
110macro_rules! trace_span_start {
111    ($kernel:expr, $($format_string:literal)PW_FMT_CONCAT+ $(, $args:expr)* $(,)?) => {{
112        #[cfg(feature = "tracing")]
113        {
114            let mut message = [0u8; 12];
115            let _ = $crate::__private::pw_tokenizer::tokenize_core_fmt_to_buffer!(
116                &mut message,
117                $($format_string)PW_FMT_CONCAT+,
118                $($args),*
119            );
120            $crate::trace::trace_span_start($kernel, message);
121        }
122        #[cfg(not(feature = "tracing"))]
123        {
124            #[allow(clippy::unnecessary_cast)]
125            let _ = (&$kernel, $($args),*);
126        }
127    }};
128}
129
130/// Ends a trace span with an optional format string and format arguments.
131#[macro_export]
132macro_rules! trace_span_end {
133    ($kernel:expr) => {{
134        #[cfg(feature = "tracing")]
135        {
136            $crate::trace::trace_span_end($kernel, [0u8; 12]);
137        }
138        #[cfg(not(feature = "tracing"))]
139        {
140            let _ = &$kernel;
141        }
142    }};
143}
144
145/// Starts a trace span with an optional format string and format arguments if condition is true.
146#[macro_export]
147macro_rules! trace_span_start_if {
148    ($kernel:expr, $condition:expr, $($format_string:literal)PW_FMT_CONCAT+ $(, $args:expr)* $(,)?) => {{
149        if $condition {
150            $crate::trace_span_start!($kernel, $($format_string)PW_FMT_CONCAT+, $($args),*);
151        } else {
152            #[allow(clippy::unnecessary_cast)]
153            let _ = (&$kernel, $($args),*);
154        }
155    }};
156}
157
158/// Ends a trace span with an optional format string and format arguments if condition is true.
159#[macro_export]
160macro_rules! trace_span_end_if {
161    ($kernel:expr, $condition:expr) => {{
162        if $condition {
163            $crate::trace_span_end!($kernel);
164        } else {
165            let _ = &$kernel;
166        }
167    }};
168}