Incremental GC for Ruby interpreter

Similar documents
RincGC: Incremental GC for CRuby/MRI interpreter. Koichi Sasada Heroku, Inc.

Object lifetime analysis with Ruby 2.1

Speedup Ruby Interpreter

MRI Internals. Koichi Sasada.

Memory management tuning in Ruby

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank

Garbage Collection Algorithms. Ganesh Bikshandi

Programming Language Implementation

Lecture 15 Advanced Garbage Collection

Garbage Collection. Hwansoo Han

Exploiting the Behavior of Generational Garbage Collector

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Lecture 15 Garbage Collection

Concurrent Garbage Collection

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

CMSC 330: Organization of Programming Languages

Lecture 13: Garbage Collection

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection

Progress report of "Ruby 3 Concurrency"

Shenandoah An ultra-low pause time Garbage Collector for OpenJDK. Christine H. Flood Roman Kennke

A new Mono GC. Paolo Molaro October 25, 2006

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Garbage Collection. CS 351: Systems Programming Michael Saelee

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Review. Partitioning: Divide heap, use different strategies per heap Generational GC: Partition by age Most objects die young

Garbage Collection (2) Advanced Operating Systems Lecture 9

CMSC 330: Organization of Programming Languages

Garbage Collection. Vyacheslav Egorov

Garbage Collection Techniques

Hard Real-Time Garbage Collection in Java Virtual Machines

Toward more efficient Ruby 2.1

Garbage Collection. Weiyuan Li

Garbage Collection. Steven R. Bagley

CS 4120 Lecture 37 Memory Management 28 November 2011 Lecturer: Andrew Myers

Automatic Garbage Collection

A Tour to Spur for Non-VM Experts. Guille Polito, Christophe Demarey ESUG 2016, 22/08, Praha

Garbage Collection (1)

A proposal of new concurrency model for Ruby 3

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Tick: Concurrent GC in Apache Harmony

Limits of Parallel Marking Garbage Collection....how parallel can a GC become?

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Contents in Detail. Who This Book Is For... xx Using Ruby to Test Itself... xx Which Implementation of Ruby?... xxi Overview...

The Generational Hypothesis

Heap Compression for Memory-Constrained Java

Simple Garbage Collection and Fast Allocation Andrew W. Appel

Implementation Garbage Collection

Reference Counting. Reference counting: a way to know whether a record has other users

The C4 Collector. Or: the Application memory wall will remain until compaction is solved. Gil Tene Balaji Iyengar Michael Wolf

Java & Coherence Simon Cook - Sales Consultant, FMW for Financial Services

Opera&ng Systems CMPSCI 377 Garbage Collec&on. Emery Berger and Mark Corner University of Massachuse9s Amherst

Mark-Sweep and Mark-Compact GC

Parallel programming in Ruby3 with Guild

CS61C : Machine Structures

Automatic Memory Management

Run-Time Environments/Garbage Collection

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Understanding Garbage Collection

Robust Memory Management Schemes

The Garbage-First Garbage Collector

Kodewerk. Java Performance Services. The War on Latency. Reducing Dead Time Kirk Pepperdine Principle Kodewerk Ltd.

Generation scavenging: A non-disruptive high-performance storage reclamation algorithm By David Ungar

Low latency & Mechanical Sympathy: Issues and solutions

RubyConf 2005 Oct. 14 SASADA Koichi Tokyo University of Agriculture and Technology Nihon Ruby no Kai

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Go GC: Prioritizing Low Latency and Simplicity

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps

Garbage-First Garbage Collection by David Detlefs, Christine Flood, Steve Heller & Tony Printezis. Presented by Edward Raff

Run-time Environments -Part 3

Reference Counting. Reference counting: a way to know whether a record has other users

G Programming Languages - Fall 2012

JVM Memory Model and GC

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Fundamentals of GC Tuning. Charlie Hunt JVM & Performance Junkie

Garbage Collection Basics

CS842: Automatic Memory Management and Garbage Collection. Mark and sweep

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

THE TROUBLE WITH MEMORY

Compiler Construction D7011E

Java Performance Tuning

File Systems. CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

