From 5bc36361a82bec03002b659452cad4f93f177876 Mon Sep 17 00:00:00 2001 From: Hans Christian Schmitz Date: Sat, 18 Oct 2025 22:40:52 +0200 Subject: [PATCH] feat: support regularly printing allocation stats Talc can optionally collect and expose some basic statistics about allocations. Provide a feature that regularly prints these statistics via another executor tassk. --- Cargo.toml | 1 + src/executor/alloc_stats.rs | 22 ++++++++++++++++++++++ src/executor/mod.rs | 11 ++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/executor/alloc_stats.rs diff --git a/Cargo.toml b/Cargo.toml index f9005fcda3..fce23f3903 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ harness = false [features] default = ["kernel-stack", "pci", "pci-ids", "acpi", "fsgsbase", "smp", "tcp", "dhcpv4", "fuse", "virtio-net", "vsock"] acpi = [] +alloc-stats = ["talc/counters"] common-os = [] console = ["virtio"] dhcpv4 = ["net", "smoltcp", "smoltcp/proto-dhcpv4", "smoltcp/socket-dhcpv4"] diff --git a/src/executor/alloc_stats.rs b/src/executor/alloc_stats.rs new file mode 100644 index 0000000000..676ba71975 --- /dev/null +++ b/src/executor/alloc_stats.rs @@ -0,0 +1,22 @@ +use core::future; +use core::task::Poll; + +use crate::executor::spawn; +use crate::mm::ALLOCATOR; + +async fn print_alloc_stats() { + future::poll_fn(|cx| { + let talc = ALLOCATOR.lock(); + + debug!("\n{}", talc.get_counters()); + + cx.waker().wake_by_ref(); + Poll::<()>::Pending + }) + .await; +} + +pub(crate) fn init() { + info!("Spawning allocation stats printing task"); + spawn(print_alloc_stats()); +} diff --git a/src/executor/mod.rs b/src/executor/mod.rs index 1451b662e1..cc65b85aa6 100644 --- a/src/executor/mod.rs +++ b/src/executor/mod.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "alloc-stats")] +mod alloc_stats; #[cfg(feature = "net")] pub(crate) mod device; #[cfg(feature = "net")] @@ -108,7 +110,12 @@ pub(crate) fn run() { /// Spawns a future on the executor. #[cfg_attr( - not(any(feature = "shell", feature = "net", feature = "vsock")), + not(any( + feature = "alloc-stats", + feature = "shell", + feature = "net", + feature = "vsock" + )), expect(dead_code) )] pub(crate) fn spawn(future: F) @@ -123,6 +130,8 @@ pub fn init() { crate::executor::network::init(); #[cfg(feature = "vsock")] crate::executor::vsock::init(); + #[cfg(feature = "alloc-stats")] + crate::executor::alloc_stats::init(); } /// Blocks the current thread on `f`, running the executor when idling.