Demand- Paged Virtual Memory

Similar documents
Caching and Demand-Paged Virtual Memory

Lecture 14: Cache & Virtual Memory

Caching and Demand- Paged Virtual Memory

CIS Operating Systems Memory Management Page Replacement. Professor Qiang Zeng Spring 2018

CIS Operating Systems Memory Management Cache Replacement & Review. Professor Qiang Zeng Fall 2017

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

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

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

Address spaces and memory management

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

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

Past: Making physical memory pretty

VIRTUAL MEMORY READING: CHAPTER 9

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Memory: Paging System

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

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

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

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

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

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

Week 2: Tiina Niklander

Operating Systems, Fall

Paging and Page Replacement Algorithms

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

Virtual Memory Management

CS370 Operating Systems

Basic Memory Management

Virtual memory. Virtual memory - Swapping. Large virtual memory. Processes

Caches! Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University. See P&H 5.2 (writes), 5.3, 5.5

Recap: Memory Management

Virtual Memory III. Jo, Heeseung

Chapter 6: Demand Paging

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

Memory Management. To improve CPU utilization in a multiprogramming environment we need multiple programs in main memory at the same time.

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2015

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

MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

Memory management, part 2: outline

Memory Hierarchy. Goal: Fast, unlimited storage at a reasonable cost per bit.

Chapter 4 Memory Management

CS 550 Operating Systems Spring Memory Management: Page Replacement

Perform page replacement. (Fig 8.8 [Stal05])

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

Operating Systems. Operating Systems Sina Meraji U of T

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

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

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

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

Virtual Memory Design and Implementation

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

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

CS370 Operating Systems

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

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

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

UC Berkeley CS61C : Machine Structures

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

CIS Operating Systems Memory Management Cache and Demand Paging. Professor Qiang Zeng Spring 2018

CS 153 Design of Operating Systems Winter 2016

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

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

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

Virtual Memory Outline

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

Random-Access Memory (RAM) Systemprogrammering 2007 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics

Chapter 8. Virtual Memory

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

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

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

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2017

Random-Access Memory (RAM) Systemprogrammering 2009 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics! The memory hierarchy

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

CS450/550 Operating Systems

Chapter 9: Virtual Memory

Advanced Memory Management

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

1. Creates the illusion of an address space much larger than the physical memory

Operating System Concepts

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

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

Chapter 9: Virtual Memory

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

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

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

Page Replacement Algorithms

OPERATING SYSTEM. Chapter 9: Virtual Memory

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

Principles of Operating Systems

Module 9: Virtual Memory

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

Chapter 8: Virtual Memory. Operating System Concepts

Chapter 8 Virtual Memory

Virtual Memory Design and Implementation

Basic Page Replacement

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

Topic 18: Virtual Memory

Page Replacement Chap 21, 22. Dongkun Shin, SKKU

Transcription:

Demand- Paged Virtual Memory

Main Points Can we provide the illusion of near infinite memory in limited physical memory? Demand- paged virtual memory Memory- mapped files How do we choose which page to replace? FIFO, MIN, LRU, LFU, Clock What types of workloads does caching work for, and how well? SpaJal/temporal locality vs. Zipf workloads

Hardware address translajon is a power tool Kernel trap on read/write to selected addresses Copy on write Fill on reference Zero on use Demand paged virtual memory Memory mapped files Modified bit emulajon Use bit emulajon

Demand Paging Illusion of (nearly) infinite memory, available to every process MulJplex virtual pages onto a limited amount of physical page frames Pages can be either resident (in physical memory, valid page table entry) non- resident (on disk, invalid page table entry) On reference to non- resident page, copy into memory, replacing some resident page From the same process, or a different process

Demand Paging (Before) Page Table Physical Memory Page Frames Disk Frame Access Page A Virtual Page B Frame for B Invalid Page B Page A Virtual Page A Frame for A R/W

Demand Paging (AVer) Page Table Physical Memory Page Frames Disk Frame Access Page A Virtual Page B Frame for B R/W Page B Page B Virtual Page A Frame for A Invalid

Demand Paging QuesJons How does the kernel provide the illusion that all pages are resident? Where are non- resident pages stored on disk? How do we find a free page frame? Which pages have been modified (must be wrixen back to disk) or acjvely used (shouldn t be evicted)? Are modified/use bits virtual or physical? What policy should we use for choosing which page to evict?

Demand Paging 1. TLB miss 2. Page table walk 3. Page fault (page invalid in page table) 4. Trap to kernel 5. Locate page on disk 6. Allocate page frame Evict page if needed 7. IniJate disk block read into page frame 8. Disk interrupt when DMA complete 9. Mark page as valid 10. Resume process at fauljng instrucjon 11. TLB miss 12. Page table walk to fetch translajon 13. Execute instrucjon

LocaJng a Page on Disk When a page is non- resident, how do we know where to find it on disk? OpJon: Reuse page table entry If resident, page frame If non- resident, disk sector OpJon: Use file system Code pages: executable image (read- only) Data/Heap/Stack: per- segment file in file system, offset in file = offset within segment

AllocaJng a Page Frame Select old page to evict Find all page table entries that refer to old page If page frame is shared (hint: use a coremap) Set each page table entry to invalid Remove any TLB entries (on any core) Why not: remove TLB entries then set to invalid? Write changes on page back to disk, if necessary Why not: write changes to disk then set to invalid?

Has page been modified/recently used? Every page table entry has some bookkeeping Has page been modified? Set by hardware on store instrucjon In both TLB and page table entry Has page been recently used? Set by hardware on in page table entry on every TLB miss Bookkeeping bits can be reset by the OS kernel When changes to page are flushed to disk To track whether page is recently used

Tracking Page ModificaJons (Before) TLB Frame Access Dirty Page Table Physical Memory Page Frames Disk R/W No Frame Access Dirty Old Page A Old Page B Virtual Page B Frame for B Invalid Page A Virtual Page A Frame for A R/W No

Tracking Page ModificaJons (AVer) TLB Frame Access Dirty Page Table Physical Memory Page Frames Disk R/W Yes Frame Access Dirty Old Page A Old Page B Virtual Page B Frame for B Invalid New Page A Virtual Page A Frame for A R/W Yes

Modified/Use Bits are (oven) Virtual Most machines keep modified/use bits in the page table entry (not the core map) why? Physical page is Modified if any page table entry that points to it is modified Recently used if any page table entry that points to it is recently used Superpages One page table entry per superpage Use/modified bit applies to enjre superpage

Use Bits are Fuzzy Page- modified bit must be ground truth What happens if we evict a modified page without wrijng the changes back to disk? Page- use bit can be approximate What happens if we evict a page that is currently being used? Evict any page not used for a while is nearly as good as evict the single page not used for the longest

EmulaJng a Modified Bit (Hardware Loaded TLB) Some processor architectures do not keep a modified bit per page Extra bookkeeping and complexity Kernel can emulate a modified bit: Set all clean pages as read- only On first write to page, trap into kernel Kernel set modified bit in core map Kernel set page table entry as read- write Resume execujon Kernel needs to keep track Current page table permission (e.g., read- only) True page table permission (e.g., writeable, clean)

EmulaJng a Recently Used Bit (Hardware Loaded TLB) Some processor architectures do not keep a recently used bit per page Extra bookkeeping and complexity Kernel can emulate a recently used bit: Set all pages as invalid On first read or write, trap into kernel Kernel set recently used bit in core map Kernel mark page table entry as read or read/write Resume execujon Kernel needs to keep track Current page table permission (e.g., invalid) True page table permission (e.g., read- only, writeable)

Models for ApplicaJon File I/O Explicit read/write system calls Data copied to user process using system call ApplicaJon operates on data Data copied back to kernel using system call Memory- mapped files Open file as a memory segment Program uses load/store instrucjons on segment memory, implicitly operajng on the file Page fault if porjon of file is not yet in memory Kernel brings missing blocks into memory, restarts process

