mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
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 <sean@xobs.io>
This commit is contained in:
parent
e652150c6e
commit
bf15ac08f4
7 changed files with 135 additions and 100 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
|
|
@ -54,10 +54,10 @@ fn handle_connection(mut conn: TcpStream, pid: PID, chn: Sender<ThreadMessage>)
|
|||
// 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<ThreadMessage>)
|
|||
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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -209,10 +209,10 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
// process and mark ourselves as awaiting an event. When a message
|
||||
// arrives, our return value will already be set to the
|
||||
// MessageEnvelope of the incoming message.
|
||||
println!(
|
||||
"KERNEL({}): did not have any waiting messages -- parking context {}",
|
||||
pid, context_nr
|
||||
);
|
||||
// println!(
|
||||
// "KERNEL({}): did not have any waiting messages -- parking context {}",
|
||||
// pid, context_nr
|
||||
// );
|
||||
server.park_context(context_nr);
|
||||
|
||||
// For baremetal targets, switch away from this process.
|
||||
|
|
@ -226,7 +226,6 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
// For hosted targets, simply return `BlockedProcess` indicating we'll make
|
||||
// a callback to their socket at a later time.
|
||||
else {
|
||||
println!("229: Switching from {}", pid);
|
||||
ss.switch_from(pid, context_nr, false)
|
||||
.map(|_| xous::Result::BlockedProcess)
|
||||
}
|
||||
|
|
@ -297,15 +296,15 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
return Err(xous::Error::ProcessNotFound);
|
||||
}
|
||||
};
|
||||
println!(
|
||||
"Returning {} bytes from {:08x} in PID {} to {:08x} in PID {} in context {}",
|
||||
len,
|
||||
server_addr.get(),
|
||||
pid,
|
||||
client_addr.get(),
|
||||
client_pid,
|
||||
client_ctx
|
||||
);
|
||||
// println!(
|
||||
// "Returning {} bytes from {:08x} in PID {} to {:08x} in PID {} in context {}",
|
||||
// len,
|
||||
// server_addr.get(),
|
||||
// pid,
|
||||
// client_addr.get(),
|
||||
// client_pid,
|
||||
// client_ctx
|
||||
// );
|
||||
|
||||
// Return the memory to the calling process
|
||||
ss.return_memory(
|
||||
|
|
@ -316,10 +315,10 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
)?;
|
||||
|
||||
// Unblock the client context to allow it to continue.
|
||||
println!(
|
||||
"KERNEL({}): Unblocking PID {} CTX {}",
|
||||
pid, client_pid, client_ctx
|
||||
);
|
||||
// println!(
|
||||
// "KERNEL({}): Unblocking PID {} CTX {}",
|
||||
// pid, client_pid, client_ctx
|
||||
// );
|
||||
ss.ready_context(client_pid, client_ctx)?;
|
||||
ss.set_context_result(client_pid, client_ctx, xous::Result::Ok)?;
|
||||
ss.switch_to(client_pid, Some(client_ctx))?;
|
||||
|
|
@ -467,7 +466,6 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
xous::Result::Message(envelope),
|
||||
)
|
||||
.and_then(|_| {
|
||||
println!("464: Switching from {}", pid);
|
||||
ss.switch_from(pid, context_nr, false)
|
||||
})
|
||||
.map(|_| xous::Result::BlockedProcess)
|
||||
|
|
@ -523,7 +521,6 @@ pub fn handle(pid: PID, call: SysCall) -> core::result::Result<xous::Result, xou
|
|||
}
|
||||
SysCall::TerminateProcess => 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) {
|
||||
|
|
|
|||
|
|
@ -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<std::alloc::System> = &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]
|
||||
|
|
|
|||
Loading…
Reference in a new issue