CS 286: Memory Paging and Virtual Memory Virtual Memory – Concepts and Implementation Department of Computer Science Southern Illinois University Edwardsville Fall, 2017 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu Virtual_Memory/000
CS 286: Memory Paging and Virtual Memory The problem in memory system Throughput “An improvement rate of 60%/year in microprocessor performance, while the access time to DRAM has been improving at less than 10%/year” “The Gap between Processor and Memory Speeds” by Carlos Carvalho
CS 286: Memory Paging and Virtual Memory Memory Hierarchy CPU Core (ALU) Registers L1 Cache L2 Cache Main Memory Virtual Memory Disk Virtual_Memory/002
CS 286: Memory Paging and Virtual Memory Memory Hierarchy Fastest CPU registers Cache memory within a CPU (Level-1 Cache) Cache memory on a motherboard (Level-2 Cache) Main memory Hard drive Slowest MemoryBasics/006
CS 286: Memory Paging and Virtual Memory We have a trade-off problem Memory Hierarchy Fastest Highest Cost CPU registers Cache memory within a CPU (Level-1 Cache) Cache memory on a motherboard (Level-2 Cache) Main memory Hard drive Slowest Lowest Cost “Cost” = $$$ amount for each byte MemoryBasics/007
Level-1 Cache (Part 2 – Intel’s Pentium 4) CS 286: Memory Paging and Virtual Memory Level-1 Cache (Part 2 – Intel’s Pentium 4) Level-1 (L1) Cache MemoryBasics/009
“External Fragmentation” CS 286: Memory Paging and Virtual Memory Memory External Fragmentation 170MB Physical Memory A 40MB Memory F 30MB A lot of small free-memory spots, but none is big enough A 40MB A 40MB 10MB “External Fragmentation” (unused gap in memory between two programs) B 40MB E 30MB B 40MB E 30MB 10MB 10MB C 40MB C 40MB C 40MB D 40MB D 40MB D 40MB 10MB 10MB 10MB 10MB free 20MB free 30MB free G 30MB Virtual_Memory/001
Solution to these problems CS 286: Memory Paging and Virtual Memory Memory External Fragmentation Problems A lot of small free-memory spots, but none is big enough The programs must be stopped during compaction For multi-tasking computers that have a large number of processes, this is VERY expensive Tight compaction will not allow programs to grow in size This process is called “compaction” Solution to these problems Paging Virtual_Memory/001
CS 286: Memory Paging and Virtual Memory What is virtual memory? 1. It is a mechanism that expands memory space using hard drive 2. It is NOT memory CPU Main Memory 64M Bytes 00000000(16) FFFFFFFFFF(16) Memory Address Space Without virtual memory, memory address space is always same as the amount of main memory Virtual_Memory/003
CS 286: Memory Paging and Virtual Memory What is virtual memory (continued): Disk Virtual Memory Physical Memory Address Space Memory Address Space CPU 00000000(16) FFFFFFFFFF(16) Logical Memory Address Space 64MB Page Main Memory Virtual_Memory/004
CS 286: Memory Paging and Virtual Memory What is virtual memory (summary): 1. It is a mechanism that expands memory space by mapping some of the virtual memory address space to hard drive 2. This implies that a CPU can execute a program that is larger than actual main memory 3. Virtual memory is not memory. It’s just an array of pointers that map pages in virtual address space to the pages in physical address space Virtual_Memory/005
CS 286: Memory Paging and Virtual Memory Implementation of Virtual Memory: Disk CPU Page# Offset 1 2 3 4 5 6 7 8 9 10 Page numbers Valid Flag 1 2 3 4 5 6 3 1 Main Memory Virtual Memory Table Virtual_Memory/006
CS 286: Memory Paging and Virtual Memory What is “offset”? Target memory address CPU Page# Offset Offset 1 2 3 4 5 6 7 8 9 10 Page numbers Valid Flag 1 2 3 4 5 6 3 1 Main Memory Virtual Memory Table Virtual_Memory/006
CS 286: Memory Paging and Virtual Memory If the requested page is in the main memory: 1. Fetch the page# field from the requested address 2. Check the valid flag in the virtual memory table 3. If the valid flag is one (valid), use the page number stored in this slot as the main memory page# 4. Use the offset field as the offset from the beginning of the page Virtual_Memory/007
CS 286: Memory Paging and Virtual Memory Implementation of Virtual Memory: Disk CPU Page# Offset #2 #7 1 2 3 4 5 6 7 8 9 10 Valid Flag 3 1 1 2 3 4 5 6 3 1 This operation is called “page fault” Main Memory Virtual Memory Table Virtual_Memory/008
CS 286: Memory Paging and Virtual Memory If the requested page is not in the main memory: 1. Fetch the page# field from the requested address 2. Check the valid flag in the virtual memory table Called “page fault” 3. If the valid flag of the requested page is zero (invalid), save the destination page in the main memory to hard drive 4. Change the valid flag of the virtual memory that was previously using the memory page to zero (invalid) 5. Load the requested page from HDD to main memory 6. Change the valid field of the requested page to one (valid) Virtual_Memory/009
CS 286: Memory Paging and Virtual Memory Dirty Flag Disk CPU Page# Offset 1 2 3 4 5 6 7 8 9 10 Dirty Flag 3 1 1 2 3 4 5 6 3 Main Memory Valid Flag Virtual Memory Table Virtual_Memory/010
CS 286: Memory Paging and Virtual Memory Dirty Flag When a new page is loaded to a physical memory page, set its “dirty flag” to ‘0’. When a physical memory page is updated, set its “dirty flag” to ‘1’. When a physical memory page is being replaced by another virtual memory page, perform one of the followings: If the page’s (the one currently in the physical memory) dirty flag is ‘0 (clean)’: Let a new page to replace the previous one in the physical memory (b) If the page’s (the one currently in the physical memory) dirty flag is ‘1 (dirty)’: Save the current page back to the hard drive, then let a new page to replace the previous one in the physical memory Virtual_Memory/011
CS 286: Memory Paging and Virtual Memory Implementation of Virtual Memory (Continued): Disk CPU CPU Logical Address Page# Offset Translation Lookaside Buffer (TLB) Main Memory 1 2 3 4 5 6 High speed cache within CPU Physical Address Virtual_Memory/012 Virtual Memory Table
CS 286: Memory Paging and Virtual Memory Implementation of Virtual Memory with L2 Cache: Disk CPU Page# Offset Memory Controller Translation Lookaside Buffer Main Memory 1 2 3 4 5 6 CPU L2 Cache Virtual_Memory/013 Virtual Memory Table