0% found this document useful (0 votes)
72 views24 pages

COS 318: Operating Systems Virtual Memory Paging: Andy Bavier Computer Science Department Princeton University

Virtual memory paging allows processes to have more memory allocated than physically available by paging portions of memory to disk as needed. When a process accesses a page not in memory, a page fault occurs which triggers the page fault handler to page in the requested page from disk. Common page replacement algorithms like least recently used (LRU) and clock replacement with second chance aim to page out the least likely to be reused pages to minimize future faults.

Uploaded by

Ankit Komar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views24 pages

COS 318: Operating Systems Virtual Memory Paging: Andy Bavier Computer Science Department Princeton University

Virtual memory paging allows processes to have more memory allocated than physically available by paging portions of memory to disk as needed. When a process accesses a page not in memory, a page fault occurs which triggers the page fault handler to page in the requested page from disk. Common page replacement algorithms like least recently used (LRU) and clock replacement with second chance aim to page out the least likely to be reused pages to minimize future faults.

Uploaded by

Ankit Komar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

COS 318: Operating Systems Virtual Memory Paging

Andy Bavier Computer Science Department Princeton University


http://www.cs.princeton.edu/courses/archive/fall10/cos318/

Todays Topics
Paging mechanism !! Page replacement algorithms
!!

Virtual Memory Paging


!!

Simple world
"!

Load entire process into memory. Run it. Exit. Slow (especially with big processes) Wasteful of space (doesnt use all of its memory all the time) Demand paging: only bring in pages actually used Paging: only keep frequently used pages in memory Virtual memory maps some to physical pages, some to disk

!!

Problems
"! "!

!!

Solution
"! "!

!!

Mechanism:
"!

VM Paging Steps
. . . subl $20 %esp movl 8(%esp), %eax . . .

VM system
fault
v v v i v vp# vp# vp# vp# pp# pp# dp# pp# pp#

Steps
!! !! !! !! !! !!

Memory reference (may cause a TLB miss) TLB entry invalid triggers a page fault and VM handler takes over Move page from disk to memory Update TLB entry w/ pp#, valid bit Restart the instruction Memory reference again

. . .

v vp# pp#

TLB

Virtual Memory Issues


!!

How to switch a process after a fault?


"! "!

Need to save state and resume Is it the same as an interrupt? Just the faulting page or more? Want to know the future Cache always too small, which page to replace? Want to know the future...

!!

What to page in?


"! "!

!!

What to replace?
"! "!

How Does Page Fault Work?


. . . subl $20 %esp movl 8(%esp), %eax . . . VM fault handler() { Save states . . . iret }

User program should not be aware of the page fault !! Fault may have happened in the middle of the instruction! !! Can we skip the faulting instruction? !! Is a faulting instruction always restartable?
!!
6

What to Page In?


!!

Page in the faulting page


"!

Simplest, but each page in has substantial overhead May reduce page faults if the additional pages are used Waste space and time if they are not used Real systems do some kind of prefetching Some systems support for user-controlled prefetching But, many applications do not always know

!!

Page in more pages each time


"! "! "!

!!

Applications control what to page in


"! "!

VM Page Replacement
!!

Things are not always available when you want them


"! "!

It is possible that no unused page frame is available VM needs to do page replacement If there is an unused frame, get it If no unused page frame available,

!!

On a page fault
"! "!

! Find a used page frame ! If it has been modified, write it to disk ! Invalidate its current PTE and TLB entry
"! "! "!

Page Replacement

Load the new page from disk Update the faulting PTE and remove its TLB entry Restart the faulting instruction A list of unused page frames A table to map page frames to PID and virtual pages, why?
8

!!

General data structures


"! "!

Which Used Page Frame To Replace?


Random !! Optimal or MIN algorithm !! NRU (Not Recently Used) !! FIFO (First-In-First-Out) !! FIFO with second chance !! Clock !! LRU (Least Recently Used) !! NFU (Not Frequently Used) !! Aging (approximate LRU) !! Working Set !! WSClock
!!
9

Optimal or MIN
!! Algorithm:
"!

Replace the page that wont be used for the longest time (Know all references in the future) Reference string: 4 page frames 6 faults

!! Example
"! "! "!
!!

1 2 3 4 1 2 5 1 2 3 4 5

Pros
"!

Optimal solution and can be used as an off-line analysis method No on-line implementation
10

!!

Cons
"!

Revisit TLB and Page Table


Virtual address
VPage # offset

VPage# PPage# VPage# PPage# . . . VPage# PPage#

... ...

Miss

Page Table

TLB Hit
PPage #
!!

offset

Important bits for paging


"! "!

Reference: Set when referencing a location in the page Modify: Set when writing to a location in the page

11

Not Recently Used (NRU)


!!

Algorithm
"!

"!

Randomly pick a page from the following (in this order) ! Not referenced and not modified ! Not referenced and modified ! Referenced and not modified ! Referenced and modified Clear reference bits 4 page frames Reference string 8 page faults Implementable Require scanning through reference bits and modified bits

!!

Example
"! "! "!

1 2 3 4 1 2 5 1 2 3 4 5

!! !!

Pros
"!

Cons
"!

12