JVM Troubleshooting MOOC: Troubleshooting Memory Issues in Java Applications

Can you see me? 2008/12/11 Future of Ruby VM - RubyConf2008 1

Evaluating and improving remembered sets in the HotSpot G1 garbage collector

Heap Management. Heap Allocation

Implementation of Ruby and later

Configuring the Heap and Garbage Collector for Real- Time Programming.

Shenandoah: An ultra-low pause time garbage collector for OpenJDK. Christine Flood Roman Kennke Principal Software Engineers Red Hat

Cycle Tracing. Presented by: Siddharth Tiwary

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

2011 Oracle Corporation and Affiliates. Do not re-distribute!

Reducing Pause Time of Conservative Collectors

Specification and Verification of Garbage Collector by Java Modeling Language

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Using Page Residency to Balance Tradeoffs in Tracing Garbage Collection

Transcription:

Incremental GC for Ruby interpreter Koichi Sasada ko1@heroku.net 1

2014 Very important year for me 2

10 th Anniversary 3

10 th Anniversary YARV development (2004/01-) First presentation at RubyConf 2004 4

Garbage Collection Improvements Good throughput and short pause time Ruby 2.2 will be released soon. 5

Time (ms) RGenGC: Micro-benchmark 3000 2500 2000 1500 1000 500 0 704.843669 1699.805974 no total mark x2.5 faster 867.740319 87.230735 RGenGC total sweep 6

RGenGC: Pause time 7

Today s topic Use incremental GC algorithm for major GC to reduce long pause time Ruby 2.2 will have it!! Goal Before Ruby 2.1 Ruby 2.1 RGenGC Incremental GC Ruby 2.2 Gen+IncGC Throughput Low High Low High Pause time Long Long Short Short 8

Achievements: RGenGC+RincGC 9

Who am I? Koichi Sasada as a Programmer CRuby committer since 2007/01 Original YARV developer since 2004/01 From Japan 10

Who am I? Koichi Sasada as a Employee 11

Who am I? Koichi Sasada as a Employee A member of Matz team Full-time CRuby developer Working in Japan Mission of our team is to improve QUALITY of CRuby interpreter 12

Upcoming Ruby 2.2 What s next? http://www.flickr.com/photos/adafruit/8483990604 13

Ruby 2.2 Syntax No notable changes (maybe) Symbol key of Hash literal can be quoted { foo-bar : baz}#=> {: foo-bar => baz} #=> not { foo-bar => baz} like JSON TRAP: easy to misunderstand 14

Ruby 2.2 Classes and Methods Some methods are introduces Kernel#itself String#unicode_normalize Etc.nprocessors 15

Ruby 2.2 Internal changes Remove obsolete C-APIs Hide internal definitions of data type 16

Ruby 2.2 Improvements Improve GC Symbol GC 4 ages generational GC Incremental GC (today s topic) Improve the performance of keyword parameters Use frozen string literals if possible 17

Ruby 2.2 Symbol GC before = Symbol.all_symbols.size 1_000_000.times{ i i.to_s.to_sym} # Make 1M symbols after = Symbol.all_symbols.size; p [before, after] # Ruby 2.1 #=> [2_378, 1_002_378] # not GCed # Ruby 2.2 (dev) #=> [2_456, 2_456] # GCed! 18

Ruby 2.2 Fast keyword parameters # time of this program def foo(k1: nil, k2: nil, k3: nil, k4: nil, k5: nil, k6: nil) 10_000_000.times{foo(k1: 1, k2: 2, k3: 3, k4: 4, k5: 5, k6: 6)} 20 15 10 5 0 17.170841 x15 faster! 1.15534 Execution time (sec) Before opt. After opt. 19

http://www.flickr.com/photos/donkeyhotey/8422065722 Break 20

Garbage collection The automatic memory management http://www.flickr.com/photos/circasassy/6817999189/ 21

History of CRuby s GC 1993/12 Ruby 0.9: Conservative mark and sweep GC Simple algorithm Easy to implement C extensions 2011/10 Ruby 1.9.3: Lazy sweep To reduce pause time on sweep 2013/02 Ruby 2.0: Bitmap marking To make CoW friendly 2013/12 Ruby 2.1: RGenGC To improve throughput 22

