Just-in-Time Compilation, Ahead-of- Further Information (2) Time Compilation

● Books: – Scott Oaks. Performance: In-Depth Advice for Péter Jeszenszky Tuning and Programming Java 8, 11, and Beyond. Faculty of Informatics, University of Debrecen 2nd ed. O'Reilly, 2020. [email protected] ● See Chapter 4, titled Working with the JIT Compiler. ● Code: https://github.com/ScottOaks/JavaPerformanceTuning Last modified: March 7, 2021

3

Further Information (1) HotSpot VM Implementations (1)

● Oracle resources: ● In Oracle JDK/JRE 8 and earlier, different implementations of the – The Specification. Java SE 15 Edition. September HotSpot JVM were provided (client VM, server VM, and minimal VM). 2020. https://docs.oracle.com/javase/specs/ – They can be launched by passing the option -client, -server, or - – Java Virtual Machine Guide. Release 15. September 2020. minimal to the java command, respectively. https://docs.oracle.com/en/java/javase/15/vm/ ● Later JDKs and JREs provide only one implementation, namely, the ● OpenJDK resources: server VM. – OpenJDK Wiki – HotSpot Internals ● See: https://wiki.openjdk.java.net/display/HotSpot/Main – Java SE 8 Documentation – Java Virtual Machine Technology ● HotSpot Runtime Overview https://docs.oracle.com/javase/8/docs/technotes/guides/vm/ https://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html – Java Virtual Machine Guide, Release 15 – Java Virtual Machine Technology ● Presentations https://wiki.openjdk.java.net/display/HotSpot/Presentations Overview – HotSpot Glossary of Terms https://docs.oracle.com/en/java/javase/15/vm/java-virtual-machine-technology https://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html -overview.html

2 4 Extra and Advanced JVM Options HotSpot VM Implementations (2) (1)

● Information about the JVM: ● These options aren't guaranteed to be supported by all JVM implementations and are subject to – See also the java.vm.name system property. change. $ java -version – openjdk version "15.0.2" 2021-01-19 Extra options: OpenJDK Runtime Environment (build 15.0.2+7-27) ● General purpose options that are specific to the HotSpot JVM. OpenJDK 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing) ● These options start with -X. – Advanced options:

● Developer options used for tuning specific areas of the HotSpot JVM operation. ● These options start with -XX.

5 7

Extra and Advanced JVM Options Launching the JVM (2)

● The java command launches a Java application ● Examples for using extra options: starting the JVM. $ java --help-extra – Usage: JDK 15 Tool Specifications – The java Command $ java -XshowSettings -version $ java -XshowSettings:locale -version https://docs.oracle.com/en/java/javase/15/docs/spe $ java -XshowSettings:properties -version cs/man/java.html $ java -XshowSettings:system -version $ java -XshowSettings:vm -version

$ java -Xmx8g -jar MemoryIntensiveApplication.jar

6 8 Extra and Advanced JVM Options Just-in-time (JIT) Compilation (2) (3)

● Boolean advanced options are used to either ● Empirical observation: programs spend the enable or disable a feature and are specified in majority of their time executing a small piece of the form -XX:+OptionName or -XX:- their code. OptionName. – See: Donald E. Knuth. An empirical study of ● Certain advanced VM options are available only FORTRAN programs. Software: Practice and if they are enabled with specifying either the Experience, 1(2):105–133, 1971. https://doi.org/10.1002/spe.4380010203 -XX:+UnlockDiagnosticVMOptions or the -XX:+UnlockExperimentalVMOptions ● These frequently executed parts of the code are option. called hot spots.

9 11

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

● Translating Java Virtual Machine code at load- ● Tradeoff: resource usage vs. performance of time or during execution into the native generated code instruction set of the host CPU. – See: The Java Virtual Machine Specification, Java SE 15 Edition. 2020. https://docs.oracle.com/javase/specs/

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

