virtual memory - university of richmondvirtual memory implementation page fault incredibly costly...

61
Virtual Memory Lecture 25 CS301

Upload: others

Post on 02-Jun-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory

Lecture 25CS301

Page 2: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

DRAM as cache

• What about programs larger than DRAM?

• When we run multiple programs, all must fit in DRAM!

• Add another larger, slower level to the memory hierarchy - use part of the hard drive.

Page 3: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory

• Memory Size -

• Protection -

Page 4: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory

• Memory Size - allows total memory allocation to exceed DRAM capacity.

• Protection -

Page 5: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory

• Memory Size - allows total memory allocation to exceed DRAM capacity.

• Protection - programs may not access each other’s memory.

Page 6: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - no VM

• Program A beginsProg A

Page 7: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - no VM

• Program A begins• Program B begins

Prog A

Prog BWhat happens if A wants more memory?

Page 8: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - no VM

• Program A begins• Program B begins

Prog A

Prog BWhat happens if A wants more memory? Out of luck. If we gave A memory after the end of B, then A would be able to access all of B’s memory.

Page 9: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - no VM

• Program A begins• Program B begins• Program A ends

Prog B

Page 10: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - no VM

• Program A begins• Program B begins• Program A ends• Program C ready Prog B

Prog C

Page 11: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - no VM

• Program A begins• Program B begins• Program A ends• Program C ready

w It can not run even though there is enough free space

w Fragmentation

Prog B

Prog C

Page 12: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory

• Use hard drive for memory that does not fit in DRAM

• Allocate memory in pages • Provide protection by page

Page 13: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Address Space

• Virtual Address Space

• Physical Address Space

Page 14: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Address Space

• Virtual Address Spacew Located on Hard Drive (essentially)w Starts at 0 for each program

• Physical Address Space

Page 15: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Address Space

• Virtual Address Spacew Located on Hard Drive (essentially) w Starts at 0 for each program

• Physical Address Spacew Located in DRAMw The “cache” for the Disk Drive

Page 16: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - VM

• Program A begins• Program B begins Prog A

Hard Drive

Virtual Address0

16K

Physical Address

Page 17: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - VM

• Program A begins• Program B begins Prog A

Hard Drive

Prog B

0

0

16K

24K

Virtual Address Physical Address

Page 18: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - VM

• Program A begins• Program B begins

What happens if A wants more memory?

Prog A

Prog B

0

0

16K

24K

Virtual Address Physical Address

Hard Drive

Page 19: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - VM

• Program A begins• Program B begins

What happens if A wants more memory?Allocate another virtual page.

Prog A

Hard Drive

Prog B

0

020K

24K

Virtual Address Physical Address

Page 20: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - VM

• Program A begins• Program B begins• Program A ends

Hard Drive

Prog B

0

24K

Virtual Address Physical Address

Page 21: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Multi-Processing - VM

• Program A begins• Program B begins• Program A ends• Program C begins

w Not all placed in DRAM

w DRAM use need not be contiguous

Hard Drive

Prog B

0

24K

Prog C

Virtual AddressPhysical Address0

28K

Page 22: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory is like caching…

• _______ is the cache for the __________w It contains only a subset of the total

space• Given an address, determine whether

it is currently in the “cache”• On a miss, obtain data and place in

“cache”

Page 23: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory is like caching…

• DRAM is the cache for the hard drivew It contains only a subset of the total

space• Given an address, determine whether

it is currently in the “cache”• On a miss, obtain data and place in

“cache”

Page 24: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory is not like caching…

• The miss penalty is orders of magnitude larger than for the cache

• You must know where it resides in DRAM before you can look it up in L1 cache

• This leads to a much different implementation

Page 25: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

The Search

• Cache – search each block in set for the proper tag

• Virtual Memory – store a table that is a mapping from virtual address to physical location (DRAM location).

Page 26: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory Implementation

• Programs use virtual addresses• VM Block is called a __________• A VM DRAM “cache miss” is called a

