Garbage collection for C

Slides:



Advertisements
Similar presentations
An Implementation of Mostly- Copying GC on Ruby VM Tomoharu Ugawa The University of Electro-Communications, Japan.
Advertisements

Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Memory management.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS61C Review Midterm Spring Five Elements of a Computer Control Datapath Memory Input Output.
Incremental Garbage Collection
C and Data Structures Baojian Hua
Garbage collection (& Midterm Topics) David Walker COS 320.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Memory Layout C and Data Structures Baojian Hua
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
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.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
C LANGUAGE Characteristics of C · Small size
Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
CPSC 231 Organizing Files for Performance (D.H.)
Precise Garbage Collection for C
ENEE150 Discussion 07 Section 0101 Adam Wang.
Dynamic Memory Allocation
Checking Memory Management
CSC 253 Lecture 8.
Data Structures and Analysis (COMP 410)
CSC 253 Lecture 8.
Smart Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Main Memory Background Swapping Contiguous Allocation Paging
Strategies for automatic memory management
Dynamic Memory Allocation
Memory Allocation CS 217.
Memory Management Overview
Malloc Lab CSCI 380: Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Dynamic Memory – A Review
Automating Memory Management
Dynamic Array: Implementation of Stack
Run-time environments
CSE 303 Concepts and Tools for Software Development
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Presentation transcript:

Garbage collection for C Jon Rafkind

Issue Memory must be managed manually in C Notoriously difficult to get right( memory leaks, double free, dangling references ) Solution: garbage collection

Program lifetime Modifies pointers Allocates objects from memory When nursery runs out of space, perform a GC

Allocation Allocations come from the nursery, unless the object is too big Nursery is an array of bytes, allocate from the current pointer and bump the pointer N bytes Used Free New top Current top Used Free

Conservative collection Scan heap for words that look like they might be pointers, mark those objects as live Pros: easy to use, sound Cons: inaccurate 0xffab432f 0x84356 0x0 0xdeadbeef 0x42 0x23

Precise collection Traverse all data structures, only mark live pointers 0xba54af02 : struct foo * Pros: accurate Cons: incompatible with naïve libraries( future research ) 0xfafaabe1 0xba54af02 0xcedd4422 0xbe400219 0xbb123456

Magpie Precise, moving( generational ) garbage collector using mark/sweep Originally written by Adam Wick in C and Scheme Scheme frontend rewritten in Ocaml using Cil http://www.cs.utah.edu/~rafkind/projects/magpie/ http://hal.cs.berkeley.edu/cil

GC process 1. Mark/traverse roots and stack 2. For each object added, mark/traverse that object as well 3. Move objects in the nursery to older pages, make a new nursery

What is needed for precise collection? Need to know: Root pointers Pointers on the stack How to traverse objects in the heap

Root pointers Global variables – pointers and structures Call from main(): void register_globals(){ add_global( &some_global, gc_atomic ); .... }

Pointers on the stack Store pointers in a shadow stack void foo( int * x ){ int * y; ... } Store pointers in a shadow stack extern void * GC_shadow_stack[]; GC_shadow_stack[0] = last_shadow_stack; GC_shadow_stack[1] = &x; GC_shadow_stack[2] = &y;

Traverse objects in the heap For each allocation, compute type and store it somewhere Types: gc_atomic – int, char, etc gc_array – arrays of pointers gc_struct_* - unique type for each struct/union

Traversal routines for structs Need to traverse data in structs, generate a routine to do it void gc_struct_traverse_foo( struct foo * f ){ traverse( f->x ); traverse( f->y ); ... } gc_struct_foo = struct{ gc_struct_traverse_foo, ... };

Using it Works fine for code that naively uses malloc, calloc, realloc ( no program modifications necessary ) Lame mp3 encoder Top xterm Doesn't work as easily with programs that wrap malloc with complicated procedures ( gcc, perl ) safemalloc from perl obstack from gcc

Quick instructions Step 1: Run the C source through the preprocessor. I like to put the resulting file in a different directory, usually called 'gc'. $ gcc -E src/file.c -o gc/file.i Step 2: Run magpie over the preprocessed file. $ cilly.asm.exe --out gc/file.c --noPrintLn --dooneRet --doallocanalysis gc/file.i Step 3: Compile the file. $ gcc -c gc/file.c -o gc/file.o Step 4: Link the program together with the garbage collector. You will also have to compile gc_implementation.o, obviously. $ gcc gc/file.o gc/gc_implementation.o -o program

Spec2000

Linux Just another C program( mostly ) Non-uniform allocators ( kmem_cache_alloc ) Reference counting Bit twiddles memory ( red-black trees ) Interrupts Rcu ( Read-Copy update ) Multiple stacks

Allocators Slab allocators – created by some modules to speed up allocation Kmalloc, kzalloc – basically like malloc __get_free_page – close enough to malloc Vmalloc – close but strange Ioremap – maps physical memory to virtual memory

Reference counting Reference counting via kref objects Cannot be removed easily because kref is intertwinned with sysfs

Bit twiddling Red black tres operate on the lower 2 bits static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p){ rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p; } static inline void rb_set_color(struct rb_node *rb, int color){ rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;

Interrupts What if.. Disable during GC Hurts performance void some_function( int * x ){ ... GC_shadow_stack[2] = &x } Disable during GC Hurts performance Interrupt!

Rcu Read-copy update stores values in a per-cpu variable Last cpu to access the variable cleans it up, has own cleanup facilities that are not orthogonal to garbage collection

So far Stand alone modules work Attempted to GC ext3 but.. Filesystems are at the heart of Linux. GC'ing the filesystem requires the entire system to be GC'd as well.

How to incrementally GC linux? Debugging is hard! Summary How to incrementally GC linux? Debugging is hard!

The end . Questions?