Operating Systems ECE344 Ashvin Goel ECE University of Toronto Page Replacement Algorithms.
-
Upload
homer-smith -
Category
Documents
-
view
238 -
download
6
Transcript of Operating Systems ECE344 Ashvin Goel ECE University of Toronto Page Replacement Algorithms.
Operating SystemsECE344
Ashvin GoelECE
University of Toronto
Page Replacement Algorithms
2
Overview
Introduction
Page replacement algorithms
Paging issues
3
Introduction
CPU generates page fault when it accesses address not in memory, OS allocates memory frames to programs on demand
If no frame is available, OS needs to evict another page to free a frame
Which page should be evicted?o A page miss is similar to a TLB miss or a cache misso However, a miss may require accessing the disk
So miss handling can be very expensive Disk access times are at least 1000x memory access times
4
When Will Paging Work?
Paging will only work if it occurs rarely
Paging schemes depend on locality of referenceo Spatial locality
Programs tend to use a small fraction of their memory, or Memory accesses are near memory accessed recently
o Temporal locality Programs use same memory over short periods of time, or Memory accessed recently will be accessed again
Programs normally have both kinds of localityo Hence, overall cost of paging is not very high
5
Page Replacement Algorithms
Aim is to reduce page misses using smart replacement algorithmso Page table, MMU, TLB handling are VM mechanismso Page replacement is a VM policy
Several Algorithmso Optimal Algorithmo First In First Out (FIFO)o Clocko Least Recently Used (LRU)o Working Set Clocko Page Fault Frequency
6
The Optimal Page Replacement Algorithm
Select page that will not be needed for longest timeo Why is this algorithm optimal?
Time 0 1 2 3 4 5 6 7 8 9 10Requests c a d b e b a b c d
Page 0 aFrames 1 b 2 c 3 d
Page faults
a a a a b b b b c c c c d d d d
X
a a a a ab b b b bc c c c ce e e e e
X X
7
The Optimal Page Replacement Algorithm
Optimal algorithm is unrealizableo Don’t know the future of a program!o Don’t know when a given page will be next needed
However, it can be used to compare to compare the performance of other algorithmso How?
8
First In First Out (FIFO)
Replace the page that has been in memory for the longest time (i.e., replace oldest page)
Time 0 1 2 3 4 5 6 7 8 9 10Requests c a d b e b a b c a
Page 0 aFrames 1 b 2 c 3 d
Page faults
a a a bc c c c d d
X
a a a a b b b b e e e e d d d d
X X
cbed
X X
cbe a
X
9
FIFO Implementation
Implementationo Keep list of all page frames in memory
E.g., add linked list in coremapo When a frame is allocated (e.g., when page is mapped to a
new frame), add frame to the front of listo On a page fault, choose frame at end of list for replacement
Problemo The oldest page may be needed again soon
E.g., some page may be important throughout execution, when it gets old, replacing it will soon cause a page fault
o Faults may increase if algorithm is given more memory! Known as Belady’s Anomaly
10
Can We Do Better that FIFO?
We need to know page access pattern in the futureo We can take advantage of locality of referenceo Pages that have been accessed in the recent past are likely
to be accessed again and should not be replaced
How can page accesses be tracked?o Tracking each memory access is very expensiveo Tracking page access requires hardware support
11
Tracking Page Accesses
Bits in page table entry allow tracking page accesseso Referenced (R) bit
Set by processor when page is read OR written Cleared by OS/software (not by hardware)
o Dirty (D) bit Set by processor when page is written Cleared by OS/software (not by hardware)
o OS/hardware must synchronize the TLB bits with PTE bits
What if h/w doesn’t maintain these bits?o OS can simulate them
When TLB read fault occurs– Set referenced bit, make page read-only (why read-only)?
When TLB read-only or write fault occurs– Set referenced & dirty bits, make page writeable
12
Clock Algorithm
FIFO, while giving second chance to referenced pageso Keep circular list of all page frames in memoryo Maintain a clock hand, from where frames are evictedo New frames are added behind the clock hand
On a page fault, when we need to evict a page frame:o Choose page starting from clock hand
If referenced bit is set– Unset referenced bit, continue
Else if referenced bit is not set– If page is dirty
• Schedule page write, continue– Else page is clean
• Select it for replacement, done
Advance clock hand to next page
41
50
20
11
00
30
frame #
referencedbit
13
Least Recently Used (LRU)
Replace page that has been used least recently using a list of pages kept sorted in LRU order
Time 0 1 2 3 4 5 6 7 8 9 10Requests c a d b e b a b c d
Page 0 aFrames 1 b 2 c 3 d
Page faults
a a a a b b b b c c c c d d d d
X
a a a a b b b be e e e d d d d
X X
abec
X X
abd c
X
cabd
acbd
dacb
bdac
ebda
beda
abed
baed
cbae
dcba
14
LRU Implementation
Updating LRU list on each memory access is too expensive
Suppose MMU maintains a counter that is incremented each clock cycleo When page is accessed
MMU writes counter value to the page table entry This timestamp value is the time of last use
o On a page fault Software looks through the page table Identifies entry with the oldest timestamp
Problem is MMU may not provide such a counter
15
LRU Approximation
OS can maintain a counter in software
Periodically (i.e., timer interrupt), increment countero In each page with referenced bit set, write countero Clear the referenced bit, why?
On a page faulto Software looks through the page tableo Identifies entry with the oldest timestampo If several have oldest time, choose one arbitrarily
Why does this method approximate LRU?
16
Working Set Model
Working set is the set of pages a program needs currently
To measure working set, look at the last time interval To T is also called working set intervalo WS(T) = { pages referenced in interval (now, now – T) }o As T gets bigger, more pages are neededo However, working set varies slowly after a while
# of unique memoryreferences over time T
Set
of
page
s ac
cess
ed
(wor
king
set
)
17
WSClock
Goal of algorithm is to keep working set in memory, so few page faults will occur due to locality of reference
Variant of Clock algorithmo Each entry contains time of last use rather than just a
referenced bit
18
WSClock
On a page fault, clock sweeps over circular listo If referenced bit is set
Update time-of-last-use field to current virtual time– Virtual time is time application has run
Unset referenced bit, continueo Else if referenced bit is not set
Compute age by comparing current time with time-of-last-use If age of the page is less than working set interval (T)
– continue Else if page is dirty
– Schedule page write, continue Else if page is clean
– Select it for replacement, done
Compare with Clock algorithm
19
Page Fault Frequency
Working set clock requires knowing working set interval for each process
Page fault frequency can provide estimate of working set needs of a programo It declines as a program is assigned more pageso Can be used to ensure fairness in working set allocation
Too High: need to give thisprogram some more frames
Too Low: take some framesaway, give to other programs
20
Page Fault Frequency Algorithm
Measuring page fault frequency:o For each thread
On each fault, count fault (f)– f = f + 1
Every second, update faults/second (fe) via aging– fe = (1 – a) * fe + a * f, f = 0– 0 < a < 1, when a -> 1, history is ignored, a is weighting factor
Goal: Allocate frames so PFF is equal for programso Choose a victim process whose PFF is lowesto Use Clock, LRU to evict a page from victim process
21
Which Algorithm is Best?
Compare algorithms based on a reference string
Run a programo Look at which memory addresses are accessed
Pages accessed: 0000001222333300114444001123444o Eliminate duplicates
012301401234, Why?o Defines the reference string
Use the same reference string for evaluating different page replacement algorithmso Count total page faults
22
Summary
Algorithm Comment
Optimal Not implementable, but useful as a benchmark
FIFO Might throw out important pages
Clock Realistic
LRU Excellent, but difficult to implement exactly
Working Set Clock Efficient working set-base algorithm
Page Fault Frequency Fairness in working set allocation
23
Paging Issues
Paging and I/O interaction
Paging performance
Thrashing
24
Paging and I/O Interaction
Example: A thread performs a read system call, suspends during I/O, then another thread runs, has a page faulto A frame selected for eviction is the one that is involved in the
reado When I/O returns, OS will copy data from disk to new page!
Solution: Each frame has a 'do not evict me' flag, called pinned page because the page is pinned/locked in memory and cannot be evicted during I/Oo Must always remember to un-pin the page, e.g., after the I/O
completes
25
Paging Performance
Paging works best if there are plenty of free frames
If all pages are full of dirty pages, then two disk operations are needed for each page fault, one to swap out the dirty page that is being invalidated, one to read in the new page
Two methods for improving performance:o Paging daemon: swap out, in advanceo Prefetching: swap in, in advance
26
Paging Daemon
OS can use a paging thread/daemon to maintain a pool of free frames
Daemon runs replacement algorithm periodically or when pool reaches low watermarko Writes out dirty pages, marks them as freeo Frees enough pages until pool reaches high watermark
Frames in pool still hold previous contents of pageso Can be rescued if page is referenced before reallocation
Issue:o If a page is going to be written again, then it would have been
better to not swap it
27
Prefetching
Page faults can only be processed one page at a time, because disk has to be read one page at a time
Suppose we can predict future page usage at current fault, then we can prefetch other pages
What if we are wrong?
28
Thrashing
Thrashing is a situation where OS spends most time paging data from disk, and user programs do not make much progresso A livelock situation
System is over-committed:o Either, page replacement algorithm is not workingo Or, system doesn't have enough memory to hold working set
of all currently running programs
Solutiono Run more programs because CPU is idle - no!o Swapping - suspend some programs for a whileo Buy more memory
29
Think Time
What the optimal page replacement policy?o Is it achievable in practice? Why is it used?
What is the problem with FIFO page replacement?
What is the assumption used by most replacement policies to improve on FIFO?
What is the working set of a process?
Which of the policies described in these slides take working set into account?
What happens when working set does not fit in memory?