First-In-First-Out (FIFO)
Page out
!! !!

5 3 4 7 9 11 2 1 15

Recently loaded

Algorithm
"!

Throw out the oldest page 4 page frames Reference string 10 page faults

Example
"! "! "!

1 2 3 4 1 2 5 1 2 3 4 5

!! !!

Pros
"!

Low-overhead implementation May replace the heavily used pages


13

Cons
"!

More Frames ! Fewer Page Faults?


!!

Consider the following with 4 page frames


"! "! "!

Algorithm: FIFO replacement Reference string: 1 2 3 4 1 2 5 1 2 3 4 5 10 page faults

!!

Same string with 3 page frames


"! "! "!

Algorithm: FIFO replacement Reference string: 1 2 3 4 1 2 5 1 2 3 4 5 9 page faults!

!!

This is so called Beladys anomaly (Belady, Nelson, Shedler 1969)


14

FIFO with 2nd Chance


If ref bit = 1

Page out
!!

5 3 4 7 9 11 2 1 15

Recently loaded

Algorithm
"! "! "!

Check the reference-bit of the oldest page If it is 0, then replace it If it is 1, clear the referent-bit, put it to the end of the list, and continue searching 4 page frames Reference string: 8 page faults Simple to implement The worst case may take a long time
15

!!

Example
"! "! "!

1 2 3 4 1 2 5 1 2 3 4 5

!! !!

Pros
"! "!

Cons

Clock
!!

FIFO clock algorithm


"! "!

Hand points to the oldest page On a page fault, follow the hand to inspect pages If the reference bit is 1, set it to 0 and advance the hand If the reference bit is 0, use it for replacement

!!

Second chance
"! "!

Oldest page

!!

Compare with the FIFO with 2nd chance


"!

Whats the difference? Take a long time to go around?


16

!!

What if memory is very large


"!

Least Recently Used


Least Recently used
!!

5 3 4 7 9 11 2 1 15

Recently loaded

Algorithm
"!

Replace page that hasnt been used for the longest time

!!

! Order the pages by time of reference ! Timestamp for each referenced page Example
"! "! "!

4 page frames Reference string: 8 page faults

1 2 3 4 1 2 5 1 2 3 4 5

!!

Pros
"!

Good to approximate MIN Difficult to implement


17

!!

Cons
"!

Approximation of LRU
!!

Use CPU ticks


"! "!

For each memory reference, store the ticks in its PTE Find the page with minimal ticks value to replace

!!

Use a smaller counter


Most recently used Least recently used N categories Pages in order of last reference

LRU Crude LRU

2 categories Pages referenced since the last page fault Pages not referenced since the last page fault
254 255

8-bit count

256 categories

18

Aging: Not Frequently Used (NFU)


!!

Algorithm
"! "!

Shift reference bits into counters Pick the page with the smallest counter to replace

00000000 00000000 10000000 00000000


!!

00000000 10000000 11000000 00000000

10000000 01000000 11100000 00000000

01000000 10100000 01110000 10000000

10100000 01010000 00111000 01000000

Old example
"! "! "!

4 page frames Reference string: 8 page faults

1 2 3 4 1 2 5 1 2 3 4 5

!!

Main difference between NFU and LRU?


"!

NFU has a short history (counter length) In practice 8 bits are quite good
19

!!

How many bits are enough?


"!

Program Behavior (Denning 1968)


!!

80/20 rule
"! "!

!!

Spatial locality
"!

Neighbors are likely to be accessed The same page is likely to be accessed again in the near future
# Pages in memory

!!

Temporal locality
"!

# Page faults

> 80% memory references are within <20% of memory space > 80% memory references are made by < 20% of code

20

Working Set
!!

Main idea (Denning 1968, 1970)


"! "!

Define a working set as the set of pages in the most recent K page references Keep the working set in memory will reduce page faults significantly The set of pages of a process used in the last T seconds On a page fault, scan through all pages of the process If the reference bit is 1, record the current time for the page If the reference bit is 0, check the time of last use,
! If the page has not been used within T, replace the page ! Otherwise, go to the next

!! !!

Approximate working set


"!

An algorithm
"! "! "!

"!

Add the faulting page to the working set


21

WSClock
!! Follow

the clock hand !! If the reference bit is 1


Set reference bit to 0 "! Set the current time for the page "! Advance the clock hand
"!

!! If

the reference bit is 0, check time of last use


If the page has been used within ", go to the next If the page has not been used within " and modify bit is 1 ! Schedule the page for page out and go to the next If the page has not been used within " and modify bit is 0 ! Replace this page

"! "!

"!

22

Replacement Algorithms
!!

The algorithms
"! "! "! "! "! "! "! "! "! "!

Optimal or MIN algorithm NRU (Not Recently Used) FIFO (First-In-First-Out) FIFO with second chance Clock LRU (Least Recently Used) NFU (Not Frequently Used) Aging (approximate LRU) Working Set WSClock

!!

Which are your top two?

23

Summary
!!

VM paging
"! "! "!

Page fault handler What to page in What to page out

LRU is good but difficult to implement !! Clock (FIFO with 2nd hand) is considered a good practical solution !! Working set concept is important
!!

24

You might also like