CSE 153 Design of Operating Systems Spring 2016 Lecture 10: Memory Management (1) Some slides from Dave O’Hallaron.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 The Memory Hierarchy Today Storage technologies and trends Locality of reference Caching in the memory hierarchy Next.
Advertisements

Carnegie Mellon 1 The Memory Hierarchy / : Introduction to Computer Systems 10 th Lecture, Feb 12, 2015 Instructors: Seth Copen Goldstein,
Carnegie Mellon 1 The Memory Hierarchy : Introduction to Computer Systems 9 th Lecture, Sep. 21, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Cache Memory Organization
Carnegie Mellon 1 The Memory Hierarchy : Introduction to Computer Systems 9th Lecture, Sep. 21, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
CS 153 Design of Operating Systems Spring 2015
CS 153 Design of Operating Systems Spring 2015
Memory Organization.
Registers  Flip-flops are available in a variety of configurations. A simple one with two independent D flip-flops with clear and preset signals is illustrated.
Computer ArchitectureFall 2007 © November 7th, 2007 Majd F. Sakr CS-447– Computer Architecture.
The Memory Hierarchy Apr 26, 2006 Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy CS213.
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy CS213.
The Memory Hierarchy.
Systems I Locality and Caching
The Memory Hierarchy Topics Storage technologies Capacity and latency trends The hierarchy Systems I.
Memory Management CSE451 Andrew Whitaker. Big Picture Up till now, we’ve focused on how multiple programs share the CPU Now: how do multiple programs.
ECE Dept., University of Toronto
1 Seoul National University The Memory Hierarchy.
1 Hardware and The Memory Hierarchy Andrew Case Slides adapted from Jinjang Li, Mohamed Zahran, Randy Bryant and Dave O’Hallaron.
Operating Systems ECE344 Ding Yuan Memory Management Lecture 7: Memory Management.
Memory Hierarchy 1 Computer Organization II © CS:APP & McQuain Cache Memory and Performance Many of the following slides are taken with.
Lecture 12: The Memory Hierarchy EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014,
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition The Memory Hierarchy : Introduction to Computer.
1 CSCI 2510 Computer Organization Memory System I Organization.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
University of Washington Memory and Caches I The Hardware/Software Interface CSE351 Winter 2013.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
CS 105 Tour of the Black Holes of Computing
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy.
Carnegie Mellon 1 The Memory Hierarchy /18-243: Introduction to Computer Systems 11 th Lecture, 09 June, 2011 Instructors: Gregory Kesden.
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy.
Carnegie Mellon 1 The Memory Hierarchy / : Introduction to Computer Systems 10 th Lecture, Sep. 29, 2011 Instructors: Dave O’Hallaron, Greg.
Introduction: Memory Management 2 Ideally programmers want memory that is large fast non volatile Memory hierarchy small amount of fast, expensive memory.
Memory hierarchy. – 2 – Memory Operating system and CPU memory management unit gives each process the “illusion” of a uniform, dedicated memory space.
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy.
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy CS 105 Tour of the Black Holes of Computing.
The Memory Hierarchy Feb. 20, 2003 Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy class12.ppt “The.
1 Memory Hierarchy ( Ⅲ ). 2 Outline The memory hierarchy Cache memories Suggested Reading: 6.3, 6.4.
University of Washington Today Midterm topics end here. HW 2 is due Wednesday: great midterm review. Lab 3 is posted. Due next Wednesday (July 31)  Time.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition The Memory Hierarchy : Introduction to Computer.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition The Memory Hierarchy MCS284: Computer Organization.
Memory Hierarchy 1 Computer Organization II © CS:APP & McQuain Cache Memory and Performance Many of the following slides are taken with.
1 Memory Hierarchy (I). 2 Outline Random-Access Memory (RAM) Nonvolatile Memory Disk Storage Suggested Reading: 6.1.
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy mem1.ppt CS 105 Tour of the Black Holes.
The Memory Hierarchy January 21, 2002 Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy Reading: 6.1 – 6.4 Problems:
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy mem1.ppt CS 105 Tour of the Black Holes.
Carnegie Mellon 1 The Memory Hierarchy / : Introduction to Computer Systems 10 th Lecture, Sep. 25, 2014 Instructors: Greg Ganger, Greg Kesden,
Memory Management memory hierarchy programs exhibit locality of reference - non-uniform reference patterns temporal locality - a program that references.
The Memory Hierarchy Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy mem1.ppt CS 105 Tour of the Black Holes.
The Memory Hierarchy Oct 4, 2001 Topics Storage technologies and trends Locality of reference Caching in the memory hierarchy class12.ppt “The course.
Advanced Computer Architecture CS 704 Advanced Computer Architecture Lecture 26 Memory Hierarchy Design (Concept of Caching and Principle of Locality)
1 Binghamton University The Memory Hierarchy CS220: Computer Systems II.
CSE 351 Section 9 3/1/12.
CSE 120 Principles of Operating
CS703 - Advanced Operating Systems
The Memory Hierarchy Topics Storage technologies and trends
Today How’s Lab 3 going? HW 3 will be out today
CS 105 Tour of the Black Holes of Computing
Memory hierarchy.
ReCap Random-Access Memory (RAM) Nonvolatile Memory
Cache Memories September 30, 2008
CS 105 Tour of the Black Holes of Computing
Memory Hierarchy (I).
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 153 Design of Operating Systems Winter 2018
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Cache Memory and Performance
Presentation transcript:

