MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

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

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

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

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

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

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

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

Swapping. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Virtual Memory. Virtual Memory

Operating Systems. Operating Systems Sina Meraji U of T

Swapping. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Virtual Memory. Chapter 8

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

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

Chapter 8. Virtual Memory

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

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

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

CS 537 Lecture 6 Fast Translation - TLBs

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

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

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

CS 537: Introduction to Operating Systems Fall 2016: Midterm Exam #1. All cell phones must be turned off and put away.

Basic Memory Management

Memory Hierarchy. Mehran Rezaei

Lecture 14: Cache & Virtual Memory

VIRTUAL MEMORY READING: CHAPTER 9

Chapter 8 Virtual Memory

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

Past: Making physical memory pretty

Virtual Memory Design and Implementation

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

ECE468 Computer Organization and Architecture. Virtual Memory

CSE 153 Design of Operating Systems

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

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

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

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

ECE4680 Computer Organization and Architecture. Virtual Memory

CS 153 Design of Operating Systems Winter 2016

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

CS162 - Operating Systems and Systems Programming. Address Translation => Paging"

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

SMD149 - Operating Systems - VM Management

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

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

CS450/550 Operating Systems

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

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems and Computer Networks. Memory Management. Dr.-Ing. Pascal A. Klein

Memory Management Prof. James L. Frankel Harvard University

Chapter 6: Demand Paging

CS 61C: Great Ideas in Computer Architecture. Virtual Memory

Address spaces and memory management

CS 61C: Great Ideas in Computer Architecture. Virtual Memory III. Instructor: Dan Garcia

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

Memory Management! Goals of this Lecture!

Main Memory: Address Translation

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

UC Berkeley CS61C : Machine Structures

Virtual Memory: From Address Translation to Demand Paging

Caching and Demand-Paged Virtual Memory

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

Operating Systems Lecture 6: Memory Management II

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

Lecture 12: Demand Paging

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

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

CS152 Computer Architecture and Engineering Lecture 18: Virtual Memory

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

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

Virtualizing Memory: Paging. Announcements

CS153: Final Review1. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

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

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

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

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

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

Memory Management! How the hardware and OS give application pgms:" The illusion of a large contiguous address space" Protection against each other"

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

Memory Management. Goals of this Lecture. Motivation for Memory Hierarchy

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

CS370 Operating Systems

Memory Management. An expensive way to run multiple processes: Swapping. CPSC 410/611 : Operating Systems. Memory Management: Paging / Segmentation 1

Chapter 3 - Memory Management

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

CS370 Operating Systems

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

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

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

Chapter 8 Virtual Memory

and data combined) is equal to 7% of the number of instructions. Miss Rate with Second- Level Cache, Direct- Mapped Speed

Simple idea 1: load-time linking. Our main questions. Some terminology. Simple idea 2: base + bound register. Protection mechanics.

ECE331: Hardware Organization and Design

Operating System Concepts

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Virtual Memory 1. Virtual Memory

A Few Problems with Physical Addressing. Virtual Memory Process Abstraction, Part 2: Private Address Space

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

Transcription:

MEMORY: SWAPPING Shivaram Venkataraman CS 537, Spring 2019

ADMINISTRIVIA - Project 2b is out. Due Feb 27 th, 11:59 - Project 1b grades are out

Lessons from p2a? 1. Start early! 2. Sketch out a design? 3. Synthesize ideas from various sources: TAs, stackoverflow.com, gdb, discussion videos 4. Handling edge cases, string handling

AGENDA / LEARNING OUTCOMES Memory virtualization How we support virtual mem larger than physical mem? What are mechanisms and policies for this?

RECAP

PAGING TRANSLATION STEPS For each mem reference: 1. extract VPN (virt page num) from VA (virt addr) 2. check TLB for VPN if miss: 3. calculate addr of PTE (page table entry) 4. read PTE from memory, add to TLB 5. extract PFN from TLB (page frame num) 6. build PA (phys addr) 7. read contents of PA from memory

Combine Paging and Segmentation Divide address space into segments (code, heap, stack) Segments can be variable length Divide each segment into fixed-sized pages Logical address divided into three portions seg # (4 bits) page number (8 bits) page offset (12 bits) Implementation Each segment has a page table Each segment track base (physical address) and bounds of the page table