Since birth of Ruby Simple Mark & Sweep Root objects marked marked marked free free 1. Mark reachable objects from root objects 2. Sweep unmarked objects (collection and deallocation) marked marked Collect unreachable objects free 23

Since Ruby 2.1 RGenGC Weak generational hypothesis: Most objects die young http://www.flickr.com/photos/ell-r-brown/5026593710 Concentrate reclamation effort only on the young objects 24

Since Ruby 2.1 RGenGC Separate young generations and old generations Create objects as youngest generation Promote to old generations after surviving GCs Many minor GC and rare major GC Usually, GC on only young space (minor GC) GC on both spaces if no memory (major/full GC) Improve total throughput 25

Since Ruby 2.1 RGenGC [Minor M&S GC] 1 st MinorGC new old new old Root objects new old new old new old old/ free new/ free new/ free collect Don t collect old object even if it is unreachable. Mark reachable objects from root objects. Mark and promote to old generation Stop traversing after old objects Reduce mark overhead Sweep not (marked or old) objects Can t collect Some unreachable objects 26

Since Ruby 2.1 RGenGC [Minor M&S GC] 2 nd MinorGC ignore old ignore old Root objects old old ignore ignore old old/ free new/ free new/ free collect Mark reachable objects from root objects. Mark and promote to old generation Assumption: Old objects only refer old objects Stop traversing after old objects Reduce mark overhead Sweep not (marked or old) objects Can t collect Some unreachable objects Don t collect old object even if it is unreachable. 27

Since Ruby 2.1 RGenGC [Remember set] Root objects ignore old old old new ignore old ignore new Assumption: Old objects only refer old objects However old objects can refer young objects by adding reference from old to new objects Ignore traversal of old object Minor GC causes marking leak!! Because minor GC ignores referenced objects by old objects Can t mark new object! Sweeping living object! (Critical BUG) 28

Since Ruby 2.1 RGenGC [Remember set] Root objects new old ignore ignore Remember set (RSet) Remember 1. Detect creation of an [old->new] type reference 2. Add an [old object] into Remember set (RSet) if an old object refer new objects old old old new 29

Since Ruby 2.1 RGenGC [Remember set] Root objects new old ignore ignore Remember set (RSet) collect new/ free new/ free 1. Mark reachable objects from root objects Remembered objects are also root objects 2. Sweep not (marked or old) objects old old old new 30

Since Ruby 2.1 RGenGC [Write barrier] To detect [old new] type references, we need to insert Write-barrier into interpreter for all Write operation old new Write barrier [Old->New] type reference detected! 31

Since Ruby 2.1 RGenGC: Challenge Trade-off of Speed and Compatibility Introducing Write barriers completely is very hard Can we achieve both speed-up w/ GenGC and keeping compatibility? 32

Since Ruby 2.1 RGenGC: Key idea Introduce WB unprotected objects 33

Since Ruby 2.1 RGenGC: Key idea Separate objects into two types WB protected objects WB unprotected objects Decide this type at creation time A class care about WB WB protected object A class don t care about WB WB unprotected object 34

Since Ruby 2.1 RGenGC: Key idea Normal objects can be changed to WB unprotected objects WB unprotect operation C-exts which don t care about WB, objects will be WB unprotected objects Example ptr = RARRAY_PTR(ary) In this case, we can t insert WB for ptr operation, so VM shade ary WB p. obj VM Create Unprotect WB unp. obj Now, WB unprotected object can t change into WB p. object 35

Since Ruby 2.1 RGenGC: Rules Treat WB unprotected objects correctly At Marking 1. Don t promote WB unprotected objects to old objects 2. Remember WB unprotected objects pointed from old objects At WB unprotect operation for old WB protected objects 1. Demote objects 2. Remember this unprotected objects 36

Since Ruby 2.1 RGenGC: [Minor M&S GC w/wb unp. objects] 1 st MinorGC old Root objects new old new old new old WB unp. Remember set (RSet) collect new/ free new/ free mark and remember new old remember Mark reachable objects from root objects Mark WB unprotected objects, and *don t promote* them to old gen objects If WB unprotected objects pointed from old objects, then remember this WB unprotected objects by RSet. Mark WB unprotected objects every minor GC!! 37

