SlideShare a Scribd company logo
1 of 96
Download to read offline
Operating Systems
       CMPSCI 377
Virtual Memory & Paging
                   Emery Berger
University of Massachusetts Amherst




UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Virtual vs. Physical Memory
    Programmers do not get direct access to


    physical memory – they get access to
    virtual memory
        One level of indirection
    

              Address you see is not “real” address
          




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   2
Virtual Memory

    Processes use virtual addresses


        Addresses start at 0
    

        OS lays process down on pages
    

    MMU (memory-management unit):


        Translates virtual to physical addresses
    

        Maintains page table (big hash table):
    

        virtual -> physical
        TLB (translation lookaside buffer) = cache of
    

        recently used page translations

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   3
Mapping Virtual to Physical




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   4
Why Virtual Memory?
    Why?


        Simpler
    

              Everyone gets illusion of
          

              whole address space
        Isolation
    

              Every process protected
          

              from every other
        Optimization
    

              Reduces space
          

              requirements


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   5
Sharing
    Paging allows sharing

    of memory across
    processes
        Reduces memory
    
        requirements
    Shared stuff includes

    code, data
        Code always R/O
    

        Data copy-on-write
    



    Requires hardware support to be fast


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   6
Translation Lookaside Buffer (TLB)
    TLB: fast, fully associative memory


        Caches page table entries
    

        Stores page numbers (key) and frame (value)
    

        in which they are stored
    Assumption: locality of reference


        Locality in memory accesses =
    

        locality in address translation
    TLB sizes: 8 to 2048 entries


        Powers of 2 simplifies translation
    

        of virtual to physical addresses
        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   7
TLB Action




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   8
Overview
    A Day in the Life of a Page


        Allocation
    

        Use
    

        Eviction
    

        Reuse
    

    Replacement policies


        Comparison
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   9
A Day in the Life of a Page
     Allocate some memory


    char * x = new char[16];

           ultimately from sbrk or mmap
      

                  void * ptr = sbrk(n);
              

                  void * ptr = mmap(0, sz, …, -1, 0);
              


0x40001000



    0x40001040 → 0x4000104F



                                                                      virtual memory layout


          UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science           10
A Day in the Life of a Page
                 Update page tables
             

   char * x = new char[16];




0x40001000



  0x40001040 → 0x4000104F


                                                       virtual                            physical
                                                       memory                             memory
                                                       layout                              layout
                   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science              11
A Day in the Life of a Page
                 Write contents – dirty page
             

   strcpy(x, “hello”);




0x40001000



  0x40001040 → 0x4000104F


                                                       virtual                            physical
                                                       memory                             memory
                                                       layout                              layout
                   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science              12
A Day in the Life of a Page
    Other processes fill up memory…





                                          virtual                            physical
                                          memory                             memory
                                          layout                              layout
      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science              13
A Day in the Life of a Page
    Forcing our page to be evicted (paged out)





     virtual                       physical                             swap
     memory                        memory                               space
     layout                         layout                              (disk)
      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       14
A Day in the Life of a Page
    Now page unmapped & protected





    virtual                        physical                             swap
    memory                         memory                               space
    layout                          layout                              (disk)
      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       15
A Day in the Life of a Page
              Touch page – swap it in
          

   y[0] = x[0];




    0x40001000




0x40001040 → 0x4000104F


                                virtual                                physical         swap
                                memory                                 memory           space
                                layout                                  layout          (disk)
                 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science            16
A Day in the Life of a Page
              Touch page – swap it in
          

   y[0] = x[0];




    0x40001000




0x40001040 → 0x4000104F


                                virtual                                physical         swap
                                memory                                 memory           space
                                layout                                  layout          (disk)
                 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science            17
Overview
    A Day in the Life of a Page


        Allocation
    

        Use
    

        Eviction
    

        Reuse
    

    Replacement policies


        Comparison
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   18
Cost of Paging
    Usually in algorithms, we pick algorithm


    with best asymptotic worst-case
        Paging: worst-case analysis useless!
    

        Easy to construct adversary:
    

        every page requires page fault

                               A, B, C, D, E, F, G, H, I, J, A...


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   19
Cost of Paging
    Worst-case analysis – useless


            Easy to construct adversary example:
        

            every page requires page fault




                                   A, B, C, D, E, F, G, H, I, J, A...
    A


                               size of available memory




            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   20
