Slide 1

Slide 1 text

RailsConf 2016 Tweaking Ruby GC parameters for fun, speed, and profit I mean: for fun! Helio Cola http://hac-rods.me @hacrods

Slide 2

Slide 2 text

RailsConf 2016 So, why I am here talking about GC?

Slide 3

Slide 3 text

RailsConf 2016 hac-rods.me Hi, I am Helio Cola! • ~15 years developing SW • ~10 years doing C/C++ on Solaris • 5/6 years since I found happiness • ~4 years full time happiness • I go by @hacrods on the internet…

Slide 4

Slide 4 text

RailsConf 2016 So, let’s get it started and talk about Ruby Garbage Collector

Slide 5

Slide 5 text

RailsConf 2016 Terms • GC: Garbage Collector • RGenGC: Restricted Generational GC • RIncGC: Restricted Incremental GC • CoW: Copy-on-write technique • ab: Apache Benchmark Tool

Slide 6

Slide 6 text

RailsConf 2016 Agenda • Why I am here talking about Ruby GC • A couple bits about its history • Configuration parameters • My approach to measure & tuning • Some of my findings • QA

Slide 7

Slide 7 text

RailsConf 2016 Why I am here… Ruby Garbage Collector

Slide 8

Slide 8 text

RailsConf 2016 Why I am here… GC • Once upon a time there was a Rails App • Why? How? How can I?

Slide 9

Slide 9 text

RailsConf 2016 Why I am here… GC • I did do a lot research on it… • I had a really great time learning & doing all this • I wanted to share it • And my talk got accepted!!!! :-)

Slide 10

Slide 10 text

RailsConf 2016 A bit about the history of Ruby GC

Slide 11

Slide 11 text

RailsConf 2016 A brief history about Ruby GC • Ruby 1.8: Simple Mark & Sweep • Ruby 1.9.3: Lazy Sweep • Ruby 2.0: Bitmap Masking (CoW friendly) • Ruby 2.1: RGenGC • Ruby 2.2: RIncGC and Symbol GC Reference: http://tmm1.net/ruby21-rgengc/

Slide 12

Slide 12 text

RailsConf 2016 A brief history about Ruby GC by Aman Gupta Reference: http://tmm1.net/ruby21-rgengc/

Slide 13

Slide 13 text

RailsConf 2016 Ruby 1.8: Simple Mark & Sweep Reference: http://tmm1.net/ruby21-rgengc/

Slide 14

Slide 14 text

RailsConf 2016 Ruby 1.9.3: Lazy Sweep Reference: http://tmm1.net/ruby21-rgengc/

Slide 15

Slide 15 text

RailsConf 2016 Ruby 2.0: Bitmap Masking (CoW) Reference: http://tmm1.net/ruby21-rgengc/

Slide 16

Slide 16 text

RailsConf 2016 Ruby 2.0.x: 2.0.0-p647

Slide 17

Slide 17 text

RailsConf 2016 Ruby 2.1: RGenGC Reference: http://tmm1.net/ruby21-rgengc/

Slide 18

Slide 18 text

RailsConf 2016 Ruby 2.2: RIncGC / Symbol GC • Symbols now gets collected by GC! • RIncGC https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/

Slide 19

Slide 19 text

RailsConf 2016 Ruby 2.2: RIncGC / Symbol GC • RIncGC • Not a silver bullet • Don’t affect throughput or response time Reference: https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/

Slide 20

Slide 20 text

RailsConf 2016 Ruby 2.1.x & 2.2.x

Slide 21

Slide 21 text

RailsConf 2016 Configuration parameters

Slide 22

Slide 22 text

RailsConf 2016 GC Parameters: ruby 2.0.x • RUBY_GC_MALLOC_LIMIT • RUBY_HEAP_MIN_SLOTS • RUBY_FREE_MIN https://github.com/ruby/ruby/blob/v2_0_0_647/gc.c#L3296

Slide 23

Slide 23 text

RailsConf 2016 GC Parameters: Ruby >= 2.1.x • RUBY_GC_HEAP_INIT_SLOTS • RUBY_GC_HEAP_FREE_SLOTS • RUBY_GC_HEAP_GROWTH_FACTOR • RUBY_GC_HEAP_GROWTH_MAX_SLOTS • RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705

Slide 24

Slide 24 text

RailsConf 2016 GC Parameters: Ruby >= 2.1.x • RUBY_GC_MALLOC_LIMIT • RUBY_GC_MALLOC_LIMIT_MAX • RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705

Slide 25

Slide 25 text

RailsConf 2016 GC Parameters: Ruby >= 2.1.x • RUBY_GC_OLDMALLOC_LIMIT • RUBY_GC_OLDMALLOC_LIMIT_MAX • RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705

Slide 26

Slide 26 text

