UNIVERSITY OF NIVERSITY OF MASSACHUSETTS ASSACHUSETTS AMHERST • MHERST • Department of Computer Science Department of Computer Science Computer Systems Principles Virtual Memory & Paging Emery Berger and Mark Corner University of Massachusetts Amherst
112
Embed
Computer Systems Principles Virtual Memory & Paging
Computer Systems Principles Virtual Memory & Paging. Emery Berger and Mark Corner University of Massachusetts Amherst. Virtual vs. Physical Memory. Apps don’t access physical memory Well, not directly Apps use virtual memory Addresses start at 0 One level of indirection - PowerPoint PPT Presentation
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
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Computer Systems PrinciplesVirtual Memory & Paging
Emery Berger and Mark CornerUniversity of Massachusetts
Amherst
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 2
Virtual vs. Physical Memory Apps don’t access physical memory
– Well, not directly Apps use virtual memory
– Addresses start at 0– One level of indirection– Address you see is not “real” address
Any ideas why?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Memory Pages Programs use memory as individual bytes OS manages groups of bytes: pages
– typically 4kB, 8kB– Why? (think Tetris with all squares)– Applies this to virtual and physical memory
• Physical pages usually called frames
A
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 4
Mapping Virtual to Physical
Note this is simplified and the data here includes the heap, not the typical data segment…
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 5
Why Virtual Memory? Why?
– Simpler• Everyone gets illusion
of whole address space
– Isolation• Every process
protected from every other
– Optimization • Reduces space
requirements
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Typical Virtual Memory Layout Some things grow
– Must leave room! Mmap and heap spaces
– Mmap increases mmap– Brk increases heap
Other layouts possible
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Quick Quiz! Are arrays contiguous in memory?
– Physical memory?– Virtual memory?
Where does the code for a program live?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Memory Management Unit Programs issue loads and stores What kind of addresses are these? MMU Translates virtual to physical addresses
– Maintains page table (big hash table):– Almost always in HW… Why?
MMU
Physical
Address
Virtual Addres
s
Program
Memory
Page Table
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Page Tables Table of translations
– virtual pages -> physical pages One page table per process One page table entry per virtual page How?
2^32/2^12*4=2^22 bytes=4MB– Is this a problem?– Isn’t this per process?– What about a 64 bit address space?
Any ideas how to fix this?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Multi-Level Page Tables Use a multi-level page table
A A
A
Level 0 Table
Level 1 Table
Level 1 Table
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Quick Activity How much mem does a page table need?
– 4kB pages, 32 bit address space– Two level page table– 20bits = 10 bits each level– page table entry (PTE) uses 4 bytes– Only first page of program is valid
2^10*4+2^10*4=2^13 bytes=8kB
Isn’t this slow?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 15
in which they are stored Assumption: locality of reference
– Locality in memory accesses =locality in address translation
TLB sizes: 8 to 2048 entries– Powers of 2 simplifies translation
of virtual to physical addresses
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Virtual Memory is an Illusion! How much memory does a process have?
– Do all processes have this? Key idea: use RAM as cache for disk
– OS transparently moves pages Requires locality:
– Working set must fit in RAM• memory referenced recently
– If not: thrashing (nothing but disk traffic)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 17
Paging
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 18
A
B
AB
Paging + Locality Most programs obey
90/10 “rule”– 90% of time spent
accessing 10% of memory
Exploit this rule:– Only keep “live” parts
of process in memory
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 19
Key Policy Decisions Two key questions: (for any cache):
– When do we read page from disk?– When do we write page to disk?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 20
Reading Pages Read on-demand:
– OS loads page on its first reference– May force an eviction of page in RAM– Pause while loading page = page fault
Can also perform pre-paging:– OS guesses which page will next be needed,
and begins loading it Most systems just do demand paging What about writes?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 21
Demand Paging On every reference, check if page is in memory
(resident bit in page table)– Who is doing this?
If not: trap to OS– How does this work in HW?
OS:– Selects victim page to be replaced– Writes victim page if necessary, marks non-resident– Begins loading new page from disk– OS can switch to another process
• more on this later
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 22
Swap Space Swap space = where victim pages go
– Partition or special file reserved on disk
Size of reserved swap space limits what?
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Tricks with Page Tables Do all pages of memory end up in swap? Parts of address space mapped into files
– see man pages for mmap
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 24
Overview A Day in the Life of a Page
– Allocation– Use– Eviction– Reuse
Terms: Resident and Non-resident
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
char * x = new char[16];
25
virtual memory layout
Allocate some memory
0x40001000
0x40001040 → 0x4000104F
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
char * x = new char[16];
26
virtual memory layout
Update page tables
0x40001000
0x40001040 → 0x4000104F
physicalmemory
layout
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
strcpy(x, “hello”);
27
virtual memory layout
Write contents – dirty page
0x40001000
0x40001040 → 0x4000104F
physicalmemory
layout
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 28
virtual memory layout
Other processes fill up memory…
physicalmemory
layout
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 29
virtual memory layout
Forcing our page to be evicted (paged out)
physicalmemory
layout
swapspace(disk)
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 30
virtual memory layout
Now page nonresident & protected
physicalmemory
layout
swapspace(disk)
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
y[0] = x[0];
31
virtual memory layout
Touch page – swap it in
0x40001000
0x40001040 → 0x4000104F
physicalmemory
layout
swapspace(disk)
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
y[0] = x[0];
32
virtual memory layout
Touch page – swap it in
0x40001000
0x40001040 → 0x4000104F
physicalmemory
layout
swapspace(disk)
A Day in the Life of a Page
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 33
Tricks with Page Tables: Sharing Paging allows sharing
of memory across processes– Reduces memory
requirements Shared stuff includes
code, data– Code typically R/O
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Tricks with Page Tables: COW Copy on write (COW)
– Just copy page tables– Make all pages read-only
What if process changes mem?
All processes are created this way!
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science
Allocating Pages ultimately from sbrk or mmap Sbrk increases # of valid pages
– Increases the heap Mmap maps address space to file
– Increases the mmap space Oddity:
– Allocators can use either mmap or brk to get pages– You will use mmap
What does mmap /dev/zero mean?– Think about COW
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 36
Overview
Replacement policies– Comparison
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 37
A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Usually in algorithms, we pick algorithm
with best asymptotic worst-case– Paging: worst-case analysis useless!– Easy to construct adversary:
every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 38
A A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 39
A B A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 40
A B C A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 41
A B C D A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 42
A B C D E A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 43
F B C D E A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 44
F G H I J A, B, C, D, E, F, G, H, I, J, A...
size of available memory
Cost of Paging Worst-case analysis – useless
– Easy to construct adversary example:every page requires page fault
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTS ASSACHUSETTS AAMHERST • MHERST • Department of Computer Science Department of Computer Science 45
Optimal Replacement (MIN/OPT) Evict page accessed furthest in future