Multilevel Page Tables 30-bit address: outer page(8 bits) inner page (10 bits) page offset (12 bits) base of page directory

Address format for multilevel Paging 30-bit address: outer page inner page page offset (12 bits) How should logical address be structured? How many bits for each paging level? Goal? Each page table fits within a page PTE size * number PTE = page size Assume PTE size = 4 bytes Page size = 2^12 bytes = 4KB à # bits for selecting inner page = Remaining bits for outer page: 30 = bits

Problem with 2 levels? Problem: page directories (outer level) may not fit in a page Solution: Split page directories into pieces Use another page dir to refer to the page dir pieces. VPN PD idx 0 PD idx 1 PT idx OFFSET How large is virtual address space with 4 KB pages, 4 byte PTEs, (each page table fits in page) 4KB / 4 bytes à 1K entries per level 1 level: 2 levels: 3 levels: 64-bit address: outer page? inner page (10 bits) page offset (12 bits)

FULL SYSTEM WITH TLBS On TLB miss: lookups with more levels more expensive Assume 3-level page table Assume 256-byte pages Assume 16-bit addresses Assume ASID of current process is 211 How many physical accesses for each instruction? (a) 0xAA10: movl 0x1111, %edi ASID VPN PFN Valid 211 0xbb 0x91 1 211 0xff 0x23 1 122 0x05 0x91 1 211 0x05 0x12 0 (Ignore ops changing TLB) (b) 0xBB13: addl $0x3, %edi (c) 0x0519: movl %edi, 0xFF10

https://tinyurl.com/cs537-sp19-bunny1

Inverted Page TAble Only need entries for virtual pages w/ valid physical mappings Naïve approach: Search through data structure <ppn, vpn+asid> to find match Too much time to search entire table Better: Find possible matches entries by hashing vpn+asid Smaller number of entries to search for exact match Managing inverted page table requires software-controlled TLB

Summary: Better PAGE TABLES Problem: Simple linear page tables require too much contiguous memory Many options for efficiently organizing page tables If OS traps on TLB miss, OS can use any data structure Inverted page tables (hashing) If Hardware handles TLB miss, page tables must follow specific format Multi-level page tables used in x86 architecture Each page table fits within a page.

SWAPPING

Motivation OS goal: Support processes when not enough physical memory Single process with very large address space Multiple processes with combined address spaces User code should be independent of amount of physical memory Correctness, if not performance Virtual memory: OS provides illusion of more physical memory Why does this work? Relies on key properties of user processes (workload) and machine architecture (hardware)

code data Program Virtual Memory

code data Program Virtual Memory

Locality of Reference Leverage locality of reference within processes Spatial: reference memory addresses near previously referenced addresses Temporal: reference memory addresses that have referenced in the past Processes spend majority of time in small portion of code Implication: Estimate: 90% of time in 10% of code Process only uses small amount of address space at any moment Only small amount of address space must be resident in physical memory

Memory Hierarchy Leverage memory hierarchy of machine architecture Each layer acts as backing store for layer above size registers cache speed cost main memory disk storage

SWAPPING Intuition Idea: OS keeps unreferenced pages on disk Slower, cheaper backing store than memory Process can run when not all pages are loaded into main memory OS and hardware cooperate to make large disk seem like memory Same behavior as if all of address space in main memory Requirements: OS must have mechanism to identify location of each page in address space à in memory or on disk OS must have policy for determining which pages live in memory and which on disk

SWAPPING Mechanisms Each page in virtual address space maps to one of three locations: Physical main memory: Small, fast, expensive Disk (backing store): Large, slow, cheap Nothing (error): Free Extend page tables with an extra bit: present permissions (r/w), valid, present Page in memory: present bit set in PTE Page on disk: present bit cleared PTE points to block on disk Causes trap into OS when page is referenced

Present Bit Disk Phys Memory PFN valid prot present 10 1 r-x 1-0 - - 23 1 rw- 0-0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - 28 1 rw- 0 4 1 rw- 1 What if access vpn 0xb?

Virtual Memory Mechanisms First, hardware checks TLB for virtual address if TLB hit, address translation is done; page in physical memory Else TLB miss... Hardware or OS walk page tables If PTE designates page is present, then page in physical memory page fault (i.e., present bit is cleared) Else Trap into OS (not handled by hardware) OS selects victim page in memory to replace Write victim page out to disk if modified (add dirty bit to PTE) OS reads referenced page from disk into memory Page table is updated, present bit is set Process continues execution

