Swapping. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Similar documents
Swapping. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Virtual Memory III. Jo, Heeseung

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

CS 153 Design of Operating Systems Winter 2016

CSE 153 Design of Operating Systems

CSE 120 Principles of Operating Systems

Lecture 12: Demand Paging

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

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

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

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Operating Systems. Operating Systems Sina Meraji U of T

MEMORY: SWAPPING. Shivaram Venkataraman CS 537, Spring 2019

Page replacement algorithms OS

Virtual Memory Management

Page Tables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Lecture 14 Page Replacement Policies

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

Virtual Memory Design and Implementation

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

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

CS370 Operating Systems

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

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

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

Page Replacement Algorithms

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

Page Replacement Chap 21, 22. Dongkun Shin, SKKU

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

VIRTUAL MEMORY READING: CHAPTER 9

Operating Systems Lecture 6: Memory Management II

Memory management, part 2: outline

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

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

Address spaces and memory management

Last Class: Demand Paged Virtual Memory

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

Paging and Page Replacement Algorithms

CS370 Operating Systems

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

CS 4284 Systems Capstone

Page Replacement Chap 21, 22. Dongkun Shin, SKKU

Chapter 4 Memory Management

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

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 8: Virtual Memory. Operating System Concepts

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

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

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

Virtual Memory. Chapter 8

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

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

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

Chapter 8. Virtual Memory

Chapter 4 Memory Management. Memory Management

! 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

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

CS 550 Operating Systems Spring Memory Management: Page Replacement

Past: Making physical memory pretty

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Virtual Memory Design and Implementation

SMD149 - Operating Systems - VM Management

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

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

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

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

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

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

CSE 120 Principles of Operating Systems

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

Virtual Memory Design and Implementation

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

Design Issues 1 / 36. Local versus Global Allocation. Choosing

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

CS370 Operating Systems

Basic Memory Management

Virtual Memory Outline

Memory: Paging System

Clock page algorithm. Least recently used (LRU) NFU algorithm. Aging (NFU + forgetting) Working set. Process behavior

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

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

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

Operating System Concepts

OPERATING SYSTEM. Chapter 9: Virtual Memory

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

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

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

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

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

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

Basic Page Replacement

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.

Transcription:

Swapping Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

Swapping Support processes when not enough physical memory User program should be independent of the amount of physical memory Single process with very large address space Multiple processes with combined address spaces Consider physical memory as a cache for disks Leverage locality of reference within processes Process only uses small amount of address space at a moment Only small amount of address space must be resident in physical memory Store the rest of them to disk EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

Memory Hierarchy Each layer acts as backing store for layer above Registers size Cache Main memory speed cost Disk storage EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

How to Swap Overlays Programmers manually move pieces of code or data in and out of memory as they were needed No special support needed from OS Process-level swapping A process is swapped temporarily out of memory to a backing store It s brought back into memory later for continued execution Page-level swapping Swap pages out of memory to a backing store (swap-out) Swap pages into memory from the backing store (swap-in) EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

Where to Swap Swap space Disk space reserved for moving pages back and forth The size of the swap space determines the maximum number of memory pages that can be in use Block size is same as the page size Can be a dedicated partition or a file in the file system Physical Memory Swap Space PFN 0 PFN PFN PFN PID 0 (VPN 0) PID 0 (VPN ) PID (VPN ) PID 0 (VPN ) PID (VPN ) Free PID (VPN 0) Blk 0 Blk Blk Blk Blk Blk Blk 6 Blk 7 PID (VPN 0) PID (VPN ) PID (VPN 0) PID (VPN ) PID (VPN ) EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