Cost of Paging
    Worst-case analysis – useless


        Easy to construct adversary example:
    

        every page requires page fault




                               A, B, C, D, E, F, G, H, I, J, A...
    AB


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   21
Cost of Paging
    Worst-case analysis – useless


        Easy to construct adversary example:
    

        every page requires page fault




                               A, B, C, D, E, F, G, H, I, J, A...
    ABC


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   22
Cost of Paging
    Worst-case analysis – useless


        Easy to construct adversary example:
    

        every page requires page fault




                               A, B, C, D, E, F, G, H, I, J, A...
    ABCD


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   23
Cost of Paging
    Worst-case analysis – useless


        Easy to construct adversary example:
    

        every page requires page fault




                               A, B, C, D, E, F, G, H, I, J, A...
    ABCDE


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   24
Cost of Paging
    Worst-case analysis – useless


        Easy to construct adversary example:
    

        every page requires page fault




                               A, B, C, D, E, F, G, H, I, J, A...
    FBCDE


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   25
Cost of Paging
    Worst-case analysis – useless


        Easy to construct adversary example:
    

        every page requires page fault




                               A, B, C, D, E, F, G, H, I, J, A...
    FGHI               J


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   26
Competitive Analysis
    Instead of worst-case:


    compare algorithms to optimal
    replacement policy (OPT)
        How much worse is algorithm than optimal?
    




    Result: LRU & FIFO both “k-competitive”


        k = size of queue
    

        Can incur k times more misses than OPT
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   27
Optimal Replacement (MIN/OPT)
    Evict page accessed furthest in future


        Optimal page replacement algorithm
    

              Invented by Belady (“MIN”), a.k.a. “OPT”
          


    Provably optimal policy


        Just one small problem...
    

        Requires predicting the future
        Useful point of comparison against other
    

        algorithms
              How far from optimal
          




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   28
MIN/OPT example

     sequence of page accesses




                                                          contents of page frames

    Page faults: 5




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science          29
Least-Recently Used (LRU)
    Evict page not used in longest time


    (least-recently used)
        Approximates OPT
    

              If recent past ≈ predictor of future
          


        Variant used in all real operating systems
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   30
LRU example




    Page faults: ?




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   31
LRU example




    Page faults: 5




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   32
LRU, example II




    Page faults: ?




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   33
LRU, example II




    Page faults: 12!


        Loop: well-known worst-case for LRU
    

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   34
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   35
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                               A, B, C, D, A, B, C, D, ...
    A


                           size of available memory




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   36
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...
    AB


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   37
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...
    ABC


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   38
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...
    ABD


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   39
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...
    ABD


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   40
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...
    ABD


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   41
Most-Recently Used (MRU)


    Evict most-recently used page


    Shines for LRU’s worst-case: loop that

    exceeds RAM size

                             A, B, C, D, A, B, C, D, ...
    ABD


                         size of available memory




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   42
FIFO
    First-in, first-out: evict oldest page


    As competitive as LRU, but

    performs miserably in practice!
        Ignores locality
    

        Suffers from Belady’s anomaly:
    

              More memory can mean more paging!
          


        LRU & similar algs. do not
    

              Stack algorithms – more memory means ≥ hits
          




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   43
FIFO & Belady’s Anomaly




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   44
LRU: No Belady’s Anomaly




    Why no anomaly for LRU?




     UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   45
The End
    Next time:

    how real OS approximate LRU




     UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   46
Virtual Memory in the Real World
    Implementing exact LRU


    Approximating LRU


        Hardware Support
    

        Clock
    

        Segmented queue
    

    Multiprogramming


        Global LRU
    

        Working Set
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   47
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



                                   A, B, C, B, C, C, D




      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   48
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



    A
                                     A, B, C, B, C, C, D
    1




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   49
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



    A       B
                                     A, B, C, B, C, C, D
    1       2




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   50
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



    A       B         C
                                     A, B, C, B, C, C, D
    1       2         3




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   51
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



    A       B         C
                                     A, B, C, B, C, C, D
    1       4         3




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   52
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



    A       B         C
                                     A, B, C, B, C, C, D
    1       4         5




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   53
Implementing Exact LRU
    On each reference, time stamp page


    When we need to evict: select oldest page

    = least-recently used



    A       B         C
                                     A, B, C, B, C, C, D
    1       4         6




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   54
Implementing Exact LRU


    On each reference, time stamp page


    When we need to evict: select oldest page


    = least-recently used

    D
    A       B         C
                                     A, B, C, B, C, C, D
    1
    7       4         6


LRU page


              How should we implement this?
        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   55
Implementing Exact LRU
    Could keep pages in order

    – optimizes eviction
        Priority queue:
    
        update = O(log n), eviction = O(log n)

    Optimize for common case!


        Common case: hits, not misses
    

        Hash table:
    
        update = O(1), eviction = O(n)


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   56
Cost of Maintaining Exact LRU


    Hash tables: too expensive


        On every reference:
    

              Compute hash of page address
          

              Update time stamp
          


        Unfortunately: 10x – 100x more
    

        expensive!



        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   57
