Just-in-Time Compilation, Ahead-of- HotSpot VM Implementations (1) Time Compilation

● In Oracle JDK/JRE 8 and earlier, different implementations of the HotSpot JVM were provided (client VM, server VM, and minimal VM). – They can be launched by passing the option -client, -server, or - Péter Jeszenszky minimal to the java command, respectively. ● Later JDKs and JREs provide only one implementation, namely, the Faculty of Informatics, University of Debrecen server VM. [email protected] ● See: – Java SE 8 Documentation – Technology Last modified: March 2, 2020 https://docs.oracle.com/javase/8/docs/technotes/guides/vm/ – Java Virtual Machine Guide, Release 13 – Java Virtual Machine Technology Overview https://docs.oracle.com/en/java/javase/13/vm/java-virtual-machine-technology -overview.html

3

Further Information HotSpot VM Implementations (2)

● OpenJDK resources: ● Information about the JVM: – OpenJDK Wiki – HotSpot Internals – See also the java.vm.name system property. https://wiki.openjdk.java.net/display/HotSpot/Main $ java -version ● HotSpot Runtime Overview version "11.0.6" 2020-01-14 http://openjdk.java.net/groups/hotspot/docs/RuntimeOver OpenJDK Runtime Environment 18.9 (build 11.0.6+10) view.html OpenJDK 64-Bit Server VM 18.9 (build 11.0.6+10, mixed mode) ● Presentations https://wiki.openjdk.java.net/display/HotSpot/Presentation s – HotSpot Glossary of Terms https://openjdk.java.net/groups/hotspot/docs/HotSp otGlossary.html

2 4 Extra and Advanced JVM Options Launching the JVM (2)

● The java command launches a Java application ● Boolean advanced options are used to either starting the JVM. enable or disable a feature and are specified in – Usage: JDK 13 Tool Specifications – The java the form -XX:+OptionName or -XX:- Command OptionName.

https://docs.oracle.com/en/java/javase/13/docs/spe ● cs/man/java.html Certain advanced VM options are available only if they are enabled with specifying either the -XX:+UnlockDiagnosticVMOptions or the -XX:+UnlockExperimentalVMOptions option.

5 7

Extra and Advanced JVM Options Just-in-time (JIT) Compilation (1) (1)

● These options aren't guaranteed to be ● Translating Java Virtual Machine code at load- supported by all JVM implementations and are time or during execution into the native subject to change. instruction set of the host CPU. – Extra options: – See: The Java Virtual Machine Specification, Java ● General purpose options that are specific to the HotSpot SE 13 Edition. 2019. JVM. https://docs.oracle.com/javase/specs/ ● These options start with -X. – Advanced options: ● These options start with -XX.

6 8 Just-in-time (JIT) Compilation (2) Just-in-time (JIT) Compilation (4)

● ● “Rather than compiling method by method, just in time, the Java HotSpot VM Empirical observation: programs spend the immediately runs the program using an interpreter, and analyzes the code as it majority of their time executing a small piece of runs to detect the critical hot spots in the program. Then it focuses the attention their code. of a global native-code optimizer on the hot spots.” ● “By avoiding compilation of infrequently executed code (most of the program), – the Java HotSpot compiler can devote more attention to the performance-critical See: Donald E. Knuth. An empirical study of parts of the program, without necessarily increasing the overall compilation FORTRAN programs. Software: Practice and time. This hot spot monitoring is continued dynamically as the program runs, so Experience, 1(2):105–133, 1971. that it literally adapts its performance on the fly to the user's needs.” https://doi.org/10.1002/spe.4380010203 ● See: – HotSpot Runtime Overview ● These frequently executed parts of the code are https://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html – The Java HotSpot Performance Engine Architecture called hot spots. https://www.oracle.com/technetwork/systems/whitepaper-135217.html

9 11

Just-in-time (JIT) Compilation (3) Just-in-time (JIT) Compilation (5)

