From bf15ac08f40fed556f1f56207690d9f4b1d90468 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 15 Jul 2020 17:08:05 +0800 Subject: [PATCH] kernel: tests: add memory measurement to test suite This adds a feature to enable memory measurement, so we can determine if we're leaking memory anywhere. Signed-off-by: Sean Cross --- Cargo.lock | 7 ++ kernel/Cargo.toml | 2 + kernel/src/arch/hosted/mod.rs | 15 ++-- kernel/src/server.rs | 8 +- kernel/src/services.rs | 12 +-- kernel/src/syscall.rs | 37 ++++---- kernel/src/test/mod.rs | 154 +++++++++++++++++++++------------- 7 files changed, 135 insertions(+), 100 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53c80feec..357e0519c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,6 +297,7 @@ dependencies = [ "lazy_static", "riscv", "sha3", + "stats_alloc", "xous", ] @@ -545,6 +546,12 @@ dependencies = [ "xous", ] +[[package]] +name = "stats_alloc" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a260c96bf26273969f360c2fc2e2c7732acc2ce49d939c7243c7230c2ad179d0" + [[package]] name = "strsim" version = "0.8.0" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 6b79f55c9..576470cae 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -9,6 +9,7 @@ description = "Core kernel for Xous, including task switching and memory managem bitflags = "1.2.1" xous = { path = "../xous-rs", features = ["forget-memory-messages"] } debug-here = "0.2.2" +stats_alloc = { version = "0.1.8", optional = true } [target.'cfg(target_arch = "riscv32")'.dependencies] riscv = "0.5.6" @@ -25,3 +26,4 @@ lazy_static = "1.4" [features] debug-print = [] +report-memory = ["stats_alloc"] diff --git a/kernel/src/arch/hosted/mod.rs b/kernel/src/arch/hosted/mod.rs index ad76288fa..e86d4f9a7 100644 --- a/kernel/src/arch/hosted/mod.rs +++ b/kernel/src/arch/hosted/mod.rs @@ -54,10 +54,10 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) // If the connection has gone away, send a `TerminateProcess` message to the main // and then exit this thread. if e.kind() != std::io::ErrorKind::WouldBlock { - println!( - "KERNEL({}): Client disconnected: {} ({:?}). Shutting down virtual process.", - pid, e, e - ); + // println!( + // "KERNEL({}): Client disconnected: {} ({:?}). Shutting down virtual process.", + // pid, e, e + // ); chn.send(ThreadMessage::SysCall(pid, xous::SysCall::TerminateProcess)) .unwrap(); return; @@ -83,7 +83,7 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender) tmp_data.resize(msg.buf.len(), 0); conn.read_exact(&mut tmp_data) .map_err(|_e| { - println!("KERNEL({}): Read Error {}", pid, _e); + // println!("KERNEL({}): Read Error {}", pid, _e); chn.send(ThreadMessage::SysCall( pid, xous::SysCall::TerminateProcess, @@ -250,7 +250,7 @@ pub fn idle() -> bool { SystemServices::with_mut(|ss| ss.switch_to(new_pid, Some(1))).unwrap(); } ThreadMessage::SysCall(pid, call) => { - println!("KERNEL({}): Received syscall {:?}", pid, call); + // println!("KERNEL({}): Received syscall {:?}", pid, call); crate::arch::process::set_current_pid(pid); // println!("KERNEL({}): Now running as the new process", pid); @@ -280,7 +280,7 @@ pub fn idle() -> bool { // Handle the syscall within the Xous kernel let response = crate::syscall::handle(pid, call).unwrap_or_else(Result::Error); - println!("KERNEL({}): Syscall response {:?}", pid, response); + // println!("KERNEL({}): Syscall response {:?}", pid, response); // There's a response if it wasn't a blocked process and we're not terminating. // Send the response back to the target. if response != Result::BlockedProcess && !is_terminate && !is_shutdown { @@ -308,7 +308,6 @@ pub fn idle() -> bool { crate::arch::process::set_current_pid(existing_pid); } // SystemServices::with_mut(|ss| { - // println!("295: Switching from {}", pid); // ss.switch_from(pid, 1, true)}).unwrap(); } diff --git a/kernel/src/server.rs b/kernel/src/server.rs index be6cd0c7f..06f384608 100644 --- a/kernel/src/server.rs +++ b/kernel/src/server.rs @@ -289,7 +289,7 @@ impl Server { } // Destructure the PID and context ID from the `pid_ctx` field - println!("Taking waiting message -- pid: {} ctx: {}", pid, ctx); + // println!("Taking waiting message -- pid: {} ctx: {}", pid, ctx); if forget { return Ok(WaitingMessage::ForgetMemory(MemoryRange::new( @@ -616,10 +616,6 @@ impl Server { // If the context mask matches this context number, remove it // and return the index. if self.ready_contexts & test_ctx_mask == test_ctx_mask { - println!( - "Returning ready context {:08b}: {}", - self.ready_contexts, ctx_number - ); self.ready_contexts &= !test_ctx_mask; return Some(ctx_number); } @@ -651,7 +647,7 @@ impl Server { /// Add the given context to the list of ready and waiting contexts. pub fn park_context(&mut self, context: CtxID) { - println!("KERNEL({}): Parking context: {}", self.pid, context); + // println!("KERNEL({}): Parking context: {}", self.pid, context); self.ready_contexts |= 1 << context; } } diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 7c60c96c1..55d56bc37 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -299,7 +299,7 @@ impl SystemServices { let new_pid = pid_from_usize(idx + 1)?; arch::process::Process::create(new_pid, init_process); // #[allow(clippy::unit_arg)] - println!("Creating new process for PID {}", new_pid); + // println!("Creating new process for PID {}", new_pid); entry.state = ProcessState::Setup(init_context); entry.ppid = crate::arch::process::current_pid(); entry.pid = new_pid; @@ -500,7 +500,7 @@ impl SystemServices { pid, context, other ), }; - println!("KERNEL({}): Readying context {} -> {:?}", pid, context, process.state); + // println!("KERNEL({}): Readying context {} -> {:?}", pid, context, process.state); Ok(()) } @@ -513,7 +513,6 @@ impl SystemServices { // println!("KERNEL(?): Getting current process..."); let process = self.get_process_mut(pid)?; // println!("KERNEL(?): Current process is {:?}", process); - println!("switch_to (start): PID {}: {:?} @ {:?}", pid, process.state, std::thread::current().id()); // Determine which context number to switch to process.state = match process.state { @@ -614,7 +613,6 @@ impl SystemServices { ProcessState::Running(new_mask) } }; - println!("switch_to (end): PID {}: {:?}", pid, process.state); // self.pid = pid; Ok(()) } @@ -634,7 +632,6 @@ impl SystemServices { can_resume: bool, ) -> Result<(), xous::Error> { let process = self.get_process_mut(pid)?; - println!("switch_from (start): PID {}: {:?}", pid, process.state); process.state = match process.state { ProcessState::Running(x) if x & (1 << context) != 0 => panic!( "PID {} context {} was already queued for running when `switch_from()` was called", @@ -656,7 +653,6 @@ impl SystemServices { pid, other ), }; - println!("switch_from (end): PID {}: {:?}", pid, process.state); Ok(()) } @@ -696,7 +692,7 @@ impl SystemServices { // println!("Activating PID {}, context {}", new_pid, new_context); let previous_pid = self.current_pid(); let previous_context = self.current_context_nr(previous_pid); - println!("KERNEL({}): Activating process {} context {}", previous_pid, new_pid, new_context); + // println!("KERNEL({}): Activating process {} context {}", previous_pid, new_pid, new_context); // Save state if the PID has changed. This will activate the new memory // space. @@ -1423,7 +1419,7 @@ impl SystemServices { process.activate()?; let parent_pid = process.ppid; process.terminate()?; - println!("KERNEL({}): Terminated", target_pid); + // println!("KERNEL({}): Terminated", target_pid); let process = self.get_process(parent_pid)?; process.activate().unwrap(); diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 3b36347e9..09395376e 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -209,10 +209,10 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result core::result::Result core::result::Result core::result::Result core::result::Result core::result::Result SystemServices::with_mut(|ss| { let context_nr = ss.current_context_nr(pid); - println!("515: Switching from {}", pid); ss.switch_from(pid, context_nr, false)?; let ppid = ss.terminate_process(pid)?; if cfg!(baremetal) { diff --git a/kernel/src/test/mod.rs b/kernel/src/test/mod.rs index 78a317010..4bfe48ba2 100644 --- a/kernel/src/test/mod.rs +++ b/kernel/src/test/mod.rs @@ -8,6 +8,12 @@ use xous::{rsyscall, SysCall}; mod shutdown; +#[cfg(feature = "report-memory")] +use stats_alloc::{Region, Stats, StatsAlloc, INSTRUMENTED_SYSTEM}; +#[cfg(feature = "report-memory")] +#[global_allocator] +static GLOBAL: &StatsAlloc = &INSTRUMENTED_SYSTEM; + const SERVER_SPEC: &str = "localhost:0"; fn start_kernel(server_spec: &str) -> (JoinHandle<()>, SocketAddr) { @@ -142,8 +148,8 @@ fn send_move_message() { Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len())) }; assert_eq!(*test_bytes, *bt); - // let s = String::from_utf8_lossy(&bt); - // println!("Got message: {:?} -> \"{}\"", bt, s); + // let s = String::from_utf8_lossy(&bt); + // println!("Got message: {:?} -> \"{}\"", bt, s); } else { panic!("unexpected message type"); } @@ -219,7 +225,9 @@ fn send_borrow_message() { // Send the message to the server println!("CLIENT: Lending message..."); - carton.lend(conn, 0).expect("couldn't lend message to server"); + carton + .lend(conn, 0) + .expect("couldn't lend message to server"); println!("CLIENT: Done"); }); @@ -278,7 +286,9 @@ fn send_mutableborrow_message() { } // Send the message to the server - carton.lend_mut(conn, 3).expect("couldn't mutably lend data"); + carton + .lend_mut(conn, 3) + .expect("couldn't mutably lend data"); let modified_bytes: &[u8] = carton.as_ref(); assert_eq!(&check_bytes, &modified_bytes); @@ -294,69 +304,97 @@ fn send_mutableborrow_message() { } #[test] -fn send_mutableborrow_message_long() { - let (main_thread, server_spec) = start_kernel(SERVER_SPEC); - let (server_addr_send, server_addr_recv) = channel(); - let test_str = "Hello, world!"; - let test_bytes = test_str.as_bytes(); +fn send_mutableborrow_message_repeat() { + #[cfg(feature = "report-memory")] + let mut reg = Region::new(&GLOBAL); + #[cfg(feature = "report-memory")] + reg.reset(); - let loops = 50; + { + let (main_thread, server_spec) = start_kernel(SERVER_SPEC); + let (server_addr_send, server_addr_recv) = channel(); + let test_str = "Hello, world!"; + let test_bytes = test_str.as_bytes(); - let xous_server = spawn(move || { - xous::hosted::set_xous_address(server_spec); - let sid = xous::create_server(0x7884_3123).expect("couldn't create test server"); - server_addr_send.send(sid).unwrap(); + let loops = 50_000; - for iteration in 0..loops { - let envelope = xous::receive_message(sid).expect("couldn't receive messages"); - let message = envelope.message; - if let xous::Message::MutableBorrow(m) = message { - let buf = m.buf; - let mut bt = unsafe { - Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len())) - }; - for letter in bt.iter_mut() { + let xous_server = spawn(move || { + xous::hosted::set_xous_address(server_spec); + let sid = xous::create_server(0x7884_3123).expect("couldn't create test server"); + server_addr_send.send(sid).unwrap(); + + for iteration in 0..loops { + let envelope = xous::receive_message(sid).expect("couldn't receive messages"); + let message = envelope.message; + if let xous::Message::MutableBorrow(m) = message { + let buf = m.buf; + let mut bt = unsafe { + Box::from_raw(core::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.len())) + }; + for letter in bt.iter_mut() { + *letter = (*letter).wrapping_add((iteration & 0xff) as u8); + } + xous::return_memory(envelope.sender, m.buf).unwrap(); + } else { + panic!("unexpected message type"); + } + } + }); + + let xous_client = spawn(move || { + xous::hosted::set_xous_address(server_spec); + + // Get the server address (out of band) so we know what to connect to + let sid = server_addr_recv.recv().unwrap(); + + // Perform a connection to the server + let conn = xous::connect(sid).expect("couldn't connect to server"); + + // Convert the message into a "Carton" that can be shipped as a message + for iteration in 0..loops { + let mut carton = xous::carton::Carton::from_bytes(&test_bytes); + let mut check_bytes = test_bytes.to_vec(); + for letter in check_bytes.iter_mut() { *letter = (*letter).wrapping_add((iteration & 0xff) as u8); } - xous::return_memory(envelope.sender, m.buf).unwrap(); + + // Send the message to the server + carton + .lend_mut(conn, 3) + .expect("couldn't mutably lend data"); + + let modified_bytes: &[u8] = carton.as_ref(); + assert_eq!(&check_bytes, &modified_bytes); + } + }); + + xous_server.join().expect("couldn't join server process"); + xous_client.join().expect("couldn't join client process"); + + // Any process ought to be able to shut down the system currently. + rsyscall(SysCall::Shutdown).expect("unable to shutdown server"); + + main_thread.join().expect("couldn't join kernel process"); + } + + #[cfg(feature = "report-memory")] + { + fn memory_in_use(start: &Stats) -> usize { + if start.bytes_deallocated > start.bytes_allocated { + eprintln!("Allocated a negative number of bytes!"); + 0 } else { - panic!("unexpected message type"); + start.bytes_allocated - start.bytes_deallocated } } - }); - let xous_client = spawn(move || { - xous::hosted::set_xous_address(server_spec); - - // Get the server address (out of band) so we know what to connect to - let sid = server_addr_recv.recv().unwrap(); - - // Perform a connection to the server - let conn = xous::connect(sid).expect("couldn't connect to server"); - - // Convert the message into a "Carton" that can be shipped as a message - for iteration in 0..loops { - let mut carton = xous::carton::Carton::from_bytes(&test_bytes); - let mut check_bytes = test_bytes.to_vec(); - for letter in check_bytes.iter_mut() { - *letter = (*letter).wrapping_add((iteration & 0xff) as u8); - } - - // Send the message to the server - carton.lend_mut(conn, 3).expect("couldn't mutably lend data"); - - let modified_bytes: &[u8] = carton.as_ref(); - assert_eq!(&check_bytes, &modified_bytes); - } - }); - - xous_server.join().expect("couldn't join server process"); - xous_client.join().expect("couldn't join client process"); - - // Any process ought to be able to shut down the system currently. - rsyscall(SysCall::Shutdown).expect("unable to shutdown server"); - - main_thread.join().expect("couldn't join kernel process"); + let after_join = reg.change(); + println!( + "After test: {:#?} ({} bytes in use)", + after_join, + memory_in_use(&after_join) + ); + } } // #[test]