micromegas_telemetry_sink/jemalloc_conf.rs
1//! Exports the `malloc_conf` weak symbol jemalloc reads at startup, so every
2//! binary declaring jemalloc as its global allocator runs with background
3//! purging and a tightened dirty decay instead of jemalloc's stock
4//! `background_thread:false,dirty_decay_ms:10000`. See
5//! `mkdocs/docs/admin/memory-allocator.md`.
6
7/// NUL-terminated: jemalloc reads this symbol as a `const char *`
8/// (`jemalloc/src/conf.c`, `jemalloc/src/jemalloc.c`).
9///
10/// `muzzy_decay_ms:0` is already jemalloc 5.3's default -- pinned explicitly
11/// since the default has moved across releases, and because it means muzzy
12/// pages are `MADV_DONTNEED`'d immediately, the opposite of what a higher
13/// value would do.
14pub const CONF: &[u8] = b"background_thread:true,dirty_decay_ms:5000,muzzy_decay_ms:0\0";
15
16/// Declares the `_rjem_malloc_conf` symbol jemalloc's `conf.c` resolves at
17/// precedence 1 (weaker than `_RJEM_MALLOC_CONF`, stronger than the
18/// compile-time `--with-malloc-conf` default), pointing it at [`CONF`].
19///
20/// Must be invoked from a **binary** crate, immediately below its
21/// `#[global_allocator]` static: jemalloc's C code defines
22/// `_rjem_malloc_conf` as a weak definition, so nothing forces the linker to
23/// pull our override out of an rlib -- only a binary's own object files are
24/// always linked in full. `$crate` (rather than a bare `CONF`) is required
25/// because this macro expands inside each calling binary crate, not inside
26/// `telemetry-sink`.
27#[macro_export]
28macro_rules! declare_jemalloc_conf {
29 () => {
30 #[cfg(not(target_os = "windows"))]
31 #[used]
32 #[unsafe(export_name = "_rjem_malloc_conf")]
33 pub static MICROMEGAS_MALLOC_CONF: Option<&'static u8> =
34 Some(&$crate::jemalloc_conf::CONF[0]);
35 };
36}