• Save
Ruby's GC 20
Upcoming SlideShare
Loading in...5
×
 

Ruby's GC 20

on

  • 3,923 views

I will talk about some improvements of GC in Ruby 2.0.0. For instance, I will introduce about implementations of Bitmap Marking GC and so on, and show results of benchmarks after these are ...

I will talk about some improvements of GC in Ruby 2.0.0. For instance, I will introduce about implementations of Bitmap Marking GC and so on, and show results of benchmarks after these are implemented.

Animation version is here: https://gumroad.com/l/xWCR (premium version)

Statistics

Views

Total Views
3,923
Views on SlideShare
2,114
Embed Views
1,809

Actions

Likes
12
Downloads
0
Comments
0

8 Embeds 1,809

http://d.hatena.ne.jp 1675
http://eventifier.co 102
https://twitter.com 24
http://feedspot.com 2
http://www.feedspot.com 2
http://eventifier.com 2
http://news.google.com 1
http://dhatena.gottabemobile.org 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Ruby's GC 20 Ruby's GC 20 Presentation Transcript

  • #rubykaigi 03nari/@nari3/authorNariNetwork Applied Communication Laboratory2013/05/31Rubys GC 2.0
  • Self-introduction➔nari, @nari3, authorNari➔A CRuby committer➔GC entertainer➔“Nakamura”– is the most powerful clan in Ruby World
  • I went to Cebu in Philippines➔I studied English a month.➔But I cant speak English....
  • Because I was always alone
  • Because Im shy
  • Todays Agenda➔Non-recursive Marking➔Bitmap Marking– My work in Ruby 2.0.0
  • What is GC ?
  • GC collects alldead objects
  • What is a deadobject?
  • What is a dead object?➔A dead object is an object that isno longer referenced by theprogram➔In GC terms, we say a that deadobject is unreachable from Roots
  • What is Roots?➔Roots is a set of pointers thatdirectly reference objects in theprogram.– e.g. Rubys local variables, etc..
  • What is GC ?
  • GC collects objectsthat are unreachablefrom Roots.
  • CRubys GCSummary
  • CRubys GC➔Mark&Sweep➔Mark phase– mark all live(reachable) objects➔Sweep phase– free up all dead(unreachable) objects– Unmark all marked objects
  • []Root@mamiLeg []Body []@mami = nil@mami = [Leg]@mami[1] = [Body]@mami[1][1] = [Head]Head@mami[1].pop #=> [Head]GC.start
  • []Root@mamiLeg []Body []HeadMark phase[]Leg []Bodymarkmarkmarkmark all live(reachable) objects
  • []Root@mamiLeg []Body []HeadSweep phase[]Leg [][]Bodyunmarkunmark unmarkunmarkfreeFree all dead(unreachable) objectsUnmark all marked objects
  • You can buy a GC book atRubyKaigi!!
  • with autograph :)http://d.hatena.ne.jp/mnishikawa/20100508/1273411900
  • Please dont throw this away
  • Non-recursiveMarking
  • Introduction ofRecursive Marking(a traditional way in CRuby)
  • Recursive MarkingAn object graph Machine Stackgc_mark()gc_mark()gc_mark()Framegc_mark()gc_mark()gc_mark()Recursive call
  • A bad case of asimple recursive call
  • Recursive MarkingA deep object graphgc_mark()gc_mark()gc_mark()Framegc_mark()gc_mark()gc_mark()gc_mark()・・・MaxSuddenlySEGVOverflow!!Machine Stack
  • In order to avoid astack overflow,CRuby adopted ...
  • KnuthsAlgorithmPhoto: http://www.cs.cuw.edu/museum/History.html
  • Whats a Knuths Algorithm?➔To avoid a stack overflow➔There is a fail-safe system whichconsists of two stages.– Using a marking buffer– Rescanning all objects
  • Using a marking bufferAgc_mark()gc_mark()Frame gc_mark()・・・MaxMachine StackBD ECFG・・・Marking bufferB CACBpush pushAvoding overflow!!
  • Marking all objects of themarking buffer at the endof the mark phaseAFrame gc_mark()Machine StackBD ECFG・・・Marking bufferB CACBgc_mark()rescan rescanD E FG
  • How do you deal withan overflow of themarking buffer?
  • Rescanning all objectsABD ECFG・・・Marking bufferS OACBoverflow!!R A HIgnoringrescan rescanrescanD E FGIts very slow!!
  • There are twoproblems
  • 1. fail-safe system is slow➔Rescanning is so slow.– If you have some deep object graphs,GC may be always slow withrescanning.
  • 2. We cant precisely checkstack overflow➔There is a trade-off between speedand precision.– Marking will be slow if we check stackoverflow in each gc_mark().– So we checked it at the appropriatetime.– But, its not precise.
  • 2. We cant precisely checkstack overflow➔This causes SEGV in the worst casescenario– For instance, Fiber sometimes failsunexpectedly.– Fiber uses small machine stack(128 KB)– At times, checking for stack overflowsdoesnt work well with Fiber.
  • So I decided to saygood bye to Knuth
  • Non-recursiveMarking
  • Non-recursive Marking➔Marking w/o the machine stack– w/ own Array based stack➔Recursive => Iterative
  • Rescanning all objectsABD ECFGStack chunk BACBFGmarkCmarkFmarkGmarkmarkMarking stack
  • Allocating newa stack chunkABD ECFGMarking stackXACBFGXmarkX X X X DStack chunkE Allocate!
  • Pros and Cons➔Pros– Good-bye complex fail-safe systems– Good-bye SEGV!➔Cons– Fast enough?– There is a risk of allocating a stackchunk during GC
  • mark benchmark OPTS="-r 5"https://gist.github.com/authorNari/3806667vm3_gc00.10.20.30.40.50.60.70.80.9originnon-recursive
  • bm_gc_deep.rbhttps://gist.github.com/authorNari/3812118depth=240 depth=5000246810121416originnon-recursive(sec)
  • In fact, Ruby 1.9.3has backported thispatch :-)
  • You can buy a GC book atRubyKaigi!!
  • BitmapMarking
  • Bitmap Marking in CRuby➔Mark-bits separate from objectheaders– for CoW friendly➔REE has adopted this approach– Since 2008– But we cant import this patch
  • Whats CopyOnWrite?(Unix)Process 1(P1)Page tableMemory Spacefork()Process 2(P2)Page tableAt first, P1 and P2 usesame memory space.
  • Whats CopyOnWrite?(Unix)Process 1(P1)Page tableMemory Spacefork()Process 2(P2)Page tablewriteP1 private use P2 private usecopy
  • If we have many forked processesProcess 1SharedProcess 2P1 P2Process 3 Process 4 ・・・copyP3 P4write・・・Increase memory usage of all forked processes
  • Marking in the old way… 16KB …Object… 16KB …Object・・・mb mb mb mb mb mb mbRuby HeapHeapBlock 1(HB)HeapBlock 2
  • GC.start… 16KB …Object… 16KB …Object・・・Ruby Heapmb mb mb mb mb mb mbwrite write write writeHeapBlock 1(HB)HeapBlock 2
  • This Marking isCoW not friendly!!Memory SpaceHB1 HB2 HB3Process 1 Process 2GC.start!!write write writecopyHB2HB1 HB3
  • Bitmap MarkingMark-bits are separated from the heap… 16KB …Object… 16KB …Object・・・mb mb mb mb mb mb mbRuby HeapHeapBlock 1HeapBlock 2Bitmapheaderheader
  • This Marking isCoW friendly!!Memory SpaceHB1 HB2 HB3Process 1 Process 2GC.start!!writecopyBMBitmap Bitmapdecrease!!
  • BitmapMarkingmakes prefork serverhappy!
  • e.g. Unicornw/ marking in the old wayMemory SpaceHB1 HB2UP1(parent) UP2(child)GC.start!!read onlywritecopy・・・Rails Rails appread/writeappwrite writeRails Rails
  • e.g. Unicornw/ BitmapMarkingMemory SpaceHB1 HB2UP1(parent) UP2(child)GC.start!!read onlywritecopy・・・Rails Rails appread/writeappwriteRailsBitmap Bitmap
  • How do you find anappropriate bit inBitmap?
  • Finding an appropriatebit for an object… 16KB …HB 1 markBitmapHeader16KB align(low 13 bits must be 0)Allocate a heap block using memory align& ~0x3fffHB1mark…
  • How do you allocatealigned memory?
  • Allocating aligned memory➔Using posix_memalign()– For Unix-like OS➔Using _aligned_malloc()– For Windows OS– mingw: __mingw_aligned_malloc()
  • Allocating aligned memory➔Using malloc()● Thanks to yugui-sans help!– For other environments– For instance, Max OS X Lion and so on– It allocates 32KB and returns an addresswhich is a multiple of 16KB● 16KB memory space is wasted● We should use mmap(), but ....
  • The structure ofRuby Heap waschanged.
  • The structure of Heap in Ruby 1.8ObjectheapsObjectheap block
  • ObjectObjectheap blockheaderheaderslot・・・・・・The structure of Heap in Ruby 2.0heapsslotfreelistfreelistEach slot has a freelist
  • Benchmark
  • skkzipcodehttps://github.com/authorNari/skkzipcodeshared memory private memory050100150200250originbmap(MB)
  • You can buy a GC book atRubyKaigi!!
  • Future
  • Other plans➔Introduce new obj_(alloc/free)events to TracePont.➔mmap()/munmap()
  • rgengcko1-san deserves praise!http://www.flickr.com/photos/recompile_net/4612052730
  • Conclusion
  • Conclusion➔I implemented Non-recursiveMarking and Bitmap Marking.➔Rgengc is so cooooool!
  • Thank you!