How to Write a Reference Page for a Project by feu29513

VIEWS: 13 PAGES: 28

More Info
									          Project: Page Replacement
                  Algorithms
                     ICS 145B
                       L. Bic


ICS 145B -- L. Bic                    1
                     Assignment
• implement different page replacement
  algorithms (global and local)
• generate reference strings
• test and compare algorithms using reference
  strings




ICS 145B -- L. Bic                          2
             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                               3
             Global page replacement
• optimal (MIN): replace page that will not be
  referenced for the longest time in the future
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   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                                                4
             Global page replacement
• random replacement:
      – generate random number r in range 0..F-1
      – replace page in M[r]




ICS 145B -- L. Bic                                 5
             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                                      6
             Global page replacement
 • LRU: replace least recently used 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   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 c
 IN     |             |                   e               c d
 OUT    |             |                   c               d e
 Q.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 b
 Q.head |            a|   a   b   b   c   a   a   d   d   e a
ICS 145B -- L. Bic                                               7
             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                                                      8
             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                            9
             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      uw
            11       01
            10       00
            01       0 0 * (remember modification)
            00       select



ICS 145B -- L. Bic                                      10
             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                             11
              Local page replacement
• pages are not selected from fixed M[F]
• instead, each process has a working set ws
• working set grows and shrinks dynamically
  with program behavior:
      – if p (from RS) is not in ws, include it
      – ws shrinks based on algorithm
• for VMIN and WS model, ws is limited by 
• for PFF, ws can in principle contain the
  entire VM
ICS 145B -- L. Bic                                12
              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   10
RS            |      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   x
Page        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                                             13
              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


ICS 145B -- L. Bic                                               14
              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   10
RS            |      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                                             15
              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                                                                 16
              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                                         17
              Local page replacement
• page fault frequency

Time        t |      0|   1   2   3   4    5   6   7   8   9    10
RS            |       |   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                                               18
              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                                           19
        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                                     20
        Generating reference strings
• model typical behavior:
      – locus of reference
            eeeeee 
      |---------|-------|------------------------------------|
      0         p                                          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                                               21
        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                                  22
        Generating reference strings
• choosing generation constants:
      p: [0..P-1]      – size of VM
      e: [E1..E2]      – size of WS
      m: [M1..M2]      – how many times is same page
                         referenced
      t: [0..1]        – length of stable period
      write/read access (for 3rd chance algorithm) –
         assume about 10% of write accesses


ICS 145B -- L. Bic                                     23
             Performance evaluations
1. 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                                    24
             Performance evaluations
2. local
      a. how close is WS to optimal (VMIN)
      b. how does page fault rate vary with  (WS,
        VMIN, or PFF)
      c. how close is PFF to WS

      – compare in terms of
            • average numbers of page faults
            • relative overhead (if applicable)



ICS 145B -- L. Bic                                   25
             Performance evaluations
3. 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                                            26
                     Summary of tasks
• develop page replacement algorithms
• develop program to generate reference strings
• compare performance:
      – individuals: (1a || 2a) && 3
      – groups of 2: 1b && 2b && 3
      – groups of 3: 1c && 2c && 3
• deliverables
      – commented/documented code
      – report presenting choices made, results obtained
        (plots, tables, interpretations), conclusions drawn
ICS 145B -- L. Bic                                            27
                     Summary of tasks
• special deal:
      – find actual reference strings (Web?) instead of
        developing program to generate them
            (must be approved by instructor ahead of time)




ICS 145B -- L. Bic                                           28

								
To top