Upload
aditya-rai
View
224
Download
0
Embed Size (px)
Citation preview
7/28/2019 Page Replacement ALgos
1/42
P r l m n l ri hm
1
OS 2009-10
7/28/2019 Page Replacement ALgos
2/42
When a page fault occurs2
OS has to choose a page to evict from memory
If the page has been modified, the OS has to schedulea disk write of the page
e page us rea overwr es a page n memory e.g.4Kbytes)
,
Same problem applies to memory caches
OS 2009-10
7/28/2019 Page Replacement ALgos
3/42
Benchmarking3
Tests are done b eneratin a e references (either
from real code or random) Sequences of page numbers (no real address, no
offset)
Example:
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
OS 2009-10
7/28/2019 Page Replacement ALgos
4/42
Optimal page replacement4
At the moment of page fault:
Label each page in memory is labeled with the number ofinstructions that will be executed before that page is firstreferenced
Replace the page with the highest number: i.e. postpone asmuch as possible the next page fault
, , The OS cant look into the future to know how long itll take to
reference every page again
OS 2009-10
7/28/2019 Page Replacement ALgos
5/42
Example: optimal5
Sequence
6 page faults
OS 2009-10
7/28/2019 Page Replacement ALgos
6/42
Beladys anomaly6
Try this sequence
With 3 page frames
With FIFO, with the optimal algorithm, (later) with the LRU
OS 2009-10
7/28/2019 Page Replacement ALgos
7/42
Not recently used algorithm7 Use Referenced and Modified bits
R&M are in hardware, potentially changed at eachreference to memory
On clock interrupt the Rbit is cleared
On page fault, to decide which page to evict: Classify:
Class 0 R=0,M=0 Class 1 R=0,M=1
Class 2 R=1,M=0 Class 3 R=1,M=1
Replace a page at random from the lowest class
OS 2009-10
7/28/2019 Page Replacement ALgos
8/42
FIFO replacement8
FIFO, first in first out for a es
Clearly not particularly optimal It mi ht end u removin a a e that is still
referenced since it only looks at the pages age
Rarely used in pure form
1 8 3 7 2
Latest load
OS 2009-10
Next removal
7/28/2019 Page Replacement ALgos
9/42
Example (FIFO)9
Sequence
PF
12 page faults
OS 2009-10
7/28/2019 Page Replacement ALgos
10/42
Second chance algorithm10
Like FIFO but
Before throwing out a page checks the R bit: If 0 remove it
If 1 clear it and move the page to the end of the list (as it werejust been loaded)
= ,FIFO (why?)
1 8 3 7 2
OS 2009-10
Latest load
7/28/2019 Page Replacement ALgos
11/42
Clock page algorithm11
implemented differently: Check startin from the
latest visited page
More efficient:
ba
lists entries all thetime dg
ef
OS 2009-10
7/28/2019 Page Replacement ALgos
12/42
Least recently used (LRU)12
Why: pages recently used tend to be used again
soon (on average)
List of all pages in memory (most recently in thehead, least recently used in the tail)
List operations are expensive (e.g. find a page)
So, difficult
OS 2009-10
7/28/2019 Page Replacement ALgos
13/42
Least recently used (LRU)13
Idea! Get a counter, maybe a 64bit counter
The counter is incremented after each instructionand stored into the page entry at each reference
Store the value of the counter in each entry of thepage table (last access time to the page)
,counter value (this is the LRU page)
Nice & good but expensive: it requires dedicatedhardware
OS 2009-10
7/28/2019 Page Replacement ALgos
14/42
Example LRU14
Sequence
PF
9 page faults
OS 2009-10
7/28/2019 Page Replacement ALgos
15/42
NFU algorithm15
Since LRU is ex ensive
NFU: Not Frequently Used algorithm At each clock interru t add the R bit to a counter for
each page: i.e. count how often a page is referenced
Remove page with lowest counter value Unfortunately, this version tends not to forget
anything
OS 2009-10
7/28/2019 Page Replacement ALgos
16/42
Aging (NFU + forgetting)16
Take NFU but
At each clock interrupt: Ri ht shift the counters (divide b 2)
Add the R bit to the left (MSB)
As for NFU remove pages with lowest counter
Note: this is different from LRU since the time
granularity is a clock tick and not every memoryreference!
OS 2009-10
7/28/2019 Page Replacement ALgos
17/42
NFU+ageing17
OS 2009-10
7/28/2019 Page Replacement ALgos
18/42
Process behavior18
Locality of reference: most of the time the last kreferences are within a finite set of pages < a largeaddress space
the working set(WS) of the process
Knowin the workin set of rocesses we can do versophisticate things (e.g. pre-paging)
Pre-paging: load pages before letting the process to
run so a e page- au ra e s ow, a so, nowthe WS when I swap the process then I can expect itto be the same in the future time when I reload the
OS 2009-10
process in memory
7/28/2019 Page Replacement ALgos
19/42
Working set19
set
Working
k-most-recent memory references
OS 2009-10
7/28/2019 Page Replacement ALgos
20/42
7/28/2019 Page Replacement ALgos
21/42
e.g.: WS based algorithm21
OS 2009-10
7/28/2019 Page Replacement ALgos
22/42
WSClock algorithm22
Use the circular structure (as seen earlier)
At page fault examine the page pointed by the handle
R=1, page in the WS dont remove it (set R to zero)
, , ,and decide depending on age)
If M=1, schedule disk write appropriately to procrastinateas ong as poss e a process sw c If return to starting point, then one page will eventually be clean
(maybe after a context switch)
c e u ng mu t p e s wr te can e e c ent n e c ent systems(with disk scheduling and multiple disks)
No write is schedulable (R=1 always), just choose a clean page
OS 2009-10
o c ean page, use e curren page un er e an e
7/28/2019 Page Replacement ALgos
23/42
WSClock23
R=1
R=0
OS 2009-10
7/28/2019 Page Replacement ALgos
24/42
7/28/2019 Page Replacement ALgos
25/42
25
Design issues
OS 2009-10
7/28/2019 Page Replacement ALgos
26/42
Design issues26
Local vs. lobal allocation olic
When a page fault occurs, whose page should the OS evict? Which process should get more or less pages? Monitor the number of page faults for every process (PFF
page fault frequency)
,less page faults
OS 2009-10
7/28/2019 Page Replacement ALgos
27/42
7/28/2019 Page Replacement ALgos
28/42
Load control28
If the WS of all rocesses > memor , theres
thrashing E.g. the PFF says a process requires more memory
but none require less
Solution: swapping swap a process out of memoryan re-ass gn ts pages to ot ers
OS 2009-10
7/28/2019 Page Replacement ALgos
29/42
Page size29
Pa e size n a es of memor
Average process size s, in pages s/p Each entr in the a e table re uires e tes
On averagep/2 is lost (fragmentation)
used within pages
Wasted memor : 2 + se
Minimizing it yields the optimal page size (undersim lif in assum tions
OS 2009-10
7/28/2019 Page Replacement ALgos
30/42
7/28/2019 Page Replacement ALgos
31/42
Other issues31
Shared a es handle shared a es e. . ro ram
code) Sharing data (e.g. shared memory)
Cleaning policy
Paging algorithms work better if there are a lot of freepages ava a e
Pages need to be swapped out to disk
and evict pages if there are to few)
OS 2009-10
7/28/2019 Page Replacement ALgos
32/42
Page fault handling32
1. Page fault, the HW traps to the kernel. . .
2. Save general purpose microprocessor information (registers, PC, PSW, etc.)3. The OS looks for which page caused the fault (sometimes this information is
already somewhere within the MMU).
protection fault is generated, and the process killed)5. The OS looks for a free page frame, if none is found then the replacement
algorithm is run. =calling process)
7. When the page frame is clean, the OS schedules another transfer to read in therequired page from disk
. ,9. The faulting instruction is backed up, the situation before the fault is restored,
the process resumes execution
OS 2009-10
7/28/2019 Page Replacement ALgos
33/42
7/28/2019 Page Replacement ALgos
34/42
Why?34
Many separate address spaces (segments) (e.g. data, stack,co e, an many ot ers nee e
Each segment is separate (e.g. addresses from 0 to someMAX)
Segments might have different lengths
Segment number + address within segment n ng s s mp e rar es w n eren segmen scan assume addresses starting from 0) e.g. if a part of thelibraries is recompiled the remainder of the code is
una ecte Shared library (DLLs) implementation is simpler (the
sharin is sim ler)
OS 2009-10
7/28/2019 Page Replacement ALgos
35/42
Comparing paging and segmentation35
Need the programmer be aware that
this technique is being used?
No Yes
How many linear address spaces are 1 Many
there?
Can the total address space exceed
the size of physical memory
Yes Yes
Can procedures and data be No Yes
distinguished and separately
protected?
Can tables whose size fluctuate be
accommodated easily?
No Yes
s s ar ng o proce ures e ween
users facilitated?
o es
Why was this technique invented? To get a large linear address space
without having to buy more physical
memory
To allow programs and data to be
broken up into logically independent
address spaces and to aid sharing
OS 2009-10
and protection
7/28/2019 Page Replacement ALgos
36/42
Pure segmentations36
B B B
A A AD
Operating system Operating system Operating system Operating system
OS 2009-10
7/28/2019 Page Replacement ALgos
37/42
7/28/2019 Page Replacement ALgos
38/42
7/28/2019 Page Replacement ALgos
39/42
The segment descriptor39
This is used by the microcode within the Pentiumto work with segments
Limit in pages/bytes
Base 24-31 G D 0 Limit 16-19 P DPL S Type Base 16-23
16/32 bit segment
Privilege level Segment type
protection
Base 0-15 Limit 0-15
Segment present in memory
Page size is 4K System/application
Limit (20 bits)
OS 2009-10
n ex r v ege
Selector
7/28/2019 Page Replacement ALgos
40/42
7/28/2019 Page Replacement ALgos
41/42
7/28/2019 Page Replacement ALgos
42/42
More on the Pentiums42
TLB, to avoid re eated accesses to memor
The whole thing can be used with just a singlesegment to obtain a linear 32bit address space
Set base and limit appropriately
Protection (a few bits)
OS 2009-10