mirror of
https://github.com/rustfs/rustfs.git
synced 2026-11-16 04:52:36 +00:00
feat(rustfs): isolate allocator live diagnostics
Gate Rust GlobalAlloc live-byte accounting behind a non-default Cargo feature, independent of tokio_unstable. Document its scope and measurement overhead. Co-Authored-By: heihutu <heihutu@gmail.com> Co-Authored-By: zhi22915 <qiuzgang@gmail.com>
This commit is contained in:
@@ -31,6 +31,8 @@ A previously published gauge can remain visible after a statistic becomes unavai
|
||||
|
||||
When live requested bytes are unavailable, capture a supported heap profile or allocation/free trace. Preserve attribution to allocation stacks and lifetime; committed bytes and RSS alone cannot establish a leak. Other raw allocator statistics, including page accounting, depend on the allocator build and are not interchangeable with live application bytes.
|
||||
|
||||
For Linux diagnostics, build with `--features diagnostic-allocator-live` to export `rustfs_memory_allocator_rust_global_requested_live_bytes` and its availability gauge. Use the byte gauge only when `rustfs_memory_allocator_rust_global_requested_live_bytes_available` is `1`; it remains unavailable on Windows and with `hotpath-alloc`. The feature wraps MiMalloc and updates an atomic counter for successful Rust `GlobalAlloc` allocations (including zeroed allocations), reallocations, and deallocations. It excludes direct native allocator calls and adds per-allocation overhead, so use it to observe memory behavior, not to compare throughput. Enabling `--cfg tokio_unstable` for Tokio blocking-pool metrics does not enable this allocator wrapper; enable both explicitly when both measurements are needed.
|
||||
|
||||
### Idle reclaim
|
||||
|
||||
The reclaim loop waits for request, delete-tail, scanner, heal, EC and GET-buffer activity to become idle. Continuous traffic can skip reclaim. Check the activity gauges, skipped reasons and idle streak rather than assuming that waiting a fixed period guarantees collection.
|
||||
|
||||
@@ -77,6 +77,8 @@ rio-v2 = ["rustfs-ecstore/rio-v2"]
|
||||
pyroscope = ["rustfs-obs/pyroscope", "dep:pyroscope", "dep:object", "dep:symbolic-demangle"]
|
||||
# Tokio runtime telemetry. Requires `--cfg tokio_unstable`; use `make build-profiling`.
|
||||
dial9 = ["rustfs-obs/dial9"]
|
||||
# Diagnostic-only Rust GlobalAlloc live-byte accounting; adds per-allocation atomic updates.
|
||||
diagnostic-allocator-live = []
|
||||
http3 = ["dep:h3", "dep:h3-quinn", "dep:quinn"]
|
||||
hotpath = [
|
||||
"hotpath/hotpath",
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
fn main() -> shadow_rs::SdResult<()> {
|
||||
println!("cargo:rustc-check-cfg=cfg(tokio_unstable)");
|
||||
println!("cargo:rerun-if-env-changed=RUSTFS_BUILD_VERSION");
|
||||
if let Ok(version) = std::env::var("RUSTFS_BUILD_VERSION")
|
||||
&& !version.is_empty()
|
||||
|
||||
+83
-3
@@ -12,9 +12,23 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[cfg(all(feature = "hotpath", feature = "hotpath-alloc", not(target_os = "windows")))]
|
||||
#[cfg(any(
|
||||
all(feature = "hotpath", feature = "hotpath-alloc", not(target_os = "windows")),
|
||||
all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
)
|
||||
))]
|
||||
use std::alloc::{GlobalAlloc, Layout};
|
||||
#[cfg(all(feature = "hotpath", feature = "hotpath-alloc", not(target_os = "windows")))]
|
||||
#[cfg(any(
|
||||
all(feature = "hotpath", feature = "hotpath-alloc", not(target_os = "windows")),
|
||||
all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
)
|
||||
))]
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[cfg(all(feature = "hotpath", feature = "hotpath-alloc", not(target_os = "windows")))]
|
||||
@@ -49,6 +63,60 @@ unsafe impl GlobalAlloc for MiMallocAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
#[derive(Default)]
|
||||
struct DiagnosticMiMalloc;
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
// SAFETY: allocation operations are forwarded to MiMalloc with the matching
|
||||
// GlobalAlloc layout contract. The diagnostic counter is updated only after
|
||||
// successful allocation changes and does not allocate memory itself.
|
||||
#[allow(unsafe_code)]
|
||||
unsafe impl GlobalAlloc for DiagnosticMiMalloc {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
// SAFETY: the caller upholds GlobalAlloc's contract for layout.
|
||||
let ptr = unsafe { rustfs_mimalloc::MiMalloc.alloc(layout) };
|
||||
if !ptr.is_null() {
|
||||
rustfs::memory_observability::record_diagnostic_global_allocator_resize(0, layout.size());
|
||||
}
|
||||
ptr
|
||||
}
|
||||
|
||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||
// SAFETY: the caller upholds GlobalAlloc's contract for layout.
|
||||
let ptr = unsafe { rustfs_mimalloc::MiMalloc.alloc_zeroed(layout) };
|
||||
if !ptr.is_null() {
|
||||
rustfs::memory_observability::record_diagnostic_global_allocator_resize(0, layout.size());
|
||||
}
|
||||
ptr
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
// SAFETY: ptr came from this allocator, is non-null by GlobalAlloc's
|
||||
// dealloc contract, and layout.size() is the original allocation size.
|
||||
let ptr = unsafe { NonNull::new_unchecked(ptr) };
|
||||
unsafe { rustfs_mimalloc::MiMalloc::free_csize_nonnull(ptr, layout.size()) };
|
||||
rustfs::memory_observability::record_diagnostic_global_allocator_resize(layout.size(), 0);
|
||||
}
|
||||
|
||||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||
// SAFETY: ptr and layout came from this allocator and are forwarded unchanged.
|
||||
let new_ptr = unsafe { rustfs_mimalloc::MiMalloc.realloc(ptr, layout, new_size) };
|
||||
if !new_ptr.is_null() {
|
||||
rustfs::memory_observability::record_diagnostic_global_allocator_resize(layout.size(), new_size);
|
||||
}
|
||||
new_ptr
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "hotpath", feature = "hotpath-alloc", not(target_os = "windows")))]
|
||||
#[global_allocator]
|
||||
static GLOBAL: hotpath::CountingAllocator<MiMallocAllocator> = hotpath::CountingAllocator::with(MiMallocAllocator);
|
||||
@@ -57,10 +125,22 @@ static GLOBAL: hotpath::CountingAllocator<MiMallocAllocator> = hotpath::Counting
|
||||
#[global_allocator]
|
||||
static GLOBAL: hotpath::CountingAllocator<std::alloc::System> = hotpath::CountingAllocator::with(std::alloc::System);
|
||||
|
||||
#[cfg(all(not(all(feature = "hotpath", feature = "hotpath-alloc")), not(target_os = "windows")))]
|
||||
#[cfg(all(
|
||||
not(feature = "diagnostic-allocator-live"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc")),
|
||||
not(target_os = "windows")
|
||||
))]
|
||||
#[global_allocator]
|
||||
static GLOBAL: rustfs_mimalloc::MiMalloc = rustfs_mimalloc::MiMalloc;
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
#[global_allocator]
|
||||
static GLOBAL: DiagnosticMiMalloc = DiagnosticMiMalloc;
|
||||
|
||||
fn main() {
|
||||
let _hotpath_guard = hotpath::HotpathGuardBuilder::new("main").build();
|
||||
|
||||
|
||||
@@ -20,6 +20,12 @@ use serde::Serialize;
|
||||
#[cfg(any(not(target_os = "windows"), test))]
|
||||
use serde_json::Value;
|
||||
use std::path::Path;
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
@@ -35,6 +41,38 @@ const CGROUP_V1_MEMORY_STAT_PATH: &str = "/sys/fs/cgroup/memory/memory.stat";
|
||||
const CGROUP_V1_MEMORY_USAGE_PATH: &str = "/sys/fs/cgroup/memory/memory.usage_in_bytes";
|
||||
const CGROUP_V1_MEMORY_LIMIT_PATH: &str = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
static DIAGNOSTIC_GLOBAL_ALLOCATOR_LIVE_REQUESTED_BYTES: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
fn adjust_diagnostic_global_allocator_live_bytes(counter: &AtomicUsize, old_size: usize, new_size: usize) {
|
||||
if new_size > old_size {
|
||||
counter.fetch_add(new_size - old_size, Ordering::Relaxed);
|
||||
} else if old_size > new_size {
|
||||
counter.fetch_sub(old_size - new_size, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the diagnostic-only live requested-byte counter for Rust allocations
|
||||
/// routed through the binary's `GlobalAlloc` wrapper.
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
#[doc(hidden)]
|
||||
pub fn record_diagnostic_global_allocator_resize(old_size: usize, new_size: usize) {
|
||||
adjust_diagnostic_global_allocator_live_bytes(&DIAGNOSTIC_GLOBAL_ALLOCATOR_LIVE_REQUESTED_BYTES, old_size, new_size);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum MemoryObservabilityServiceState {
|
||||
@@ -397,7 +435,48 @@ fn allocator_snapshot_or_unavailable(snapshot: Option<AllocatorMemorySnapshot>)
|
||||
})
|
||||
}
|
||||
|
||||
/// Record the serving Tokio runtime's worker and blocking-pool thread counts.
|
||||
/// Blocking-pool counts require a diagnostic build with `tokio_unstable`.
|
||||
fn record_tokio_runtime_metrics() {
|
||||
let runtime = tokio::runtime::Handle::current().metrics();
|
||||
metrics::gauge!("rustfs_runtime_worker_threads").set(runtime.num_workers() as f64);
|
||||
|
||||
#[cfg(tokio_unstable)]
|
||||
{
|
||||
let total = runtime.num_blocking_threads();
|
||||
let idle = runtime.num_idle_blocking_threads().min(total);
|
||||
metrics::gauge!("rustfs_runtime_blocking_pool_metrics_available").set(1.0);
|
||||
metrics::gauge!("rustfs_runtime_blocking_threads_total").set(total as f64);
|
||||
metrics::gauge!("rustfs_runtime_blocking_threads_idle").set(idle as f64);
|
||||
metrics::gauge!("rustfs_runtime_blocking_threads_active").set(total.saturating_sub(idle) as f64);
|
||||
}
|
||||
|
||||
#[cfg(not(tokio_unstable))]
|
||||
metrics::gauge!("rustfs_runtime_blocking_pool_metrics_available").set(0.0);
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
{
|
||||
metrics::gauge!("rustfs_memory_allocator_rust_global_requested_live_bytes_available").set(1.0);
|
||||
metrics::gauge!("rustfs_memory_allocator_rust_global_requested_live_bytes")
|
||||
.set(DIAGNOSTIC_GLOBAL_ALLOCATOR_LIVE_REQUESTED_BYTES.load(Ordering::Relaxed) as f64);
|
||||
}
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
)))]
|
||||
metrics::gauge!("rustfs_memory_allocator_rust_global_requested_live_bytes_available").set(0.0);
|
||||
}
|
||||
|
||||
async fn record_memory_snapshot(process_sampler: Arc<Mutex<ProcessSampler>>) {
|
||||
// Sample before this tick submits its own spawn_blocking snapshot task.
|
||||
record_tokio_runtime_metrics();
|
||||
|
||||
match tokio::task::spawn_blocking(move || {
|
||||
let mut sampler = process_sampler.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
let (resource, process) = sampler.snapshot_resource_and_system();
|
||||
@@ -464,6 +543,12 @@ pub fn init_memory_observability(ctx: CancellationToken) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
use super::adjust_diagnostic_global_allocator_live_bytes;
|
||||
use super::{
|
||||
CgroupMemorySnapshot, MEMORY_OBSERVABILITY_SERVICE_NAME, MemoryObservabilityCancellationSource,
|
||||
MemoryObservabilityController, MemoryObservabilityDesiredState, MemoryObservabilityServiceState,
|
||||
@@ -472,6 +557,34 @@ mod tests {
|
||||
};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
#[cfg(all(
|
||||
feature = "diagnostic-allocator-live",
|
||||
not(target_os = "windows"),
|
||||
not(all(feature = "hotpath", feature = "hotpath-alloc"))
|
||||
))]
|
||||
#[test]
|
||||
fn diagnostic_global_allocator_counter_tracks_alloc_realloc_and_free() {
|
||||
let counter = AtomicUsize::new(0);
|
||||
|
||||
adjust_diagnostic_global_allocator_live_bytes(&counter, 0, 32);
|
||||
assert_eq!(counter.load(std::sync::atomic::Ordering::Relaxed), 32);
|
||||
|
||||
adjust_diagnostic_global_allocator_live_bytes(&counter, 32, 64);
|
||||
assert_eq!(counter.load(std::sync::atomic::Ordering::Relaxed), 64);
|
||||
|
||||
adjust_diagnostic_global_allocator_live_bytes(&counter, 64, 8);
|
||||
assert_eq!(counter.load(std::sync::atomic::Ordering::Relaxed), 8);
|
||||
|
||||
adjust_diagnostic_global_allocator_live_bytes(&counter, 8, 0);
|
||||
assert_eq!(counter.load(std::sync::atomic::Ordering::Relaxed), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_cgroup_memory_stat_extracts_tracked_numeric_fields() {
|
||||
|
||||
Reference in New Issue
Block a user