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

Similar documents
Memory management, part 2: outline

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

Chapter 4 Memory Management

Chapter 4 Memory Management. Memory Management

Module 9: Virtual Memory

Module 9: Virtual Memory

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

Virtual Memory Design and Implementation

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

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

Week 2: Tiina Niklander

Chapter 9: Virtual Memory

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Operating Systems, Fall

Virtual Memory Management

Chapter 9: Virtual-Memory

Virtual Memory Outline

Chapter 9: Virtual Memory

Page replacement algorithms OS

CS450/550 Operating Systems

Perform page replacement. (Fig 8.8 [Stal05])

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

Virtual Memory III. Jo, Heeseung

CS370 Operating Systems

Memory Management Prof. James L. Frankel Harvard University

Background. Demand Paging. valid-invalid bit. Tevfik Koşar. CSC Operating Systems Spring 2007

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

Chapter 10: Virtual Memory. Background

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

Background. Virtual Memory (2/2) Demand Paging Example. First-In-First-Out (FIFO) Algorithm. Page Replacement Algorithms. Performance of Demand Paging

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

Chapter 10: Virtual Memory. Background. Demand Paging. Valid-Invalid Bit. Virtual Memory That is Larger Than Physical Memory

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

CS510 Operating System Foundations. Jonathan Walpole

Paging and Page Replacement Algorithms

Chapter 9: Virtual Memory

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

Lecture 14 Page Replacement Policies

CS370 Operating Systems

Principles of Operating Systems

Chapter 8: Virtual Memory. Operating System Concepts

Basic Page Replacement

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

CS 550 Operating Systems Spring Memory Management: Page Replacement

Operating Systems Lecture 6: Memory Management II

Page Replacement Algorithms

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

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

Chapter 9: Virtual Memory

Virtual Memory Design and Implementation

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

Operating System Concepts

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Objectives. Background. Virtual-address address Space

Virtual Memory Design and Implementation

CS370 Operating Systems

Move back and forth between memory and disk. Memory Hierarchy. Two Classes. Don t

Memory Management Ch. 3

Where are we in the course?

Chapter 6: Demand Paging

1. Background. 2. Demand Paging

Virtual Memory - Overview. Programmers View. Virtual Physical. Virtual Physical. Program has its own virtual memory space.

Optimal Algorithm. Replace page that will not be used for longest period of time Used for measuring how well your algorithm performs

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

Modeling Page Replacement: Stack Algorithms. Design Issues for Paging Systems

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

Lecture 12: Demand Paging

VIRTUAL MEMORY READING: CHAPTER 9

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

Chapter 9: Virtual Memory

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

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

Swapping. Operating Systems I. Swapping. Motivation. Paging Implementation. Demand Paging. Active processes use more physical memory than system has

Memory Management Virtual Memory

Basic Memory Management

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

OPERATING SYSTEM. Chapter 9: Virtual Memory

Demand Paging. Valid-Invalid Bit. Steps in Handling a Page Fault. Page Fault. Transfer of a Paged Memory to Contiguous Disk Space

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

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

MODERN OPERATING SYSTEMS. Chapter 3 Memory Management

Memory Management Outline. Operating Systems. Motivation. Paging Implementation. Accessing Invalid Pages. Performance of Demand Paging

Lecture 17. Edited from slides for Operating System Concepts by Silberschatz, Galvin, Gagne

Operating Systems. Operating Systems Sina Meraji U of T

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

CS307: Operating Systems

OPERATING SYSTEMS. After A.S.Tanenbaum, Modern Operating Systems 3rd edition Uses content with permission from Assoc. Prof. Florin Fortis, PhD

Virtual Memory COMPSCI 386

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

CS 153 Design of Operating Systems Winter 2016

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

Chapter 3: Virtual Memory ว ตถ ประสงค. Background สามารถอธ บายข อด ในการท ระบบใช ว ธ การจ ดการหน วยความจ าแบบเสม อนได

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

