1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use rustler::{Error, NifResult};

#[derive(rustler::NifMap)]
pub struct JemallocStats {
    active: u64,
    allocated: u64,
    epoch: u64,
    mapped: u64,
    metadata: u64,
    resident: u64,
    retained: u64,
}

macro_rules! jemalloc_value {
    ($name: ident) => {
        jemalloc_ctl::$name::mib()
            .and_then(|x| x.read())
            .map(|x| x as u64)
            .map_err(|x| Error::Term(Box::new(x.to_string())))
    };
}

macro_rules! jemalloc_stat_value {
    ($name: ident) => {
        jemalloc_ctl::stats::$name::mib()
            .and_then(|x| x.read())
            .map(|x| x as u64)
            .map_err(|x| Error::Term(Box::new(x.to_string())))
    };
}

#[rustler::nif]
pub fn allocation_info() -> NifResult<JemallocStats> {
    jemalloc_ctl::epoch::mib().and_then(|x| x.advance()).ok();

    Ok(JemallocStats {
        active: jemalloc_stat_value!(active)?,
        allocated: jemalloc_stat_value!(allocated)?,
        epoch: jemalloc_value!(epoch)?,
        mapped: jemalloc_stat_value!(mapped)?,
        metadata: jemalloc_stat_value!(metadata)?,
        resident: jemalloc_stat_value!(resident)?,
        retained: jemalloc_stat_value!(retained)?,
    })
}