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

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

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

Page 1. Review: Address Segmentation " Review: Address Segmentation " Review: Address Segmentation "

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

Page 1. CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

CS162 Operating Systems and Systems Programming Lecture 10 Caches and TLBs"

VIRTUAL MEMORY READING: CHAPTER 9

Paging! 2/22! Anthony D. Joseph and Ion Stoica CS162 UCB Fall 2012! " (0xE0)" " " " (0x70)" " (0x50)"

10/9/2013 Anthony D. Joseph and John Canny CS162 UCB Fall 2013

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

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

10/7/13! Anthony D. Joseph and John Canny CS162 UCB Fall 2013! " (0xE0)" " " " (0x70)" " (0x50)"

CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement

CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement

CMPT 300 Introduction to Operating Systems

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

CS370 Operating Systems

CMPT 300 Introduction to Operating Systems. Page Replacement Algorithms

CS162 Operating Systems and Systems Programming Lecture 13. Caches and TLBs. Page 1

Past: Making physical memory pretty

CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement. Page 1

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

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

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

CS370 Operating Systems

Lecture 12: Demand Paging

CS162 Operating Systems and Systems Programming Lecture 16. Page Allocation and Replacement (con t) I/O Systems

CS370 Operating Systems

CS162 Operating Systems and Systems Programming Lecture 12. Address Translation. Page 1

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

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

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

MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

Operating Systems. Operating Systems Sina Meraji U of T

CS 153 Design of Operating Systems Winter 2016

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

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

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

Basic Memory Management

CS Advanced Operating Systems Structures and Implementation Lecture 16. Virtual Memory (finished) Paging Devices.

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

COEN-4730 Computer Architecture Lecture 3 Review of Caches and Virtual Memory

Last Class: Demand Paged Virtual Memory

Virtual Memory III. Jo, Heeseung

CSE 153 Design of Operating Systems

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

Address spaces and memory management

Chapter 9: Virtual-Memory

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

CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement

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

Virtual Memory Management

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

1/19/2009. Data Locality. Exploiting Locality: Caches

Chapter 9: Virtual Memory

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

Memory Management Cache Base and Limit Registers base limit Binding of Instructions and Data to Memory Compile time absolute code Load time

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

CS162 Operating Systems and Systems Programming Lecture 16. Page Allocation and Replacement (con t) I/O Systems

CSE 120 Principles of Operating Systems

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

CISC 7310X. C08: Virtual Memory. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/22/2018 CUNY Brooklyn College

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

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

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

Chapter 8: Virtual Memory. Operating System Concepts

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Memory: Paging System

Chapter 6: Demand Paging

Lecture #15: Translation, protection, sharing

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

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

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Memory Hierarchy Review

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Virtual Memory. Chapter 8

Memory Hierarchy, Fully Associative Caches. Instructor: Nick Riasanovsky

Recall: Paging. Recall: Paging. Recall: Paging. CS162 Operating Systems and Systems Programming Lecture 13. Address Translation, Caching

Agenda. CS 61C: Great Ideas in Computer Architecture. Virtual Memory II. Goals of Virtual Memory. Memory Hierarchy Requirements

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

CS 5523 Operating Systems: Memory Management (SGG-8)

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

Modern Computer Architecture

CS370 Operating Systems

CS510 Operating System Foundations. Jonathan Walpole

Operating System Concepts

Virtual Memory Design and Implementation

CS162 Operating Systems and Systems Programming Lecture 13. Address Translation (con t) Caches and TLBs

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

ECE468 Computer Organization and Architecture. Virtual Memory

CS 153 Design of Operating Systems Winter 2016

CS399 New Beginnings. Jonathan Walpole

Virtual Memory. Reading: Silberschatz chapter 10 Reading: Stallings. chapter 8 EEL 358

ECE4680 Computer Organization and Architecture. Virtual Memory

CS370 Operating Systems

Transcription:

