1use std::path::Path;
19use std::sync::{Mutex, PoisonError};
20
21use second_brain_core::residency::{self, ComponentResidency, IndexMemory};
22
23use crate::AppState;
24
25pub const ARENA_CAP: i32 = 2;
26
27pub fn cap_arenas() {
32 if std::env::var_os("MALLOC_ARENA_MAX").is_some() {
33 return;
34 }
35 set_arena_max(ARENA_CAP);
36}
37
38#[cfg(target_env = "gnu")]
39fn set_arena_max(arenas: i32) {
40 if unsafe { libc::mallopt(libc::M_ARENA_MAX, arenas) } == 0 {
41 eprintln!("cannot cap glibc at {arenas} malloc arenas");
42 }
43}
44
45#[cfg(not(target_env = "gnu"))]
46fn set_arena_max(_arenas: i32) {}
47
48const ARENA_BYTES: u64 = 64 * 1024 * 1024;
53
54#[derive(Debug, Clone, Copy, Default, serde::Serialize)]
55pub struct HeapStats {
56 pub live_bytes: u64,
58 pub free_bytes: u64,
61 pub system_bytes: u64,
63 pub rss_anon_bytes: u64,
65 pub arena_mappings: usize,
67}
68
69#[derive(Debug, Clone, Copy, serde::Serialize)]
71pub struct HeapReport {
72 #[serde(flatten)]
73 pub stats: HeapStats,
74 pub components: ComponentResidency,
75 pub unattributed_bytes: i64,
78}
79
80static INDEX_WRITER: Mutex<Option<IndexMemory>> = Mutex::new(None);
81
82pub fn record_index_writer(memory: Option<IndexMemory>) {
85 *INDEX_WRITER.lock().unwrap_or_else(PoisonError::into_inner) = memory;
86}
87
88fn recorded_index_writer() -> Option<IndexMemory> {
89 *INDEX_WRITER.lock().unwrap_or_else(PoisonError::into_inner)
90}
91
92pub fn component_residency(state: &AppState) -> ComponentResidency {
93 let reader = state.indexes.as_ref().map(|indexes| {
94 indexes
95 .read()
96 .unwrap_or_else(PoisonError::into_inner)
97 .memory()
98 });
99 residency::measure(
100 state.embedder.as_deref(),
101 state.reranker.as_deref(),
102 reader,
103 recorded_index_writer(),
104 )
105}
106
107pub fn heap_report(state: &AppState) -> HeapReport {
109 let stats = heap_stats();
110 let components = component_residency(state);
111 HeapReport {
112 stats,
113 components,
114 unattributed_bytes: stats.rss_anon_bytes as i64 - components.total() as i64,
115 }
116}
117
118pub fn arena_mappings(maps_path: &Path) -> usize {
119 let Ok(maps) = std::fs::read_to_string(maps_path) else {
120 return 0;
121 };
122 arena_mappings_from_maps(&maps)
123}
124
125fn arena_mappings_from_maps(maps: &str) -> usize {
126 maps.lines()
127 .filter(|line| {
128 let mut fields = line.split_whitespace();
129 let Some(range) = fields.next() else {
130 return false;
131 };
132 if fields.clone().count() >= 5 {
135 return false;
136 }
137 let Some((start, end)) = range.split_once('-') else {
138 return false;
139 };
140 let (Ok(start), Ok(end)) =
141 (u64::from_str_radix(start, 16), u64::from_str_radix(end, 16))
142 else {
143 return false;
144 };
145 end - start == ARENA_BYTES && start % ARENA_BYTES == 0
146 })
147 .count()
148}
149
150pub fn heap_stats() -> HeapStats {
151 let allocator = residency::allocator_bytes();
152 HeapStats {
153 live_bytes: allocator.live,
154 free_bytes: allocator.free,
155 system_bytes: allocator.system,
156 rss_anon_bytes: residency::rss_anon_bytes(),
157 arena_mappings: arena_mappings(Path::new("/proc/self/maps")),
158 }
159}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164
165 #[test]
166 fn only_unnamed_64mib_aligned_mappings_count_as_arenas() {
167 let maps = concat!(
168 "7f0004000000-7f0008000000 rw-p 00000000 00:00 0\n",
170 "7f0010000000-7f0014000000 rw-p 00000000 08:01 1234 /lib/libc.so.6\n",
172 "7f0018100000-7f001c100000 rw-p 00000000 00:00 0\n",
174 "7f0020000000-7f0020200000 rw-p 00000000 00:00 0\n",
176 "555500000000-555500100000 rw-p 00000000 00:00 0 [heap]\n",
177 );
178 assert_eq!(arena_mappings_from_maps(maps), 1);
179 }
180}