Implementation ofRuby 1.9.3
and later
Koichi Sasada
Department of Creative Informatics,
Graduate School ofInformation Science and Technology,
The University of Tokyo1

Today’s topic
 Ruby 1.9.3
 Next and next version of MRI

2

We released!

Ruby 1.9.3…rc13

Yugui-san (release manager)says …
“I will release Ruby 1.9.3 within thistwo weeks unless any serious problem isreported. If you have any trouble withRuby 1.9.3, please let us know.”
It’s means…
4

We need human sacrifice

http://www.flickr.com/photos/babomike/4741964697/5Ruby 1.9.3
New Features

RTFN
Read the F*****g News

6

Ruby 1.9.3 Features
 License Original and (GPLv2 → 2-clause BSDL) Syntax, Methods/Libraries
 Private constants DL/YAML → Fiddle/Psyck
 Test::Unit → Parallel extension (by@sora_h, talking at next room)
 String#prepend, IO.write, etc … (Actually, I don’t know about Ruby world)
7

Implementation ofRuby 1.9.3

Internals
and later
Koichi Sasada
Department of Creative Informatics,
Graduate School ofInformation Science and Technology,
The University of Tokyo
8

Ruby 1.9.3Interpreter internals
 Garbage Collection• Lazy Sweep• Parameter tuning Timer thread implementation• Reducing power consumption
 GVL implementation for multi-core• Solve an problem about non-thread switching on
multi-core9Garbage Collection

http://www.flickr.com/photos/ninithedreamer/4649075746/

10

Introducing
Lazy Sweep GC
 Before 1.9.3: Stop the world mark and sweep
Ruby

MarkSweep

Ruby execution

Stop the (Ruby) World After 1.9.3: Stop the world mark, and incremental sweep
Ruby

MarkSweep

Stop the (Ruby)
WorldShorterstop time

Sweep

SweepSweep

Sweep

Ruby execution withincremental sweep(shorter stopping time)11

Garbage collection
Parameter tuning

Read a source code for details :pQuoted from nari3’s slide
12Recycle Presentation@RubyKaigi2011, again

Ruby 1.9.3
All About Timer Thread(core, details)
Koichi Sasada
Department of Creative Informatics,
Graduate School ofInformation Science and Technology,
The University of Tokyo

13

Power consumption

http://www.flickr.com/photos/livingos/2494500294/

14

Background
Timer Thread?
 Timer thread is an internal mechanism

on Ruby 1.9

 For thread scheduling For signal delivery
Wake up

Wake upTimerthreadtell the timing

Wake uptell the timing

tell the timing

Ruby
thread 1
Pass the GVL

Ruby
thread 2

Pass the GVL

Pass the GV

15

Problem
Power Consumption?
 Timer thread awake every 10ms
 To notify a running thread to release GVL
 Wake-up anytime even if it is not necessary (noscheduling is needed) → CPU can’t sleep enough A few people claimed that the timer thread consumes

power

Wake up

Timerthread

Wake up

10ms

Wake up

10ms

Ruby
thread(sleeping)16

Solution
Rewrite Timer Thread Code Make timer thread more smart
 Timer thread sleeps infinitely if only one

thread is needed Using pipe trick
TimerthreadStart
periodical
wake up

Wake up

Wake up

10ms
tell the timing

Ruby
thread 1
Create new thread

Pass the GVL

Ruby
thread 217

Evaluation (?)
Power consumption
60
59
58

0

1

2

4

8

16

32

64

128

57

W

56
55

1.9.2
proposed

54
53
52
51
50
18

Evaluation
100

90
80
70

0

12

4

8

1632

64

128

60

1.9.2
proposed

50
40
30
20

Reduce 0.3W
on 1 Ruby process

10019

Caution!
Incompatibility Pipe trick needs additional filedescriptors

 You should not close them

Passenger does it!!
 Skip to close such file descriptors w/

new C API “rb_reserved_fd_p()”20

Thread Scheduleron the multi-core

http://www.flickr.com/photos/marksze/4231114748/

21

Recycle Presentation@RubyKaigi2011 by Kosaki-san

Ruby 1.9.3
GVLおよびロックの改善
GVL and Lock improvement
He is aRuby andLinux kernel
committer

Fujitsu
Motohiro Kosaki
22Background again
Only one thread can run w/GVL
 Timer thread is an internal mechanism

on Ruby 1.9

 For thread scheduling For signal delivery
Wake up

Wake upTimerthreadtell the timing

Wake uptell the timing

tell the timing

Ruby
thread 1
Pass the GVL

