diff --git a/Cargo.lock b/Cargo.lock index 9ed5b57d1..be03543ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3090,6 +3090,15 @@ dependencies = [ "utralib", ] +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "shellchat" version = "0.1.0" @@ -3133,6 +3142,9 @@ dependencies = [ "susres", "threadpool", "ticktimer-server", + "tracing", + "tracing-subscriber", + "tracking-allocator", "trng", "tts-frontend", "usb-device-xous", @@ -3444,6 +3456,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + [[package]] name = "threadpool" version = "1.8.1" @@ -3530,6 +3551,74 @@ dependencies = [ "xmas-elf", ] +[[package]] +name = "tracing" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160" +dependencies = [ + "cfg-if 1.0.0", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6b8ad3567499f98a1db7a752b07a7c8c7c7c34c332ec00effb2b0027974b7c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7709595b8878a4965ce5e87ebf880a7d39c9afc6837721b21a5a816a8117d921" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bc28f93baff38037f64e6f43d34cfa1605f27a49c34e8a04c5e78b0babf2596" +dependencies = [ + "ansi_term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "tracking-allocator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa201e9faf2265fff28d9be73e71bee5fc51fc9b46d1107e2874dec9c75b5e72" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "trng" version = "0.1.0" @@ -3754,6 +3843,12 @@ dependencies = [ "svd2utra", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vault" version = "0.1.0" diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 6807e4ef2..f90de4067 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -64,6 +64,9 @@ rand = {version = "0.8.5", features = ["getrandom"]} # for ditherpunk testing png = {version = "0.17.5", optional = true} +tracking-allocator = {version = "0.3.0", optional = true} +tracing = {version = "0.1.35", optional = true} +tracing-subscriber = {version = "0.3.11", optional = true} # for the curve25519 tests # hardware acceleration adaptations are inserted into a fork of the main branch. @@ -96,4 +99,5 @@ aestests = [] # adds AES tests tts = [] # adds text to speech plugin pddbtest = [] ditherpunk = ["png"] +tracking-alloc = ["tracking-allocator", "tracing", "tracing-subscriber"] default = [] # "debugprint" diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index c9a3dbd4f..972256cbd 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -289,6 +289,24 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } #[cfg(feature="ditherpunk")] "image" => { + #[cfg(feature="tracking-alloc")] + use tracking_allocator::{ + AllocationGroupToken, AllocationRegistry, + }; + #[cfg(feature="tracking-alloc")] + use crate::StdoutTracker; + #[cfg(feature="tracking-alloc")] + let mut local_token = { + let _ = AllocationRegistry::set_global_tracker(StdoutTracker{total: core::sync::atomic::AtomicIsize::new(0)}) + .expect("no other global tracker should be set yet"); + + AllocationRegistry::enable_tracking(); + AllocationGroupToken::register().expect("failed to register allocation group") + }; + + #[cfg(feature="tracking-alloc")] + let local_guard = local_token.enter(); + // use std::io::BufRead; // not implemented yet! if let Some(url) = tokens.next() { match url.split_once('/') { @@ -342,7 +360,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } else { if len < last_chunk { log::info!("heuristic termination of read loop"); - break; + // break; } } }, @@ -410,6 +428,10 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } else { write!(ret, "Usage: image bunniefoo.com/bunnie/bunny.png").unwrap(); } + #[cfg(feature="tracking-alloc")] + drop(local_guard); + #[cfg(feature="tracking-alloc")] + AllocationRegistry::disable_tracking(); } "tls" => { write!(ret, "Work in progress. Please check back later!").unwrap(); diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index 90f3753c8..8b5ecec4f 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -50,6 +50,54 @@ use locales::t; #[cfg(feature="tts")] use tts_frontend::*; +#[cfg(feature="tracking-alloc")] +use tracking_allocator::{ + AllocationGroupId, AllocationTracker, Allocator, +}; +#[cfg(feature="tracking-alloc")] +use std::alloc::System; +#[cfg(feature="tracking-alloc")] +#[global_allocator] +static GLOBAL: Allocator = Allocator::system(); +#[cfg(feature="tracking-alloc")] +use core::sync::atomic::{AtomicIsize, Ordering}; +#[cfg(feature="tracking-alloc")] +struct StdoutTracker { + pub total: AtomicIsize, +} + +#[cfg(feature="tracking-alloc")] +impl AllocationTracker for StdoutTracker { + fn allocated(&self, addr: usize, size: usize, group_id: AllocationGroupId) { + // Allocations have all the pertinent information upfront, which you may or may not want to store for further + // analysis. Notably, deallocations also know how large they are, and what group ID they came from, so you + // typically don't have to store much data for correlating deallocations with their original allocation. + self.total.store(self.total.load(Ordering::SeqCst) + size as isize, Ordering::SeqCst); + println!( + "allocation -> total={} addr=0x{:0x} size={} group_id={:?}", + self.total.load(Ordering::SeqCst), addr, size, group_id + ); + } + + fn deallocated( + &self, + addr: usize, + size: usize, + source_group_id: AllocationGroupId, + current_group_id: AllocationGroupId, + ) { + // When a deallocation occurs, as mentioned above, you have full access to the address, size of the allocation, + // as well as the group ID the allocation was made under _and_ the active allocation group ID. + // + // This can be useful beyond just the obvious "track how many current bytes are allocated by the group", instead + // going further to see the chain of where allocations end up, and so on. + self.total.store(self.total.load(Ordering::SeqCst) - size as isize, Ordering::SeqCst); + println!( + "deallocation -> total={} addr=0x{:0x} size={} source_group_id={:?} current_group_id={:?}", + self.total.load(Ordering::SeqCst), addr, size, source_group_id, current_group_id + ); + } +} #[derive(Debug)] struct History { // the history record diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d22d23e8d..d412ae5c7 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -415,6 +415,7 @@ fn main() -> Result<(), Box> { Some(&[ "--features", "pddbtest", "--features", "ditherpunk", + "--features", "tracking-alloc", ]), false)? } Some("hosted-ci") => {