|
| 1 | +//! Set the allocator to `jemalloc`. |
| 2 | +//! |
| 3 | +//! Due to `jemalloc` requiring configuration at compile time or immediately upon runtime |
| 4 | +//! initialisation it is configured via a Cargo config file in `.cargo/config.toml`. |
| 5 | +//! |
| 6 | +//! The `jemalloc` tuning can be overriden by: |
| 7 | +//! |
| 8 | +//! A) `JEMALLOC_SYS_WITH_MALLOC_CONF` at compile-time. |
| 9 | +//! B) `_RJEM_MALLOC_CONF` at runtime. |
| 10 | +use jemalloc_ctl::{arenas, epoch, stats, Error}; |
| 11 | +use lazy_static::lazy_static; |
| 12 | +use lighthouse_metrics::{set_gauge, try_create_int_gauge, IntGauge}; |
| 13 | + |
| 14 | +#[global_allocator] |
| 15 | +static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; |
| 16 | + |
| 17 | +// Metrics for jemalloc. |
| 18 | +lazy_static! { |
| 19 | + pub static ref NUM_ARENAS: lighthouse_metrics::Result<IntGauge> = |
| 20 | + try_create_int_gauge("jemalloc_num_arenas", "The number of arenas in use"); |
| 21 | + pub static ref BYTES_ALLOCATED: lighthouse_metrics::Result<IntGauge> = |
| 22 | + try_create_int_gauge("jemalloc_bytes_allocated", "Equivalent to stats.allocated"); |
| 23 | + pub static ref BYTES_ACTIVE: lighthouse_metrics::Result<IntGauge> = |
| 24 | + try_create_int_gauge("jemalloc_bytes_active", "Equivalent to stats.active"); |
| 25 | + pub static ref BYTES_MAPPED: lighthouse_metrics::Result<IntGauge> = |
| 26 | + try_create_int_gauge("jemalloc_bytes_mapped", "Equivalent to stats.mapped"); |
| 27 | + pub static ref BYTES_METADATA: lighthouse_metrics::Result<IntGauge> = |
| 28 | + try_create_int_gauge("jemalloc_bytes_metadata", "Equivalent to stats.metadata"); |
| 29 | + pub static ref BYTES_RESIDENT: lighthouse_metrics::Result<IntGauge> = |
| 30 | + try_create_int_gauge("jemalloc_bytes_resident", "Equivalent to stats.resident"); |
| 31 | + pub static ref BYTES_RETAINED: lighthouse_metrics::Result<IntGauge> = |
| 32 | + try_create_int_gauge("jemalloc_bytes_retained", "Equivalent to stats.retained"); |
| 33 | +} |
| 34 | + |
| 35 | +pub fn scrape_jemalloc_metrics() { |
| 36 | + scrape_jemalloc_metrics_fallible().unwrap() |
| 37 | +} |
| 38 | + |
| 39 | +pub fn scrape_jemalloc_metrics_fallible() -> Result<(), Error> { |
| 40 | + // Advance the epoch so that the underlying statistics are updated. |
| 41 | + epoch::advance()?; |
| 42 | + |
| 43 | + set_gauge(&NUM_ARENAS, arenas::narenas::read()? as i64); |
| 44 | + set_gauge(&BYTES_ALLOCATED, stats::allocated::read()? as i64); |
| 45 | + set_gauge(&BYTES_ACTIVE, stats::active::read()? as i64); |
| 46 | + set_gauge(&BYTES_MAPPED, stats::mapped::read()? as i64); |
| 47 | + set_gauge(&BYTES_METADATA, stats::metadata::read()? as i64); |
| 48 | + set_gauge(&BYTES_RESIDENT, stats::resident::read()? as i64); |
| 49 | + set_gauge(&BYTES_RETAINED, stats::retained::read()? as i64); |
| 50 | + |
| 51 | + Ok(()) |
| 52 | +} |
0 commit comments