Skip to content

Commit 5bc3636

Browse files
hcschmkroening
authored andcommitted
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.
1 parent 70cc57c commit 5bc3636

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ harness = false
4747
[features]
4848
default = ["kernel-stack", "pci", "pci-ids", "acpi", "fsgsbase", "smp", "tcp", "dhcpv4", "fuse", "virtio-net", "vsock"]
4949
acpi = []
50+
alloc-stats = ["talc/counters"]
5051
common-os = []
5152
console = ["virtio"]
5253
dhcpv4 = ["net", "smoltcp", "smoltcp/proto-dhcpv4", "smoltcp/socket-dhcpv4"]

src/executor/alloc_stats.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use core::future;
2+
use core::task::Poll;
3+
4+
use crate::executor::spawn;
5+
use crate::mm::ALLOCATOR;
6+
7+
async fn print_alloc_stats() {
8+
future::poll_fn(|cx| {
9+
let talc = ALLOCATOR.lock();
10+
11+
debug!("<alloc-stats>\n{}", talc.get_counters());
12+
13+
cx.waker().wake_by_ref();
14+
Poll::<()>::Pending
15+
})
16+
.await;
17+
}
18+
19+
pub(crate) fn init() {
20+
info!("Spawning allocation stats printing task");
21+
spawn(print_alloc_stats());
22+
}

src/executor/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(feature = "alloc-stats")]
2+
mod alloc_stats;
13
#[cfg(feature = "net")]
24
pub(crate) mod device;
35
#[cfg(feature = "net")]
@@ -108,7 +110,12 @@ pub(crate) fn run() {
108110

109111
/// Spawns a future on the executor.
110112
#[cfg_attr(
111-
not(any(feature = "shell", feature = "net", feature = "vsock")),
113+
not(any(
114+
feature = "alloc-stats",
115+
feature = "shell",
116+
feature = "net",
117+
feature = "vsock"
118+
)),
112119
expect(dead_code)
113120
)]
114121
pub(crate) fn spawn<F>(future: F)
@@ -123,6 +130,8 @@ pub fn init() {
123130
crate::executor::network::init();
124131
#[cfg(feature = "vsock")]
125132
crate::executor::vsock::init();
133+
#[cfg(feature = "alloc-stats")]
134+
crate::executor::alloc_stats::init();
126135
}
127136

128137
/// Blocks the current thread on `f`, running the executor when idling.

0 commit comments

Comments
 (0)