Cost of Maintaining Exact LRU

    Alternative: doubly-linked list


        Move items to front when referenced
    

        LRU items at end of list
    

        Still too expensive
    

              4-6 pointer updates per reference
          




    Can we do better?





        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   58
Virtual Memory in the Real World
    Implementing exact LRU


    Approximating LRU


        Hardware Support
    

        Clock
    

        Segmented queue
    

    Multiprogramming


        Global LRU
    

        Working Set
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   59
Hardware Support
    Maintain reference bits for every page


            On each access, set reference bit to 1
        

            Page replacement algorithm periodically
        

            resets reference bits




    A           B         C
                                         A, B, C, B, C, C, D
    1           1         1




            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   60
Hardware Support
    Maintain reference bits for every page


            On each access, set reference bit to 1
        

            Page replacement algorithm periodically
        

            resets reference bits




    A           B         C
                                         A, B, C, B, C, C, D
    0           0         0

                                     reset reference bits


            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   61
Hardware Support
    Maintain reference bits for every page


            On each access, set reference bit to 1
        

            Page replacement algorithm periodically
        

            resets reference bits




    A           B         C
                                         A, B, C, B, C, C, D
    0           1         0




            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   62
Hardware Support
    Maintain reference bits for every page


            On each access, set reference bit to 1
        

            Page replacement algorithm periodically
        

            resets reference bits




    A           B         C
                                         A, B, C, B, C, C, D
    0           1         1




            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   63
Hardware Support
    Maintain reference bits for every page


            On each access, set reference bit to 1
        

            Page replacement algorithm periodically
        

            resets reference bits




    A           B         C
                                         A, B, C, B, C, C, D
    0           1         1




            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   64
Hardware Support
    Maintain reference bits for every page


         On each access, set reference bit to 1
     

         Page replacement algorithm periodically
     

         resets reference bits
         Evict page with reference bit = 0
     




    D
    A        B         C
                                      A, B, C, B, C, C, D
    0
    1        1         1


    Cost per miss = O(n)


         UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   65
Virtual Memory in the Real World
    Implementing exact LRU


    Approximating LRU


        Hardware Support
    

        Clock
    

        Segmented queue
    

    Multiprogramming


        Global LRU
    

        Working Set
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   66
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           1                    1
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      1
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       67
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           1                    1
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      1
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       68
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           1                    1
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      1
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       69
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           0                    1
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      1
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       70
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           0                    1
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      1
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       71
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           0                    0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      1
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       72
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A                    C
        Checks reference bit of
    
                                           0                    0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       73
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           1
                                           0                    0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       74
The Clock Algorithm


    Variant of FIFO & LRU                                                      B

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           0                    0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       75
The Clock Algorithm


    Variant of FIFO & LRU                                                      B
                                                                               F

                                                                               0
                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           0                    0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       76
The Clock Algorithm


    Variant of FIFO & LRU                                                      B
                                                                               F

                                                                               0
                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           0                    1
                                                                0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       77
The Clock Algorithm


    Variant of FIFO & LRU                                                      B
                                                                               F

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           0                    1
                                                                0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       78
The Clock Algorithm


    Variant of FIFO & LRU                                                      B
                                                                               F

                                                                               0
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           0                    1
                                                                0
        next frame
        If reference bit = 0,
    
                                                     D
        replace page, set bit to 1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       79
The Clock Algorithm


    Variant of FIFO & LRU                                                      B
                                                                               F

                                                                               0
                                                                               1
    Keep frames in circle


    On page fault, OS:

                                           A
                                           E                    C
        Checks reference bit of
    
                                           0                    0
                                                                1
        next frame
        If reference bit = 0,
    
                                                     D
                                                     G
        replace page, set bit to 1
                                                      1
                                                      0
        If reference bit = 1, set bit
    

        to 0, advance pointer to
                                      A, B, C, D, B, C, E, F, C, G
        next frame

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science       80
Enhancing Clock
    Recall: don’t write back unmodified pages


        Idea: favor eviction of unmodified pages
    

        Extend hardware to keep another bit:
    

        modified bit
    Total order of tuples: (ref bit, mod bit)


        (0,0), (0,1), (1,0), (1,1)
    

        Evict page from lowest nonempty class
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   81
Page Replacement, Enhanced Clock
     OS scans at most three times


           Page (0,0) – replace that page
    

           Page (0,1) – write out page, clear mod bit
    

           Page (1,0), (1,1) – clear reference bit
    

     Passes:


           all pages (0,0) or (0,1)
    1.

           all pages (0,1) - write out pages
    2.

           all pages (0,0) – replace any page
    3.




     Fast, but still coarse approximation of LRU




         UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   82
Segmented Queue
    Real systems: segment queue into two


        approximate for frequently-referenced pages
    

              e.g., first 1/3 page frames – fast
          


        exact LRU for infrequently-referenced pages
    

              last 2/3 page frames; doubly-linked list – precise
          


    How do we move between two segments?





            clock                                          exact LRU
        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   83
