| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::alloc::System; |
| 9 | use std::collections::HashMap; |
| 10 | use std::sync::Mutex; |
| 11 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 12 | |
| 13 | use stats_alloc::StatsAlloc; |
| 14 | use tracking_allocator::{AllocationGroupId, AllocationRegistry, AllocationTracker, Allocator}; |
| 15 | |
| 16 | static SIZES: [AtomicUsize; 24] = [const { AtomicUsize::new(0) }; 24]; |
| 17 | static TRACES: Mutex<Option<HashMap<String, usize>>> = Mutex::new(None); |
| 18 | |
| 19 | |
| 20 | static SEEN_REALLOCS: AtomicUsize = AtomicUsize::new(0); |
| 21 | |
| 22 | struct Track; |
| 23 | impl 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 | |
| 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 | |
| 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] |
| 65 | static A: StatsAlloc<Allocator<System>> = StatsAlloc::new(Allocator::system()); |
| 66 | |
| 67 | fn 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 | } |