Virtual Memory Page Replacement Concepts Welcome! Generic Cache Memory Access Time Paging In Parallel How long does it take to access? No Line in Cache? (Miss) Load Line (Some Cycles) Yes (Hit) Get Line (few cycles) ! There is something strange with this formula. Memory Access Time (1-P CM)*T C+P CM*(T MM) T C: T MM : P CM : 1-P CM : Time to access cache Time to access main memory Probably of cache miss Probably of cache hit TLB How much time does it take? EAT(effective access time) = P x hit memory time + (1-P) x miss memory time • Consider that the page is in main memory, not on disk. • TLB hit rate: 80% • TLB read time: 20 nanoseconds • RAM read time: 100 nanoseconds EAT(effective access time) = 0.80 x (20read TLB + 100read page from ram) + 0.20 x (100 bring TLB entry from ram + 20read TLB + 100read page from ram) = 140 nanoseconds (The Pennsylvania State University, 2014) Page Replacement But what if the main memory is full? Paging Replacement Tracking Page Data int main() { char chunkA[3072]; char chunkB[2048]; char* chunkC = new char[2048]; chunkB[42] = 9001; chunkA[0] = 42; chunkC[1] = chunkA[0]; } Reference String 0,7 ,6 ,2 ,6 ,7 ,7 ,2 We can remove consecutive references (as they are redundant) to get a reduced reference string. [7] [6] Stack 28-32KiB 24-28KiB [5] 20-24KiB [4] 16-20KiB [3] 12-16KiB [2] Heap 8-12KiB [1] Data 4-8KiB [0] Text 0-4KiB 0,7 ,6 ,2 ,7 ,7 ,6 ,2 0,7 ,6 ,2 ,7 ,6 ,2 Paging Replacement Algorithms Green: Page Fault Optimal (Read: Magic) Blue: Already in Memory • A hypothetical optimal solution shows us the best possible page fault rate (lowest page fault rate). Page Frames Reduced Reference String 0 1 2 0 2 3 1 0 4 1 2 0 3 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - 1 1 1 1 1 1 1 1 1 1 1 1 1 1 - - 2 2 2 3 3 3 4 4 2 2 3 3 3 Time • The Page Fault Rate is 7/15 (46.7%). Green: Page Fault Paging Replacement Algorithms Blue: Already in Memory First-In-First-Out (FIFO) Red: Oldest Page • With First-In-First-Out (FIFO) page replacement, the oldest page is evicted first. Page Frames Reduced Reference String 0 1 2 0 2 3 1 0 4 1 2 0 3 0 3 0 0 0 0 0 3 3 3 3 1 1 1 3 3 3 - 1 1 1 1 1 1 0 0 0 2 2 2 2 2 - - 2 2 2 2 2 2 4 4 4 0 0 0 0 Time • The Page Fault Rate is 10/15 (66.7%). ! For FIFO, even if we increase the number of frames, there are always situations where the fault rate increases. This is called the Bélády’s anomaly. Green: Page Fault Paging Replacement Algorithms Blue: Already in Memory Least Recently Used (LRU) Red: Oldest Page • The Least Recently Used (LRU) algorithm evicts the most “stale” page. Page Frames Reduced Reference String 0 1 2 0 2 3 1 0 4 1 2 0 3 0 3 0 0 0 0 0 1 1 1 1 1 1 1 3 3 3 - 1 1 1 1 3 3 3 4 4 4 0 0 0 0 - - 2 2 2 2 2 0 0 0 2 2 2 2 2 Time • The Page Fault Rate is 10/15 (66.7%). ! LRU is superior because it does not suffer weird edge cases like FIFO (Bélády’s anomaly). However, it’s impractical / inefficient (requires in-place list of modifications). Thank You References • The Pennsylvania State University. (2014). Diagram of TLB [Online Image]. The Pennsylvania State University. https://www.cse.psu.edu/~deh25/cmpsc473/notes/OSC/MemoryManagement.html