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. pw_kernel_tracing/lib.rs

pw_kernel_tracing/
lib.rs

1// Copyright 2025 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
15//! # pw_kernel_tracing
16//!
17//! `pw_kernel_tracing` is a `no_std` library that provides the core types and traits
18//! for tracing within `pw_kernel`. It is designed to be efficient for embedded usage
19//! and supports lock-free buffering of trace records.  Care is taken to ensure that
20//! generating trace records and writing them to the buffer result in simple u32
21//! copies.
22//!
23//! ## Overview
24//!
25//! This crate provides:
26//! - **`Record`**: A fixed-size trace entry containing metadata (timestamp, event type) and payload.
27//! - **`Buffer`**: A circular buffer implementation for storing trace records using atomic indices.
28//!
29//! ## Concurrency Model
30//!
31//! The [`Buffer`] uses atomic operations to reserve slots for writing records, allowing
32//! multiple producers (e.g., interrupt handlers, threads) to write to the trace buffer
33//! concurrently without lock contention.
34//!
35//! ## Example
36//!
37//! ```
38//! use pw_kernel_tracing::{Buffer, EventType, Record, ContextSwitchEvent};
39//! use core::sync::atomic::AtomicUsize;
40//!
41//! // Create a buffer with 4 entries.
42//! // In a real kernel, you would likely use a larger size and a static instance.
43//! let buffer = Buffer::<AtomicUsize, 4>::new();
44//!
45//! // Add a Context Switch record.
46//! // Arguments: timestamp, event_type, event_data words.
47//! let event = ContextSwitchEvent {
48//!     old_thread_id: 1,
49//!     new_thread_id: 2,
50//!     old_thread_state: 3,
51//! };
52//!
53//! buffer.add_record(
54//!     0x1234,                     // Timestamp
55//!     EventType::ContextSwitch,   // Event Type
56//!     event.encode(),             // Event Payload
57//! );
58//! ```
59
60#![cfg_attr(not(feature = "std"), no_std)]
61
62mod buffer;
63mod record;
64
65pub use buffer::*;
66pub use record::*;