Download Demand Paging - Computer Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
CSE 410Computer Systems
L
t^
D
d P
i^
& P
Lecture 21 – Demand Paging & Page
Replacement
Demand PagingDemand
Paging
-^
We’ve hinted that pages can be moved between memory and disk
- this process is called demand paging
- is different than swapping (entire process moved, not page)
- OS uses main memory as a (page) cache of all of the data
allocated by processes in the system
- initially, pages are allocated from physical memory frames• when physical memory fills up, allocating a page in
i^
th
t^
b
i t d f
it
h
i^
l
requires some other page to be evicted from its physicalmemory frame
- evicted pages go to disk (only need to write if they are dirty)
t^
fil
- to a swap file• movement of pages between memory / disk is done by the
OS
- is transparent to the application• is transparent to the application
2
Why does this work?Why
does this work?
-^
Locality!
l l
li
- temporal locality
- locations referenced recently tend to be referenced again
soon
spatial locality
- locations near recently references locations are likely to
be referenced soon (think about why)
-^
Locality means paging can be infrequent
- once you’ve paged something in, it will be used many times– on average, you use things that are paged in– but, this depends on many things:
d
f l
lit
i^
li
ti
- degree of locality in application• page replacement policy and application reference
pattern
- amount of physical memory and application footprint
amount of physical memory and application footprint
4
Why is this
“demand” paging?
Why is this
demand
paging?
•^
Think about when a process first starts up:
- it has a brand new page table, with all PTE valid bits
‘false’
- no pages are yet mapped to physical memory
p g
y
pp
p y
y
- when process starts executing:
- instructions immediately fault on both code and
data pagesdata pages
- faults stop when all necessary code/data pages are
in memory
- only the code/data that is needed (demanded!) by• only the code/data that is needed (demanded!) by
process needs to be loaded
- what is needed changes over time, of course…
5
#1: Belady
’s Algorithm
#1: Belady s Algorithm•^
Provably optimal lowest fault rate (remember SJF?)
y
p
(^
- pick the page that won’t be used for longest time in
futureproblem: impossible to predict future
- problem: impossible to predict future -^
Why is Belady’s algorithm useful?
- as a yardstick to compare other algorithms to optimal
y
p
g
p
- if Belady’s isn’t much better than yours, yours is
pretty good
•^
Is there a lower bound?
-^
Is there a lower bound?
- unfortunately, lower bound depends on workload
- but, random replacement is pretty bad
7
#2: FIFO#2:
FIFO
•^
FIFO is obvious, and simple to implement
- when you page in something, put in on tail of list– on eviction, throw away page on head of list -^
Why might this be good?Why might this be good?
- maybe the one brought in longest ago is not being
used
•^
Why might this be bad?
-^
Why might this be bad?
- then again, maybe it is being used– have absolutely no information either way -^
FIFO suffers from Belady’s Anomaly
- fault rate might increase when algorithm is given more
physical memory
8
Approximating LRUApproximating
LRU
-^
Many approximations, all use the PTE reference bit
- keep a counter for each page– at some regular interval, for each page, do:
- if ref bit = 0, increment the counter
(hasn’t been used)
- if ref bit = 1, zero the counter
(has been used)
- regardless, zero ref bit
- the counter will contain the # of intervals since the last
reference to the page
- page with largest counter is least recently used -^
Some architectures don’t have PTE reference bits
- can simulate reference bit using the valid bit to induce faults
10
#4: LRU Clock#4:
LRU Clock
-^
AKA Not Recently Used (NRU) or Second Chance
- replace page that is “old enough”– arrange all physical page frames in a big circle (clock)
- just a circular linked list
- a “clock hand” is used to select a good LRU candidate
- sweep through the pages in circular order like a clock• if ref bit is off, it hasn’t been used recently, we have a
y
victim
- so, what is minimum “age” if ref bit is off?
- if the ref bit is on, turn it off and go to next page
- arm moves quickly when pages are needed– low overhead if have plenty of memory– if memory is large, “accuracy” of information degrades
11
Clock QuestionsClock
Questions
Will Clock always find a page to replace?
y
p g
p
- at worst it will clear all the reference bits, finally
coming around to the oldest page
If the hand is moving slowly?
If the hand is moving quickly?
If the hand is moving quickly?
- many page faults– lots of reference bits set
lots of reference bits set
13
Another Problem: allocation of framesAnother
Problem: allocation of frames
•^
In a multiprogramming system, we need a way to allocate
h
i^
l^
t^
ti
physical memory to competing processes
- what if a victim page belongs to another process?– family of replacement algorithms that takes this into
accountaccount
•^
Fixed space algorithms
- each process is given a limit of pages it can use
hen it reaches its limit it replaces from its o
n pages
- when it reaches its limit, it replaces from its own pages– local replacement: some process may do well, others
suffer
•^
Variable space algorithms
-^
Variable space algorithms
- processes’ set of pages grows and shrinks dynamically– global replacement: one process can ruin it for the rest
linux uses global replacement
- linux uses global replacement
14
#5: Working Set Size#5:
Working Set Size
•^
The working set size changes with program locality
- during periods of poor locality, more pages are
referenced
- within that period of time, the working set size is larger I t iti
l^
ki
t^
t b
i^
th
i
•^
Intuitively, working set must be in memory, otherwiseyou’ll experience heavy faulting (thrashing)
- when people ask “How much memory does Firefox
need?” really they are asking “what is Firefox’sneed? , really they are asking
what is Firefox s
average (or worst case) working set size?”
•^
Hypothetical algorithm:
- associate parameter “w” with each process
associate parameter
w
with each process
- only allow a process to start if it’s “w”, when added to
all other processes, still fits in memory
- use a local replacement algorithm within each
use a oca
ep ace
e t a go t
t^
eac
process
16
#6: Page Fault Frequency (PFF)#6:
Page Fault Frequency (PFF)
PFF is a variable-space algorithm that uses a more
p
g
ad-hoc approach
- monitor the fault rate for each process
if fault rate is above a given threshold give it more
- if fault rate is above a given threshold, give it more
memory
- so that it faults less• doesn’t always work (FIFO, Belady’s anomaly)
- if the fault rate is below threshold, take away
memorymemory
- should fault more• again, not always
17
SummarySummary •^
demand paging
start with no physical pages mapped, load them in on demand
-^
page replacement algorithms
#1: Belady’s – optimal, but unrealizable
y
p
,
#2: Fifo – replace page loaded furthest in past
#3: LRU – replace page referenced furthest in past
-^
approximate using PTE reference bit pp
g
#4: LRU Clock – replace page that is “old enough”
#5: working set – keep set of pages in memory that induces theminimal fault rate
#6: page fault frequency – grow/shrink page set as a function offault rate
-^
local vs. global replacement
should processes be allowed to evict each other’s pages?
19