● Tradeoff: resource usage vs. performance of ● The HotSpot JVM contains two JIT compilers: generated code – Client/C1: ● A fast JIT compiler that performs only basic optimizations. ● Originally, it was intended to be used for desktop applications. ● It is written in ++ and can be found under the src/hotspot//c1/ directory of the OpenJDK source tree. – Server/C2/“opto”: ● A slower JIT compiler with high resource demands that performs aggressive optimizations. ● Originally, it was intended to be used for long-running server applications. ● It is written in C++ and can be found under the src/hotspot/share/opto/ directory of the OpenJDK source tree.

Source: Tobias Hartmann. The Java HotSpot VM – Under the Hood. August 2017. http://cr.openjdk.java.net/~thartmann/talks/2017-Hotspot_Under_The_Hood.pdf 10 12 Just-in-time (JIT) Compilation (6) Just-in-time (JIT) Compilation (8)

● Tiered compilation combines the C1 and C2 JIT ● GraalVM compiler (continued): compilers. – It is part of the JDK build and it is delivered as an – It was introduced in Java SE 7. internal module, jdk.internal.vm.compiler. ● See: Java HotSpot Virtual Machine Performance – It communicates with the JVM using the JVM Compiler Enhancements Interface (JVMCI). https://docs.oracle.com/javase/7/docs/technotes/guides/vm/pe rformance-enhancements-7.html ● The JVMCI is also part of the JDK build and it is contained – It is the default mode for the server VM since Java SE 8. within the internal module: jdk.internal.vm.ci. – ● See: Java HotSpot Virtual Machine Performance See: Java Virtual Machine Guide, Release 13 – Graal: Enhancements a Java-Based JIT Compiler https://docs.oracle.com/javase/8/docs/technotes/guides/vm/pe https://docs.oracle.com/en/java/javase/13/vm/java-hots rformance-enhancements-7.html pot-virtual-machine-performance-enhancements.html# GUID-19475E49-11C6-43DF-B21F-DFEC436EC849

13 15

Just-in-time (JIT) Compilation (7) Just-in-time (JIT) Compilation (9)

● GraalVM compiler: ● GraalVM compiler (continued): – A high-performance JIT compiler written in Java that – Further information: integrates with the HotSpot JVM. ● https://github.com/oracle/graal/blob/master/compiler/REA – A component of GraalVM. DME.md ● – It was introduced in JDK 10. Oracle GraalVM Enterprise Edition 19 Guide – Compiler https://docs.oracle.com/en/graalvm/enterprise/19/guide/o ● See: JDK 10 https://openjdk.java.net/projects/jdk/10/ verview/compiler.html – JEP 317: Experimental Java-Based JIT Compiler https://openjdk.java.net/jeps/317 – It is an experimental feature and is currently supported only on x64 systems.

14 16 Just-in-time (JIT) Compilation (10) Just-in-time (JIT) Compilation (12)

● Disabling JIT compilation: ● Showing the methods that are compiled to – The -Xint option of the java command runs the native code: application in interpreted-only mode. – How to interpret the output: – Compilation to native code is disabled, and all ● Stephen Fox. Quick reference for -XX:+PrintCompilation bytecode is executed by the interpreter. JVM flag. 29 December 2018. https://foxstephen.net/quick-ref-print-compilation – The performance benefits offered by the JIT compiler are not available in this mode. $ java -XX:+PrintCompilation pkg.Main

17 19

Just-in-time (JIT) Compilation (11) Just-in-time (JIT) Compilation (13)

● Specifying the JIT compiler to be used: ● Showing assembly code generated by the JIT compiler: JIT Compiler VM Option – C1 -client Requires the hsdis JVM plugin: C2 -server http://hg.openjdk.java.net/jdk/jdk/file/tip/src/utils/hsdi Tiered -XX:+TieredCompilation s/ Graal -XX:+UnlockExperimentalVMOptions ● The hsdis-amd64.so/hsdis-amd64.dll file must be -XX:+EnableJVMCI copied to the lib/server/ directory of the JDK. -XX:+UseJVMCICompiler - -Xint $ java -XX:+UnlockDiagnosticVMOptions \ -XX:+PrintAssembly pkg.Main

