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

Similar documents
Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

CS 153 Design of Operating Systems Winter 2016

CSE 120 Principles of Operating Systems

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

CSE 153 Design of Operating Systems

Lecture 12: Demand Paging

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

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

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Virtual Memory III. Jo, Heeseung

Virtual Memory Design and Implementation

Operating Systems. Operating Systems Sina Meraji U of T

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

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

Virtual Memory Design and Implementation

VIRTUAL MEMORY READING: CHAPTER 9

Virtual Memory Management

CS 153 Design of Operating Systems Spring 18

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Virtual Memory Design and Implementation

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

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

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

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

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

Swapping. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Swapping. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS510 Operating System Foundations. Jonathan Walpole

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

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

Memory management, part 2: outline

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Operating Systems Lecture 6: Memory Management II

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

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

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture 14 Page Replacement Policies

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

Last Class: Demand Paged Virtual Memory

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

Past: Making physical memory pretty

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

Memory: Paging System

Page replacement algorithms OS

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

Chapter 8 Virtual Memory

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

10: Virtual Memory Management

Page Replacement Algorithms

Lecture 14: Cache & Virtual Memory

MEMORY MANAGEMENT. Term Paper. Operating Systems CS-384

Virtual Memory. ICS332 Operating Systems

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

CS 4284 Systems Capstone

Chapter 6: Demand Paging

Operating Systems CSE 410, Spring Virtual Memory. Stephen Wagner Michigan State University

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

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

Memory Management Ch. 3

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

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

Virtual Memory I. Jo, Heeseung

Chapter 8 Virtual Memory

Address spaces and memory management

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

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

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

Principles of Operating Systems

CS370 Operating Systems

Last Class. Demand Paged Virtual Memory. Today: Demand Paged Virtual Memory. Demand Paged Virtual Memory

MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

Memory Management Virtual Memory

Chapter 9: Virtual Memory

CS370 Operating Systems

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Caching and Demand-Paged Virtual Memory

Operating System Principles: Memory Management Swapping, Paging, and Virtual Memory CS 111. Operating Systems Peter Reiher

SMD149 - Operating Systems - VM Management

Virtual Memory Outline

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

Basic Memory Management

Basic Page Replacement

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

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

Chapter 8: Virtual Memory. Operating System Concepts Essentials 2 nd Edition

Unit 2 Buffer Pool Management

Memory Management: Virtual Memory and Paging CS 111. Operating Systems Peter Reiher

CS370 Operating Systems

Process size is independent of the main memory present in the system.

Module 9: Virtual Memory

Operating System Concepts

Chapter 4 Memory Management. Memory Management

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

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

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

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

Chapter 8 Virtual Memory

Transcription:

CSE 451: Operating Systems Autumn 2012 Module 12 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Reminder: Mechanics of address translation virtual address virtual # offset table frame # physical address frame # offset physical memory frame 0 frame 1 frame 2 frame 3 Ed Lazowska lazowska@cs.washington.edu Allen Center 570 Note: Each process has its own table! frame Y 2012 Gribble, Lazowska, Levy, Zahorjan 2 Reminder: Page Table Entries (PTEs) 1 1 1 V R M 2 prot frame number PTE s control mapping the valid bit says whether or not the PTE can be used says whether or not a virtual address is valid it is checked each time a virtual address is used the referenced bit says whether the has been accessed it is set when a has been read or written to the modified bit says whether or not the is dirty it is set when a write to the has occurred the protection bits control which operations are allowed read, write, execute the frame number determines the physical physical start address = PFN 20 2012 Gribble, Lazowska, Levy, Zahorjan 3 Paged virtual memory We ve hinted that all the s of an address space do not need to be resident in memory the full (used) address space exists on secondary storage (disk) in -sized blocks the OS uses main memory as a () cache a that is needed is transferred to a free frame if there are no free frames, a must be evicted evicted s go to disk (only need to write if they are dirty) all of this is transparent to the application (except for performance ) managed by hardware and OS Traditionally called d virtual memory 2012 Gribble, Lazowska, Levy, Zahorjan 4 Page faults What happens when a process references a virtual address in a that has been evicted (or never loaded)? when the was evicted, the OS set the PTE as invalid and noted the disk location of the in a data structure (that looks like a table but holds disk addresses) when a process tries to access the, the invalid PTE will cause an exception ( fault) to be thrown OK, it s actually an interrupt! the OS will run the fault handler in response handler uses the like a table data structure to locate the on disk handler reads into a physical frame, updates PTE to point to it and to be valid OS restarts the faulting process there are a million and one details 2012 Gribble, Lazowska, Levy, Zahorjan 5 Demand paging Pages are only brought into main memory when they are referenced only the code/data that is needed (demanded!) by a process needs to be loaded What s needed changes over time, of course Hence, it s called demand paging Few systems try to anticipate future needs OS crystal ball module notoriously ineffective But it s not uncommon to cluster s OS keeps track of s that should come and go together bring in all when one is referenced interface may allow programmer or compiler to identify clusters 2012 Gribble, Lazowska, Levy, Zahorjan 6 1

