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

Another compilation method in Java

Another compilation method in Java

AOT (Ahead of time) compilation overview, which was delivered at JJUG CCC 2017 fall.

Akihiro Nishikawa

November 18, 2017
Tweet

More Decks by Akihiro Nishikawa

Other Decks in Technology

Transcript

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

    Another compilation method in Java AOT (Ahead of Time) Compilation Akihiro Nishikawa Oracle Corporation Japan 1
  2. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    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, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  3. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    Agenda JIT (Just-in-Time) Compilation AOT (Ahead-of-Time) Compilation Limitations Summary 1 2 3 4 3
  4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    5 JIT (Just in Time) Compilation Source Code (*.java) javac Class file (*.class) HotSpot Compile
  5. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    6 For improvement of warm up time Tiered compilation C1 (-client/client use) C2 (-server/server use) • Longer compilation time and longer startup time, but generated code runs faster. • Heavy optimization • In case of prioritizing performance after invocation... • Shorter compilation time and shorter startup time, but generated code runs slow. • Less optimization • In case of prioritizing startup time...
  6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    7 Compilation Level • 4: C2 • 0: Interpreter C2 Interpreter C1 • 1: C1 full optimization (no profiling) • 2: C1 with profiling about invocation and back-edge only • 3: C1 full profiling (level2 + MDO) 1,500 10,000
  7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    AOT – Ahead Of Time compilation • Generate native code in advance • JEP 295 • Based on Graal 8
  8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    9 AOT Compilation Source Code (*.java) javac Class file (*.class) HotSpot jaotc Shared object file (*.so)
  9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    10 *.java javac *.class HotSpot jaotc *.so javac Compile
  10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    *.so 11 Able to share “*.so” file among JVMs HotSpot HotSpot HotSpot HotSpot
  11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    CDS/AppCDS (Class Data Sharing) • CDS is the scheme for sharing Java SE class library only. • In case of AppCDS, application classes are also included. 13 Class Data classes.jsa HotSpot HotSpot HotSpot HotSpot
  12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    Works for... • Quicker warmup • Lower total memory footprint Does not work for... • No feature to persist or share machine (native) code. 14 CDS/AppCDS
  13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    16 $ gcc -o HelloAoT HelloAoT.c In case of C...
  14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    AOT Java Compilers • IBM Java SDK for AIX • IBM Java SDK for z/OS • Oracle Java ME Embedded Client • WebSphere Real Time • Gluon VM etc. 18
  15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    19 Does AOT support tiered compilation (C1/C2)? !
  16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    AOT Supports Tiered Compilation • Non-tiered compilation mode (default) –Similar to C1 in Client VM –No collecting profiling information –No JIT recompilation if AOT code is not deoptimized. • Tied compilation mode (--compile-for-tiered) –Profiling level is as same as C1 Level 2. –If hitting AOT invocation thresholds, methods are recompiled by C1 at Level 3 first for gathering full profiling information. 20
  17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    Benefits might be gained from AOT... • No compilation overhead at runtime. • Improve startup time and able to achieve peak performance faster. • Able to run on the platform where native code cannot be not generated at runtime (e.g. iOS, embedded). • Density improvement - Able to share native code. 22
  18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    23 $ jaotc --output libHelloAOT.so HelloAOT.class $ jaotc --output libjava.base.so --module java.base $ jaotc --output libmyapp.so --jar myapp.jar $ jaotc -J-XX:+UseCompressedOops --output libHelloAOT.so HelloAOT.class How to use jaotc
  19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    jaotc does not resolve all referenced classes • jaotc does not resolve referenced classes which are not system classes or part of compiled classes. 1. Have to add referenced classes to class path. 2. Specify additional java modules 24 jaotc --output=libfoo.so --jar foo.jar ¥ -J-cp -J./ jaotc --output=libactivation.so --module ¥ java.activation -J--add-module=java.se.ee
  20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    25 $ java -XX:AOTLibrary=./libHelloAOT.so HelloAOT Hello AOT! $ java -XX:+PrintAOT ¥ -XX:AOTLibrary=./libHelloAoT.so HelloAoT 13 1 loaded ./libHelloAoT.so aot library 76 1 aot[ 1] HelloAoT.<init>()V 76 2 aot[ 1] HelloAoT.main([Ljava/lang/String;)V Hello AOT! Run AOT compiled code
  21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    On JVM startup... • AOT initialization code looks for well-known AOT libraries in well- known location ($JAVA_HOME/lib) or libraries specified using -XX:AOTLibrary. • JVM knows AOT library name for the following Java modules. –java.base –jdk.compiler (javac) –jdk.scripting.nashorn (Nashorn) –jdk.internal.vm.ci (JVMCI) –jdk.internal.vm.compiler (Graal) 26
  22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    VM Options should be synchronized. • libjava.base.so -XX:-UseCompressedOops -XX:+UseG1GC • libjava.base-coop.so -XX:+UseCompressedOops -XX:+UseG1GC • libjava.base-nong1.so -XX:-UseCompressedOops -XX:+UseParallelGC • libjava.base-coop-nong1.so -XX:+UseCompressedOops -XX:+UseParallelGC 27
  23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    Limitations • Experimental release • No support • No official document (except for JEP) 29 First of all...
  24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    Limitations • Available only when using either Parallel or G1 GC on Linux x64 with libelf.so. • AOT compilation must be executed on the same system or a system with the same configuration on which AOT code will be used by Java application. • Unable to compile java code using dynamically generated classes and bytecode (lambda expressions, InvokeDynamic) 30 These limitations are in Java 9, and may be addressed in future releases.
  25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    When and where to use AOT? • Event-driven services such as Functions • Embedded, IoT, mobile • Not often invoked classes... (not hot code) • Application code which should be protected from decompilers ...etc. 31 If AOT is officially supported and is available on several platforms...
  26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    Summary • Since Java 9 (HotSpot), AOT compilation is available, but positioned as experimental release and no official support. • AOT have several characteristics and is expected to improve performance of applications, especially such short-lived objects as functions • Working to remove current limitation and improve AOT. 32
  27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    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, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 33