diff --git a/Cargo.lock b/Cargo.lock index ed7d97d9..1f8535f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1328,6 +1328,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "memory-stats" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34f79cf9964c5c9545493acda1263f1912f8d2c56c8a2ffee2606cb960acaacc" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "mime" version = "0.3.17" @@ -1568,6 +1578,7 @@ dependencies = [ "humantime", "humantime-serde", "log", + "memory-stats", "mime_guess", "num_cpus", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 4d2dbef9..497805f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ http = "1.1.0" humantime = "2.1.0" humantime-serde = "1.1.1" log = "0.4.21" +memory-stats = { version = "1.1.0", features = ["always_use_statm"] } mime_guess = "2.0.4" num_cpus = "1.16.0" once_cell = "1.19.0" diff --git a/TODO.md b/TODO.md index 8183e339..163a07e7 100644 --- a/TODO.md +++ b/TODO.md @@ -23,3 +23,4 @@ - [x] add remark for config - [ ] support multi host for location? - [ ] support set upstream_keepalive_pool_size +- [ ] graceful restart for admin web diff --git a/src/proxy/server.rs b/src/proxy/server.rs index d1748808..48d5a12d 100644 --- a/src/proxy/server.rs +++ b/src/proxy/server.rs @@ -9,8 +9,10 @@ use crate::utils; use async_trait::async_trait; use base64::{engine::general_purpose::STANDARD, Engine}; use bytes::Bytes; +use bytesize::ByteSize; use http::StatusCode; use log::{error, info}; +use memory_stats::memory_stats; use once_cell::sync::Lazy; use pingora::http::ResponseHeader; use pingora::listeners::TlsSettings; @@ -167,6 +169,8 @@ struct ServerStats { processing: i32, accepted: u64, hostname: String, + physical_mem_mb: usize, + physical_mem: String, } pub struct ServerServices { @@ -269,10 +273,17 @@ impl Server { Ok(ServerServices { lb, bg_services }) } async fn send_stats_response(&self, session: &mut Session, ctx: &mut State) { + let mut physical_mem = 0; + if let Some(value) = memory_stats() { + physical_mem = value.physical_mem; + } + let buf = serde_json::to_vec(&ServerStats { accepted: self.accepted.load(Ordering::Relaxed), processing: self.processing.load(Ordering::Relaxed), hostname: HOST_NAME.to_string(), + physical_mem: ByteSize(physical_mem as u64).to_string_as(true), + physical_mem_mb: physical_mem / (1024 * 1024), }) .unwrap_or_default();