18 20 Just-in-time (JIT) Compilation (14) Just-in-time (JIT) Compilation (16)

● Logging of compilation activity: ● JIT compilation in other JVMs: – Output is written to hotspot_pid.log. – OpenJ9:

● $ java -XX:+UnlockDiagnosticVMOptions \ The JIT compiler https://www.eclipse.org/openj9/docs/jit/ -XX:+LogCompilation pkg.Main ● -Xjit/-Xnojit https://www.eclipse.org/openj9/docs/xjit/

21 23

Just-in-time (JIT) Compilation (15) Ahead-of-Time Compilation (1)

● Tools: ● Ahead-of-Time (AOT) compilation means compiling Java classes to native code prior to launching the – JITWatch (license: Simplified BSD License) https://github.com/AdoptOpenJDK/jitwatch/ virtual machine. ● ● A tool for analyzing and visualizing the compilation AOT compilation was introduced in JDK 9. activity of the HotSpot JIT compiler. ● It is an experimental feature and is currently ● Instructions: supported only on Linux x64 systems. https://github.com/AdoptOpenJDK/jitwatch/wiki/Instructio ns ● See: – JEP 295: Ahead-of-Time Compilation https://openjdk.java.net/jeps/295

22 24 Ahead-of-Time Compilation (2) Ahead-of-Time Compilation (4)

● AOT compilation is done by the jaotc command ● Example (continued): line tool. $ javac Hello.java – See: JDK 13 Tool Specifications – The jaotc $ jaotc --output libHello.so Hello.class $ java -XX:AOTLibrary=./libHello.so Hello Command Hello, World! https://docs.oracle.com/en/java/javase/13/docs/specs/ man/jaotc.html – The tool produces native code in the form of a shared library for the Java methods in specified Java class files. ● The JVM can load these AOT libraries and use native code from them when corresponding Java methods are called.

25 27

Ahead-of-Time Compilation (3) Ahead-of-Time Compilation (5)

● Example: ● Example (continued):

$ java -XX:+PrintAOT -XX:AOTLibrary=./libHello.so Hello // Hello.java: 5 1 loaded ./libHello.so aot library public class Hello { [Found [B in ./libHello.so] 32 1 aot[ 1] Hello.()V 32 2 aot[ 1] Hello.main([Ljava/lang/String;)V public static String getGreeting(String name) { 32 3 aot[ 1] Hello.getGreeting(Ljava/lang/String;)Ljava/lang/String return String.format("Hello, %s!", name); Hello, World! }

public static void main(String[] args) { System.out.println(getGreeting("World")); }

}

26 28 Ahead-of-Time Compilation (6) Ahead-of-Time Compilation (8)

● Example (continued): ● Further recommended reading: – If class bytecode changes but the corresponding – Igor Veresov, Vladimir Kozlov. Ahead of Time AOT library is not updated, the native code for that Compilation. 2018. particular class is not used during execution. http://cr.openjdk.java.net/~thartmann/offsite_2018/A OT_offsite_2018.pdf # The parameter "Hello" has been changed to "Buddy" in Hello.java $ javac Hello.java # jaotc is not executed $ java -XX:+PrintAOT -XX:AOTLibrary=./libHello.so Hello 4 1 loaded ./libHello.so aot library [Found [B in ./libHello.so] Hello, Buddy!

29 31

Ahead-of-Time Compilation (7) Monitoring the JVM

● Example (continued): ● jconsole https://docs.oracle.com/en/java/javase/13/docs/specs/man/j – Modules can also be AOT-compiled: console.html

$ jaotc --output libjava.base.so --module java.base – It is part of the JDK. $ java -XX:AOTLibrary=./libHello.so,./libjava.base.so Hello ● JDK Mission Control (written in: Java; license: Hello, World! BSD-style/UPL) https://openjdk.java.net/projects/jmc/ https://wiki.openjdk.java.net/display/jmc/Main https://github.com/openjdk/jmc ● VisualVM (written in: Java; license: GPLv2) https://visualvm.github.io/ https://github.com/oracle/visualvm – Installation (SDKMAN!): sdk install visualvm

30 32