CSE 153 Design of Operating Systems Spring 2016 Lecture 10: Memory Management (1) Some slides from Dave O’Hallaron

Announcements l Project Redux u Understanding why so many did not finish the project l Get started on project 2 ASAP l Exam Redux: u How was it? l Please ask questions if anything is unclear u REMINDER: 4% for class participation/bonuses u Come to office hours CSE 153 – Lecture 10 – Memory Management2

OS Abstractions 3 Operating System Hardware Applications CPUDiskRAM ProcessFile systemVirtual memory CSE 153 – Lecture 10 – Memory Management

Today l Memory/storage technologies and trends l Locality of reference l Caching in the memory hierarchy l Virtual memory and memory sharing

Random-Access Memory (RAM) l Key features u RAM is traditionally packaged as a chip. u Basic storage unit is normally a cell (one bit per cell). u Multiple RAM chips form a memory. l Static RAM (SRAM) u Each cell stores a bit with a four or six-transistor circuit. u Retains value indefinitely, as long as it is kept powered. u Relatively insensitive to electrical noise (EMI), radiation, etc. u Faster and more expensive than DRAM. l Dynamic RAM (DRAM) u Each cell stores bit with a capacitor. One transistor is used for access u Value must be refreshed every ms. u More sensitive to disturbances (EMI, radiation,…) than SRAM. u Slower and cheaper than SRAM.

SRAM vs DRAM Summary Trans.AccessNeedsNeeds per bit timerefresh?EDC?CostApplications SRAM4 or 61XNoMaybe100xCache memories DRAM110XYesYes1XMain memories, frame buffers

Conventional DRAM Organization l d x w DRAM: u dw total bits organized as d supercells of size w bits cols rows Internal row buffer 16 x 8 DRAM chip addr data supercell (2,1) 2 bits / 8 bits / Memory controller (to/from CPU)

Reading DRAM Supercell (2,1) Step 1(a): Row access strobe (RAS) selects row 2. Step 1(b): Row 2 copied from DRAM array to row buffer. Cols Rows RAS = Internal row buffer 16 x 8 DRAM chip 3 addr data 2/2/ 8/8/ Memory controller

Reading DRAM Supercell (2,1) Step 2(a): Column access strobe (CAS) selects column 1. Step 2(b): Supercell (2,1) copied from buffer to data lines, and eventually back to the CPU. Cols Rows Internal row buffer 16 x 8 DRAM chip CAS = 1 addr data 2/2/ 8/8/ Memory controller supercell (2,1) supercell (2,1) To CPU

Memory Modules : supercell (i,j) 64 MB memory module consisting of eight 8Mx8 DRAMs addr (row = i, col = j) Memory controller DRAM 7 DRAM bit doubleword at main memory address A bits 0-7 bits 8-15 bits bits bits bits bits bits bit doubleword

Enhanced DRAMs l Basic DRAM cell has not changed since its invention in u Commercialized by Intel in l DRAM cores with better interface logic and faster I/O : u Synchronous DRAM (SDRAM) »Uses a conventional clock signal instead of asynchronous control »Allows reuse of the row addresses (e.g., RAS, CAS, CAS, CAS) u Double data-rate synchronous DRAM (DDR SDRAM) »Double edge clocking sends two bits per cycle per pin »Different types distinguished by size of small prefetch buffer: n DDR (2 bits), DDR2 (4 bits), DDR4 (8 bits) »By 2010, standard for most server and desktop systems »Intel Core i7 supports only DDR3 SDRAM

