CSE325 Principles of Operating Systemscs325/spring 11/Lectures/Lec14-VirtualMemory.pdfDemand Paging...

Post on 11-Jul-2020

0 views 0 download

Transcript of CSE325 Principles of Operating Systemscs325/spring 11/Lectures/Lec14-VirtualMemory.pdfDemand Paging...

CSE325 Principles of Operating Systems

Virtual Memory

David P. Duggan dduggan@sandia.gov

March 29, 2011

3/30/11 CSE325 - Virtual Memory 2

Outline •  Paging •  Why virtual memory? •  Virtual memory organization •  Demand paging, handle page faults •  Page replacement

– Random Replacement – Belady’s Optimal Algorithm – Least Recently Used (LRU) – Least Frequently Used (LFU) – First In First Out (FIFO)

Paging Fundamentals •  Memory management scheme •  Allows use of non-contiguous physical

memory by a process •  Requires a backing store •  Requires hardware support

3/30/11 CSE325 - Virtual Memory 3

Basic Method •  Physical memory is structured into frames •  Logical memory is structured into pages •  Size of frames and pages are the same •  Backing store is structured into fixed-size

blocks the same size as frames •  Addresses are divided into 2 parts

– Page number (p) [index into page table] – Page offset [index into page]

3/30/11 CSE325 - Virtual Memory 4

Basic Method, Cont. •  Hardware support

– Could be implemented in registers – Page-table base register (PTBR) – Translation look-aside buffer (TLB)

•  Small, usually 64 to 1024 entries

•  Protection – Pages can be set as read-only, read-write – Page-table length register (PTLR)

•  Page table has valid-invalid designators 3/30/11 CSE325 - Virtual Memory 5

Shared Pages •  Some code is pure, or reentrant, code •  Many processes can share those pages

•  What types of programs might have reentrant code?

3/30/11 CSE325 - Virtual Memory 6

Page Tables •  Large memory space can make large page

tables •  Can use a two-level page table structure

– Section, page, offset

•  Hashed page tables •  Inverted page tables

3/30/11 CSE325 - Virtual Memory 7

3/30/11 CSE325 - Virtual Memory 8

Motivation for Virtual Memory •  Virtual memory – separation of user logical memory

from physical memory. –  Only part of the program needs to be in memory for

execution. –  Physical address space can be non-contiguous. –  Logical address space can therefore be much larger than

physical address space. –  Allows address spaces to be shared by several processes. –  Allows for more efficient process creation.

•  Virtual memory can be implemented via:

–  Demand paging –  Demand segmentation

3/30/11 CSE325 - Virtual Memory 9

Virtual Memory Organization

⇒"

Virtual Memory That is Larger Than Physical Memory

10

Why virtual memory is an feasible idea?

•  Address space is logically partitioned –  Text, data, stack –  Initialization, main, error handle

•  Different parts have different reference patterns:

Initialization code (used once) Code for Φ1 Code for Φ2 Code for Φ3 Code for error 1 Code for error 2 Code for error 3 Data & stack

Address Space for Pi

30%

20%

35%

15%

<1%

<1%

Execution time

Is there any correlation between the size of a part of the address space and the amount of time that a process will execute in that locality?

Locality

3/30/11 CSE325 - Virtual Memory 11

Virtual Memory •  Every process has code and data locality

– Code tends to execute in a few fragments at one time

– Tend to reference same set of data structures •  Dynamically load/unload currently-used

address space fragments as the process executes

•  Uses dynamic address relocation/binding – Generalization of base-limit registers – Physical address corresponding to a compile-

time address is not bound until run time

3/30/11 CSE325 - Virtual Memory 12

Virtual Memory (Cont.)

•  Since binding changes with time, use a dynamic virtual address map, Bt

Virtual Address Space

Bt

13

Fragmented Virtual Address Space

Virtual Address Space for Pi Virtual Address Space for Pj

Virtual Address Space for Pk

Secondary Memory

•  Complete virtual address space is stored in secondary memory

Primary Memory

0

n-1

Physical Address Space

•  Fragments of the virtual address space are dynamically loaded into primary memory at any given time

•  Each address space is fragmented

3/30/11 CSE325 - Virtual Memory 14

Size of Blocks of Memory •  Virtual memory system transfers “blocks” of the

address space to/from primary memory •  Fixed size blocks: System-defined pages are

moved back and forth between primary and secondary memory

•  Variable size blocks: Programmer-defined segments – corresponding to logical fragments – are the unit of movement

•  Paging is the commercially dominant form of virtual memory today

3/30/11 CSE325 - Virtual Memory 15

Demand Paging •  Bring a page into memory only when it is

needed (benefits?)

