Skip to main content

Profiler

Struct Profiler 

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

Scoped profiler that captures a delta between start and stop.

Read the snapshot of the installed ModAlloc on construction and again on Profiler::stop, returning the difference. If no ModAlloc is installed as #[global_allocator] and no allocation has occurred through it yet, both snapshots are zero and the delta is zero.

§Example

use mod_alloc::{ModAlloc, Profiler};

#[global_allocator]
static GLOBAL: ModAlloc = ModAlloc::new();

fn main() {
    let p = Profiler::start();
    let v: Vec<u8> = vec![0; 1024];
    drop(v);
    let stats = p.stop();
    println!("Captured {} alloc events", stats.alloc_count);
}

Implementations§

Source§

impl Profiler

Source

pub fn start() -> Self

Begin profiling, capturing the current allocation state.

If no ModAlloc is installed as #[global_allocator] or no allocation has occurred yet, the captured baseline is all zeros.

§Example
use mod_alloc::Profiler;

let p = Profiler::start();
let _delta = p.stop();
Examples found in repository?
examples/basic.rs (line 11)
10fn main() {
11    let p = Profiler::start();
12
13    let v: Vec<u64> = (0..1_000).collect();
14    let sum: u64 = v.iter().sum();
15    drop(v);
16
17    let mut owned: Vec<String> = Vec::with_capacity(100);
18    for i in 0..100 {
19        owned.push(format!("item-{i}"));
20    }
21    drop(owned);
22
23    let delta = p.stop();
24    println!("Profiler delta (alloc/total/current = delta; peak = absolute):");
25    println!("  alloc_count:   {}", delta.alloc_count);
26    println!("  total_bytes:   {}", delta.total_bytes);
27    println!("  current_bytes: {}", delta.current_bytes);
28    println!("  peak_bytes:    {}", delta.peak_bytes);
29
30    let snap = GLOBAL.snapshot();
31    println!();
32    println!("Process-wide snapshot:");
33    println!("  alloc_count:   {}", snap.alloc_count);
34    println!("  total_bytes:   {}", snap.total_bytes);
35    println!("  current_bytes: {}", snap.current_bytes);
36    println!("  peak_bytes:    {}", snap.peak_bytes);
37
38    println!();
39    println!("(workload checksum: {sum})");
40}
Source

pub fn stop(self) -> AllocStats

Stop profiling and return the delta from start.

alloc_count, total_bytes, and current_bytes are deltas from start() to stop(). peak_bytes is the absolute high-water mark observed during the profiling window (peak has no meaningful delta semantic).

§Example
use mod_alloc::Profiler;

let p = Profiler::start();
let stats = p.stop();
assert_eq!(stats.alloc_count, 0);
Examples found in repository?
examples/basic.rs (line 23)
10fn main() {
11    let p = Profiler::start();
12
13    let v: Vec<u64> = (0..1_000).collect();
14    let sum: u64 = v.iter().sum();
15    drop(v);
16
17    let mut owned: Vec<String> = Vec::with_capacity(100);
18    for i in 0..100 {
19        owned.push(format!("item-{i}"));
20    }
21    drop(owned);
22
23    let delta = p.stop();
24    println!("Profiler delta (alloc/total/current = delta; peak = absolute):");
25    println!("  alloc_count:   {}", delta.alloc_count);
26    println!("  total_bytes:   {}", delta.total_bytes);
27    println!("  current_bytes: {}", delta.current_bytes);
28    println!("  peak_bytes:    {}", delta.peak_bytes);
29
30    let snap = GLOBAL.snapshot();
31    println!();
32    println!("Process-wide snapshot:");
33    println!("  alloc_count:   {}", snap.alloc_count);
34    println!("  total_bytes:   {}", snap.total_bytes);
35    println!("  current_bytes: {}", snap.current_bytes);
36    println!("  peak_bytes:    {}", snap.peak_bytes);
37
38    println!();
39    println!("(workload checksum: {sum})");
40}

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