Skip to main content

ThreadSampler

Struct ThreadSampler 

Source
pub struct ThreadSampler<'sampler, const N: usize> { /* private fields */ }
Available on macOS only.
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>

Source

pub const fn is_running(&self) -> bool

Returns true if counting is currently enabled.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<const N: usize> Debug for ThreadSampler<'_, N>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Drop for ThreadSampler<'_, N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'sampler, const N: usize> Freeze for ThreadSampler<'sampler, N>

§

impl<'sampler, const N: usize> RefUnwindSafe for ThreadSampler<'sampler, N>

§

impl<'sampler, const N: usize> !Send for ThreadSampler<'sampler, N>

§

impl<'sampler, const N: usize> !Sync for ThreadSampler<'sampler, N>

§

impl<'sampler, const N: usize> Unpin for ThreadSampler<'sampler, N>

§

impl<'sampler, const N: usize> UnsafeUnpin for ThreadSampler<'sampler, N>

§

impl<'sampler, const N: usize> UnwindSafe for ThreadSampler<'sampler, N>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.