use crate::policy::AdmissionDecision;
use super::CachePolicy;
use parking_lot::Mutex;
use std::collections::HashMap;
use std::hash::Hash;
#[derive(Clone, Copy, Debug)]
struct ClockEntry {
cost: u64,
referenced: bool,
}
#[derive(Debug)]
pub struct ClockPolicy<K> {
items: Mutex<HashMap<K, ClockEntry>>,
order: Mutex<Vec<K>>,
hand: Mutex<usize>,
}
impl<K> ClockPolicy<K> {
pub fn new() -> Self {
Self {
items: Mutex::new(HashMap::new()),
order: Mutex::new(Vec::new()),
hand: Mutex::new(0),
}
}
}
impl<K, V> CachePolicy<K, V> for ClockPolicy<K>
where
K: Eq + Hash + Clone + Send + Sync,
V: Send + Sync,
{
fn on_access(&self, key: &K, _cost: u64) {
let mut items = self.items.lock();
if let Some(entry) = items.get_mut(key) {
entry.referenced = true;
}
}
fn on_admit(&self, key: &K, cost: u64) -> AdmissionDecision<K> {
let mut order = self.order.lock();
let mut items = self.items.lock();
if !items.contains_key(key) {
items.insert(
key.clone(),
ClockEntry {
cost,
referenced: false,
},
);
order.push(key.clone());
}
AdmissionDecision::Admit }
fn on_remove(&self, key: &K) {
let mut order = self.order.lock();
let mut items = self.items.lock();
let mut hand = self.hand.lock();
if items.remove(key).is_some() {
if let Some(pos) = order.iter().position(|k| k == key) {
order.remove(pos);
if pos <= *hand && *hand > 0 {
*hand -= 1;
}
}
}
}
fn evict(&self, mut cost_to_free: u64) -> (Vec<K>, u64) {
let mut victims = Vec::new();
let mut total_cost_freed = 0;
let mut order = self.order.lock();
let mut items = self.items.lock();
let mut hand = self.hand.lock();
while cost_to_free > 0 && !order.is_empty() {
let mut swept = 0;
let order_len = order.len();
while swept < order_len * 2 {
if *hand >= order.len() {
*hand = 0;
}
let key = &order[*hand];
let entry = items.get_mut(key).unwrap();
if entry.referenced {
entry.referenced = false;
*hand += 1;
} else {
let key_to_evict = order.remove(*hand);
let cost = items.remove(&key_to_evict).unwrap().cost;
cost_to_free = cost_to_free.saturating_sub(cost);
total_cost_freed += cost;
victims.push(key_to_evict);
break;
}
swept += 1;
}
}
(victims, total_cost_freed)
}
fn clear(&self) {
self.items.lock().clear();
self.order.lock().clear();
*self.hand.lock() = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_item_admitted() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
let items = policy.items.lock();
let order = policy.order.lock();
assert!(items.contains_key(&1));
assert_eq!(items.get(&1).unwrap().cost, 1);
assert!(
!items.get(&1).unwrap().referenced,
"New item should not be referenced"
);
assert_eq!(*order, vec![1]);
}
#[test]
fn test_access_sets_referenced_bit() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
assert!(!policy.items.lock().get(&1).unwrap().referenced);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &1, 1);
assert!(
policy.items.lock().get(&1).unwrap().referenced,
"Accessed item should be referenced"
);
}
#[test]
fn test_evict_unreferenced_item() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &2, 1);
let (victims, cost_freed) = <ClockPolicy<i32> as CachePolicy<i32, ()>>::evict(&policy, 1);
assert_eq!(victims, vec![1]);
assert_eq!(cost_freed, 1);
assert!(!policy.items.lock().contains_key(&1));
assert_eq!(*policy.order.lock(), vec![2]);
assert_eq!(
*policy.hand.lock(),
0,
"Hand should reset or stay at 0 after eviction"
);
}
#[test]
fn test_evict_gives_second_chance() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &2, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &1, 1);
let (victims, cost_freed) = <ClockPolicy<i32> as CachePolicy<i32, ()>>::evict(&policy, 1);
assert_eq!(victims, vec![2]);
assert_eq!(cost_freed, 1);
let items = policy.items.lock();
let order = policy.order.lock();
let hand = policy.hand.lock();
assert!(items.contains_key(&1));
assert!(!items.contains_key(&2));
assert_eq!(*order, vec![1]);
assert!(
!items.get(&1).unwrap().referenced,
"Referenced bit for item 1 should be cleared"
);
assert_eq!(*hand, 1, "Hand should have advanced past item 1");
}
#[test]
fn test_evict_wraps_around_clock() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &2, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &1, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &2, 1);
let (victims, cost_freed) = <ClockPolicy<i32> as CachePolicy<i32, ()>>::evict(&policy, 1);
assert_eq!(victims, vec![1]);
assert_eq!(cost_freed, 1);
assert!(!policy.items.lock().contains_key(&1));
assert_eq!(*policy.order.lock(), vec![2]);
}
#[test]
fn test_on_remove_adjusts_hand() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &2, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &3, 1);
let _ = <ClockPolicy<i32> as CachePolicy<i32, ()>>::evict(&policy, 0); *policy.hand.lock() = 1;
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_remove(&policy, &1);
assert_eq!(*policy.order.lock(), vec![2, 3]);
assert_eq!(*policy.hand.lock(), 0, "Hand should be decremented");
}
#[test]
fn test_clear_resets_state() {
let policy = ClockPolicy::<i32>::new();
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &1, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &1, 1);
*policy.hand.lock() = 1;
<ClockPolicy<i32> as CachePolicy<i32, ()>>::clear(&policy);
assert!(policy.items.lock().is_empty());
assert!(policy.order.lock().is_empty());
assert_eq!(*policy.hand.lock(), 0);
}
#[test]
fn test_evict_multiple_items() {
let policy = ClockPolicy::<i32>::new();
for i in 1..=5 {
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_admit(&policy, &i, 1);
}
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &3, 1);
<ClockPolicy<i32> as CachePolicy<i32, ()>>::on_access(&policy, &5, 1);
let (victims, cost_freed) = <ClockPolicy<i32> as CachePolicy<i32, ()>>::evict(&policy, 3);
assert_eq!(cost_freed, 3);
assert_eq!(victims, vec![1, 2, 4]);
let order = policy.order.lock();
assert_eq!(*order, vec![3, 5]);
let items = policy.items.lock();
assert!(
!items.get(&3).unwrap().referenced,
"Item 3 should have its bit cleared"
);
assert!(
items.get(&5).unwrap().referenced,
"Item 5 was not scanned, should still be referenced"
);
}
}