From b2b31a27139bd4e202ae30f3ee10f63c3aa1b9e1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 13 May 2024 17:26:38 +0800 Subject: [PATCH] cleanup and re-org code --- services/xous-swapper/src/main.rs | 64 ++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index a19407c08..ac6aa2351 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -73,6 +73,7 @@ use num_traits::*; use platform::{SwapHal, PAGE_SIZE}; use xous::{MemoryFlags, Message, Result, CID, PID, SID}; +/// userspace swapper -> kernel ABI /// This ABI is copy-paste synchronized with what's in the kernel. It's left out of /// xous-rs so that we can change it without having to push crates to crates.io. /// Since there is only one place the ABI could be used, we're going to stick with @@ -84,6 +85,7 @@ pub enum SwapAbi { Evict = 1, GetFreeMem = 2, FetchAllocs = 3, + SetOomThresh = 4, } /// SYNC WITH `kernel/src/swap.rs` impl SwapAbi { @@ -93,11 +95,41 @@ impl SwapAbi { 1 => Evict, 2 => GetFreeMem, 3 => FetchAllocs, + 4 => SetOomThresh, _ => Invalid, } } } +/// kernel -> swapper handler ABI +/// This structure mirrors the BlockingSwapOp's that the kernel can issue to userspace. +/// The actual numbers for the opcode are transcribed manually into the kernel, as the +/// kernel's encoding of its enum is composite to track call state. +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +#[repr(usize)] +pub enum KernelOp { + /// Take the page lent to us, encrypt it and write it to swap + WriteToSwap = 0, + /// Find the requested page, decrypt it, and return it + ReadFromSwap = 1, + /// Kernel message advising us that a page of RAM was allocated + ExecFetchAllocs = 2, + /// OOM warning from the kernel + OomDoom = 3, +} + +/// public userspace & swapper handler -> swapper userspace ABI +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +#[repr(usize)] +pub enum Opcode { + /// Trigger back to userspace to indicate that alloc fetching is done. + FetchAllocsDone, + /// Trigger the OOM routine + HandleOomDoom, + /// Test messages + Test0, +} + pub struct PtPage { pub entries: [u32; 1024], } @@ -115,29 +147,6 @@ pub struct SwapCountTracker { pub counts: &'static mut [u32], } -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -#[repr(usize)] -pub enum KernelOp { - /// Take the page lent to us, encrypt it and write it to swap - WriteToSwap = 0, - /// Find the requested page, decrypt it, and return it - ReadFromSwap = 1, - /// Kernel message advising us that a page of RAM was allocated - ExecFetchAllocs = 2, - /// Test messages - Test0 = 128, - /// A message from the kernel handler context to evaluate if a trim is needed - EvalTrim = 256, -} - -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -#[repr(usize)] -pub enum Opcode { - /// Trigger back to userspace to indicate that alloc fetching is done. - FetchAllocsDone, - /// Test messages - Test0, -} /// This structure contains shared state accessible between the userspace code and the blocking swap call /// handler. pub struct SwapperSharedState { @@ -437,6 +446,15 @@ fn main() { // handle for the current sorted vector of swap-out candidates let mut swap_candidates: Option> = None; + /// Threshold for OomDoom callback + const OOM_THRESH_PAGES: usize = 16; + /// Target of free pages we want to get to after OomDoom call + const FREE_PAGE_TARGET: usize = 32; + // set OOM threshold - first argument is the threshold, in pages, below which we ping the swapper to start + // clearing memory + xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::SetOomThresh as usize, OOM_THRESH_PAGES, 0, 0, 0, 0, 0)) + .ok(); + thread::spawn({ let conn = conn.clone(); move || {