| @@ -319,26 +319,74 @@ pub(crate) unsafe fn drop_detached(v: Value) { |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | /// Live block accounting: the leak oracle for Perceus tests. |
| 322 | +/// |
| 323 | +/// Each thread counts the blocks it allocates and frees; the number of |
| 324 | +/// live blocks is the sum. A shared counter would make every allocation |
| 325 | +/// and free an atomic read-modify-write, which cost 20–30% of the run |
| 326 | +/// time of allocation-heavy programs. A block freed on another thread |
| 327 | +/// than the one that allocated it makes one count go up and another down; |
| 328 | +/// the sum stays right. |
| 322 | 329 | pub mod stats { |
| 323 | 330 | #[cfg(feature = "stats")] |
| 324 | | − static LIVE: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0); |
| 331 | + mod counts { |
| 332 | + use std::cell::Cell; |
| 333 | + use std::sync::atomic::{AtomicI64, Ordering}; |
| 334 | + use std::sync::{Mutex, PoisonError}; |
| 335 | + |
| 336 | + /// The count of every thread that ever allocated or freed a block |
| 337 | + /// (one leaked word per thread, so a count outlives its thread). |
| 338 | + static ALL: Mutex<Vec<&'static AtomicI64>> = Mutex::new(Vec::new()); |
| 339 | + |
| 340 | + thread_local! { |
| 341 | + // No destructor: usable while thread-locals are destroyed. |
| 342 | + static MINE: Cell<Option<&'static AtomicI64>> = const { Cell::new(None) }; |
| 343 | + } |
| 344 | + |
| 345 | + #[inline] |
| 346 | + pub fn add(n: i64) { |
| 347 | + let count = MINE.get().unwrap_or_else(register); |
| 348 | + // Only this thread writes its count: no read-modify-write. |
| 349 | + count.store(count.load(Ordering::Relaxed) + n, Ordering::Relaxed); |
| 350 | + } |
| 351 | + |
| 352 | + #[cold] |
| 353 | + fn register() -> &'static AtomicI64 { |
| 354 | + let count = Box::leak(Box::new(AtomicI64::new(0))); |
| 355 | + ALL.lock() |
| 356 | + .unwrap_or_else(PoisonError::into_inner) |
| 357 | + .push(count); |
| 358 | + MINE.set(Some(count)); |
| 359 | + count |
| 360 | + } |
| 361 | + |
| 362 | + pub fn live() -> i64 { |
| 363 | + ALL.lock() |
| 364 | + .unwrap_or_else(PoisonError::into_inner) |
| 365 | + .iter() |
| 366 | + .map(|c| c.load(Ordering::Relaxed)) |
| 367 | + .sum() |
| 368 | + } |
| 369 | + } |
| 325 | 370 | |
| 326 | 371 | #[inline] |
| 327 | 372 | pub fn alloc() { |
| 328 | 373 | #[cfg(feature = "stats")] |
| 329 | | − LIVE.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 374 | + counts::add(1); |
| 330 | 375 | } |
| 331 | 376 | |
| 332 | 377 | #[inline] |
| 333 | 378 | pub fn free() { |
| 334 | 379 | #[cfg(feature = "stats")] |
| 335 | | − LIVE.fetch_sub(1, std::sync::atomic::Ordering::Relaxed); |
| 380 | + counts::add(-1); |
| 336 | 381 | } |
| 337 | 382 | |
| 383 | + /// The number of live blocks; meaningful when no other thread is |
| 384 | + /// allocating or freeing (at exit, say). -1 without the `stats` |
| 385 | + /// feature. |
| 338 | 386 | #[unsafe(no_mangle)] |
| 339 | 387 | pub extern "C" fn tl_stats_live_blocks() -> i64 { |
| 340 | 388 | #[cfg(feature = "stats")] |
| 341 | | − return LIVE.load(std::sync::atomic::Ordering::Relaxed); |
| 389 | + return counts::live(); |
| 342 | 390 | #[cfg(not(feature = "stats"))] |
| 343 | 391 | return -1; |
| 344 | 392 | } |