ORACLC Polyglot programming in with Tooling, AOT compilation, and Embedding Petr Chalupa Principal Member of Technical Staff Oracle Labs November 28,2019 Graal Safe harbor statement The following is intended to outline our general product direction. 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, timing, and pricing of any features or functionality described for Oracle' products may change and remains at the sole discretion of Oracle Corporation. GraalVM Native Image technology (including SubstrateVM) is early adopter technology. It is available only under an early adopter license and remains subject to potentially significant further changes, compatibility testing and certification. Copyright © 2019 Oracle Program agenda 1 GraalVM pg|| 2 Truffle brainfuck Polyglot 'J3V8 gQ) fortress _ Embedding and native image generation ~g FII1111 | Instrumentation and Tooling ClOJUFG 6 Language status pr£l0flr,.JL CD M gr C++111 5S 4- 3 4 5 apl 4 Copyright © 2019 Oracle o 5 Copyright © 2019 Oracle What is GraalVM? • Drop-in replacement for Oracle Java 8 and Java 11 - Run your Java application faster • Ahead-of-time compilation for Java - Create standalone binaries with low footprint • High-performance JavaScript, Python, Ruby, R,... - The first VM for true polyglot programming - Implement your own language or DSL 6 Copyright © 2019 Oracle What is Graal? • A Java just in time compiler - At runtime, translates Java bytecodes into machine code • Modern design, implemented in Java - Maintainable - Extendable • Fully Java compliant => production ready • High performance 7 Copyright © 2019 Oracle o Java" Jscala -^gT J 5 ^JRuby 4 * 4 Automatic transform of intert )reters to compiler Graal Engine integration native and managed 4 Open 4 4 4 oracle; DATABASE My 4 KACLG' standalone NETSUITE 8 Copyright © 2019 Oracle o GraalVM Architecture tí Ruby Ml Java Scala python Oc Sulong (LLVM) Truffle Framework Graal Compiler JVM Compiler Interface (JVMCI) JEP 243 Java HotSpot Runtime Substrate VM 9 Copyright © 2019 Oracle O Community Edition Enterprise Edition GraalVM Community is available for free for evaluation, development and production use. It is built from the GraalVM sources available on . We provide pre-built binaries for Linux, macOS X, and Windows platforms on x86 64-bit systems. Windows support is GraalVM Enterprise provides additional performance, security, and scalability relevant for running applications in production. It is free for evaluation uses and available for download from the We provide binaries for Linux, macOS X, and Windows platforms on x86 64-bit systems. Windows support is FREE on Oracle Cloud! 10 Copyright © 2019 Oracle o GraalVM Open Source Open Source LOC actively maintained by GraalVM team core: 1420K compiler: 500K truffle: 300K substrate: sulong: other: 270K 200K 150K Total: 3,640,000 lines of code 11 Copyright © 2019 Oracle Twitter uses GraalVM compiler in production to run their Scala microservices :hafv«t u» • • • *■ ■ ^ b ft"' TWEET SERVICE: LATENCY P99 MM 28 ON * a a a a VO/\ED vYS 12 Copyright © 2019 Oracle o Cloud Infrastructure • Peak performance: +10% • Garbage collection time: -25% • Seamless migration GraalVM EE 19.1 0015 OO.X 002i 00» 00» 0040 00:« 00.50 00» 13 Copyright © 2019 Oracle o 58^ nviDiA The rich ecosystem of CUDA-X libraries is now available for GraalVM applications. GPU kernels can be directly launched from GraalVM languages such as R, JavaScript, Scala and other JVM-based languages. 14 Copyright © 2019 Oracle Performance: GraalVM Summary ), higher is better Ruby R Native JavaScript Performance relative to: HotSpot/Server, HotSpot/Server running JRuby, GNU R, LLVM AOT compiled, V8 15 Copyright © 2019 Oracle 16 Copyright © 2019 Oracle O Truffle • Self-optimizing abstract tree interpreter - Rewrites itself • The simplest way to implement a language • A Java library - Node classes - Execute methods 18 Copyright © 2019 Oracle Speculate and Optimize... 19 Copyright © 2019 Oracle and Transfer to Interpreter and Reoptimize! 20 Copyright © 2019 Oracle Truffle cOGenerateUncached public abstract class IsFrozenNode extends RubyBaseWithoutContextNode { Si; public static IsFrozenNode c return IsFrozenNodeGen.create } public abstract boolean e te(Object object); ^Specialization protected boolean isFrozen(boolean object) { return true: } ^Specialization protected boolean isFrozen(int object) { return true: } ^Specialization protected boolean isFrozen(long object) { return true: } ^Specialization protected boolean isFrozen(double object) { return true: } ^Specialization protected boolean isFrozen( DynamicObject object, acached ReadObjectFieldNode readFrozenNode) { return (boolean) readFrozenNode.execute(object, Layouts. FROZ£N_ IDENTIFIER, false); 21 Copyright © 2019 Oracle o Polyglot use-cases • Missing library - You are not limited to libraries only written in your language of choice • Legacy code - Migration of legacy project from language A to B can be gradual • Sharing code - Share domain logic between frontend (JS) and server code (Java, Ruby, • Execute on GPU • User scripting - Run your user provided scaling rules - Run stored functions in DB - And more 23 Copyright © 2019 Oracle Web application demo - Ruby JavaScript frontend Ruby backend - In-memory database Shared domain logic written in JavaScript - No duplication - Identical behavior - The same validation code runs in the webserver and on the server Ruby server JS domain logic JS frontend JS domain logic Shared source code 24 Copyright © 2019 Oracle O Web application demo - Ruby • Single table of people - With first name, last name, and age • A form to add new person - With immediate validations 9 Q People demo <- -> C © localhost:4567 x + People First name Last name Age John Doe 50 Adam Bojacny 25 first name last name age _ Add person Add invalid person Quit the application. 25 Copyright © 2019 Oracle O Web application demo - The shared JS domain function n(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = Number.p t(age); this.errors = {}; if (!this.firstName) { this.errors.firstName = 'cannot be empty'; } if (!this.lastName) { this.errors.lastName = 'cannot be empty'; } // ... age validation skipped (Integer, -1 > age < 151) Object.f e(this); } i.prototype.: = function () { return Object.k ?ys(this.errors).length; }; 26 Copyright © 2019 Oracle o Web application demo - The Ruby server r Load and evaluate JS file J class PolyglotApp < Sinatra::Base # ... configuration skipped Polyglot.eval_file('js', File.join(File.dirname(__FILE__); 'public', 'person.js')) JsPerson = Polyglot.eval('j s', 'Person') PEOPLE_DB = [] def self.i PEOPLE_DB.clear PEOPLE_DB.push JsPerson./ ('John', 'Doe', 50) end Get the defined class by evaluation and store it in Ruby constant _J initialize db reate new instance of Person defined in JS 27 Copyright © 2019 Oracle Web application demo - The Ruby server get '/' do File.read(File.join(self.class.public_folder, 1index.html')) end d e f h(js_user) _ { firstName: js_user[:firstName], lastName: js_user[:lastName], —I end get 1/people.json1 do data = PEOPLE_DB.map { \js_user\ js_person_as_hash(js_£/S£,Jr) } data.to_json end age: 28 Copyright © 2019 Oracle Web application demo - The Ruby server post 1/person.json1 do person_json js_person js_person_is_invalid JSON.parse request.body.read JsPerson.fromObject OpenStruct./ person_json js_person.islnvalid if js_person_is_invalid > 0 [403, 'Invalid user.'] else PEOPLE_DB.push js_person [200, 'Ok'] end end L Call function on a JS object Creates new instance j L Call function on a JS object. Checks the validity. 29 Copyright © 2019 Oracle Language independent interoperability messages • Messages - Array like ° HAS_SIZE, GET_SIZE, READ, WRITE - Members ° HAS_KEYS, KEYS, INVOKE, READ, WRITE, - etc. • Each language defines its behavior for these messages only - Otherwise there would be language combination explosion - Otherwise adding new languages would be hard 31 Copyright © 2019 Oracle Idiomatic syntax translates to interoperability messages js_function = Polyglot.eval(1js1, -JS) function(arg) { return [1,2,3,5,8].slice(0,arg); } JS js_array = js_function.call 4 # EXECUTE puts js_array.size # ==> 4 # GET_SIZE puts js_array[3] # ==> 5 # READ JavalntArray = Java.type 'int[]1 java_int_array = JavalntArray.new 1 # NEW puts java_int_array.size # => 1 # GET_SIZE java_int_array[0] = 42 # WRITE puts java_int_array[0] # ==> 42 # READ 32 Copyright © 2019 Oracle Explicit message usage when necessary • It's not possible to get a size of an array in C • Therefore there is always a way to call the messages explicitly ■ bool polyglot_has_array_elements(const void *value); - uint64_t polyglot_get_array_size(const void *array); - array[idx]; 33 Copyright © 2019 Oracle High-Performance Language Interoperability var a = obj.value; value JS-specific object access 34 Copyright © 2019 Oracle High-Performance Language Interoperability var a = obj.value; obj is a Ruby object Map message to Rb access Dynamic Compilation C-specific object access C-specific and Rb-specific object accesses 35 Copyright © 2019 Oracle Polyglot performance • Uses the same AST rewriting framework • Optimizes through the language barrier • The foreign language nodes make no difference to Truffle • It can optimize as well as if it were nodes from the host language - Same inline cache principle as for regular method calls 36 Copyright © 2019 Oracle o Using Numpy from Java - Calling into Numpy try (Context context = Context.newBullder() .option("python.PythonPath", "/path/to/numpy-1.16.4-py3.7-macosx-10.14-x86_64.egg") . allowAHAccess (true) . build ( ) ) { Value geomean = context.eval("python", "import numpy\n" + "import math\n" + "lambda x: math.pow(numpy.array(x).prod(), 1/len(x))"); anonymous function that calculates the geometric mean using numpy and the math module double[] values = new double[] { 1, 5, 8, 3, 5, 8, 8, 1, 5, 6 }; double mean = geomean.execute(values).asDouble(); System.out.println(mean); // 4.905181164183902 } 38 Copyright © 2019 Oracle o Using CUDA to Access Nvidia GPUs • Different binding libraries / APIs for CUDA in different programming languages • Varying set of supported features • Translation to/from unmanaged environment (in Java, C#, Python, etc.) Python Numba, cuPy, PyCUDA Java JCuda, jCUDA, CUDA4J C/C++ CUDA C/C++ (language extension) R gpuR, indirectly through Repp JS gpu.js (WebGL), node-cuda, cuda-ts C# Hybridizer, ManagedCUDA, Alea GPU, ILGPU Ruby 39 Copyright © 2019 Oracle o Using grCUDA to Access Nvidia GPUs • Efficient exchange of data between host language and GPU without burdening the programmer • Expose GPU resources in ways that are native in the host language, e.g., as arrays • Allow programmers to invoke existing GPU code from their host language • Allow programmers to define new GPU kernels on the fly • Polyglot interface: uniform bindings across all programming languages • Implemented as a "Truffle Language" (although "CUDA" is a platform, not a language) Developed by NVIDIA in collaboration with Oracle Labs BSD 3-clause license GraalVM TM <|gjava™ ÄRuby I* python 40 Copyright © 2019 Oracle o Creating and Using Device Arrays (Python) import polyglot # Get constructor function as callable DeviceArray = polyglot.eval(language='grcuda', string='DeviceArray') # Create ID device array that can hold 1000 int values dev_int_arr = DeviceArray('int', 1000) # Create 2D device array that can hold 1000 x 100 float values dev_float_2d = DeviceArray('float', 1000, 100) # Setting array elements ax x j.i for i in range(len(dev_int_array)): dev_int_arr[i] = i for i in range(len(dev_float_2d)): for j in range(len(dev_float_2d[0])): dev_float_2d[i][j] = i + j As fast or faster than native language 41 Copyright © 2019 Oracle GPU Kernels in CUDA C++ global_ void inc_kernel(float *out_arr, const float *in_arr, size_t num_elements) { for (auto idx = blockldx.x * blockDim.x + threadldx.x; idx < num_elements; idx += gridDim.x * blockDim.x) { out_arr[idx] = in_arr[idx] + 1; } Block 0 Block 1 Block 2 Number of blocks and threads can Thread i be configured in 3 dimensions (x, y, z) blockDim D v y ' <-► _gridDim_ 42 Copyright © 2019 Oracle o Launching GPU Kernels (JS) const DeviceArray = Polyglot.eval('grcuda', string='DeviceArray') const N = 1000 const in_arr = DeviceArray('float', N) const out_arr = DeviceArray('float', N) for (let i = 0; i < N; i++) in_arr[i] = i const code = '_global_ void inc_kernel(...) const buildkernel = Polyglot.eval('grcuda', string='buildkernel') const incKernel = buildkernel(code, 'inc_kernel', 'pointer, pointer, uint64') // Launch kernel in grid consisting of 160 blocks with 256 threads each incKernel(160, 25 6)(out_arr, in_arr, N) for (let i = 0; i < 10; i++) { console.log(out_arr[i]); } 43 Copyright © 2019 Oracle Embedding and native image generation 44 Copyright © 2019 Oracle GraalVM Architecture tí Ruby Ml Java Scala python Oc Sulong (LLVM) Truffle Framework Graal Compiler JVM Compiler Interface (JVMCI) JEP 243 Java HotSpot Runtime Substrate VM 45 Copyright © 2019 Oracle O ^ Java Scala -#ÄBT - Kotlin (W jiT GraalVM™ A0T $ Java MyMainClass $ native-image MyMai Q_gP $ ./mymainclass Copyright © 2019 Oracle Startup Speed Currently Memory Footprint AOT Packaging Size Peak Throughput Max Latency ■ > 47 Copyright © 2019 Oracle Native Image: Principle Input: All classes from application, libraries, and VM Application Libraries Substrate VM ■ 49 Copyright © 2019 Oracle Initialize application Output: Native executable h Benefits of the Image Heap Without GraalVM Native Image Compile Sources I GraalVM Native Image (default) Build time Load Classes i Run time Build time ompile Sources j Run time Load Configuration File i Run Workload Load Configuration File I Run Workload GraalVM Native Image: Load configuration file at build time Compile Sources i Build time Run time Load Configuration File I Run Workload 51 Copyright © 2019 Oracle Startup Time of Java Microservice Frameworks Memory Footprint Architecture of Node.js running via GraalVM node modules with native node modules with only JavaScript extensions node standard library native extensions node bindings (socket, http, etc) V8 API thread event pool loop (libeio) (libev) DNS crypto (c-ares) (OpenSSL) Adapter V8 API to Graal.js via JNI GraalVM JavaScript Engine £f Ruby * python ^ A lava~ * ď Try Database in the Cloud for Free See what you can accomplish with US$300 in free credits. module.exports.helloworld = function () { return "Hello World"; } export function helloworld():string; shell> dbjs deploy helloworld.js sql> SELECT helloworldQ from dual; https://www.orac1e.com/technetwork/ database/multilingual-engine/ 55 Copyright © 2019 Oracle Embedding small JS snippet import org.graalvm.polyglot.*; //■■■ public static void main(String[] args) throws Java.io.IOException { System.out.println("Walking path: " + Paths.get(args.length > 0 ? args[0] : ".")); final Context context = Context.create("js"); final Value jsFn = context.eval("js", "function(name, size) { return name + ': ' + size}"); try (Stream paths = Files.walk(Paths.get("."))) { paths.filter(Files::isRegularFile). forEach((Path p) -> { File f = p.toFileQ; Value v = jsFn.execute(f.getName() f.length()); System.out.println(v); }); } } 56 Copyright © 2019 Oracle o Embedding small JS snippet 57 Copyright © 2019 Oracle Embedding small JS snippet fish /Users/p 3. fish /Users/pitr/Workspace/labs/graalvm-demos/native-list-dir (fish) Build on Se [extlistdir [extlistdir [extlistdir Warning: De t_java_nio_ rayBaseOffs cted automa cally detec [extlistdir [extlistdir [extlistdir [extlistdir 7022 method [extlistdir [extlistdir [extlistdir [extlistdir [extlistdir [extlistdir [extlistdir [extlistdir rver(pid: :13992] :13992] :13992] tected un DirectByt et. The a tically. ted subst :13992] :13992] :13992] :13992] (s) inclu :13992] :13992] :13992] :13992] :13992] :13992] :13992] :13992] 13992, port: 51063)* classlist: 4,906.11 ms (cap): 1,573.08 ms setup: 9,185.80 ms necessary Recompute FieldValue.ArrayBaseOffset com.oracle.svm.core.jdk.Targe eBuffer.arrayBaseOffset substitution field for java.nio.DirectByteBuffer.ar nnotated field can be removed. This ArrayBaseOffset computation can be dete Use option -H:+UnsafeAutomaticSubstitutionsLogLevel=2 to print all automati itutions. (typeflow): 24,695.47 ms (objects): 51,975.49 ms (features): 5,307.08 ms analysis: 99,034.79 ms ded for runtime compilation universe: 4,572.60 ms (parse): 8,670.41 ms (inline): 13,117.23 ms (compile): 70,414.10 ms compile: 124,462.72 ms image: 14,027.42 ms write: 5,549.62 ms [total]: 296,971.65 ms 58 Copyright © 2019 Oracle Embedding small JS snippet • time graalvm-ee-1.0.0-rc6/.../bin/java ExtListDir • time ./extlistdir Time (lower is better) jvm °'04 0,01 0,01 native i real auser «sys 59 Copyright © 2019 Oracle Embedding small JS snippet • extlistdir is just 72MB • It is a native image without dependencies on JVM • It has the JS engine in it • It executed almost as fast as native Is command 60 Copyright © 2019 Oracle Debugging with Chrome DevTools • Run the server with —inspect option - ruby --polyglot --experimental-options --ruby.single-threaded --inspect app.rb • Follow the instructions and open the chrome-devtools: link - Step through the languages 62 Copyright © 2019 Oracle o • Q chrome-devtools://devtools/L x + <- C © Not Secure I chrome-de\rtools://devtools/bundled/js_app.html?ws=127.0.0.1:9229/383534aa-66f426a6837cb * :■ B Doc I Console Sources Filesystem Snippets 5 Labs Memory I Work | Profiler + Add (older to workspace demo ► public || Gemfile ^ Gemfile.lock apo.rb h demo.iml ^ examples.rb || single threaded.rb optcarrot truffleruby | Console [0 ® 0 Main Context O 4, □ * P * K I Sessions | Other Bookmarks [0 app.rb x base.rb 1 require 'pp' 2 require 'sinatra/base' 3 require relative 'single_threaded' A 5 # noinspection RubyConstantNamingConvention.RubyParenthesesAfterMethodCalllnspection 6 class PolyglotApp < Sinatra::Base 7 8 enable :static 9 set :server, 'webrick' 10 disable :logging 11 12 if defined? Polyglot 13 person_definition_path = File.join File.dirname(_FILE_), 14 'public', 'person.js' 15 Polyglot.eval_file( , person_definition_path) 16 JsUser Polyglot.eval('js', 'User') 17 PE0PLE_DB (JsUser.new< John', 'Doe', 59)] IS else 19 PE0PLE_DB (] def js_user_as_hash(js_user) [ifirstNaire, : lastNatie, : age]. reduced■}) 1 hash-update key js_user[keyr 5 end 5 end File.read(File.join(self.class.public_folder, 'index.html")) data - PEOPLE_DB.irap js_user js_user_as_hash(j s_user) end data.to_j son post '/person.json' do request.body.rewind # in case soreone already read it person = JSON.parse request.body.read defined1 Polyglot js_user - JsUser.fromDbject OpenStruct.new person {} Line 24. Column 1 Default levels » Q Group similar EE Rack::Head«call Si natra:: ShowExcepti ons#cal I show.exceptlorts. rb:22 Sinatra::Ex:encedRack*cail base.rb:194 Sinatra::Wrapper#call base.rb:1958 block in Sinatra::Base.call base.rb:1502 Sinatra::Base.synchronize base.rb:1729 Sinatra::Base.call base.rb:1502 Rack::Handler::WEBrick#service webrick.rb:86 WEBrick::GenericServer«start thread single threaded.rb:93 block {2 levels) in WEBrick::GenericSeiver#start single threaded.rb:39 block in WEBrick::GenericServerf»siart single threaded.rb:33 WEBrick::GenericServerSstart sir>glejhreaded.rb:12 webrick. rb:34 base.rb: 1525 base.rb: 1459 app.rb: 62 Rack::Handler::WEBrick.run Sinatra::Base.start server Sinatra:: Base, run:
t Scope »Local ►(self): PolyglotApp #"John"} ► key: Symbol :lastNaire ► rubytruffle_temp_destructure_127: NilClass ni ► Global global » Breakpoints Q app.rb:24 hash.update key => js_user[key] X * I 63 Copyright © 2019 Oracle CPU-Sampler • Let's introduce extra slowness affecting /people. j s request • CPU-Sampler can tell us which methods take the most time -ruby --jvm --polyglot -Xsingle_threaded --cpusampler app.rb 65 Copyright © 2019 Oracle o Line: 1 Plain Text 0 Soft Tabs: 4 v # 0 66 Copyright © 2019 Oracle o CPU-Sampler in Chrome DevTools 9 Q chrome-devtools://devtools/b X + -> C © Not Secure | chrome-devtools://devtools/bundled/js_app.html?ws=127.0.0.l:9229/7b33... -fr Console Sources Memory Profiler • i _ Profiles Heavy (Bottom Up) ▼ & X C Self Time CPU PROFILES 5688.0 ms 51.36 % 183.0 ms 1.65% 63.0 ms 0.57% 53.0 ms 0.48% 52.0 ms 0.47% 39.0 ms 0.35 % 30.0 ms 0.27% 18.0 ms 0.16% 16.0 ms 0.14% 13.0 ms 0.12% 13.0 ms 0.12% 13.0 ms 0.12% 12.0 ms 0.11 % 11.0 ms 0.10% 9.0 ms 0.08 % 8.0 ms 0.07% 7.0 ms 0.06% 6.0 ms 0.05 % 6.0 ms 0.05 % Total Time 6315.0 ms 57.03% 251.0 ms 2.27% 558.0 ms 5.04% 53.0 ms 0.48% 52.0 ms 0.47% 40.0 ms 0.36% 42.0 ms 0.38% 18.0 ms 0.16% 16.0 ms 0.14% 13.0 ms 0.12% 65.0 ms 0.59 % 14.0 ms 0.13% 20.0 ms 0.18% 41.0 ms 0.37% 13.0 ms 0.12% 6323.0 ms 57.10% 11.0 ms 0.10% 380.0 ms 3.43% 423.0 ms 3.82 % Function ► WEBrick::GenericServer#start_thread ► PolyglotApp#GET /people.json ► Rack::Handler::WEBrick#service ► Timeout#timeout ► block in PolyglotApp#js_person_as_hash ► Sinatra::Base#root ► PolyglotApp#GET / ► block in Rack::Handler::WEBrick#service ► WEBrick::HTTPResponse#setup_header ► Rack::Protection::PathTraversal#cleanup ► PolyglotApp#js_person_as_hash ► Rack::Protection::JsonCsrf#has_vector? ► Sinatra::Helpers#content_type ► Sinatra::Base#static! ► Rack::Protection::Base#html? ► block (2 levels) in WEBrick::GenericServer#start ► Mustermann::Pattern#params ► Sinatra::Base#invoke ► Sinatra::Base#call! single threaded.rb:; app.rb:: webrick.rb:! single threaded.rb:i: app.rb:: base.rb:18: app.rb:: webrick.rb:! webrick.rb:' path traversal.rb:i app.rb:: ison csrf.rb:: base.rb:3-base.rb:10( base.rb:i: single threaded.rb:: pattern.rb:2( base.rb:10" base.rb:9' 67 Copyright © 2019 Oracle CPU-Tracer Visual Studio Code Plugins l>3 Visual Studio 1 Marketplace Sign in Visual Studio Azure DevOps Subscriptions Build your own Publish extensions graalvm X 5 Results Showing: All categories v Sort By: Relevance *» Graal Graal R Graal 1» Graal •» Graal GraalVM Oracle Labs Gras ^100 Basic support for GraalVM GraalVM R Oracle Labs Graal = 37 Support for GraalVM R GraalVM Python Oracle Labs Graa ^76 Support for GraalVM Python GraalVM Ruby Oracle Labs Graa ^ 38 Support for GraalVM Ruby GraalVM Extension I Oracle Labs Grae ,423 VS Code extensions for GraalVM based development. FREE FREE FREE FREE FREE https://marketp1ace.visua1studio.com 69 Copyright © 2019 Oracle O Instrumentation • One API built on top of rewriting AST - Languages need to only implement the instrumentation API ° Minimal language support requirements - A language gets all the tools for free - Tools work for all languages • Languages tags nodes - Expression, Call, Root, etc. • Tools then can request to subscribe to events on tagged nodes • Close to zero overhead when not used 71 Copyright © 2019 Oracle Instrumentation r Tagged AST node to ne instrumented Wrapper node: - Language specific - Reports events AST rewrite Probe node: - Propagates events - Manages tool's subscriptions 72 Copyright © 2019 Oracle Implementing difficult language features • Instrumentation is also used by the languages themselves • R stepping • Rubyset_trace_func - Attaches a function which gets executed on each: call, line,... 74 Copyright © 2019 Oracle Graal VisualVM • Graal VisualVM has special support to understand Ruby, JS heap • Compute retained sizes - The size including internal structures ° String-> Rope -> byte[] • Let's create 300 extra people in the in-memory database - http://localhost:4567/add/500 - We can lookup the array and its size (0.1%) 76 Copyright © 2019 Oracle • <ä % &J Ěl f5 Graal VisualVM 20180807-unknown-revn Applications T gl Local UvtsualVM S IntelliJ Platform tpid 91601) ▼ Rubytpid 76376) S3 [heapdump] 11:30:57 AM •SP Remote Cfo VM Coredumps @ Snapshots [3 Overview Q Monitor § Threads Xfc Sampler O Profiler [heapdump] 11:30:57 AM C Ruby (pid 76376) Heap Dump Objects - Name Preset: All Objects w Aggregation: ^, References ^ Java Object Retained ▼ ► B Class 2,068 (0.1*) 203,608 B (0.1%) 3,437,903 B (1.950 ► B String 7,232 (0.350 694,272 B (0.459 1,303,434 B (0.750 Module 193 (0%) 18,800 B (05í) 1,248,656 B (0.750 ► B Hash 215 (0%) 160,824 B (0.150 1,116,858 8 10.6%) Regexp 658 (0%) 63,168 B (050 867,816 B (0.550 ▼ 19 Array 1,064 (0%) 170,264 B (0.150 548.198 B (0.350 (050 100,568 B ▼ i ^ store (hidden) = 81java.lang.Objectl]#3568 4,120 B (050 100,472 B ^3 size (hidden) = □ object 301 - - ► ► : Array#1002 : 231 items 2,168 B (050 71,517 B (050 ► 1) Array* 15144 2 items 136 B (050 33,244 B (050 ► "b Array#1699 : 2S6 items 2,168 B (050 26,744 B (050 All Objects > tu Array > Array#14300 r/ Type Filter: TF.Iter o a Variables Name ■ja size (hidden) = □ object 301 ^1 store (hidden) = 11 java.lang.Object[]#3 5686 512 iten JSUserObject#14301 JSUserObject#17343 JSUserObject#17341 JSUserObject#17339 JSUserObject#17337 JSUserObject#17335 JSUserObject#17333 JSUserObject#17331 shape #4243 shape #4243 shape #4243 shape #4243 shape #4243 shape #4243 shape #4243 shape #4243 77 Copyright © 2019 Ora cle Z CraalVM (pid 85381) Graal Sampler Simple: O CHI |*n,„, Status sampling inactive CPU sample % I" i ItelKftl • U ■ V.r* ft CI R B* Collected data t> Snapshot Thl Hmn Taul Time Toul rmc icn f (-amain 70.314 ms (100» 2.450 m: » H JavaScript.anonymous 70.3 14 ms 1100» 2.450 m: ▼ H JavaScript parserOnHeadersComplete 3.085 ms 14.4» 2.23S m: ▼ 11 JavaScript parserOnlncomlng 3.08S ms 2.235 m v H JavaScript emit 3.OSS ms 2.235 m: ▼ H JavaScript.emltTwo 3.08S ms 2.235 m v H JavaScript.Function.prototype call 3.08S ms 14.4» 2.23S m v H JavaScript app 3.08S ms 14.4» 2.23Sm: ▼ 11 JavaScript.handle 3.08S ms 14.4» 2.23S m: v M JavaScript handle 3.08S ms 2.23S m ▼ 11 JavaScript.next 3.08S ms 2.23S m v H JavaScript.process params 3.08S ms 2.23S m: ▼ 11 JavaScript.anonymous 0 3.08S ms 14.4» 2.235 m: ▼ H JavaScript.trim prefix 3.08S ms 14.4» 2.23S m: v H JavaScript.handle 3.08S ms 14.4» 2.23S m v H JavaScript.query 3.08S ms 2.23S m v H JavaScript.next 3.08S ms 14.4» 2.23S m: ▼ H JavaScript.process_params 3.08S ms 14.4» 2.235 m: ▼ If JavaScript.anonymous 3.085 ms 14.4» 2.23S m: v H JavaScript trlmpreflx 3.085 ms 14.4» 2.235 m v at JavaScript.handle 3.085 ms 2.23S m: v H JavaScript expresslnit 3.085 ms 14.4» 2.235 m: ▼ 11 JavaScript.next 3.085 ms 2.235 m v H JavaScript.process params 3.085 ms 2.235 m: ▼ H JavaScript anonymous 3.085 ms 2.235 m: v W JavaScript handle 3.085 ms 2.235 m: v H JavaScript dispatch 3.085 ms 2.235 m: v H JavaScript.next 3.085 ms 14.4» 2.235 m v M JavaScript handle 3.085 ms 2.235 m: v H JavaScript anonymous 3.MS mi 2.235 m 2.884 ms 14.1» 2.219 m ▼ a R print 11 2.884 ms 2.219 m ' 41 R pnnt trellis 2.596 ms 1.963 m v at R plot trellis 2.5% ms 13.7» 1.963 m ' tK tryCatch 896 ms 11.3» 754 m v H R tryCatchList 896 ms 11.3» 754 m » ll R tryCatchOne Hi nr. 11.3» 754 m 78 Copyright © 2019 Oracle T *Rd°Tn*'"" wuyiigiiiwiuijumuc > ^ ^ checkArgsAndCall 896 ms 11.3» 754 m 493 ms 10.7» 383 m Visual Polyglot Stack Trace 1 CraalVM(pid 85432) ©|_ Z GraalVM (pid 85432) Craal Sampler Sample: 0CPU Q Memory Status: memory sampling in progress Heap histogram [jjjj Overview Q Monitor § Threads XI Sampler $X Craal Sampler Q Profiler Settings Results U W E] Collected data Q Snapshot Perform CC Heap Dump Name Live Objects T Allocated Objects i_i R.symbol 22,328 (29.9%) 22,328 (3.9%) R.character 14,537 (19.5%) 58,964 (10.2%) i_i R.promise 7,242 (9.7%) 69,326 112%) i_i R.integer S.391 (7.2» 38,144 (6.6%) 1_i R.closure 3,416 (4.6%) 4,285 (0.7%) ,_i JavaScript.Function 3,039 (4.1%) 3,335 10.6%) i_i JavaScript.Object 2,943 (3.9%) 7.158 (1.2%) i_i Ruby.String 2,819 (3.8%) 10,452 11.8%) í_i JavaScript .Array 2,184 (2.9%) 3,698 (0.6%) H R.list 2,124 (2.8%) 20,560 (3.6%) ,_i R.logical 1.721 (2.3%) 29,441 (5.1%) R.externalptr 1.459 (2%) 1.517 (0.3%) 1_1 R.S4 874 (1.2%) 874 (0.2%) ,_i Ruby .Array 865 (1.2%) 6,029 (1%) LJ R.double 728 11%) 3,076 (0.5%) i_, R.pairlist 700 (0.9%) 133,662 (23.2%) ,_i R.environment 413 (0.6%) 431 (0.1%) H R.builtin 276 (0.4%) 276 (0%) ,_i Ruby.Kramdown::Utils::Entities::Entity 256 (0.3%) 256 (0%) ,_, R.language 210 (0.3%) 40.445 (7%) i_i JavaScript. 201 (0.3%) 225 (0%) ,_, Ruby.Regexp 95 (0.1%) 161 (0%) ,_i JavaScript.RegExp 89 (0.1%) 326 (0.1%) LJ R.NULL 81 (0.1%) 3,084 (0.5%) ,_i JavaScript.NatJveModule 78 (0.1%) 78 (0%) i_i JavaScript.Module 64 (0.1%) 64 (0%) i_i Ruby.Gem::Requirement 36 (0%) 51 (0%) ,_i Ruby.Kramdown::Options::Definition 33 (0%) 33 (0%) i_i Ruby.Cem::Version 31 (0%) 31 (0%) i_i Ruby.Kramdown::Parser::Kramdown::Data 31 (0%) 31 (0%) ,__. R.raw 26 (0%) 27 (0%) LJ Ruby.Hash 19 (0%) 38 (0%) ,_i Ruby.Cem::Dependency 19 (0%) 20 <0%> i_i JavaScript.IPv4 15 (0%) 15 (0%) i_i JavaScript.ArrayBuffer IS 10%) 49 (0%) ,_i lavaScript.Layer 13 10%) 13 (0%) 79 Copyright © 2019 Oracle Visual Polyglot Heap Dump JS compatibility 46% 81% 98% 96% 97% 28% Edge 81 Copyright © 2019 Oracle GraalVM 19,0,Q[3] JJS..1.Q https://www.graalvm.org/docs/reference-manual/compatibility/ Quickly check if an NPM module, Ruby gem, or R package is compatible with GraalVM. ison-url Graal.js NAME VERSION STATUS json-url ~>1.0 100.00% tests pass - '- - 82 Copyright © 2019 Oracle JavaScript Memory Footprint: V8 vs. GraalVM Memory for the first JavaScript instance: 23 MByte vs. 19 MByte Memory for each additional JavaScript instance: 12 MByte vs. 0.5 MByte [MByte] 25 20 15 10 5 0 V8 JavaScript VM 7 t1 -V 12 ,0 1 2 3 4 5 6 7 8 9 10 11 12 Number of JavaScript instances 84 Copyright © 2019 Oracle GraalVM 19,C o, 5 1 2 3 4 5 6 7 8 9 10 11 12 - RSS -PSS •USS Optcarrot - TruffleRuby performance fps history (up to 3000 frames) 300 H- 0 500 1000 1500 2000 2500 3000 frames 85 Copyright © 2019 Oracle Python Performance Geomean Speedup over CPython (more is better) 70 ■ Comparable to PyPy, the fastest alternative Micro Benchmarks Shootout Benchmarks ■ GraalVM EE GraalVMCE BPyPy 86 Copyright © 2019 Oracle Conclusion For User • Polyglot • Tooling • Performance • Compatibility For language implementer • AST interpreter - simple implementation • All the tooling from the start - Debugging - Profiling • Performance from the beginning • AOT compilation - Executable - Embedding 88 Copyright © 2019 Oracle Production-Ready Experimental Visionary Java Scala, Groovy, Kotlin JavaScript Node.js Native Image VisualVM 89 Copyright © 2019 Oracle Ruby R LLVM Toolchain Python VSCode Plugin GPU Integration Webassembly LLVM Backend ■ Internship https://www.graalvm.org/community/internship/ The deadline for application is November 30,2019 90 Copyright © 2019 Oracle GraalVM Internship Program The GraalVM team has created a universal virtual machin< more productive. After years of research. GraalVM is now and additional capabilities to a wide range ot languages ai with an ambitious goal to make software engineers all over the world ) production-ready technology, bringing performance improvements I platforms. Now we are expanding our GraalVM internship program with several openings, available for all our research centers. This is a great opportunity to work and learn within an international team of professionals and contribute to the project success. We are proud for our ongoing collaboration with several acknowledged universities, such as Johannes Kepler University in Linz. Charles University of Prague. Technical University of Berlin, University of Edinburgh, LaBRI, University of California (Irvine), Purdue University, Technical University of Dortmund, University of California (Davis), and the University of Lugano. Keep reading to find out about research centers, possible research topics and application process. Opportunities for You Get a chance to apply your skills and knowledge to solve complex con Contribute to an open-source technology with contributors and users : Work in a distributed self-driven international team Choose one of our research centers across the globe Gain invaluable experience in what it is like to work at a leading global Learn from the colleagues who are industry experts and scientists Our Research Center Locations Brno. Czech Republi Possible Research Areas Explore new just-in-time compiler optimization phases Research the application of machine learning for optimizing compiler configurations Investigate techniques for more efficient memory usage Add a new language to the ecosystem of GraalVM Join the efforts to develop a fully meta-circular Java runtime written in Java Create better tooling for polyglot programming and other GraalVM features Discover and close attack vectors available to malicious code Embed GraalVM in other data storage engines Discover and close attack vectors available to malicious code Build security testing frameworks to automate assessment of a guest language attack surface Your Skills Given the broad range of opportunities, specific skills will depend on the specific topic. In general, if you ci following skills, we probably have a place for you: ■ Strong Java programming knowledge (required) e following languages: Fluent English Experience with compiler Node.js developer experit Programming experience ■ JavaScript ■ Python How to Apply In order to apply, please send an email to ■ YourCV ■ Description of your motivation and area of interest ■ Your preferred location ■ Link to your GitHub profile (optional) The deadline for application is November 30, 20f 9. The length of the internship can vary based on the candidate' We pay a competitive salary depending on the location of choice. including the following: Safe harbor statement The preceding is intended to outline our general product direction. 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, timing, and pricing of any features or functionality described for Oracle' products may change and remains at the sole discretion of Oracle Corporation. GraalVM Native Image technology (including SubstrateVM) is early adopter technology. It is available only under an early adopter license and remains subject to potentially significant further changes, compatibility testing and certification. 92 Copyright © 2019 Oracle