|
| 1 | +#![allow(missing_docs)] |
| 2 | + |
| 3 | +#[cfg(not(no_gc))] |
| 4 | +#[allow(nonstandard_style)] |
| 5 | +#[allow(missing_debug_implementations)] |
| 6 | +pub mod api { |
| 7 | + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); |
| 8 | +} |
| 9 | + |
| 10 | +#[cfg(not(no_gc))] |
| 11 | +pub use api::*; |
| 12 | + |
| 13 | +pub mod metrics { |
| 14 | + #[derive(Copy, Clone, Debug)] |
| 15 | + pub enum Metric { |
| 16 | + AllocationsArc, |
| 17 | + AllocationsGc, |
| 18 | + AllocationsRc, |
| 19 | + AllocationsBox, |
| 20 | + FinalizersRun, |
| 21 | + FinalizersElided, |
| 22 | + FinalizersRegistered, |
| 23 | + } |
| 24 | + |
| 25 | + trait MetricsImpl { |
| 26 | + fn init(&self) {} |
| 27 | + fn increment(&self, _amount: u64, _metric: Metric) {} |
| 28 | + fn capture(&self, _is_last: bool) {} |
| 29 | + } |
| 30 | + |
| 31 | + #[cfg(feature = "gc-metrics")] |
| 32 | + mod active { |
| 33 | + use core::sync::atomic::{AtomicU64, Ordering}; |
| 34 | + |
| 35 | + use super::{Metric, MetricsImpl}; |
| 36 | + |
| 37 | + pub(super) struct Metrics { |
| 38 | + finalizers_registered: AtomicU64, |
| 39 | + finalizers_elidable: AtomicU64, |
| 40 | + finalizers_completed: AtomicU64, |
| 41 | + barriers_visited: AtomicU64, |
| 42 | + allocated_gc: AtomicU64, |
| 43 | + allocated_arc: AtomicU64, |
| 44 | + allocated_rc: AtomicU64, |
| 45 | + allocated_boxed: AtomicU64, |
| 46 | + } |
| 47 | + |
| 48 | + impl Metrics { |
| 49 | + pub const fn new() -> Self { |
| 50 | + Self { |
| 51 | + finalizers_registered: AtomicU64::new(0), |
| 52 | + finalizers_elidable: AtomicU64::new(0), |
| 53 | + finalizers_completed: AtomicU64::new(0), |
| 54 | + barriers_visited: AtomicU64::new(0), |
| 55 | + allocated_gc: AtomicU64::new(0), |
| 56 | + allocated_arc: AtomicU64::new(0), |
| 57 | + allocated_rc: AtomicU64::new(0), |
| 58 | + allocated_boxed: AtomicU64::new(0), |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + pub extern "C" fn record_post_collection(event: crate::GC_EventType) { |
| 64 | + if event == crate::GC_EventType_GC_EVENT_END { |
| 65 | + super::METRICS.capture(false); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + impl MetricsImpl for Metrics { |
| 70 | + fn init(&self) { |
| 71 | + unsafe { |
| 72 | + crate::GC_enable_benchmark_stats(); |
| 73 | + crate::GC_set_on_collection_event(Some(record_post_collection)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fn increment(&self, amount: u64, metric: Metric) { |
| 78 | + match metric { |
| 79 | + Metric::AllocationsArc => { |
| 80 | + self.allocated_boxed.fetch_sub(amount, Ordering::Relaxed); |
| 81 | + self.allocated_arc.fetch_add(amount, Ordering::Relaxed); |
| 82 | + } |
| 83 | + Metric::AllocationsRc => { |
| 84 | + self.allocated_boxed.fetch_sub(amount, Ordering::Relaxed); |
| 85 | + self.allocated_rc.fetch_add(amount, Ordering::Relaxed); |
| 86 | + } |
| 87 | + Metric::AllocationsBox => { |
| 88 | + self.allocated_boxed.fetch_add(amount, Ordering::Relaxed); |
| 89 | + } |
| 90 | + Metric::AllocationsGc => { |
| 91 | + self.allocated_gc.fetch_add(amount, Ordering::Relaxed); |
| 92 | + } |
| 93 | + Metric::FinalizersRun => { |
| 94 | + self.finalizers_completed.fetch_add(amount, Ordering::Relaxed); |
| 95 | + } |
| 96 | + Metric::FinalizersElided => { |
| 97 | + self.finalizers_completed.fetch_add(amount, Ordering::Relaxed); |
| 98 | + } |
| 99 | + Metric::FinalizersRegistered => { |
| 100 | + self.finalizers_registered.fetch_add(amount, Ordering::Relaxed); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fn capture(&self, is_last: bool) { |
| 106 | + // Must preserve this ordering as it's hardcoded inside BDWGC. |
| 107 | + // See src/bdwgc/misc.c:2812 |
| 108 | + unsafe { |
| 109 | + crate::GC_log_metrics( |
| 110 | + self.finalizers_completed.load(Ordering::Relaxed), |
| 111 | + self.finalizers_registered.load(Ordering::Relaxed), |
| 112 | + self.allocated_gc.load(Ordering::Relaxed), |
| 113 | + self.allocated_arc.load(Ordering::Relaxed), |
| 114 | + self.allocated_rc.load(Ordering::Relaxed), |
| 115 | + self.allocated_boxed.load(Ordering::Relaxed), |
| 116 | + is_last as i32, |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + #[cfg(not(feature = "gc-metrics"))] |
| 124 | + pub mod noop { |
| 125 | + use super::MetricsImpl; |
| 126 | + |
| 127 | + #[derive(Debug, Default)] |
| 128 | + pub struct Metrics; |
| 129 | + |
| 130 | + impl Metrics { |
| 131 | + pub const fn new() -> Self { |
| 132 | + Self |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + impl MetricsImpl for Metrics {} |
| 137 | + } |
| 138 | + |
| 139 | + #[cfg(feature = "gc-metrics")] |
| 140 | + use active::Metrics; |
| 141 | + #[cfg(not(feature = "gc-metrics"))] |
| 142 | + use noop::Metrics; |
| 143 | + |
| 144 | + static METRICS: Metrics = Metrics::new(); |
| 145 | + |
| 146 | + pub fn init() { |
| 147 | + METRICS.init(); |
| 148 | + } |
| 149 | + |
| 150 | + pub fn record_final() { |
| 151 | + METRICS.capture(true); |
| 152 | + } |
| 153 | + |
| 154 | + pub fn increment(amount: u64, metric: Metric) { |
| 155 | + METRICS.increment(amount, metric); |
| 156 | + } |
| 157 | +} |
0 commit comments