more refactoring

This commit is contained in:
bunnie 2021-11-22 01:19:57 +08:00
parent 00339b0c5f
commit e4e5e6c390
3 changed files with 36 additions and 46 deletions

View file

@ -417,12 +417,6 @@ impl BasisCache {
if let Some(dict_entry) = basis.dicts.get(dict) { if let Some(dict_entry) = basis.dicts.get(dict) {
// see if we need to make a kcache entry // see if we need to make a kcache entry
if let Some(kcache) = dict_entry.keys.get(key) { if let Some(kcache) = dict_entry.keys.get(key) {
if kcache.descriptor_index.is_none() {
// index hasn't been allocated yet, if we don't have extra space in an already allocated page, we'll need a new one
if (DK_PER_VPAGE - dict_entry.key_count as usize % DK_PER_VPAGE) == 0 {
pages_needed += 1;
};
}
// now check for data reservations // now check for data reservations
if reserved < SMALL_CAPACITY { if reserved < SMALL_CAPACITY {
// it's probably going in the small pool. // it's probably going in the small pool.
@ -839,19 +833,6 @@ impl BasisCacheEntry {
for (key_name, key) in dict.keys.iter_mut() { for (key_name, key) in dict.keys.iter_mut() {
log::info!("merging in key {}", key_name); log::info!("merging in key {}", key_name);
if !key.clean { if !key.clean {
// allocate a key index if one hasn't already been allocated
if key.descriptor_index.is_none() {
if let Some(new_index) = dict.get_free_key_index() {
key.descriptor_index = Some(new_index);
// if the new index is outside the currently known set, raise the search extent for the brute-force search
if new_index.get() > dict.last_disk_key_index {
dict.last_disk_key_index = new_index.get();
}
} else {
log::error!("Dictionary {} ran out of key capacity during sync", name);
return Err(Error::new(ErrorKind::OutOfMemory, "Dict ran out of key slots"));
}
}
if key.descriptor_vaddr(dict_offset) >= cur_vpage && if key.descriptor_vaddr(dict_offset) >= cur_vpage &&
key.descriptor_vaddr(dict_offset) < next_vpage { key.descriptor_vaddr(dict_offset) < next_vpage {
// key is within the current page, add it to the target list // key is within the current page, add it to the target list
@ -871,12 +852,7 @@ impl BasisCacheEntry {
for (&src, dst) in key_desc.deref().iter().zip(dk_entry.data.iter_mut()) { for (&src, dst) in key_desc.deref().iter().zip(dk_entry.data.iter_mut()) {
*dst = src; *dst = src;
} }
if let Some(dk_index) = key.descriptor_index { dk_vpage.elements[key.descriptor_index.get() as usize % DK_PER_VPAGE] = Some(dk_entry);
dk_vpage.elements[dk_index.get() as usize % DK_PER_VPAGE] = Some(dk_entry);
} else {
log::error!("Dictionary {} ran out of key capacity during sync", name);
return Err(Error::new(ErrorKind::OutOfMemory, "Dict ran out of key slots"));
}
} }
key.clean = true; key.clean = true;
} }

View file

@ -209,7 +209,7 @@ impl DictCacheEntry {
reserved: keydesc.reserved, reserved: keydesc.reserved,
flags: keydesc.flags, flags: keydesc.flags,
age: keydesc.age, age: keydesc.age,
descriptor_index: Some(NonZeroU32::new(try_entry as u32).unwrap()), descriptor_index: NonZeroU32::new(try_entry as u32).unwrap(),
clean: true, clean: true,
data: None, data: None,
}; };
@ -280,7 +280,7 @@ impl DictCacheEntry {
reserved: keydesc.reserved, reserved: keydesc.reserved,
flags: keydesc.flags, flags: keydesc.flags,
age: keydesc.age, age: keydesc.age,
descriptor_index: Some(NonZeroU32::new(try_entry as u32).unwrap()), descriptor_index: NonZeroU32::new(try_entry as u32).unwrap(),
clean: true, clean: true,
data: None, data: None,
}; };
@ -503,13 +503,18 @@ impl DictCacheEntry {
for &b in data { for &b in data {
alloc_data.push(b); alloc_data.push(b);
} }
let descriptor_index = if let Some(di) = self.get_free_key_index() {
di
} else {
return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of key indices in dictionary"));
};
let kcache = KeyCacheEntry { let kcache = KeyCacheEntry {
start: self.index as u64 * SMALL_POOL_START + SMALL_POOL_START, start: self.index as u64 * SMALL_POOL_START + SMALL_POOL_START,
len: (data.len() + offset) as u64, len: (data.len() + offset) as u64,
reserved: reservation as u64, reserved: reservation as u64,
flags: kf, flags: kf,
age: 0, age: 0,
descriptor_index: None, descriptor_index,
clean: false, clean: false,
data: Some(KeyCacheData::Small(KeySmallData{ data: Some(KeyCacheData::Small(KeySmallData{
clean: false, clean: false,
@ -529,13 +534,18 @@ impl DictCacheEntry {
}); });
let mut kf = KeyFlags(0); let mut kf = KeyFlags(0);
kf.set_valid(true); kf.set_valid(true);
let descriptor_index = if let Some(di) = self.get_free_key_index() {
di
} else {
return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of key indices in dictionary"));
};
let kcache = KeyCacheEntry { let kcache = KeyCacheEntry {
start: large_alloc_ptr.as_u64(), start: large_alloc_ptr.as_u64(),
len: (data.len() + offset) as u64, len: (data.len() + offset) as u64,
reserved: reservation.as_u64(), reserved: reservation.as_u64(),
flags: kf, flags: kf,
age: 0, age: 0,
descriptor_index: None, descriptor_index,
clean: false, clean: false,
data: None, // no caching implemented yet for large keys data: None, // no caching implemented yet for large keys
}; };
@ -571,6 +581,7 @@ impl DictCacheEntry {
self.ensure_key_entry(hw, v2p_map, cipher, name_str); self.ensure_key_entry(hw, v2p_map, cipher, name_str);
let name = String::from(name_str); let name = String::from(name_str);
let mut need_rebuild = false; let mut need_rebuild = false;
let mut need_free_key: Option<u32> = None;
if let Some(kcache) = self.keys.get_mut(&name) { if let Some(kcache) = self.keys.get_mut(&name) {
if let Some(small_index) = small_storage_index_from_key(kcache, self.index) { if let Some(small_index) = small_storage_index_from_key(kcache, self.index) {
// handle the small pool case // handle the small pool case
@ -598,18 +609,17 @@ impl DictCacheEntry {
} }
} }
} }
// free up the key index in the dictionary need_free_key = Some(kcache.descriptor_index.get());
if let Some(index) = kcache.descriptor_index {
self.put_free_key_index(index.get());
} else {
// this should be a rare case; later on we can change this warn to a debug, but print something as a bread crumb to future me...
log::warn!("Key cache descriptor was none -- should only happen in the case of a key that was removed before it was synchronized to disk");
}
if need_rebuild {
// no stable "retain" api, so we have to clear the heap and rebuild it https://github.com/rust-lang/rust/issues/71503
self.rebuild_free_pool();
}
} }
// free up the key index in the dictionary, if necessary
if let Some(key_to_free) = need_free_key {
self.put_free_key_index(key_to_free);
}
if need_rebuild {
// no stable "retain" api, so we have to clear the heap and rebuild it https://github.com/rust-lang/rust/issues/71503
self.rebuild_free_pool();
}
// we don't remove the cache entry, because it hasn't been synchronized to disk. // we don't remove the cache entry, because it hasn't been synchronized to disk.
// at this point: // at this point:
// - in-memory representation will return an entry, but with its valid flag set to false. // - in-memory representation will return an entry, but with its valid flag set to false.
@ -629,7 +639,7 @@ impl DictCacheEntry {
if !ksp.clean { if !ksp.clean {
for keyname in &ksp.contents { for keyname in &ksp.contents {
let kce = self.keys.get(keyname).expect("data allocated but no index entry"); let kce = self.keys.get(keyname).expect("data allocated but no index entry");
if kce.descriptor_index.is_none() { if kce.flags.unresolved() {
data_estimate += SMALL_CAPACITY - ksp.avail as usize; data_estimate += SMALL_CAPACITY - ksp.avail as usize;
index_estimate += 1; index_estimate += 1;
} }
@ -704,7 +714,7 @@ impl DictCacheEntry {
pub(crate) fn sync_large_pool(&self) { pub(crate) fn sync_large_pool(&self) {
} }
pub(crate) fn get_free_key_index(&self) -> Option<NonZeroU32> { pub(crate) fn get_free_key_index(&mut self) -> Option<NonZeroU32> {
if let Some(free_key) = self.free_keys.borrow_mut().pop() { if let Some(free_key) = self.free_keys.borrow_mut().pop() {
let index = free_key.start; let index = free_key.start;
if free_key.run > 0 { if free_key.run > 0 {
@ -715,6 +725,10 @@ impl DictCacheEntry {
} }
) )
} }
if index > self.last_disk_key_index {
// if the new index is outside the currently known set, raise the search extent for the brute-force search
self.last_disk_key_index = index;
}
NonZeroU32::new(index as u32) NonZeroU32::new(index as u32)
} else { } else {
log::warn!("Ran out of dict index space"); log::warn!("Ran out of dict index space");

View file

@ -79,7 +79,7 @@ pub(crate) struct KeyCacheEntry {
pub(crate) flags: KeyFlags, pub(crate) flags: KeyFlags,
pub(crate) age: u32, pub(crate) age: u32,
/// the current on-disk index of the KeyCacheEntry item, enumerated as "0" being the Dict descriptor and "1" being the first valid key /// the current on-disk index of the KeyCacheEntry item, enumerated as "0" being the Dict descriptor and "1" being the first valid key
pub(crate) descriptor_index: Option<NonZeroU32>, pub(crate) descriptor_index: NonZeroU32,
/// indicates if the descriptor cache entry is currently synchronized with what's on disk. Does not imply anything about the data, /// indicates if the descriptor cache entry is currently synchronized with what's on disk. Does not imply anything about the data,
/// but if the `data` field is None then there is nothing to in cache to be dirtied. /// but if the `data` field is None then there is nothing to in cache to be dirtied.
pub(crate) clean: bool, pub(crate) clean: bool,
@ -90,15 +90,15 @@ pub(crate) struct KeyCacheEntry {
impl KeyCacheEntry { impl KeyCacheEntry {
/// Given a base offset of the dictionary containing the key, compute the starting VirtAddr of the key itself. /// Given a base offset of the dictionary containing the key, compute the starting VirtAddr of the key itself.
pub(crate) fn descriptor_vaddr(&self, dict_offset: VirtAddr) -> VirtAddr { pub(crate) fn descriptor_vaddr(&self, dict_offset: VirtAddr) -> VirtAddr {
VirtAddr::new(dict_offset.get() + ((self.descriptor_index.unwrap().get() as u64) * DK_STRIDE as u64)).unwrap() VirtAddr::new(dict_offset.get() + ((self.descriptor_index.get() as u64) * DK_STRIDE as u64)).unwrap()
} }
/// Computes the modular position of the KeyDescriptor within a vpage. /// Computes the modular position of the KeyDescriptor within a vpage.
pub(crate) fn descriptor_modulus(&self) -> usize { pub(crate) fn descriptor_modulus(&self) -> usize {
(self.descriptor_index.unwrap().get() as usize) % (VPAGE_SIZE / DK_STRIDE) (self.descriptor_index.get() as usize) % (VPAGE_SIZE / DK_STRIDE)
} }
/// Computes the vpage offset as measured from the start of the dictionary storage region /// Computes the vpage offset as measured from the start of the dictionary storage region
pub(crate) fn descriptor_vpage_num(&self) -> usize { pub(crate) fn descriptor_vpage_num(&self) -> usize {
(self.descriptor_index.unwrap().get() as usize) / (VPAGE_SIZE / DK_STRIDE) (self.descriptor_index.get() as usize) / (VPAGE_SIZE / DK_STRIDE)
} }
/// returns the list of large-pool virtual pages belonging to this entry, if any. /// returns the list of large-pool virtual pages belonging to this entry, if any.
pub(crate) fn large_pool_vpages(&self) -> Vec::<VirtAddr> { pub(crate) fn large_pool_vpages(&self) -> Vec::<VirtAddr> {