Elementary, my dear operator. Designing a page replacement algorithm for a custom operating system requires balancing hit ratio maximization, overhead minimization, and adaptability to workloads. Traditional algorithms like FIFO (suffers Belady's anomaly), LRU (high bookkeeping cost), or Clock (good approximation but static) fall short in diverse scenarios—think bursty I/O, streaming data, or ML workloads.
I've deduced SDPR: a hybrid, adaptive algorithm inspired by deductive reasoning. It combines recency (like LRU), frequency (like LFU), and predictive aging (to avoid thrashing). It's efficient (O(1) amortized), fault-tolerant to hardware interrupts, and tunable for your OS's constraints (e.g., frame count, TLB size).
Key Assumptions for Custom OS
Page size: 4KB (configurable).
Frames: Up to 64K (scalable).
Hardware: No fancy counters (e.g., no cycle-accurate timestamps); use jiffies or monotonic clock.
Goals: >95% hit ratio on SPEC benchmarks; <1% overhead.
Core Data Structures (Per-Frame Metadata)
Each frame holds a lightweight PageInfo struct (32-64 bytes):
c
struct PageInfo {
pid_t pid; // Owner process (multi-process support)
u64 last_access; // Jiffies since boot (recency)
u8 freq; // Saturated counter (0-255, frequency)
u8 refs; // Reference bit (1=recently used)
u8 age_bias; // 0-15 (predictive aging for cold pages)
bool dirty; // For write-back
struct list_head list; // For hot/cold lists
};
Global: Two doubly-linked lists (hot_list, cold_list) + a victim clock hand (pointer for scanning).
Total overhead: ~4KB for 1GB RAM (negligible).
Algorithm Mechanics
SDPR classifies pages as Hot (frequently accessed recently), Cold (aged out), or Ghosts (recent evictees, for prediction).
1. On Page Fault (Victim Selection)
code
function find_victim():
scan = clock_hand // Start from last evicted
while true:
page = scan->page_info
// Deduction Phase 1: Quick evict clean cold pages
if not page.refs and not page.dirty and page.age_bias >= 8:
return page // O(1) ideal case
// Phase 2: Age and score (LRU + Freq deduction)
score = (page.freq * 16) / (jiffies - page.last_access + 1)
score *= (1 << (15 - page.age_bias)) // Penalize aged pages
if score < threshold (tunable, default 32) and not page.dirty:
return page
// Phase 3: Second chance (Clock-like)
page.refs = 0
advance_clock_hand(scan)
if full_cycle(): // Wrapped around
decay_all_freqs() // Freq /= 2 globally (anti-LFU infinity)
reset_age_bias() // Bump cold pages' bias
// Fallback: Evict oldest dirty (write-back async)
2. On Page Access (Hit or Load)
code
function on_access(page, is_write):
info = page->info
// Update recency/freq
info.last_access = jiffies
info.freq = min(255, info.freq + 1 + (is_write ? 1 : 0)) // Writes are "hotter"
info.refs = 1
// Promote to hot list
if on_cold_list(info):
move_to_hot_list(info)
// Predictive boost: If freq exploded, reduce age_bias
if info.freq > 128:
info.age_bias = max(0, info.age_bias - 1)
3. Eviction & Ghost Prediction
Evict: Write-back if dirty (async queue). Add to ghost queue (LRU of last 32 evictees).
Ghost Insight: On fault, if faulting page was recently a ghost (match VPN), pre-warm neighbors (scan page table for adjacent pages, prefetch if hot history).
Thrashing Detection: If faults > 10% in 1s window, shrink working set (evict low-score pages aggressively).
4. Tuning & Adaptation
Parameters:
Param
Default
Tune For
score_threshold
32
Hit ratio
clock_size
#frames/2
Memory pressure
decay_interval
1s
Long-running jobs
ghost_depth
32
Prefetch accuracy
Adaptive Mode: Every 10s, measure fault rate. If high, shift to LFU-heavy (boost freq weight); if low, LRU-heavy.