Lecture: Page Eviction
Limited Memory
  - What should the OS do if processes want more memory when all frames are taken?
  
    - option 1: out of memory error, mobile OSes typically kill processes that overused memory to make space
- option 2: pick a frame to evict, write the frame to a storage device, then re-allocate the frame
    - why do we need to write the data to disk?
- do we always have to write the data out?
 
 
- Eviction Mechanisms:
  
    - Where to write data to?
      - swap partition: a section on disk that stores swapped out memory content
- swap allocation: track free space inside the swap partition, often bitmap
- update bookkeeping structures so we can find the data later
 
- How to reflect the result of eviction?
      - remove the existing page to frame mapping
      
        - what if there are multiple pages mapped to the frame? (cow, shared memory) 
- struct core_map_entrytracks information about each frame
 
- shootdown TLB if the evicted page is within the current process
    
 
 
- Eviction Policies: deciding which page to evict
Eviction Policies
  - What pages should be our eviction candidates?
  
    - global policies: pages from all processes
- local policies: pages within the requesting process
 
- Policies
  
    - FIFO:
    
      - evicts pages in the order that has been in memory the longest
- what access pattern might work poorly for this?
- Belady's anomaly (OSPP: Ch 9.5.6)
 
- LRU:
    
      - evicts pages that were least recently used
- how would you implement this?
- what access pattern might work poorly for this?
 
- Clock:
    
      - approximates LRU
- uses the access bit inside a page table entry, bit set by hardware whenever the page is accessed (brought into TLB)
- algorithm:
      
          
- track a clock hand (starting frame to look at for eviction)
        
- look at the frame at the clock hand, check the page table entry for the mapped page
        
- if pte's access bit is 0, select the page to evict, advance the clock hand to the next frame
- if pte's access bit is 1, clear the access bit to 0, shootdown TLB entry, advance the clock hand, and go to step 2
 
- recall not all evicted pages need to be written out to swap, but clock doesn't care
 
- Enhanced Clock (Second Chance):
    
      - clock but look at both access and dirty bit
      
- if page is not dirty then we don't need to write it out (cheaper)
- conditions:
      
        - if access is 0 and dirty is 0, select page to evict, advance the clock hand
- if access is 1 and dirty is 0, clear access bit, advance the clock hand, keep searching
- if access is 0 and dirty is 1, clear dirty bit (track the dirty info elsewhere), advance the clock hand, keep searching
- if access is 1 and dirty is 1, clear access bit, advance the clock hand, keep searching