Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables

Slides:



Advertisements
Similar presentations
Paging 1 CS502 Spring 2006 Paging CS-502 Operating Systems.
Advertisements

May 7, A Real Problem  What if you wanted to run a program that needs more memory than you have?
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.
1 A Real Problem  What if you wanted to run a program that needs more memory than you have?
CS 153 Design of Operating Systems Spring 2015
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
Computer ArchitectureFall 2008 © November 10, 2007 Nael Abu-Ghazaleh Lecture 23 Virtual.
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
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.
Translation Buffers (TLB’s)
©UCB CS 162 Ch 7: Virtual Memory LECTURE 13 Instructor: L.N. Bhuyan
©UCB CS 161 Ch 7: Memory Hierarchy LECTURE 24 Instructor: L.N. Bhuyan
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Cosc 3P92 Week 9 & 10 Lecture slides
Lecture 19: Virtual Memory
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
IT253: Computer Organization
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
Virtual Memory Expanding Memory Multiple Concurrent Processes.
© 2004, D. J. Foreman 1 Virtual Memory. © 2004, D. J. Foreman 2 Objectives  Avoid copy/restore entire address space  Avoid unusable holes in memory.
Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.
CS399 New Beginnings Jonathan Walpole. Virtual Memory (1)
The Three C’s of Misses 7.5 Compulsory Misses The first time a memory location is accessed, it is always a miss Also known as cold-start misses Only way.
Ch7d- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Individual Space Every process wants its own space Ideally, it would like.
Virtual Memory. Virtual Memory: Topics Why virtual memory? Virtual to physical address translation Page Table Translation Lookaside Buffer (TLB)
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
1 Some Real Problem  What if a program needs more memory than the machine has? —even if individual programs fit in memory, how can we run multiple programs?
Virtual Memory Additional Slides Slide Source: Topics Address translation Accelerating translation with TLBs class12.ppt.
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Multilevel Caches Microprocessors are getting faster and including a small high speed cache on the same chip.
Demand Paging Reference Reference on UNIX memory management
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Virtual Memory Review Goal: give illusion of a large memory Allow many processes to share single memory Strategy Break physical memory up into blocks (pages)
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
CS203 – Advanced Computer Architecture Virtual Memory.
COS 318: Operating Systems Virtual Memory Paging.
CS161 – Design and Architecture of Computer
Translation Lookaside Buffer
Lecture 11 Virtual Memory
Virtual Memory Chapter 7.4.
ECE232: Hardware Organization and Design
CS161 – Design and Architecture of Computer
Virtual Memory User memory model so far:
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.
Some Real Problem What if a program needs more memory than the machine has? even if individual programs fit in memory, how can we run multiple programs?
Virtual Memory © 2004, D. J. Foreman.
CS510 Operating System Foundations
Lecture 28: Virtual Memory-Address Translation
EECE.4810/EECE.5730 Operating Systems
Page Replacement.
Demand Paged Virtual Memory
Translation Lookaside Buffer
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
Translation Buffers (TLB’s)
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Translation Buffers (TLB’s)
CSC3050 – Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Paging and Segmentation
Virtual Memory: Working Sets
Virtual Memory © 2004, D. J. Foreman.
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.
Review What are the advantages/disadvantages of pages versus segments?
Lecture Topics: 11/20 HW 7 What happens on a memory reference Traps
Presentation transcript:

Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables paged page tables inverted page tables TLBs Virtual memory

Virtual Addresses Programs use virtual addresses which don't correlate to physical addresses CPU translates all memory references from virtual addresses to physical addresses OS still uses physical addresses CPU Translation Box virtual address physical address Main Memory User mode: CPU Translation Box Main Memory physical address Kernel mode:

Paging 1 2 3 4 5 6 7 8 9 10 11 12 13 Translation Word IE5 Virtual Page # Physical Page # 0x0000 0x6000 0x4000 0xE000 Divide a process's virtual address space into fixed-size chunks (called pages) Divide physical memory into pages of the same size Any virtual page can be located at any physical page Translation box converts from virtual pages physical pages

Page Tables Page Table Virtual Page # Physical page # virtual address VPN Offset physical address PPN Process ID A page table maps virtual page numbers to physical page numbers Lots of different types of page tables arrays, lists, hashes

Flat Page Table A flat page table uses the VPN to index into an array PPN Page Table 5 6 2 13 10 9 1 3 4 7 8 11 12 Offset 100 Memory A flat page table uses the VPN to index into an array What's the problem? (Hint: how many entries are in the table?)

Flat Page Table Evaluation Very simple to implement Flat page tables don't work for sparse address spaces code starts at 0x00400000 stack starts at 0x7FFFFFFF With 8K pages, this requires 1MB of memory per page table 1MB per process must be kept in main memory (can't be put on disk) 64-bit addresses are a nightmare (4 TB)

Multi-level Page Tables Use multiple levels of page tables each page table points to another page table the last page table points to the PPN The VPN is divided into Index into level 1 page Index into level 2 page …

Multi-level Page Tables 1 2 3 4 5 6 7 8 9 10 11 12 13 L1 Page Table NO VPN Offset 100 Memory L2Page Tables

Multi-Level Evaluation Only allocate as many page tables as we need--works with the sparse address spaces Only the top page table must be in pinned in physical memory Each page table usually fills exactly 1 page so it can be easily moved to/from disk Requires multiple physical memory references for each virtual memory reference