Goals for Today" CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement" Finish discussion on TLBs! Page Replacement Policies! FIFO, LRU! Clock Algorithm!! Working Set/Thrashing! February 28, 211! Ion Stoica! http://inst.eecs.berkeley.edu/~cs162! Note: Some slides and/or pictures in the following are" adapted from slides 25 Silberschatz, Galvin, and Gagne. Many slides generated from my lecture notes by Kubiatowicz." Lec 11.2! Review: Caching Applied to Address Translation" Problem: address translation expensive (especially multi-level)! Solution: cache address translation (TLB)! Instruction accesses spend a lot of time on the same (since accesses sequential)! Stack accesses have definite locality of reference! Data accesses have less locality, but still some! CPU" Virtual" Address" TL Cached?" Yes" No" Translate" (MMU)" Data Read or Write" (untranslated)" Save" Result" Physical" Address" Physical" Memory" Lec 11.3! TLB organization" How big does TLB actually have to be?! Usually small: 128-512 entries! Not very big, can support higher associativity! TLB usually organized as fully-associative cache! Lookup is by Virtual Address! Returns Physical Address! What happens when fully-associative is too slow?! Put a small (4-16 entry) direct-mapped cache in front! Called a TLB Slice! When does TLB lookup occur?! Before cache lookup?! In parallel with cache lookup?! Lec 11.4! Page 1

Reducing translation time further" As described, TLB lookup is in serial with cache lookup:! Virtual Address V no. TLB Lookup V Access Rights 1 offset PA P no. Machines with TLBs go one step further: they overlap TLB lookup with cache access.! Works because offset available early! offset 1 Physical Address Lec 11.5! Here is how this might work with a 4K cache:! 3 Hit/" Miss" Overlapping TLB & Cache Access" TL P assoc" lookup" #" index" disp" " What if cache size is increased to 8KB?! Overlap not complete! Need to do something else. See CS152/252! Another option: Virtual Caches! Tags in cache are virtual addresses! Translation only happens on cache misses! =" 4K Cache" 4 bytes" P Data" 1 K" Hit/" Miss" Lec 11.6! Review: Paging & Address Translation" Virtual Address:! Virtual! Virtual! P1 index! P2 index! Offset! Physical! Memory:! Review: Translation Look-aside Buffer" Virtual Address:! Virtual! Virtual! P1 index! P2 index! Offset! Physical! Memory:! PageTablePtr! Physical Address:! Physical! Page #! Offset! PageTablePtr! Physical Address:! Physical! Page #! Offset! (1 st level)! (1 st level)! (2 nd level)! (2 nd level)! TLB:!! Lec 11.7! Lec 11.8! Page 2

Virtual Address:! Virtual! Virtual! P1 index! P2 index! Offset! PageTablePtr! Review: Cache" Physical Address:! Physical! Page #! Offset! Physical! Memory:! Demand Paging" Modern programs require a lot of physical memory! Memory per system growing faster than 25%-3%/year! But they donʼt use all their memory all of the time! 9-1 rule: programs spend 9% of their time in 1% of their code! Wasteful to require all of userʼs code to be in memory! Solution: use main memory as cache for disk! (1 st level)! TLB:! (2 nd level)!! tag! index! byte! cache:! tag:! block:! Datapath Processor Control On-Chip Cache Second Level Cache (SRAM) Main Memory (DRAM) Caching" Secondary Storage (Disk) Tertiary Storage (Tape) Lec 11.9! Lec 11.1! Demand Paging is Caching" Since Demand Paging is Caching, must ask:! What is block size?!» 1! What is organization of this cache (i.e. direct-mapped, setassociative, fully-associative)?!» Fully associative: arbitrary virtual physical mapping! How do we find a in the cache when look for it?!» First check TLB, then -table traversal! What is replacement policy? (i.e. LRU, Random )!» This requires more explanation (kinda LRU)! What happens on a miss?!» Go to lower level to fill miss (i.e. disk)! What happens on a write? (write-through, write back)!» Definitely write-back. Need a dirty bit (D)!! Lec 11.11! Demand Paging Mechanisms" PTE helps us implement demand paging! Valid Page in memory, PTE points at physical! Not Valid Page not in memory; use info in PTE to find it on disk when necessary! Suppose user references with invalid PTE?! Memory Management Unit (MMU) traps to OS!» Resulting trap is a Page Fault! What does OS do on a Page Fault?:!» Choose an old to replace!» If old modified ( D=1 ), write contents back to disk!» Change its PTE and any cached TLB to be invalid!» Load new into memory from disk!» Update table entry, invalidate TLB for new entry!» Continue thread from original faulting location! TLB for new will be loaded when thread continued!! While pulling s off disk for one process, OS runs another process from ready queue!» Suspended process sits on wait queue! Lec 11.12! Page 3