•  Page is needed ⇒ ? –  invalid reference ⇒ abort –  not-in-memory ⇒ bring to memory

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

reference to it

3/30/11 CSE325 - Virtual Memory 16

Demand Paging •  A page is a fixed size, 2h, block of virtual

addresses •  A page frame is a fixed size, 2h, block of physical

memory (the same size as a page) •  When a virtual address, x, in page i is referenced

by the CPU –  If page i is loaded at page frame j, the virtual address is

relocated to page frame j –  If page is not loaded, the OS interrupts the process and

loads the page into a page frame

17

Valid-Invalid Bit •  With each page table entry a valid–invalid bit is associated

(1 ⇒ in-memory, 0 ⇒ not-in-memory) •  Initially valid–invalid bit is set to 0 on all entries •  Example of a page table snapshot:

•  During address translation, if valid–invalid bit in page table entry is 0 ⇒ page fault

1"1"1"1"0"

0"0"

"

Frame #" valid-invalid bit"

page table"

3/30/11 CSE325 - Virtual Memory 18

Page Table When Some Pages Are Not in Main Memory Important!

3/30/11 CSE325 - Virtual Memory 19

Page Fault •  If there is ever a reference to a page, first reference

will trap to OS ⇒ page fault •  OS looks at another table to decide:

–  Invalid reference ⇒ abort. –  Just not in memory.

•  Find an empty frame. •  Swap page into frame. •  Reset tables, validation bit = 1. •  Restart instruction

3/30/11 CSE325 - Virtual Memory 20

Handling a Page Fault

3/30/11 CSE325 - Virtual Memory 21

Demand Paging Algorithm

1.  Page fault occurs 2.  Process with missing page is interrupted 3.  Memory manager locates the missing page 4.  Page frame is unloaded (replacement policy) 5.  Page is loaded in the vacated page frame 6.  Page table is updated 7.  Process is continued with instruction that caused

page fault

3/30/11 CSE325 - Virtual Memory 22

Addresses •  Suppose there are G= 2g×2h=2g+h virtual

addresses and H=2j+h physical addresses assigned to a process –  Each page/page frame is 2h addresses –  There are 2g pages in the virtual address space –  2j page frames are allocated to the process –  Rather than map individual addresses

• Bt maps the 2g pages to the 2j page frames •  That is, page_framej = Bt(pagei) •  Address k in pagei corresponds to address k in

page_framej

3/30/11 CSE325 - Virtual Memory 23

Page-Based Address Translation •  Let N = {d0, d1, … dn-1} be the pages •  Let M = {b0, b1, …, bm-1} be page frames •  Virtual address, i, satisfies 0≤i<G= 2g+h •  Physical address, k = U2h+V (0≤V<G= 2h )

– U is page frame number – V is the line number within the page (offset) –  Bt:[0:G-1] → <U, V> ∪ {Ω} –  Since every page is size c=2h

•  page number = U = ⎣i/c⎦ •  line number = V = i mod c

3/30/11 CSE325 - Virtual Memory 24

What happens if there is no free frame?

•  Page replacement – find some page in memory, but not really in use, swap it out –  algorithm –  performance – want an algorithm which

will result in minimum number of page faults

•  Same page may be brought into memory several times

3/30/11 CSE325 - Virtual Memory 25

Performance of Demand Paging •  Page Fault Rate 0 ≤ p ≤ 1.0

–  if p = 0 no page faults –  if p = 1, every reference is a fault

•  Page fault overhead is: –  Time to swap page out, not always needed –  Time to swap page in –  Time to restart process

•  Effective Access Time (EAT) EAT = (1 – p) x memory access time + p x page fault overhead

3/30/11 CSE325 - Virtual Memory 26

Demand Paging Example •  Memory access time = 1 microsecond

•  50% of the time the page that is being

replaced has been modified and therefore needs to be swapped out

•  Swap Page Time = 10 msec = 10,000 µsec •  EAT = (1 – p) x 1 + p(15000)

= 1 + 14999p (in µsec)

3/30/11 CSE325 - Virtual Memory 27

Need For Page Replacement

3/30/11 CSE325 - Virtual Memory 28

Page Replacement •  Prevent over-allocation of memory by modifying

page-fault service routine to include page replacement

•  Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk

•  Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory

29

Basic Page Replacement 1.  Find the location of the desired page on disk 2.  Find a free frame:

- If there is a free frame, use it - If there is no free frame, use a page

replacement algorithm to select a victim frame 3.  Read the desired page into the (newly) free frame.

Update the page and frame tables. 4.  Continue the process from instruction that caused the

page fault

3/30/11 CSE325 - Virtual Memory 30

Page Replacement