Page Allocation and Replacement

Slides:



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

Caching and Virtual Memory. Main Points Cache concept – Hardware vs. software caches When caches work and when they don’t – Spatial/temporal locality.
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
Lecture 34: Chapter 5 Today’s topic –Virtual Memories 1.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
1 Virtual Memory vs. Physical Memory So far, all of a job’s virtual address space must be in physical memory However, many parts of programs are never.
OS Spring’03 Introduction Operating Systems Spring 2003.
Translation Buffers (TLB’s)
Virtual Memory and Paging J. Nelson Amaral. Large Data Sets Size of address space: – 32-bit machines: 2 32 = 4 GB – 64-bit machines: 2 64 = a huge number.
1  1998 Morgan Kaufmann Publishers Chapter Seven Large and Fast: Exploiting Memory Hierarchy (Part II)
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Virtual Memory.
Caching and Virtual Memory. Main Points Cache concept – Hardware vs. software caches When caches work and when they don’t – Spatial/temporal locality.
Computer Architecture Lecture 28 Fasih ur Rehman.
Lecture 19: Virtual Memory
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
July 30, 2001Systems Architecture II1 Systems Architecture II (CS ) Lecture 8: Exploiting Memory Hierarchy: Virtual Memory * Jeremy R. Johnson Monday.
IT253: Computer Organization
Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables
CS 162 Discussion Section Week 6 3/6 – 3/7. Today’s Section ●Administrivia (5 min) ●Quiz (5 min) ●Lecture Review (15 min) ●Worksheet and Discussion (25.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Lecture Topics: 11/24 Sharing Pages Demand Paging (and alternative) Page Replacement –optimal algorithm –implementable algorithms.
Multilevel Caches Microprocessors are getting faster and including a small high speed cache on the same chip.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement March 4, 2013 Anthony D. Joseph
1 Contents Memory types & memory hierarchy Virtual memory (VM) Page replacement algorithms in case of VM.
COS 318: Operating Systems Virtual Memory Paging.
Virtual Memory Chapter 8.
CS161 – Design and Architecture of Computer
8 July 2015 Charles Reiss
CS161 – Design and Architecture of Computer
CS352H: Computer Systems Architecture
Modeling Page Replacement Algorithms
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
Day 08 Processes.
Day 09 Processes.
Morgan Kaufmann Publishers
Morgan Kaufmann Publishers
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Chapter 9: Virtual-Memory Management
Virtual Memory 4 classes to go! Today: Virtual Memory.
March 3, 2014 Anthony D. Joseph CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement.
Demand Paged Virtual Memory
Modeling Page Replacement Algorithms
Anthony D. Joseph and Ion Stoica
Architectural Support for OS
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
Translation Buffers (TLB’s)
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Contents Memory types & memory hierarchy Virtual memory (VM)
CSE451 Virtual Memory Paging Autumn 2002
Translation Buffers (TLB’s)
CSC3050 – Computer Architecture
Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
CSE 471 Autumn 1998 Virtual memory
Lecture 9: Caching and Demand-Paged Virtual Memory
Translation Buffers (TLBs)
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
COMP755 Advanced Operating Systems
Review What are the advantages/disadvantages of pages versus segments?
Presentation transcript:

Page Allocation and Replacement

Demand Paging Modern programs require a lot of physical memory Memory per system growing faster than 25%-30%/year But they don’t use all their memory all of the time 90-10 rule: programs spend 90% of their time in 10% of their code Wasteful to require all of user’s code to be in memory Solution: use main memory as cache for disk On-Chip Cache Control Datapath Secondary Storage (Disk) Processor Main Memory (DRAM) Second Level (SRAM) Tertiary (Tape) Caching

Demand Paging Mechanisms PTE helps us implement demand paging Valid  Page in memory, PTE points at physical page Not Valid  Page not in memory; use info in PTE to find it on disk when necessary Suppose user references page with invalid PTE? Memory Management Unit (MMU) traps to OS Resulting trap is a “Page Fault” What does OS do on a Page Fault?: Choose an old page to replace If old page modified (“D=1”), write contents back to disk Change its PTE and any cached TLB to be invalid Load new page into memory from disk Update page table entry, invalidate TLB for new entry Continue thread from original faulting location TLB for new page will be loaded when thread continued! While pulling pages off disk for one process, OS runs another process from ready queue Suspended process sits on wait queue Cache

What Factors Lead to Misses? Compulsory Misses: Pages that have never been paged into memory before How might we remove these misses? Prefetching: loading them into memory before needed Need to predict future somehow! More later. Capacity Misses: Not enough memory. Must somehow increase size. Can we do this? One option: Increase amount of DRAM (not quick fix!) Another option: If multiple processes in memory: adjust percentage of memory allocated to each one! Conflict Misses: Technically, conflict misses don’t exist in virtual memory, since it is a “fully-associative” cache Policy Misses: Caused when pages were in memory, but kicked out prematurely because of the replacement policy How to fix? Better replacement policy

Replacement Policies (Con’t) LRU (Least Recently Used): Replace page that hasn’t been used for the longest time Programs have locality, so if something not used for a while, unlikely to be used in the near future. Seems like LRU should be a good approximation to MIN. How to implement LRU? Use a list! On each use, remove page from list and place at head LRU page is at tail Problems with this scheme for paging? List operations complex Many instructions for each hardware access In practice, people approximate LRU (more later) Page 6 Page 7 Page 1 Page 2 Head Tail (LRU)

Example: FIFO Suppose we have 3 page frames, 4 virtual pages, and following reference stream: A B C A B D A D B C B Consider FIFO Page replacement: FIFO: 7 faults. When referencing D, replacing A is bad choice, since need A again right away 3 2 1 Ref: Page: A B C A B D A D B C B A B C D A B C

Example: MIN Suppose we have the same reference stream: A B C A B D A D B C B Consider MIN Page replacement: MIN: 5 faults Look for page not referenced farthest in future. What will LRU do? Same decisions as MIN here, but won’t always be true! 3 2 1 Ref: Page: A B C A B D A D B C B A B C D C

Implementing LRU & Second Chance Perfect: Timestamp page on each reference Keep list of pages ordered by time of reference Too expensive to implement in reality for many reasons Second Chance Algorithm: Approximate LRU Replace an old page, not the oldest page FIFO with “use” bit Details A “use” bit per physical page On page fault check page at head of queue If use bit=1  clear bit, and move page at tail (give the page second chance!) If use bit=0  replace page Moving pages to tail still complex

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives first loaded page last loaded page B u:0 A u:1 D u:0 C u:0

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives first loaded page last loaded page B u:0 A u:1 D u:0 C u:0

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives first loaded page last loaded page A u:1 D u:0 C u:0 F u:0

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives Access page D first loaded page last loaded page A u:1 D u:1 C u:0 F u:0

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives Access page D Page E arrives first loaded page last loaded page A u:1 D u:1 C u:0 F u:0

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives Access page D Page E arrives first loaded page last loaded page D u:1 C u:0 F u:0 A u:0

Second Chance Illustration Max page table size 4 Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives Access page D Page E arrives first loaded page last loaded page C u:0 F u:0 A u:0 E u:0 D u:0

Clock Algorithm Clock Algorithm: more efficient implementation of second chance algorithm Arrange physical pages in circle with single clock hand Details: On page fault: Check use bit: 1used recently; clear and leave it alone 0selected candidate for replacement Advance clock hand (not real time) Will always find a page or loop forever?

Clock Replacement Illustration Max page table size 4 Invariant: point at oldest page Page B arrives B u:0

Clock Replacement Illustration Max page table size 4 Invariant: point at oldest page Page B arrives Page A arrives Access page A B u:0 A u:0

Clock Replacement Illustration Max page table size 4 Invariant: point at oldest page Page B arrives Page A arrives Access page A Page D arrives B u:0 A u:1 D u:0

Clock Replacement Illustration Max page table size 4 Invariant: point at oldest page Page B arrives Page A arrives Access page A Page D arrives Page C arrives B u:0 C u:0 A u:1 D u:0

Clock Replacement Illustration Max page table size 4 Invariant: point at oldest page Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives B u:0 F u:0 C u:0 A u:1 D u:0

Clock Replacement Illustration Max page table size 4 Invariant: point at oldest page Page B arrives Page A arrives Access page A Page D arrives Page C arrives Page F arrives Access page D Page E arrives F u:0 C u:0 E u:0 A u:0 A u:1 D u:1 D u:0

Kernel/User

Dual-Mode Operation Can an application modify its own translation maps or PTE bits? If it could, could get access to all of physical memory Has to be restricted somehow To assist with protection, hardware provides at least two modes (Dual-Mode Operation): “Kernel” mode (or “supervisor” or “protected”) “User” mode (Normal program mode) Mode set with bits in special control register only accessible in kernel-mode Intel processors actually have four “rings” of protection: PL (Privilege Level) from 0 – 3 PL0 has full access, PL3 has least Typical OS kernels on Intel processors only use PL0 (“kernel”) and PL3 (“user”)

How to get from KernelUser What does the kernel do to create a new user process? Allocate and initialize process control block Read program off disk and store in memory Allocate and initialize translation map Point at code in memory so program can execute Possibly point at statically initialized data Run Program: Set machine registers Set hardware pointer to translation table Set processor status word for user mode Jump to start of program How does kernel switch between processes (we learned about this!) ? Same saving/restoring of registers as before Save/restore hardware pointer to translation map

UserKernel (System Call) Can’t let inmate (user) get out of padded cell on own Would defeat purpose of protection! So, how does the user program get back into kernel? System call: Voluntary procedure call into kernel Hardware for controlled UserKernel transition Can any kernel routine be called? No! Only specific ones System call ID encoded into system call instruction Index forces well-defined interface with kernel I/O: open, close, read, write, lseek Files: delete, mkdir, rmdir, chown Process: fork, exit, join Network: socket create, select

UserKernel (Exceptions: Traps and Interrupts) System call instr. causes a synchronous exception (or “trap”) In fact, often called a software “trap” instruction Other sources of Synchronous Exceptions: Divide by zero, Illegal instruction, Bus error (bad address, e.g. unaligned access) Segmentation Fault (address out of range) Page Fault Interrupts are Asynchronous Exceptions Examples: timer, disk ready, network, etc…. Interrupts can be disabled, traps cannot! SUMMARY – On system call, exception, or interrupt: Hardware enters kernel mode with interrupts disabled Saves PC, then jumps to appropriate handler in kernel For some processors (x86), processor also saves registers, changes stack, etc.