University of Washington Today Lab 5 out  Puts a *lot* together: pointers, debugging, C, etc.

Slides:



Advertisements
Similar presentations
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Advertisements

Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts / : Introduction to Computer Systems 18 th Lecture, March 24, 2015 Instructors:
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
1 Dynamic Memory Allocation: Basic Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaro.
Carnegie Mellon 1 Virtual Memory: Concepts : Introduction to Computer Systems 15 th Lecture, Oct. 14, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Dynamic Memory Allocation I Nov 5, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies class21.ppt “The course that gives.
Dynamic Memory Allocation I May 21, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies.
Dynamic Memory Allocation I November 1, 2006 Topics Simple explicit allocators Data structures Mechanisms Policies class18.ppt “The course that.
– 1 – P6 (PentiumPro,II,III,Celeron) memory system bus interface unit DRAM Memory bus instruction fetch unit L1 i-cache L2 cache cache bus L1 d-cache inst.
Virtual Memory: Concepts
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Chapter 4 Memory Management Virtual Memory.
1 Virtual Memory: Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
1 Dynamic Memory Allocation: Basic Concepts. 2 Today Basic concepts Implicit free lists.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts CENG331 - Computer Organization.
Virtual Memory Topics Motivations for VM Address translation Accelerating translation with TLBs CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Basic Concepts :
Dynamic Memory Allocation April 9, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies Reading: 10.9 Problems: and class21.ppt.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts Instructors: Adapted from CMU course
University of Washington Indirection in Virtual Memory 1 Each process gets its own private virtual address space Solves the previous problems Physical.
Instructor: Phil Gibbons
Dynamic Memory Allocation I
Section 9: Virtual Memory (VM)
Instructor: Haryadi Gunawi
Today How was the midterm review? Lab4 due today.
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Dynamic Memory Allocation I October 28, 2003
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
The Hardware/Software Interface CSE351 Winter 2013
CSE 153 Design of Operating Systems Winter 2018
Memory Allocation I CSE 351 Spring 2017
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory II CSE 351 Autumn 2016
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Concepts /18-213/14-513/15-513: Introduction to Computer Systems 17th Lecture, October 23, 2018.
Memory Management I: Dynamic Storage Allocation March 2, 2000
Memory Allocation I CSE 351 Spring 2017
ECE Dept., University of Toronto
P6 (PentiumPro,II,III,Celeron) memory system
Dynamic Memory Allocation I
Dynamic Memory Allocation
Memory Allocation I CSE 351 Winter 2018
Dynamic Memory Allocation I
Instructors: Majd Sakr and Khaled Harras
Dynamic Memory Allocation: Basic Concepts /18-213/14-513/15-513: Introduction to Computer Systems 19th Lecture, October 30, 2018.
Memory Allocation I CSE 351 Winter 2018
CSE 451: Operating Systems Autumn 2005 Memory Management
Dynamic Memory Allocation November 2, 2000
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Virtual Memory II CSE 351 Winter 2018
Dynamic Memory Allocation I
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS703 - Advanced Operating Systems
Memory Allocation I CSE 351 Autumn 2019
CSE 153 Design of Operating Systems Winter 2019
Instructors: Majd Sakr and Khaled Harras
CSE 153 Design of Operating Systems Winter 2019
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.
Memory Allocation I CSE 351 Autumn 2018
Instructor: Phil Gibbons
P6 (PentiumPro,II,III,Celeron) memory system
Presentation transcript:

University of Washington Today Lab 5 out  Puts a *lot* together: pointers, debugging, C, etc.

University of Washington Address Translation: Page Hit 2 1) Processor sends virtual address to MMU (memory management unit) 2-3) MMU fetches PTE from page table in cache/memory 4) MMU sends physical address to cache/memory 5) Cache/memory sends data word to processor MMU Cache/ Memory PA Data CPU VA CPU Chip PTEA PTE

University of Washington Address Translation: Page Fault 3 1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in cache/memory 4) Valid bit is zero, so MMU triggers page fault exception 5) Handler identifies victim (and, if dirty, pages it out to disk) 6) Handler pages in new page and updates PTE in memory 7) Handler returns to original process, restarting faulting instruction MMU Cache/ Memory CPU VA CPU Chip PTEA PTE Disk Page fault handler Victim page New page Exception 6 7

University of Washington Hmm… Translation Sounds Slow! The MMU accesses memory twice: once to first get the PTE for translation, and then again for the actual memory request from the CPU  The PTEs may be cached in L1 like any other memory word  But they may be evicted by other data references  And a hit in the L1 cache still requires 1-3 cycles What can we do to make this faster? 4

University of Washington Speeding up Translation with a TLB Solution: add another cache! Translation Lookaside Buffer (TLB):  Small hardware cache in MMU  Maps virtual page numbers to physical page numbers  Contains complete page table entries for small number of pages  Modern Intel processors: 128 or 256 entries in TLB 5

University of Washington TLB Hit 6 MMU Cache/ Memory PA Data CPU VA CPU Chip PTE A TLB hit eliminates a memory access TLB VPN 3

University of Washington TLB Miss 7 MMU Cache/ Memory PA Data CPU VA CPU Chip PTE TLB VPN 4 PTEA 3 A TLB miss incurs an additional memory access (the PTE) Fortunately, TLB misses are rare

