| @@ -622,14 +622,17 @@ pub(crate) unsafe fn drop_detached(v: Value) { |
| 622 | 622 | } |
| 623 | 623 | } |
| 624 | 624 | |
| 625 | | −/// Live block accounting: the leak oracle for Perceus tests. |
| 625 | +/// Live block accounting: the leak oracle for Perceus tests; and, from |
| 626 | +/// the same per-thread counts, the total of blocks ever allocated (the |
| 627 | +/// `LOUISE_STATS` measurement, `docs/runtime-abi.md`). |
| 626 | 628 | /// |
| 627 | 629 | /// Each thread counts the blocks it allocates and frees; the number of |
| 628 | 630 | /// live blocks is the sum. A shared counter would make every allocation |
| 629 | 631 | /// and free an atomic read-modify-write, which cost 20–30% of the run |
| 630 | 632 | /// time of allocation-heavy programs. A block freed on another thread |
| 631 | 633 | /// than the one that allocated it makes one count go up and another down; |
| 632 | | −/// the sum stays right. |
| 634 | +/// the sum stays right. The total only ever goes up: a free on another |
| 635 | +/// thread does not lower it. |
| 633 | 636 | pub mod stats { |
| 634 | 637 | #[cfg(feature = "stats")] |
| 635 | 638 | mod counts { |
| @@ -637,13 +640,20 @@ pub mod stats { |
| 637 | 640 | use std::sync::atomic::{AtomicI64, Ordering}; |
| 638 | 641 | use std::sync::{Mutex, PoisonError}; |
| 639 | 642 | |
| 640 | | − /// The count of every thread that ever allocated or freed a block |
| 641 | | − /// (one leaked word per thread, so a count outlives its thread). |
| 642 | | − static ALL: Mutex<Vec<&'static AtomicI64>> = Mutex::new(Vec::new()); |
| 643 | + /// A thread's counts: the blocks it has live (`allocated − freed`) |
| 644 | + /// and the blocks it ever allocated. |
| 645 | + pub struct Counts { |
| 646 | + live: AtomicI64, |
| 647 | + allocs: AtomicI64, |
| 648 | + } |
| 649 | + |
| 650 | + /// The counts of every thread that ever allocated or freed a block |
| 651 | + /// (one leaked struct per thread, so a count outlives its thread). |
| 652 | + static ALL: Mutex<Vec<&'static Counts>> = Mutex::new(Vec::new()); |
| 643 | 653 | |
| 644 | 654 | thread_local! { |
| 645 | 655 | // No destructor: usable while thread-locals are destroyed. |
| 646 | | − static MINE: Cell<Option<&'static AtomicI64>> = const { Cell::new(None) }; |
| 656 | + static MINE: Cell<Option<&'static Counts>> = const { Cell::new(None) }; |
| 647 | 657 | } |
| 648 | 658 | |
| 649 | 659 | /// Inlined into every allocation and free, on actor fibers that |
| @@ -651,41 +661,70 @@ pub mod stats { |
| 651 | 661 | /// `sched` for why that is sound today, and what is left to |
| 652 | 662 | /// review (`docs/code-health.md`, M7). |
| 653 | 663 | #[inline] |
| 654 | | − pub fn add(n: i64) { |
| 655 | | − let count = MINE.get().unwrap_or_else(register); |
| 656 | | − // Only this thread writes its count: no read-modify-write. |
| 657 | | − count.store(count.load(Ordering::Relaxed) + n, Ordering::Relaxed); |
| 664 | + fn mine() -> &'static Counts { |
| 665 | + MINE.get().unwrap_or_else(register) |
| 658 | 666 | } |
| 659 | 667 | |
| 660 | 668 | #[cold] |
| 661 | | − fn register() -> &'static AtomicI64 { |
| 662 | | − let count = Box::leak(Box::new(AtomicI64::new(0))); |
| 669 | + fn register() -> &'static Counts { |
| 670 | + let counts = Box::leak(Box::new(Counts { |
| 671 | + live: AtomicI64::new(0), |
| 672 | + allocs: AtomicI64::new(0), |
| 673 | + })); |
| 663 | 674 | ALL.lock() |
| 664 | 675 | .unwrap_or_else(PoisonError::into_inner) |
| 665 | | − .push(count); |
| 666 | | − MINE.set(Some(count)); |
| 667 | | − count |
| 676 | + .push(counts); |
| 677 | + MINE.set(Some(counts)); |
| 678 | + counts |
| 668 | 679 | } |
| 669 | 680 | |
| 670 | | − pub fn live() -> i64 { |
| 681 | + #[inline] |
| 682 | + pub fn alloc() { |
| 683 | + let counts = mine(); |
| 684 | + // Only this thread writes its counts: no read-modify-write. |
| 685 | + counts |
| 686 | + .live |
| 687 | + .store(counts.live.load(Ordering::Relaxed) + 1, Ordering::Relaxed); |
| 688 | + counts |
| 689 | + .allocs |
| 690 | + .store(counts.allocs.load(Ordering::Relaxed) + 1, Ordering::Relaxed); |
| 691 | + } |
| 692 | + |
| 693 | + #[inline] |
| 694 | + pub fn free() { |
| 695 | + let counts = mine(); |
| 696 | + counts |
| 697 | + .live |
| 698 | + .store(counts.live.load(Ordering::Relaxed) - 1, Ordering::Relaxed); |
| 699 | + } |
| 700 | + |
| 701 | + fn sum(of: fn(&Counts) -> &AtomicI64) -> i64 { |
| 671 | 702 | ALL.lock() |
| 672 | 703 | .unwrap_or_else(PoisonError::into_inner) |
| 673 | 704 | .iter() |
| 674 | | − .map(|c| c.load(Ordering::Relaxed)) |
| 705 | + .map(|c| of(c).load(Ordering::Relaxed)) |
| 675 | 706 | .sum() |
| 676 | 707 | } |
| 708 | + |
| 709 | + pub fn live() -> i64 { |
| 710 | + sum(|c| &c.live) |
| 711 | + } |
| 712 | + |
| 713 | + pub fn allocs() -> i64 { |
| 714 | + sum(|c| &c.allocs) |
| 715 | + } |
| 677 | 716 | } |
| 678 | 717 | |
| 679 | 718 | #[inline] |
| 680 | 719 | pub fn alloc() { |
| 681 | 720 | #[cfg(feature = "stats")] |
| 682 | | − counts::add(1); |
| 721 | + counts::alloc(); |
| 683 | 722 | } |
| 684 | 723 | |
| 685 | 724 | #[inline] |
| 686 | 725 | pub fn free() { |
| 687 | 726 | #[cfg(feature = "stats")] |
| 688 | | − counts::add(-1); |
| 727 | + counts::free(); |
| 689 | 728 | } |
| 690 | 729 | |
| 691 | 730 | /// The number of live blocks; meaningful when no other thread is |
| @@ -698,6 +737,17 @@ pub mod stats { |
| 698 | 737 | #[cfg(not(feature = "stats"))] |
| 699 | 738 | return -1; |
| 700 | 739 | } |
| 740 | + |
| 741 | + /// The number of blocks ever allocated, summed over threads; |
| 742 | + /// meaningful when no other thread is allocating (at exit, say). -1 |
| 743 | + /// without the `stats` feature. |
| 744 | + #[unsafe(no_mangle)] |
| 745 | + pub extern "C" fn tl_stats_allocations() -> i64 { |
| 746 | + #[cfg(feature = "stats")] |
| 747 | + return counts::allocs(); |
| 748 | + #[cfg(not(feature = "stats"))] |
| 749 | + return -1; |
| 750 | + } |
| 701 | 751 | } |
| 702 | 752 | |
| 703 | 753 | /// Release one count of `v` while the drop worklist is being drained |