Ruby
thread 2

Pass the GVL

Pass the GV

23

Doesn’t work some programon Multi-core environment# example
f = false
Thread.new {....;
f = true}
Thread.pass until f24

Why?

25

Good old OS scheduler

Only one Queue

Threadcore

ThreadThreadThreadThreadcore

Threadcore

Quoted from Kosaki-san’s slide
26

New fast OS scheduler

Same number of CPUcores
Threadcore

ThreadThreadThreadcoreThread

Threadcore

ThreadQuoted from Kosaki-san’s slide27

Situation (1)Yield!

Return into Queue

Ruby
Thread１
core

ThreadThread 1

Threadcore

Ruby
Thread２（ ＾ω＾）
I can expect resume!
Quoted from Kosaki-san’s slide28

Situation (2)Head ofQueue
Ruby
Thread１
core

Resumeagain!

ThreadThread 1

Threadcore

Ruby
Thread２(´・ω・｀)
I can’t resume again…
Quoted from Kosaki-san’s slide29

Solution
 We change GVL passing strategy
 Passing GVL definitely Ask me later if you want to know
gvl_acquiremutex_lock(&lock->lock)cond_wait(&lock->wait)
lock->acquired = 1;

gvl_releasemutex_lock(&lock->lock)lock->acquired = 0;
cond_signal(&lock->wait_cond)// notice
cond_signal(&lock->switch_cond)// sleeping until
cond_wait(&lock->switch_cond)mutex_unlock(&lock->lock)mutex_unlock(&lock->lock)
Quoted from Kosaki-san’s slide30Thread related performance

Quoted from Kosaki-san’s slide

31

Futurehttp://www.flickr.com/photos/viernullvier/5698630227/
32

Implementation ofRuby 1.9.3

and later
Koichi SasadaDepartment of Creative Informatics,
Graduate School ofInformation Science and Technology,
The University of Tokyo
33

Ruby 1.9.4? Ruby 2.0?
 Now discussing which version should bereleased• Matz says “Next version is 2.0 including several
new syntax”• keyword argument support for method definitions• Module#mix• Module#prepend• and others (refinement, classbox, or methodshelter?)• Another says “Should be 1.9.4”

34

Ruby 2.0?
What feature do you want?

Teach me here,
raise your hand!
Anything is okay.
35

OKay.
There are manyrequirements.
36

Ask MatzYesterday, I asked Matz what should we do.
He doesn’t want to release 1.9.4 anymore.
He want to say there are no progress in 1.9
series any more, but only in 2.0 series.
If spec is concluded, he shrink 2.0 spec andrelease force.

Please ask Matz tomorrow,
if you have opinion about it.
37

My concerns on next version
 Performance This is why I’m here
 Profiler/debugger interface
 Please teach me what information you
want to know!

 Compiling supports
 I’ll introduce several possibility andactivities about “Ruby in Future”

38Profiling / Debugging
support We add more support forprofiling/debuggin Please teach me whatinformation you want to know!

39

ObjectSpace.reference_from(obj) 2 days ago, I posted a patch ofObjectSpace.reference_from(obj)• I’m sad there are few feedback…
xobjy

ObjectSpace.reference_from(obj)#=>{98752943875 => x,48334232r23 => y}# keys are object id# value are object themselves40Research of my student:
Performance profiler
 ll-prof Real-time profiler for LL languages•Ruby and Python (he is Pythonian) Viewer in JavaScript•You can see results in you browser
TCP/IPRuby

ProfilerCollector

HTTPBrowser
Browser41

Demo(movie made by student)

42Performance Speed VM w/ runtime performance GC performance Memory consumption Power consumption

43Performance
VM w/ runtime speed
 Performance improvements VM
 Easy escape analysis to prevent

generating temporary objects

44Performance
VM w/ runtime speed Semi-automatic Type inference

and translate Ruby code into C(C extension)

45

Student’s research: CastOff
A performance improvement tool for ruby1.9.3(1) Use fromruby script

Programmer

require ‘cast_off’
CastOff.compile(Klass,:Method,binding,TypeInfo)…

(2) Use fromcommand line
$ CastOff“program”
Programmer•Compile
Klass#Method•Load compiled binary•Run faster•Run and Profile“program”•Compile methods
in “program”•Run faster
Quoted from Shiba-san’s slideCompilation flow
Ruby program# Tuning codeclass Sample
CastOff.compile(self, :sample,binding,:f => Foo)
end

Makefile

CodeManager

CSource

Configuration
Compilation
TargetC buildtools