Virtual Memory in the Real World
    Implementing exact LRU


    Approximating LRU


        Hardware Support
    

        Clock
    

        Segmented queue
    

    Multiprogramming


        Global LRU
    

        Working Set
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   84
Multiprogramming & VM

    Multiple programs compete for main


    memory
      Processes move memory from and to disk
    

     Pages needed by one process may get

      squeezed out by another process
    thrashing - effective cost of memory access
      = cost of disk access = really really bad
    Must balance memory across processes to


    avoid thrashing

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   85
Global LRU
    Put all pages from all processes in one pool


        Manage with LRU (Segmented Queue)
    

        Used by Linux, BSD, etc.
    

    Advantages:


        Easy
    

    Disadvantages:


        Many
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   86
Global LRU Disadvantages
    No isolation between processes


        One process touching many pages can force
    
        another process’ pages to be evicted
    Priority ignored, or inverted


        All processes treated equally
    

    Greedy (or wasteful) processes rewarded


        Programs with poor locality squeeze out
    
        those with good locality
        Result: more page faults
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   87
Global LRU Disadvantages, Cont.
    “Sleepyhead” problem


        Intermittent, important process
    

        Every time it wakes up – no pages! – back to
    
        sleep...
    Susceptible to denial of service


        Non-paying “guest”, lowest priority, marches
    
        over lots of pages – gets all available memory

    Alternatives?





        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   88
Working Set
    Denning: Only run processes whose


    working set fits in RAM
        Other processes: deactivate (suspend)
    

    Classical definition:


    working set = pages touched in last 
    references
    Provides isolation


        Process’s reference behavior only affects
    

        itself


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   89
Working Set Problems
    Algorithm relies on key parameter, 


        How do we set ?
    

        Is there one correct ?
    

              Different processes have different timescales over
          

              which they touch pages
    Not acceptable (or necessarily possible) to


    suspend processes altogether

    Not really used


        Very rough variant used in Windows
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   90
Solution: CRAMM
    New VM management alg:


    Cooperative Robust Automatic Memory
    Management
    [OSDI 2006, Yang et al.]
    Redefine working set size =


    pages required to spend < n% time paging
        CRAMM default = 5%
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   91
Calculating WSS w.r.t 5%
                                    Memory reference sequence
                    mmmm g h h h k kmmm n kmmm n
                    nnnnnggj j jknnkcknn
                    bdddef i i i l l lccl l l
                    abeef
                    kkcf
                    ccl
                    ll

                                                                         LRU Queue
         mmmmm j h h g e d d b a
         nakkk
         bnnnni igf fec
         akccc
         kcllj
         cl
         l    l                                                   Pages in Least Recently
                                                                       Used order
         1                                              14

                                                                       Hit Histogram
         00004000000100
             0
             1
             2
             3      0
                                                                    Associated with each
                                                                        LRU position
     5
                                                                        Fault Curve
faults
                                                                                
                                                                   faulti   hist i  1
     1
                                                                                i
         1      4                           11          14
                                                  pages




             UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science            92
Computing hit histogram
       Not possible in standard VM:
   
           Global LRU queues
       

           No per process/file information or control
       

             Difficult to estimate app’s WSS / available memory


       CRAMM VM:
   

           Per process/file page management:
       

                 Page list: Active, Inactive, Evicted
             

                 Add & maintain histogram
             




           UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   93
Managing pages per process
                       Pages protected by turning                    Pages evicted to disk.
                       off permissions (minor fault)                 (major fault)
 Active
(CLOCK)                     Inactive (LRU)                              Evicted (LRU)
                                                                Major fault
 Header

             Refill & Adjustment                                Evicted
 Page
 Des

                     Minor fault
 AVL node




            faults



                                                                                        Pages
                                            Histogram
                 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science           94
Controlling overhead
                      Pages protected by turning                     Pages evicted to disk.
                      off permissions (minor fault)                  (major fault)
 Active
(CLOCK)                     Inactive (LRU)                              Evicted (LRU)

 Header

 Page
 Des
 AVL node


                                                                    Buffer




            faults
              control the boundary:
              1% of execution time
                                                                                        Pages
                                            Histogram
                 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science           95
The End




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   96

More Related Content

What's hot

Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory ManagementAkmal Cikmat
 
Operating system paging and segmentation
Operating system paging and segmentationOperating system paging and segmentation
Operating system paging and segmentationhamza haseeb
 
Structure of the page table
Structure of the page tableStructure of the page table
Structure of the page tableduvvuru madhuri
 
Ch6 CPU Scheduling galvin
Ch6 CPU Scheduling galvinCh6 CPU Scheduling galvin
Ch6 CPU Scheduling galvinShubham Singh
 