Steps in Handling a Page Fault" Lec 11.13! Demand Paging Example" Since Demand Paging like caching, can compute average access time! ( Effective Access Time )! EAT = Hit Rate x Hit Time + Miss Rate x Miss Time! Example:! Memory access time = 2 nanoseconds! Average -fault service time = 8 milliseconds! Suppose p = Probability of miss, 1-p = Probably of hit! Then, we can compute EAT as follows:!!!eat!= (1 p) x 2ns + p x 8 ms!!!= (1 p) x 2ns + p x 8,,ns! = 2ns + p x 7,999,8ns! If one access out of 1, causes a fault, then EAT = 8.2 μs:! This is a slowdown by a factor of 4!! What if want slowdown by less than 1%?! 2ns x 1.1 < EAT p < 2.5 x 1-6! This is about 1 fault in 4,!! Lec 11.14! What Factors Lead to Misses?" Compulsory Misses:! Pages that have never been d into memory before! How might we remove these misses?!» Prefetching: loading them into memory before needed!» Need to predict future somehow! More later.! Capacity Misses:! Not enough memory. Must somehow increase size.! Can we do this?!» One option: Increase amount of DRAM (not quick fix!)!» Another option: If multiple processes in memory: adjust percentage of memory allocated to each one!! Conflict Misses:! Technically, conflict misses donʼt exist in virtual memory, since it is a fully-associative cache! Policy Misses:! Caused when s were in memory, but kicked out prematurely because of the replacement policy! How to fix? Better replacement policy! Lec 11.15! Page Replacement Policies" Why do we care about Replacement Policy?!! Replacement is an issue with any cache! Particularly important with s!» The cost of being wrong is high: must go to disk!» Must keep important s in memory, not toss them out! FIFO (First In, First Out)! Throw out oldest. Be fair let every live in memory for same amount of time.! Bad, because throws out heavily used s instead of infrequently used s! MIN (Minimum):! Replace that wonʼt be used for the longest time! Great, but canʼt really know future! Makes good comparison case, however! RANDOM:! Pick random for every replacement! Typical solution for TLBʼs. Simple hardware! Unpredictable! Lec 11.16! Page 4

Replacement Policies (Conʼt)" LRU (Least Recently Used):! Replace that hasnʼt been used for the longest time! Programs have locality, so if something not used for a while, unlikely to be used in the near future.! Seems like LRU should be a good approximation to MIN.! How to implement LRU? Use a list!! Head" Page 6" Page 7" Page Page Tail (LRU)" On each use, remove from list and place at head! LRU is at tail! Problems with this scheme for paging?! List operations complex!» Many instructions for each hardware access! In practice, people approximate LRU (more later)! Example: FIFO" Suppose we have 3 frames, 4 virtual s, and following reference stream:! A B C A B D A D B C B! Consider FIFO Page replacement:! Ref:" Page:" FIFO: 7 faults.! When referencing D, replacing A is bad choice, since need A again right away! Lec 11.17! Lec 11.18! Example: MIN" Suppose we have the same reference stream:! A B C A B D A D B C B! Consider MIN Page replacement:! Ref:" Page:" MIN: 5 faults! Look for not referenced farthest in future.! What will LRU do?! Same decisions as MIN here, but wonʼt always be true!! Lec 11.19! When will LRU perform badly?" Consider the following: A B C D A B C D A B C D! LRU Performs as follows (same as FIFO here):! Ref:" Page:" Every reference is a fault!! MIN Does much better:! Ref:" Page:" Lec 11.2! Page 5

Graph of Page Faults Versus The Number of Frames" Administrivia" Project 1! Code: Tuesday, March 1 st! Final document, peer evaluation: Wednesday, March 2 nd! Midterm next week:! Wednesday, March 9 th! Closed book, one of hand-written notes (both sides)! One desirable property: When you add memory the miss rate goes down! Does this always happen?! Seems like it should, right?! No: BeLadyʼs anomaly! Certain replacement algorithms (FIFO) donʼt have this obvious property!! Lec 11.21! Midterm Topics: Everything up to this Wednesday, March 2 nd! Lec 11.22! 5min Break" Lec 11.23! Adding Memory Doesnʼt Always Help Fault Rate" Does adding memory reduce number of faults?! Yes for LRU and MIN! Not necessarily for FIFO! (Called Beladyʼs anomaly)! Page:" E" A E" 4" After adding memory:! With FIFO, contents can be completely different! In contrast, with LRU or MIN, contents of memory with X s are a subset of contents with X+1 Page! Page:" E" E" E" E" E" Lec 11.24! Page 6