Nonvolatile Memories l DRAM and SRAM are volatile – lose info without power l Nonvolatile memories (NVMs) retain value u Read-only memory (ROM): programmed during production u Programmable ROM (PROM): can be programmed once u Eraseable PROM (EPROM): can be bulk erased (UV, X-Ray) u Electrically eraseable PROM (EEPROM): electronic erase u Flash memory: EEPROMs with partial (sector) erase capability »Wears out after about 100,000 erasings. u Phase Change Memories (PCMs): also wear out l Uses for NVMs u Firmware programs stored in a ROM (BIOS, controllers for disks, network cards, graphics accelerators, security subsystems,…) u Solid state disks (replace rotating disks in thumb drives, smart phones, mp3 players, tablets, laptops,…) u Getting better -- many expect Universal memory to come

Traditional Bus Structure Connecting CPU and Memory l A bus is a collection of parallel wires that carry address, data, and control signals. l Buses are typically shared by multiple devices. Main memory I/O bridge Bus interface ALU Register file CPU chip System busMemory bus

Memory Read Transaction (1) l CPU places address A on the memory bus. ALU Register file Bus interface A 0 A x Main memory I/O bridge %eax Load operation: movl A, %eax

Memory Read Transaction (2) l Main memory reads A from the memory bus, retrieves word x, and places it on the bus. ALU Register file Bus interface x 0 A x Main memory %eax I/O bridge Load operation: movl A, %eax

Memory Read Transaction (3) l CPU read word x from the bus and copies it into register %eax. x ALU Register file Bus interface x Main memory 0 A %eax I/O bridge Load operation: movl A, %eax

Memory Write Transaction (1) l CPU places address A on bus. Main memory reads it and waits for the corresponding data word to arrive. y ALU Register file Bus interface A Main memory 0 A %eax I/O bridge Store operation: movl %eax, A

Memory Write Transaction (2) l CPU places data word y on the bus. y ALU Register file Bus interface y Main memory 0 A %eax I/O bridge Store operation: movl %eax, A

Memory Write Transaction (3) l Main memory reads data word y from the bus and stores it at address A. y ALU register file bus interface y main memory 0 A %eax I/O bridge Store operation: movl %eax, A

The CPU-Memory Gap Disk DRAM CPU SSD

Locality to the Rescue! The key to bridging this CPU-Memory gap is a fundamental property of computer programs known as locality

Today l Storage technologies and trends l Locality of reference l Caching in the memory hierarchy l Virtual memory and memory sharing

Locality l Principle of Locality: Programs tend to use data and instructions with addresses near or equal to those they have used recently l Temporal locality: u Recently referenced items are likely to be referenced again in the near future l Spatial locality: u Items with nearby addresses tend to be referenced close together in time

Locality Example l Data references u Reference array elements in succession (stride-1 reference pattern).  Reference variable sum each iteration. l Instruction references u Reference instructions in sequence. u Cycle through loop repeatedly. sum = 0; for (i = 0; i < n; i++) sum += a[i]; return sum; Spatial locality Temporal locality Spatial locality Temporal locality

Qualitative Estimates of Locality l Claim: Being able to look at code and get a qualitative sense of its locality is a key skill for a professional programmer. Question: Does this function have good locality with respect to array a ? int sum_array_rows(int a[M][N]) { int i, j, sum = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) sum += a[i][j]; return sum; }

Locality Example Question: Does this function have good locality with respect to array a ? int sum_array_cols(int a[M][N]) { int i, j, sum = 0; for (j = 0; j < N; j++) for (i = 0; i < M; i++) sum += a[i][j]; return sum; }

Locality Example Question: Can you permute the loops so that the function scans the 3-d array a with a stride-1 reference pattern (and thus has good spatial locality)? int sum_array_3d(int a[M][N][N]) { int i, j, k, sum = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) for (k = 0; k < N; k++) sum += a[k][i][j]; return sum; }

Memory Hierarchies l Some fundamental and enduring properties of hardware and software: u Fast storage technologies cost more per byte, have less capacity, and require more power (heat!). u The gap between CPU and main memory speed is widening. u Well-written programs tend to exhibit good locality. l These fundamental properties complement each other beautifully. l They suggest an approach for organizing memory and storage systems known as a memory hierarchy.

Today l Storage technologies and trends l Locality of reference l Caching in the memory hierarchy l Virtual memory and memory sharing

An Example Memory Hierarchy Registers L1 cache (SRAM) Main memory (DRAM) Local secondary storage (local disks) Larger, slower, cheaper per byte Remote secondary storage (tapes, distributed file systems, Web servers) Local disks hold files retrieved from disks on remote network servers Main memory holds disk blocks retrieved from local disks L2 cache (SRAM) L1 cache holds cache lines retrieved from L2 cache CPU registers hold words retrieved from L1 cache L2 cache holds cache lines retrieved from main memory L0: L1: L2: L3: L4: L5: Smaller, faster, costlier per byte