CSC Operating Systems Spring Lecture - XIV Virtual Memory - II. Tevfik Ko!ar. Louisiana State University. March 27 th, 2008.

Memory Management. Memory Management. G53OPS: Operating Systems. Memory Management Monoprogramming 11/27/2008. Graham Kendall.

Memory: Paging System

Chapters 9 & 10: Memory Management and Virtual Memory

Operating System Concepts 9 th Edition

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

First-In-First-Out (FIFO) Algorithm

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

Transcription:

Memory management, part 2: outline 1

Page Replacement Algorithms Page fault forces choice o which page must be removed to make room for incoming page? Modified page must first be saved o unmodified just overwritten Better not to choose an often used page o will probably need to be brought back in soon 2

Optimal page replacement algorithm Remove the page that will be referenced latest (or not at all) in the future Unrealistic: assumes we know future sequence of page references Example 7 5 1 3 page frames Assume that, starting from this configuration, the sequence of (virtual) page references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First page to remove is last to be used 3

Optimal page replacement algorithm Remove the page that will be referenced latest Unrealistic: assumes we know future sequence of page references Example 7 5 0 3 page frames Assume that, starting from this configuration, the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First page to remove is last to be used 4

Optimal page replacement algorithm Remove the page that will be referenced latest Unrealistic: assumes we know future sequence of page references Example 7 4 0 3 page frames Assume that, starting from this configuration, the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First page to remove is last to be used 5

Optimal page replacement algorithm Remove the page that will be referenced latest Unrealistic: assumes we know future sequence of page references Example 7 2 0 3 page frames Assume that, starting from this configuration, the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First page to remove is last to be used 6

Optimal page replacement algorithm Remove the page that will be referenced latest Unrealistic: assumes we know future sequence of page references Example 7 1 0 3 page frames Assume that, starting from this configuration, the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 Altogether 4 page replacements. What if we used FIFO? 7

Optimal vs. FIFO Example 7 5 1 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 8

Optimal vs. FIFO Example 0 5 1 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 9

Optimal vs. FIFO Example 0 4 1 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 10

Optimal vs. FIFO Example 0 4 7 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 11

Optimal vs. FIFO Example 2 4 7 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 12

Optimal vs. FIFO Example 2 1 7 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 13

Optimal vs. FIFO Example 2 1 0 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 First in first out 14

Optimal vs. FIFO Example 7 1 0 3 page frames Assume that, starting from this configuration (7,5,1), the sequence of references is: 0, 5, 4, 7, 0, 2, 1, 0, 7 FIFO does 7 replacements, 3 more than optimal. 15

Page replacement: NRU - Not Recently Used Referenced (R) and Modified (M) bits set by hardware, reset by software There are 4 classes of pages, according to the value-pairs of the referenced and modified bits Select a page at random from the least-needed class Easy scheme to implement Referenced=false Referenced=true Modified=false Modified=true A B C D 16

2'nd chance FIFO replacement algorithm May be implemented by using a queue FIFO: oldest page may be most referenced page An improvement: second chance FIFO o Inspect pages from oldest to newest o If page's referenced bit is on, give it a second chance: Clear bit Move to end of queue o Else Remove page Second chance FIFO can be implemented more efficiently as a circular queue: the clock algorithm 17

Second Chance Page Replacement Algorithm Operation of second chance FIFO o pages sorted in FIFO order o Page list if fault occurs at time 20, A has R bit set (numbers above pages are times of insertion to list) o When A moves forward its R bit is cleared, timestamp updated 18

The Clock Page Replacement Algorithm 19

LRU - Least Recently Used Most recently used pages have high probability of being referenced again (temporal locality of reference) Replace page used least recently Not easy to implement total order must be maintained Possible hardware solutions o Use a large HW-manipulated counter, store counter value in page entry on each reference, select page with smallest value. o Use an nxn bit array (see next slide) When page-frame k is referenced, set all bits of row k to 1 and all bits of column k to 0. The row with lowest binary value is least recently used 20

