diff --git a/src/admin/debug/commands.rs b/src/admin/debug/commands.rs index 62741f38..5fd0360d 100644 --- a/src/admin/debug/commands.rs +++ b/src/admin/debug/commands.rs @@ -632,12 +632,12 @@ pub(super) async fn resolve_true_destination( pub(super) fn memory_stats() -> RoomMessageEventContent { let html_body = conduit::alloc::memory_stats(); - if html_body.is_empty() { + if html_body.is_none() { return RoomMessageEventContent::text_plain("malloc stats are not supported on your compiled malloc."); } RoomMessageEventContent::text_html( "This command's output can only be viewed by clients that render HTML.".to_owned(), - html_body, + html_body.expect("string result"), ) } diff --git a/src/admin/server/commands.rs b/src/admin/server/commands.rs index 06007fb8..229f05e1 100644 --- a/src/admin/server/commands.rs +++ b/src/admin/server/commands.rs @@ -20,17 +20,12 @@ pub(super) async fn show_config(_body: Vec<&str>) -> Result) -> Result { - let response0 = services().memory_usage().await?; - let response1 = services().db.db.memory_usage()?; - let response2 = conduit::alloc::memory_usage(); + let services_usage = services().memory_usage().await?; + let database_usage = services().db.db.memory_usage()?; + let allocator_usage = conduit::alloc::memory_usage().map_or(String::new(), |s| format!("\nAllocator:\n{s}")); Ok(RoomMessageEventContent::text_plain(format!( - "Services:\n{response0}\nDatabase:\n{response1}\n{}", - if !response2.is_empty() { - format!("Allocator:\n {response2}") - } else { - String::new() - } + "Services:\n{services_usage}\nDatabase:\n{database_usage}{allocator_usage}", ))) } diff --git a/src/core/alloc/default.rs b/src/core/alloc/default.rs index 4e2f8d7e..83bfca7d 100644 --- a/src/core/alloc/default.rs +++ b/src/core/alloc/default.rs @@ -1,9 +1,9 @@ //! Default allocator with no special features -/// Always returns the empty string +/// Always returns None #[must_use] -pub fn memory_stats() -> String { String::default() } +pub fn memory_stats() -> Option { None } -/// Always returns the empty string +/// Always returns None #[must_use] -pub fn memory_usage() -> String { String::default() } +pub fn memory_usage() -> Option { None } diff --git a/src/core/alloc/hardened.rs b/src/core/alloc/hardened.rs index 6727407f..335a3307 100644 --- a/src/core/alloc/hardened.rs +++ b/src/core/alloc/hardened.rs @@ -4,9 +4,10 @@ static HMALLOC: hardened_malloc_rs::HardenedMalloc = hardened_malloc_rs::HardenedMalloc; #[must_use] -pub fn memory_usage() -> String { - String::default() //TODO: get usage -} +//TODO: get usage +pub fn memory_usage() -> Option { None } #[must_use] -pub fn memory_stats() -> String { "Extended statistics are not available from hardened_malloc.".to_owned() } +pub fn memory_stats() -> Option { + Some("Extended statistics are not available from hardened_malloc.".to_owned()) +} diff --git a/src/core/alloc/je.rs b/src/core/alloc/je.rs index 5e3c361f..08bfc49a 100644 --- a/src/core/alloc/je.rs +++ b/src/core/alloc/je.rs @@ -10,7 +10,7 @@ use tikv_jemallocator as jemalloc; static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc; #[must_use] -pub fn memory_usage() -> String { +pub fn memory_usage() -> Option { use mallctl::stats; let mibs = |input: Result| { @@ -27,14 +27,14 @@ pub fn memory_usage() -> String { let metadata = mibs(stats::metadata::read()); let resident = mibs(stats::resident::read()); let retained = mibs(stats::retained::read()); - format!( + 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" - ) + )) } #[must_use] -pub fn memory_stats() -> String { +pub fn memory_stats() -> Option { const MAX_LENGTH: usize = 65536 - 4096; let opts_s = "d"; @@ -51,7 +51,7 @@ pub fn memory_stats() -> String { unsafe { ffi::malloc_stats_print(Some(malloc_stats_cb), opaque, opts_p) }; str.truncate(MAX_LENGTH); - format!("
{str}
") + Some(format!("
{str}
")) } extern "C" fn malloc_stats_cb(opaque: *mut c_void, msg: *const c_char) {