diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index f9019105e..2b2b969f0 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -54,4 +54,7 @@ u64_pa = [] # the page table when being updated in the case of a power loss, in exchange for more than doubling the time it # takes to update the page table. mbbb = [] -default = ["mbbb"] +# selecting deterministic makes the allocation and page replacement algorithms deterministic. normally we don't want +# this but it's helpful for debugging. +deterministic = [] +default = ["mbbb", "deterministic"] diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index d123c2767..4e5b48c1a 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -10,11 +10,19 @@ use core::ops::{Deref, DerefMut}; use core::mem::size_of; use std::collections::HashMap; +#[cfg(not(feature="deterministic"))] use std::collections::HashSet; +#[cfg(feature="deterministic")] +use std::collections::BTreeSet; use std::collections::BinaryHeap; use std::cmp::Reverse; use std::io::{Result, Error, ErrorKind}; +#[cfg(not(feature="deterministic"))] +type FspaceSet = HashSet::; +#[cfg(feature="deterministic")] +type FspaceSet = BTreeSet::; + /// Implementation-specific PDDB structures: for Precursor/Xous OS pair pub(crate) const MBBB_PAGES: usize = 10; pub(crate) const FSCB_PAGES: usize = 16; @@ -72,7 +80,7 @@ pub(crate) struct PddbOs { /// derived cipher for handling fastspace -- cache it, so we can save the time cost of constructing the cipher key schedule cipher_ecb: Option, /// fast space cache - fspace_cache: HashSet, + fspace_cache: FspaceSet, /// memoize the location of the fscb log pages fspace_log_addrs: Vec::, /// memoize the current target offset for the next log entry @@ -117,7 +125,7 @@ impl PddbOs { data_phys_base: PageAlignedPa::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32), system_basis_key: None, cipher_ecb: None, - fspace_cache: HashSet::::new(), + fspace_cache: FspaceSet::new(), fspace_log_addrs: Vec::::new(), fspace_log_next_addr: None, fspace_log_len: 0, @@ -139,7 +147,7 @@ impl PddbOs { data_phys_base: PageAlignedPa::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32), system_basis_key: None, cipher_ecb: None, - fspace_cache: HashSet::::new(), + fspace_cache: FspaceSet::new(), fspace_log_addrs: Vec::::new(), fspace_log_next_addr: None, fspace_log_len: 0, @@ -182,7 +190,7 @@ impl PddbOs { #[cfg(not(any(target_os = "none", target_os = "xous")))] /// used to reset the hardware structure for repeated runs of testing within a single invocation pub fn test_reset(&mut self) { - self.fspace_cache = HashSet::::new(); + self.fspace_cache = FspaceSet::new(); self.fspace_log_addrs = Vec::::new(); self.system_basis_key = None; self.cipher_ecb = None; diff --git a/services/pddb/src/backend/types.rs b/services/pddb/src/backend/types.rs index 86a728bc1..d16591214 100644 --- a/services/pddb/src/backend/types.rs +++ b/services/pddb/src/backend/types.rs @@ -4,6 +4,7 @@ use super::{PAGE_SIZE, VPAGE_SIZE}; use crate::SpaceState; use bitfield::bitfield; use std::hash::{Hash, Hasher}; +use std::cmp::Ordering; /// for the life of me, I can't figure out how to query the AES crate to give me the length of a 256-bit key. /// I mean, we know what it is, it's well-defined and never changes. But it'd just be nice to you know, @@ -52,6 +53,16 @@ impl PartialEq for PhysPage { self.page_number() == other.page_number() } } +impl Ord for PhysPage { + fn cmp(&self, other: &Self) -> Ordering { + self.page_number().cmp(&other.page_number()) + } +} +impl PartialOrd for PhysPage { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} /// Storage for journal revisions. pub type JournalType = u32; diff --git a/services/pddb/src/tests.rs b/services/pddb/src/tests.rs index 531d125f0..598ce9fb0 100644 --- a/services/pddb/src/tests.rs +++ b/services/pddb/src/tests.rs @@ -2,7 +2,7 @@ use rand::prelude::*; use rand_chacha::ChaCha8Rng; use crate::*; use core::sync::atomic::{AtomicU64, Ordering}; -use std::collections::{BTreeSet, BTreeMap}; +use std::collections::BTreeSet; const UPPER_BOUND: usize = 9000; const LOWER_BOUND: usize = 12; // needs to be big enough to compute murmur3 hash + hold checksum @@ -91,11 +91,18 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((LOWER_BOUND, UPPER_BOUND - 4)); let extra_reserved = maybe_extra_reserved.unwrap_or(0); - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); + let dict_start_index = dict_list.len(); for (evicted, evict_dict) in dict_list.iter().enumerate() { if evicted < evict_count { @@ -134,17 +141,27 @@ pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache, let patch_offset = maybe_patch_offset.unwrap_or(5); let patch_data = maybe_patch_data.unwrap_or("patched!".to_string()); - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); for dict in dict_list.iter() { if let Ok(key_list_unord) = basis_cache.key_list(hw, dict, None) { + #[cfg(feature = "deterministic")] let mut key_list = BTreeSet::::new(); + #[cfg(feature = "deterministic")] for s in key_list_unord { key_list.insert(s); } + #[cfg(not(feature = "deterministic"))] + let key_list = key_list_unord; for key in key_list.iter() { // this actually does something a bit more complicated on a multi-basis system than you'd think: // it will get the union of all key names, and then patch the *latest open basis* only with new data. @@ -203,11 +220,17 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, let extra_reserved = maybe_extra_reserved.unwrap_or(0); let mut evicted_dicts = Vec::::new(); - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); let mut evict_iter = 0; for (evicted, evict_dict) in dict_list.iter().enumerate() { if evicted < evict_count { @@ -230,11 +253,17 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, evicted_dicts.push(evict_dict.to_string()); evict_iter += 1; } - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); for dict in dict_list.iter() { let da = basis_cache.dict_attributes(hw, dict, None).unwrap(); log::debug!("{:?}", da); @@ -256,11 +285,17 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, } } } - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); for dict in dict_list.iter() { let da = basis_cache.dict_attributes(hw, dict, None).unwrap(); log::debug!("{:?}", da); @@ -271,11 +306,17 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, log::info!("all-basis sync done"); list_all(hw, basis_cache); - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); for dict in dict_list.iter() { let da = basis_cache.dict_attributes(hw, dict, None).unwrap(); log::info!("{:?}", da); @@ -283,11 +324,17 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, } pub(crate) fn list_all(hw: &mut PddbOs, basis_cache: &mut BasisCache) { - let dict_list_unord = basis_cache.dict_list(hw, None); + #[cfg(feature = "deterministic")] let mut dict_list = BTreeSet::::new(); - for s in dict_list_unord { - dict_list.insert(s); + #[cfg(feature = "deterministic")] + { + let dict_list_unord = basis_cache.dict_list(hw, None); + for s in dict_list_unord { + dict_list.insert(s); + } } + #[cfg(not(feature = "deterministic"))] + let dict_list = basis_cache.dict_list(hw, None); // now list all the attributes of the basis for dict in dict_list.iter() {