use std::path::Path;
use std::sync::{Mutex, PoisonError};
use second_brain_core::residency::{self, ComponentResidency, IndexMemory};
use crate::AppState;
const ARENA_BYTES: u64 = 64 * 1024 * 1024;
#[derive(Debug, Clone, Copy, Default, serde::Serialize)]
pub struct HeapStats {
pub live_bytes: u64,
pub free_bytes: u64,
pub system_bytes: u64,
pub rss_anon_bytes: u64,
pub arena_mappings: usize,
}
#[derive(Debug, Clone, Copy, serde::Serialize)]
pub struct HeapReport {
#[serde(flatten)]
pub stats: HeapStats,
pub components: ComponentResidency,
pub unattributed_bytes: i64,
}
static INDEX_WRITER: Mutex<Option<IndexMemory>> = Mutex::new(None);
pub fn record_index_writer(memory: Option<IndexMemory>) {
*INDEX_WRITER.lock().unwrap_or_else(PoisonError::into_inner) = memory;
}
fn recorded_index_writer() -> Option<IndexMemory> {
*INDEX_WRITER.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn component_residency(state: &AppState) -> ComponentResidency {
let reader = state.indexes.as_ref().map(|indexes| {
indexes
.read()
.unwrap_or_else(PoisonError::into_inner)
.memory()
});
residency::measure(
state.embedder.as_deref(),
state.reranker.as_deref(),
reader,
recorded_index_writer(),
)
}
pub fn heap_report(state: &AppState) -> HeapReport {
let stats = heap_stats();
let components = component_residency(state);
HeapReport {
stats,
components,
unattributed_bytes: stats.rss_anon_bytes as i64 - components.total() as i64,
}
}
pub fn arena_mappings(maps_path: &Path) -> usize {
let Ok(maps) = std::fs::read_to_string(maps_path) else {
return 0;
};
arena_mappings_from_maps(&maps)
}
fn arena_mappings_from_maps(maps: &str) -> usize {
maps.lines()
.filter(|line| {
let mut fields = line.split_whitespace();
let Some(range) = fields.next() else {
return false;
};
if fields.clone().count() >= 5 {
return false;
}
let Some((start, end)) = range.split_once('-') else {
return false;
};
let (Ok(start), Ok(end)) =
(u64::from_str_radix(start, 16), u64::from_str_radix(end, 16))
else {
return false;
};
end - start == ARENA_BYTES && start % ARENA_BYTES == 0
})
.count()
}
pub fn heap_stats() -> HeapStats {
let allocator = residency::allocator_bytes();
HeapStats {
live_bytes: allocator.live,
free_bytes: allocator.free,
system_bytes: allocator.system,
rss_anon_bytes: residency::rss_anon_bytes(),
arena_mappings: arena_mappings(Path::new("/proc/self/maps")),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_unnamed_64mib_aligned_mappings_count_as_arenas() {
let maps = concat!(
"7f0004000000-7f0008000000 rw-p 00000000 00:00 0\n",
"7f0010000000-7f0014000000 rw-p 00000000 08:01 1234 /lib/libc.so.6\n",
"7f0018100000-7f001c100000 rw-p 00000000 00:00 0\n",
"7f0020000000-7f0020200000 rw-p 00000000 00:00 0\n",
"555500000000-555500100000 rw-p 00000000 00:00 0 [heap]\n",
);
assert_eq!(arena_mappings_from_maps(maps), 1);
}
}