LRU with bit tables 1 2 3 2 1 0 3 2 3 Reference string is: 0,1,2,3,2,1,0,3,2,3 21

Why is this algorithm correct? Claim 1 The diagonal is always composed of 0 s. Claim 2 Right after page i is referenced, matrix row i has the maximum binary value (all other rows have at least one more 0 in addition to that in the i th column) Claim 3 For all distinct i, j, k, a reference to page k does not change the order between matrix lines i, j. A new referenced page gets line with maximum value and does not change previous order, so a simple induction proof works. 22

NFU - Not Frequently Used Approximating LRU in software In order to record frequently used pages add a counter field to each page frame but don't update on each memory reference, update every clock tick. At each clock tick, add the R bit to the counters (and zero the bit) Select page with lowest counter for replacement problem: remembers everything remedy (an aging algorithm): o shift-right the counter before adding the reference bit o add the reference bit at the left (most Significant Bit) 23

The aging algorithm 24

Differences between LRU and NFU If two pages have the same number of zeroes before the first 1, whom should we select? (E.g., processes 3, 5 after (e) ) If two pages have 0 counters, whom should we select? (counter has too few bits ) Therefore NFU is only an approximation of LRU. 25

Memory management, part 2: outline 26

Belady's anomaly Belady s anomaly The same algorithm may cause MORE page faults when given MORE page frames! Example: FIFO with reference string 012301401234 0 1 2 3 0 1 4 0 1 2 3 4 Youngest frame Oldest frame 0 1 2 3 0 1 4 4 4 2 3 3 0 1 2 3 0 1 1 1 4 2 2 0 1 2 3 0 0 0 1 4 4 P P P P P P P P P 9 page faults 0 1 2 3 0 1 4 0 1 2 3 4 Youngest frame 0 1 2 3 3 3 4 0 1 2 3 4 0 1 2 2 2 3 4 0 1 2 3 0 1 1 1 2 3 4 0 1 2 Oldest frame 0 0 0 1 2 3 4 0 1 P P P P P P P P P P 10 page faults! 27

Modeling page replacement algorithms Reference string sequence of page accesses made by process number of virtual pages n number of physical page frames m (we assume a single process) a page replacement algorithm can be represented by an array M of n rows 1 n m 28

Stack Algorithms M(m, r): for a fixed process P and reference string, the set of virtual pages in memory after the r th reference of process P, with memory size m. Definition: stack algorithms A page replacement algorithm is a stack algorithm if, for every P, m and reference string r, it holds that M(m, r) M(m+1, r) Stack algorithms do not suffer from Belady s anomaly Example: LRU, optimal replacement algorithm FIFO is not a stack algorithm Useful definition Distance string: distance of referenced page from top of stack 29

Computing page faults number C i is the number of times that i is in the distance string The number of page faults when we have m page frames is n F m = Ck + C k m 1 31

Back to page replacement algorithms: Taking multiprogramming into account Local vs. global algorithms Fair share is not the best policy (static!!) Allocate according to process size so, so Must be a minimum for running a process... A0 10 A0 A0 A1 7 A1 A1 A2 5 Timestamp A2 A2 A3 4 A3 A3 A4 6 A4 A4 A5 3 A6 A5 B0 9 B0 B0 B1 4 B1 B1 B2 6 B2 B2 B3 2 B3 A6 B4 5 B4 B4 B5 6 B5 B5 B6 12 B6 B6 C1 3 C1 C1 C2 5 C2 C2 C3 6 C3 C3 Local policy Global policy 32

