Page Frame Reclaiming

Similar documents
CSE 506: Opera.ng Systems. The Page Cache. Don Porter

The Page Cache 3/16/16. Logical Diagram. Background. Recap of previous lectures. The address space abstracvon. Today s Problem.

Lecture 12: Demand Paging

Ext3/4 file systems. Don Porter CSE 506

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

The Art and Science of Memory Allocation

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

VFS, Continued. Don Porter CSE 506

CS 153 Design of Operating Systems Winter 2016

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

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

Process Address Spaces and Binary Formats

OPERATING SYSTEM. Chapter 9: Virtual Memory

Chapter 9: Virtual Memory

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

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

CSE506: Operating Systems CSE 506: Operating Systems

CSE 120 Principles of Operating Systems

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

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

The Art and Science of Memory Alloca4on

Virtual Memory. ICS332 Operating Systems

CSE 120 Principles of Operating Systems Spring 2017

Address spaces and memory management

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

Virtual Memory III. Jo, Heeseung

CSE506: Operating Systems CSE 506: Operating Systems

Chapter 8: Virtual Memory. Operating System Concepts

Virtual File System. Don Porter CSE 306

CS140 Final Review. Winter 2014

Multi-level Translation. CS 537 Lecture 9 Paging. Example two-level page table. Multi-level Translation Analysis

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

CS 318 Principles of Operating Systems

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

Chapter 6: Demand Paging

Applications of. Virtual Memory in. OS Design

CS420: Operating Systems

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

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

Recap: Memory Management

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

CSE506: Operating Systems CSE 506: Operating Systems

Scheduling, part 2. Don Porter CSE 506

Virtual Memory #3 Oct. 10, 2018

Virtual Memory Outline

Block Device Scheduling. Don Porter CSE 506

Block Device Scheduling

RCU. ò Dozens of supported file systems. ò Independent layer from backing storage. ò And, of course, networked file system support

NUMA replicated pagecache for Linux

Memory Management. Fundamentally two related, but distinct, issues. Management of logical address space resource

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

PROCESS VIRTUAL MEMORY PART 2. CS124 Operating Systems Winter , Lecture 19

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

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

ò Very reliable, best-of-breed traditional file system design ò Much like the JOS file system you are building now

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

Chapter 10: Virtual Memory

Memory Hierarchy. Mehran Rezaei

Operating System Concepts 9 th Edition

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

MEMORY MANAGEMENT. Jo, Heeseung

Virtual Memory Management

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 25

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Operating System Concepts

The cow and Zaphod... Virtual Memory #2 Feb. 21, 2007

Outline. V Computer Systems Organization II (Honors) (Introductory Operating Systems) Advantages of Multi-level Page Tables

FILE SYSTEMS, PART 2. CS124 Operating Systems Fall , Lecture 24

Lecture 4: Memory Management & The Programming Interface

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

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

Virtual File System. Don Porter CSE 506

CS307: Operating Systems

ò Paper reading assigned for next Tuesday ò Understand low-level building blocks of a scheduler User Kernel ò Understand competing policy goals

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Memory Management. Jo, Heeseung

First-In-First-Out (FIFO) Algorithm

Where are we in the course?

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

VIRTUAL MEMORY READING: CHAPTER 9

Page Replacement Chap 21, 22. Dongkun Shin, SKKU

CSE 153 Design of Operating Systems

Operating Systems Design Exam 2 Review: Fall 2010

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

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

Operating Systems Design Exam 2 Review: Spring 2011

Design Overview of the FreeBSD Kernel CIS 657

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

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

CS 416: Opera-ng Systems Design March 23, 2012

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

16 Sharing Main Memory Segmentation and Paging

CS370 Operating Systems

Advanced Database Systems

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

Basic Memory Management

Page Replacement Chap 21, 22. Dongkun Shin, SKKU

Chapter 9: Virtual Memory

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 451: Operating Systems Winter Page Table Management, TLBs and Other Pragmatics. Gary Kimura

