Top Banner
Lecture 8 Memory Management
25

Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Dec 15, 2015

Download

Documents

Clare Hearn
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Lecture 8Memory

Management

Page 2: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Paging

• Too slow -> TLB• Too big -> multi-level page table

• What if all that stuff does not fit into memory?

Page 3: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

How Many Physical Accesses

• Assume a linear page table• Assume 256-byte pages, 16-bit addresses.• Assume ASID of current process is 211

• 0xAA10: movl 0x1111, %edi• 0xBB13: addl $0x3, %edi• 0x0519: movl %edi, 0xFF10

valid VPN PFN ASID Prot

1 0xBB 0x91 211 ?

1 0xFF 0x23 211 ?

1 0x05 0x91 112 ?

0 0x05 0x12 211 ?

Page 4: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

How Many Physical Accesses

• Assume a 3-level page table• Assume 256-byte pages, 16-bit addresses.• Assume ASID of current process is 211

• 0xAA10: movl 0x1111, %edi• 0xBB13: addl $0x3, %edi• 0x0519: movl %edi, 0xFF10

valid VPN PFN ASID Prot

1 0xBB 0x91 211 ?

1 0xFF 0x23 211 ?

1 0x05 0x91 112 ?

0 0x05 0x12 211 ?

Page 5: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Page Table Size

• The page size is increased by 4x, but the total sizes of physical and virtual memory are unchanged. PTE’s are originally 20 bits.

• (a) does the number of virtual pages increase or decrease? By how much?

• (b) by what factor does the number of PTE’s (rows in PT) increase or decrease?

• (c) does the number of physical pages increase or decrease? By how much?

• (d) how many more (or fewer) bits are needed to store a physical page number?

• (e) by what factor does the size of PTE’s (columns in PT) increase or decrease?

• (f) by what factor does the size of the page table increase or decrease?

Page 6: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Two-level PT Translations

• Assume 12-bit virtual address• (a) 0x123• (b) 0xCBA• (c) 0x777

Page 7: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

The Memory Hierarchy

Registers

Cache

Main Memory

Secondary Storage

Page 8: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?
Page 9: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Swap Space

• Reserved disk space for moving pages back and forth

Page 10: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

How to know where a page lives?The Present Bit

• Now Proc 1 accesses VPN 0, …

Page 11: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Translation Steps

H/W: for each mem reference:extract VPN from VAcheck TLB for VPNTLB hit: build PA from PFN and offset fetch PA from memoryTLB miss: fetch PTE if (!valid): exception [segfault] else if (!present): exception [page fault, or page miss] else: extract PFN, insert in TLB, retry

Page 12: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

The Page Fault

• OS handles the page fault• Regardless of hardware-managed or OS-managed TLB• Page faults to disk are slow, so no need to use hardware• Page faults are complicated to handler, so easier for OS

• Where is the page on disk?• Store the disk address in the PTE

Page 13: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

p

Page 14: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Page-Fault Handler (OS)

PFN = FindFreePage()if (PFN == -1) PFN = EvictPage()DiskRead(PTE.DiskAddr, PFN)PTE.present = 1PTE.PFN = PFNretry instruction

<- policy<- blocking

Page 15: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

When Replacements Really Occur• High watermark (HW) and low watermark (LW)

• A background thread (swap daemon/page daemon)• Frees pages when there are fewer than LW• Clusters or groups a number of pages

• The page fault handler leverages this

Page 16: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Average Memory Access Time (AMAT)• Hit% = portion of accesses that go straight to RAM• Miss% = portion of accesses that go to disk first• Tm = time for memory access• Td = time for disk access• AMAT = (Hit% * Tm) + (Miss% * Td) • Mem-access time is 100 nanoseconds, disk-access

time is 10 milliseconds, what is AMAT when hit rate is (a) 50% (b) 98% (c) 99% (d) 100%

Page 17: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

The Optimal Replacement Policy• Replace the page that will be accessed furthest in

the future

• Given 0, 1, 2, 0, 1, 3, 0, 3, 1, 2, 1, hit rate?• Assume cache for three pages

• Three C’s: types of cache misses• compulsory miss• capacity miss• conflict miss

Page 18: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

FIFO

• Given 0, 1, 2, 0, 1, 3, 0, 3, 1, 2, 1, hit rate?• Assume cache for three pages

• Belady’s Anomaly• 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, hit rate if 3-page cache• 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, hit rate if 4-page cache

• Random

Page 19: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Let’s Consider History

• Principle of locality: spatial and temporal

• LRU evicts least-recently used• LFU evicts least-frequently used

• Given 0, 1, 2, 0, 1, 3, 0, 3, 1, 2, 1, LRU hit rate?• Assume cache for three pages

• MRU, MFU

Page 20: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Implementing Historical Algorithms• Need to track every page access• Accurate implementation is expensive

• Approximating LRU• Adding reference bit: set upon access, cleared by OS• Clock algorithm

Page 21: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Other factors

• Assume page is both in RAM and on disk

• Do we have to write to disk for eviction?• not if page is clean• track with dirty bit

Page 22: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Other VM Policies

• Demand paging• Don’t even load pages into memory until they are first

used• Less I/O needed• Less memory needed • Faster response• More users

• Rrefetching

Page 23: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Thrashing

• A machine is thrashing when there is not enough RAM, and we constantly swap in/out pages

• Solutions?• admission control (like scheduler project)• buy more memory• Linux out-of-memory killer!

Page 24: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Next

• Other VM-related material not in OSTEP

• Concurrency

Page 25: Lecture 8 Memory Management. Paging Too slow -> TLB Too big -> multi-level page table What if all that stuff does not fit into memory?

Discuss

• Can Belady’s anomaly happen with LRU?• Stack property: smaller cache always subset of

bigger• The set of pages in memory when we have f frames

is always a subset ofThe set of pages in memory when we have f+1 frames• Said a different way, having more frames will let the

algorithm keep additional pages in memory,but, it will never choose to throw out a page that would have remained in memory with fewer frames

• Does optimal have stack property?