From 126896bf54cd22f96c09a5746916bb5416798ce9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jul 2025 01:24:38 +0800 Subject: [PATCH] optimize kernel message passing times Based on this benchmark of elapsed_ms(): ```rust pub fn elapsed_ms(&self) -> u64 { let response = send_message( self.conn, xous::Message::new_blocking_scalar(api::Opcode::ElapsedMs.to_usize().unwrap(), 0, 0, 0, 0), ) .expect("Ticktimer: failure to send message to Ticktimer"); if let xous::Result::Scalar2(upper, lower) = response { upper as u64 | ((lower as u64) << 32) } else { panic!("Ticktimer elapsed_ms(): unexpected return value."); } } ``` We find the following context switches: 1. send_message -> kernel/src/syscall.rs @ 166 - ss.remember_server_message() - *context switch round trip* to enqueue the message into the target thread's space, then switch back. - takes 546ns just in the target context, out of 4430ns round-trip (measured as leave userspace of caller to enter userspace of callee) (12% of total time) - emplaces the message in the target queue - generates "sender_idx" which is the index in the message queue to find the message, which is put into Envelope 2. send_message -> kernel/src/syscall.rs @ 222 - ss.activate_process_thread() - activates the thread to run - Total penalty is closer to 1us - 22% of total time - once pre/amble/post-amble overhead considered This set of patches removes the context-switch round trip to emplace messages in the target's thread space, as well as to set thread results, by passing them as arguments to `activate_process_thread`. What then happens is that these items are emplaced after the activation privot to the target process space occurs, saving a set of TLB-flushes and page table walks. More testing is required, but at least on a reasonable set of cases the system seems to be stable with these patcehs, and we get a 24% speedup on blocking scalar messages and 8% speedup on mutable lends in the case that the target is waiting to receive messages. --- kernel/src/services.rs | 143 +++++++++++++++++++++++++++++------------ kernel/src/syscall.rs | 100 ++++++++++++++++++---------- 2 files changed, 169 insertions(+), 74 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 27d6f5456..2753033e8 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -66,6 +66,14 @@ pub struct SystemServices { pub servers: [Option; MAX_SERVER_COUNT], } +pub enum PostActivateOp<'a> { + None, + // (result) + SetThreadResult(xous_kernel::Result), + // (sidx, current_pid, current_thread, message, client_address) + RememberServerMessage(usize, PID, TID, &'a Message, Option), +} + #[derive(Copy, Clone, PartialEq)] pub enum ProcessState { /// This is an unallocated, free process @@ -1042,13 +1050,21 @@ impl SystemServices { /// Resume the given process, picking up exactly where it left off. If the /// process is in the Setup state, set it up and then resume. + /// + /// `lazy_arg` is an argument that should be placed in the activated target process, + /// after the activation is complete. The purpose of this is to reduce the incidence + /// of the idiom where we swap into the a target process' memory state just to emplace + /// a small argument or piece of data, only to swap back to then return to it on activation. + /// This optimization improves message latency by around 15% in the fast path. pub fn activate_process_thread( &mut self, previous_tid: TID, new_pid: PID, mut new_tid: TID, can_resume: bool, + lazy_arg: PostActivateOp, ) -> Result { + let mut sender_idx: Option = None; let previous_pid = self.current_pid(); #[cfg(feature = "debug-print")] @@ -1060,59 +1076,63 @@ impl SystemServices { // Save state if the PID has changed. This will activate the new memory // space. - let new = self.get_process_mut(new_pid)?; if new_pid != previous_pid { - klog!("New process original state: {:?}", new.state); + { + let new = self.get_process_mut(new_pid)?; + klog!("New process original state: {:?}", new.state); - // Ensure the new process can be run. - match new.state { - ProcessState::Free => { - klog!("PID {} was free", new_pid); - return Err(xous_kernel::Error::ProcessNotFound); - } - ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID, - ProcessState::Exception(_) => { - new_tid = crate::arch::process::EXCEPTION_TID; - // new.current_thread = new_tid; - } - ProcessState::Ready(x) => { - // If no new context is specified, take the previous - // context. If that is not runnable, do a round-robin - // search for the next available context. - assert!(x != 0, "process was {:?} but had no runnable threads", new.state); - if new_tid == 0 { - new_tid = Self::find_next_thread(x, new.current_thread); - } - if x & (1 << new_tid) == 0 { - println!( - "process state is {:?}, but new thread {} is not runnable", - new.state, new_tid - ); + // Ensure the new process can be run. + match new.state { + ProcessState::Free => { + klog!("PID {} was free", new_pid); + return Err(xous_kernel::Error::ProcessNotFound); + } + ProcessState::Setup(_) | ProcessState::Allocated => new_tid = INITIAL_TID, + ProcessState::Exception(_) => { + new_tid = crate::arch::process::EXCEPTION_TID; + // new.current_thread = new_tid; + } + ProcessState::Ready(x) => { + // If no new context is specified, take the previous + // context. If that is not runnable, do a round-robin + // search for the next available context. + assert!(x != 0, "process was {:?} but had no runnable threads", new.state); + if new_tid == 0 { + new_tid = Self::find_next_thread(x, new.current_thread); + } + if x & (1 << new_tid) == 0 { + println!( + "process state is {:?}, but new thread {} is not runnable", + new.state, new_tid + ); + return Err(xous_kernel::Error::ProcessNotFound); + } + new.current_thread = new_tid as _; + } + ProcessState::Running(_) => { + panic!("process was running even though the pid was different") + } + #[cfg(feature = "gdb-stub")] + ProcessState::Debug(_) | ProcessState::DebugIrq(_) => { + return Err(xous_kernel::Error::ProcessNotFound); + } + ProcessState::Sleeping | ProcessState::BlockedException(_) => { + // println!("PID {} was sleeping or being debugged", new_pid); return Err(xous_kernel::Error::ProcessNotFound); } - new.current_thread = new_tid as _; - } - ProcessState::Running(_) => { - panic!("process was running even though the pid was different") - } - #[cfg(feature = "gdb-stub")] - ProcessState::Debug(_) | ProcessState::DebugIrq(_) => { - return Err(xous_kernel::Error::ProcessNotFound); - } - ProcessState::Sleeping | ProcessState::BlockedException(_) => { - // println!("PID {} was sleeping or being debugged", new_pid); - return Err(xous_kernel::Error::ProcessNotFound); } } // Perform the actual switch to the new memory space. From this // point onward, we will need to activate the previous memory space // if we encounter an error. + let new = self.get_process(new_pid)?; new.mapping.activate()?; // Set up the new process, if necessary. Remove the new thread from // the list of ready threads. // let old_state = new.state; + let new = self.get_process_mut(new_pid)?; new.state = match new.state { ProcessState::Setup(thread_init) => { // klog!("Setting up new process..."); @@ -1141,6 +1161,27 @@ impl SystemServices { }; // log_process_update(file!(), line!(), new, old_state); new.activate()?; + match lazy_arg { + PostActivateOp::RememberServerMessage( + sidx, + current_pid, + current_thread, + message, + client_address, + ) => { + let si = { + let server = + self.server_from_sidx_mut(sidx).expect("couldn't re-discover server index"); + server.queue_response(current_pid, current_thread, message, client_address)? + }; + sender_idx = Some(si); + } + PostActivateOp::SetThreadResult(result) => { + let mut arch_process = ArchProcess::current(); + arch_process.set_thread_result(new_tid, result); + } + _ => (), + } // Mark the previous process as ready to run, since we just switched // away @@ -1196,6 +1237,26 @@ impl SystemServices { // can_resume // ); } else { + match lazy_arg { + PostActivateOp::RememberServerMessage( + sidx, + current_pid, + current_thread, + message, + client_address, + ) => { + let si = { + let server = + self.server_from_sidx_mut(sidx).expect("couldn't re-discover server index"); + server.queue_response(current_pid, current_thread, message, client_address)? + }; + sender_idx = Some(si); + } + PostActivateOp::SetThreadResult(result) => { + ArchProcess::current().set_thread_result(new_tid, result); + } + _ => (), + } let new = self.get_process_mut(new_pid)?; // If we wanted to switch to a "new" thread, and it's the same @@ -1253,7 +1314,9 @@ impl SystemServices { self.get_process_mut(new_pid)?.state ); - Ok(new_tid) + // return argument based upon the lazy_arg type + // we get "lucky" in that sender_idx *and* new_tid are both `usize` types underneath + if let Some(si) = sender_idx { Ok(si) } else { Ok(new_tid) } } /// Move memory from one process to another. @@ -1725,7 +1788,7 @@ impl SystemServices { if arch_process.thread_exists(join_tid) { // The target thread exists -- put this thread to sleep let ppid = self.get_process(pid).unwrap().ppid; - self.activate_process_thread(tid, ppid, 0, false) + self.activate_process_thread(tid, ppid, 0, false, PostActivateOp::None) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) } else { diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 0e1097cc8..1b41951b4 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -11,7 +11,7 @@ use crate::arch::process::Process as ArchProcess; use crate::irq::{interrupt_claim, interrupt_free}; use crate::mem::MemoryManager; use crate::server::{SenderID, WaitingMessage}; -use crate::services::SystemServices; +use crate::services::{PostActivateOp, SystemServices}; #[cfg(feature = "swap")] use crate::swap::{Swap, SwapAbi}; @@ -70,7 +70,7 @@ fn do_yield(_pid: PID, tid: TID) -> SysCallResult { SystemServices::with_mut(|ss| { // TODO: Advance thread let result = ss - .activate_process_thread(tid, parent_pid, parent_ctx, true) + .activate_process_thread(tid, parent_pid, parent_ctx, true, PostActivateOp::None) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)); @@ -162,17 +162,7 @@ fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult // "there are threads available in PID {} to handle this message -- marking as Ready", // server_pid // ); - let sender_idx = if message.is_blocking() { - ss.remember_server_message(sidx, pid, tid, &message, client_address).map_err(|e| { - klog!("error remembering server message: {:?}", e); - ss.server_from_sidx_mut(sidx) - .expect("server couldn't be located") - .return_available_thread(server_tid); - e - })? - } else { - 0 - }; + let sender_idx = 0; // lazy-evaluate to a real number for blocking messages; eagerly set to 0 for non-blocking let sender = SenderID::new(sidx, sender_idx, Some(pid)); klog!("server connection data: sidx: {}, idx: {}, server pid: {}", sidx, sender_idx, server_pid); let envelope = MessageEnvelope { sender: sender.into(), body: message }; @@ -191,19 +181,33 @@ fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult // --- NOTE: Returning this value // return if blocking && cfg!(baremetal) { if !runnable { + // re-bind the envelope because the sender_idx binding is lazily evaluated + let message = envelope.take_message(); + let sender_idx = ss + .remember_server_message(sidx, pid, tid, &message, client_address) + .map_err(|e| { + klog!("error remembering server message: {:?}", e); + ss.server_from_sidx_mut(sidx) + .expect("server couldn't be located") + .return_available_thread(server_tid); + e + })?; + let sender = SenderID::new(sidx, sender_idx, Some(pid)); + let envelope = MessageEnvelope { sender: sender.into(), body: message }; + // If it's not runnable (e.g. it's being debugged), switch to the parent. let (ppid, ptid) = unsafe { (&mut *(&raw mut SWITCHTO_CALLER)).take().unwrap() }; klog!( "Activating Server parent process (server is blocked) and switching away from Client" ); - ss.set_thread_result( - server_pid, - server_tid, - xous_kernel::Result::MessageEnvelope(envelope), - ) - .expect("couldn't set result for server thread"); let result = ss - .activate_process_thread(tid, ppid, ptid, false) + .activate_process_thread( + tid, + ppid, + ptid, + false, + PostActivateOp::SetThreadResult(xous_kernel::Result::MessageEnvelope(envelope)), + ) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)); @@ -219,11 +223,36 @@ fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult } else { // Switch to the server, since it's in a state to be run. klog!("Activating Server context and switching away from Client"); - ss.activate_process_thread(tid, server_pid, server_tid, false) - .map(|_| Ok(xous_kernel::Result::MessageEnvelope(envelope))) - .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) + let message = envelope.take_message(); + match ss.activate_process_thread( + tid, + server_pid, + server_tid, + false, + PostActivateOp::RememberServerMessage(sidx, pid, tid, &message, client_address), + ) { + Ok(sender_idx) => { + let sender = SenderID::new(sidx, sender_idx, Some(pid)); + let envelope = MessageEnvelope { sender: sender.into(), body: message }; + Ok(xous_kernel::Result::MessageEnvelope(envelope)) + } + _ => Err(xous_kernel::Error::ProcessNotFound), + } } } else if blocking && !cfg!(baremetal) { + // re-bind the envelope: lazy eval has to happen now. + let message = envelope.take_message(); + let sender_idx = + ss.remember_server_message(sidx, pid, tid, &message, client_address).map_err(|e| { + klog!("error remembering server message: {:?}", e); + ss.server_from_sidx_mut(sidx) + .expect("server couldn't be located") + .return_available_thread(server_tid); + e + })?; + let sender = SenderID::new(sidx, sender_idx, Some(pid)); + let envelope = MessageEnvelope { sender: sender.into(), body: message }; + klog!("Blocking client, since it sent a blocking message"); ss.unschedule_thread(pid, tid)?; ss.switch_to_thread(server_pid, Some(server_tid))?; @@ -262,7 +291,7 @@ fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult let ppid = process.ppid; unsafe { SWITCHTO_CALLER = None }; let result = ss - .activate_process_thread(tid, ppid, 0, false) + .activate_process_thread(tid, ppid, 0, false, PostActivateOp::None) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)); @@ -590,13 +619,16 @@ fn reply_and_receive_next( } else { // For baremetal targets, switch away from this process. if cfg!(baremetal) { - // 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)) + // Set the thread result for the client and activate the client thread and switch to it + ss.activate_process_thread( + server_tid, + response.pid, + response.tid, + false, + crate::services::PostActivateOp::SetThreadResult(response.result), + ) + .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. @@ -650,7 +682,7 @@ fn receive_message(pid: PID, tid: TID, sid: SID, blocking: ExecutionType) -> Sys let ppid = ss.get_process(pid).expect("Can't get current process").ppid; // TODO: Advance thread let result = ss - .activate_process_thread(tid, ppid, 0, false) + .activate_process_thread(tid, ppid, 0, false, PostActivateOp::None) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)); ss.set_last_thread(PID::new(ORIGINAL_PID.load(Relaxed)).unwrap(), ORIGINAL_TID.load(Relaxed)) @@ -841,7 +873,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR // "Activating process thread {} in pid {} coming from pid {} thread {}", // new_context, new_pid, pid, tid // ); - let new_tid = ss.activate_process_thread(tid, new_pid, new_tid, true)?; + let new_tid = ss.activate_process_thread(tid, new_pid, new_tid, true, PostActivateOp::None)?; ORIGINAL_PID.store(new_pid.get(), Relaxed); ORIGINAL_TID.store(new_tid, Relaxed); Ok(xous_kernel::Result::ResumeProcess) @@ -870,7 +902,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR // TODO: Advance thread if cfg!(baremetal) { let result = ss - .activate_process_thread(tid, ppid, 0, false) + .activate_process_thread(tid, ppid, 0, false, PostActivateOp::None) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)); ss.set_last_thread(PID::new(ORIGINAL_PID.load(Relaxed)).unwrap(), ORIGINAL_TID.load(Relaxed))