Skip to main content

ProcessSpan

Struct ProcessSpan 

Source
pub struct ProcessSpan { /* private fields */ }
Expand description

A measurement of process-wide allocations over the span’s lifetime.

Returned by Operation::measure_process. It captures the process’s allocation counters at creation and records the delta when it is dropped, so the measured work should live inside the span’s scope.

Before the span is dropped the caller must state how many iterations the measured work covers by calling iterations. Dropping a span without an iteration count panics, because a measurement with no iteration count is a programming error. If the thread is already unwinding from a panic when the span drops, it records nothing and does not panic again, leaving the original panic to propagate.

Reach for this when the threads doing the work cannot be instrumented themselves, or when several of them collaborate on every iteration so that no single thread’s span covers a whole one. When each worker can be instrumented and counts iterations of its own, give each one its own ThreadSpan naming the same operation instead. The trade-offs a process span accepts are listed under Operation::measure_process.

§Examples

Work that a library fans out over its own threads:

use std::hint::black_box;
use std::thread;

use alloc_tracker::{Allocator, Session};

#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();

// Stands in for a library that owns its worker threads. You cannot give them thread
// spans because you never see them, which is what leaves process scope as the option.
fn fan_out(items: usize) {
    thread::scope(|scope| {
        for item in 0..items {
            scope.spawn(move || {
                black_box(vec![item as u8; 1024]);
            });
        }
    });
}

let session = Session::new();
let operation = session.operation("fan_out");

const ITEMS: usize = 4;
let span = operation.measure_process();

fan_out(ITEMS);

drop(span.iterations(1));

Implementations§

Source§

impl ProcessSpan

Source

pub fn iterations(self, iterations: u64) -> Self

Sets how many iterations the measured work covers.

This must be called before the span is dropped. Pass the number of times the measured region repeats the work, or 1 for a single unit of work.

Passing 0 — for example when a benchmark could not execute its workload — is permitted; the operation then reports a NaN per-iteration figure to signal that no valid measurement was produced.

Trait Implementations§

Source§

impl Debug for ProcessSpan

Source§

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

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

impl Drop for ProcessSpan

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl RefUnwindSafe for ProcessSpan

Auto Trait Implementations§

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 = !

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

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

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.