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
impl Profiler
Sourcepub fn start() -> Self
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}Sourcepub fn stop(self) -> AllocStats
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§
impl Freeze for Profiler
impl RefUnwindSafe for Profiler
impl Send for Profiler
impl Sync for Profiler
impl Unpin for Profiler
impl UnsafeUnpin for Profiler
impl UnwindSafe for Profiler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more