Implementing LRU & Second Chance" Perfect:! Timestamp on each reference! Keep list of s ordered by time of reference! Too expensive to implement in reality for many reasons! Second Chance Algorithm:! Approximate LRU!» Replace an old, not the oldest! FIFO with use bit! Details! A use bit per physical! On fault check at head of queue!» If use bit=1 clear bit, and move at tail (give the second chance!)!» If use bit= replace! Moving s to tail still complex! Clock Algorithm" Clock Algorithm: more efficient implementation of second chance algorithm! Arrange physical s in circle with single clock hand! Details:! On fault:!» Advance clock hand (not real time)!» Check use bit: 1 used recently; clear and leave it alone selected candidate for replacement! Will always find a or loop forever?! What if hand moving slowly?! Good sign or bad sign?!» Not many faults and/or find quickly! What if hand is moving quickly?! Lots of faults and/or lots of reference bits set! Lec 11.25! Lec 11.26! Second Chance Illustration" Second Chance Illustration" Access A! Access A! B u: A u:1 D u: C u: B u: A u:1 D u: C u: Lec 11.27! Lec 11.28! Page 7

Second Chance Illustration" Second Chance Illustration" Access A! Access A! Access D! Page E arrives! A u:1 D u: C u: F u: A u:1 D u:1 C u: F u: Lec 11.29! Lec 11.3! Second Chance Illustration" Second Chance Illustration" Access A! Access A! Access D! Page E arrives! Access D! Page E arrives! D u:1 C u: F u: A u: C u: F u: A u: D E u: Lec 11.31! Lec 11.32! Page 8

Clock Replacement Illustration" Clock Replacement Illustration" Invariant: point at oldest! Invariant: point at oldest! B u: Access A! B u: A u: Lec 11.33! Lec 11.34! Clock Replacement Illustration" Clock Replacement Illustration" Invariant: point at oldest! Invariant: point at oldest! Access A! B u: A u: 1 Access A! C u: B u: A u: 1 D u: D u: Lec 11.35! Lec 11.36! Page 9

Clock Replacement Illustration" Invariant: point at oldest! Clock Replacement Illustration" Invariant: point at oldest! Access A! C u: B u: F u: D u: A u: 1 Access A! Access D! Page E arrives! C E u: F u: D u: 1 A u: 1 Lec 11.37! Lec 11.38! N th Chance version of Clock Algorithm" N th chance algorithm: Give N chances! OS keeps counter per : # sweeps! On fault, OS checks use bit:!» 1 clear use and also clear counter (used in last sweep)!» increment counter; if count=n, replace! Means that clock hand has to sweep by N times without being used before is replaced! How do we pick N?! Why pick large N? Better approx to LRU!» If N ~ 1K, really good approximation! Why pick small N? More efficient!» Otherwise might have to look a long way to find free! What about dirty s?! Takes extra overhead to replace a dirty, so give dirty s an extra chance before replacing?! Common approach:!» Clean s, use N=1!» Dirty s, use N=2 (and write back to disk when N=1)! Lec 11.39! Clock Algorithms: Details" Which bits of a PTE entry are useful to us?! Use: Set when is referenced; cleared by clock algorithm! Modified: set when is modified, cleared when written to disk! Valid: ok for program to reference this! Read-only: ok for program to read, but not modify!» For example for catching modifications to code s!! Do we really need hardware-supported modified bit?! No. Can emulate it (BSD Unix) using read-only bit!» Initially, mark all s as read-only, even data s!» On write, trap to OS. OS sets software modified bit, and marks as read-write.!» Whenever comes back in from disk, mark read-only! Lec 11.4! Page 1

Clock Algorithms Details (continued)" Do we really need a hardware-supported use bit?! No. Can emulate it using invalid bit:!» Mark all s as invalid, even if in memory!» On read to invalid, trap to OS!» OS sets use bit, and marks read-only! When clock hand passes by, reset use bit and mark as invalid again! Summary (1/2)" TLB is cache on translations! Fully associative to reduce conflicts! Can be overlapped with cache access! Demand Paging:! Treat memory as cache on disk! Cache miss get from disk! Transparent Level of Indirection! User program is unaware of activities of OS behind scenes! Data can be moved without affecting application correctness! Lec 11.41! Lec 11.42! Summary (2/2)" Replacement policies! FIFO: Place s on queue, replace at end! MIN: Replace that will be used farthest in future! LRU: Replace used farthest in past! Clock Algorithm: Approximation to LRU! Arrange all s in circular list! Sweep through them, marking as not in use! If not in use for one pass, than can replace! Second-Chance List algorithm: Yet another approx LRU! Divide s into two groups, one of which is truly LRU and managed on faults.! Lec 11.43! Page 11