1use std::path::Path;
18use std::sync::{Mutex, PoisonError};
19
20use second_brain_core::residency::{self, ComponentResidency, IndexMemory};
21
22use crate::AppState;
23
24const ARENA_BYTES: u64 = 64 * 1024 * 1024;
29
30#[derive(Debug, Clone, Copy, Default, serde::Serialize)]
31pub struct HeapStats {
32 pub live_bytes: u64,
34 pub free_bytes: u64,
37 pub system_bytes: u64,
39 pub rss_anon_bytes: u64,
41 pub arena_mappings: usize,
43}
44
45#[derive(Debug, Clone, Copy, serde::Serialize)]
47pub struct HeapReport {
48 #[serde(flatten)]
49 pub stats: HeapStats,
50 pub components: ComponentResidency,
51 pub unattributed_bytes: i64,
54}
55
56static INDEX_WRITER: Mutex<Option<IndexMemory>> = Mutex::new(None);
57
58pub fn record_index_writer(memory: Option<IndexMemory>) {
61 *INDEX_WRITER.lock().unwrap_or_else(PoisonError::into_inner) = memory;
62}
63
64fn recorded_index_writer() -> Option<IndexMemory> {
65 *INDEX_WRITER.lock().unwrap_or_else(PoisonError::into_inner)
66}
67
68pub fn component_residency(state: &AppState) -> ComponentResidency {
69 let reader = state.indexes.as_ref().map(|indexes| {
70 indexes
71 .read()
72 .unwrap_or_else(PoisonError::into_inner)
73 .memory()
74 });
75 residency::measure(
76 state.embedder.as_deref(),
77 state.reranker.as_deref(),
78 reader,
79 recorded_index_writer(),
80 )
81}
82
83pub fn heap_report(state: &AppState) -> HeapReport {
85 let stats = heap_stats();
86 let components = component_residency(state);
87 HeapReport {
88 stats,
89 components,
90 unattributed_bytes: stats.rss_anon_bytes as i64 - components.total() as i64,
91 }
92}
93
94pub fn arena_mappings(maps_path: &Path) -> usize {
95 let Ok(maps) = std::fs::read_to_string(maps_path) else {
96 return 0;
97 };
98 arena_mappings_from_maps(&maps)
99}
100
101fn arena_mappings_from_maps(maps: &str) -> usize {
102 maps.lines()
103 .filter(|line| {
104 let mut fields = line.split_whitespace();
105 let Some(range) = fields.next() else {
106 return false;
107 };
108 if fields.clone().count() >= 5 {
111 return false;
112 }
113 let Some((start, end)) = range.split_once('-') else {
114 return false;
115 };
116 let (Ok(start), Ok(end)) =
117 (u64::from_str_radix(start, 16), u64::from_str_radix(end, 16))
118 else {
119 return false;
120 };
121 end - start == ARENA_BYTES && start % ARENA_BYTES == 0
122 })
123 .count()
124}
125
126pub fn heap_stats() -> HeapStats {
127 let allocator = residency::allocator_bytes();
128 HeapStats {
129 live_bytes: allocator.live,
130 free_bytes: allocator.free,
131 system_bytes: allocator.system,
132 rss_anon_bytes: residency::rss_anon_bytes(),
133 arena_mappings: arena_mappings(Path::new("/proc/self/maps")),
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 #[test]
142 fn only_unnamed_64mib_aligned_mappings_count_as_arenas() {
143 let maps = concat!(
144 "7f0004000000-7f0008000000 rw-p 00000000 00:00 0\n",
146 "7f0010000000-7f0014000000 rw-p 00000000 08:01 1234 /lib/libc.so.6\n",
148 "7f0018100000-7f001c100000 rw-p 00000000 00:00 0\n",
150 "7f0020000000-7f0020200000 rw-p 00000000 00:00 0\n",
152 "555500000000-555500100000 rw-p 00000000 00:00 0 [heap]\n",
153 );
154 assert_eq!(arena_mappings_from_maps(maps), 1);
155 }
156}