Intro (Distributed computing)
Intro (Distributed computing)Intro (Distributed computing)
Intro (Distributed computing)Sri Prasanna
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memorysgpraju
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Dma transfer
Dma transferDma transfer
Dma transfergmnithya
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory managementrprajat007
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual MemoryArchith777
 
Unification and Lifting
Unification and LiftingUnification and Lifting
Unification and LiftingMegha Sharma
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memoryvampugani
 
4.5 mining the worldwideweb
4.5 mining the worldwideweb4.5 mining the worldwideweb
4.5 mining the worldwidewebKrish_ver2
 

What's hot (20)

Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory Management
 
Operating system paging and segmentation
Operating system paging and segmentationOperating system paging and segmentation
Operating system paging and segmentation
 
Cpu organisation
Cpu organisationCpu organisation
Cpu organisation
 
Structure of the page table
Structure of the page tableStructure of the page table
Structure of the page table
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Cache memory
Cache memoryCache memory
Cache memory
 
Ch6 CPU Scheduling galvin
Ch6 CPU Scheduling galvinCh6 CPU Scheduling galvin
Ch6 CPU Scheduling galvin
 
Intro (Distributed computing)
Intro (Distributed computing)Intro (Distributed computing)
Intro (Distributed computing)
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Vliw or epic
Vliw or epicVliw or epic
Vliw or epic
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Dma transfer
Dma transferDma transfer
Dma transfer
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Unification and Lifting
Unification and LiftingUnification and Lifting
Unification and Lifting
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
4.5 mining the worldwideweb
4.5 mining the worldwideweb4.5 mining the worldwideweb
4.5 mining the worldwideweb
 
Demand paging
Demand pagingDemand paging
Demand paging
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 

Viewers also liked

Viewers also liked (8)

Paging and Segmentation in Operating System
Paging and Segmentation in Operating SystemPaging and Segmentation in Operating System
Paging and Segmentation in Operating System
 
Memory management
Memory managementMemory management
Memory management
 
04 Cache Memory
04  Cache  Memory04  Cache  Memory
04 Cache Memory
 
Paging
PagingPaging
Paging
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Memory management
Memory managementMemory management
Memory management
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 

Similar to Virtual Memory and Paging

Operating Systems - Virtual Memory
Operating Systems - Virtual MemoryOperating Systems - Virtual Memory
Operating Systems - Virtual MemoryEmery Berger
 
Garbage Collection without Paging
Garbage Collection without PagingGarbage Collection without Paging
Garbage Collection without PagingEmery Berger
 
Operating Systems - Architecture
Operating Systems - ArchitectureOperating Systems - Architecture
Operating Systems - ArchitectureEmery Berger
 
Operating Systems - Garbage Collection
Operating Systems - Garbage CollectionOperating Systems - Garbage Collection
Operating Systems - Garbage CollectionEmery Berger
 
Operating Systems - Introduction
Operating Systems - IntroductionOperating Systems - Introduction
Operating Systems - IntroductionEmery Berger
 
Operating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementOperating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementEmery Berger
 
Exterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High ProbabilityExterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High ProbabilityEmery Berger
 

Similar to Virtual Memory and Paging (7)

Operating Systems - Virtual Memory
Operating Systems - Virtual MemoryOperating Systems - Virtual Memory
Operating Systems - Virtual Memory
 
Garbage Collection without Paging
Garbage Collection without PagingGarbage Collection without Paging
Garbage Collection without Paging
 
Operating Systems - Architecture
Operating Systems - ArchitectureOperating Systems - Architecture
Operating Systems - Architecture
 
Operating Systems - Garbage Collection
Operating Systems - Garbage CollectionOperating Systems - Garbage Collection
Operating Systems - Garbage Collection
 
Operating Systems - Introduction
Operating Systems - IntroductionOperating Systems - Introduction
Operating Systems - Introduction
 
Operating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementOperating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory Management
 
Exterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High ProbabilityExterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High Probability
 

More from Emery Berger

Doppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language BarrierDoppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language BarrierEmery Berger
 
Dthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic MultithreadingDthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic MultithreadingEmery Berger
 
Programming with People
Programming with PeopleProgramming with People
Programming with PeopleEmery Berger
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationEmery Berger
 
DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)Emery Berger
 
Operating Systems - Advanced File Systems
Operating Systems - Advanced File SystemsOperating Systems - Advanced File Systems
Operating Systems - Advanced File SystemsEmery Berger
 
Operating Systems - File Systems
Operating Systems - File SystemsOperating Systems - File Systems
Operating Systems - File SystemsEmery Berger
 
Operating Systems - Networks
Operating Systems - NetworksOperating Systems - Networks
Operating Systems - NetworksEmery Berger
 
