Virtual Memory Paging



Similar documents
COS 318: Operating Systems. Virtual Memory and Address Translation

OPERATING SYSTEM - VIRTUAL MEMORY

& Data Processing 2. Exercise 3: Memory Management. Dipl.-Ing. Bogdan Marin. Universität Duisburg-Essen

Operating Systems. Virtual Memory

Computer Architecture

Lecture 17: Virtual Memory II. Goals of virtual memory

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

Virtual vs Physical Addresses

Virtual Memory. Virtual Memory. Paging. CSE 380 Computer Operating Systems. Paging (1)

Chapter 12. Paging an Virtual Memory Systems

HY345 Operating Systems

Operating Systems. Lecture 03. February 11, 2013

Virtual Memory. How is it possible for each process to have contiguous addresses and so many of them? A System Using Virtual Addressing

COS 318: Operating Systems. Storage Devices. Kai Li and Andy Bavier Computer Science Department Princeton University

Board Notes on Virtual Memory

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

Operating Systems, 6 th ed. Test Bank Chapter 7

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

COS 318: Operating Systems. Storage Devices. Kai Li Computer Science Department Princeton University. (

The Quest for Speed - Memory. Cache Memory. A Solution: Memory Hierarchy. Memory Hierarchy

Computer Architecture

A Deduplication File System & Course Review

Computer Organization and Architecture. Characteristics of Memory Systems. Chapter 4 Cache Memory. Location CPU Registers and control unit memory

Midterm Exam #2 Solutions November 10, 1999 CS162 Operating Systems

361 Computer Architecture Lecture 14: Cache Memory

Memory unit sees only the addresses, and not how they are generated (instruction counter, indexing, direct)

OS OBJECTIVE QUESTIONS

COS 318: Operating Systems

Far-western University Central Office, Mahendranagar Operating System

The Classical Architecture. Storage 1 / 36

Midterm Exam #2 November 10, 1999 CS162 Operating Systems

Kernel Optimizations for KVM. Rik van Riel Senior Software Engineer, Red Hat June

Peter J. Denning, Naval Postgraduate School, Monterey, California

Comparison of Memory Management Systems of BSD, Windows, and Linux

Memory Management 1. Memory Management. Multitasking without memory management is like having a party in a closet.

Chapter 11 I/O Management and Disk Scheduling

Linux Process Scheduling Policy

Memory management. Chapter 4: Memory Management. Memory hierarchy. In an ideal world. Basic memory management. Fixed partitions: multiple programs

W4118 Operating Systems. Instructor: Junfeng Yang

COS 318: Operating Systems

W4118: segmentation and paging. Instructor: Junfeng Yang

How To Write A Page Table

Memory Management Outline. Background Swapping Contiguous Memory Allocation Paging Segmentation Segmented Paging

MS SQL Performance (Tuning) Best Practices:

Page Replacement for Write References in NAND Flash Based Virtual Memory Systems

Computer-System Architecture

OPERATING SYSTEMS MEMORY MANAGEMENT

Lecture 16: Storage Devices

Performance Comparison of RTOS

Memory Management under Linux: Issues in Linux VM development

A COOL AND PRACTICAL ALTERNATIVE TO TRADITIONAL HASH TABLES

Chapter 1 Computer System Overview

Scheduling. Yücel Saygın. These slides are based on your text book and on the slides prepared by Andrew S. Tanenbaum

Virtual Shared Memory (VSM)

National CR16C Family On-Chip Emulation. Contents. Technical Notes V

COS 318: Operating Systems. Virtual Machine Monitors

1. Computer System Structure and Components

Managing Prefetch Memory for Data-Intensive Online Servers

White paper. QNAP Turbo NAS with SSD Cache

Project 3A. CS 170, Tao Yang

Virtual Memory Behavior in Red Hat Linux Advanced Server 2.1

Timing of a Disk I/O Transfer

Chapter 7 Memory Management

Facebook: Cassandra. Smruti R. Sarangi. Department of Computer Science Indian Institute of Technology New Delhi, India. Overview Design Evaluation

Hands-On Microsoft Windows Server 2008

Recovery Principles in MySQL Cluster 5.1

Secondary Storage. Any modern computer system will incorporate (at least) two levels of storage: magnetic disk/optical devices/tape systems

Chapter 11 I/O Management and Disk Scheduling

CSC 2405: Computer Systems II

Midterm II SOLUTIONS December 4 th, 2006 CS162: Operating Systems and Systems Programming

We r e going to play Final (exam) Jeopardy! "Answers:" "Questions:" - 1 -

Operating Systems. Steven Hand. Michaelmas / Lent Term 2008/ lectures for CST IA. Handout 3. Operating Systems N/H/MWF@12

Data Storage - I: Memory Hierarchies & Disks

Operating Systems Lecture #6: Process Management

6.828 Operating System Engineering: Fall Quiz II Solutions THIS IS AN OPEN BOOK, OPEN NOTES QUIZ.

Database Maintenance Essentials

The Xen of Virtualization

The Lagopus SDN Software Switch. 3.1 SDN and OpenFlow. 3. Cloud Computing Technology

Virtualization. Explain how today s virtualization movement is actually a reinvention

Memory ICS 233. Computer Architecture and Assembly Language Prof. Muhamed Mudawar

COS 318: Operating Systems. Virtual Machine Monitors

Geospatial Server Performance Colin Bertram UK User Group Meeting 23-Sep-2014

Pros and Cons of HPC Cloud Computing

b. A program calls malloc to request more memory from the operating system. i.what system call would malloc use to request more memory of the OS?

This Unit: Virtual Memory. CIS 501 Computer Architecture. Readings. A Computer System: Hardware

Confinement Problem. The confinement problem Isolating entities. Example Problem. Server balances bank accounts for clients Server security issues:

Exception and Interrupt Handling in ARM

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

Big Graph Processing: Some Background

find model parameters, to validate models, and to develop inputs for models. c 1994 Raj Jain 7.1

Database Management Systems

Lecture 1: Data Storage & Index

CHAPTER 6 TASK MANAGEMENT

All JNTU WORLD. Wipro Previous Year Question Paper. Aptitude Questions :

Timer, Interrupt, Exception in ARM

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Transcription:

COS 318: Operating Systems Virtual Memory Paging Kai Li Computer Science Department Princeton University (http://www.cs.princeton.edu/courses/cos318/)

Today s Topics Paging mechanism Page replacement algorithms 2

Virtual Memory Paging Simple world Load entire process into memory. Run it. Exit. Problems Slow (especially with big processes) Wasteful of space (doesn t use all of its memory all the time) Solution Demand paging: only bring in pages actually used Paging: only keep frequently used pages in memory Mechanism: Virtual memory maps some to physical pages, some to disk 3

VM Paging Steps Steps. subl $20 %esp movl 8(%esp), %eax. fault Memory reference (may cause a TLB miss) TLB entry invalid triggers a page fault and VM handler takes over Move page from disk to memory Update TLB entry w/ pp#, valid bit Restart the instruction Memory reference again VM system v vp# v vp# v i vp# v vp# v vp#. TLB pp# pp# dp# pp# pp# pp# 4

Virtual Memory Issues How to switch a process after a fault? Need to save state and resume Is it the same as an interrupt? What to page in? Just the faulting page or more? Want to know the future What to replace? Cache always too small, which page to replace? Want to know the future... 5

How Does Page Fault Work?. subl $20 %esp movl 8(%esp), %eax... VM fault handler() { Save states... iret } User program should not be aware of the page fault Fault may have happened in the middle of the instruction! Can we skip the faulting instruction? Is a faulting instruction always restartable? 6

What to Page In? Page in the faulting page Simplest, but each page in has substantial overhead Page in more pages each time May reduce page faults if the additional pages are used Waste space and time if they are not used Real systems do some kind of prefetching Applications control what to page in Some systems support for user-controlled prefetching But, many applications do not always know 7

VM Page Replacement Things are not always available when you want them It is possible that no unused page frame is available VM needs to do page replacement On a page fault If there is an unused frame, get it If no unused page frame available, Find a used page frame If it has been modified, write it to disk Invalidate its current PTE and TLB entry Load the new page from disk Update the faulting PTE and remove its TLB entry Restart the faulting instruction General data structures A list of unused page frames A table to map page frames to PID and virtual pages, why? Page Replacement 8

Which Used Page Frame To Replace? Random Optimal or MIN algorithm NRU (Not Recently Used) FIFO (First-In-First-Out) FIFO with second chance Clock LRU (Least Recently Used) NFU (Not Frequently Used) Aging (approximate LRU) Working Set WSClock 9

Optimal or MIN Algorithm: Replace the page that won t be used for the longest time (Know all references in the future) Example Reference string: 4 page frames 1 2 3 4 1 2 5 1 2 3 4 5 6 faults Pros Cons Optimal solution and can be used as an off-line analysis method No on-line implementation 10

Revisit TLB and Page Table Virtual address VPage # offset VPage# VPage# VPage# PPage#... PPage#. PPage#... TLB Miss Page Table Hit PPage # offset Important bits for paging Reference: Set when referencing a location in the page Modify: Set when writing to a location in the page 11

Not Recently Used (NRU) Algorithm Example Pros Cons Randomly pick a page from the following (in this order) Not referenced and not modified Not referenced and modified Referenced and not modified Referenced and modified Clear reference bits 4 page frames Reference string 8 page faults Implementable 1 2 3 4 1 2 5 1 2 3 4 5 Require scanning through reference bits and modified bits 12

First-In-First-Out (FIFO) Page out 5 3 4 7 9 11 2 1 15 Recently loaded Algorithm Example Pros Cons Throw out the oldest page 4 page frames Reference string 10 page faults Low-overhead implementation May replace the heavily used pages 1 2 3 4 1 2 5 1 2 3 4 5 13

More Frames Fewer Page Faults? Consider the following with 4 page frames Algorithm: FIFO replacement Reference string: 1 2 3 4 1 2 5 1 2 3 4 5 10 page faults Same string with 3 page frames Algorithm: FIFO replacement Reference string: 9 page faults! 1 2 3 4 1 2 5 1 2 3 4 5 This is so called Belady s anomaly (Belady, Nelson, Shedler 1969) 14

FIFO with 2nd Chance Page out If ref bit = 1 5 3 4 7 9 11 2 1 15 Recently loaded Algorithm Check the reference-bit of the oldest page If it is 0, then replace it If it is 1, clear the referent-bit, put it to the end of the list, and continue searching Example Pros Cons 4 page frames Reference string: 8 page faults Simple to implement The worst case may take a long time 1 2 3 4 1 2 5 1 2 3 4 5 15

Clock FIFO clock algorithm Hand points to the oldest page On a page fault, follow the hand to inspect pages Second chance If the reference bit is 1, set it to 0 and advance the hand If the reference bit is 0, use it for replacement Compare with the FIFO with 2nd chance What s the difference? What if memory is very large Take a long time to go around? Oldest page 16

Least Recently Used Least Recently used Algorithm 5 3 4 7 9 11 2 1 15 Recently loaded Example Pros Cons Replace page that hasn t been used for the longest time Order the pages by time of reference Timestamp for each referenced page 4 page frames Reference string: 8 page faults Good to approximate MIN 1 2 3 4 1 2 5 1 2 3 4 5 Difficult to implement 17

Approximation of LRU Use CPU ticks For each memory reference, store the ticks in its PTE Find the page with minimal ticks value to replace Use a smaller counter Most recently used Least recently used LRU Pages in order of last reference N categories Crude LRU Pages referenced since the last page fault Pages not referenced since the last page fault 2 categories 8-bit count 254 255 256 categories 18

Aging: Not Frequently Used (NFU) Algorithm Shift reference bits into counters Pick the page with the smallest counter to replace 00000000 00000000 10000000 01000000 10100000 00000000 10000000 01000000 10100000 01010000 10000000 11000000 11100000 01110000 00111000 00000000 Old example 4 page frames Reference string: 8 page faults 00000000 Main difference between NFU and LRU? NFU has a short history (counter length) How many bits are enough? In practice 8 bits are quite good 00000000 10000000 01000000 1 2 3 4 1 2 5 1 2 3 4 5 19

Program Behavior (Denning 1968) 80/20 rule > 80% memory references are within <20% of memory space > 80% memory references are made by < 20% of code Spatial locality # Page faults Neighbors are likely to be accessed Temporal locality The same page is likely to be accessed again in the near future # Pages in memory 20

Working Set Main idea (Denning 1968, 1970) Define a working set as the set of pages in the most recent K page references Keep the working set in memory will reduce page faults significantly Approximate working set The set of pages of a process used in the last T seconds An algorithm On a page fault, scan through all pages of the process If the reference bit is 1, record the current time for the page If the reference bit is 0, check the time of last use, If the page has not been used within T, replace the page Otherwise, go to the next Add the faulting page to the working set 21

WSClock Follow the clock hand If the reference bit is 1 Set reference bit to 0 Set the current time for the page Advance the clock hand If the reference bit is 0, check time of last use If the page has been used within δ, go to the next If the page has not been used within δ and modify bit is 1 Schedule the page for page out and go to the next If the page has not been used within δ and modify bit is 0 Replace this page 22

Replacement Algorithms The algorithms Random Optimal or MIN algorithm NRU (Not Recently Used) FIFO (First-In-First-Out) FIFO with second chance Clock LRU (Least Recently Used) NFU (Not Frequently Used) Aging (approximate LRU) Working Set WSClock Which are your top two? 23

Summary VM paging Page fault handler What to page in What to page out LRU is good but difficult to implement Clock (FIFO with 2 nd hand) is considered a good practical solution Working set concept is important 24