CSE 153 Design of Operating Systems

Similar documents
CS 153 Design of Operating Systems Winter 2016

CSE 120 Principles of Operating Systems

Lecture 12: Demand Paging

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

All Paging Schemes Depend on Locality. VM Page Replacement. Paging. Demand Paging

CS 153 Design of Operating Systems Spring 18

Virtual Memory III. Jo, Heeseung

Operating Systems. Operating Systems Sina Meraji U of T

Reminder: Mechanics of address translation. Paged virtual memory. Reminder: Page Table Entries (PTEs) Demand paging. Page faults

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

EvicMng the best page #2: FIFO. #1: Belady s Algorithm. CS 537 Lecture 11 Page Replacement Policies 11/1/11. Michael SwiF

Virtual Memory - II. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - XVI. University at Buffalo.

Swapping. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Swapping. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Paging algorithms. CS 241 February 10, Copyright : University of Illinois CS 241 Staff 1

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Virtual Memory. Today.! Virtual memory! Page replacement algorithms! Modeling page replacement algorithms

Virtual Memory Design and Implementation

VIRTUAL MEMORY READING: CHAPTER 9

ECE7995 Caching and Prefetching Techniques in Computer Systems. Lecture 8: Buffer Cache in Main Memory (I)

!! What is virtual memory and when is it useful? !! What is demand paging? !! When should pages in memory be replaced?

Virtual Memory Management

Outline. 1 Paging. 2 Eviction policies. 3 Thrashing 1 / 28

Page Replacement. (and other virtual memory policies) Kevin Webb Swarthmore College March 27, 2018

Operating Systems Lecture 6: Memory Management II

CS 333 Introduction to Operating Systems. Class 14 Page Replacement. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 14 Page Replacement. Jonathan Walpole Computer Science Portland State University

CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement"

CS370 Operating Systems

Last Class: Demand Paged Virtual Memory

Basic Page Replacement

Memory: Paging System

Lecture 14 Page Replacement Policies

Virtual Memory Design and Implementation

Virtual Memory: Page Replacement. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Memory management, part 2: outline

Past: Making physical memory pretty

3/3/2014! Anthony D. Joseph!!CS162! UCB Spring 2014!

Chapter 4 Memory Management

Memory management, part 2: outline. Operating Systems, 2017, Danny Hendler and Amnon Meisels

Virtual Memory Design and Implementation

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 15: Caching: Demand Paged Virtual Memory

CS 4410 Operating Systems. Page Replacement (2) Summer 2016 Cornell University

Operating Systems. Overview Virtual memory part 2. Page replacement algorithms. Lecture 7 Memory management 3: Virtual memory

Chapter 4 Memory Management. Memory Management

10: Virtual Memory Management

Virtual or Logical. Logical Addr. MMU (Memory Mgt. Unit) Physical. Addr. 1. (50 ns access)

Memory Management. Chapter 4 Memory Management. Multiprogramming with Fixed Partitions. Ideally programmers want memory that is.

Page Replacement Algorithms

SMD149 - Operating Systems - VM Management

CS510 Operating System Foundations. Jonathan Walpole

CS450/550 Operating Systems

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Operating Systems, Fall

Global Replacement Algorithms (1) Ken Wong Washington University. Global Replacement Algorithms (2) Replacement Example (1)

Virtual Memory. 1 Administrivia. Tom Kelliher, CS 240. May. 1, Announcements. Homework, toolboxes due Friday. Assignment.

Virtual Memory. ICS332 Operating Systems

Chapter 4: Memory Management. Part 1: Mechanisms for Managing Memory

CS370 Operating Systems

Week 2: Tiina Niklander

Lecture#16: VM, thrashing, Replacement, Cache state

MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

Readings and References. Virtual Memory. Virtual Memory. Virtual Memory VPN. Reading. CSE Computer Systems December 5, 2001.

Address spaces and memory management