Page replacement When you read in a, where does it go? if there are free frames, grab one what data structure might support this? if not, must evict something else this is called replacement Page replacement algorithms try to pick a that won t be needed in the near future try to pick a that hasn t been modified (thus saving the disk write) OS typically tries to keep a pool of free s around so that allocations don t inevitably cause evictions OS also typically tries to keep some clean s around, so that even if you have to evict a, you won t have to write it accomplished by pre-writing when there s nothing better to do Much more on this later! How do you load a program? Create process descriptor (process control block) Create table Put address space image on disk in -sized chunks Build table (pointed to by process descriptor) all PTE valid bits false an analogous data structure indicates the disk location of the corresponding when process starts executing: instructions immediately fault on both code and data s faults taper off, as the necessary code/data s enter memory 2012 Gribble, Lazowska, Levy, Zahorjan 7 2012 Gribble, Lazowska, Levy, Zahorjan 8 Oh, man, how can any of this possibly work? Locality! temporal locality locations referenced recently tend to be referenced again soon spatial locality locations near recently references locations are likely to be referenced soon (think about why) Locality means paging can be infrequent once you ve d something in, it will be used many times on average, you use things that are d in but, this depends on many things: degree of locality in the application replacement policy and application reference pattern amount of physical memory vs. application footprint or working set 2012 Gribble, Lazowska, Levy, Zahorjan 9 Evicting the best The goal of the replacement algorithm: reduce fault rate by selecting best victim to remove system fault rate or program fault rate?? the best to evict is one that will never be touched again duh never is a long time Belady s proof: evicting the that won t be used for the longest period of time minimizes fault rate Rest of this module: survey a bunch of replacement algorithms for now, assume that a process s against itself, using a fixed number of frames 2012 Gribble, Lazowska, Levy, Zahorjan 10 #1: Belady s Algorithm Provably optimal: lowest fault rate (remember SJF?) evict the that won t be used for the longest time in future problem: impossible to predict the future Why is Belady s algorithm useful? as a yardstick to compare other algorithms to optimal if Belady s isn t much better than yours, yours is pretty good how could you do this comparison? Is there a best practical algorithm? no; depends on workload Is there a worst algorithm? no, but random replacement does pretty badly don t laugh there are some other situations where OS s use near-random algorithms quite effectively! #2: FIFO FIFO is obvious, and simple to implement when you in something, put it on the tail of a list evict at the head of the list Why might this be good? maybe the one brought in longest ago is not being used Why might this be bad? then again, maybe it is being used have absolutely no information either way In fact, FIFO s performance is typically lousy In addition, FIFO suffers from Belady s Anomaly there are reference strings for which the fault rate increases when the process is given more physical memory 2012 Gribble, Lazowska, Levy, Zahorjan 11 2012 Gribble, Lazowska, Levy, Zahorjan 12 2

#3: Least Recently Used (LRU) LRU uses reference information to make a more informed replacement decision idea: past experience gives us a guess of future behavior on replacement, evict the that hasn t been used for the longest amount of time LRU looks at the past, Belady s wants to look at future How is LRU different from FIFO? when does LRU do well? when is it lousy? #3: LRU continued Implementation to be perfect, must grab a timestamp on every memory reference, put it in the PTE, order or search based on the timestamps way too $$ in memory bandwidth, algorithm execution time, etc. so, we need a cheap approximation Example bad case: looping through array amount of physical memory 2012 Gribble, Lazowska, Levy, Zahorjan 13 2012 Gribble, Lazowska, Levy, Zahorjan 14 Approximating LRU Many approximations, all use the PTE s referenced bit keep a counter for each at some regular interval, for each, do: if ref bit = 0, increment the counter (hasn t been used) if ref bit = 1, zero the counter (has been used) regardless, zero ref bit the counter will contain the # of intervals since the last reference to the with largest counter is least recently used Some architectures don t have PTE reference bits can simulate reference bit using the valid bit to induce faults hack, hack, hack 2012 Gribble, Lazowska, Levy, Zahorjan 15 #4: LRU Clock AKA Not Recently Used (NRU) or Second Chance replace that is old enough logically, arrange all physical frames in a big circle (clock) just a circular linked list a clock hand is used to select a good LRU candidate sweep through the s in circular order like a clock if ref bit is off, it hasn t been used recently, we have a victim so, what is minimum age if ref bit is off? if the ref bit is on, turn it off and go to next arm moves quickly when s are needed low overhead if have plenty of memory if memory is large, accuracy of information degrades add more hands to fix 2012 Gribble, Lazowska, Levy, Zahorjan 16 Allocation of frames among processes FIFO and LRU Clock each can be implemented as either local or global replacement algorithms local each process is given a limit of s it can use it s against itself (evicts its own s) global the victim is chosen from among all frames, regardless of owner processes frame allocation can vary dynamically Issues with local replacement? Issues with global replacement? Linux uses global replacement Hybrid algorithms local replacement an explicit mechanism for adding or removing frames 2012 Gribble, Lazowska, Levy, Zahorjan 17 2012 Gribble, Lazowska, Levy, Zahorjan 18 3

