mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
add PDDB backpressure items (#109 partial)
This commit is contained in:
parent
6793f9c5da
commit
87efc267f1
3 changed files with 166 additions and 10 deletions
|
|
@ -144,6 +144,7 @@ use std::iter::IntoIterator;
|
||||||
use std::collections::{BinaryHeap, HashMap, HashSet};
|
use std::collections::{BinaryHeap, HashMap, HashSet};
|
||||||
use std::io::{Result, Error, ErrorKind};
|
use std::io::{Result, Error, ErrorKind};
|
||||||
use std::cmp::Reverse;
|
use std::cmp::Reverse;
|
||||||
|
use std::cmp::Ordering;
|
||||||
use core::num::NonZeroU32;
|
use core::num::NonZeroU32;
|
||||||
|
|
||||||
pub(crate) const SMALL_POOL_START: u64 = 0x0000_003F_8000_0000;
|
pub(crate) const SMALL_POOL_START: u64 = 0x0000_003F_8000_0000;
|
||||||
|
|
@ -245,10 +246,15 @@ impl DerefMut for BasisRoot {
|
||||||
pub(crate) struct BasisCache {
|
pub(crate) struct BasisCache {
|
||||||
/// the cache entries themselves
|
/// the cache entries themselves
|
||||||
cache: Vec::<BasisCacheEntry>,
|
cache: Vec::<BasisCacheEntry>,
|
||||||
|
/// ticktimer reference, for managing atimes
|
||||||
|
pub(crate) tt: ticktimer_server::Ticktimer,
|
||||||
}
|
}
|
||||||
impl BasisCache {
|
impl BasisCache {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
BasisCache { cache: Vec::new() }
|
BasisCache {
|
||||||
|
cache: Vec::new(),
|
||||||
|
tt: ticktimer_server::Ticktimer::new().unwrap(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn select_basis(&mut self, basis_name: Option<&str>) -> Option<usize> {
|
fn select_basis(&mut self, basis_name: Option<&str>) -> Option<usize> {
|
||||||
if self.cache.len() == 0 {
|
if self.cache.len() == 0 {
|
||||||
|
|
@ -314,6 +320,7 @@ impl BasisCache {
|
||||||
small_pool: Vec::<KeySmallPool>::new(),
|
small_pool: Vec::<KeySmallPool>::new(),
|
||||||
small_pool_free: BinaryHeap::<KeySmallPoolOrd>::new(),
|
small_pool_free: BinaryHeap::<KeySmallPoolOrd>::new(),
|
||||||
aad: my_aad,
|
aad: my_aad,
|
||||||
|
tt: ticktimer_server::Ticktimer::new().unwrap(),
|
||||||
};
|
};
|
||||||
log::debug!("adding dictionary {}", name);
|
log::debug!("adding dictionary {}", name);
|
||||||
basis.dicts.insert(String::from(name), dict_cache);
|
basis.dicts.insert(String::from(name), dict_cache);
|
||||||
|
|
@ -418,6 +425,7 @@ impl BasisCache {
|
||||||
if let Some(dict_entry) = basis.dicts.get_mut(dict) {
|
if let Some(dict_entry) = basis.dicts.get_mut(dict) {
|
||||||
if dict_entry.ensure_key_entry(hw, &mut basis.v2p_map, &basis.cipher, key) {
|
if dict_entry.ensure_key_entry(hw, &mut basis.v2p_map, &basis.cipher, key) {
|
||||||
let kcache = dict_entry.keys.get_mut(key).expect("Entry was assured, but then not there!");
|
let kcache = dict_entry.keys.get_mut(key).expect("Entry was assured, but then not there!");
|
||||||
|
kcache.set_atime(self.tt.elapsed_ms());
|
||||||
// the key exists, *and* there's sufficient space for the data
|
// the key exists, *and* there's sufficient space for the data
|
||||||
if kcache.start < SMALL_POOL_END {
|
if kcache.start < SMALL_POOL_END {
|
||||||
// small pool fetch
|
// small pool fetch
|
||||||
|
|
@ -964,7 +972,97 @@ impl BasisCache {
|
||||||
self.basis_unmount(hw, &basis).ok();
|
self.basis_unmount(hw, &basis).ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns a relative measure of cache size. It is not absolutely accurate as
|
||||||
|
/// overhead is not accounted for, but the actual data cached is relatively correct.
|
||||||
|
pub(crate) fn cache_size(&mut self) -> usize {
|
||||||
|
let mut total_size = 0;
|
||||||
|
for basis in self.cache.iter() {
|
||||||
|
for dict in basis.dicts.values() {
|
||||||
|
for key in dict.keys.values() {
|
||||||
|
total_size += key.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total_size
|
||||||
|
}
|
||||||
|
/// attempts to prune `target_bytes` out of the cached data set
|
||||||
|
pub(crate) fn cache_prune(&mut self, hw: &mut PddbOs, target_bytes: usize) -> usize {
|
||||||
|
let mut pruned = 0;
|
||||||
|
// this does it a "dumb" way, but at least it's sort of obvious how it works
|
||||||
|
// 0. sync the basis and dictionaries to disk, so that removing cache entries are guaranteed not to be problematic.
|
||||||
|
// 1. iterate through all the known keys, creating a sorted heap of fully-specified keys (basis/dict/key)
|
||||||
|
// sorted by access time
|
||||||
|
// 2. evict keys with the oldest access time, until target_bytes is hit.
|
||||||
|
self.sync(hw, None).unwrap();
|
||||||
|
|
||||||
|
let mut candidates = BinaryHeap::new();
|
||||||
|
for basis in self.cache.iter() {
|
||||||
|
for (dict_name, dict) in basis.dicts.iter() {
|
||||||
|
for (key_name, key) in dict.keys.iter() {
|
||||||
|
candidates.push(Reverse(
|
||||||
|
KeyAge {
|
||||||
|
atime: key.atime(),
|
||||||
|
_size: key.size(),
|
||||||
|
key: key_name.to_string(),
|
||||||
|
dict: dict_name.to_string(),
|
||||||
|
basis: basis.name.to_string(),
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Some(Reverse(ka)) = candidates.pop() {
|
||||||
|
for basis in self.cache.iter_mut() {
|
||||||
|
if basis.name == ka.basis {
|
||||||
|
match basis.dicts.get_mut(&ka.dict) {
|
||||||
|
Some(dentry) => {
|
||||||
|
let freed = dentry.evict_keycache_entry(&ka.key);
|
||||||
|
log::debug!("pruned {} bytes from atime {} / total {}", freed, ka.atime, pruned);
|
||||||
|
pruned += freed;
|
||||||
|
if pruned >= target_bytes {
|
||||||
|
return pruned;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pruned
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Revise this to use references instead of allocations once we've refactored the interior mutability
|
||||||
|
// issues with the PDDB.
|
||||||
|
struct KeyAge {
|
||||||
|
atime: u64,
|
||||||
|
// currently unused but collected in case we want to use it in the future
|
||||||
|
_size: usize,
|
||||||
|
key: String,
|
||||||
|
dict: String,
|
||||||
|
basis: String,
|
||||||
|
}
|
||||||
|
impl Ord for KeyAge {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
self.atime.cmp(&other.atime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialOrd for KeyAge {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq for KeyAge {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.atime == other.atime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Eq for KeyAge {}
|
||||||
|
|
||||||
/// This is the RAM cached copy of a basis as maintained in the PDDB.
|
/// This is the RAM cached copy of a basis as maintained in the PDDB.
|
||||||
pub(crate) struct BasisCacheEntry {
|
pub(crate) struct BasisCacheEntry {
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,8 @@ pub(crate) struct DictCacheEntry {
|
||||||
pub(crate) small_pool_free: BinaryHeap<KeySmallPoolOrd>,
|
pub(crate) small_pool_free: BinaryHeap<KeySmallPoolOrd>,
|
||||||
/// copy of our AAD, for convenience
|
/// copy of our AAD, for convenience
|
||||||
pub(crate) aad: Vec::<u8>,
|
pub(crate) aad: Vec::<u8>,
|
||||||
|
/// ticktimer reference, for managing atimes
|
||||||
|
pub(crate) tt: ticktimer_server::Ticktimer,
|
||||||
}
|
}
|
||||||
impl DictCacheEntry {
|
impl DictCacheEntry {
|
||||||
pub fn new(dict: Dictionary, index: usize, aad: &Vec<u8>) -> DictCacheEntry {
|
pub fn new(dict: Dictionary, index: usize, aad: &Vec<u8>) -> DictCacheEntry {
|
||||||
|
|
@ -80,6 +82,7 @@ impl DictCacheEntry {
|
||||||
small_pool: Vec::<KeySmallPool>::new(),
|
small_pool: Vec::<KeySmallPool>::new(),
|
||||||
small_pool_free: BinaryHeap::<KeySmallPoolOrd>::new(),
|
small_pool_free: BinaryHeap::<KeySmallPoolOrd>::new(),
|
||||||
aad: my_aad,
|
aad: my_aad,
|
||||||
|
tt: ticktimer_server::Ticktimer::new().unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Populates cache entries, reporting the maximum extent of large alloc data seen so far.
|
/// Populates cache entries, reporting the maximum extent of large alloc data seen so far.
|
||||||
|
|
@ -157,6 +160,7 @@ impl DictCacheEntry {
|
||||||
descriptor_index: NonZeroU32::new(try_entry as u32).unwrap(),
|
descriptor_index: NonZeroU32::new(try_entry as u32).unwrap(),
|
||||||
clean: true,
|
clean: true,
|
||||||
data: None,
|
data: None,
|
||||||
|
atime: self.tt.elapsed_ms(),
|
||||||
};
|
};
|
||||||
let kname = std::str::from_utf8(&keydesc.name.data[..keydesc.name.len as usize]).expect("key is not valid utf-8");
|
let kname = std::str::from_utf8(&keydesc.name.data[..keydesc.name.len as usize]).expect("key is not valid utf-8");
|
||||||
let key_exists_and_valid =
|
let key_exists_and_valid =
|
||||||
|
|
@ -259,6 +263,7 @@ impl DictCacheEntry {
|
||||||
descriptor_index: NonZeroU32::new(try_entry as u32).unwrap(),
|
descriptor_index: NonZeroU32::new(try_entry as u32).unwrap(),
|
||||||
clean: true,
|
clean: true,
|
||||||
data: None,
|
data: None,
|
||||||
|
atime: self.tt.elapsed_ms(),
|
||||||
};
|
};
|
||||||
self.keys.insert(kname.to_string(), kcache);
|
self.keys.insert(kname.to_string(), kcache);
|
||||||
self.try_fill_small_key(hw, v2p_map, cipher, &mut data_cache, &kname);
|
self.try_fill_small_key(hw, v2p_map, cipher, &mut data_cache, &kname);
|
||||||
|
|
@ -297,13 +302,25 @@ impl DictCacheEntry {
|
||||||
}
|
}
|
||||||
let ksp = &mut self.small_pool[pool_index];
|
let ksp = &mut self.small_pool[pool_index];
|
||||||
if !ksp.clean {
|
if !ksp.clean {
|
||||||
ksp.contents.push(key_name.to_string());
|
if !ksp.contents.contains(&key_name.to_string()) {
|
||||||
assert!(kcache.reserved >= kcache.len, "Reserved amount is less than length, this is an error!");
|
log::trace!("creating ksp entry for {}", key_name);
|
||||||
assert!(kcache.reserved <= VPAGE_SIZE as u64, "Reserved amount is not appropriate for the small pool. Logic error in prior PDDB operation!");
|
ksp.contents.push(key_name.to_string());
|
||||||
log::trace!("avail: {} reserved: {}", ksp.avail, kcache.reserved);
|
assert!(kcache.reserved >= kcache.len, "Reserved amount is less than length, this is an error!");
|
||||||
assert!((ksp.avail as u64) >= kcache.reserved, "Total amount allocated to a small pool chunk is incorrect; suspect logic error in prior PDDB operation!");
|
assert!(kcache.reserved <= VPAGE_SIZE as u64, "Reserved amount is not appropriate for the small pool. Logic error in prior PDDB operation!");
|
||||||
ksp.avail -= kcache.reserved as u16;
|
log::trace!("avail: {} reserved: {}", ksp.avail, kcache.reserved);
|
||||||
// note: small_pool_free is updated only after all the entries have been read in
|
assert!((ksp.avail as u64) >= kcache.reserved, "Total amount allocated to a small pool chunk is incorrect; suspect logic error in prior PDDB operation!");
|
||||||
|
ksp.avail -= kcache.reserved as u16;
|
||||||
|
// note: small_pool_free is updated only after all the entries have been read in
|
||||||
|
} else {
|
||||||
|
// if the entry was previously created, don't update the metadata.
|
||||||
|
// This might not be the best place for this note, but, as a reminder to myself,
|
||||||
|
// the small pool code is written assuming you scan through all the keys and fill it
|
||||||
|
// into RAM at least once. Once you've done that, you can evict cached data to free up some
|
||||||
|
// space, but it's sort of dumb that you have to do that in the first place. This is going to
|
||||||
|
// lead to a crisis once the small pool data exceeds the available RAM -- which isn't too far from now.
|
||||||
|
// This will be tracked in issue #109.
|
||||||
|
log::trace!("refilling data only for {}", key_name);
|
||||||
|
}
|
||||||
|
|
||||||
// now grab the *data* referred to by this key. Maybe this is a "bad" idea -- this can really eat up RAM fast to hold
|
// now grab the *data* referred to by this key. Maybe this is a "bad" idea -- this can really eat up RAM fast to hold
|
||||||
// all the small pool data right away, but let's give it a try and see how it works. Later on we can always skip this.
|
// all the small pool data right away, but let's give it a try and see how it works. Later on we can always skip this.
|
||||||
|
|
@ -357,6 +374,7 @@ impl DictCacheEntry {
|
||||||
self.clean = false;
|
self.clean = false;
|
||||||
if self.ensure_key_entry(hw, v2p_map, cipher, name) {
|
if self.ensure_key_entry(hw, v2p_map, cipher, name) {
|
||||||
let kcache = self.keys.get_mut(name).expect("Entry was assured, but then not there!");
|
let kcache = self.keys.get_mut(name).expect("Entry was assured, but then not there!");
|
||||||
|
kcache.set_atime(self.tt.elapsed_ms());
|
||||||
kcache.clean = false;
|
kcache.clean = false;
|
||||||
// the update isn't going to fit in the reserved space, remove it, and re-insert it with an entirely new entry.
|
// the update isn't going to fit in the reserved space, remove it, and re-insert it with an entirely new entry.
|
||||||
if kcache.reserved < (data.len() + offset) as u64 {
|
if kcache.reserved < (data.len() + offset) as u64 {
|
||||||
|
|
@ -620,7 +638,8 @@ impl DictCacheEntry {
|
||||||
data: Some(KeyCacheData::Small(KeySmallData{
|
data: Some(KeyCacheData::Small(KeySmallData{
|
||||||
clean: false,
|
clean: false,
|
||||||
data: alloc_data
|
data: alloc_data
|
||||||
}))
|
})),
|
||||||
|
atime: self.tt.elapsed_ms(),
|
||||||
};
|
};
|
||||||
self.keys.insert(name.to_string(), kcache);
|
self.keys.insert(name.to_string(), kcache);
|
||||||
self.key_count += 1;
|
self.key_count += 1;
|
||||||
|
|
@ -649,6 +668,7 @@ impl DictCacheEntry {
|
||||||
descriptor_index,
|
descriptor_index,
|
||||||
clean: false,
|
clean: false,
|
||||||
data: None, // no caching implemented yet for large keys
|
data: None, // no caching implemented yet for large keys
|
||||||
|
atime: self.tt.elapsed_ms(),
|
||||||
};
|
};
|
||||||
self.keys.insert(name.to_string(), kcache);
|
self.keys.insert(name.to_string(), kcache);
|
||||||
self.key_count += 1;
|
self.key_count += 1;
|
||||||
|
|
@ -668,7 +688,7 @@ impl DictCacheEntry {
|
||||||
Ok(large_alloc_ptr)
|
Ok(large_alloc_ptr)
|
||||||
}
|
}
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn key_contains(&mut self, name: &str) -> bool {
|
pub fn key_contains(&self, name: &str) -> bool {
|
||||||
self.keys.contains_key(&String::from(name))
|
self.keys.contains_key(&String::from(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -969,6 +989,27 @@ impl DictCacheEntry {
|
||||||
basis: basis_name.to_string(),
|
basis: basis_name.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// removes a key cache entry from a dictionary cache, returning the amount of space liberated with
|
||||||
|
/// the eviction. 0 means the key was either 0-sized, or it could not be evicted (possibly
|
||||||
|
/// because it was dirty; you need to call sync before evicting anything from the cache)
|
||||||
|
pub(crate) fn evict_keycache_entry(&mut self, key: &str) -> usize {
|
||||||
|
if let Some(kcache) = self.keys.get_mut(key) {
|
||||||
|
// if the cache entry is dirty, abort
|
||||||
|
if kcache.flags.valid() && !kcache.clean {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if kcache.data.is_some() {
|
||||||
|
let pruned = kcache.size();
|
||||||
|
kcache.data.take(); // this effectively frees up the key cache data
|
||||||
|
pruned
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// converts a dictionary index -- which is a 1-offset number of dictionaries -- plus a key
|
/// converts a dictionary index -- which is a 1-offset number of dictionaries -- plus a key
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,8 @@ pub(crate) struct KeyCacheEntry {
|
||||||
pub(crate) reserved: u64,
|
pub(crate) reserved: u64,
|
||||||
pub(crate) flags: KeyFlags,
|
pub(crate) flags: KeyFlags,
|
||||||
pub(crate) age: u32,
|
pub(crate) age: u32,
|
||||||
|
/// this is an ephemeral time since boot, not commited to disk, used only for deciding which entries to evict
|
||||||
|
pub(crate) atime: u64,
|
||||||
/// the current on-disk index of the KeyCacheEntry item, enumerated as "0" being the Dict descriptor and
|
/// the current on-disk index of the KeyCacheEntry item, enumerated as "0" being the Dict descriptor and
|
||||||
/// "1" being the first valid key. This is used to find the location of the key's metadata as stored on disk;
|
/// "1" being the first valid key. This is used to find the location of the key's metadata as stored on disk;
|
||||||
/// it has nothing to do with where the data itself is stored (that's derived from `start`).
|
/// it has nothing to do with where the data itself is stored (that's derived from `start`).
|
||||||
|
|
@ -132,6 +134,21 @@ impl KeyCacheEntry {
|
||||||
}
|
}
|
||||||
vpages
|
vpages
|
||||||
}
|
}
|
||||||
|
/// returns a rough measure of the amount of data consumed by a given entry. It's not meant to
|
||||||
|
/// be absolutely accurate, but it should give an accurate impression of the relative size of
|
||||||
|
/// different cache entries. This metric is used to help decide which entries to discard.
|
||||||
|
pub(crate) fn size(&self) -> usize {
|
||||||
|
let data_size = match &self.data {
|
||||||
|
None => 0,
|
||||||
|
Some(kcd) => match kcd {
|
||||||
|
KeyCacheData::Small(ksd) => ksd.data.len(),
|
||||||
|
KeyCacheData::Large(kld) => kld.data.len(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
core::mem::size_of::<KeyCacheEntry>() + data_size
|
||||||
|
}
|
||||||
|
pub(crate) fn atime(&self) -> u64 { self.atime }
|
||||||
|
pub(crate) fn set_atime(&mut self, atime: u64) { self.atime = atime; }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub (crate) enum KeyCacheData {
|
pub (crate) enum KeyCacheData {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue