Virtual Memory Today Virtual memory Page replacement algorithms

Slides:



Advertisements
Similar presentations
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Advertisements

Chapter 4 Memory Management Basic memory management Swapping
9.4 Page Replacement What if there is no free frame?
Page Replacement Algorithms
4.4 Page replacement algorithms
Page Replacement Algorithms
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Page-replacement policies On some standard algorithms for the management of resident virtual memory pages.
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Memory Management.
Page Replacement Algorithms
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 3 Memory Management Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
1 Memory Management Managing memory hierarchies. 2 Memory Management Ideally programmers want memory that is –large –fast –non volatile –transparent Memory.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Paging Algorithms Vivek Pai / Kai Li Princeton University.
Introduction to Systems Programming Lecture 7
Memory Management Virtual Memory Page replacement algorithms
CS 333 Introduction to Operating Systems Class 14 – Page Replacement Jonathan Walpole Computer Science Portland State University.
Virtual Memory Management B.Ramamurthy. Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Page Replacement Algorithms
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Demand Paging Virtual memory = indirect addressing + demand paging –Without demand paging, indirect addressing by itself is valuable because it reduces.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
CS 333 Introduction to Operating Systems Class 14 – Page Replacement Jonathan Walpole Computer Science Portland State University.
Virtual Memory.
O RERATıNG S YSTEM LESSON 10 MEMORY MANAGEMENT II 1.
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Memory Management Page replacement algorithms, segmentation Tanenbaum, ch. 3 p Silberschatz, ch. 8, 9 p
Memory Management From Chapter 4, Modern Operating Systems, Andrew S. Tanenbaum.
1 Memory Management 4.1 Basic memory management 4.2 Swapping 4.3 Virtual memory 4.4 Page replacement algorithms 4.5 Modeling page replacement algorithms.
1 First-in, First-out Remove the oldest page Determine the age based on the loading time, not on the time being referenced Old pages may be heavily used.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Paging.
“ Memory Management Function ” Presented By Lect. Rimple Bala GPC,Amritsar 1.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 9: Virtual-Memory Management.
Demand Paging Reference Reference on UNIX memory management
Virtual Memory Questions answered in this lecture: How to run process when not enough physical memory? When should a page be moved from disk to memory?
Virtual Memory The address used by a programmer will be called a virtual address or logical address. An address in main memory is called a physical address.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.
1 Memory Management Chapter Basic memory management 4.2 Swapping 4.3 Virtual memory 4.4 Page replacement algorithms 4.5 Modeling page replacement.
10.1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples.
1 Memory Management Adapted From Modern Operating Systems, Andrew S. Tanenbaum.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
COS 318: Operating Systems Virtual Memory Paging.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Virtual Memory What if program is bigger than available memory?
Memory Management (2).
ITEC 202 Operating Systems
Demand Paging Reference Reference on UNIX memory management
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9 Virtual Memory
Demand Paging Reference Reference on UNIX memory management
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Operating Systems CMPSC 473
Lecture 9: Caching and Demand-Paged Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory CSS503 Systems Programming
Virtual Memory.
Presentation transcript:

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

EECS 343 Operating Systems Northwestern University Virtual memory Handling processes >> than allocated memory Keep in memory only what’s needed Full address space does not need to be resident in memory Leave it on disk OS uses main memory as a cache Overlay approach Implemented by user Easy on the OS, hard on the programmer 20K 30K 10K Common routines Symbol tables Overlay driver Atlas (1962) – first machine w/ virtual memory hardware (U. Manchester) Overlay for a two-pass assembler: Pass 1 70KB Pass 2 80KB Symbol Table 20KB Common Routines 30KB Total 200KB Two overlays: 120 + 130KB Pass 2 80K Pass 1 70K EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Virtual memory Hide the complexity – let the OS do the job Virtual address space split into pages Physical memory split into page frames Page & page frames = size (512B … 64KB) Map pages into page frames Doing the translation – OS + MMU Atlas (1962) – first machine w/ virtual memory hardware (U. Manchester) EECS 343 Operating Systems Northwestern University

Pages, page frames and tables A simple example with 64KB virtual address space 4KB pages 32KB physical address space 16 pages and 8 page frames Try to access : MOV REG, 0 Virtual address 0 Page frame 2 Physical address 8192 MOV REG, 8192 Virtual address 8192 Page frame 6 Physical address 24576 MOV REG, 20500 Virtual address 20500 (20480 + 20) Page frame 3 Physical address 20+12288 EECS 343 Operating Systems Northwestern University

Since virtual memory >> physical memory Use a present/absent bit MMU checks – If not there, “page fault” to the OS (trap) OS picks a victim (?) … sends victim to disk … brings new one … updates page table MOVE REG, 32780 Virtual address 32780 Virtual page 8, byte 12 (32768+12) Page is unmapped – page fault! EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Details of the MMU work MMU with 16 4KB pages Page # (first 4 bits) index into page table If not there Page fault Else Output register + 12 bit offset → 15 bit physical address Page number Offset EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Page table entry Looking at the details Page frame number – the most important field Protection – 1 bit for R&W or R or 3 bits for RWX Present/absent bit Says whether or not the virtual address is used Modified (M): dirty bit Set when a write to the page has occurred Referenced (R): Has it being used? To ensure we are not reading from cache (D) Key for pages that map onto device registers rather than memory … Page frame number Prot. Present/absent R M D Caching disable EECS 343 Operating Systems Northwestern University

Page replacement algorithms OS uses main memory as (page) cache If only load when reference – demand paging Page fault – cache miss Need room for new page? Page replacement algorithm What’s your best candidate for removal? The one you will never touch again – duh! What do you do with victim page? Modified page must first be saved Unmodified one just overwritten Better not to choose an often used page It will probably need to be brought back in soon Try to avoid thrashing OS wastes most of the time moving pages around Fix the algorithm, swap out somebody, get more memory EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University How can any of this work?!?! Locality Temporal locality – location recently referenced tend to be referenced again soon Spatial locality – locations near recently referenced are more likely to be referenced soon Locality means paging could be infrequent Once you brought a page in, you’ll use it many times Some issues that may play against you Degree of locality of application Page replacement policy and application reference pattern Amount of physical memory and application footprint EECS 343 Operating Systems Northwestern University

Optimal algorithm (Belady’s algorithm) For now, assume a process pages against itself, using a fixed number of page frames Best page to replace – the one you’ll never need again Replace page needed at the farthest point in future Optimal but unrealizable Estimate by … Logging page use on previous runs of process Although impractical, useful for comparison Need room for this one Your ideal victim! 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 5 1 2 3 5 EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University FIFO algorithm Maintain a linked list of all pages – in order of arrival Victim is first page of list Maybe the oldest page will not be used again … Disadvantage But maybe it will – the fact is, you have no idea! Increasing physical memory might increase page faults (Belady’s anomaly, we’ll come back to this) A, B, C, D, A, B, E, A, B, C, D, E A B A C B A B D C D A C B A D E B A E B A E B A C E B D C E D C E EECS 343 Operating Systems Northwestern University

Least recently used (LRU) algorithm Pages used recently will used again soon Throw out page unused for longest time Idea: past experience is a decent predictor of future behavior LRU looks at the past, Belady’s wants to look at the future how is LRU different from FIFO? Must keep a linked list of pages Most recently used at front, least at rear Update this list every memory reference!! Too expensive in memory bandwidth, algorithm execution time, etc Alternatively keep counter in page table entry Choose page with lowest value counter Periodically zero the counter EECS 343 Operating Systems Northwestern University

A second HW LRU implementation Use a matrix – n page frames – n x n matrix Page k is reference Set all bits of row k to 1 Set all bits of column k to 0 Page of lowest row is LRU 0,1,2,3,2,1,0,3,2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 1 1 1 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 1 1 1 1 … 1,0,3,2 EECS 343 Operating Systems Northwestern University

Simulating LRU in software Not Frequently Used Software counter per page At clock interrupt – add R to counter for each page Problem - it never forgets! Better – Aging Push R from the left, drop bit on the right How is this not LRU? One bit per tick & a finite number of bits per counter 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 1 1 1 One bit per tick – two pages touched at 9 and 9.5 in 9-10 tick are the same Two pages with 8 bits are the same, even if one had just “dropped” a 1 from the 9th tick 1 1 1 1 1 1 1 EECS 343 Operating Systems Northwestern University

Not recently used (NRU) algorithm Each page has Reference and Modified bits Set when page is referenced, modified R bit set means recently referenced, so you must clear it every now and then Pages are classified NRU removes page at random from lowest numbered, non-empty class Easy to understand, relatively efficient to implement and sort-of OK performance R M Class Not referenced, not modified (0,0 → 0) 1 Not referenced, modified (0,1 → 1) Referenced, but not modified (1,0 → 2) Referenced and modified (1,1 → 3) How can this occur? EECS 343 Operating Systems Northwestern University

Second chance algorithm Simple modification of FIFO Avoid throwing out a heavily used page – look at the R bit Operation of second chance Pages sorted in FIFO order Page list if fault occurs at time 20, A has R bit set (time is loading time) Page Time R H 18 X G 15 F 14 E 12 D 8 C 7 B 3 A 1 Page Time R A 20 H 18 X G 15 F 14 E 12 D 8 C 7 B 3 Most recently loaded Oldest page EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Clock algorithm Quit moving pages around – move a pointer? Same as Second chance but for implementation When page fault Look at page pointed at by hand If R = 0, evict page If R = 1. clear R & move hand R: 0 A B R: 1 C D E F G I J K L R: 0 R: 0 EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Working set Demand paging Simplest strategy, load page when needed Most programs show locality of reference Over a short time, just a few common pages Working set Set of pages used by k most recent memory references ws(k, t) – size of working set at time t (k is WS window size) What bounds ws(k, t) as you increase k? How could you use this to reduce turnaround time? ws(k,t) k Clearly ws(ki,t) ≤ ws(kj,t) for i < j EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Working set algorithm Working set and page replacement Victim – a page not in the working set At each clock interrupt – scan the page table R = 1? Write Current Virtual Time (CVT) into Time of Last Use R = 0? CVT – Time of Last Use > Threshold ? out! else see if there’s someone and evict oldest (w/ R=0) If all are in the working set (all R = 1) random … R bit Information about a page 1213 Current virtual time 2204 2014 1 Time of last use 2020 1 2032 1 1620 EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University WSClock algorithm Problem with WS algorithm – Scans the whole table Combine clock & working set If R = 1, same as working set If R = 0, if age > T and page clean, out If dirty, schedule write and check next one If loop around, There’s 1+ write scheduled – you’ll have a clean page soon There’s none, pick any one Current virtual time 2204 1620 2084 1 2032 1 2003 1 2020 1 1980 1 2204 1 2014 1 2204 1 1213 R = 0 & 2204 – 1213 > T EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Belady's anomaly The more page frames the fewer page faults, right? FIFO with 3 page frames FIFO with 4 page frames All page frames initially empty 0 1 2 3 0 1 4 0 1 2 3 4 1 2 3 4 P P P P P P P 1 2 3 4 P P P P EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Belady's anomaly The more page frames the fewer page faults, right? FIFO with 3 page frames FIFO with 4 page frames 0 1 2 3 0 1 4 0 1 2 3 4 1 2 3 4 9 page faults P P P P P P P P P 1 2 3 4 10 page faults P P P P P P P P P P EECS 343 Operating Systems Northwestern University

Modeling page replacement algorithms Paging system can be characterized by Page replacement algorithm a reference string # page frames Abstract interpreter with Internal array, M, to keep track of memory state Size of (M) = # virtual pages, n Split in two parts Top m entries, for m pages frame The bottom part (n – m) for pages that have been referenced but eventually paged out Initially M is empty EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University An example using LRU Reference to a page (5) out of the blue box → page fault Pages in page frames Pages paged out to disk EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Stack algorithms Model works well with other algorithms. Particularly interesting … Stack algorithm: M(m,r) ⊆ M(m+1,r) Pages in memory with m pages frames and after r memory references Distance string – each page reference denoted by the distance from top of the stack where the page was located (if not yet referenced: ∞) Probability density function of two distance strings EECS 343 Operating Systems Northwestern University

Distance string & page faults Computation of page fault rate from distance string Ci – number of occurrences of i in distance string Fm – number of page faults with m frames Number of times 1 occur in distance string Ci Fi 1 2 3 4 5 6 7 ∞ 4 3 1 2 3 4 5 6 7 ∞ Number of page faults with 6 frames C7 + C∞ EECS 343 Operating Systems Northwestern University

Distance string & page faults Computation of page fault rate from distance string Ci – number of occurrences of i in distance string Fm – number of page faults with m frames Ci Fi 1 2 3 4 5 6 7 ∞ 4 3 2 1 8 1 2 3 4 5 6 7 ∞ 20 17 14 11 9 8 5s in distance string – one more page frame (4 to 5) means 9 instead of 11 page faults (C5=) 2 fewer: 9 EECS 343 Operating Systems Northwestern University

EECS 343 Operating Systems Northwestern University Next time … You now understand how things work, i.e. the mechanism … We’ll now consider design & implementation issues for paging systems Things you want/need to pay attention for good performance EECS 343 Operating Systems Northwestern University