|
| 1 | +//! A byte-budgeted page cache with LRU eviction. |
| 2 | +//! |
| 3 | +//! Entries are reference-counted ([`Arc`]) page frames. Dirty frames are pinned |
| 4 | +//! until the next commit flushes them, so eviction only reclaims clean entries; |
| 5 | +//! if every entry is dirty the cache may temporarily exceed its budget. |
| 6 | +//! |
| 7 | +//! Eviction currently scans for the least-recently-used clean entry (O(n)); the |
| 8 | +//! working sets here are small and this is not yet on a measured hot path. An |
| 9 | +//! O(1) intrusive LRU is a Phase 11 optimization candidate. |
| 10 | +
|
| 11 | +use std::collections::HashMap; |
| 12 | +use std::sync::Arc; |
| 13 | + |
| 14 | +use crate::page::{Frame, PAGE_SIZE}; |
| 15 | + |
| 16 | +struct Entry { |
| 17 | + frame: Arc<Frame>, |
| 18 | + dirty: bool, |
| 19 | + tick: u64, |
| 20 | +} |
| 21 | + |
| 22 | +pub(crate) struct PageCache { |
| 23 | + entries: HashMap<u64, Entry>, |
| 24 | + budget_bytes: usize, |
| 25 | + tick: u64, |
| 26 | +} |
| 27 | + |
| 28 | +impl PageCache { |
| 29 | + pub(crate) fn new(budget_bytes: usize) -> Self { |
| 30 | + PageCache { |
| 31 | + entries: HashMap::new(), |
| 32 | + budget_bytes, |
| 33 | + tick: 0, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + fn bump(&mut self) -> u64 { |
| 38 | + self.tick = self.tick.wrapping_add(1); |
| 39 | + self.tick |
| 40 | + } |
| 41 | + |
| 42 | + /// Fetch a cached frame, marking it most-recently-used. |
| 43 | + pub(crate) fn get(&mut self, id: u64) -> Option<Arc<Frame>> { |
| 44 | + let tick = self.bump(); |
| 45 | + let entry = self.entries.get_mut(&id)?; |
| 46 | + entry.tick = tick; |
| 47 | + Some(Arc::clone(&entry.frame)) |
| 48 | + } |
| 49 | + |
| 50 | + /// Insert or replace a frame. A `dirty` insert pins the entry until the |
| 51 | + /// next commit; re-inserting never clears an existing dirty flag. |
| 52 | + pub(crate) fn insert(&mut self, id: u64, frame: Arc<Frame>, dirty: bool) { |
| 53 | + let tick = self.bump(); |
| 54 | + match self.entries.get_mut(&id) { |
| 55 | + Some(entry) => { |
| 56 | + entry.frame = frame; |
| 57 | + entry.tick = tick; |
| 58 | + entry.dirty = entry.dirty || dirty; |
| 59 | + } |
| 60 | + None => { |
| 61 | + self.entries.insert(id, Entry { frame, dirty, tick }); |
| 62 | + } |
| 63 | + } |
| 64 | + self.evict_to_budget(); |
| 65 | + } |
| 66 | + |
| 67 | + fn evict_to_budget(&mut self) { |
| 68 | + while self.entries.len().saturating_mul(PAGE_SIZE) > self.budget_bytes { |
| 69 | + let victim = self |
| 70 | + .entries |
| 71 | + .iter() |
| 72 | + .filter(|(_, e)| !e.dirty) |
| 73 | + .min_by_key(|(_, e)| e.tick) |
| 74 | + .map(|(id, _)| *id); |
| 75 | + match victim { |
| 76 | + Some(id) => { |
| 77 | + self.entries.remove(&id); |
| 78 | + } |
| 79 | + None => break, // everything resident is dirty; keep it |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// All currently-dirty frames, for the commit flush. Does not clear flags. |
| 85 | + pub(crate) fn dirty_pages(&self) -> Vec<(u64, Arc<Frame>)> { |
| 86 | + self.entries |
| 87 | + .iter() |
| 88 | + .filter(|(_, e)| e.dirty) |
| 89 | + .map(|(id, e)| (*id, Arc::clone(&e.frame))) |
| 90 | + .collect() |
| 91 | + } |
| 92 | + |
| 93 | + /// Mark every entry clean (after a successful commit flush). |
| 94 | + pub(crate) fn clear_dirty(&mut self) { |
| 95 | + for entry in self.entries.values_mut() { |
| 96 | + entry.dirty = false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[cfg(test)] |
| 101 | + pub(crate) fn len(&self) -> usize { |
| 102 | + self.entries.len() |
| 103 | + } |
| 104 | + |
| 105 | + #[cfg(test)] |
| 106 | + pub(crate) fn contains(&self, id: u64) -> bool { |
| 107 | + self.entries.contains_key(&id) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod tests { |
| 113 | + use super::*; |
| 114 | + use crate::page; |
| 115 | + |
| 116 | + fn frame(id: u64) -> Arc<Frame> { |
| 117 | + let mut f = page::zeroed(); |
| 118 | + page::finalize(&mut f, page::PageType::Data, crate::PageId::new(id)); |
| 119 | + Arc::from(f) |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn evicts_clean_lru_first() { |
| 124 | + // Budget for two pages. |
| 125 | + let mut cache = PageCache::new(2 * PAGE_SIZE); |
| 126 | + cache.insert(2, frame(2), false); |
| 127 | + cache.insert(3, frame(3), false); |
| 128 | + let _ = cache.get(2); // touch 2 → 3 is now LRU |
| 129 | + cache.insert(4, frame(4), false); // over budget → evict 3 |
| 130 | + assert!(cache.contains(2)); |
| 131 | + assert!(cache.contains(4)); |
| 132 | + assert!(!cache.contains(3)); |
| 133 | + assert_eq!(cache.len(), 2); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn dirty_pages_are_pinned_over_budget() { |
| 138 | + let mut cache = PageCache::new(PAGE_SIZE); // budget for one page |
| 139 | + cache.insert(2, frame(2), true); |
| 140 | + cache.insert(3, frame(3), true); |
| 141 | + // Both dirty → neither evicted even though over budget. |
| 142 | + assert_eq!(cache.len(), 2); |
| 143 | + assert_eq!(cache.dirty_pages().len(), 2); |
| 144 | + cache.clear_dirty(); |
| 145 | + // Now clean; next insert can evict down to budget. |
| 146 | + cache.insert(4, frame(4), false); |
| 147 | + assert_eq!(cache.len(), 1); |
| 148 | + } |
| 149 | +} |
0 commit comments