Inverted Page Tables Inverted page tables hash the VPN to get the PPN Offset Inverted Page Table Memory Inverted page tables hash the VPN to get the PPN Requires O(1) lookup Storage is proportional to number of physical pages being used not the size of the address space

Translation Problem Each virtual address reference requires multiple accesses to physical memory Physical memory is 50 times slower than accessing the on-chip cache If the VPN->PPN translation was made for each reference, the computer would run as fast as a Commodore-64 Fortunately, locality allows us to cache translations on chip

Translation Lookaside Buffer The translation lookaside buffer (TLB) is a small on-chip cache of VPN->PPN translations In common case, translation is in the TLB and no need to go through page tables Common TLB parameters 64 entries fully associative separate data and instruction TLBs (why?) Virtual Page # Physical Page # Control Info 11 6 valid, read/write 200 13 valid, read only -- invalid 14

TLB On a TLB miss, the CPU asks the OS to add the translation to the TLB OS replacement policies are usually approximations of LRU On a context switch all TLB entries are invalidated because the next process has different translations A TLB usually has a high hit rate 99-99.9% so virtual address translation doesn't cost anything

Virtual Memory Virtual memory spills unused memory to disk abstraction: infinite memory reality: finite physical memory In computer science, virtual means slow think Java Virtual Machine VM was invented when memory was small and expensive needed VM because memories were too small 1965-75 CPU=1 MIPS, 1MB=$1000, disk=30ms Now cost of accessing is much more expensive 2000 CPU=1000 MIPS, 1MB=$1, disk=10ms VM is still convenient for massive multitasking, but few programs need more than 128MB

Virtual Memory VPN 1 2 3 4 5 6 7 8 9 10 memory page file Simple idea: page table entry can point to a PPN or a location on disk (offset into page file) A page on disk is swapped back in when it is referenced page fault

Page Fault Example VPN 1 2 3 4 5 6 7 8 9 10 memory page file VPN 1 2 3 1 2 3 4 5 6 7 8 9 10 memory page file VPN 1 2 3 4 5 6 7 8 9 10 memory page file VPN 1 2 3 4 5 6 7 8 9 10 memory page file Reference to VPN 10 causes a page fault because it is on disk. VPN 5 has not been used recently. Write it to the page file. Read VPN 10 from the page file into physical memory.

Virtual Memory vs. Caches Physical memory is a cache of the page file Many of the same concepts we learned with caches apply to virtual memory both work because of locality dirty bits prevent pages from always being written back Some concepts don't apply VM is usually fully associative with complex replacement algorithms because a page fault is so expensive

Replacement Algorithms How do we decide which virtual page to replace in memory? FIFO--throw out the oldest page very bad because throws out frequently used pages RANDOM--pick a random page works better than you would guess, but not good enough MIN--pick the page that won't be used for the longest time provably optimal, but impossible because requires knowledge of the future LRU--approximation of MIN, still impractical CLOCK--practical approximation of LRU

Perfect LRU Perfect LRU timestamp each page when it is referenced on page fault, find oldest page too much work per memory reference

LRU Approximation: Clock Clock algorithm arrange physical pages in a circle, with a clock hand keep a use bit per physical page bit is set on each reference bit isn't set  page not used in a long time On page fault Advance clock hand to next page & check use bit If used, clear the bit and go to next page If not used, replace this page

Clock Example PPN 0 has been used; clear and advance 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 PPN 0 has been used; clear and advance PPN 1 has been used; clear and advance PPN 2 has been used; clear and advance 1 2 3 4 5 6 7 1 2 3 4 5 6 7 PPN 3 has been not been used; replace and set use bit

Clock Questions Will Clock always find a page to replace? What does it mean if the hand is moving slowly? What does it mean if the hand is moving quickly?

Thrashing Thrashing occurs when pages are tossed out, but are still needed listen to the harddrive crunch Example: a program touches 50 pages often but only 40 physical pages What happens to performance? enough memory 2 ns/ref (most refs hit in cache) not enough memory 2 ms/ref (page faults every few instructions) Very common with shared machines thrashing jobs/sec # users

Thrashing Solutions If one job causes thrashing rewrite program to have better locality If multiple jobs cause thrashing only run processes that fit in memory Big red button

Working Set The working set of a process is the set of pages that it is actually using usually much smaller than the amount of memory that is allocated As long as a process's working set fits in memory it won't thrash Formally: the set of pages a job has referenced in the last T seconds How do we pick T? too big => could run more programs too small => thrashing

What happens on a memory reference? An instruction refers to memory location X: Is X's VPN in the TLB? Yes: get data from cache or memory. Done. (Often don't look in TLB if data is in the L1 cache) Trap to OS to load X's VPN into the cache OS: Is X's VP located in physical memory? Yes: replace TLB entry with X's VPN. Return control to CPU, which restarts the instruction. Done. Must load X's VP from disk pick a page to replace, write it back to disk if dirty load X's VP from disk into physical memory Replace the TLB entry with X's VPN. Return control to CPU, which restarts the instruction.

What is a Trap? http://www.cs.wayne.edu/~tom/guide/os2.html http://www.cs.nyu.edu/courses/fall99/G22.2250-001/class-notes.html