forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

alloc_probe.rs96 lines, 3.8 KiBHistoryRawDownload
1//! Temporary lane-alloc instrument: count allocations and sample backtraces.
2//!
3//! Allocation totals come from `stats_alloc`. The per-size histogram and the
4//! sampled backtraces come from `tracking-allocator`, which calls back into a
5//! tracker on every allocation. Neither crate needs `unsafe` in this file.
6//! Reallocs are excluded from totals and histogram, as the old hand-written
7//! allocator did.
8use std::alloc::System;
9use std::collections::HashMap;
10use std::sync::Mutex;
11use std::sync::atomic::{AtomicUsize, Ordering};
12
13use stats_alloc::StatsAlloc;
14use tracking_allocator::{AllocationGroupId, AllocationRegistry, AllocationTracker, Allocator};
15
16static SIZES: [AtomicUsize; 24] = [const { AtomicUsize::new(0) }; 24];
17static TRACES: Mutex<Option<HashMap<String, usize>>> = Mutex::new(None);
18/// `reallocations` as of the last tracked allocation. A realloc bumps that counter
19/// and then reaches the tracker as an allocation, so a bump means "not a new alloc".
20static SEEN_REALLOCS: AtomicUsize = AtomicUsize::new(0);
21
22struct Track;
23impl AllocationTracker for Track {
24 fn allocated(&self, _addr: usize, object_size: usize, _wrapped: usize, _g: AllocationGroupId) {
25 let st = A.stats();
26 if st.reallocations != SEEN_REALLOCS.swap(st.reallocations, Ordering::Relaxed) {
27 return;
28 }
29 // `allocations` already includes this allocation; index it from 0 like the old counter.
30 let n = st.allocations - 1;
31 let cls = (usize::BITS - object_size.leading_zeros()) as usize;
32 SIZES[cls.min(23)].fetch_add(1, Ordering::Relaxed);
33 if n.is_multiple_of(512) {
34 let bt = format!("{}", std::backtrace::Backtrace::force_capture());
35 let mut key: Vec<String> = Vec::new();
36 let lines: Vec<&str> = bt.lines().collect();
37 for (i, l) in lines.iter().enumerate() {
38 if l.contains("ec_av1::") && key.len() < 6 {
39 let at = lines
40 .get(i + 1)
41 .filter(|n| n.trim_start().starts_with("at "))
42 .map(|n| n.trim().to_string())
43 .unwrap_or_default();
44 key.push(format!("{} {}", l.trim(), at));
45 }
46 }
47 let mut g = TRACES.lock().unwrap();
48 *g.get_or_insert_with(HashMap::new).entry(key.join(" <- ")).or_insert(0) += 1;
49 // The capture's own reallocs were not tracked; skip them too.
50 SEEN_REALLOCS.store(A.stats().reallocations, Ordering::Relaxed);
51 }
52 }
53 fn deallocated(
54 &self,
55 _addr: usize,
56 _object_size: usize,
57 _wrapped: usize,
58 _source: AllocationGroupId,
59 _current: AllocationGroupId,
60 ) {
61 }
62}
63
64#[global_allocator]
65static A: StatsAlloc<Allocator<System>> = StatsAlloc::new(Allocator::system());
66
67fn main() {
68 AllocationRegistry::set_global_tracker(Track).expect("tracker");
69 AllocationRegistry::enable_tracking();
70 let path = std::env::args().nth(1).expect("usage: alloc_probe <stream.obu>");
71 let data = std::fs::read(&path).expect("read");
72 let mut shown = 0usize;
73 let r = ec_av1::stream::decode_stream_with(&data, |_f, _i, s| {
74 if s {
75 shown += 1;
76 }
77 Ok(())
78 });
79 println!("shown={shown} result={:?}", r.is_ok());
80 let st = A.stats();
81 println!("allocs={} bytes={}", st.allocations, st.bytes_allocated);
82 for (i, c) in SIZES.iter().enumerate() {
83 let v = c.load(Ordering::Relaxed);
84 if v > 0 {
85 println!("size 2^{i} : {v}");
86 }
87 }
88 let g = TRACES.lock().unwrap();
89 if let Some(m) = g.as_ref() {
90 let mut v: Vec<_> = m.iter().collect();
91 v.sort_by_key(|(_, c)| std::cmp::Reverse(**c));
92 for (k, c) in v.into_iter().take(30) {
93 println!("{c:6} {k}");
94 }
95 }
96}