1use std::{
4 alloc::Layout,
5 io::Write,
6 panic::catch_unwind,
7 process::abort,
8 sync::atomic::{AtomicBool, AtomicU64, Ordering},
9};
10
11#[cfg(feature = "jemalloc_conf")]
12use const_str::concat_bytes;
13use jevmalloc::{
14 Jemalloc,
15 global::hook::{ALLOC, ALLOC_ZEROED},
16 stats::print as print_stats,
17};
18pub use jevmalloc::{arenas::trim, background_thread_enable};
19#[cfg(feature = "jemalloc_conf")]
20use libc::c_char;
21use libc::{STDOUT_FILENO, c_void, write};
22
23use crate::{arrayvec::ArrayVec, utils::BoolExt};
24
25type TraceLine = ArrayVec<u8, 128>;
28
29#[cfg(feature = "jemalloc_conf")]
37#[used]
38#[unsafe(no_mangle)]
39pub static mut malloc_conf: *const c_char = MALLOC_CONF.as_ptr().cast();
40
41#[cfg(feature = "jemalloc_conf")]
50#[used]
51#[unsafe(export_name = "_rjem_malloc_conf")]
52pub static mut MALLOC_CONF_PREFIXED: *const c_char = MALLOC_CONF.as_ptr().cast();
53
54#[cfg(feature = "jemalloc_conf")]
55const MALLOC_CONF: &[u8] = concat_bytes!(
56 "tcache:true",
57 MALLOC_CONF_PERCPU_ARENA,
58 MALLOC_CONF_METADATA_THP,
59 MALLOC_CONF_BACKGROUND,
60 ",lg_extent_max_active_fit:4",
61 ",oversize_threshold:2097152",
62 ",tcache_max:8192",
63 ",dirty_decay_ms:16000",
64 ",muzzy_decay_ms:144000",
65 0
67);
68
69#[cfg(feature = "jemalloc_conf")]
75const MALLOC_CONF_PERCPU_ARENA: &str = cfg_select! {
76 any(
77 target_os = "linux",
78 target_os = "android",
79 target_os = "freebsd",
80 target_os = "dragonfly",
81 target_vendor = "apple",
82 target_os = "windows",
83 ) => ",percpu_arena:percpu",
84 _ => "",
85};
86
87#[cfg(feature = "jemalloc_conf")]
96const MALLOC_CONF_METADATA_THP: &str = cfg_select! {
97 any(
98 target_os = "solaris",
99 target_os = "illumos",
100 all(
101 any(target_os = "linux", target_os = "android"),
102 not(target_arch = "arm"),
103 ),
104 ) => ",metadata_thp:always",
105 _ => "",
106};
107
108#[cfg(feature = "jemalloc_conf")]
109const _MALLOC_CONF_PROF: &str = cfg_select! {
110 all(feature = "jemalloc_prof", target_arch = "x86_64") => ",prof_active:false",
111 _ => "",
112};
113
114#[cfg(feature = "jemalloc_conf")]
116const MALLOC_CONF_BACKGROUND: &str = cfg_select! {
117 target_vendor = "apple" => "",
118 _ => ",background_thread:false,max_background_threads:-1",
119};
120
121#[global_allocator]
122static JEMALLOC: Jemalloc = Jemalloc;
123
124static GLOBAL_ALLOCS: AtomicU64 = AtomicU64::new(0);
125static COUNT_GLOBAL_ALLOCS: AtomicBool = AtomicBool::new(false);
126static TRACE_GLOBAL_ALLOCS: AtomicBool = AtomicBool::new(false);
127
128#[crate::ctor(unsafe)]
134fn _static_initialization() {
135 unsafe { ALLOC = Some(global_alloc_hook) };
138
139 unsafe { ALLOC_ZEROED = Some(global_alloc_zeroed_hook) };
141}
142
143fn global_alloc_hook(layout: Layout) {
144 catch_unwind(move || handle_global_alloc(layout))
145 .map_err(|_| abort())
146 .ok();
147}
148
149fn global_alloc_zeroed_hook(layout: Layout) {
150 catch_unwind(move || handle_global_alloc(layout))
151 .map_err(|_| abort())
152 .ok();
153}
154
155fn handle_global_alloc(layout: Layout) {
156 let do_count = COUNT_GLOBAL_ALLOCS.load(Ordering::Relaxed);
157 let count = GLOBAL_ALLOCS.fetch_add(do_count.into(), Ordering::Relaxed);
158
159 if TRACE_GLOBAL_ALLOCS.load(Ordering::Relaxed) {
160 let mut buf = TraceLine::new();
161
162 writeln!(&mut buf, "{count} align={} size={}", layout.align(), layout.size())
163 .expect("writeln! to buffer failed");
164
165 unsafe { write(STDOUT_FILENO, buf.as_ptr().cast::<c_void>(), buf.len()) }
167 .ge(&0)
168 .into_result()
169 .expect("write(2) error");
170 }
171}
172
173#[inline]
179#[must_use]
180pub fn global_alloc_count() -> u64 { GLOBAL_ALLOCS.load(Ordering::Relaxed) }
181
182#[must_use]
187pub fn memory_stats(opts: &str) -> Option<String> {
188 const MAX_LENGTH: usize = 1_048_576;
189
190 let mut stats = vec![0; MAX_LENGTH];
191 let length = print_stats(opts, &mut stats).ok()?.len();
192 if length == 0 {
193 return None;
194 }
195
196 stats.truncate(length);
197 String::from_utf8(stats).ok()
198}
199
200pub mod this_thread {
205 pub use jevmalloc::thread::this::{decay, set_muzzy_decay};
206}