Share
Embed
Matthias Grimmer on "One Vm to rule them all"
Enterprise Java User Group Austria
March 10, 2015
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
56 slides
-
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
One VM To Rule Them All! And With Interoperability Bind Them Matthias Grimmer, @grimmer_m, JKU Linz Thomas Wuerthinger, @thomaswue, Oracle Labs -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
2 Let’s ask a search engine… One Language to Rule Them All? -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
3 One Language to Rule Them All? Let’s ask Stack Overflow… -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
5 3 Goal: One VM for all languages means interoperability and being able to choose the best language for the task! Lower is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
6 You can execute any language on the JVM / CLR - as long as it looks like Java / C#. -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
Everything is a dynamic map Everything is a map and every operation a function call Everything is a map and every operation a function call and everything is a vector C Everything is an address -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
8 Parser and language work to build syntax tree (AST), AST Interpreter Write a “real” VM In C/C++, still using AST interpreter, spend a lot of time implementing runtime system, GC, … Define a bytecode format and write bytecode interpreter People complain about performance Write a JIT compiler Improve the garbage collector Performance is still bad Prototype a new language -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
9 Parser and language work to build syntax tree (AST), AST Interpreter Write a “real” VM In C/C++, still using AST interpreter, spend a lot of time implementing runtime system, GC, … People start using it Define a bytecode format and write bytecode interpreter People complain about performance Write a JIT compiler Improve the garbage collector Performance is still bad Prototype a new language -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
10 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Managed Host Language or Unmanaged Language Unmanaged Language (typically C or C++) Managed Host Language Written in: -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
11 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
12 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle int main() { struct complex *a = …; struct complex *b = …; add(a, b); } -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
13 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle int main() { struct complex *a = …; struct complex *b = …; add(a, b); } -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
14 Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle C -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
15 a + b Object int String -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
16 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
17 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } int add(int a, int b) { return a + b; } -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
18 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } int add(int a, int b) { return a + b; } String add(String a, String b) { return a + b; } -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
19 Object add(Object a, Object b) { if(a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } Object add(Object a, Object b) { return genericAdd(a, b); } int add(int a, int b) { return a + b; } String add(String a, String b) { return a + b; } -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
20 U U U U U I I I G G I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Compilation using Partial Evaluation Compiled Code Node Transitions S U I D G Uninitialized Integer Generic Double String -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
21 I I I G G I I I G G Deoptimization to AST Interpreter D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
Graal VM § A new JIT compiler for Java written in Java. § Designed for multi-language performance. § Downloadable from the Oracle Technology Network (OTN) with Java and JavaScript support. C -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
25 Graal Truffle R Ruby Java Scala Smalltalk C J Python JavaScript Parallel Graph AnalyLcs C -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
26 Performance – Java and Scala 0 0.5 1 1.5 2 2.5 Java Scala server graal DaCapo benchmarks - Normalized versus client compiler Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
27 0 1 2 3 4 5 6 7 V8 Graal/JS Performance – JavaScript Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
28 Performance – C 0 50 100 150 200 250 300 350 400 450 500 Composite NB FA SN FK MB FFT SOR MC SM LU GCC O0 GCC best TruffleC Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
33 CRuby FFI NOT SUPPORTED -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
34 1 typedef VALUE void*; 2 typedef ID void*; 3 4 // Define a C function as a Ruby method 5 void rb_define_method 6 (VALUE class, const char* name, 7 VALUE(*func)(), int argc); 8 9 // Store an array element into a Ruby array 10 void rb_ary_store 11 (VALUE ary, long idx, VALUE val); 12 13 // Get the Ruby internal representation of an 14 // identifier 15 ID rb_intern(const char* name); 16 17 // Get instance variables of a Ruby object 18 VALUE rb_iv_get(VALUE object, 19 const char* iv_name) 20 21 // Invoke a Ruby method from C 22 VALUE rb_funcall(VALUE receiver ID method_id, 23 int argc, ...); 24 25 // Convert a Ruby Fixnum to C long 26 long FIX2INT(VALUE value); Well designed API? è Internal functions of CRuby -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
35 CRuby MRI API *.rb *.c executes extension -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
36 VALUE rb_ary_write(VALUE ary, long idx, VALUE value); rubyArray = Array.new 42 #include “ruby.h” // … rb_ary_write(rubyArray, 0, DBL2NUM(42)); // … TruffleRuby Java *.rb executes Graal/Ruby *.c TruffleC Java executes Graal/C -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
37 VALUE rb_ary_write(VALUE ary, long idx, VALUE value); *.c TruffleC Java executes TruffleRuby Java *.rb executes rubyArray = Array.new 42 #include “ruby.h” // … rb_ary_write(rubyArray, 0, DBL2NUM(42)); // … rubyArray.[] = 0, 42 Graal/Ruby Graal/C -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
38 1x 8x 11x 0 5 10 15 20 25 30 35 CRuby Graal/Ruby CRuby+C Graal/Ruby+C Image Processing Composite Speedup Pure Ruby Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
39 1x 8x 11x 0 5 10 15 20 25 30 35 CRuby Graal/Ruby CRuby+C Graal/Ruby+C Image Processing Composite Speedup Pure Ruby Ruby + C Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
40 1x 8x 11x 32x 0 5 10 15 20 25 30 35 CRuby Graal/Ruby CRuby+C Graal/Ruby+C Image Processing Composite Speedup Pure Ruby Ruby + C Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
42 Let’s write a small part of a program in a different language! Ø There is no interface to the language I would need Ø I don’t want to write a lot of wrapper code Ø Performance will be bad #include<stdio.h> struct complex { double r; double i; } int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } struct complex *a struct complex *b -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
43 def function(v1, v2) sum = [] for i in 0..n do sum[i] = v1[i] + v2[i] end end function(v1, v2) { v1 + v2 } v1 v2 Let’s write a small part of a program in a different language! Ø There is no interface to the language I would need Ø I don’t want to write a lot of wrapper code Ø Performance will be bad -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
44 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts Gradually migrate existing projects § Programmers need to write wrapper code Reusing existing source code § Language borders introduce a performance bottleneck -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
45 #include<stdio.h> struct complex { double r; double i; } int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } add(a, b) a->r b->r a->i b->i main.c complex.js mx graalML main.c complex.js -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
46 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts Gradually migrate existing projects § Programmers need to write wrapper code Reusing existing source code § Language borders introduce a performance bottleneck -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
47 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects § Programmers need to write wrapper code Reusing existing source code § Language borders introduce a performance bottleneck -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
48 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects § Programmers need to write wrapper code è Without writing boilerplate code Reusing existing source code § Language borders introduce a performance bottleneck -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
49 Programmers want to write programs in multiple languages Use the most suitable language for a given problem § Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects § Programmers need to write wrapper code è Without writing boilerplate code Reusing existing source code § Language borders introduce a performance bottleneck è Cross-language inlining -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
High-Performance Language Interoperability 0 200 400 600 800 1000 1200 Ruby data JS data C data Ruby program JS program C program C Scores for mixing Ruby/JS/C executing the scimark benchmarks. Higher is better -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
§ 51 § Graal VM Binaries for Java and JavaScript! http://www.oracle.com/technetwork/oracle-labs/program-languages § Graal and Truffle API! http://openjdk.java.net/projects/graal/ § R! https://bitbucket.org/allr/fastr § Ruby! https://github.com/jruby/jruby/wiki/Truffle § Python! https://bitbucket.org/ssllab/zippy § Other projects for Smalltalk, Clojure, Lua, … -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
52 Your Language? http://openjdk.java.net/projects/graal/ [email protected] $ hg clone hZp://hg.openjdk.java.net/graal/graal $ cd graal $ ./mx.sh -‐-‐vm server build $ ./mx.sh ideinit $ ./mx.sh -‐-‐vm server uniZest SumTest -
Copyright © 2015 Oracle and/or its affiliates. All rights reserved.
53 Safe Harbor Statement The preceding is intended to provide some insight into a line of research in Oracle Labs. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle.