From aae5f3737ce9b572a1d1ea1cd11e21232d6a191b Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 29 Dec 2022 21:04:01 +0800 Subject: [PATCH 1/4] kernel: implement reply_and_receive_next Implement `ReplyAndReceiveNext` as a syscall. This will reply to a message, pull the next message out of the queue, and then re-queue the server to run. This should make servers much more responsive. This patch has been tested on both hosted and native modes. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 147 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 61b30c9bf..ae1fec095 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -616,6 +616,150 @@ fn return_scalar5( }) } +fn reply_and_receive_next( + server_pid: PID, + server_tid: TID, + _in_irq: bool, + sender: MessageSender, + arg0: usize, + arg1: usize, + arg2: usize, + arg3: usize, + arg4: usize, + scalar_type: usize, +) -> SysCallResult { + let sender = SenderID::from(sender); + + SystemServices::with_mut(|ss| { + struct MessageResponse { + pid: PID, + tid: TID, + result: xous_kernel::Result, + } + + let (result, next_message) = { + let server = ss + .server_from_sidx_mut(sender.sidx) + .ok_or(xous_kernel::Error::ServerNotFound)?; + if server.pid != server_pid { + println!( + "WARNING: PIDs don't match! The server is from PID {}, but our PID is {}", + server.pid, server_pid + ); + return Err(xous_kernel::Error::ServerNotFound); + } + + let waiting_message = server.take_waiting_message(sender.idx, None)?; + + let next_message = server.take_next_message(sender.sidx); + // If there is no message, park the server thread. We do this here because + // we cannot hold the `Server` object while we also modify process state. + if next_message.is_none() { + server.park_thread(server_tid); + } + + (waiting_message, next_message) + }; + + // TODO: Have errors turn into calls to `ReceiveMessage` + let response = match result { + WaitingMessage::ScalarMessage(pid, tid) => { + let result = match scalar_type { + 1 => xous_kernel::Result::Scalar1(arg1), + 2 => xous_kernel::Result::Scalar2(arg1, arg2), + _ => xous_kernel::Result::Scalar5(arg0, arg1, arg2, arg3, arg4), + }; + MessageResponse { pid, tid, result } + } + WaitingMessage::ForgetMemory(_) => { + println!( + "WARNING: Tried to wait on a scalar message that was actually forgetting memory" + ); + return Err(xous_kernel::Error::ProcessNotFound); + } + WaitingMessage::BorrowedMemory(pid, tid, _server_addr, client_addr, len) => { + #[cfg(baremetal)] + let src_virt = _server_addr.get() as _; + #[cfg(not(baremetal))] + let src_virt = arg1 as _; // TODO: Check this is correct on hosted mode + + // Return the memory to the calling process + ss.return_memory(src_virt, pid, tid, client_addr.get() as _, len.get())?; + + MessageResponse { + pid, + tid, + result: xous_kernel::Result::MemoryReturned(MemorySize::new(arg3), MemorySize::new(arg4)), + } + } + WaitingMessage::MovedMemory => { + println!( + "WARNING: Tried to wait on a scalar message that was actually moved memory" + ); + return Err(xous_kernel::Error::ProcessNotFound); + } + WaitingMessage::None => { + println!("WARNING: Tried to wait on a message that didn't exist"); + return Err(xous_kernel::Error::ProcessNotFound); + } + }; + + // If there is a pending message, fetch it and schedule the thread to run + if let Some(msg) = next_message { + if cfg!(baremetal) { + // When the server is resumed, it will receive this as a return value. + ss.set_thread_result( + server_pid, + server_tid, + xous_kernel::Result::MessageEnvelope(msg), + )?; + ss.ready_thread(response.pid, response.tid).unwrap(); + ss.set_thread_result(response.pid, response.tid, response.result)?; + ss.activate_process_thread(server_tid, response.pid, response.tid, true) + .map(|_| Ok(xous_kernel::Result::ResumeProcess)) + .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) + } else { + println!("Rescheduling server because of full queue"); + + // Switch to the client and return the result + ss.ready_thread(response.pid, response.tid)?; + ss.switch_to_thread(response.pid, Some(response.tid))?; + ss.set_thread_result(response.pid, response.tid, response.result)?; + + // Return the new message envelope to the server + Ok(xous_kernel::Result::MessageEnvelope(msg)) + } + } else { + // For baremetal targets, switch away from this process. + if cfg!(baremetal) { + // Switch back to the client process. + ss.ready_thread(response.pid, response.tid)?; + + // Set the thread result for the client + ss.set_thread_result(response.pid, response.tid, response.result)?; + + // Activate the client thread and switch to it + ss.activate_process_thread(server_tid, response.pid, response.tid, false) + .map(|_| Ok(xous_kernel::Result::ResumeProcess)) + .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) + } + // For hosted targets, simply return `BlockedProcess` indicating we'll make + // a callback to their socket at a later time. + else { + println!("Unscheduling server due to empty queue"); + ss.unschedule_thread(server_pid, server_tid)?; + // Switch to the client and return the result + ss.ready_thread(response.pid, response.tid)?; + ss.switch_to_thread(response.pid, Some(response.tid))?; + ss.set_thread_result(response.pid, response.tid, response.result)?; + + // Indicate that the server should block its process + Ok(xous_kernel::Result::BlockedProcess) + } + } + }) +} + fn receive_message(pid: PID, tid: TID, sid: SID, blocking: ExecutionType) -> SysCallResult { SystemServices::with_mut(|ss| { assert!( @@ -936,6 +1080,9 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR SysCall::ReturnScalar5(sender, arg1, arg2, arg3, arg4, arg5) => { return_scalar5(pid, tid, in_irq, sender, arg1, arg2, arg3, arg4, arg5) } + SysCall::ReplyAndReceiveNext(sender, a0, a1, a2, a3, a4, scalar_type) => { + reply_and_receive_next(pid, tid, in_irq, sender, a0, a1, a2, a3, a4, scalar_type) + } SysCall::TrySendMessage(cid, message) => send_message(pid, tid, cid, message), SysCall::TerminateProcess(_ret) => SystemServices::with_mut(|ss| { ss.unschedule_thread(pid, tid)?; From 23c17d7632143c9ad3a7af4eb4a480acb299a50e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 29 Dec 2022 21:05:26 +0800 Subject: [PATCH 2/4] test-spawn: rename `Message` to `MessageEnvelope` Signed-off-by: Sean Cross --- services/test-spawn/spawn/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/test-spawn/spawn/src/main.rs b/services/test-spawn/spawn/src/main.rs index 9f7703cc9..dfeac1627 100644 --- a/services/test-spawn/spawn/src/main.rs +++ b/services/test-spawn/spawn/src/main.rs @@ -32,7 +32,7 @@ fn handle_panic(_arg: &core::panic::PanicInfo) -> ! { pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -> ! { let server = xous::SID::from_u32(server1, server2, server3, server4); loop { - if let Ok(xous::Result::Message(envelope)) = + if let Ok(xous::Result::MessageEnvelope(envelope)) = xous::rsyscall(xous::SysCall::ReceiveMessage(server)) { match envelope.id().into() { From e045574d39a8d4a3236ec23079ea1a3b832c39d8 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 29 Dec 2022 21:06:51 +0800 Subject: [PATCH 3/4] services: xous-ticktimer: use reply_and_receive_next() Convert the ticktimer server to use the new reply_and_receive_next_legacy() call. This is an example of how to use this API for future projects. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 119 ++++++++++++++++++---------- 1 file changed, 75 insertions(+), 44 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index 49490cabf..db2d6a7e7 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -3,7 +3,7 @@ #![cfg_attr(target_os = "none", no_main)] use xous_api_ticktimer::*; -#[cfg(feature="timestamp")] +#[cfg(feature = "timestamp")] mod version; use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; @@ -62,7 +62,7 @@ impl core::cmp::PartialEq for TimerRequest { } } -#[cfg(any(feature="precursor", feature="renode"))] +#[cfg(any(feature = "precursor", feature = "renode"))] mod implementation { const TICKS_PER_MS: u64 = 1; use super::TimerRequest; @@ -284,8 +284,9 @@ mod implementation { } } -#[cfg(any(not(target_os = "xous"), - not(any(feature="precursor", feature="renode", not(target_os = "xous"))) +#[cfg(any( + not(target_os = "xous"), + not(any(feature = "precursor", feature = "renode", not(target_os = "xous"))) ))] mod implementation { use crate::RequestKind; @@ -524,14 +525,14 @@ fn main() -> ! { log::set_max_level(log::LevelFilter::Info); info!("my PID is {}", xous::process::id()); - #[cfg(feature="timestamp")] + #[cfg(feature = "timestamp")] { log::info!("****************************************************************"); log::info!("Welcome to Xous {}", version::SEMVER); log::info!("Built on {}", version::TIMESTAMP); log::info!("****************************************************************"); } - #[cfg(not(feature="timestamp"))] + #[cfg(not(feature = "timestamp"))] { log::info!("****************************************************************"); log::info!("Welcome to Xous"); @@ -598,39 +599,65 @@ fn main() -> ! { let mut mutex_hash: HashMap, HashMap>> = HashMap::new(); + let mut msg_opt = None; + let mut return_type = 0; loop { #[cfg(feature = "watchdog")] ticktimer.reset_wdt(); //#[cfg(feature = "watchdog")] // for debugging the watchdog //ticktimer.check_wdt(); - let mut msg = xous::receive_message(ticktimer_server).unwrap(); + xous::reply_and_receive_next_legacy(ticktimer_server, &mut msg_opt, &mut return_type) + .unwrap(); + let msg = msg_opt.as_mut().unwrap(); log::trace!("msg: {:x?}", msg); match num_traits::FromPrimitive::from_usize(msg.body.id()) .unwrap_or(api::Opcode::InvalidCall) { api::Opcode::ElapsedMs => { - let time = ticktimer.elapsed_ms() as i64; - xous::return_scalar2( - msg.sender, - (time & 0xFFFF_FFFFi64) as usize, - ((time >> 32) & 0xFFF_FFFFi64) as usize, - ) - .expect("couldn't return time request"); + if let Some(scalar) = msg.body.scalar_message_mut() { + let time = ticktimer.elapsed_ms() as i64; + scalar.arg1 = (time & 0xFFFF_FFFFi64) as usize; + scalar.arg2 = ((time >> 32) & 0xFFF_FFFFi64) as usize; + scalar.id = 0; + + // API calls expect a `Scalar2` value in response + return_type = 2; + } + } + api::Opcode::SleepMs => { + if let Some(scalar) = msg.body.scalar_message_mut() { + let ms = scalar.arg1 as i64; + let sender = msg.sender; + + // Forget the contents of the message, replacing it with `None`. This + // will prevent it from getting dropped, which (under normal circumstances) + // would cause it to be responded to automatically. + // + // The ideal approach here would be to have the struct -- in this case the + // TimerRequest -- keep track of the element itself. That way, it could + // return the value as part of its response routine. However, due to the + // legacy split of `BlockingScalars`, these are allowed to have an explicit + // return path, so we need to forget the original message here to prevent + // it from getting returned early. + // + // (n.b. this auto-response hasn't been enabled yet for Scalar messages, + // but this definitely would need to be done for e.g. Memory messages). + core::mem::forget(msg_opt.take()); + + // let timeout_queue = timeout_heap.entry(msg.sender.pid()).or_default(); + recalculate_sleep( + &mut ticktimer, + &mut sleep_heap, + Some(TimerRequest { + msec: ms, + sender: sender, + kind: RequestKind::Sleep, + data: 0, + }), + ); + } } - api::Opcode::SleepMs => xous::msg_blocking_scalar_unpack!(msg, ms, _, _, _, { - // let timeout_queue = timeout_heap.entry(msg.sender.pid()).or_default(); - recalculate_sleep( - &mut ticktimer, - &mut sleep_heap, - Some(TimerRequest { - msec: ms as i64, - sender: msg.sender, - kind: RequestKind::Sleep, - data: 0, - }), - ) - }), api::Opcode::RecalculateSleep => { // let timeout_queue = timeout_heap.entry(msg.sender.pid()).or_default(); if let Some(args) = msg.body.scalar_message() { @@ -686,28 +713,24 @@ fn main() -> ! { msg.body.memory_message_mut().unwrap(), ) }; - #[cfg(feature="timestamp")] + #[cfg(feature = "timestamp")] buf.replace(version::get_version()).unwrap(); - #[cfg(not(feature="timestamp"))] + #[cfg(not(feature = "timestamp"))] { let v = crate::api::VersionString { - version: xous_ipc::String::from_str("--no-timestamp requested for build") + version: xous_ipc::String::from_str("--no-timestamp requested for build"), }; buf.replace(v).unwrap(); } } api::Opcode::LockMutex => { let pid = msg.sender.pid(); - if !msg.body.is_blocking() { - info!("sender made LockMutex request that was not blocking"); - continue; - } - if let Some(scalar) = msg.body.scalar_message() { + if let Some(scalar) = msg.body.scalar_message_mut() { let ready = mutex_ready_hash.entry(pid).or_default(); // If this item is in the Ready list, return right away without blocking if ready.remove(&scalar.arg1) { - xous::return_scalar(msg.sender, 0).unwrap(); + scalar.id = 0; continue; } @@ -719,6 +742,12 @@ fn main() -> ! { // the message will get a response. let mutex_entry = awaiting.entry(scalar.arg1).or_default(); mutex_entry.push_back(msg.sender); + + // We've saved the `msg.sender` above and will return the lock as part of the mutex + // work thread. Forget the contents of `msg_opt` without running its destructor. + core::mem::forget(msg_opt.take()); + } else { + info!("sender made LockMutex request that was not blocking"); } } api::Opcode::UnlockMutex => { @@ -745,11 +774,7 @@ fn main() -> ! { } api::Opcode::WaitForCondition => { let pid = msg.sender.pid(); - if !msg.body.is_blocking() { - info!("sender made WaitForCondition request that was not blocking"); - continue; - } - if let Some(scalar) = msg.body.scalar_message() { + if let Some(scalar) = msg.body.scalar_message_mut() { let condvar = scalar.arg1; let timeout = scalar.arg2; @@ -769,8 +794,10 @@ fn main() -> ! { { if *excess > 0 { *excess -= 1; - xous::return_scalar(msg.sender, 0) - .expect("couldn't respond to message"); + scalar.id = 0; + + // Rust libstd expects a `Scalar1` return type for this call + return_type = 1; continue; } } @@ -798,7 +825,11 @@ fn main() -> ! { .push_back(msg.sender); // log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar)); - continue; + + // The message will be responded to as part of the notification hash + // when the condvar is unlocked. Forget the contents of the message + // in order to prevent it from being responded to early. + core::mem::forget(msg_opt.take()); } else { info!( "sender made WaitForCondition request that wasn't a BlockingScalar Message" From 3bcc955aba12f52a92928e36d7cc0fbc4c767fbd Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 30 Dec 2022 15:21:43 +0800 Subject: [PATCH 4/4] add test code that also benchmarks the new routines. --- services/shellchat/src/cmds/test.rs | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/services/shellchat/src/cmds/test.rs b/services/shellchat/src/cmds/test.rs index 719c99c53..10c009f30 100644 --- a/services/shellchat/src/cmds/test.rs +++ b/services/shellchat/src/cmds/test.rs @@ -680,6 +680,91 @@ impl<'a> ShellCmdApi<'a> for Test { ).unwrap(); write!(ret, "\nDid EC auto update command").unwrap(); } + #[cfg(feature="benchmarks")] + "bench" => { + let bench_original_sid = xous::create_server().unwrap(); + let bench_original_cid = xous::connect(bench_original_sid).unwrap(); + std::thread::spawn({ + move || { + loop { + let msg = xous::receive_message(bench_original_sid).unwrap(); + xous::msg_blocking_scalar_unpack!(msg, a1, _, _, _, { + xous::return_scalar(msg.sender, a1 + 1).unwrap(); + }); + if msg.id() == 1 { + break; + } + } + log::info!("Quitting old bench thread"); + } + }); + + let bench_new_sid = xous::create_server().unwrap(); + let bench_new_cid = xous::connect(bench_new_sid).unwrap(); + std::thread::spawn({ + move || { + let mut msg_opt = None; + let mut return_type = 0; + loop { + xous::reply_and_receive_next_legacy(bench_new_sid, &mut msg_opt, &mut return_type) + .unwrap(); + let msg = msg_opt.as_mut().unwrap(); + if let Some(scalar) = msg.body.scalar_message_mut() { + scalar.arg1 += 1; + return_type = 1; + if scalar.id == 1 { + scalar.id = 1; + xous::return_scalar(msg.sender, scalar.arg1).ok(); + core::mem::forget(msg_opt.take()); + break; + } else { + scalar.id = 0; + } + } + } + log::info!("Quitting new bench thread"); + } + }); + const ITERS: usize = 10_000; + let tt = ticktimer_server::Ticktimer::new().unwrap(); + let start_time = tt.elapsed_ms(); + let mut a = 0; + while a < ITERS { + a = match xous::send_message(bench_original_cid, + Message::new_blocking_scalar(0, a, 0, 0, 0) + ) { + Ok(xous::Result::Scalar1(a_prime)) => a_prime, + _ => panic!("incorrect return type") + } + } + let result = format!("Original took {}ms for {} iters\n", tt.elapsed_ms() - start_time, ITERS); + log::info!("{}", result); + write!(ret, "{}\n", result).ok(); + // this quits the thread + xous::send_message(bench_original_cid, + Message::new_blocking_scalar(1, 0, 0, 0, 0) + ).ok(); + unsafe {xous::disconnect(bench_original_cid).ok()}; + + let start_time = tt.elapsed_ms(); + let mut a = 0; + while a < ITERS { + a = match xous::send_message(bench_new_cid, + Message::new_blocking_scalar(0, a, 0, 0, 0) + ) { + Ok(xous::Result::Scalar1(a_prime)) => a_prime, + _ => panic!("incorrect return type") + } + } + let result = format!("New took {}ms for {} iters\n", tt.elapsed_ms() - start_time, ITERS); + write!(ret, "{}", result).ok(); + log::info!("{}", result); + // this quits the thread + xous::send_message(bench_new_cid, + Message::new_blocking_scalar(1, 0, 0, 0, 0) + ).ok(); + unsafe {xous::disconnect(bench_new_cid).ok()}; + } _ => { () // do nothing }