Since Ruby 2.1 RGenGC: [Minor M&S GC w/wb unp. objects] 2 nd MinorGC ignore old Root objects old old new old ignore ignore WB unp. Remember set (RSet) old collect new/ free new/ free Mark reachable objects from root objects Mark WB unprotected objects, and *don t promote* them to old gen objects If WB unprotected objects pointed from old objects, then remember this WB unprotected objects by RSet. Mark WB unprotected objects every minor GC!! 38

Since Ruby 2.1 RGenGC: [Unprotect operation] old old Remember set (RSet) WB old unp. new Anytime Object can give up to keep write barriers [Unprotect operation] Change old WB protected objects to WB unprotected objects Example: RARRAY_PTR(ary) (1) Demote object (old new) (2) Register it to Remember Set 39

1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106 111 116 121 126 131 136 Time (ms) RGenGC: Micro-benchmark 25 20 15 10 5 0 n-th GC mark time/no sweep time/no mark time/rgengc sweep time/rgengc 40

Time (ms) RGenGC: Micro-benchmark 3000 2500 2000 704.843669 1500 1000 500 0 1699.805974 no 867.740319 87.230735 RGenGC total mark total sweep 41

1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 113 117 121 125 129 133 137 141 145 Time (ms) RGenGC: Rdoc application 200 180 160 140 120 100 80 60 40 20 0 nth-gc mark time/no sweep time/no mark time/rgengc sweep time/rgengc 42

Time (ms) RGenGC: Rdoc application 9000 8000 7000 6000 5000 4000 3000 2000 1000 0 total mark total sweep no 8210.724461 3524.425873 RGenGC 1049.27184 3579.584833 no RGenGC 43

Time (ms) RGenGC: Rdoc application 120000 100000 80000 60000 40000 20000 0 no RGenGC total sweep 3524.425873 3579.584833 total mark 8210.724461 1049.27184 others 100907.4225 100640.8472 others total mark total sweep 44

1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 113 117 121 125 129 133 137 141 145 RGenGC: Rdoc application 3,000,000 2,500,000 2,000,000 1,500,000 1,000,000 500,000 0 available slots live slots old objects unprotected objects remembered unprotected objects 45

1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 113 117 121 125 129 133 137 141 145 RGenGC: Rdoc application 70,000 60,000 50,000 40,000 30,000 20,000 10,000 0 unprotected objects remembered unprotected objects 46

Since Ruby 2.1 RGenGC timing chart 2.0.0 GC (M&S w/lazy sweep) Ruby Mark Sweep Sweep Sweep Sweep Sweep Stop the (Ruby) World w/rgengc (Minor GC) Ruby Mark Sweep Sweep Sweep Sweep Sweep Stop the (Ruby) World w/rgengc (Major GC) Ruby Mark Sweep Sweep Sweep Sweep Sweep Stop the (Ruby) World 47

Issue of RGenGC: Long pause time RGenGC achieves high throughput Minor GC stops only short pause time Major GC still stops long pause time Introducing Incremental GC for major GC Generational GC Incremental GC Gen+Inc GC Throughput High Low High Pause time Long Short Short 48

RincGC: Restricted Incremental GC algorithms RincGC algorithm is implemented for Ruby 2.2. 49

Incremental GC Well-known GC algorithm to reduce pause time Do GC steps incrementally Interleaving with Ruby s execution (mutator) and GC process. Lazy sweep is part of an incremental GC STW GC Ruby Mark Sweep Stop the (Ruby) World Sweep Sweep Sweep Sweep Inc GC Ruby Mark Mark Mark Mark Mark Sweep Sweep Sweep Short pause time No total time change 50

Terminology: Tri-color GC Define three colors for objects White objects is not d objects Grey objects are marking objects Black objects are marked objects 51

Incremental GC 1. Color all objects white Root objects Collect white objects 2. Grey root objects 3. Choose a grey object and grey all reachable white objects, and black the chosen object (incremental marking) 4. Finish marking when no grey objects 5. Sweep white objects as unmarked objects 52

