Virtual Memory Lecture 25 CS301 DRAM as cache What about programs - - PowerPoint PPT Presentation

virtual memory
SMART_READER_LITE
LIVE PREVIEW

Virtual Memory Lecture 25 CS301 DRAM as cache What about programs - - PowerPoint PPT Presentation

Virtual Memory Lecture 25 CS301 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.


slide-1
SLIDE 1

Virtual Memory

Lecture 25 CS301

slide-2
SLIDE 2

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.

slide-3
SLIDE 3

Virtual Memory

  • Memory Size -
  • Protection -
slide-4
SLIDE 4

Virtual Memory

  • Memory Size - allows total memory

allocation to exceed DRAM capacity.

  • Protection -
slide-5
SLIDE 5

Virtual Memory

  • Memory Size - allows total memory

allocation to exceed DRAM capacity.

  • Protection - programs may not access

each other’s memory.

slide-6
SLIDE 6

Multi-Processing - no VM

  • Program A begins

Prog A

slide-7
SLIDE 7

Multi-Processing - no VM

  • Program A begins
  • Program B begins

Prog A Prog B What happens if A wants more memory?

slide-8
SLIDE 8

Multi-Processing - no VM

  • Program A begins
  • Program B begins

Prog A Prog B What 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.

slide-9
SLIDE 9

Multi-Processing - no VM

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

Prog B

slide-10
SLIDE 10

Multi-Processing - no VM

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

Prog B Prog C

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Virtual Memory

  • Use hard drive for memory that does

not fit in DRAM

  • Allocate memory in pages
  • Provide protection by page
slide-13
SLIDE 13

Address Space

  • Virtual Address Space
  • Physical Address Space
slide-14
SLIDE 14

Address Space

  • Virtual Address Space

w Located on Hard Drive (essentially) w Starts at 0 for each program

  • Physical Address Space
slide-15
SLIDE 15

Address Space

  • Virtual Address Space

w Located on Hard Drive (essentially) w Starts at 0 for each program

  • Physical Address Space

w Located in DRAM w The “cache” for the Disk Drive

slide-16
SLIDE 16

Multi-Processing - VM

  • Program A begins
  • Program B begins

Prog A Hard Drive Virtual Address 16K Physical Address

slide-17
SLIDE 17

Multi-Processing - VM

  • Program A begins
  • Program B begins

Prog A Hard Drive Prog B 16K 24K Virtual Address Physical Address

slide-18
SLIDE 18

Multi-Processing - VM

  • Program A begins
  • Program B begins

What happens if A wants more memory? Prog A Prog B 16K 24K Virtual Address Physical Address Hard Drive

slide-19
SLIDE 19

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 20K 24K Virtual Address Physical Address

slide-20
SLIDE 20

Multi-Processing - VM

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

Hard Drive Prog B 24K Virtual Address Physical Address

slide-21
SLIDE 21

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 24K Prog C Virtual Address Physical Address 28K

slide-22
SLIDE 22

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”

slide-23
SLIDE 23

Virtual Memory is like caching…

  • DRAM is the cache for the hard drive

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”

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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).

slide-26
SLIDE 26

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 ________________.

slide-27
SLIDE 27

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 ________________.

slide-28
SLIDE 28

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 ________________.

slide-29
SLIDE 29

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 ________________.

slide-30
SLIDE 30

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.

slide-31
SLIDE 31

Translation

Virtual Memory Implementation

  • Translation process:
  • Why is Physical address smaller than Virtual?

Page offset Page offset Virtual page number Physical page number

slide-32
SLIDE 32

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

slide-33
SLIDE 33

Virtual Memory Implementation Page faults incredibly costly

  • DRAM is a cache -

w Direct-mapped? w Set-associative? w Fully associative?

  • Low associativity

w miss rate, search time

  • High associativity

w miss rate, search time

slide-34
SLIDE 34

Virtual Memory Implementation Page 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 associativity

w low miss rate, high search time

slide-35
SLIDE 35

Virtual Memory Implementation Page 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 associativity

w low miss rate, high search time

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

slide-36
SLIDE 36

Virtual Memory Implementation Page 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 associativity

w low miss rate, high search time

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

slide-37
SLIDE 37

Virtual Memory Implementation Page fault incredibly costly

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

replacement policy

w updates are hidden under page fault penalty w Cost warranted to decrease miss rate

slide-38
SLIDE 38

Virtual Memory Implementation Page 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 faults

w Minor: page is in DRAM, but not mapped to process

slide-39
SLIDE 39

Address Space

  • Virtual Address Space

w Located on Hard Drive (essentially) w Starts at 0 for each program

  • Physical Address Space

w Located in DRAM w The “cache” for the Disk Drive

slide-40
SLIDE 40

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?
slide-41
SLIDE 41

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?
slide-42
SLIDE 42

Translation

  • Does the same virtual address always

translate to the same physical address?

w No - each process has same virtual addresses w Store translations in process page table

  • How often does a translation occur?
slide-43
SLIDE 43

Translation

  • Does the same virtual address always

translate to the same physical address?

w No - each process has same Virtual addresses w Store translations in process page table

  • How often does a translation occur?

w At least once per instruction

slide-44
SLIDE 44

Translation

  • Does the same virtual address always

translate to the same physical address?

w No - each process has same Virtual addresses w Store translations in process page table

  • How often does a translation occur?

w At least once per instruction w Need to perform translation quickly w Cache recent translations!

slide-45
SLIDE 45

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 ____________________

slide-46
SLIDE 46

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 ___________________

slide-47
SLIDE 47

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 ___________________

slide-48
SLIDE 48

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

slide-49
SLIDE 49

Step 1: TLB

  • Search all locations of TLB in parallel

(fully-associative)

Page offset Virtual page number VPN PPN Page offset Physical page number

slide-50
SLIDE 50

Step 1: TLB

  • Search all locations of TLB in parallel
  • Hit -
  • Miss -
slide-51
SLIDE 51

Step 1: TLB

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

memory access

  • Miss -
slide-52
SLIDE 52

Step 1: TLB

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

memory access

  • Miss - retrieve translation from

process page table –

slide-53
SLIDE 53

Step 1: TLB

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

memory access

  • Miss - retrieve translation from

process page table

w Exception/Trap to OS

slide-54
SLIDE 54

Step 2: TLB Miss

  • Access the process’ page table to

retrieve translation

  • If valid (DRAM hit)

w fill in TLB with this entry w restart TLB access and translation

  • If invalid, page fault (DRAM miss)
slide-55
SLIDE 55

Step 3: Page Fault

  • Operating System invoked to swap a

page in DRAM with the page requested

  • n hard drive.
  • Operating system looks up page’s

location on hard drive.

  • Operating system maintains

replacement algorithm

  • OS updates the process’ page tables
slide-56
SLIDE 56

Putting it all together

TLB Access Write access bit on? Cache Hit? Write? TLB Hit? Stall Cache read Write to $ (Depends) No TLB Miss Exception No No No Write protection Exception Return data Yes Yes Yes Yes

slide-57
SLIDE 57

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?

slide-58
SLIDE 58

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?

slide-59
SLIDE 59

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 line w Write data into $ line

  • What is the maximum number of misses

encountered on a read request?

slide-60
SLIDE 60

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 line w 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

slide-61
SLIDE 61

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