ICS 145B -- L. B ic 1 Project: Page Replacement Algorithms Textbook: pages 496-500 ICS 145B L. Bic
ICS 145B -- L. Bic 1
Project: Page Replacement Algorithms
Textbook: pages 496-500
ICS 145B
L. Bic
ICS 145B -- L. Bic 2
Assignment• implement different page replacement
algorithms (global and local)
• generate reference strings
• test and compare algorithms using reference strings
ICS 145B -- L. Bic 3
Overall Organizationgenerate reference string RS
initialize memory and aux structuresrepeat for each element r of RS { if (r is not in main memory) { record page fault replace an existing page with r } record other statistical data }
page replacement algorithm
parameters:P, p, e, m, t, len
plot and interpret data
file
F
ICS 145B -- L. Bic 4
Global page replacement• assume single-process system
• virtual memory: P pages [0..P-1]
• reference string RS: sequence of integers, p– each p is in range 0..P-1
• main memory: F frames [0..F-1]; – implement as array M[F]– each M[f] contains page number p
• page replacement: if p (from RS) not in M, select f, replace resident page: M[f]=p
ICS 145B -- L. Bic 5
Global page replacement• optimal (MIN): replace page that will not be
referenced for the longest time in the futureTime t | 0| 1 2 3 4 5 6 7 8 9 10
RS | | c a d b e b a b c d Frame 0| a| a a a a a a a a a d Frame 1| b| b b b b b b b b b b Frame 2| c| c c c c c c c c c c Frame 3| d| d d d d e e e e e e IN | | e d OUT | | d a • at page fault: search RS for most distant pg
ICS 145B -- L. Bic 6
Global page replacement• random replacement:
– generate random number r in range 0..F-1– replace page in M[r]
ICS 145B -- L. Bic 7
Global page replacement• FIFO: replace oldest page
Time t | 0| 1 2 3 4 5 6 7 8 9 10
RS | | c a d b e b a b c d Frame 0|>a|>a >a >a >a e e e e >e d Frame 1| b| b b b b >b >b a a a >a Frame 2| c| c c c c c c >c b b b Frame 3| d| d d d d d d d >d c c IN | | e a b c d OUT | | a b c d e
• maintain array index of oldest page• increment (mod F) when page replaced
ICS 145B -- L. Bic 8
Global page replacement• LRU: replace least recently used pageTime t | 0| 1 2 3 4 5 6 7 8 9 10RS | | c a d b e b a b c d Frame 0| a| a a a a a a a a a d Frame 1| b| b b b b b b b b b b Frame 2| c| c c c c e e e e e d Frame 3| d| d d d d d d d d c cIN | | e c d OUT | | c d eQ.end | d| c a d b e b a b c d | c| d c a d b e b a b c | b| b d c a d d e e a bQ.head | a| a b b c a a d d e a
ICS 145B -- L. Bic 9
Global page replacement• LRU:
– for the purposes of measurements, implement the queue directly in M; no need to maintain additional array (Q)
– at each reference to p:• find i where M[i] == p
• if p is not resident, set i=0 and record a page fault
• M[k] = M[k+1] for i k < F-1 (shift elements)
• M[F-1] = p
ICS 145B -- L. Bic 10
Global page replacement• second-chance algorithm
… 4 5 6 7 8 9 10
… b e b a b c d … >a/1 e/1 e/1 e/1 e/1 >e/1 d/1 … b/1 >b/0 >b/1 b/0 b/1 b/1 >b/0… c/1 c/0 c/0 a/1 a/1 a/1 a/0 … d/1 d/0 d/0 >d/0 >d/0 c/1 c/0 … e a c d
• maintain – current pointer – array U[F] of use-bits
ICS 145B -- L. Bic 11
Global page replacement• third-chance algorithm
– u-bit set at every reference (read or write)– w-bit set at write reference– to select a page, cycle through frames, resetting
bits, until uw==00:uw uw1 1 0 11 0 0 00 1 0 0 * (remember modification)0 0 select
ICS 145B -- L. Bic 12
Global page replacement… 0 | 1 2 3 4 5 … | c aw d bw e … >a/10 |>a/10 >a/11 >a/11 >a/11 a/00*… b/10 | b/10 b/10 b/10 b/11 b/00*… c/10 | c/10 c/10 c/10 c/10 e/10… d/10 | d/10 d/10 d/10 d/10 >d/00… | e
• maintain– current pointer – array U[F] of use-bits– array W[F] of write-bits
• no need to maintain marker bits (asterisk)
ICS 145B -- L. Bic 13
Local page replacement• pages are not selected from fixed M[F]
• instead, each process has a working set ws (= resident set of pages)
• working set grows and shrinks dynamically with program behavior:– if p (from RS) is not in ws, include it– ws shrinks based on algorithm (limited by )
ICS 145B -- L. Bic 14
Local page replacement• working set model (=3)• uses trailing window of size +1 (=WS)Time t | 0| 1 2 3 4 5 6 7 8 9 10RS | a| c c d b c e c e a d Page a | x| x x x - - - - - x x
Page b | -| - - - x x x x - - - Page c | -| x x x x x x x x x xPage d | x| x x x x x x - - - x Page e | x| x - - - - x x x x x IN | | c b e a d OUT | | e a d b
ICS 145B -- L. Bic 15
Local page replacement• working set model
– maintain array WIN[+1]– WIN represents sliding window (queue):
contains last +1 references from RS– at each reference, p:
• slide WIN to the right such that:– p becomes the right-most element of WIN– the left-most element, q, drops out of WIN
• if p was not already in WIN, record a page fault and increase ws by 1
• if q no longer in WIN, ws shrinks by 1
ICS 145B -- L. Bic 16
Local page replacement• optimal (VMIN) with =3• uses forward-looking sliding window (WS)Time t | 0| 1 2 3 4 5 6 7 8 9 10RS | d| c c d b c e c e a d Page a | -| - - - - - - - - x -
Page b | -| - - - x - - - - - - Page c | -| x x x x x x x - - -Page d | x| x x x - - - - - - x Page e | -| - - - - - x x x - - IN | | c b e a d OUT | | d b c e a
ICS 145B -- L. Bic 17
Local page replacement• VMIN
– use WIN[+1] as with WS model– WIN is forward-looking sliding window:
contains future +1 references from RS– at each reference, p:
• slide WIN to the right such that:– the the left-most element drops out of WIN– p becomes the new left-most element of WIN– the reference, q, that is steps in future becomes the right-
most element of WIN
• if q was not already in WIN, record a page fault (will occur steps in future)
ICS 145B -- L. Bic 18
Local page replacement• page fault frequency
– if time between page faults < , grow resident set: add new page to resident set
– if time between page faults , shrink resident set: add new page but remove all pages not referenced since last page fault
ICS 145B -- L. Bic 19
Local page replacement• page fault frequency
Time t | 0| 1 2 3 4 5 6 7 8 9 10RS | | c c d b c e c e a d Page a | x| x x x - - - - - x x
Page b | -| - - - x x x x x - -
Page c | -| x x x x x x x x x x
Page d | x| x x x x x x x x - x
Page e | x| x x x - - x x x x x IN | | c b e a d OUT | | ae bd
ICS 145B -- L. Bic 20
Local page replacement• PFF
– maintain virtual memory representation, VM:• VM[p].res -- page p is resident/not resident• VM[p].u -- use bit for page p
– at each reference to p: VM[p].u=1– at page fault:
• set VM[p].res=1 • record page fault • if time between page faults :
remove all resident pages with VM[p].u=0• reset u-bit of all pages (VM[*].u=0)
ICS 145B -- L. Bic 21
Generating reference strings• no locality: pick random number in [0..P-1]
• typical behavior:– periods of stable WS– punctuated by transitions: WS grows rapidly,
then settles into new stable size
ICS 145B -- L. Bic 22
Generating reference strings• model typical behavior:
– locus of reference |---------|-------|------------------------------------|0 p p+e P-1
• stable period:– assume constant rate in one direction (1 step
every m references)• transition:
– generate new locus with probability t• locus defined by p, e, m, t
ICS 145B -- L. Bic 23
Generating reference strings• algorithm to generate RS:
– select P, p, e, m, t– repeat until RS generated:
• pick m random numbers in [p..p+e]; include in RS
• generate random number 0 <= r <= 1;• if (r < t) generate new p
else increment p (mod P)
ICS 145B -- L. Bic 24
Choosing simulation constants• P: size of VM (# pages)• e: size of working set (# pages)
– P and e need to be chosen together: e<P, but:• if e is too close to P, working sets overlap after transitions• if e is too small, the program will rarely revisit previous
locations (important for LRU to benefit)• suggestion: e varies from 2 to P/10, P=500 or 1000
• p random number within [0..P-1]• m: # of times a page is referenced
– typically, m100 (see page 267, fig 8-13b)• t: length of stable period
– random number in [0..1]; typically, t<0.1 (i.e., thousands of instructions are executed between transitions)
ICS 145B -- L. Bic 25
Choosing simulation constants• length of RS:
• must be large to make behavior statistically significant• suggestion: >100,000
• write vs read access (for 3rd chance algorithm)• assume 10% (or less) of write accesses
• F: number of frames– must be chosen together with P and e– generally e<F<P
• with e approaching (or exceeding) F, thrashing is observed• with F approaching P, # page faults approaches 0• suggestion: set F=P/10; vary e from 2 to F
: window size • typically: e
ICS 145B -- L. Bic 26
Performance evaluations1. global
a. how much is gained using FIFO over Random; or LRU over FIFO
b. how close is LRU to optimal (MIN)
c. how effective are 2nd-chance and 3rd-chance in approximating LRU (for 3rd chance, assume that some percentage of requests are write ops)
– compare in terms of • average numbers of page faults
• relative overhead
ICS 145B -- L. Bic 27
Performance evaluations2. local
a. how close is WS to optimal (VMIN)b. how does page fault rate vary with (for WS,
VMIN, or PFF)c. how close is PFF to WS– compare in terms of
• average numbers of page faults• average size of working set (e.g., # of page faults
will be the same for VMIN and WS but the average working set size will be smaller for VMIN)
• relative overhead (if applicable)
ICS 145B -- L. Bic 28
Performance evaluations3. local versus global
– choose local algorithm– for a given , determine:
• average size of working set (# frames)• number of page faults
– use # frames for a global replacement• determine number of page faults• compare number of faults for local and global• relative overhead (if applicable)
ICS 145B -- L. Bic 29
Summary of tasks
• develop page replacement algorithms
• develop program to generate reference strings
• compare performance:– individuals: (1a || 1b || 2a) && 3
– groups of 3: 1c && 2b && 3
• deliverables– commented/documented code
– report presenting choices made, results obtained (plots, tables, interpretations), conclusions drawn