Page 1. Goals for Today" TLB organization" CS162 Operating Systems and Systems Programming Lecture 11. Page Allocation and Replacement"

Course Outline. Processes CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Distributed Systems

Operating Systems (1DT020 & 1TT802) Lecture 9 Memory Management : Demand paging & page replacement. Léon Mugwaneza

Virtual Memory. Chapter 8

Chapter 6: Demand Paging

How to create a process? What does process look like?

Memory Management Prof. James L. Frankel Harvard University

Page replacement algorithms OS

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory

ADRIAN PERRIG & TORSTEN HOEFLER Networks and Operating Systems ( ) Chapter 6: Demand Paging

Today. Adding Memory Does adding memory always reduce the number of page faults? FIFO: Adding Memory with LRU. Last Class: Demand Paged Virtual Memory

Memory - Paging. Copyright : University of Illinois CS 241 Staff 1

Paging Policies, Load control, Page Fault Handling, Case studies January WT 2008/09

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory. Demand Paging

Virtual Memory - Overview. Programmers View. Virtual Physical. Virtual Physical. Program has its own virtual memory space.

a process may be swapped in and out of main memory such that it occupies different regions

Memory Management. Virtual Memory. By : Kaushik Vaghani. Prepared By : Kaushik Vaghani

Chapter 8 Virtual Memory

Chapter 8. Virtual Memory

Chapter 9: Virtual Memory

! What is virtual memory and when is it useful? ! What is demand paging? ! What pages should be. ! What is the working set model?

Chapter 9: Virtual-Memory

Move back and forth between memory and disk. Memory Hierarchy. Two Classes. Don t

Memory Management Ch. 3

Virtual Memory Outline

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Page Replacement. 3/9/07 CSE 30341: Operating Systems Principles

Virtual Memory. Virtual Memory. Demand Paging. valid-invalid bit. Virtual Memory Larger than Physical Memory

Chapter 8 Virtual Memory

Recall from Tuesday. Our solution to fragmentation is to split up a process s address space into smaller chunks. Physical Memory OS.

Perform page replacement. (Fig 8.8 [Stal05])

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

ECE7995 (3) Basis of Caching and Prefetching --- Locality

Memory Allocation, Page Replacement and Working Sets

CS 4284 Systems Capstone

Transcription:

CSE 153 Design of Operating Systems Winter 18 Lecture 18/19: Page Replacement

Memory Management Memory management systems Physical and virtual addressing; address translation Techniques: Partitioning, paging, segmentation Page table size, TLBs, VM tricks Policies Page replacement algorithms (3) CSE 153 Lecture 18/19 Page Replacement 2

Lecture Overview Review paging and page replacement Survey page replacement algorithms Discuss local vs. global replacement Discuss thrashing CSE 153 Lecture 18/19 Page Replacement 3

Demand Paging (OS) We use demand paging (similar to other caches): Pages loaded from disk when referenced Pages may be evicted to disk when memory is full Page faults trigger paging operations What is the alternative to demand paging? Some kind of prefetching Lazy vs. aggressive policies in systems CSE 153 Lecture 18/19 Page Replacement 4

Demand Paging (Process) Demand paging when a process first starts up When a process is created, it has A brand new page table with all valid bits off No pages in memory When the process starts executing Instructions fault on code and data pages Faulting stops when all necessary code and data pages are in memory Only code and data needed by a process needs to be loaded This, of course, changes over time CSE 153 Lecture 18/19 Page Replacement 5

Page Replacement When a page fault occurs, the OS loads the faulted page from disk into a page frame of memory At some point, the process has used all of the page frames it is allowed to use This is likely (much) less than all of available memory When this happens, the OS must replace a page for each page faulted in It must evict a page to free up a page frame Written back only if it is has been modified (i.e., dirty )! CSE 153 Lecture 18/19 Page Replacement 6

