euv_performance/allocator/
impl.rs1use super::*;
2
3impl CountingAllocator {
5 #[inline(always)]
11 fn record_alloc(size: usize) {
12 if ALLOC_TRACK_DEPTH.load(Ordering::Relaxed) == 0 {
13 return;
14 }
15 let bytes: u64 = size as u64;
16 let signed: i64 = size as i64;
17 let _: u64 = ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
18 let _: u64 = BYTES_ALLOCATED.fetch_add(bytes, Ordering::Relaxed);
19 let _: i64 = LIVE_ALLOCS_DELTA.fetch_add(1, Ordering::Relaxed);
20 let next_live: i64 = LIVE_BYTES_DELTA
21 .fetch_add(signed, Ordering::Relaxed)
22 .saturating_add(signed);
23 if next_live > 0 {
24 let _: u64 = PEAK_LIVE_BYTES.fetch_max(next_live as u64, Ordering::Relaxed);
25 }
26 }
27
28 #[inline(always)]
34 fn record_dealloc(size: usize) {
35 if ALLOC_TRACK_DEPTH.load(Ordering::Relaxed) == 0 {
36 return;
37 }
38 let bytes: u64 = size as u64;
39 let signed: i64 = size as i64;
40 let _: u64 = FREE_COUNT.fetch_add(1, Ordering::Relaxed);
41 let _: u64 = BYTES_FREED.fetch_add(bytes, Ordering::Relaxed);
42 let _: i64 = LIVE_ALLOCS_DELTA.fetch_sub(1, Ordering::Relaxed);
43 let _: i64 = LIVE_BYTES_DELTA.fetch_sub(signed, Ordering::Relaxed);
44 }
45
46 #[inline(always)]
53 fn record_realloc(old_size: usize, new_size: usize) {
54 if ALLOC_TRACK_DEPTH.load(Ordering::Relaxed) == 0 {
55 return;
56 }
57 let _: u64 = ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
58 let _: u64 = FREE_COUNT.fetch_add(1, Ordering::Relaxed);
59 let _: u64 = BYTES_ALLOCATED.fetch_add(new_size as u64, Ordering::Relaxed);
60 let _: u64 = BYTES_FREED.fetch_add(old_size as u64, Ordering::Relaxed);
61 let delta: i64 = (new_size as i64).saturating_sub(old_size as i64);
62 let next_live: i64 = LIVE_BYTES_DELTA
63 .fetch_add(delta, Ordering::Relaxed)
64 .saturating_add(delta);
65 if next_live > 0 {
66 let _: u64 = PEAK_LIVE_BYTES.fetch_max(next_live as u64, Ordering::Relaxed);
67 }
68 }
69}
70
71unsafe impl GlobalAlloc for CountingAllocator {
73 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
84 let ptr: *mut u8 = unsafe { System.alloc(layout) };
85 if !ptr.is_null() {
86 Self::record_alloc(layout.size());
87 }
88 ptr
89 }
90
91 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
99 Self::record_dealloc(layout.size());
100 unsafe { System.dealloc(ptr, layout) };
101 }
102
103 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
114 let ptr: *mut u8 = unsafe { System.alloc_zeroed(layout) };
115 if !ptr.is_null() {
116 Self::record_alloc(layout.size());
117 }
118 ptr
119 }
120
121 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
134 let next: *mut u8 = unsafe { System.realloc(ptr, layout, new_size) };
135 if !next.is_null() {
136 Self::record_realloc(layout.size(), new_size);
137 }
138 next
139 }
140}
141
142impl RawAllocCounters {
144 #[must_use]
155 pub fn delta_since(&self, before: RawAllocCounters) -> AllocationSnapshot {
156 let alloc_count: u64 = self
157 .get_alloc_count()
158 .saturating_sub(before.get_alloc_count());
159 let free_count: u64 = self
160 .get_free_count()
161 .saturating_sub(before.get_free_count());
162 let bytes_allocated: u64 = self
163 .get_bytes_allocated()
164 .saturating_sub(before.get_bytes_allocated());
165 let bytes_freed: u64 = self
166 .get_bytes_freed()
167 .saturating_sub(before.get_bytes_freed());
168 let signed_live_bytes: i64 = self
169 .get_live_bytes_delta()
170 .saturating_sub(before.get_live_bytes_delta());
171 let signed_live_allocs: i64 = self
172 .get_live_allocs_delta()
173 .saturating_sub(before.get_live_allocs_delta());
174 let live_bytes_delta: u64 = if signed_live_bytes > 0 {
175 signed_live_bytes as u64
176 } else {
177 0
178 };
179 let live_allocs_delta: u64 = if signed_live_allocs > 0 {
180 signed_live_allocs as u64
181 } else {
182 0
183 };
184 let peak_live_bytes: u64 = self.get_peak_live_bytes().max(live_bytes_delta);
185 AllocationSnapshot::new(
186 alloc_count,
187 free_count,
188 bytes_allocated,
189 bytes_freed,
190 live_bytes_delta,
191 live_allocs_delta,
192 peak_live_bytes,
193 )
194 }
195}
196
197impl AllocationScopeGuard {
199 #[must_use]
205 pub fn enter() -> Self {
206 let already_in_scope: bool = ALLOC_THREAD_IN_SCOPE.with(|flag: &Cell<bool>| flag.get());
207 if already_in_scope {
208 let _: usize = ALLOC_TRACK_DEPTH.fetch_add(1, Ordering::AcqRel);
209 let start_counters: RawAllocCounters = snapshot_raw_alloc_counters();
210 return Self {
211 _lock_guard: None,
212 owns_scope: false,
213 start_counters,
214 };
215 }
216 let lock_guard: MutexGuard<'static, ()> = ALLOC_SCOPE_LOCK
217 .lock()
218 .unwrap_or_else(|poisoned: PoisonError<MutexGuard<'static, ()>>| poisoned.into_inner());
219 ALLOC_THREAD_IN_SCOPE.with(|flag: &Cell<bool>| flag.set(true));
220 LIVE_BYTES_DELTA.store(0, Ordering::Relaxed);
221 LIVE_ALLOCS_DELTA.store(0, Ordering::Relaxed);
222 PEAK_LIVE_BYTES.store(0, Ordering::Relaxed);
223 let _: usize = ALLOC_TRACK_DEPTH.fetch_add(1, Ordering::AcqRel);
224 fence(Ordering::SeqCst);
225 let start_counters: RawAllocCounters = snapshot_raw_alloc_counters();
226 Self {
227 _lock_guard: Some(lock_guard),
228 owns_scope: true,
229 start_counters,
230 }
231 }
232
233 #[must_use]
243 pub fn elapsed_snapshot(&self) -> AllocationSnapshot {
244 let Self { start_counters, .. } = *self;
245 fence(Ordering::SeqCst);
246 snapshot_raw_alloc_counters().delta_since(start_counters)
247 }
248}
249
250impl Drop for AllocationScopeGuard {
252 fn drop(&mut self) {
258 let Self { owns_scope, .. } = *self;
259 fence(Ordering::SeqCst);
260 let _: usize = ALLOC_TRACK_DEPTH.fetch_sub(1, Ordering::AcqRel);
261 if owns_scope {
262 ALLOC_THREAD_IN_SCOPE.with(|flag: &Cell<bool>| flag.set(false));
263 }
264 }
265}