● “Rather than compiling method by method, just in time, the Java HotSpot VM ● Tiered compilation combines the C1 and C2 JIT immediately runs the program using an interpreter, and analyzes the code as it runs to detect the critical hot spots in the program. Then it focuses the attention compilers. of a global native-code optimizer on the hot spots.” – It was introduced in Java SE 7. ● “By avoiding compilation of infrequently executed code (most of the program), the Java HotSpot compiler can devote more attention to the performance-critical ● See: Java HotSpot Virtual Machine Performance parts of the program, without necessarily increasing the overall compilation Enhancements time. This hot spot monitoring is continued dynamically as the program runs, so https://docs.oracle.com/javase/7/docs/technotes/guides/vm/pe that it literally adapts its performance on the fly to the user's needs.” rformance-enhancements-7.html ● See: – It is the default mode for the server VM since Java SE 8. – HotSpot Runtime Overview https://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html ● See: Java HotSpot Virtual Machine Performance – The Java HotSpot Performance Engine Architecture Enhancements https://www.oracle.com/java/technologies/whitepaper.html https://docs.oracle.com/javase/8/docs/technotes/guides/vm/pe rformance-enhancements-7.html

13 15

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

● The HotSpot JVM contains two JIT compilers: ● GraalVM compiler: – Client/C1: – A high-performance JIT compiler written in Java that ● A fast JIT compiler that performs only basic optimizations. integrates with the HotSpot JVM. ● Originally, it was intended to be used for desktop applications. – ● It is written in C++ and can be found under the A component of GraalVM. src/hotspot/share/c1/ directory of the OpenJDK source tree. – It was introduced in JDK 10. – Server/C2/“opto”: ● See: JDK 10 https://openjdk.java.net/projects/jdk/10/ ● A slower JIT compiler with high resource demands that performs aggressive optimizations. – JEP 317: Experimental Java-Based JIT Compiler https://openjdk.java.net/jeps/317 ● Originally, it was intended to be used for long-running server applications. – ● It is written in C++ and can be found under the It is an experimental feature and is currently supported src/hotspot/share/opto/ directory of the OpenJDK source tree. only on x64 systems.

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

● GraalVM compiler (continued): ● Disabling JIT compilation: – It is part of the JDK build and it is delivered as an – The -Xint option of the java command runs the internal module, jdk.internal.vm.compiler. application in interpreted-only mode. – It communicates with the JVM using the JVM Compiler – Compilation to native code is disabled, and all Interface (JVMCI). bytecode is executed by the interpreter. ● The JVMCI is also part of the JDK build and it is contained – The performance benefits offered by the JIT within the internal module: jdk.internal.vm.ci. compiler are not available in this mode. – See: Java Virtual Machine Guide, Release 15 – Graal: a Java-Based JIT Compiler https://docs.oracle.com/en/java/javase/15/vm/java-hots pot-virtual-machine-performance-enhancements.html

17 19

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

● GraalVM compiler (continued): ● Specifying the JIT compiler to be used: – Further information: JIT Compiler VM Option

● https://github.com/oracle/graal/blob/master/compiler/REA C1 -client DME.md C2 -server ● Oracle GraalVM Enterprise Edition 21 Guide – GraalVM Tiered -XX:+TieredCompilation Compiler Graal -XX:+UnlockExperimentalVMOptions https://docs.oracle.com/en/graalvm/enterprise/21/docs/ref -XX:+EnableJVMCI erence-manual/compiler/ -XX:+UseJVMCICompiler - -Xint

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

● Showing the methods that are compiled to ● Logging of compilation activity: native code: – Output is written to hotspot_pid.log. – How to interpret the output: $ java -XX:+UnlockDiagnosticVMOptions \ ● Stephen Fox. Quick reference for -XX:+PrintCompilation -XX:+LogCompilation pkg.Main JVM flag. 29 December 2018. https://foxstephen.net/quick-ref-print-compilation $ java -XX:+PrintCompilation pkg.Main