CExtentionCode
GeneratorConfiguratio.conf.conf.confn

Quoted from Shiba-san’s slide

47Performance improvements
Execution time ratio（CRuby 1.9.3 / CastOff）
Guard

faster

90
80
70
60
50
4030
20
100

NoGuard
7

1.2

6

1

50.8

40.6

30.4

20.2

10
bm_sieve.rb bm_lists.rb

0bm_rdoc

Quoted from Shiba-san’s slidePerformance
Garbage collection
 MRI/CRuby’s GC is slow

 Stop the world and mark all Generational?

 Issue: C extension compatibility

 Ideas Parallel GC → Yesterday’s nari3’s talk  Escape analysis and reduce GC

managed objects
 Smart data-structure to reduce GC
managed objects

49Performance
Memory consumption
 One Rails app consume 100MB→ Encourage sharing resources
 such as Strings, Arrays, and so on
 REE/Kiji CoW friendly, generational50

Performance
Power consumption
 Time thread modification at

1.9.3 → small one
 More aggressive way?

51

Performance
Parallel computing

http://www.flickr.com/photos/juror8/394285511/

52

Parallel Execution
 Run threads in parallel (JRuby, MacRuby, …)
 Good: Well known approach Bad: Difficult to make safe/correct

multi-threaded programs• Many tragedy (in Java, etc.)

 Bad: Difficult to make efficient

implementation with fine-grain lock
 Parallel processes (dRuby, …)
 Good: No need to implement
 Bad: Marshal overheads
53Support friendly
Coarse-grained parallel computing

 Encourage Multi-process Traditional well-known approach Toward advanced dRuby

 Multi-VM
 VMs in one process Light-weight communication

54

Student’s reseachTunnel: Inter-process
communication w/shared memory

 Object transfer w/ sharedmemory

Quoted from Nkagawa-san’s slide

55Student reserch (cont.)
Space: Inter-process
Space w/shared memory

 Shared space between rubyprocesses

 Similar to Linda/Rinda

Quoted from Nkagawa-san’s slide

56

Evaluation of Tunnel

Quoted from Nkagawa-san’s slide

57

MVM

A.k.a. vaperware 
We have progress on it, this year.
58

Parallel Execution
Multiple-VM (MVM) on Ruby
 Multiple VMs in one process VMs are completely isolated (Each VM has anindependent object space (heap)) VMs run in parallel• Each VM has own GVL (w/o fine grained lock)

Ruby process
Ruby VM

Ruby VM
GVL

Threa
Threadd
Serial execution w/GVL

GVL

Channel- Passing
References- Memory copy

ThreadThread59

Our Approach
Multiple-VM (MVM) on Ruby
 Channel: Inter-VM Communication

mechanism The only way to communicate with other VMs
 Simply passing references or copying memory in thesame address space
Ruby process

Share the same address space

Ruby VM

Ruby VM
GVL

Threa
Threa
dd
Serial execution w/GVL

GVL

Channel- Passing
References- Memory copy

ThreadThread60

Evaluation
HTML rendering app
 Master dispatch string to worker
and worker returns renderedHTML.
Ruby process

Master VM

StringWorker VM

Rendered
HTML6161

Evaluation
HTML rendering app
98Speedup ratio

765Processes+pipe

4MVM+channel
32100

5101520# of VMs/Processes

2530

62

Evaluation
DB app
 Benchmark assuming web application
 Several front-end VMs and one DB VM
 YUBIN-Number (zip-code) DB on memory

 Using dRuby (w/MVM) framework

Ruby process
Front-end VM

63QueryString(YUBIN#)
ResultString(Address)

DB VMOn memory
YUBIN# DB63

Evaluation
DB appQuery/sec64

Future work Extend this communication channel between inter-process (w/shared memory), inter-node Migratable Ruby activity (threads, blocks (closures) and so on)
Node

Node

Ruby Process

Ruby Process

VM

ThreadVM

ThreadChannelVM

ThreadThreadVM

ThreadChannelThreadThreadThreadChannelRuby Process
VM

ThreadThreadVM

ThreadThread(Abstract)
Shared Object Space
sharedobjectsharedobject65

Summary Ruby 1.9.3 will be released soon!
 Be Sacrifice!

NEWS file and blog posts are good to knowchanges
This talk introduces background on Ruby 1.9.3

 We are continuing the hacking to the future
Stay tuned to the next announcement
66

おしまい
Thank you for your attention

Implementation ofRuby 1.9.3
and later
ささだこういちko1@atdot.net, ko1@rvm.jp
67