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>
impl<AtomicUsize: AtomicUsize, const NUM_ENTRIES: usize> Buffer<AtomicUsize, NUM_ENTRIES>
Sourcepub const fn buffer_len(&self) -> usize
pub const fn buffer_len(&self) -> usize
Returns the total size of the buffer in bytes.
Sourcepub const unsafe fn buffer(&self) -> *const ()
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.
Sourcepub fn add_record(
&self,
time_stamp: u32,
event_type: EventType,
event_data: [u32; 3],
)
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:
impl<AtomicUsize: AtomicUsize, const NUM_ENTRIES: usize> Sync for Buffer<AtomicUsize, NUM_ENTRIES>
Safety: The buffer is safe to share across threads because:
posis anAtomicUsizewhich handles concurrent updates safely.recordsis accessed viaUnsafeCell, but the protocol for writing ensures that each slot is effectively owned by a single writer at a time (barring wraparound collisions).