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
  6. Buffer

Buffer

Struct Buffer 

Source
pub struct Buffer<AtomicUsize: AtomicUsize, const NUM_ENTRIES: usize> { /* private fields */ }
Expand description

A circular buffer for storing trace records.

The buffer uses atomic operations to manage a write position, allowing multiple producers to write concurrently without locking.

§Type Parameters

  • AtomicUsize: The atomic type used for the position counter (e.g., pw_atomic::AtomicUsize).
  • NUM_ENTRIES: The number of entries in the buffer.

Implementations§

Source§

impl<AtomicUsize: AtomicUsize, const NUM_ENTRIES: usize> Buffer<AtomicUsize, NUM_ENTRIES>

Source

pub const fn new() -> Self

Creates a new, empty buffer.

Source

pub const fn buffer_len(&self) -> usize

Returns the total size of the buffer in bytes.

Source

pub const unsafe fn buffer(&self) -> *const ()

Returns a raw pointer to the underlying buffer storage.

§Safety

This provides direct access to the raw buffer storage. The caller must ensure that they do not violate any safety guarantees, especially if reading while other threads are writing.

Source

pub fn add_record( &self, time_stamp: u32, event_type: EventType, event_data: [u32; 3], )

Adds a record to the buffer.

This method reserves a slot in the circular buffer using atomic operations and writes the record data.

§Arguments
  • time_stamp: The timestamp of the event.
  • event_type: The type of event.
  • event_data: The raw payload of the event.
§Example
use pw_kernel_tracing::{Buffer, EventType, ContextSwitchEvent};
use core::sync::atomic::AtomicUsize;

let buffer = Buffer::<AtomicUsize, 4>::new();
let event = ContextSwitchEvent {
    old_thread_id: 1,
    new_thread_id: 2,
    old_thread_state: 3,
};
buffer.add_record(0x1234, EventType::ContextSwitch, event.encode());

Trait Implementations§

Source§

impl<AtomicUsize: AtomicUsize, const NUM_ENTRIES: usize> Sync for Buffer<AtomicUsize, NUM_ENTRIES>

Safety: The buffer is safe to share across threads because:

  1. pos is an AtomicUsize which handles concurrent updates safely.
  2. records is accessed via UnsafeCell, but the protocol for writing ensures that each slot is effectively owned by a single writer at a time (barring wraparound collisions).

Auto Trait Implementations§

§

impl<AtomicUsize, const NUM_ENTRIES: usize> !Freeze for Buffer<AtomicUsize, NUM_ENTRIES>

§

impl<AtomicUsize, const NUM_ENTRIES: usize> !RefUnwindSafe for Buffer<AtomicUsize, NUM_ENTRIES>

§

impl<AtomicUsize, const NUM_ENTRIES: usize> Forget for Buffer<AtomicUsize, NUM_ENTRIES>

§

impl<AtomicUsize, const NUM_ENTRIES: usize> Move for Buffer<AtomicUsize, NUM_ENTRIES>

§

impl<AtomicUsize, const NUM_ENTRIES: usize> Send for Buffer<AtomicUsize, NUM_ENTRIES>
where UnsafeCell<[RecordEntry; NUM_ENTRIES]>: Send,

§

impl<AtomicUsize, const NUM_ENTRIES: usize> Unpin for Buffer<AtomicUsize, NUM_ENTRIES>
where UnsafeCell<[RecordEntry; NUM_ENTRIES]>: Unpin, AtomicUsize: Unpin,

§

impl<AtomicUsize, const NUM_ENTRIES: usize> UnsafeUnpin for Buffer<AtomicUsize, NUM_ENTRIES>
where UnsafeCell<[RecordEntry; NUM_ENTRIES]>: UnsafeUnpin, AtomicUsize: UnsafeUnpin,

§

impl<AtomicUsize, const NUM_ENTRIES: usize> UnwindSafe for Buffer<AtomicUsize, NUM_ENTRIES>
where UnsafeCell<[RecordEntry; NUM_ENTRIES]>: UnwindSafe, AtomicUsize: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = !

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.