Incremental GC requires WBs Root objects Mutator can add reference from a Black object to a white object!! Mark miss! No more traversal from black objects 53

Incremental GC requires WBs Root objects Use write barrier to detect an addition of references from black objects to white objects, and grey black objects 54

RincGC: Restricted Incremental GC using WBunprotected objects Use WB unprotected objects like RGenGC Introducing a new rule: Scan all black WB unprotected objects at the end of incremental GC at once WB unprotected objects can point white objects Scan from all ( Black and WB unprotected objects ) at once (stop the world marking) 55

RincGC Restricted Incremental GC using WB-unprotected objects Root objects WB unp. WB unp. Root 1. Color all objects white 2. Grey root objects 3. Choose a grey object and grey reachable white objects, and black the chosen object (incremental marking) 4. Finish marking when no grey objects 5. Scan all black WB unprotected objects at once 6. Sweep white objects as unmarked objects 56

RincGC: Discussion Long pause time than usual incremental GC step This technique can introduce long pause time, relative to the number of WB unprotected objects at last. This is why this algorithm is named Restricted Similar/shorter pause time than Minor GC of RGenGC. 57

Implementation 58

Ruby s implementation State chart garbage_collect() State: marking State: sweeping Direct transition To mutator Ruby program (mutators) newobj() if (no pages) marks_start() sweep_start() if (incremental_marking) marks_continue() marks_step() sweep_step() if (sweep_pages) sweep_continue() marks_finish() sweep_finish() State: none 59

Ruby s implementation WB protected/unprotected Make popular class instances WB protected String, Array, Hash, and so on Implement unprotect operation for Array and so on Remain WB unprotected objects Difficult to insert WBs: a part of Module, Proc (local variables) and so on. Minor features 60

Ruby s implementation Data structure Introduce 2 bits age information to represent young and old objects Age 0, 1, 2 is young object Age 3 is old object Surviving 1 GC increase one age Add 3 bits information for each objects (we already have mark bit) WB unprotected bit Long lived bit (old objects or remembered WB unprotected objects) Remembered old object bit / Marking (Grey) bit They can share 1 bit field because the former is used only at minor GC and the latter is used only at major GC (incremental GC) 61

Ruby s implementation Bitmap technique Each bits are managed by bitmap technique Easy to mange remember set Fast traversing Easy to get a set Remember set: (Remembered old object bitmap) (Long lived bitmap & WB unp. Bitmap) Living unprotected objects: (mark bitmap & WB unprotected bitmap) 62

RincGC: Evaluation Measure pause times for <https://github.com/tenderlove/ko1-test-app> by Aaron Patterson 63

Evaluation 64

1 69 137 205 273 341 409 477 545 613 681 749 817 885 953 1021 1089 1157 1225 1293 1361 1429 1497 1565 1633 1701 1769 1837 1905 1973 2041 2109 2177 2245 2313 2381 2449 GC pause time (sec) Evaluation 0.018 0.016 0.014 0.012 0.01 0.008 0.006 0.004 0.002 0 2,500 GC's pause time since one major GC Long major marking time of RGenGC Incremental Major GC of RincGC Lazy sweep pauses only short time Same pause time for Minor marking time pause time (rgengc) pause time (rincgc) 65

Evaluation 0.02 maximum pause time 0.015 0.01 0.005 0 rgengc rincgc max 0.015923438 0.004662491 66

NOTE: Incremental GC is not silver bullet Incremental GC does not guarantee improving your application s response time Incremental GC does not reduce total GC time, so that a big task includes several major GC doesn t improve its response time. Check GC counts with GC.stat(:major_gc_count) and GC.stat(:minor_gc_count) for each request. 67

Summary Introducing incremental GC algorithm into major GC to reduce long pause time Ruby 2.2 will have it!! Goal Before Ruby 2.1 Ruby 2.1 RGenGC Incremental GC Ruby 2.2 Gen+IncGC Throughput Low High Low High Pause time Long Long Short Small 68

Thank you for your attention Koichi Sasada <ko1@heroku.com> 69