RailsConf 2016 Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705 / https://github.com/ruby/ruby/blob/v2_2_3/gc.c#L6972 https://github.com/ruby/ruby/blob/v2_3_0/gc.c#L7256 Ruby v2.1.7 And also: v2.2.x & v2.3.x

Slide 27

Slide 27 text

RailsConf 2016 Running it $ export RUBY_GC_HEAP_INIT_SLOTS=800000 $ export RUBY_GC_HEAP_FREE_SLOTS=32768 $ export RUBY_GC_MALLOC_LIMIT=33554432 $ export RUBY_GC_MALLOC_LIMIT_MAX=50331648 $ rails s

Slide 28

Slide 28 text

RailsConf 2016 My approach to measure & tuning

Slide 29

Slide 29 text

RailsConf 2016 Me: measuring & tuning • Find out if your current/default experience is good, normal, bad, or really bad.

Slide 30

Slide 30 text

RailsConf 2016 Me: measuring & tuning • Research mode, reading mode, studying mode, read ruby source code, compile it… ✴ I have to upgrade the one with 2.0.x ✴ I don’t know yet if my app with 2.1.x is in good or bad shape • And after a lot of research…

Slide 31

Slide 31 text

RailsConf 2016 So, what now?! • Let me start by running it all on my laptop … and ab it! … and… still doesn’t look like my production app!

Slide 32

Slide 32 text

RailsConf 2016 So, and now what?! • A couple of days go by… … testing, measuring, analyzing data, thinking about what I am seeing… • Some well known reasons: • My laptop isn’t like my AWS servers • I don’t have the same amount of data • My ab calls aren’t reflecting very well the traffic from my real users … so… the app on my laptop won’t be like it is in prod servers!

Slide 33

Slide 33 text

RailsConf 2016 So… • Sleep on it • Go spend a day doing something else • Grab a teammate and talk to him • Explain what you are doing to someone else (or at least try to explain! :-) • Plan your next steps

Slide 34

Slide 34 text

RailsConf 2016 And: either this or that • You may open your eyes to something else • Or you establish a laptop baseline DEV PROD

Slide 35

Slide 35 text

RailsConf 2016 Finally: I got this

Slide 36

Slide 36 text

RailsConf 2016 Change, Measure & Analyze • Do that over & over & over again… • Analyze your data, document your numbers in a piece of paper or notepad • Make one single change (at a time) and re-run your tests

Slide 37

Slide 37 text

RailsConf 2016 Again: either this or that • If you will find something that improves your app ̣ Document it nicely and share with your team ̣ Make a small talk out of it ̣ And you won’t sweat to make it into the Production environment! • Or that will happen…

Slide 38

Slide 38 text

RailsConf 2016 Some of my findings

Slide 39

Slide 39 text

RailsConf 2016 Some of my findings Parameters I changed: • RUBY_GC_HEAP_INIT_SLOTS • RUBY_GC_HEAP_FREE_SLOTS • RUBY_GC_MALLOC_LIMIT • RUBY_GC_MALLOC_LIMIT_MAX

Slide 40

Slide 40 text

RailsConf 2016 Some of my findings A few comparisons: • Ruby 2.0.0 vs Ruby 2.1.7 (before) • Ruby 2.1.7: before and after • Ruby 2.2.3: before and after • Ruby 2.1.7 vs Ruby 2.2.3 (after)

Slide 41

Slide 41 text

RailsConf 2016 Ruby 2.0.0 vs Ruby 2.1.7

Slide 42

Slide 42 text

RailsConf 2016 v2.0.0 v2.1.7

Slide 43

Slide 43 text

RailsConf 2016 v2.0.0 v2.1.7

Slide 44

Slide 44 text

RailsConf 2016 Ruby 2.1.7 before vs after

Slide 45

Slide 45 text

RailsConf 2016 v2.1.7 before v2.1.7 after

Slide 46

Slide 46 text

RailsConf 2016 v2.1.7 before v2.1.7 after

Slide 47

Slide 47 text

RailsConf 2016 Ruby 2.2.3 before vs after

Slide 48

Slide 48 text

RailsConf 2016 v2.2.3 before v2.2.3 after

Slide 49

Slide 49 text

RailsConf 2016 v2.2.3 before v2.2.3 after

Slide 50

Slide 50 text

RailsConf 2016 Ruby 2.1.7 vs 2.2.3 (after)

Slide 51

Slide 51 text

RailsConf 2016 v2.1.7 after v2.2.3 after

Slide 52

Slide 52 text

RailsConf 2016 v2.1.7 after v2.2.3 after

Slide 53

Slide 53 text

RailsConf 2016 Thank you! Q&A?! Helio Cola http://hac-rods.me @hacrods

Slide 54

Slide 54 text

RailsConf 2016 Thanks Thanks to these folks that helped me along the way NoTwitterErik [email protected]