From be6ff50d7f478205493654d985fd09bdadfd7a92 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Thu, 19 Apr 2018 12:42:24 +0800 Subject: [PATCH] Test framework for FifoSwapManager. Not passed. --- crate/memory/src/swap/fifo.rs | 64 +++++++++++++++++++++++++++++------ crate/memory/src/swap/mod.rs | 2 +- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/crate/memory/src/swap/fifo.rs b/crate/memory/src/swap/fifo.rs index 7e92951..8a91f52 100644 --- a/crate/memory/src/swap/fifo.rs +++ b/crate/memory/src/swap/fifo.rs @@ -1,13 +1,15 @@ use alloc::vec_deque::VecDeque; use super::*; -struct FifoSwapManager { +struct FifoSwapManager { + page_table: &'static T, deque: VecDeque, } -impl SwapManager for FifoSwapManager { - fn new(_page_table: &T) -> Self { +impl SwapManager for FifoSwapManager { + fn new(page_table: &'static T) -> Self { FifoSwapManager { + page_table, deque: VecDeque::::new() } } @@ -35,16 +37,56 @@ impl SwapManager for FifoSwapManager { #[cfg(test)] mod test { use super::*; + use super::mock_page_table::MockPageTable; + + enum MemOp { + R(usize), W(usize) + } + + fn assert_pgfault_eq(x: usize) { + assert_eq!(unsafe{ PGFAULT_COUNT }, x); + } + + // For pgfault_handler: + static mut PGFAULT_COUNT: usize = 0; + static mut PAGE: *mut MockPageTable = 0 as *mut _; + static mut FIFO: *mut FifoSwapManager = 0 as *mut _; + + fn pgfault_handler(pt: &mut MockPageTable, addr: Addr) { + unsafe{ PGFAULT_COUNT += 1; } + let fifo = unsafe{ &mut *FIFO }; + if let Some(x) = fifo.swap() { + pt.unmap(x); + } + pt.map(addr); + } #[test] fn test() { -// let mut pt = mock_page_table::MockPageTable::new(); -// let mut sm = FifoSwapManager::new(); -// let write_seq = [3, 1, 4, 2, 5, 2, 1, 2, 3, 4, 5, 1, 1]; -// let pgfault_count = [4, 4, 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, 11]; -// for i in write_seq { -// pt.write(i); -// } - + use self::MemOp::{R, W}; + let mut pt = MockPageTable::new(pgfault_handler); + let mut fifo = FifoSwapManager::::new( + unsafe{ &*(&pt as *const _) }); + unsafe { + PAGE = &mut pt as *mut _; + FIFO = &mut fifo as *mut _; + } + let op_seq = [ + R(1), R(2), R(3), R(4), + W(3), W(1), W(4), W(2), W(5), + W(2), W(1), W(2), W(3), W(4), + W(5), R(1), W(1)]; + let pgfault_count = [ + 1, 2, 3, 4, + 4, 4, 4, 4, 5, + 5, 6, 7, 8, 9, + 10, 11, 11]; + for (op, &count) in op_seq.iter().zip(pgfault_count.iter()) { + match op { + R(addr) => pt.read(*addr), + W(addr) => pt.write(*addr), + } + assert_pgfault_eq(count); + } } } \ No newline at end of file diff --git a/crate/memory/src/swap/mod.rs b/crate/memory/src/swap/mod.rs index 8a65921..badc394 100644 --- a/crate/memory/src/swap/mod.rs +++ b/crate/memory/src/swap/mod.rs @@ -5,7 +5,7 @@ type Addr = usize; trait SwapManager { /// Create and initialize for the swap manager - fn new(page_table: &T) -> Self; + fn new(page_table: &'static T) -> Self; /// Called when tick interrupt occured fn tick(&mut self); /// Called when map a swappable page into the memory