SWAPPING Policies

SWAPPING Policies Goal: Minimize number of page faults Page faults require milliseconds to handle (reading from disk) Implication: Plenty of time for OS to make good decision OS has two decisions Page selection When should a page (or pages) on disk be brought into memory? Page replacement Which resident page (or pages) in memory should be thrown out to disk?

Page Selection Demand paging: Load page only when page fault occurs Intuition: Wait until page must absolutely be in memory When process starts: No pages are loaded in memory Problems: Pay cost of page fault for every newly accessed page Prepaging (anticipatory, prefetching): Load page before referenced OS predicts future accesses (oracle) and brings pages into memory early Works well for some access patterns (e.g., sequential) Problems? Hints: Combine above with user-supplied hints about page references User specifies: may need page in future, don t need this page anymore, or sequential access pattern,... Example: madvise() in Unix

Page Replacement Which page in main memory should selected as victim? Write out victim page to disk if modified (dirty bit set) If victim page is not modified (clean), just discard OPT: Replace page not used for longest time in future Advantages: Guaranteed to minimize number of page faults Disadvantages: Requires that OS predict the future; Not practical, but good for comparison

Page Replacement FIFO: Replace page that has been in memory the longest Intuition: First referenced long time ago, done with it now Advantages: Fair: All pages receive equal residency; Easy to implement Disadvantage: Some pages may always be needed LRU: Least-recently-used: Replace page not used for longest time in past Intuition: Use past to predict the future Advantages: With locality, LRU approximates OPT Disadvantages: Harder to implement, must track which pages have been accessed Does not handle all workloads well

Page Replacement Example Page reference string: ABCABDADBCB Metric: Miss count Three pages of physical memory ABC A B D A D B C B OPT FIFO LRU

https://tinyurl.com/cs537-sp19-bunny2

Page Replacement Comparison Add more physical memory, what happens to performance? LRU, OPT: Guaranteed to have fewer (or same number of) page faults Smaller memory sizes are guaranteed to contain a subset of larger memory sizes Stack property: smaller cache always subset of bigger FIFO: Usually have fewer page faults Belady s anomaly: May actually have more page faults!

Fifo Performance may Decrease! Consider access stream: ABCDABEABCDE Consider physical memory size: 3 pages vs. 4 pages How many misses with FIFO?

Implementing LRU Software Perfect LRU OS maintains ordered list of physical pages by reference time When page is referenced: Move page to front of list When need victim: Pick page at back of list Trade-off: Slow on memory reference, fast on replacement Hardware Perfect LRU Associate timestamp register with each page When page is referenced: Store system clock in register When need victim: Scan through registers to find oldest clock Trade-off: Fast on memory reference, slow on replacement (especially as size of memory grows) In practice, do not implement Perfect LRU LRU is an approximation anyway, so approximate more Goal: Find an old page, but not necessarily the very oldest

Clock Algorithm Hardware Keep use (or reference) bit for each page frame When page is referenced: set use bit Operating System Page replacement: Look for page with use bit cleared (has not been referenced for awhile) Implementation: Keep pointer to last examined page frame Traverse pages in circular buffer Clear use bits as search Stop when find page with already cleared use bit, replace this page

Clock: Look For a Page Use= Use= Use= Use= Physical Mem: 0 1 2 3 clock hand

Clock Extensions Replace multiple pages at once Intuition: Expensive to run replacement algorithm and to write single block to disk Find multiple victims each time and track free list Use dirty bit to give preference to dirty pages Intuition: More expensive to replace dirty pages Dirty pages must be written to disk, clean pages do not Replace pages that have use bit and dirty bit cleared

SUMMARY: VIRTUAL MEMORY Abstraction: Virtual address space with code, heap, stack Address translation - Contiguous memory: base, bounds, segmentation - Using fixed sizes pages with page tables Challenges with paging - Extra memory references: avoid with TLB - Page table size: avoid with multi-level paging, inverted page tables etc. Larger address spaces: Swapping mechanisms, policies (LRU, Clock)

NEXT STEPS Project 2b: Out now Next class: New module on Concurrency