kevy_alloc/snapshot.rs
1//! Turning a heap into a [`Stats`] snapshot.
2//!
3//! Split out of `heap.rs` for the file-size rule, and the seam is a real
4//! one: everything here reads, nothing allocates, and it runs on an INFO
5//! call rather than per operation. Only `live` and `rounding` have to be
6//! maintained as allocations happen — they depend on the size a caller
7//! asked for, which nothing else records. The rest is derived by walking
8//! the segments when someone asks.
9
10use crate::class;
11use crate::class::SPAN_BYTES;
12use crate::heap::Heap;
13use crate::segment::{FIRST_DATA_SPAN, NO_CLASS, SEGMENT_BYTES, SPANS_PER_SEGMENT};
14use crate::stats::Stats;
15
16impl Heap {
17 /// Where every mapped byte is (`bench/V5-ACCOUNTING-CONTRACT.md` §1).
18 ///
19 /// Walks the segments rather than maintaining seven counters on the
20 /// hot path: only `live` and `rounding` depend on the requested size
21 /// and must be tracked as allocations happen. Stats are read on INFO,
22 /// not per operation.
23 #[must_use]
24 pub fn snapshot(&self) -> Stats {
25 let mut st =
26 Stats { live: self.live_bytes, rounding: self.rounding_bytes, ..Stats::default() };
27 let mut seg = self.segments;
28 while !seg.is_null() {
29 // SAFETY: live header from our own list.
30 let s = unsafe { &*seg };
31 st.mapped += SEGMENT_BYTES as u64;
32 st.segment_overhead += SPAN_BYTES as u64;
33 // Slots freed by another thread are still inside this
34 // heap's `live`/`rounding` totals, because that thread could
35 // not reach across to adjust them. Move the amount over here
36 // so every byte is counted exactly once.
37 let parked = s.foreign_bytes.load(core::sync::atomic::Ordering::Relaxed) as u64;
38 let parked_live = s.foreign_live.load(core::sync::atomic::Ordering::Relaxed) as u64;
39 st.cache += parked;
40 st.live -= parked_live;
41 st.rounding -= parked - parked_live;
42 for ix in FIRST_DATA_SPAN..SPANS_PER_SEGMENT {
43 add_span(&mut st, &s.spans[ix]);
44 if s.spans[ix].class != NO_CLASS {
45 st.spans_assigned += 1;
46 }
47 }
48 seg = s.next;
49 }
50 // Claimed-word bits the heap holds locally: span-side they
51 // count as live (they pin pages exactly as live slots do), but
52 // no caller holds them — they are resident, allocatable bytes,
53 // which is the definition of `span_free`.
54 st.span_free += self.claims_unused_bytes();
55 st
56 }
57}
58
59/// Fold one span's bytes into a snapshot.
60fn add_span(st: &mut Stats, meta: &crate::segment::SpanMeta) {
61 if meta.class == NO_CLASS {
62 st.hysteresis += SPAN_BYTES as u64;
63 return;
64 }
65 let slot = class::size_of(meta.class as usize) as u64;
66 // live + rounding are already counted from the requested sizes; the
67 // slots themselves are exactly live * slot, so only the free parts
68 // are classified here. A free slot below the high-water mark is
69 // `returned` when every page it overlaps has been discarded
70 // (mapped, not resident) and `span_free` otherwise (touched,
71 // resident). Everything at or above the mark — including the tail
72 // no slot covers — was never touched: `virgin`.
73 for i in 0..u32::from(meta.high_water) {
74 if meta.is_live(i) {
75 continue;
76 }
77 let (pa, pb) = crate::pagemap::pages_of_slot(i, slot as usize);
78 let all_gone = (pa..=pb).all(|p| meta.discarded & (1u16 << p) != 0);
79 if all_gone {
80 st.returned += slot;
81 } else {
82 st.span_free += slot;
83 }
84 }
85 st.virgin += SPAN_BYTES as u64 - u64::from(meta.high_water) * slot;
86}