21 23

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

● Showing assembly code generated by the JIT ● Tools: compiler: – JITWatch (license: Simplified BSD License) – Requires the hsdis JVM plugin: https://github.com/AdoptOpenJDK/jitwatch/ http://hg.openjdk.java.net/jdk/jdk/file/tip/src/utils/hsdi ● A tool for analyzing and visualizing the compilation s/ activity of the HotSpot JIT compiler. ● The hsdis-amd64.so/hsdis-amd64.dll file must be ● Instructions: copied to the lib/server/ directory of the JDK. https://github.com/AdoptOpenJDK/jitwatch/wiki/Instructio ns $ java -XX:+UnlockDiagnosticVMOptions \ -XX:+PrintAssembly pkg.Main

22 24 Just-in-time (JIT) Compilation (16) Ahead-of-Time Compilation (2)

● JIT compilation in other JVMs: ● AOT compilation is done by the jaotc command – Eclipse OpenJ9: line tool. – ● The JIT compiler https://www.eclipse.org/openj9/docs/jit/ See: JDK 15 Tool Specifications – The jaotc Command ● -Xjit/-Xnojit https://www.eclipse.org/openj9/docs/xjit/ https://docs.oracle.com/en/java/javase/15/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 (1) Ahead-of-Time Compilation (3)

● Ahead-of-Time (AOT) compilation means compiling ● Example: Java classes to native code prior to launching the virtual machine. // Hello.java: public class Hello { ● AOT compilation was introduced in JDK 9. public static String getGreeting(String name) { ● It is an experimental feature and is currently return String.format("Hello, %s!", name); supported only on Linux x64 systems. } ● See: public static void main(String[] args) { System.out.println(getGreeting("World")); – JEP 295: Ahead-of-Time Compilation } https://openjdk.java.net/jeps/295 }

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

● Example (continued): ● Example (continued):

$ javac Hello.java – If class bytecode changes but the corresponding $ jaotc --output libHello.so Hello.class AOT library is not updated, the native code for that $ java -XX:AOTLibrary=./libHello.so Hello particular class is not used during execution. Hello, World! # 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 (5) Ahead-of-Time Compilation (7)

● Example (continued): ● Example (continued):

$ java -XX:+PrintAOT -XX:AOTLibrary=./libHello.so Hello – Modules can also be AOT-compiled: 5 1 loaded ./libHello.so aot library [Found [B in ./libHello.so] 32 1 aot[ 1] Hello.()V $ jaotc --output libjava.base.so --module java.base 32 2 aot[ 1] Hello.main([Ljava/lang/String;)V $ java -XX:AOTLibrary=./libHello.so,./libjava.base.so Hello 32 3 aot[ 1] Hello.getGreeting(Ljava/lang/String;)Ljava/lang/String Hello, World! Hello, World!

30 32 Ahead-of-Time Compilation (8) Monitoring the JVM

● Further recommended reading: ● jconsole https://docs.oracle.com/en/java/javase/15/docs/specs/man/jconsole.html – Igor Veresov, Vladimir Kozlov. Ahead of Time – It is part of the JDK. Compilation. 2018. ● JDK Mission Control (written in: Java; license: BSD-style/UPL) https://openjdk.java.net/projects/jmc/ http://cr.openjdk.java.net/~thartmann/offsite_2018/A https://wiki.openjdk.java.net/display/jmc/Main https://github.com/openjdk/jmc OT_offsite_2018.pdf ● VisualVM (written in: Java; license: GPLv2) https://visualvm.github.io/ https://github.com/oracle/visualvm – Installation (SDKMAN!): sdk install visualvm – Usage: Getting Started with VisualVM https://visualvm.github.io/gettingstarted.html – IDE support:

● IntelliJ IDEA plugin: VisualVM Launcher https://plugins.jetbrains.com/plugin/7115-visualvm-launcher

33 34