Page replacement policy What we discussed so far (page faults, swap, page table structures, etc ) is mechanisms Page replacement policy: determine which page to remove when we need a victim Does it matter? Yes! Page faults are super expensive Getting the number down, can improve the performance of the system significantly CSE 153 Lecture 18/19 Page Replacement 7

Considerations Page replacement support has to be simple during memory accesses They happen all the time, we cannot make that part slow But it can be complicated/expensive when a page fault occurs why? Reason 1: if we are successful, this will be rare Reason 2: when it happens we are paying the cost of I/O» I/O is very slow: can afford to do some extra computation» Worth it if we can save some future page faults What makes a good page replacement policy? CSE 153 Lecture 18/19 Page Replacement 8

Locality to the Rescue Recall that virtual memory works because of locality Temporal and spatial Work at different scales: for cache, at a line level, for VM, at page level, and even at larger scales All paging schemes depend on locality What happens if a program does not have locality? High cost of paging is acceptable, if infrequent Processes usually reference pages in localized patterns, making paging practical CSE 153 Lecture 18/19 Page Replacement 9

Evicting the Best Page Goal is to reduce the page fault rate The best page to evict is the one never touched again Will never fault on it Never is a long time, so picking the page closest to never is the next best thing Evicting the page that won t be used for the longest period of time minimizes the number of page faults Proved by Belady We re now going to survey various replacement algorithms, starting with Belady s CSE 153 Lecture 18/19 Page Replacement 10

Belady s Algorithm Belady s algorithm Idea: Replace the page that will not be used for the longest time in the future Optimal? How would you show? Problem: Have to predict the future Why is Belady s useful then? Use it as a yardstick/upper bound Compare implementations of page replacement algorithms with the optimal to gauge room for improvement» If optimal is not much better, then algorithm is pretty good What s a good lower bound?» Random replacement is often the lower bound CSE 153 Lecture 18/19 Page Replacement 11

First-In First-Out (FIFO) FIFO is an obvious algorithm and simple to implement Maintain a list of pages in order in which they were paged in On replacement, evict the one brought in longest time ago Why might this be good? Maybe the one brought in the longest ago is not being used Why might this be bad? Then again, maybe it s not We don t have any info to say one way or the other FIFO suffers from Belady s Anomaly The fault rate might actually increase when the algorithm is given more memory (very bad) CSE 153 Lecture 18/19 Page Replacement 12

Least Recently Used (LRU) LRU uses reference information to make a more informed replacement decision Idea: We can t predict the future, but we can make a guess based upon past experience On replacement, evict the page that has not been used for the longest time in the past (Belady s: future) When does LRU do well? When does LRU do poorly? Implementation To be perfect, need to time stamp every reference (or maintain a stack) much too costly So we need to approximate it CSE 153 Lecture 18/19 Page Replacement 13

Approximating LRU LRU approximations use the PTE reference bit Keep a counter for each page At regular intervals, for every page do:» If ref bit = 0, increment counter» If ref bit = 1, zero the counter» Zero the reference bit The counter will contain the number of intervals since the last reference to the page The page with the largest counter is the least recently used Some architectures don t have a reference bit Can simulate reference bit using the valid bit to induce faults CSE 153 Lecture 18/19 Page Replacement 14

LRU Approximation Reference bits LRU counter Problem: Overhead of one counter value per page CSE 153 Lecture 18/19 Page Replacement 15

LRU Clock (Not Recently Used) Not Recently Used (NRU) Used by Unix Replace page that is old enough Arrange all of physical page frames in a big circle (clock) A clock hand is used to select a good LRU candidate» Sweep through the pages in circular order like a clock» If the ref bit is off, it hasn t been used recently What is the minimum age if ref bit is off?» If the ref bit is on, turn it off and go to next page Arm moves quickly when pages are needed Low overhead when plenty of memory If memory is large, accuracy of information degrades» What does it degrade to?» One fix: use two hands (leading erase hand, trailing select hand) CSE 153 Lecture 18/19 Page Replacement 16