______________.• To access data, the address must

translate it to a _______________.• This translation is called

_______________ or ________________.

Page 27: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory Implementation

• Programs use virtual addresses• VM Block is called a page• A VM DRAM “cache miss” is called a

______________.• To access data, the address must

translate it to a _______________.• This translation is called

_______________ or ________________.

Page 28: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory Implementation

• Programs use virtual addresses• VM Block is called a page• A VM DRAM “cache miss” is called a

page fault.• To access data, the address must

translate it to a _______________.• This translation is called

_______________ or ________________.

Page 29: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory Implementation

• Programs use virtual addresses• VM Block is called a page• A VM DRAM “cache miss” is called a

page fault.• To access data, the address must

translate it to a _physical address_.• This translation is called

_______________ or ________________.

Page 30: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory Implementation

• Programs use virtual addresses• VM Block is called a page• A VM DRAM “cache miss” is called a

page fault.• To access data, the address must

translate it to a _physical address_.• This translation is called memory

mapping_ or _virtual to physical translation.

Page 31: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

Virtual Memory Implementation

• Translation process:

• Why is Physical address smaller than Virtual?

Page offset

Page offset

Virtual page number

Physical page number

Page 32: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

Virtual Memory Implementation

• Translation process:

• Why is Physical address smaller than Virtual? DRAM is the cache – should be smaller than the total virtual address space

Page offset

Page offset

Virtual page number

Physical page number

Page 33: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory ImplementationPage faults incredibly costly

• DRAM is a cache -w Direct-mapped?w Set-associative?w Fully associative?

• Low associativity w miss rate, search time

• High associativityw miss rate, search time

Page 34: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory ImplementationPage faults incredibly costly

• DRAM is a cache -w Direct-mapped?w Set-associative?w Fully associative?

• Low associativity w high miss rate, low search time

• High associativityw low miss rate, high search time

Page 35: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory ImplementationPage faults incredibly costly

• DRAM is a cache -w Direct-mapped?w Set-associative?w Fully associative?

• Low associativity w high miss rate, low search time

• High associativityw low miss rate, high search time

Access time: ~50 cycles + search, miss penalty: hundreds of cycles

Page 36: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory ImplementationPage fault incredibly costly

• DRAM is a cache -w Direct-mapped?w Set-associative?w Fully associative!!!!

• Low associativity w high miss rate, low search time

• High associativityw low miss rate, high search time

Access time: ~50 cycles + search, miss penalty: hundreds of cycles

Page 37: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory ImplementationPage fault incredibly costly

• Fully associative!!!!• Large block size (4KB-16KB)• Sophisticated software to implement

replacement policyw updates are hidden under page fault

penaltyw Cost warranted to decrease miss rate

Page 38: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory ImplementationPage fault incredibly costly

• How costly? w Well, designers like to see fewer than 2

page faults per second, and in general definitely no more than 20 per second

w And some programs simply can’t page fault (or bad things happen)§ E.g., interrupt handlers

• Minor vs Major page faultsw Minor: page is in DRAM, but not mapped

to process

Page 39: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Address Space

• Virtual Address Spacew Located on Hard Drive (essentially) w Starts at 0 for each program

• Physical Address Spacew Located in DRAMw The “cache” for the Disk Drive

Page 40: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Does the same virtual address (in all processes) always translate to the same physical address (at one moment in time)?

• How often does a translation occur?

Page 41: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Does the same virtual address always translate to the same physical address?w No - each process has same virtual

addresses

• How often does a translation occur?

Page 42: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Does the same virtual address always translate to the same physical address?w No - each process has same virtual

addressesw Store translations in process page table

• How often does a translation occur?

Page 43: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Does the same virtual address always translate to the same physical address?w No - each process has same Virtual

addressesw Store translations in process page table

• How often does a translation occur?w At least once per instruction

Page 44: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Does the same virtual address always translate to the same physical address?w No - each process has same Virtual addressesw Store translations in process page table

