From 1261d73451901d673a9e9b67221b59c6ab2304f1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 7 Apr 2022 01:13:49 +0800 Subject: [PATCH] remove execution gate server and use deferred callback idiom instead reduces one connection on all PIDs that require suspend/resume callbacks; lightens kernel by about 16k runtime load --- services/susres/src/api.rs | 9 ++--- services/susres/src/lib.rs | 12 +++---- services/susres/src/main.rs | 66 +++++++++++++++++-------------------- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/services/susres/src/api.rs b/services/susres/src/api.rs index e4dbb825f..b44439120 100644 --- a/services/susres/src/api.rs +++ b/services/susres/src/api.rs @@ -4,7 +4,6 @@ use utralib::generated::*; pub(crate) const SERVER_NAME_SUSRES: &str = "_Suspend/resume manager_"; -pub(crate) const SERVER_NAME_EXEC_GATE: &str = "_Suspend/resume execution gate_"; /// Note: there must be at least one subscriber to the `Last` suspend order event, otherwise /// the logic will never terminate. There may be multiple `Last` subscribers, but the order at @@ -61,6 +60,9 @@ pub(crate) enum Opcode { /// not tested - reboot address RebootVector, //(u32), + /// used by processes to indicate they are suspending now; this blocks until resume using the "execution gate" + SuspendingNow, + /// exit the server Quit, } @@ -79,11 +81,6 @@ pub(crate) enum SuspendEventCallback { Drop, } -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -pub(crate) enum ExecGateOpcode { - SuspendingNow, - Drop, -} ///////////////////////////////////////////////////////////////////////// /////////////////////////// suspend-resume hardware management primitives diff --git a/services/susres/src/lib.rs b/services/susres/src/lib.rs index 87a6c7b63..2c9423bee 100644 --- a/services/susres/src/lib.rs +++ b/services/susres/src/lib.rs @@ -14,14 +14,12 @@ use num_traits::{ToPrimitive, FromPrimitive}; pub struct Susres { conn: CID, suspend_cb_sid: Option, - execution_gate_conn: CID, } impl Susres { #[cfg(any(target_os = "none", target_os = "xous"))] pub fn new(order: Option, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_SUSRES).expect("Can't connect to SUSRES"); - let execution_gate_conn = xns.request_connection_blocking(api::SERVER_NAME_EXEC_GATE).expect("Can't connect to the execution gate"); let sid = xous::create_server().unwrap(); let sid_tuple = sid.to_u32(); @@ -39,7 +37,6 @@ impl Susres { Ok(Susres { conn, suspend_cb_sid: Some(sid), - execution_gate_conn, }) } // suspend/resume is not implemented in hosted mode, and will break if you try to do it. @@ -53,7 +50,6 @@ impl Susres { Ok(Susres { conn: 0, suspend_cb_sid: None, - execution_gate_conn: 0, }) } pub fn conn(&self) -> CID { self.conn } @@ -64,7 +60,6 @@ impl Susres { Ok(Susres { conn, suspend_cb_sid: None, - execution_gate_conn: 0, }) } @@ -86,9 +81,12 @@ impl Susres { Message::new_scalar(Opcode::SuspendReady.to_usize().unwrap(), token, 0, 0, 0) ).map(|_|())?; log::trace!("blocking until suspend"); + + // sometime between here and when this next message unblocks, the power went out... + // now block until we've resumed - send_message(self.execution_gate_conn, - Message::new_blocking_scalar(ExecGateOpcode::SuspendingNow.to_usize().unwrap(), 0, 0, 0, 0) + send_message(self.conn, + Message::new_blocking_scalar(Opcode::SuspendingNow.to_usize().unwrap(), 0, 0, 0, 0) ).map(|_|())?; let response = send_message(self.conn, diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index 978e29f2a..7ca5b6af5 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -6,7 +6,7 @@ mod murmur3; mod api; -use api::{Opcode, ScalarHook, SuspendEventCallback, ExecGateOpcode}; +use api::{Opcode, ScalarHook, SuspendEventCallback}; use num_traits::{ToPrimitive, FromPrimitive}; use xous_ipc::Buffer; @@ -530,35 +530,6 @@ pub fn timeout_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { } static SHOULD_RESUME: AtomicBool = AtomicBool::new(false); -static RESUME_EXEC: AtomicBool = AtomicBool::new(false); -pub fn execution_gate() { - let xns = xous_names::XousNames::new().unwrap(); - let execgate_sid = xns.register_name(api::SERVER_NAME_EXEC_GATE, None).expect("can't register execution gate"); - log::trace!("execution_gate registered with NS -- {:?}", execgate_sid); - - loop { - let msg = xous::receive_message(execgate_sid).unwrap(); - match FromPrimitive::from_usize(msg.body.id()) { - // the entire purpose of SupendingNow is to block the thread that sent the message, until we're ready to resume. - Some(ExecGateOpcode::SuspendingNow) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { - println!("checking exec gate:"); - while !RESUME_EXEC.load(Ordering::Relaxed) { - println!("execution gate active"); - xous::yield_slice(); - } - println!("execution is ungated!"); - xous::return_scalar(msg.sender, 0).expect("couldn't return dummy message to unblock execution"); - }), - Some(ExecGateOpcode::Drop) => { - break; - } - None => { - log::error!("received unknown opcode in execution_gate"); - } - } - } - xous::destroy_server(execgate_sid).unwrap(); -} #[xous::xous_main] fn xmain() -> ! { @@ -578,9 +549,6 @@ fn xmain() -> ! { // so this is the only way to debug what's going on. println!("App UART debug printing is on!"); - // start up the execution gate - xous::create_thread_0(execution_gate).unwrap(); - let xns = xous_names::XousNames::new().unwrap(); // unlimited connections allowed let susres_sid = xns.register_name(api::SERVER_NAME_SUSRES, None).expect("can't register server"); @@ -603,6 +571,8 @@ fn xmain() -> ! { let mut suspend_subscribers = Vec::::new(); let mut current_op_order = crate::api::SuspendOrder::Early; + + let mut gated_pids = Vec::::new(); loop { let msg = xous::receive_message(susres_sid).unwrap(); if reboot_requested { @@ -634,6 +604,16 @@ fn xmain() -> ! { let hookdata = buffer.to_original::().unwrap(); do_hook(hookdata, &mut suspend_subscribers); }, + Some(Opcode::SuspendingNow) => { + if !suspend_requested { + // this is harmless, it means a process' execution gate came a bit later than expected, so just ignore and tell it to resume + // the execution gate is only requested until *after* a process has checked in and said it is ready to suspend, anyways. + log::warn!("exec gate message received late, ignoring"); + xous::return_scalar(msg.sender, 0).expect("couldn't return dummy message to unblock execution"); + } else { + gated_pids.push(msg.sender); + } + }, Some(Opcode::SuspendReady) => msg_scalar_unpack!(msg, token, _, _, _, { //log::trace!("suspendready with token {}", token); if !suspend_requested { @@ -674,6 +654,11 @@ fn xmain() -> ! { timeout_pending = false; // susres_hw.debug_delay(500); // let the messages print susres_hw.do_suspend(false); + + // ---- power turns off ---- + // ---- time passes while we are asleep ---- + // ---- omg power came back! --- + // when do_suspend() returns, it means we've resumed suspend_requested = false; log_server::resume(); // log server is a special case, in order to avoid circular dependencies @@ -682,7 +667,9 @@ fn xmain() -> ! { } // this now allows all other threads to commence log::trace!("low-level resume done, restoring execution"); - RESUME_EXEC.store(true, Ordering::Relaxed); + for pid in gated_pids.drain(..) { + xous::return_scalar(pid, 0).expect("couldn't return dummy message to unblock execution"); + } susres_hw.restore_wfi(); } else if all_ready { log::debug!("finished with {:?} going to next round", current_op_order); @@ -716,7 +703,6 @@ fn xmain() -> ! { suspend_requested = true; // clear the resume gate SHOULD_RESUME.store(false, Ordering::Relaxed); - RESUME_EXEC.store(false, Ordering::Relaxed); // clear the ready to suspend flag and failed to suspend flag for sub in suspend_subscribers.iter_mut() { sub.ready_to_suspend = false; @@ -755,6 +741,11 @@ fn xmain() -> ! { // susres_hw.debug_delay(500); // let the messages print // force a suspend susres_hw.do_suspend(true); + + // ---- power turns off ---- + // ---- time passes while we are asleep ---- + // ---- omg power came back! --- + // when do_suspend() returns, it means we've resumed suspend_requested = false; log_server::resume(); // log server is a special case, in order to avoid circular dependencies @@ -763,7 +754,10 @@ fn xmain() -> ! { } else { log::error!("We forced a suspend, but the bootloader is claiming we did a clean suspend. Internal state may be inconsistent."); } - RESUME_EXEC.store(true, Ordering::Relaxed); + for pid in gated_pids.drain(..) { + xous::return_scalar(pid, 0).expect("couldn't return dummy message to unblock execution"); + } + susres_hw.restore_wfi(); } else { log::trace!("clean suspend timeout received, ignoring"); // this means we did a clean suspend, we've resumed, and the timeout came back after the resume