When to Swap Proactively based on thresholds OS wants to keep a small portion of memory free Two threshold values: HW (high watermark) and LW (low watermark) A background thread called swap daemon (or page daemon) is responsible for freeing memory e.g. kswapd in Linux If (# free pages < LW), the swap daemon starts to evict pages from physical memory If (# free pages > HW), the swap daemon goes to sleep What if the allocation speed is faster than reclamation speed? EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 6

Swapping in Linux Free pages kswapd woken up Allocating process frees pages synchronously zone balanced kswapd sleeps high_wmark_pages low_wmark_pages min_wmark_pages time Rate of page consumption is slowed by kswapd but still allocating too fast GFP_ATOMIC allocation can go below min_wmark_pages(zone) EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 7

What to Swap What happens to each type of page frame on low mem. Kernel code Kernel data Page tables for user processes Kernel stack for user processes User code pages User data pages User heap/stack pages Files mmap ed to user processes Page cache pages Not swapped Not swapped Not swapped Not swapped Dropped Droppedor swapped Swapped Dropped or go to file system Dropped or go to file system Page replacement policy chooses the pages to evict EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 8

Page Replacement Which page in physical memory should be selected as a victim? Write out the victim page to disk if modified (dirty bit set) If the victim page is clean, just discard The original version is either in the file system or in the swap space Why not use direct-mapped or set-associative design similar to CPU caches? Goal: minimize the page fault rate (miss rate) The miss penalty (cost of disk access) is so high (> x00,000) A tiny miss rate quickly dominates the overall AMAT (Average Memory Access Time) EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 9

OPT (or MIN) Belady s optimal replacement policy Replace the page that will not be used for the longest time in the future Shows the lowest fault rate for any page reference stream Problem: have to predict the future Not practical, but good for comparison Reference: PF rate = 7 / EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 0

FIFO () First-In First-Out Replace the page that has been in memory the longest Why might this be good? Maybe, the one brought in the longest ago is not being used Why might this be bad? Maybe, it s not the case Some pages may always be needed Obvious and simple to implement Fair: All pages receive equal residency FIFO suffers from Belady s anomaly The fault rate might increase when the algorithm is given more memory EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

FIFO () Reference: PF rate = 9 / Reference: PF rate = 0 / EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

LRU () Least Recently Used Replace the page that has not been used for the longest time in the past Use past to predict the future cf. OPT wants to look at the future With locality, LRU approximates OPT Stack algorithm: does not suffer from Belady s anomaly Harder to implement: must track which pages have been accessed Does not consider the frequency of page accesses Does not handle all workloads well EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

LRU () Stack algorithms Policies that guarantee increasing memory size does not increase the number of page faults (e.g. OPT, LRU, etc.) Any page in memory with m frames is also in memory with m+ frames Reference: Stack distance: PF rate = 0 / EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

RANDOM Another simple policy Simply picks a random page to replace under memory pressure Simple to implement: no bookkeeping needed Performance depends on the luck of the draw Outperforms FIFO and LRU for certain workloads Reference: PF rate = 8 / EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

Comparisons 00% random 80% references to 0% of the pages Looping 0 pages in sequence EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 6

Implementing LRU Software approach OS maintains ordered list of page frames by reference time When page is referenced: move page to the front of the list When need victim: pick the page in the back of the list Slow on memory reference, fast on replacement Hardware approach Associate timestamp register with each page frame When page is referenced: store system clock in register When need victim: scan through registers to find oldest clock Fast on memory reference, slow on replacement (especially as the size of memory grows) EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 7

CLOCK An LRU approximation algorithm Uses R (Reference) bit in each PTE Arranges all of physical page frames in a big circle A clock hand is used to select a victim When a page fault occurs, the page the hand is pointing to is inspected If (R == ), turn it off and go to next page (second chance) If (R == 0), evict the page Arm moves quickly when pages are needed If memory is large, accuracy of information degrades EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 8

Clock Extensions Clustering: Replace multiple pages at once Expensive to run replacement algorithm A single large write is more efficient than many small ones Use M (modify) bit to give preference to dirty pages More expensive to replace dirty pages Replace pages that have R bit and M bit cleared Add software counter for each page frame Better ability to differentiate across pages Increment software counter if R bit is 0 Smaller counter value means the page accessed more recently Replace pages when counter exceeds some specified limit EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 9

Physical Memory Allocation Polices Fixed-space allocation policy Each process is given a limit of page frames it can use When it reaches its limit, it replaces from its own page frames Local replacement: some process may do well, others suffer Variable-space allocation policy Processes set of pages grows and shrinks dynamically Global replacement: one process can ruin it for the rest Used in Linux EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu) 0

Thrashing What happens when physical memory is not enough to hold all the working sets of processes Working set: a set of pages that a process is using actively Most of the time is spent by an OS paging data back and forth from disk Possible solutions: Kill processes Buy more memory EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

Summary VM mechanisms Physical and virtual addressing Partitioning, segmentation, paging Page table management, TLBs, etc. VM policies Page replacement policy, page allocation policy VM optimizations Demand paging, copy-on-write (space) Multi-level page tables (space) Efficient translation using TLBs (time) Page replacement policy (time) EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

OPT (or MIN) Belady s optimal replacement policy Replace the page that will not be used for the longest time in the future Shows the lowest fault rate for any page reference stream Problem: have to predict the future Not practical, but good for comparison Reference: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Page fault rate = / EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)

LRU () Least Recently Used Replace the page not used for longest time in the past Use past to predict the future With locality, LRU approximates OPT Harder to implement Does not handle all workloads well Reference: Stack distance: PF rate = 0 / EEE0: Introduction to Operating Systems, Fall 07, Jinkyu Jeong (jinkyu@skku.edu)