Skip to main content

pprof_alloc/stats/
mod.rs

1use serde::Serialize;
2use std::sync::atomic::{AtomicU64, Ordering};
3
4pub mod cgroups;
5pub mod malloc;
6mod malloc_info;
7mod procmaps;
8pub mod smaps;
9
10#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize)]
11pub struct Allocations {
12	pub allocated: u64,
13	pub freed: u64,
14	pub allocations: u64,
15	pub frees: u64,
16}
17
18impl Allocations {
19	pub fn in_use_bytes(&self) -> u64 {
20		self.allocated.saturating_sub(self.freed)
21	}
22
23	pub fn in_use_allocations(&self) -> u64 {
24		self.allocations.saturating_sub(self.frees)
25	}
26}
27
28#[derive(Default, Debug)]
29pub(crate) struct AtomicAllocations {
30	pub allocated: AtomicU64,
31	pub freed: AtomicU64,
32	pub allocations: AtomicU64,
33	pub frees: AtomicU64,
34}
35
36impl AtomicAllocations {
37	pub(crate) const fn new() -> AtomicAllocations {
38		AtomicAllocations {
39			allocated: AtomicU64::new(0),
40			freed: AtomicU64::new(0),
41			allocations: AtomicU64::new(0),
42			frees: AtomicU64::new(0),
43		}
44	}
45
46	pub(crate) fn snapshot(&self) -> Allocations {
47		Allocations {
48			allocated: self.allocated.load(Ordering::Relaxed),
49			freed: self.freed.load(Ordering::Relaxed),
50			allocations: self.allocations.load(Ordering::Relaxed),
51			frees: self.frees.load(Ordering::Relaxed),
52		}
53	}
54}
55
56impl From<AtomicAllocations> for Allocations {
57	fn from(val: AtomicAllocations) -> Self {
58		val.snapshot()
59	}
60}