Caches l Cache: A smaller, faster storage device that acts as a staging area for a subset of the data in a larger, slower device. l Fundamental idea of a memory hierarchy: u For each k, the faster, smaller device at level k serves as a cache for the larger, slower device at level k+1. l Why do memory hierarchies work? u Because of locality, programs tend to access the data at level k more often than they access the data at level k+1. u Thus, the storage at level k+1 can be slower, and thus larger and cheaper per bit. l Big Idea: The memory hierarchy creates a large pool of storage that costs as much as the cheap storage near the bottom, but that serves data to programs at the rate of the fast storage near the top.

General Cache Concepts Cache Memory Larger, slower, cheaper memory viewed as partitioned into “blocks” Data is copied in block-sized transfer units Smaller, faster, more expensive memory caches a subset of the blocks

General Cache Concepts: Hit Cache Memory Data in block b is needed Request: Block b is in cache: Hit!

General Cache Concepts: Miss Cache Memory Data in block b is needed Request: 12 Block b is not in cache: Miss! Block b is fetched from memory Request: Block b is stored in cache Placement policy: determines where b goes Replacement policy: determines which block gets evicted (victim)

General Caching Concepts: Types of Cache Misses l Cold (compulsory) miss u Cold misses occur because the cache is empty. l Conflict miss u Most caches limit blocks at level k+1 to a small subset (sometimes a singleton) of the block positions at level k. »E.g. Block i at level k+1 must be placed in block (i mod 4) at level k. u Conflict misses occur when the level k cache is large enough, but multiple data objects all map to the same level k block. »E.g. Referencing blocks 0, 8, 0, 8, 0, 8,... would miss every time. l Capacity miss u Occurs when the set of active cache blocks (working set) is larger than the cache.

Examples of Caching in the Hierarchy Hardware0On-Chip TLBAddress translationsTLB Web browser10,000,000Local diskWeb pagesBrowser cache Web cache Network buffer cache Buffer cache Virtual Memory L2 cache L1 cache Registers Cache Type Web pages Parts of files 4-KB page 64-bytes block 4-8 bytes words What is Cached? Web proxy server 1,000,000,000Remote server disks OS100Main memory Hardware1On-Chip L1 Hardware10On/Off-Chip L2 AFS/NFS client10,000,000Local disk Hardware + OS100Main memory Compiler0 CPU core Managed ByLatency (cycles)Where is it Cached? Disk cacheDisk sectorsDisk controller100,000Disk firmware

Summary so far l The speed gap between CPU, memory and mass storage continues to widen. l Well-written programs exhibit a property called locality. l Memory hierarchies based on caching close the gap by exploiting locality.

CSE 153 – Lecture 10 – Memory Management38 Sharing Memory l Rewind to the days of “second-generation” computers u Programs use physical addresses directly u OS loads job, runs it, unloads it l Multiprogramming changes all of this u Want multiple processes in memory at once »Overlap I/O and CPU of multiple jobs u How to share physical memory across multiple processes? »Many programs do not need all of their code and data at once (or ever) – no need to allocate memory for it »A program can run on machine with less memory than it “needs”

CSE 153 – Lecture 10 – Memory Management39 Virtual Addresses l To make it easier to manage the memory of processes running in the system, we’re going to make them use virtual addresses (logical addresses) u Virtual addresses are independent of the actual physical location of the data referenced u OS determines location of data in physical memory l Instructions executed by the CPU issue virtual addresses u Virtual addresses are translated by hardware into physical addresses (with help from OS) u The set of virtual addresses that can be used by a process comprises its virtual address space

CSE 153 – Lecture 10 – Memory Management40 Virtual Addresses l Many ways to do this translation… u Need hardware support and OS management algorithms l Requirements u Need protection – restrict which addresses jobs can use u Fast translation – lookups need to be fast u Fast change – updating memory hardware on context switch vmapprocessor physical memory virtual addresses physical addresses

CSE 153 – Lecture 10 – Memory Management41 Fixed Partitions l Physical memory is broken up into fixed partitions u Size of each partition is the same and fixed u Hardware requirements: base register u Physical address = virtual address + base register u Base register loaded by OS when it switches to a process Physical Memory P1 P2 P3 P4 P5

CSE 153 – Lecture 10 – Memory Management42 Fixed Partitions P4’s Base + Offset Virtual Address Physical Memory Base Register P1 P2 P3 P4 P5 How do we provide protection?