Thrashing If a process does not have enough pages, the page-fault rate is very high. This leads to: o Low CPU utilization o `Chain reaction may cause OS to think it needs to increase the degree of multiprogramming o More processes added to the system Thrashing a process busy in swapping pages in and out 33

Thrashing Diagram thrashing degree of multiprogramming Increasing multiprogramming level initially increases CPU utilization Beyond some point, thrashing sets in and multiprogramming level must be decreased. 34

Process thrashing as function of page frames # 35

The impact of page-faults For a page-fault rate p, memory access time of 100 nanosecs and page-fault service time of 25 millisecs, the effective access time is (1-p) x 100 + p x 25,000,000 For p=0.001 the effective access time is still larger than 100 nanosecs by a factor of 250 For a goal of only 10% degradation in access time we need p = 0.0 000 004 Difficult to know how much frames to allocate to processes differ in size; structure; priority; 36

Memory management, part 2: outline 37

Working-Set Model: assumptions Locality of reference: during any phase of execution, a process is accessing a small number of pages the process' working set. Process migrates from one locality to the other; localities may overlap If assigned physical memory is smaller than working set we get thrashing The working set of a process is the set of pages used by the Δ most recent memory references (for some predetermined Δ) WS(t,Δ) denotes the working set at time t Optional: use pre-paging to bring a process' WS 38

Working set model Page reference string... 2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 1 3 2 3 4 4 4 3 4 4 4... =10 =10 t 1 t 2 WS(t 1,10) = {1,2,5,6,7} WS(t 2,10) = {3,4} Figure 9.16 Working-set model 39

Working-Set Function 40

Working-Set Model Δ working-set window a fixed number of page references o If Δ too small - will not encompass entire locality. o If Δ too large - will encompass several localities. o If Δ = will encompass entire program. D t = WS(t) total size of all working sets at time t o If D t > m thrashing o Policy: if D t > m, then suspend one of the processes. How can we estimate WS(t) without doing a memory update at every memory reference? 41

Working set algorithm using virtual time The working-set window cannot be based on memory references - too expensive One way to enlarge the time gap between updates is to use some clock tick triggering o Reference bits are updated by the hardware o Virtual time maintained for each process (in PCB entry) o Every timer tick, update process virtual time and virtual time of referenced paging-table entries; then the R bit is cleared Page p s age is the diff between the process virtual time and the time of p's last reference At PF time, the table is scanned and the entry with R=0 and the largest age is selected for eviction We use process virtual time (rather than global time) since it is more correlated to the process' working set 42

The Working Set Page Replacement Algorithm (2) The working set algorithm 43

The WSClock Page Replacement Algorithm 44

WSClock: global version Applies same idea for for pages held by all processes in memory Circling the clock, the algorithm uses pages reference bits and time of last reference [ref(frame)], and current virtual times of processes [T p ] Replace page when two conditions apply o reference bit is unset o T p - ref(frame) > 45

WSClock algorithm: example 3 processes p0, p1 and p2 Current (virtual) times of the 3 processes are Tp0 = 50; Tp1 = 70; Tp2 = 90 WSClock: replace when T p - ref(frame) > The minimal distance ( window size ) is = 20 The clock hand is currently pointing to page frame 4 page-frames: 0 1 2 3 4 5 6 7 8 9 10 ref. bit: 0 0 1 1 10 0 10 0 0 1 0 process ID: 0 1 0 1 2 1 0 0 1 2 2 last ref: 10 30 42 65 81 57 31 37 31 47 55 90 50 13 13 39 >20 46

Review of Page Replacement Algorithms 47

Memory management, part 2: outline 48

Operating system involvement in paging Upon new process creation o Determine initial size of program + data o Create a page table: allocate space, initialize o Allocate space in swap area o Initialize swap area o Update info about page-table and swap area in PTE Upon process scheduling for execution o Reset MMU and flush TLB o Select new process' page table as current o Optionally bring some of the processes pages to memory Upon process exit o Release page table, pages and swap area o Don't release shared pages if still referenced 49

Valid (In-memory) Bit A valid bit is associated with each page table entry (1 in-memory, 0 not-in-memory) Initially, valid-invalid is set to 0 on all entries During address translation, if in-memory bit in page table entry is 0 page fault Frame # Valid bit (in-memory) 1 1 1 1 0 0 0 Page table 50

Page Fault If there is ever a reference to a page, first reference will trap to OS page fault OS checks: o Illegal reference abort o Just not in memory Get empty frame (page replacement algorithm) Swap page into frame Update PTE, in-memory bit = 1 Restart instruction 51

Handling page faults The MMU sends a hardware interrupt, PC saved on stack Registers are saved, kernel is called Kernel discovers the virtual page that caused fault Kernel checks valid bit and verifies protection. If illegal access send signal to process. Otherwise o Check for a free page frame. If non available, apply a page replacement algorithm o If selected page frame is dirty write to disk (context switch). Mark frame as busy o When page frame is clean, read from disk (process still suspended) o When page arrives, update page table, mark frame as normal state o Upon process re-scheduling, re-execute faulting instruction, reload registers, continue execution 52

Instruction backup MOVE.L #6(A1), 2(A0) 16 bits 1000 1002 1004 Move 6 2 Block Move Above instruction makes 3 memory accesses. How does kernel know which caused the fault and where the instruction starts? If instruction does auto-increment/decrement, how do we know if it was already done? Some machines provide this info in hardware registers, Otherwise, OS sweats 53

Memory access with page faults P = probability of a page fault MA = memory access time PF = time to process page faults EMA Effective Memory Access = (1-p) x MA + P x PF where PF = page-fault interrupt service time + Read-in page time (maybe write-page too?) + Restart process time 54

Demand Paging Bring a page into memory only when it is needed o Less memory needed o Faster response o More users Page is needed reference it o Invalid reference abort o not-in-memory bring to memory 55

Locking Pages in Memory Virtual memory and I/O occasionally interact Process A issues read from I/O device into buffer o DMA transfer begins, process A blocks o process B starts executing o Process B causes a page fault o Page including buffer copied to by DMA may be chosen to be paged out Need to be able to lock page until I/O completes o Page exempted from being considered for replacement 56

Page Sharing Multiple processes may execute same code, we would like the text pages to be shared Paging out process A pages may cause many page faults for process B that shares them Looking up for evicted pages in all page tables is impossible Solution: maintain special data structures for shared pages Data pages may be shared also. E.g., when doing fork(), the copy-on-write mechanism. 57

Cleaning Policy Pre-paging more efficient than pure demand-paging Often implemented by paging daemon o periodically inspects state of memory. Cleans pages, possibly removing them When too few frames are free o selects pages to evict using a replacement algorithm It can use same circular list (WSclock) o as regular page replacement algorithm but with a different pointer 58

Handling the backing store Need to store non-resident pages on disk swap area Alternative 1 (static) : allocate a fixed chunk of the swap area to process upon creation, keep offset in PTE o Problem: memory requirements may change dynamically Alternative 2: Reserve separate swap areas for text, data, stack, allow each to extend over multiple chunks Alternative 3 (dynamic) : allocate swap space to a page when it is swapped out, de-allocate when back in. o Need to keep swap address for each page 59

Backing Store (a) Paging to static swap area (b) Backing up pages dynamically 60

Virtual Memory - Advantages Programs may use much smaller physical memory than their maximum requirements (much code or data is unused ) o Higher level of multiprogramming Programs can use much larger (virtual) memory o simplifies programming and enable using powerful software o swapping time is smaller External fragmentation is eliminated More flexible memory protection (but less so than segmentation ) 61

Virtual Memory - Disadvantages Special hardware for address translation - some instructions may require 5-6 address translations! Difficulties in restarting instructions (chip/microcode complexity) Complexity of OS! Overhead - a page-fault is an expensive operation in terms of both CPU and I/O overhead. Page-faults bad for real time Thrashing problem 62