Transcription:

Page Frame Reclaiming Don Porter Binary Formats RCU Memory Management File System Logical Diagram Memory Allocators Threads System Calls Today s Lecture Networking (kernel level Sync mem. management) Device CPU Drivers Scheduler User Kernel Interrupts Disk Net Consistency Hardware 1 2 Last Ome We saw how you go from a file or process to the consotuent memory pages making it up Where in memory is page 2 of file foo? Or, where is address 0x1000 in process 100? Today, we look at reverse mapping: Given physical page X, what has a reference to it? Then we will look at page reclamaoon: Which page is the best candidate to reuse? MoOvaOon: Swapping Most OSes allow virtual memory to become overcommi_ed Processes may allocate more virtual memory than there is physical memory in the system How does this work? OS transparently takes some pages away and writes them to disk I.e., the OS swaps them to disk and reassigns the physical page 3 4 Swapping, cont. If we swap a page out, what do we do with the old page table entries poinong to it? We clear the PTE_P bit so that we get a page fault What do we do when we get a page fault for a swapped page? We need to allocate another physical page, reread the page from disk, and re-map the new page Choices, choices The Linux kernel decides what to swap based on scanning the page descriptor table Similar to the Pages array in JOS I.e., primarily by looking at physical pages Today s lecture: 1) Given a physical page descriptor, how do I find all of the mappings? Remember, pages can be shared. 2) What strategies should we follow when selecong a page to swap? 5 6 1

Shared memory Recall: A represents a region of a process s virtual address space A is private to a process Yet physical pages can be shared The pages caching libc in memory Even anonymous applicaoon data pages can be shared, ager a copy-on-write fork() So far, we have elided this issue. No longer! Anonymous memory When anonymous memory is mapped, a is created Pages are added on demand (laziness rules!) When the first page is added, an anon_ structure is also created and page descriptor point to anon_ anon_ stores all mapping s in a circular linked list When a mapping becomes shared (e.g., COW fork), create a new VMA, link it on the anon_ list 7 8 Process A Example Physical page descriptors anon Virtual memory Physical memory Process B (forked) Process A Example (2 nd Page) Physical page descriptors No update? anon Anonymous VMAs tend to be COW Virtual memory Physical memory Process B 9 10 Reverse mapping Suppose I pick a physical page X, what is it being used for? Many ways you could represent this Remember, some systems have a lot of physical memory So we want to keep fixed, per-page overheads low Can dynamically allocate some extra bookkeeping Linux strategy Add 2 fields to each page descriptor _mapcount: Tracks the number of acove mappings -1 == unmapped 0 == single mapping (unshared) 1+ == shared mapping: Pointer to the owning object Address space (file/device) or anon_ (process) Least Significant Bit encodes the type (1 == anon_) 11 12 2

Anonymous page lookup Given a physical address, page descriptor index is just simple division by page size Given a page descriptor: Look at _mapcount to see how many mappings. If 0+: Read mapping to get pointer to the anon_ Be sure to check, mask out low bit Iterate over s on the anon_ list Linear scan of page table entries for each -> mm -> pgdir Page 0x10 _mapcount: 1 mapping: Example (anon + low bit) Physical page descriptors Process foreach A anon Virtual memory Linear scan of page tables Process B 13 Page 0x10000 Divide by 0x1000 (4k) Physical memory 14 File vs. anon mappings Given a page mapping a file, we store a pointer in its page descriptor to the inode address space page->index caches the offset into the file being mapped Now to find all processes mapping the file So, let s just do the same thing for files as anonymous mappings, no? Could just link all VMAs mapping a file into a linked list on the inode s address_space. 2 complicaoons: ComplicaOon 1 Not all file mappings map the enore file Many map only a region of the file So, if I am looking for all mappings of page 4 of a file a linear scan of each mapping may have to filter s that don t include page 4 15 16 ComplicaOon 2 IntuiOon: anonymous mappings won t be shared much How many children won t exec a new executable? In contrast, (some) mapped files will be shared a lot Example: libc Problem: Lots of entries on the list + many that might not overlap SoluOon: Need some sort of filter Priority Search Tree Idea: binary search tree that uses overlapping ranges as node keys Bigger, enclosing ranges are the parents, smaller ranges are children Not balanced (in Linux, some uses balance them) Use case: Search for all ranges that include page N Most of that logarithmic lookup goodness you love from tree-structured data! 17 18 3

