diff --git a/docs/guides/metrics.mdx b/docs/guides/metrics.mdx index cb5df8c70..de01db04a 100644 --- a/docs/guides/metrics.mdx +++ b/docs/guides/metrics.mdx @@ -82,6 +82,7 @@ Names below are as they appear in Prometheus. OTLP uses the dotted OpenTelemetry | `continuwuity_database_block_cache_bytes` | gauge | `column` | RocksDB block cache usage | | `continuwuity_cache_entries` | gauge | `name` | Entries in the auth chain and state info caches | | `continuwuity_jemalloc_memory_bytes` | gauge | `kind` | jemalloc statistics, only with the `jemalloc_stats` feature | +| `continuwuity_mimalloc_memory_bytes` | gauge | `kind` | mimalloc statistics, only with the `mimalloc` feature | | `process_uptime_seconds` | gauge | | Time since the server started | | `process_cpu_time_seconds_total` | counter | `cpu_mode` | CPU time used by the process | | `process_memory_usage_bytes` | gauge | | Resident memory (Linux) | diff --git a/src/admin/debug/commands.rs b/src/admin/debug/commands.rs index 3109730cb..8846ffbaa 100644 --- a/src/admin/debug/commands.rs +++ b/src/admin/debug/commands.rs @@ -1007,7 +1007,7 @@ impl crate::Context<'_> { pub(super) async fn memory_stats(&self, opts: Option) -> Result { const OPTS: &str = "abcdefghijklmnopqrstuvwxyz"; - let opts: String = OPTS + let mut mapped_opts: String = OPTS .chars() .filter(|&c| { let allow_any = opts.as_ref().is_some_and(|opts| opts == "*"); @@ -1018,7 +1018,11 @@ impl crate::Context<'_> { }) .collect(); - let stats = conduwuit::alloc::memory_stats(&opts).unwrap_or_default(); + if opts.as_ref().is_some_and(|opts| opts.contains('J')) { + mapped_opts.push('J'); + } + + let stats = conduwuit::alloc::memory_stats(&mapped_opts).unwrap_or_default(); self.write_str("```\n").await?; self.write_str(&stats).await?; diff --git a/src/admin/debug/mod.rs b/src/admin/debug/mod.rs index 9bf3647b3..85a1c3a77 100644 --- a/src/admin/debug/mod.rs +++ b/src/admin/debug/mod.rs @@ -192,7 +192,8 @@ pub enum DebugCommand { /// /// Optional argument is a character mask (a sequence of characters in any /// order) which enable additional extended statistics. Known characters are - /// "abdeglmx". For convenience, a '*' will enable everything. + /// "abdeglmx" and 'J' for JSON output. For convenience, a '*' will enable + /// everything. MemoryStats { opts: Option, }, diff --git a/src/core/alloc/default.rs b/src/core/alloc/default.rs index 7d6f94e34..23d56e5b2 100644 --- a/src/core/alloc/default.rs +++ b/src/core/alloc/default.rs @@ -1,5 +1,9 @@ //! Default allocator with no special features +use arrayvec::ArrayVec; + +pub(crate) fn metric() -> Option<(&'static str, &'static str)> { None } + /// Always returns Ok pub fn trim>>(_: I) -> crate::Result { Ok(()) } @@ -12,4 +16,4 @@ pub fn memory_stats(_opts: &str) -> Option { None } pub fn memory_usage() -> Option { None } #[must_use] -pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None } +pub fn memory_usage_stats() -> Option> { None } diff --git a/src/core/alloc/je.rs b/src/core/alloc/je.rs index cad356f8e..dc92c1a1e 100644 --- a/src/core/alloc/je.rs +++ b/src/core/alloc/je.rs @@ -103,22 +103,30 @@ fn _static_initialization() { } } +pub(crate) fn metric() -> Option<(&'static str, &'static str)> { + Some(( + "continuwuity.jemalloc.memory", + "jemalloc memory statistics, when built with jemalloc_stats", + )) +} + #[must_use] #[cfg(feature = "jemalloc_stats")] pub fn memory_usage() -> Option { + use std::fmt::Write; + let mibs = |input: usize| { let whole = u32::try_from(input / (1024 * 1024)).unwrap_or(u32::MAX); let rem_kibs = u32::try_from((input / 1024) % 1024).unwrap_or_default(); f64::from(whole) + f64::from(rem_kibs) / 1024.0 }; - let [allocated, active, mapped, metadata, resident, retained] = - memory_usage_stats()?.map(|(_, bytes)| mibs(bytes)); - Some(format!( - "allocated: {allocated:.2} MiB\nactive: {active:.2} MiB\nmapped: {mapped:.2} \ - MiB\nmetadata: {metadata:.2} MiB\nresident: {resident:.2} MiB\nretained: {retained:.2} \ - MiB\n" - )) + let mut out = String::new(); + for (name, bytes) in memory_usage_stats()? { + writeln!(out, "{name}: {:.2} MiB", mibs(bytes)).ok()?; + } + + Some(out) } #[must_use] @@ -127,25 +135,25 @@ pub fn memory_usage() -> Option { None } #[must_use] #[cfg(feature = "jemalloc_stats")] -pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { +pub fn memory_usage_stats() -> Option> { use mallctl::stats; // Advance the epoch; ensure latest stats are pulled in inc_epoch().ok()?; - Some([ + Some(ArrayVec::from([ ("allocated", stats::allocated::read().unwrap_or_default()), ("active", stats::active::read().unwrap_or_default()), ("mapped", stats::mapped::read().unwrap_or_default()), ("metadata", stats::metadata::read().unwrap_or_default()), ("resident", stats::resident::read().unwrap_or_default()), ("retained", stats::retained::read().unwrap_or_default()), - ]) + ])) } #[must_use] #[cfg(not(feature = "jemalloc_stats"))] -pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None } +pub fn memory_usage_stats() -> Option> { None } struct StatsPrintState { buf: String, diff --git a/src/core/alloc/mimalloc.rs b/src/core/alloc/mimalloc.rs index c12c0f843..495fc0879 100644 --- a/src/core/alloc/mimalloc.rs +++ b/src/core/alloc/mimalloc.rs @@ -1,68 +1,165 @@ //! mimalloc allocator -use std::ptr; +use std::{ + any::Any, + ffi::{CStr, c_void}, + panic::{self, AssertUnwindSafe}, + ptr, +}; +use arrayvec::ArrayVec; use libmimalloc_sys as ffi; use mimalloc::MiMalloc; +use serde::Deserialize; #[global_allocator] static MIMALLOC: MiMalloc = MiMalloc; +#[derive(Deserialize)] +struct StatCount { + current: i64, + peak: i64, +} + +#[derive(Deserialize)] +struct Stats { + reserved: StatCount, + committed: StatCount, + purged: i64, + reset: i64, +} + +trait OutputFnChar { + type Char; +} + +impl OutputFnChar for Option { + type Char = C; +} + +type MiChar = ::Char; + +struct StatsPrintState { + buf: String, + max_len: usize, + panic: Option>, +} + +pub(crate) fn metric() -> Option<(&'static str, &'static str)> { + Some(("continuwuity.mimalloc.memory", "mimalloc memory statistics")) +} + pub fn trim>>(_: I) -> crate::Result { // SAFETY: `mi_collect` is thread-safe and takes a single boolean flag. - unsafe { - ffi::mi_collect(true); - } + unsafe { ffi::mi_collect(true) }; Ok(()) } +fn read_stats() -> Option { + let stats = MiMalloc::stats_json().ok()?; + let json = stats.to_str().ok()?; + serde_json::from_str(json).ok() +} + +fn non_neg_usize(value: i64) -> usize { usize::try_from(value.max(0)).unwrap_or(usize::MAX) } + +fn mibs(input: usize) -> f64 { + let whole = u32::try_from(input / (1024 * 1024)).unwrap_or(u32::MAX); + let rem_kibs = u32::try_from((input / 1024) % 1024).unwrap_or_default(); + f64::from(whole) + f64::from(rem_kibs) / 1024.0 +} + #[must_use] pub fn memory_usage() -> Option { - let mut current_rss = 0; - let mut peak_rss = 0; - let mut current_commit = 0; - let mut peak_commit = 0; - - // SAFETY: All non-null out-pointers are valid writable `usize` stack slots; - // null pointers are explicitly permitted by `mi_process_info`. - unsafe { - ffi::mi_process_info( - ptr::null_mut(), - ptr::null_mut(), - ptr::null_mut(), - &raw mut current_rss, - &raw mut peak_rss, - &raw mut current_commit, - &raw mut peak_commit, - ptr::null_mut(), - ); - } - - let mibs = |input: usize| { - let whole = u32::try_from(input / (1024 * 1024)).unwrap_or(u32::MAX); - let rem_kibs = u32::try_from((input / 1024) % 1024).unwrap_or_default(); - f64::from(whole) + f64::from(rem_kibs) / 1024.0 - }; + let stats = read_stats()?; Some(format!( - "current_rss: {:.2} MiB\npeak_rss: {:.2} MiB\ncurrent_commit: {:.2} MiB\npeak_commit: \ - {:.2} MiB\n", - mibs(current_rss), - mibs(peak_rss), - mibs(current_commit), - mibs(peak_commit), + "reserved: {:.2} MiB\nreserved_peak: {:.2} MiB\ncommitted: {:.2} MiB\ncommitted_peak: \ + {:.2} MiB\npurged_total: {:.2} MiB\nreset_total: {:.2} MiB\n", + mibs(non_neg_usize(stats.reserved.current)), + mibs(non_neg_usize(stats.reserved.peak)), + mibs(non_neg_usize(stats.committed.current)), + mibs(non_neg_usize(stats.committed.peak)), + mibs(non_neg_usize(stats.purged)), + mibs(non_neg_usize(stats.reset)), )) } #[must_use] -pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None } +pub fn memory_usage_stats() -> Option> { + let stats = read_stats()?; + let mut out = ArrayVec::new(); + out.extend([ + ("reserved", non_neg_usize(stats.reserved.current)), + ("reserved_peak", non_neg_usize(stats.reserved.peak)), + ("committed", non_neg_usize(stats.committed.current)), + ("committed_peak", non_neg_usize(stats.committed.peak)), + ]); + + Some(out) +} #[must_use] -pub fn memory_stats(_opts: &str) -> Option { - MiMalloc::stats_json() - .ok() - .and_then(|stats| stats.to_str().ok().map(str::to_owned)) +pub fn memory_stats(opts: &str) -> Option { + const MAX_LENGTH: usize = 1_048_576; + + if opts.contains('\0') { + return None; + } + + if opts.contains('J') { + let stats = MiMalloc::stats_json().ok()?; + let json = String::from_utf8_lossy(stats.to_bytes()); + let end = json.floor_char_boundary(json.len().min(MAX_LENGTH)); + return json.get(..end).map(str::to_owned); + } + + let mut state = StatsPrintState { + buf: String::new(), + max_len: MAX_LENGTH, + panic: None, + }; + let opaque = ptr::from_mut(&mut state).cast::(); + + // SAFETY: `state` remains live in this frame for the duration of + // `mi_stats_print_out()`, which invokes `mi_stats_cb` synchronously. + unsafe { ffi::mi_stats_print_out(Some(mi_stats_cb), opaque) }; + + if let Some(payload) = state.panic { + panic::resume_unwind(payload); + } + + Some(state.buf) +} + +unsafe extern "C" fn mi_stats_cb(msg: *const MiChar, arg: *mut c_void) { + // SAFETY: `arg` points to `StatsPrintState` on the caller's stack. + let Some(state) = (unsafe { arg.cast::().as_mut() }) else { + return; + }; + + if state.panic.is_some() || msg.is_null() || state.buf.len() >= state.max_len { + return; + } + + // SAFETY: mimalloc guarantees `msg` is a valid NUL-terminated C string. + let msg = unsafe { CStr::from_ptr(msg.cast()) }; + + if let Err(err) = panic::catch_unwind(AssertUnwindSafe(|| { + let msg = String::from_utf8_lossy(msg.to_bytes()); + let remaining = state.max_len.saturating_sub(state.buf.len()); + if msg.len() <= remaining { + state.buf.push_str(msg.as_ref()); + } else { + if let Some(prefix) = msg.get(..msg.floor_char_boundary(remaining)) { + state.buf.push_str(prefix); + } + state.max_len = state.buf.len(); + } + })) { + state.panic = Some(err); + } } #[cfg(test)] @@ -73,15 +170,65 @@ mod tests { fn trim_succeeds() { trim(None).expect("trim(None)"); } #[test] - fn memory_usage_returns_rss_and_commit() { - let usage = memory_usage().expect("memory_usage"); - assert!(usage.contains("current_rss:"), "unexpected usage: {usage}"); - assert!(usage.contains("current_commit:"), "unexpected usage: {usage}"); + fn non_neg_usize_clamps_negative_values() { + assert_eq!(non_neg_usize(-1), 0, "expected -1 to clamp to 0"); + assert_eq!(non_neg_usize(i64::MIN), 0, "expected i64::MIN to clamp to 0"); + assert_eq!(non_neg_usize(0), 0, "expected 0 to remain 0"); + assert_eq!(non_neg_usize(4096), 4096, "expected positive value unchanged"); } #[test] - fn memory_stats_returns_json() { - let stats = memory_stats("").expect("memory_stats"); - assert!(stats.starts_with('{'), "expected JSON object, got: {stats}"); + fn memory_usage_stats_reports_allocator_bytes() { + let buf = std::hint::black_box(vec![0_u8; 64 * 1024]); + let stats = memory_usage_stats().expect("memory_usage_stats"); + drop(buf); + + let keys: Vec<_> = stats.iter().map(|&(key, _)| key).collect(); + assert_eq!( + keys, + ["reserved", "reserved_peak", "committed", "committed_peak"], + "unexpected stat keys" + ); + + for (key, bytes) in stats { + assert!(bytes > 0, "expected {key} > 0, got {bytes}"); + } + } + + #[test] + fn memory_usage_returns_allocator_summary() { + let usage = memory_usage().expect("memory_usage"); + for expected in [ + "reserved:", + "reserved_peak:", + "committed:", + "committed_peak:", + "purged_total:", + "reset_total:", + "MiB", + ] { + assert!(usage.contains(expected), "missing {expected:?} in usage:\n{usage}"); + } + assert!(!usage.contains("current_rss:"), "unexpected current_rss in usage:\n{usage}"); + assert!(!usage.contains("peak_rss:"), "unexpected peak_rss in usage:\n{usage}"); + } + + #[test] + fn memory_stats_supports_text_json_and_rejects_nul() { + assert!(memory_stats("invalid\0opts").is_none(), "expected NUL opts to return None"); + + for opts in ["", "abcdefghijklmnopqrstuvwxyz"] { + let stats = memory_stats(opts).expect("memory_stats text"); + assert!(!stats.is_empty(), "expected non-empty stats for opts={opts:?}"); + assert!(!stats.starts_with('{'), "expected formatted text, got JSON:\n{stats}"); + assert!(stats.contains("reserved"), "missing reserved in stats:\n{stats}"); + assert!(stats.contains("committed"), "missing committed in stats:\n{stats}"); + } + + let json = memory_stats("J").expect("memory_stats JSON"); + assert!(json.starts_with('{'), "expected JSON object, got:\n{json}"); + let parsed: serde_json::Value = + serde_json::from_str(&json).expect("expected valid JSON from memory_stats(\"J\")"); + assert!(parsed.get("committed").is_some(), "expected committed field in JSON"); } } diff --git a/src/core/alloc/mod.rs b/src/core/alloc/mod.rs index fe3441b74..3acfb43a0 100644 --- a/src/core/alloc/mod.rs +++ b/src/core/alloc/mod.rs @@ -4,16 +4,22 @@ #[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] pub mod je; #[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] +pub(crate) use je::metric; +#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] pub use je::{memory_stats, memory_usage, memory_usage_stats, trim}; // mimalloc #[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))] pub mod mimalloc; #[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))] +pub(crate) use mimalloc::metric; +#[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))] pub use mimalloc::{memory_stats, memory_usage, memory_usage_stats, trim}; // default #[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))] pub mod default; #[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))] +pub(crate) use default::metric; +#[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))] pub use default::{memory_stats, memory_usage, memory_usage_stats, trim}; diff --git a/src/core/metrics/instruments.rs b/src/core/metrics/instruments.rs index b9ba973b2..10bff549a 100644 --- a/src/core/metrics/instruments.rs +++ b/src/core/metrics/instruments.rs @@ -196,9 +196,13 @@ fn register_process(meter: &Meter) { } fn register_allocator(meter: &Meter) { + let Some((name, description)) = crate::alloc::metric() else { + return; + }; + meter - .u64_observable_gauge("continuwuity.jemalloc.memory") - .with_description("jemalloc memory statistics, when built with jemalloc_stats") + .u64_observable_gauge(name) + .with_description(description) .with_unit("By") .with_callback(|observer| { for (kind, bytes) in crate::alloc::memory_usage_stats().into_iter().flatten() { diff --git a/src/core/metrics/tests.rs b/src/core/metrics/tests.rs index fdbe52c9d..3f13d8bd5 100644 --- a/src/core/metrics/tests.rs +++ b/src/core/metrics/tests.rs @@ -38,3 +38,23 @@ async fn renders_prometheus_names() { } assert!(!body.contains("otel_scope_name")); } + +#[tokio::test] +#[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))] +async fn renders_mimalloc_memory_metric() { + let (metrics, _provider) = metrics(); + let body = metrics.render().unwrap(); + + for expected in [ + "continuwuity_mimalloc_memory_bytes{kind=\"reserved\"}", + "continuwuity_mimalloc_memory_bytes{kind=\"reserved_peak\"}", + "continuwuity_mimalloc_memory_bytes{kind=\"committed\"}", + "continuwuity_mimalloc_memory_bytes{kind=\"committed_peak\"}", + ] { + assert!(body.contains(expected), "missing {expected:?} in:\n{body}"); + } + assert!( + !body.contains("continuwuity_jemalloc_memory_bytes"), + "unexpected jemalloc metric in mimalloc build:\n{body}" + ); +}