43 Fixed Partitions l Advantages u Easy to implement »Need base register »Verify that offset is less than fixed partition size u Fast context switch l Problems? u Internal fragmentation: memory in a partition not used by a process is not available to other processes u Partition size: one size does not fit all (very large processes?) CSE 153 – Lecture 10 – Memory Management

44 Variable Partitions l Natural extension – physical memory is broken up into variable sized partitions u Hardware requirements: base register and limit register u Physical address = virtual address + base register l Why do we need the limit register? u Protection: if (virtual address > limit) then fault CSE 153 – Lecture 10 – Memory Management

45 Variable Partitions P3’s Base + Offset Virtual Address Base Register P2 P3< Protection Fault Yes? No? P3’s Limit Limit Register P1

46 Variable Partitions l Advantages u No internal fragmentation: allocate just enough for process l Problems? u External fragmentation: job loading and unloading produces empty holes scattered throughout memory CSE 153 – Lecture 10 – Memory Management P2 P3 P1 P4

CSE 153 – Lecture 10 – Memory Management47 Paging l Paging solves the external fragmentation problem by using fixed sized units in both physical and virtual memory Virtual Memory Page 1 Page 2 Page 3 Page N Physical Memory

CSE 153 – Lecture 10 – Memory Management48 Process Perspective l Processes view memory as one contiguous address space from 0 through N u Virtual address space (VAS) l In reality, pages are scattered throughout physical storage l The mapping is invisible to the program l Protection is provided because a program cannot reference memory outside of its VAS u The address “0x1000” maps to different physical addresses in different processes

CSE 153 – Lecture 10 – Memory Management49 Paging l Translating addresses u Virtual address has two parts: virtual page number and offset u Virtual page number (VPN) is an index into a page table u Page table determines page frame number (PFN) u Physical address is PFN::offset l Page tables u Map virtual page number (VPN) to page frame number (PFN) »VPN is the index into the table that determines PFN u One page table entry (PTE) per page in virtual address space »Or, one PTE per VPN

CSE 153 – Lecture 10 – Memory Management50 Page Lookups Page frame Page numberOffset Virtual Address Page Table Page frameOffset Physical Address Physical Memory

CSE 153 – Lecture 10 – Memory Management51 Paging Example l Pages are 4KB u Offset is 12 bits (because 4KB = 2 12 bytes) u VPN is 20 bits (32 bits is the length of every virtual address) l Virtual address is 0x7468 u Virtual page is 0x7, offset is 0x468 l Page table entry 0x7 contains 0x2000 u Page frame number is 0x2000 u Seventh virtual page is at address 0x2000 (2nd physical page) l Physical address = 0x x468 = 0x2468

52 Page Table Entries (PTEs) l Page table entries control mapping u The Modify bit says whether or not the page has been written »It is set when a write to the page occurs u The Reference bit says whether the page has been accessed »It is set when a read or write to the page occurs u The Valid bit says whether or not the PTE can be used »It is checked each time the virtual address is used (Why?) u The Protection bits say what operations are allowed on page »Read, write, execute (Why do we need these?) u The page frame number (PFN) determines physical page RVMProtPage Frame Number CSE 153 – Lecture 10 – Memory Management

53 Paging Advantages l Easy to allocate memory u Memory comes from a free list of fixed size chunks u Allocating a page is just removing it from the list u External fragmentation not a problem »All pages of the same size l Simplifies protection u All chunks are the same size u Like fixed partitions, don’t need a limit register l Simplifies virtual memory – later CSE 153 – Lecture 10 – Memory Management

54 Paging Limitations l Can still have internal fragmentation u Process may not use memory in multiples of a page l Memory reference overhead u 2 references per address lookup (page table, then memory) u What can we do? l Memory required to hold page table can be significant u Need one PTE per page u 32 bit address space w/ 4KB pages = 2 20 PTEs u 4 bytes/PTE = 4MB/page table u 25 processes = 100MB just for page tables! u What can we do?

CSE 153 – Lecture 10 – Memory Management55 Segmentation l Segmentation is a technique that partitions memory into logically related data units u Module, procedure, stack, data, file, etc. u Units of memory from user’s perspective l Natural extension of variable-sized partitions u Variable-sized partitions = 1 segment/process u Segmentation = many segments/process u Fixed partition : Paging :: Variable partition : Segmentation l Hardware support u Multiple base/limit pairs, one per segment (segment table) u Segments named by #, used to index into table u Virtual addresses become

CSE 153 – Lecture 10 – Memory Management56 Segment Lookups limitbase +< Protection Fault Segment #Offset Virtual Address Segment Table Yes? No? Physical Memory