Skip to content

Commit

Permalink
Add elision and barrier metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-hughes committed Feb 10, 2025
1 parent dddcb72 commit 0275994
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use core::sync::atomic::AtomicU64;
/// Global counters for various GC stats.
pub static GC_COUNTERS: GcCounters = GcCounters {
finalizers_registered: AtomicU64::new(0),
finalizers_elidable: AtomicU64::new(0),
barriers_visited: AtomicU64::new(0),
allocated_gc: AtomicU64::new(0),
allocated_boxed: AtomicU64::new(0),
allocated_rc: AtomicU64::new(0),
Expand All @@ -35,6 +37,8 @@ pub static GC_COUNTERS: GcCounters = GcCounters {
#[derive(Debug, Default)]
pub struct GcCounters {
pub finalizers_registered: AtomicU64,
pub finalizers_elidable: AtomicU64,
pub barriers_visited: AtomicU64,
pub allocated_gc: AtomicU64,
pub allocated_boxed: AtomicU64,
pub allocated_rc: AtomicU64,
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub struct GcStats {
pub premopt_enabled: u8,
pub num_finalizers_registered: u64,
pub num_finalizers_completed: u64,
pub num_finalizers_elidable: u64,
pub num_barriers_visited: u64,
pub num_allocated_gc: u64,
pub num_allocated_boxed: u64,
pub num_allocated_arc: u64,
Expand Down Expand Up @@ -199,6 +201,8 @@ pub fn stats() -> GcStats {
.finalizers_registered
.load(atomic::Ordering::Relaxed),
num_finalizers_completed: unsafe { bdwgc::GC_finalized_total() },
num_finalizers_elidable: GC_COUNTERS.finalizers_elidable.load(atomic::Ordering::Relaxed),
num_barriers_visited: GC_COUNTERS.barriers_visited.load(atomic::Ordering::Relaxed),
num_allocated_gc: GC_COUNTERS.allocated_gc.load(atomic::Ordering::Relaxed),
num_allocated_boxed: GC_COUNTERS.allocated_boxed.load(atomic::Ordering::Relaxed),
num_allocated_rc: GC_COUNTERS.allocated_rc.load(atomic::Ordering::Relaxed),
Expand Down Expand Up @@ -288,6 +292,8 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Gc<U>> for Gc<T> {}
#[cfg(all(not(bootstrap), not(test), feature = "premature-finalizer-prevention"))]
impl<T: ?Sized> Drop for Gc<T> {
fn drop(&mut self) {
#[cfg(feature = "log-stats")]
GC_COUNTERS.barriers_visited.fetch_add(1, atomic::Ordering::Relaxed);
keep_alive(self);
}
}
Expand Down Expand Up @@ -524,6 +530,10 @@ impl<T> Gc<T> {

#[cfg(feature = "log-stats")]
{
GC_COUNTERS.finalizers_elidable.fetch_add(
crate::mem::needs_finalizer::<T>() as u64,
atomic::Ordering::Relaxed,
);
GC_COUNTERS.finalizers_registered.fetch_add(1, atomic::Ordering::Relaxed);
}
}
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub(crate) fn log_stats() {
premature finalizer prevention enabled,\
premopt enabled,\
finalizers registered,\
finalizers completed,\
barriers visited,\
Gc allocated,\
Box allocated,\
Rc allocated,\
Expand All @@ -139,6 +141,8 @@ pub(crate) fn log_stats() {
stats.premopt_enabled,
stats.num_finalizers_registered,
stats.num_finalizers_completed,
stats.num_finalizers_elidable,
stats.num_barriers_visited,
stats.num_allocated_gc,
stats.num_allocated_boxed,
stats.num_allocated_rc,
Expand Down

0 comments on commit 0275994

Please sign in to comment.