Operating Systems - Queuing Systems
Operating Systems - Queuing SystemsOperating Systems - Queuing Systems
Operating Systems - Queuing SystemsEmery Berger
 
Operating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingOperating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingEmery Berger
 
Operating Systems - Concurrency
Operating Systems - ConcurrencyOperating Systems - Concurrency
Operating Systems - ConcurrencyEmery Berger
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationEmery Berger
 
Operating Systems - Synchronization
Operating Systems - SynchronizationOperating Systems - Synchronization
Operating Systems - SynchronizationEmery Berger
 
Processes and Threads
Processes and ThreadsProcesses and Threads
Processes and ThreadsEmery Berger
 
MC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained EnvironmentsMC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained EnvironmentsEmery Berger
 
Vam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory AllocatorVam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory AllocatorEmery Berger
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementQuantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementEmery Berger
 
DieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe LanguagesDieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe LanguagesEmery Berger
 
Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++Emery Berger
 
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Emery Berger
 

More from Emery Berger (20)

Doppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language BarrierDoppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language Barrier
 
Dthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic MultithreadingDthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic Multithreading
 
Programming with People
Programming with PeopleProgramming with People
Programming with People
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance Evaluation
 
DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)
 
Operating Systems - Advanced File Systems
Operating Systems - Advanced File SystemsOperating Systems - Advanced File Systems
Operating Systems - Advanced File Systems
 
Operating Systems - File Systems
Operating Systems - File SystemsOperating Systems - File Systems
Operating Systems - File Systems
 
Operating Systems - Networks
Operating Systems - NetworksOperating Systems - Networks
Operating Systems - Networks
 
Operating Systems - Queuing Systems
Operating Systems - Queuing SystemsOperating Systems - Queuing Systems
Operating Systems - Queuing Systems
 
Operating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingOperating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel Computing
 
Operating Systems - Concurrency
Operating Systems - ConcurrencyOperating Systems - Concurrency
Operating Systems - Concurrency
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced Synchronization
 
Operating Systems - Synchronization
Operating Systems - SynchronizationOperating Systems - Synchronization
Operating Systems - Synchronization
 
Processes and Threads
Processes and ThreadsProcesses and Threads
Processes and Threads
 
MC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained EnvironmentsMC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained Environments
 
Vam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory AllocatorVam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory Allocator
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementQuantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
 
DieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe LanguagesDieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe Languages
 
Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++
 
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
 

Recently uploaded

Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 

Recently uploaded (20)

Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 