Advantages to Memory- mapped Files Programming simplicity, esp for large files Operate directly on file, instead of copy in/copy out Zero- copy I/O Data brought from disk directly into page frame Pipelining Process can start working before all the pages are populated Interprocess communicajon Shared memory segment vs. temporary file

ImplemenJng Memory- Mapped Files Memory mapped file is a (logical) segment Per segment access control (read- only, read- write) File pages brought in on demand Using page fault handler ModificaJons wrixen back to disk on evicjon, file close Using per- page modified bit TransacJonal (atomic, durable) updates to memory mapped file requires more mechanism

From Memory- Mapped Files to Demand- Paged Virtual Memory Every process segment backed by a file on disk Code segment - > code porjon of executable Data, heap, stack segments - > temp files Shared libraries - > code file and temp data file Memory- mapped files - > memory- mapped files When process ends, delete temp files Unified memory management across file buffer and process memory

Cache Replacement Policy On a cache miss, how do we choose which entry to replace? Assuming the new entry is more likely to be used in the near future In direct mapped caches, not an issue! Policy goal: reduce cache misses Improve expected case performance Also: reduce likelihood of very poor performance

A Simple Policy Random? Replace a random entry FIFO? Replace the entry that has been in the cache the longest Jme What could go wrong?

FIFO in AcJon Worst case for FIFO is if program strides through memory that is larger than the cache

MIN, LRU, LFU MIN Replace the cache entry that will not be used for the longest Jme into the future OpJmality proof based on exchange: if evict an entry used sooner, that will trigger an earlier cache miss Least Recently Used (LRU) Replace the cache entry that has not been used for the longest Jme in the past ApproximaJon of MIN Least Frequently Used (LFU) Replace the cache entry used the least oven (in the recent past)

LRU/MIN for SequenJal Scan

Belady s Anomaly

QuesJon How accurately do we need to track the least recently/least frequently used page? If miss cost is low, any approximajon will do Hardware caches If miss cost is high but number of pages is large, any not recently used page will do Main memory paging with small pages If miss cost is high and number of pages is small, need to be precise Main memory paging with superpages

Clock Algorithm: EsJmaJng LRU Hardware sets use bit Periodically, OS sweeps through all pages If page is unused, reclaim If page is used, mark as unused Page Frames 0- use:0 1- use:1 2- use:0 3- use:0 4- use:0 5- use:1 6- use:1 8- use:0 7- use:1

Nth Chance: Not Recently Used Instead of one bit per page, keep an integer notinusesince: number of sweeps since last use Periodically sweep through all page frames if (page is used) { notinuseforxsweeps = 0; } else if (notinuseforxsweeps < N) { notinuseforxsweeps++; } else { reclaim page; write modificajons if needed }

ImplementaJon Note Clock and Nth Chance can run synchronously In page fault handler, run algorithm to find next page to evict Might require wrijng changes back to disk first Or asynchronously Create a thread to maintain a pool of recently unused, clean pages Find recently unused dirty pages, write mods back to disk Find recently unused clean pages, mark as invalid and move to pool On page fault, check if requested page is in pool! If not, evict page from the pool

Recap MIN is opjmal replace the page or cache entry that will be used farthest into the future LRU is an approximajon of MIN For programs that exhibit spajal and temporal locality Clock/Nth Chance is an approximajon of LRU Bin pages into sets of not recently used

Working Set Model Working Set: set of memory locajons that need to be cached for reasonable cache hit rate Thrashing: when system has too small a cache

Cache Working Set 100% 75% Hit Rate 50% 25% 0% 1 2 4 8 16 Cache Size (KB)

Phase Change Behavior 100% 75% Hit Rate 50% 25% 0% Time

QuesJon What happens to system performance as we increase the number of processes? If the sum of the working sets > physical memory?

Zipf DistribuJon Caching behavior of many systems are not well characterized by the working set model An alternajve is the Zipf distribujon Popularity ~ 1/k^c, for kth most popular item, 1 < c < 2

Zipf DistribuJon Popularity 1 k Rank

Zipf Examples Web pages Movies Library books Words in text Salaries City populajon Common thread: popularity is self- reinforcing

