Page Replacement

advertisement
CSE120
Principles of Operating Systems
Prof Yuanyuan (YY) Zhou
Page Replacement
Review
l 
At a memory instruction
– 
– 
Does it use a virtual address or physical address?
What can happen?
l 
l 
l 
Demand paging
– 
– 
l 
What is it?
Why introduces it?
Page fault
– 
– 
– 
2
Best case
What if you are unlucky?
– 
What is it?
Why does it happen?
Who handles it?
How costly is it?
November 11, 2015
CSE 120
Demand Paging Algorithm
l 
Algorithm Never bring a page into primary memory
until its needed.
1. 
2. 
3. 
4. 
Page fault
Check if a valid virtual memory address. Kill job if not.
If valid reference, check if its cached in memory already
(perhaps for some other process.) If so, skip to 7).
Find a free page frame. If no free page available, choose
one to evict
4a. If the victim page is dirty, write it out to disk first
5. 
6. 
7. 
3
Suspend user process, Map address into disk block and
fetch disk block into page frame..
When disk read finished, add vm mapping for page frame.
If necessary, restart process.
11/11/15
CS 323 - Operating Systems,
Yuanyuan Zhou
Demand Paging (detail)
l 
Some
– 
– 
– 
Pages are evicted to disk when memory is full
Pages loaded from disk when referenced again
References to evicted pages cause a TLB miss
l 
– 
– 
l 
OS allocates a page frame, reads page from disk
When I/O completes, the OS fills in PTE, marks it valid, and
restarts faulting process
Dirty vs. clean pages
– 
– 
4
PTE was invalid, causes fault
Actually, only dirty pages (modified) need to be written to
disk
Clean pages do not – but you need to know where on disk
to read them from again
November 11, 2015
CSE 120 – Lecture 11 – Page
Replacement
Page Replacement
1. 
2. 
Find location of page on disk
Find a free page frame
1. 
2. 
3. 
3. 
4. 
5. 
5
If free page frame use it
Otherwise, select a page frame using the page
replacement algorithm
Write the selected page to the disk and update any
necessary tables
Read the requested page from the disk.
Restart the user process.
It is necessary to be careful of synchronization
problems. For example, page faults may occur for
pages being paged out.
11/11/15
CS 323 - Operating Systems,
Yuanyuan Zhou
Issue: Eviction
l 
Hopefully, kick out a less-useful page
– 
Dirty pages require writing, clean pages don’t
l 
– 
Where do you write?
l 
l 
l 
To “swap space”
Goal: kick out the page that’s least useful
Problem: how do you determine utility?
– 
– 
6
Hardware has a dirty bit for each page frame indicating this
page has been updated or not
Kick out pages that aren’t likely to be used again
Heuristic: temporal locality exists
November 11, 2015
CSE 120
Page Replacement Strategies
l 
The Principle of Optimality
– 
l 
Random page replacement
– 
l 
An approximation to LRU.
Working Set
– 
7
Replace the page that is used least often
NRU - Not Recently Used
– 
l 
Replace the page that has not been used for the longest time
LFU - Least Frequently Used
– 
l 
Replace the page that has been in primary memory the longest
LRU - Least Recently Used
– 
l 
Choose a page randomly
FIFO - First in First Out
– 
l 
Replace the page that will not be used again the farthest time in
the future.
Keep in memory those pages that the process is actively using.
November 11, 2015
CSE 120
Belady’s Algorithm
l 
Known as the optimal page replacement algorithm
because it has the lowest fault rate for any page
reference stream/sequence
– 
– 
l 
Idea: Replace the page that will not be used for the longest
time in the future
Problem: Have to predict the future
Why is Belady’s useful then? Use it as a yardstick
– 
– 
– 
Compare implementations of page replacement algorithms
with the optimal to gauge room for improvement
If optimal is not much better, then algorithm is pretty good
If optimal is much better, then algorithm could use some
work
l 
8
Random replacement is often the lower bound
November 11, 2015
CSE 120
Optimal Example
12 references,
7 faults
Hit Ratio: 5/12
Miss Ratio: 7/12
9
Evict c (farthest in
the future)
November 11, 2015
CSE 120
First-In First-Out (FIFO)
l 
FIFO is an obvious algorithm and simple to
implement
– 
– 
l 
Why might this be good?
– 
l 
– 
Then again, maybe it’s not
We don’t have any info to say one way or the other
FIFO suffers from “Belady’s Anomaly”
– 
10
Maybe the one brought in the longest ago is not being used
Why might this be bad?
– 
l 
Maintain a list of pages in order in which they were paged in
On replacement, evict the one brought in longest time ago
The fault rate might actually increase when the algorithm is
given more memory (very bad)
November 11, 2015
CSE 120
FIFO
12 references, 9
faults
Miss rate: 9/12
Hit rate: 3/12
11
Evict A (oldest)
Evict B (oldest)
November 11, 2015
CSE 120
Intuitive Paging Behavior with
Increasing Number of Page Frames
12
November 11, 2015
CSE 120
Belady's Anomaly (for FIFO)
As the number
of page frames
increase, so
does the fault
rate.
12 references,
10 faults
13
November 11, 2015
CSE 120
Least Recently Used (LRU)
l 
LRU uses reference information to make a more
informed replacement decision
– 
– 
– 
l 
Implementation
– 
– 
14
Idea: We can’t predict the future, but we can make a guess
based upon past experience
On replacement, evict the page that has not been used for
the longest time in the past (Belady’s: future)
When does LRU do well? When does LRU do poorly?
To be perfect, need to time stamp every reference (or
maintain a stack) – much too costly
So we need to approximate it
November 11, 2015
CSE 120
LRU
12 references,
10 faults
Evict A (least recent)
Evict B (least recent)
15
November 11, 2015
CSE 120
Least Recently Used Issues
l 
l 
Does not suffer from Belady's anomaly
How to track “recency”?
– 
use time
record time of reference with page table entry
l  use counter as clock
l  search for smallest time.
l 
– 
use stack
remove reference of page from stack (linked list)
l  push it on top of stack
l 
l 
16
both approaches require large processing
overhead, more space, and hardware
support.
November 11, 2015
CSE 120
LRU and Anomalies
Anomalies
cannot
occur,
why?
12 references,
8 faults
17
November 11, 2015
CSE 120
NRU: A LRU Approximation
l 
l 
NRU: Evict a page that is NOT recently used;
LRU: evict a page that is LEAST recently used;
NRU Implementation: simpler than LRU
– 
– 
– 
– 
– 
– 
– 
18
additional reference bits
a register is kept per page
a one bit is set in the register if the page is referenced
the register is shifted by one after some time interval
00110011 would be accessed more recently than 00010111
the page with register holding the lowest number is the least
recently used.
the value may not be unique. use FIFO to resolve conflicts.
November 11, 2015
CSE 120
LRU Clock
(A Simple Not Recently Used)
l 
A Simple Not Recently Used (NRU) – Used by Unix
– 
– 
– 
Replace page that is “cold enough”
Arrange all of physical page frames in a big circle (clock)
A clock hand is used to select a good LRU candidate
l 
l 
l 
– 
– 
– 
Arm moves quickly when pages are needed
Low overhead when plenty of memory
If memory is large, “accuracy” of information degrades
l 
19
Sweep through the pages in circular order like a clock
If the ref bit is off, it hasn’t been used recently
–  What is the minimum “age” if ref bit is off?
If the ref bit is on, turn it off and go to next page
What does it degrade to?
November 11, 2015
CSE 120
Switching Gear
l 
l 
20
So far, all we have talked about is memory
management for a single process
What about multiple applications/processes?
November 11, 2015
CSE 120
Thrashing and CPU Utilization
l 
l 
l 
l 
21
As the page fault rate goes up, processes get suspended on page
out queues for the disk.
the system may try to optimize performance by starting new jobs.
l  But is it always good?
starting new jobs will reduce the number of page frames available
to each process, increasing the page fault requests.
system throughput plunges.
November 11, 2015
CSE 120
Fixed vs. Variable Space
l 
l 
In a multiprogramming system, we need a
way to allocate memory to competing
processes
Problem: How to determine how much
memory to give to each process?
– 
Fixed space algorithms
l 
l 
l 
– 
Variable space algorithms
l 
l 
22
Each process is given a limit of pages it can use
When it reaches the limit, it replaces from its own pages
Local replacement
–  Some processes may do well while others suffer
Process’ set of pages grows and shrinks dynamically
Global replacement
–  One process can ruin it for the rest
November 11, 2015
CSE 120
Working Set
l 
l 
l 
23
the working set model
assumes locality.
the principle of locality
states that a program
clusters its access to
data and text temporally.
As the number of page
frames increases above
some threshold, the page
fault rate will drop
dramatically.
November 11, 2015
CSE 120
Working Set Model
l 
A working set of a process is used to model
the dynamic locality of its memory usage
– 
l 
Definition
– 
– 
l 
24
Defined by Peter Denning in 60s
WS(t,w) = {pages P such that P was referenced in
the time interval (t, t-w)}
t – time, w – working set window (measured in
page refs)
A page is in the working set (WS) only if it
was referenced in the last w references
November 11, 2015
CSE 120
Working Set in Action
l 
Algorithm
– 
– 
l 
l 
25
if # free page frames > working set of some
suspended process, then activate process and
map in all its working set
if working set size of some process increases and
no page frame is free, suspend process and
release all its pages
moving window over reference string used
for determination.
keeping track of working set.
November 11, 2015
CSE 120
Working Set Example
Window size
is 
12 references,
8 faults
26
November 11, 2015
CSE 120
Working Set Size
l 
The working set size is the number of pages in the
working set
– 
l 
The working set size changes with program locality
– 
– 
l 
During periods of poor locality, you reference more pages
Within that period of time, the working set size is larger
Intuitively, want the working set to be the set of
pages a process needs in memory to prevent heavy
faulting
– 
– 
27
The number of pages referenced in the interval (t, t-w)
Each process has a parameter w that determines a working
set with few faults
Denning: Don’t run a process unless working set is in
memory
November 11, 2015
CSE 120
Page Fault Frequency Version of
Working Set
28
November 11, 2015
CSE 120
Working Set Solution
l 
l 
l 
29
Approximate working set model using timer
and reference bit.
Set timer to interrupt after approximately x
references, τ.
Remove pages that have not been
referenced and reset reference bit.
November 11, 2015
CSE 120
Locality
l 
Most paging schemes depend on locality
– 
l 
Temporal locality
– 
l 
Locations near recently referenced locations are likely to be
referenced soon
Although the cost of paging is high, if it is infrequent
enough it is acceptable
– 
30
Locations referenced recently likely to be referenced again
Spatial locality
– 
l 
Processes reference pages in localized patterns
Processes usually exhibit both kinds of locality during their
execution, making paging practical
November 11, 2015
CSE 120
Page Size Considerations
l 
Small pages
– 
Pros:
l 
l 
– 
Cons
l 
l 
require large page tables
Large pages
– 
Pros
l 
l 
– 
Small page table
I/O transfers have high seek time, so better to transfer more
data per seek
Cons:
l 
31
Locality of reference tends to be small (256)
Less fragmentation
Internal fragmentation
November 11, 2015
CSE 120
Summary
l 
Page replacement algorithms
– 
– 
– 
Belady’s – optimal replacement (minimum # of faults)
FIFO – replace page loaded furthest in past
LRU – replace page referenced furthest in past
l 
– 
– 
– 
l 
LRU Clock – replace page that is “old enough”
Working Set – keep the set of pages in memory that has
minimal fault rate (the “working set”)
Page Fault Frequency – grow/shrink page set as a function
of fault rate
Multiprogramming
– 
32
Approximate using PTE reference bit
Should a process replace its own page, or that of another?
November 11, 2015
CSE 120
Download