pub struct ThreadSampler<'sampler, const N: usize> { /* private fields */ }Expand description
Per-thread performance counter reader.
Created via Sampler::thread. You call start to enable
counting, sample to read the current raw counter values,
and stop to disable counting. A ThreadSampler is
reusable across multiple start/stop cycles.
Hardware performance counters are thread-local: each CPU core maintains
separate counter registers, and the kernel tracks per-thread accumulations
as threads migrate between cores. This means a ThreadSampler must be
used on the thread that created it, which is why it is !Send + !Sync.
Implementations§
Source§impl<'sampler, const N: usize> ThreadSampler<'sampler, N>
impl<'sampler, const N: usize> ThreadSampler<'sampler, N>
Sourcepub const fn is_running(&self) -> bool
pub const fn is_running(&self) -> bool
Returns true if counting is currently enabled.
Sourcepub fn start(&mut self) -> Result<(), SamplerError>
pub fn start(&mut self) -> Result<(), SamplerError>
Enables counting for the configured events.
If counting is already enabled, this is a no-op.
§Errors
Returns SamplerError if the kernel rejects the counting request.
Sourcepub fn sample(&self) -> Result<[u64; N], SamplerError>
pub fn sample(&self) -> Result<[u64; N], SamplerError>
Reads the current raw counter values for the configured events.
Each element in the returned array corresponds to the event at the same
index in the events array passed to Sampler::thread. Values are
absolute hardware counter readings; compute deltas between two calls to
get per-region counts.
§Errors
Returns SamplerError if reading thread counters fails.
Sourcepub fn stop(&mut self) -> Result<(), SamplerError>
pub fn stop(&mut self) -> Result<(), SamplerError>
Disables counting.
If counting is already disabled, this is a no-op.
§Errors
Returns SamplerError if the kernel rejects the request. Both calls
are attempted even if the first fails.