Skip to main content Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. Rust
  4. pw_kernel
  5. pw_kernel_tracing

Crate pw_kernel_tracing

Crate pw_kernel_tracing 

Source
Expand description

§pw_kernel_tracing

pw_kernel_tracing is a no_std library that provides the core types and traits for tracing within pw_kernel. It is designed to be efficient for embedded usage and supports lock-free buffering of trace records. Care is taken to ensure that generating trace records and writing them to the buffer result in simple u32 copies.

§Overview

This crate provides:

  • Record: A fixed-size trace entry containing metadata (timestamp, event type) and payload.
  • Buffer: A circular buffer implementation for storing trace records using atomic indices.

§Concurrency Model

The Buffer uses atomic operations to reserve slots for writing records, allowing multiple producers (e.g., interrupt handlers, threads) to write to the trace buffer concurrently without lock contention.

§Example

use pw_kernel_tracing::{Buffer, EventType, Record, ContextSwitchEvent};
use core::sync::atomic::AtomicUsize;

// Create a buffer with 4 entries.
// In a real kernel, you would likely use a larger size and a static instance.
let buffer = Buffer::<AtomicUsize, 4>::new();

// Add a Context Switch record.
// Arguments: timestamp, event_type, event_data words.
let event = ContextSwitchEvent {
    old_thread_id: 1,
    new_thread_id: 2,
    old_thread_state: 3,
};

buffer.add_record(
    0x1234,                     // Timestamp
    EventType::ContextSwitch,   // Event Type
    event.encode(),             // Event Payload
);

Structs§

Buffer
A circular buffer for storing trace records.
ContextSwitchEvent
Context switch event data
Record
A trace record.
TraceSpanEvent
Trace span event data

Enums§

EventPayload
Strongly-typed payload for defined event types.
EventType
Supported trace event types.