LRU Clock P3: 1 P3: 0 P2: 1 P4: 0 P2: 0 P4: 0 P1: 1 P5: 1 P1: 0 P5: 1 P8: 0 P6: 0 P8: 1 P6: 0 P7: 0 P7: 0 CSE 153 Lecture 18/19 Page Replacement 17

Example: gcc Page Replace CSE 153 Lecture 18/19 Page Replacement 18

Example: Belady s Anomaly CSE 153 Lecture 18/19 Page Replacement 19

Other ideas Victim buffer Add a buffer (death row!) we put a page on when we decide to replace it Buffer is FIFO If you get accessed while on death row clemency! If you are the oldest page on death row replacement! CSE 153 Lecture 18/19 Page Replacement 20

Fixed vs. Variable Space In a multiprogramming system, we need a way to allocate memory to competing processes Problem: How to determine how much memory to give to each process? Fixed space algorithms» Each process is given a limit of pages it can use» When it reaches the limit, it replaces from its own pages» Local replacement Some processes may do well while others suffer Variable space algorithms» Process set of pages grows and shrinks dynamically» Global replacement One process can ruin it for the rest CSE 153 Lecture 18/19 Page Replacement 21

Working Set Model A working set of a process is used to model the dynamic locality of its memory usage Defined by Peter Denning in 60s Definition WS(t,w) = {set of pages P, such that every page in P was referenced in the time interval (t, t-w)} t time, w working set window (measured in page refs) A page is in the working set (WS) only if it was referenced in the last w references CSE 153 Lecture 18/19 Page Replacement 22

Working Set Size The working set size is the number of pages in the working set The number of pages referenced in the interval (t, t-w) The working set size changes with program locality During periods of poor locality, you reference more pages Within that period of time, the working set size is larger Intuitively, want the working set to be the set of pages a process needs in memory to prevent heavy faulting Each process has a parameter w that determines a working set with few faults Denning: Don t run a process unless working set is in memory CSE 153 Lecture 18/19 Page Replacement 23

Example: gcc Working Set CSE 153 Lecture 18/19 Page Replacement 24

Working Set Problems Problems How do we determine w? How do we know when the working set changes? Too hard to answer So, working set is not used in practice as a page replacement algorithm However, it is still used as an abstraction The intuition is still valid When people ask, How much memory does Firefox need?, they are in effect asking for the size of Firefox s working set CSE 153 Lecture 18/19 Page Replacement 25

Page Fault Frequency (PFF) Page Fault Frequency (PFF) is a variable space algorithm that uses a more ad-hoc approach Monitor the fault rate for each process If the fault rate is above a high threshold, give it more memory» So that it faults less» But not always (FIFO, Belady s Anomaly) If the fault rate is below a low threshold, take away memory» Should fault more» But not always Hard to use PFF to distinguish between changes in locality and changes in size of working set CSE 153 Lecture 18/19 Page Replacement 26

Thrashing Page replacement algorithms avoid thrashing When most of the time is spent by the OS in paging data back and forth from disk No time spent doing useful work (making progress) In this situation, the system is overcommitted» No idea which pages should be in memory to reduce faults» Could just be that there isn t enough physical memory for all of the processes in the system» Ex: Running Windows95 with 4 MB of memory Possible solutions» Swapping write out all pages of a process» Buy more memory CSE 153 Lecture 18/19 Page Replacement 27

Summary Page replacement algorithms Belady s optimal replacement (minimum # of faults) FIFO replace page loaded furthest in past LRU replace page referenced furthest in past» Approximate using PTE reference bit LRU Clock replace page that is old enough Working Set keep the set of pages in memory that has minimal fault rate (the working set ) Page Fault Frequency grow/shrink page set as a function of fault rate Multiprogramming Should a process replace its own page, or that of another? CSE 153 Lecture 18/19 Page Replacement 28