Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Олег Шелаев — Про GraalVM

Олег Шелаев — Про GraalVM

GraalVM — виртуальная машина для разных языков программирования:
JVM языки, типа: Java, Scala, Kotlin; JavaScript, Ruby, R, Python и все, что использует LLVM.

GraalVM отличается высокой производительностью благодаря Graal компилятору, который можно использовать и как JIT, и как АОТ.

О возможностях GraalVM:
- улучшение производительности Java кода,
- создание native images для быстрого стартапа програм,
- смешивание кода на разных языках программирования без потери производительности,
- безопасный запуск "нативных" языков
и прочее.

О том, что Graal компилятор делает с кодом, как получить нужную дебаг информацию, какие действуют ограничения и как лучше всего генерировать native images.

Про API для полиглот приложений, как использовать инструменты разработчика типа дебаггера или профилировщика для разных языков и прочие интересные возможности GraalVM.

Moscow JUG

April 04, 2019
Tweet

More Decks by Moscow JUG

Other Decks in Programming

Transcript

  1. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    Про Oleg Šelajev Developer Advocate, GraalVM team, Oracle Labs @shelajev
  2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | Safe Harbor Statement The following 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. 2
  3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !4 Fast Java, Scala, Kotlin, Groovy, Clojure... Instant startup, low footprint Polyglot & embeddable Interoperability between languages: node.js, Python, Ruby, R Why GraalVM?
  4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | Confidential – Oracle Internal/Restricted/Highly Restricted !5 standalone Automatic transformation of interpreters to compilers Engine integration native and managed
  5. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | 6. Extend a JVM-based application 7. Extend a native application 8. Java code as a native library 9. Polyglot in the database 10. Create your own language Top 10 Things To Do With GraalVM 1. High-performance modern Java 2. Low-footprint, fast-startup Java 3. Combine JavaScript, Java, Ruby, and R 4. Run native languages on the JVM 5. Tools that work across all languages medium.com/graalvm/graalvm-ten-things-12d9111f307d
  6. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !8 Java HotSpot VM Graal Compiler Truffle Framework Sulong (LLVM)
  7. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !9 http://chrisseaton.com/rubytruffle/pldi17-truffle/pldi17-truffle.pdf
  8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !14 JavaScrabble.run(); (1.9x) https://medium.com/graalvm/stream-api-performance-with-graalvm-be6cfe7fbb52
  9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !15 sbt > clean; compile; https://medium.com/graalvm/compiling-scala-faster-with-graalvm-86c5c0857fa3
  10. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !18 Matrix multiplication https://youtu.be/RFF2SfPMfpk?t=5281
  11. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !19 valhallaBench.Multiply.multiply (lower is better) JDK 11 (size) Mode Cnt Score Error Units valhallaBench.Multiply.multiply 100 avgt 3 7944.935 ± 1963.931 us/op JDK11 + Graal (size) Mode Cnt Score Error Units valhallaBench.Multiply.multiply 100 avgt 3 3450.944 ± 1130.123 us/op GraalVM EE 1.0-rc8 (size) Mode Cnt Score Error Units valhallaBench.Multiply.multiply 100 avgt 3 3134.066 ± 518.81 us/op Matrix multiplication 2.3x 2.5x
  12. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !20 Graal: How to use the new JVM JIT compiler in real life
  13. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !24 Program graph: Graal IR int average(int a, int b) { return (a + b) / 2; }
  14. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !25 Program graph: Graal IR int average(int a, int b) { return (a + b) / 2; }
  15. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !26 Program graph: Graal IR int average(int[] values) { int sum = 0; for (int n = 0; n < values.length; n++) { sum += values[n]; } return sum / values.length; }
  16. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !27 Program graph: Graal IR int average(int[] values) { int sum = 0; for (int n = 0; n < values.length; n++) { sum += values[n]; } return sum / values.length; }
  17. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !29 Troubleshooting • Disable Graal to check if the issue persists • -XX:-UseJVMCICompiler • Check if the method actually compiles: • -XX:+PrintCompilation • -XX:+TraceDeoptimization • Graal IR dumps • -Dgraal.Dump -Dgraal.MethodFilter=MyClass.someMethod • -Dgraal.PrintIdealGraphFile=true
  18. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !31 Native images • Full AOT compilation to machine code • Works with memory management • Secure execution (e.g., bounds checks) • Embeddable with native applications
  19. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !32 Native images • Full AOT compilation to machine code • Works with memory management • Secure execution (e.g., bounds checks) • Embeddable with native applications
  20. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !35 Image build time Runtime Native image lifecycle
  21. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !36 Static initializers Static class initialization blocks, pre-initialized static variables.
  22. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !37 Static initializers By default static class initialization is done during native image construction. Large static data structures are pre-allocated allowing faster startup of the generated image. No instance-specific initializations can be done in static initializers.
  23. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !38 Static initializers - things not to do • start application threads that continue to run in the background • load native libraries using `java.lang.Runtime.load(String)` • open files or sockets, or • allocate C memory, e.g., `java.nio.ByteBuffer.allocateDirect(int)`.
  24. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !39 Static initializers Write your own initialization methods and call them explicitly from your main entry point.
  25. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !41 > $JAVA_HOME/bin/javac HelloStartupTime.java > $JAVA_HOME/bin/java HelloStartupTime Startup: Fri Aug 31 13:17:05 PDT 2018 Now: Fri Aug 31 13:17:05 PDT 2018
  26. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !42 > $JAVA_HOME/bin/javac HelloStartupTime.java > $JAVA_HOME/bin/java HelloStartupTime Startup: Fri Aug 31 13:17:05 PDT 2018 Now: Fri Aug 31 13:17:05 PDT 2018
  27. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !43 > $JAVA_HOME/bin/javac HelloStartupTime.java > $JAVA_HOME/bin/java HelloStartupTime Startup: Fri Aug 31 13:17:05 PDT 2018 Now: Fri Aug 31 13:17:05 PDT 2018 > $JAVA_HOME/bin/native-image HelloStartupTime > ./hellostartuptime Startup: Fri Aug 31 13:22:12 PDT 2018 Now: Fri Aug 31 14:35:42 PDT 2018
  28. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !44 Static initializers delay --delay-class-initialization-to-runtime= class,list
  29. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !45 Reflection -H:ReflectionConfigurationFiles=
  30. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !49 Resources -H:IncludeResources=<regexp> -H:IncludeResources= "application.yml|META-INF/services/*.*"
  31. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    -H:+ReportUnsupportedElementsAtRuntime --allow-incomplete-classpath !50 java.lang.NoClassDefFoundError
  32. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !51 Substitutions Core classes (annotations): com.oracle.svm.core.annotate.TargetClass com.oracle.svm.core.annotate.Substitute
  33. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !53 native-image.properties META-INF/native-image/
  34. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !54 native-image.properties META-INF/native-image/groupId/artifactId/ reflection-config.json META-INF/native-image/groupId/artifactId/ native-image.properties
  35. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !55 native-image.properties META-INF/native-image/groupId/artifactId/ reflection-config.json META-INF/native-image/groupId/artifactId/ native-image.properties
  36. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !56 native-image.properties ImageName = netty-server Args = --features=com.oracle.svm.nettyplot.PlotterSingletonFeature \ -H:ReflectionConfigurationResources=${.}/reflection-config.json \ --delay-class-initialization-to-runtime=\ io.netty.handler.codec.http.HttpObjectEncoder \ -H:+SpawnIsolates
  37. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    https://medium.com/graalvm/instant-netty-startup-using-graalvm-native- image-generation-ed6f14ff7692
  38. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !63 git clone https://github.com/graalvm/graalvm-demos cd graalvm-demos/scala-days-2018/scalac-native/scala-substitutions sbt package cd ../ $GRAALVM_HOME/bin/native-image -cp $SCALA_HOME/lib/scala-compiler.jar: $SCALA_HOME/lib/scala-library.jar:$SCALA_HOME/lib/scala-reflect.jar:$PWD/ scalac-substitutions/target/scala-2.12/scalac-substitutions_2.12-0.1.0- SNAPSHOT.jar \ -H:SubstitutionResources=substitutions.json,substitutions-2.12.json \ -H:ReflectionConfigurationFiles=scalac-substitutions/reflection- config.json \ -H:Class=scala.tools.nsc.Main \ -H:Name=scalac Native scalac
  39. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !64 https://medium.com/graalvm/compiling-scala-faster-with-graalvm-86c5c0857fa3
  40. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !65 https://github.com/spring-projects/spring-framework/issues/21529
  41. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !69 Heapdump -H:+AllowVMInspection $ ps -e | grep native-name $ kill -SIGUSR1 <pid>
  42. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !71 Runtime https://github.com/giltene/wrk2
  43. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

    !72 Runtime https://github.com/giltene/wrk2
  44. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | You can plug new languages into GraalVM $ gu install ruby $ gu install python $ gu install R
  45. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | You can plug new languages into GraalVM $ gu install ruby $ gu install python $ gu install R $ gu install --file ruby-installable-ce-1.0.0-rc8-macos-amd64.jar $ gu install --file python-installable-ce-1.0.0-rc8-macos-amd64.jar $ gu install --file r-installable-ce-1.0.0-rc8-macos-amd64.jar
  46. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | You can plug new languages into GraalVM $ gu install ruby $ gu install python $ gu install R $ gu install --file ruby-installable-ce-1.0.0-rc8-macos-amd64.jar $ gu install --file python-installable-ce-1.0.0-rc8-macos-amd64.jar $ gu install --file r-installable-ce-1.0.0-rc8-macos-amd64.jar $ gu rebuild-images polyglot libpolyglot js llvm python ruby In reality you’d want to run this, but it takes a long time
  47. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | You can plug new languages into GraalVM $ ruby --version
 truffleruby 1.0.0-rc14, like ruby 2.6.2, GraalVM CE Native [x86_64-darwin] $ graalpython --version Graal Python 3.7.0 (GraalVM CE Native 1.0.0-rc14) $ R --version R version 3.5.1 (FastR)
  48. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !85 Graal.js on JDK11 https://github.com/graalvm/graal-js-jdk11-maven-demo
  49. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !86 https://www.youtube.com/watch?v=mRKjWrNJ8DI
  50. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !87 https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb
  51. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !90 https://medium.com/graalvm/analyzing-the-heap-of-graalvm-polyglot-applications-b9963e68a6a
  52. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !92 High performance, polyglot, language-level virtualization layer… embeddable across the stack in native and JVM-based applications.
  53. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | https://oracle.github.io/oracle-db-mle/releases/0.2.7/
  54. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | https://oracle.github.io/oracle-db-mle/releases/0.2.7/ https://blogs.oracle.com/apex/oracle-database-%2b-apex-%2b-javascriptpython-%3d-awesome
  55. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | $ docker load --input mle-docker-0.2.7.tar.gz $ docker run mle-docker-0.2.7 # takes a while $ docker ps $ docker exec -ti <container_id> bash -li
  56. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | $ echo "{}" > package.json $ npm install validator $ npm install @types/validator $ dbjs deploy -u scott -p tiger -c localhost:1521/ORCLCDB validator $ sqlplus scott/tiger@localhost:1521/ORCLCDB
  57. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | SQL> select validator.isEmail( '[email protected]') from dual; SQL> select validator.isEmail('not-an-email') from dual;
  58. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | !98 https://medium.com/graalvm/safe-and-sandboxed-execution-of-native-code-f6096b35c360
  59. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | Team Oracle Florian Angerer Danilo Ansaloni Stefan Anzinger Martin Balin Cosmin Basca Daniele Bonetta Dušan Bálek Matthias Brantner Lucas Braun Petr Chalupa Jürgen Christ Laurent Daynès Gilles Duboscq Svatopluk Dědic Martin Entlicher Pit Fender Francois Farquet Brandon Fish Matthias Grimmer Christian Häubl Peter Hofer Bastian Hossbach Christian Humer Tomáš Hůrka Mick Jordan Oracle (continued) Vojin Jovanovic Anantha Kandukuri Harshad Kasture Cansu Kaynak Peter Kessler Duncan MacGregor Jiří Maršík Kevin Menard Miloslav Metelka Tomáš Myšík Petr Pišl Oleg Pliss Jakub Podlešák Aleksandar Prokopec Tom Rodriguez Roland Schatz Benjamin Schlegel Chris Seaton Jiří Sedláček Doug Simon Štěpán Šindelář Zbyněk Šlajchrt Boris Spasojevic Lukas Stadler Codrut Stancu JKU Linz Hanspeter Mössenböck Benoit Daloze Josef Eisl Thomas Feichtinger Josef Haider Christian Huber David Leopoldseder Stefan Marr Manuel Rigger Stefan Rumzucker Bernhard Urban TU Berlin: Volker Markl Andreas Kunft Jens Meiners Tilmann Rabl University of Edinburgh Christophe Dubach Juan José Fumero Alfonso Ranjeet Singh Toomas Remmelg LaBRI Floréal Morandat University of California, Irvine Michael Franz Yeoul Na Mohaned Qunaibit Gulfem Savrun Yeniceri Wei Zhang Purdue University Jan Vitek Tomas Kalibera Petr Maj Lei Zhao T. U. Dortmund Peter Marwedel Helena Kotthaus Ingo Korb University of California, Davis Duncan Temple Lang Nicholas Ulle University of Lugano, Switzerland Walter Binder Sun Haiyang Oracle Interns Brian Belleville Ondrej Douda Juan Fumero Miguel Garcia Hugo Guiroux Shams Imam Berkin Ilbeyi Hugo Kapp Alexey Karyakin Stephen Kell Andreas Kunft Volker Lanting Gero Leinemann Julian Lettner Joe Nash Tristan Overney Aleksandar Pejovic David Piorkowski Philipp Riedmann Gregor Richards Robert Seilbeck Rifat Shariyar Oracle Alumni Erik Eckstein Michael Haupt Christos Kotselidis David Leibs Adam Welc Till Westmann Oracle (continued) Jan Štola Tomáš Stupka Farhan Tauheed Jaroslav Tulach Alexander Ulrich Michael Van De Vanter Aleksandar Vitorovic Christian Wimmer Christian Wirth Paul Wögerer Mario Wolczko Andreas Wöß Thomas Würthinger Tomáš Zezula Yudi Zheng Red Hat Andrew Dinn Andrew Haley Intel Michael Berg Twitter Chris Thalinger
  60. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.

    | Confidential – Oracle Internal/Restricted/Highly Restricted !100 • Test your applications with GraalVM – Documentation and downloads at http://www.graalvm.org • Connect your technology with GraalVM – Integrate GraalVM into your application – Run your own programming language or DSL – Build language-agnostic tools • Join the conversation – Report issues or pull requests on GitHub – [email protected] – Follow @graalvm Building a Universal VM is a Community Effort