Virtual Memory and Paging

  • 1. Operating Systems CMPSCI 377 Virtual Memory & Paging Emery Berger University of Massachusetts Amherst UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  • 2. Virtual vs. Physical Memory Programmers do not get direct access to  physical memory – they get access to virtual memory One level of indirection  Address you see is not “real” address  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2
  • 3. Virtual Memory Processes use virtual addresses  Addresses start at 0  OS lays process down on pages  MMU (memory-management unit):  Translates virtual to physical addresses  Maintains page table (big hash table):  virtual -> physical TLB (translation lookaside buffer) = cache of  recently used page translations UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3
  • 4. Mapping Virtual to Physical UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 4
  • 5. Why Virtual Memory? Why?  Simpler  Everyone gets illusion of  whole address space Isolation  Every process protected  from every other Optimization  Reduces space  requirements UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5
  • 6. Sharing Paging allows sharing  of memory across processes Reduces memory  requirements Shared stuff includes  code, data Code always R/O  Data copy-on-write  Requires hardware support to be fast  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
  • 7. Translation Lookaside Buffer (TLB) TLB: fast, fully associative memory  Caches page table entries  Stores page numbers (key) and frame (value)  in which they are stored Assumption: locality of reference  Locality in memory accesses =  locality in address translation TLB sizes: 8 to 2048 entries  Powers of 2 simplifies translation  of virtual to physical addresses UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 7
  • 8. TLB Action UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8
  • 9. Overview A Day in the Life of a Page  Allocation  Use  Eviction  Reuse  Replacement policies  Comparison  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9
  • 10. A Day in the Life of a Page Allocate some memory  char * x = new char[16]; ultimately from sbrk or mmap  void * ptr = sbrk(n);  void * ptr = mmap(0, sz, …, -1, 0);  0x40001000 0x40001040 → 0x4000104F virtual memory layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10
  • 11. A Day in the Life of a Page Update page tables  char * x = new char[16]; 0x40001000 0x40001040 → 0x4000104F virtual physical memory memory layout layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11
  • 12. A Day in the Life of a Page Write contents – dirty page  strcpy(x, “hello”); 0x40001000 0x40001040 → 0x4000104F virtual physical memory memory layout layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12
  • 13. A Day in the Life of a Page Other processes fill up memory…  virtual physical memory memory layout layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13
  • 14. A Day in the Life of a Page Forcing our page to be evicted (paged out)  virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14
  • 15. A Day in the Life of a Page Now page unmapped & protected  virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15
  • 16. A Day in the Life of a Page Touch page – swap it in  y[0] = x[0]; 0x40001000 0x40001040 → 0x4000104F virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16
  • 17. A Day in the Life of a Page Touch page – swap it in  y[0] = x[0]; 0x40001000 0x40001040 → 0x4000104F virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17
  • 18. Overview A Day in the Life of a Page  Allocation  Use  Eviction  Reuse  Replacement policies  Comparison  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18
  • 19. Cost of Paging Usually in algorithms, we pick algorithm  with best asymptotic worst-case Paging: worst-case analysis useless!  Easy to construct adversary:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19
  • 20. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... A size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20
  • 21. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... AB size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21
  • 22. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... ABC size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22
  • 23. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... ABCD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23
  • 24. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... ABCDE size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24
  • 25. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... FBCDE size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 25
  • 26. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... FGHI J size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 26
  • 27. Competitive Analysis Instead of worst-case:  compare algorithms to optimal replacement policy (OPT) How much worse is algorithm than optimal?  Result: LRU & FIFO both “k-competitive”  k = size of queue  Can incur k times more misses than OPT  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 27
  • 28. Optimal Replacement (MIN/OPT) Evict page accessed furthest in future  Optimal page replacement algorithm  Invented by Belady (“MIN”), a.k.a. “OPT”  Provably optimal policy  Just one small problem...  Requires predicting the future Useful point of comparison against other  algorithms How far from optimal  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 28
  • 29. MIN/OPT example sequence of page accesses contents of page frames Page faults: 5  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 29
  • 30. Least-Recently Used (LRU) Evict page not used in longest time  (least-recently used) Approximates OPT  If recent past ≈ predictor of future  Variant used in all real operating systems  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 30
  • 31. LRU example Page faults: ?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 31
  • 32. LRU example Page faults: 5  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 32
  • 33. LRU, example II Page faults: ?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 33
  • 34. LRU, example II Page faults: 12!  Loop: well-known worst-case for LRU  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 34
  • 35. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 35
  • 36. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... A size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 36
  • 37. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... AB size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 37
  • 38. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABC size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 38
  • 39. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 39
  • 40. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 40
  • 41. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 41
  • 42. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 42
  • 43. FIFO First-in, first-out: evict oldest page  As competitive as LRU, but  performs miserably in practice! Ignores locality  Suffers from Belady’s anomaly:  More memory can mean more paging!  LRU & similar algs. do not  Stack algorithms – more memory means ≥ hits  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 43
  • 44. FIFO & Belady’s Anomaly UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 44
  • 45. LRU: No Belady’s Anomaly Why no anomaly for LRU?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 45
  • 46. The End Next time:  how real OS approximate LRU UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 46
  • 47. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 47
  • 48. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A, B, C, B, C, C, D UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 48
  • 49. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A A, B, C, B, C, C, D 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 49
  • 50. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B A, B, C, B, C, C, D 1 2 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 50
  • 51. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 2 3 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 51
  • 52. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 4 3 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 52
  • 53. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 4 5 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 53
  • 54. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 4 6 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 54
  • 55. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used D A B C A, B, C, B, C, C, D 1 7 4 6 LRU page How should we implement this? UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 55
  • 56. Implementing Exact LRU Could keep pages in order  – optimizes eviction Priority queue:  update = O(log n), eviction = O(log n) Optimize for common case!  Common case: hits, not misses  Hash table:  update = O(1), eviction = O(n) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 56
  • 57. Cost of Maintaining Exact LRU Hash tables: too expensive  On every reference:  Compute hash of page address  Update time stamp  Unfortunately: 10x – 100x more  expensive! UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 57
  • 58. Cost of Maintaining Exact LRU Alternative: doubly-linked list  Move items to front when referenced  LRU items at end of list  Still too expensive  4-6 pointer updates per reference  Can we do better?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 58
  • 59. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 59
  • 60. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 1 1 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 60
  • 61. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 0 0 reset reference bits UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 61
  • 62. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 1 0 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 62
  • 63. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 1 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 63
  • 64. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 1 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 64
  • 65. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits Evict page with reference bit = 0  D A B C A, B, C, B, C, C, D 0 1 1 1 Cost per miss = O(n)  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 65
  • 66. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 66
  • 67. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  1 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 67
  • 68. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  1 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 68
  • 69. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  1 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 69
  • 70. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 70
  • 71. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 71
  • 72. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 72
  • 73. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 73
  • 74. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  1 0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 74
  • 75. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 75
  • 76. The Clock Algorithm Variant of FIFO & LRU B F  0 1 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 76
  • 77. The Clock Algorithm Variant of FIFO & LRU B F  0 1 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 1 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 77
  • 78. The Clock Algorithm Variant of FIFO & LRU B F  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 1 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 78
  • 79. The Clock Algorithm Variant of FIFO & LRU B F  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 1 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 79
  • 80. The Clock Algorithm Variant of FIFO & LRU B F  0 1 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 0 1 next frame If reference bit = 0,  D G replace page, set bit to 1 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 80
  • 81. Enhancing Clock Recall: don’t write back unmodified pages  Idea: favor eviction of unmodified pages  Extend hardware to keep another bit:  modified bit Total order of tuples: (ref bit, mod bit)  (0,0), (0,1), (1,0), (1,1)  Evict page from lowest nonempty class  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 81
  • 82. Page Replacement, Enhanced Clock OS scans at most three times  Page (0,0) – replace that page  Page (0,1) – write out page, clear mod bit  Page (1,0), (1,1) – clear reference bit  Passes:  all pages (0,0) or (0,1) 1. all pages (0,1) - write out pages 2. all pages (0,0) – replace any page 3. Fast, but still coarse approximation of LRU  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 82
  • 83. Segmented Queue Real systems: segment queue into two  approximate for frequently-referenced pages  e.g., first 1/3 page frames – fast  exact LRU for infrequently-referenced pages  last 2/3 page frames; doubly-linked list – precise  How do we move between two segments?  clock exact LRU UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 83
  • 84. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 84
  • 85. Multiprogramming & VM Multiple programs compete for main  memory Processes move memory from and to disk   Pages needed by one process may get squeezed out by another process thrashing - effective cost of memory access = cost of disk access = really really bad Must balance memory across processes to  avoid thrashing UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 85
  • 86. Global LRU Put all pages from all processes in one pool  Manage with LRU (Segmented Queue)  Used by Linux, BSD, etc.  Advantages:  Easy  Disadvantages:  Many  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 86
  • 87. Global LRU Disadvantages No isolation between processes  One process touching many pages can force  another process’ pages to be evicted Priority ignored, or inverted  All processes treated equally  Greedy (or wasteful) processes rewarded  Programs with poor locality squeeze out  those with good locality Result: more page faults  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 87
  • 88. Global LRU Disadvantages, Cont. “Sleepyhead” problem  Intermittent, important process  Every time it wakes up – no pages! – back to  sleep... Susceptible to denial of service  Non-paying “guest”, lowest priority, marches  over lots of pages – gets all available memory Alternatives?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 88
  • 89. Working Set Denning: Only run processes whose  working set fits in RAM Other processes: deactivate (suspend)  Classical definition:  working set = pages touched in last  references Provides isolation  Process’s reference behavior only affects  itself UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 89
  • 90. Working Set Problems Algorithm relies on key parameter,   How do we set ?  Is there one correct ?  Different processes have different timescales over  which they touch pages Not acceptable (or necessarily possible) to  suspend processes altogether Not really used  Very rough variant used in Windows  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 90
  • 91. Solution: CRAMM New VM management alg:  Cooperative Robust Automatic Memory Management [OSDI 2006, Yang et al.] Redefine working set size =  pages required to spend < n% time paging CRAMM default = 5%  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 91
  • 92. Calculating WSS w.r.t 5% Memory reference sequence mmmm g h h h k kmmm n kmmm n nnnnnggj j jknnkcknn bdddef i i i l l lccl l l abeef kkcf ccl ll LRU Queue mmmmm j h h g e d d b a nakkk bnnnni igf fec akccc kcllj cl l l Pages in Least Recently Used order 1 14 Hit Histogram 00004000000100 0 1 2 3 0 Associated with each LRU position 5 Fault Curve faults  faulti   hist i  1 1 i 1 4 11 14 pages UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 92
  • 93. Computing hit histogram Not possible in standard VM:  Global LRU queues  No per process/file information or control  Difficult to estimate app’s WSS / available memory CRAMM VM:  Per process/file page management:  Page list: Active, Inactive, Evicted  Add & maintain histogram  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 93
  • 94. Managing pages per process Pages protected by turning Pages evicted to disk. off permissions (minor fault) (major fault) Active (CLOCK) Inactive (LRU) Evicted (LRU) Major fault Header Refill & Adjustment Evicted Page Des Minor fault AVL node faults Pages Histogram UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 94
  • 95. Controlling overhead Pages protected by turning Pages evicted to disk. off permissions (minor fault) (major fault) Active (CLOCK) Inactive (LRU) Evicted (LRU) Header Page Des AVL node Buffer faults control the boundary: 1% of execution time Pages Histogram UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 95
  • 96. The End UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 96