pub struct CountersFile { /* private fields */ }Expand description
View over a counters file region. Owns no memory; constructed from a pointer into a shared-memory mapping or a stack buffer (test only).
Implementations§
Source§impl CountersFile
impl CountersFile
Sourcepub unsafe fn init(ptr: NonNull<u8>) -> Self
pub unsafe fn init(ptr: NonNull<u8>) -> Self
Initialise a freshly-zeroed reserved region as a counters file. Must be called exactly once per segment, by whichever process creates the segment.
§Safety
ptr must point to at least COUNTERS_FILE_RESERVED_BYTES of
writable memory that outlives the returned view, and that memory
must be zero-initialised before this call.
Sourcepub unsafe fn attach(ptr: NonNull<u8>) -> Result<Self, AttachError>
pub unsafe fn attach(ptr: NonNull<u8>) -> Result<Self, AttachError>
Attach to an existing counters file region. Validates the magic and version.
§Safety
ptr must point to at least COUNTERS_FILE_RESERVED_BYTES of
readable memory whose lifetime contains the returned view. The
memory must already have been initialised by a writer.
Sourcepub fn header(&self) -> &CountersHeader
pub fn header(&self) -> &CountersHeader
Header reference.
Sourcepub fn register(
&self,
id: u32,
flags: u32,
label: &str,
) -> Option<CounterHandle>
pub fn register( &self, id: u32, flags: u32, label: &str, ) -> Option<CounterHandle>
Reserve a new slot, populate id, flags, and label, and
return a writer handle. Returns None when the slot capacity is
exhausted.
Sourcepub fn boxed() -> OwnedCountersFile
pub fn boxed() -> OwnedCountersFile
Allocate a fresh, zero-initialised counters file on the heap and
initialise it. Returns an OwnedCountersFile that owns the
backing buffer alongside the CountersFile view.
Safe alternative to CountersFile::init for single-process use
cases (tests, in-process metrics) where the counters file does
not need to be shared across processes via SHM or mmap.
The returned OwnedCountersFile derefs to &CountersFile, so
all register / snapshot / etc. operations work directly:
use disruptor_mp::observability::CountersFile;
let file = CountersFile::boxed();
let handle = file.register(1, 0, "events_published").unwrap();
handle.inc();
assert_eq!(file.snapshot()[0].value, 1);Sourcepub fn snapshot(&self) -> Vec<CounterSnapshot>
pub fn snapshot(&self) -> Vec<CounterSnapshot>
Snapshot all in-use slots — (id, flags, value, label) tuples.
Intended for external readers and tests.