1#![allow(clippy::extra_unused_lifetimes)]
4
5use rustler::{Error, NifResult};
6
7#[derive(rustler::NifMap)]
8pub struct JemallocStats {
9 active: u64,
10 allocated: u64,
11 epoch: u64,
12 mapped: u64,
13 metadata: u64,
14 resident: u64,
15 retained: u64,
16}
17
18macro_rules! jemalloc_value {
19 ($name: ident) => {
20 jemalloc_ctl::$name::mib()
21 .and_then(|x| x.read())
22 .map(|x| x as u64)
23 .map_err(|x| Error::Term(Box::new(x.to_string())))
24 };
25}
26
27macro_rules! jemalloc_stat_value {
28 ($name: ident) => {
29 jemalloc_ctl::stats::$name::mib()
30 .and_then(|x| x.read())
31 .map(|x| x as u64)
32 .map_err(|x| Error::Term(Box::new(x.to_string())))
33 };
34}
35
36#[rustler::nif]
37pub fn jemalloc_allocation_info() -> NifResult<JemallocStats> {
38 jemalloc_ctl::epoch::mib().and_then(|x| x.advance()).ok();
39
40 Ok(JemallocStats {
41 active: jemalloc_stat_value!(active)?,
42 allocated: jemalloc_stat_value!(allocated)?,
43 epoch: jemalloc_value!(epoch)?,
44 mapped: jemalloc_stat_value!(mapped)?,
45 metadata: jemalloc_stat_value!(metadata)?,
46 resident: jemalloc_stat_value!(resident)?,
47 retained: jemalloc_stat_value!(retained)?,
48 })
49}