University of Washington Virtual Memory Summary Programmer’s view of virtual memory  Each process has its own private linear address space  Cannot be corrupted by other processes, why? System view of virtual memory  Uses memory efficiently by caching virtual memory pages  Efficient only because of locality  Simplifies memory management and sharing  Simplifies protection by providing a convenient interpositioning point to check permissions 8

University of Washington Roadmap 9 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG(); get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp ret Java: C: Assembly language: Machine code: Computer system: OS: Data & addressing Integers & floats Machine code & C x86 assembly programming Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C

University of Washington Memory Allocation Topics Dynamic memory allocation  Size/number of data structures may only be known at run time  Need to allocate space on the heap  Need to de-allocate (free) unused memory so it can be re-allocated Implementation  Implicit free lists  Explicit free lists – subject of next programming assignment  Segregated free lists Garbage collection Common memory-related bugs in C programs 10

University of Washington Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc ) to acquire VM at run time.  For data structures whose size is only known at runtime. Dynamic memory allocators manage an area of process virtual memory known as the heap. Program text (.text ) Initialized data (.data ) User stack 0 Top of heap ( brk ptr) Application Dynamic Memory Allocator Heap 11 Heap (via malloc ) Uninitialized data (. bss )

University of Washington Dynamic Memory Allocation Allocator maintains heap as collection of variable sized blocks, which are either allocated or free  Allocator requests space in heap region; VM hardware and kernel allocate these pages to the process  Application objects are typically smaller than pages, so the allocator manages blocks within pages Types of allocators  Explicit allocator: application allocates and frees space  E.g. malloc and free in C  Implicit allocator: application allocates, but does not free space  E.g. garbage collection in Java, ML, and Lisp 12

University of Washington The malloc Package #include void *malloc(size_t size)  Successful:  Returns a pointer to a memory block of at least size bytes (typically) aligned to 8-byte boundary  If size == 0, returns NULL  Unsuccessful: returns NULL and sets errno void free(void *p)  Returns the block pointed at by p to pool of available memory  p must come from a previous call to malloc or realloc Other functions  calloc : Version of malloc that initializes allocated block to zero.  realloc: Changes the size of a previously allocated block.  sbrk : Used internally by allocators to grow or shrink the heap. 13

University of Washington Malloc Example void foo(int n, int m) { int i, *p; /* allocate a block of n ints */ p = (int *)malloc(n * sizeof(int)); if (p == NULL) { perror("malloc"); exit(0); } for (i=0; i<n; i++) p[i] = i; /* add space for m ints to end of p block */ if ((p = (int *)realloc(p, (n+m) * sizeof(int))) == NULL) { perror("realloc"); exit(0); } for (i=n; i < n+m; i++) p[i] = i; /* print new array */ for (i=0; i<n+m; i++) printf("%d\n", p[i]); free(p); /* return p to available memory pool */ } 14

University of Washington Assumptions Made in This Lecture Memory is word addressed (each word can hold a pointer)  block size is a multiple of words Allocated block (4 words) Free block (3 words) Free word Allocated word 15

University of Washington Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 16

University of Washington Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 17

University of Washington Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 18

University of Washington Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 19

University of Washington Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 20

University of Washington How are going to implement that?!? What information does the allocator need to keep track of? 21

University of Washington Constraints Applications  Can issue arbitrary sequence of malloc() and free() requests  free() requests must be made only for a previously malloc()’d block Allocators  Can’t control number or size of allocated blocks  Must respond immediately to malloc() requests  i.e., can’t reorder or buffer requests  Must allocate blocks from free memory  i.e., blocks can’t overlap, why not?  Must align blocks so they satisfy all alignment requirements  8 byte alignment for GNU malloc ( libc malloc) on Linux boxes  Can’t move the allocated blocks once they are malloc()’d  i.e., compaction is not allowed. Why not? 22

University of Washington Performance Goal: Throughput Given some sequence of malloc and free requests:  R 0, R 1,..., R k,..., R n-1 Goals: maximize throughput and peak memory utilization  These goals are often conflicting Throughput:  Number of completed requests per unit time  Example:  5,000 malloc() calls and 5,000 free() calls in 10 seconds  Throughput is 1,000 operations/second 23

University of Washington Performance Goal: Peak Memory Utilization Given some sequence of malloc and free requests:  R 0, R 1,..., R k,..., R n-1 Def: Aggregate payload P k  malloc(p) results in a block with a payload of p bytes  After request R k has completed, the aggregate payload P k is the sum of currently allocated payloads Def: Current heap size = H k  Assume H k is monotonically nondecreasing  Allocator can increase size of heap using sbrk() Def: Peak memory utilization after k requests  U k = ( max i<k P i ) / H k  Goal: maximize utilization for a sequence of requests.  Why is this hard? And what happens to throughput? 24

University of Washington Fragmentation Poor memory utilization is caused by fragmentation  internal fragmentation  external fragmentation 25

University of Washington Internal Fragmentation For a given block, internal fragmentation occurs if payload is smaller than block size Caused by  overhead of maintaining heap data structures (inside block, outside payload)  padding for alignment purposes  explicit policy decisions (e.g., to return a big block to satisfy a small request) why would anyone do that? payload Internal fragmentation block Internal fragmentation 26

University of Washington External Fragmentation Occurs when there is enough aggregate heap memory, but no single free block is large enough Depends on the pattern of future requests  Thus, difficult to measure p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(6) Oops! (what would happen now?) 27

University of Washington 28