Figure 17-2 (from Understanding the Linux Kernel) 0 1 2 3 4 5 radix size heap 0,2,2 0,4,4 2,3,5 2,0,2 1,2,3 0,0,0 0,4,4 2,3,5 0,0,0 0,2,2 1,2,3 2,0,2 How to find page 1? 0 1 2 3 4 5 radix size heap 0,2,2 0,4,4 2,3,5 2,0,2 1,2,3 0,0,0 All 0,4,4 2,3,5 All Right All All 0,0,0 0,2,2 1,2,3 2,0,2 Leg (a) (b) (a) (b) Figure 17-2. A simple example of priority search tree Radix start of interval, heap = last page Range is exclusive, e.g., [0, 5) Figure 17-2. A simple example of priority search tree If in range: search both children If out of range: search only right or leg child 19 20 PST + s Each node in the PST contains a list of s mapping that interval Only one for unusual mappings So what about duplicates (ex: all programs using libc)? A very long list on the (0, filesz, filesz) node I.e., the root of the tree Reverse lookup, review Given a page, how do I find all mappings? 21 22 Problem 2: Reclaiming UnOl there is a problem, kernel caches and processes can go wild allocaong memory SomeOmes there is a problem, and the kernel needs to reclaim physical pages for other uses Low memory, hibernaoon, free memory below a goal Which ones to pick? Goal: Minimal performance disrupoon on a wide range of systems (from phones to supercomputers) Types of pages Unreclaimable free pages (obviously), pages pinned in memory by a process, temporarily locked pages, pages used for certain purposes by the kernel Swappable anonymous pages, tmpfs, shared IPC memory Syncable cached disk data Discardable unused pages in cache allocators 23 24 4

General principles Free harmless pages first Steal pages from user programs, especially those that haven t been used recently When a page is reclaimed, remove all references at once Removing one reference is a waste of Ome Temporal locality: get pages that haven t been used in a while Laziness: Favor pages that are cheaper to free Ex: WaiOng on write back of dirty data takes Ome Note: Dirty pages are soll reclaimed, just not preferred! Another view Suppose the system is bogging down because memory is scarce The problem is only going to go away permanently if a process can get enough memory to finish Then it will free memory permanently! When the OS reclaims memory, we want to avoid harming progress by taking away memory a process really needs to make progress If possible, avoid this with educated guesses 25 26 LRU lists All pages are on one of 2 LRU lists: acove or inacove IntuiOon: a page access causes it to be switched to the acove list A page that hasn t been accessed in a while moves to the inacove list How to detect use? Tag pages with last access Ome Obviously, explicit kernel operaoons (mmap, mprotect, read, etc.) can update this What about when a page is mapped? Remember those hardware access bits in the page table? Periodically clear them; if they don t get re-set by the hardware, you can assume the page is cold If they do get set, it is hot 27 28 Big picture Kernel keeps a heurisoc target of free pages Makes a best effort to maintain that target; can fail Kernel gets really worried when allocaoons start failing In the worst case, starts out-of-memory (OOM) killing processes unol memory can be reclaimed Editorial Choosing the right pages to free is a problem without a lot of good science behind it Many systems don t cope well with low-memory condioons But they need to get be_er (Think phones and other small devices) Important problem perhaps an opportunity? 29 30 5

Summary Reverse mappings for shared: Anonymous pages File-mapping pages Basic tricks of page frame reclaiming LRU lists Free cheapest pages first Unmap all at once Etc. 31 6