• How often does a translation occur?w At least once per instructionw Need to perform translation quickly w Cache recent translations!

Page 45: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Maintaining per process page tables w Process page table maintained by ____ -

___________________________• Making translations fast

w Use a TLB (__________________________) to cache recent translations

w Fully associative but ____________________

Page 46: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Maintaining per process page tables w Process page table maintained by OS -

some pinned into DRAM• Making translations fast

w Use a TLB (__________________________) to cache recent translations

w Fully associative but ___________________

Page 47: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Maintaining per process page tables w Process page table maintained by OS -

some pinned into DRAM• Making translations fast

w Use a TLB (Translation Lookaside Buffer) to cache recent translations

w Fully associative but ___________________

Page 48: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Translation

• Maintaining per process page tables w Process page table maintained by OS -

some pinned into DRAM• Making translations fast

w Use a Translation Lookaside Buffer (TLB) to cache recent translations

w Fully associative but very small - 16-64 entries

Page 49: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 1: TLB

• Search all locations of TLB in parallel (fully-associative)

Page offsetVirtual page number

VPN PPN

Page offsetPhysical page number

Page 50: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 1: TLB

• Search all locations of TLB in parallel• Hit -

• Miss -

Page 51: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 1: TLB

• Search all locations of TLB in parallel• Hit - return address, proceed with

memory access• Miss -

Page 52: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 1: TLB

• Search all locations of TLB in parallel• Hit - return address, proceed with

memory access• Miss - retrieve translation from

process page table –

Page 53: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 1: TLB

• Search all locations of TLB in parallel• Hit - return address, proceed with

memory access• Miss - retrieve translation from

process page tablew Exception/Trap to OS

Page 54: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 2: TLB Miss

• Access the process’ page table to retrieve translation

• If valid (DRAM hit) w fill in TLB with this entryw restart TLB access and translation

• If invalid, page fault (DRAM miss)

Page 55: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Step 3: Page Fault

• Operating System invoked to swap a page in DRAM with the page requested on hard drive.

• Operating system looks up page’s location on hard drive.

• Operating system maintains replacement algorithm

• OS updates the process’ page tables

Page 56: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Putting it all together

TLB Access

Write access bit on?

Cache Hit?

Write?

TLB Hit?

Stall

Cache read

Write to $(Depends)

No

TLB MissException

No

No

No

Write protectionException

Return data

Yes

Yes

Yes

Yes

Page 57: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

TLB

• Why do we have to wait for the TLB access to complete before accessing the cache?

• What happens on a write miss to the $?

• What is the maximum number of misses encountered on a read request?

Page 58: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

TLB

• Why do we have to wait for the TLB access to complete before accessing the cache?w Cache is indexed using physical addresses which

we don’t have until after the TLB request finishes.

• What happens on a write miss to the $?• What is the maximum number of misses

encountered on a read request?

Page 59: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

TLB

• Why do we have to wait for the TLB access to complete before accessing the cache?w Cache is indexed using physical addresses which we don’t

have until after the TLB request finishes.• What happens on a write miss to the $?

w Evict line w Go get your linew Write data into $ line

• What is the maximum number of misses encountered on a read request?

Page 60: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

TLB

• Why do we have to wait for the TLB access to complete before accessing the cache?w Cache is indexed using physical addresses which we don’t have

until after the TLB request finishes.• What happens on a write miss to the $?

w Evict line w Go get your linew Write data into $ line

• What is the maximum number of misses encountered on a read request?w TLB miss, process page table miss (may require going all the way

to disk, creating its own page fault), page fault, L1 cache miss, L2 cache miss, ..., hits in memory because of page fault but might not be in any caches

Page 61: Virtual Memory - University of RichmondVirtual Memory Implementation Page fault incredibly costly •DRAM is a cache - wDirect-mapped? wSet-associative? wFully associative!!!! •Low

Virtual Memory Summary

• VM increases total available memory• VM provides multi-process

protection• TLB is necessary for fast translations• OS manages virtual memory

w Therefore it is sloooooooow