Number of memory references between faults Number of frames allocated to process Where would you like to operate? 2012 Gribble, Lazowska, Levy, Zahorjan 19 The working set model of program behavior The working set of a process is used to model the dynamic locality of its memory usage working set = set of s process currently needs formally defined by Peter Denning in the 1960 s Definition: WS(t,w) = {s P such that P was referenced in the time interval (t, t-w)} t: time w: working set window (measured in refs) a is in the working set (WS) only if it was referenced in the last w references obviously the working set (the particular s) varies over the life of the program so does the working set size (the number of s in the WS) 2012 Gribble, Lazowska, Levy, Zahorjan 20 Working set size #5: Hypothetical Working Set algorithm The working set size, WS(t,w), changes with program locality during periods of poor locality, more s are referenced within that period of time, the working set size is larger Intuitively, the working set must be in memory, otherwise you ll experience heavy faulting (thrashing) when people ask How much memory does Firefox need?, really they re asking what is Firefox s average (or worst case) working set size? Estimate WS(0,w) for a process Allow that process to start only if you can allocate it that many frames Use a local replacement algorithm (LRU Clock?) make sure that the right s (the working set) are occupying the process s frames Track each process s working set size, and reallocate frames among processes dynamically Problem? Solution? What the heck is w? 2012 Gribble, Lazowska, Levy, Zahorjan 21 2012 Gribble, Lazowska, Levy, Zahorjan 22 #6: Page Fault Frequency (PFF) PFF is a variable-space algorithm that uses a more ad hoc approach Attempt to equalize the fault rate among all processes, and to have a tolerable system-wide fault rate monitor the fault rate for each process if fault rate is above a given threshold, give it more memory so that it faults less if the fault rate is below threshold, take away memory should fault more, allowing someone else to fault less Thrashing Thrashing is when the system spends most of its time servicing faults, little time doing useful work could be that there is enough memory but a lousy replacement algorithm (one incompatible with program behavior) could be that memory is over-committed too many active processes 2012 Gribble, Lazowska, Levy, Zahorjan 23 2012 Gribble, Lazowska, Levy, Zahorjan 24 4

System throughput (requests/sec.) with zero overhead Number of active processes System throughput (requests/sec.) with thrashing Number of active processes 2012 Gribble, Lazowska, Levy, Zahorjan 25 2012 Gribble, Lazowska, Levy, Zahorjan 26 Where is life interesting? Not if system has too much memory replacement algorithm doesn t much matter (overprovisioning) Not if system has too little memory replacement algorithm doesn t much matter (overcommitted) Life is only interesting on the border between overprovisioned and over-committed Networking analogies Aloha Network as an example of thrashing over-provisioning as an alternative to Quality of Service guarantees Virtual memory Page faults Demand paging don t try to anticipate Page replacement local, global, hybrid Locality temporal, spatial Working set Thrashing Summary 2012 Gribble, Lazowska, Levy, Zahorjan 27 2012 Gribble, Lazowska, Levy, Zahorjan 28 Page replacement algorithms #1: Belady s optimal, but unrealizable #2: FIFO replace loaded furthest in the past #3: LRU replace referenced furthest in the past approximate using PTE reference bit #4: LRU Clock replace that is old enough #5: Working Set keep the working set in memory #6: Page Fault Frequency grow/shrink number of frames as a function of fault rate 2012 Gribble, Lazowska, Levy, Zahorjan 29 5