Zipf and Caching 1 Cache Hit Rate 0.001%.01%.1% 1% 10% all Cache Size (Log Scale)

ImplemenJng LFU First Jme an object is referenced, is it: Unpopular, so evict quickly? New and possibly popular, so avoid evicjng? Compute frequency from first observajon # of references/jme since first loaded into cache ImplicaJon: which page to evict changes dynamically over Jme Sort list each Jme we need to evict a page?

SoVware Cache Replacement Object size can vary Evict large objects to make room for smaller ones? Cost of cache miss can vary Local flash vs. disk vs. remote data center Cache computajon: miss cost can be arbitrary Replacement algorithm can be very complex

Power of Choices Pick k objects at random Eval each object: how good a candidate is this? FuncJon can consider LRU, LFU, Jme in cache, object size, cost of replacement, Evict best choice Keep next best 2-3 objects for next iterajon If k ~ 10, very close approx of perfect sort On an arbitrary funcjon!

Cache Lookup: Fully AssociaJve address value address =? =? =? =? match at any address? yes return value

Cache Lookup: Direct Mapped address value hash(address) =? match at hash(address)? yes return value

Cache Lookup: Set AssociaJve address value address value hash(address) 0x0053 0x120d =? match at hash(address)? yes =? match at hash(address)? yes return value return value

Page Coloring What happens when cache size >> page size? Direct mapped or set associajve MulJple pages map to the same cache line OS page assignment maxers! Example: 8MB cache, 4KB pages 1 of every 2K pages lands in same place in cache What should the OS do?

Page Coloring Processors Virtual Address Address Mod K Cache Memory O K 2K 3K Choose pages for each process to balance usage across page types (colors)

DefiniJons Cache Copy of data that is faster to access than the original Hit: if cache has copy Miss: if cache does not have copy Cache block Unit of cache storage (muljple memory locajons) Temporal locality Programs tend to repeatedly reference the same memory locajons Example: instrucjons in a loop SpaJal locality Programs tend to reference nearby locajons Example: data in a loop

Cache Concept (Read) Cache Fetch Address Address In Cache? No Fetch Address Yes Store Value in Cache

Cache Concept (Write) Cache Store Value at Address Store Value at Address Address In Cache? No Fetch Address WriteBuffer Write through: changes sent immediately to next level of storage Write back: changes stored in cache unjl cache block is replaced Yes Store Value in Cache If Write Through Store Value at Address

Memory Hierarchy i7 has 8MB as shared 3 rd level cache; 2 nd level cache is per- core

Demand Paging on MIPS 1. TLB miss 2. Trap to kernel 3. Page table walk 4. Find page is invalid 5. Locate page on disk 6. Allocate page frame Evict page if needed 7. IniJate disk block read into page frame 8. Disk interrupt when DMA complete 9. Mark page as valid 10. Load TLB entry 11. Resume process at fauljng instrucjon 12. Execute instrucjon

EmulaJng Modified/Use Bits w/ MIPS SoVware Loaded TLB MIPS TLB entries can be read- only or read- write On a TLB read miss: If page is clean (in core map), load TLB entry as read- only if page is dirty, load as read- write Mark page as recently used in core map On TLB write miss: Mark page as modified/recently used in core map Load TLB entry as read- write On a TLB write to an unmodified page: Mark page as modified/recently used in core map Reset TLB entry to be read- write

Tracking Recently Used Pages (MIPS) MIPS takes a trap on every TLB miss No trap when TLB entry is evicted (!) Keep pages sorted by how recently used? Every TLB miss, move to front of list Evict off the tail of the list SJll an approximajon! Why? Can we get by with less state per page?

Tracking Recently Used Pages (MIPS) Keep pages approximately sorted? (bit per page) AcJve pages, recently loaded into the TLB InacJve pages, not recently loaded Background thread to flush inacjve pages back to disk Evict any clean inacjve page MulJple pools acjve, not as acjve, even less acjve,, inacjve Flush dirty inacjve pages Evict any clean inacjve page