metriki_jemalloc/
lib.rs

1//! # Metriki Jemalloc Instrumentation
2//!
3//! This library provide metrics of jemalloc memory allocator.
4//! The data is fetched from a library called `jemalloc-ctl`, and we
5//! are using the [tikv maintained version of
6//! it](https://github.com/tikv/jemallocator). It is recommended to
7//! use that version of jemallocator too.
8//!
9//! The instrumentation is provided as a `MetricsSet`. Use
10//! `MetricsRegistry::register_metrics_set` to add it to your metriki
11//! registry.
12//!
13//! [An
14//! example](https://github.com/sunng87/metriki/blob/master/metriki-jemalloc/examples/jemalloc.rs)
15//! can be found in our github repo.
16
17use std::collections::HashMap;
18
19use metriki_core::metrics::Metric;
20use metriki_core::MetricsSet;
21use tikv_jemalloc_ctl::{epoch, stats};
22
23/// The MetricsSet that provides gauges of jemalloc data.
24///
25/// Currently, the data is fetched from `jemalloc_ctl::stats`,
26/// including:
27///
28/// - `prefix.jemalloc.active`: bytes of active pages
29/// - `prefix.jemalloc.allocated`: total allocated bytes
30/// - `prefix.jemalloc.metadata`: jemalloc metadata bytes
31/// - `prefix.jemalloc.mapped`: bytes in active extents mapped by the
32///   allocator
33/// - `prefix.jemalloc.resident`: bytes in active extents mapped by
34///   the allocator
35/// - `prefix.jemalloc.retianed`: bytes in physically resident data
36///   pages mapped by the allocator.
37#[derive(Debug)]
38pub struct JemallocMetricsSet {
39    prefix: &'static str,
40}
41
42impl JemallocMetricsSet {
43    /// Create a `JemallocMetricsSet` and specify a prefix for its
44    /// metrics names.
45    pub fn new(prefix: &'static str) -> JemallocMetricsSet {
46        JemallocMetricsSet { prefix }
47    }
48}
49
50impl MetricsSet for JemallocMetricsSet {
51    fn get_all(&self) -> HashMap<String, Metric> {
52        let mut result = HashMap::new();
53
54        epoch::advance().unwrap();
55
56        let active = Metric::gauge(Box::new(|| stats::active::read().unwrap() as f64)).into();
57        result.insert(format!("{}.jemalloc.active", self.prefix), active);
58
59        let allocated = Metric::gauge(Box::new(|| stats::allocated::read().unwrap() as f64)).into();
60        result.insert(format!("{}.jemalloc.allocated", self.prefix), allocated);
61
62        let metadata = Metric::gauge(Box::new(|| stats::metadata::read().unwrap() as f64)).into();
63        result.insert(format!("{}.jemalloc.metadata", self.prefix), metadata);
64
65        let mapped = Metric::gauge(Box::new(|| stats::mapped::read().unwrap() as f64)).into();
66        result.insert(format!("{}.jemalloc.mapped", self.prefix), mapped);
67
68        let resident = Metric::gauge(Box::new(|| stats::resident::read().unwrap() as f64)).into();
69        result.insert(format!("{}.jemalloc.resident", self.prefix), resident);
70
71        let retained = Metric::gauge(Box::new(|| stats::retained::read().unwrap() as f64)).into();
72        result.insert(format!("{}.jemalloc.retained", self.prefix), retained);
73
74        result
75    }
76}