+ All Categories
Home > Documents > Péter Jeszenszky Faculty of Informatics, University of ...

Péter Jeszenszky Faculty of Informatics, University of ...

Date post: 28-Dec-2021
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
34
Just-in-Time Compilation, Ahead-of- Time Compilation Péter Jeszenszky Faculty of Informatics, University of Debrecen [email protected] Last modified: March 7, 2021
Transcript
Page 1: Péter Jeszenszky Faculty of Informatics, University of ...

Just-in-Time Compilation, Ahead-of-Time Compilation

Péter JeszenszkyFaculty of Informatics, University of Debrecen

[email protected]

Last modified: March 7, 2021

Page 2: Péter Jeszenszky Faculty of Informatics, University of ...

2

Further Information (1)

● Oracle resources:– The Java Virtual Machine Specification. Java SE 15 Edition. September

2020. https://docs.oracle.com/javase/specs/– Java Virtual Machine Guide. Release 15. September 2020.

https://docs.oracle.com/en/java/javase/15/vm/● OpenJDK resources:

– OpenJDK Wiki – HotSpot Internals https://wiki.openjdk.java.net/display/HotSpot/Main

● HotSpot Runtime Overview https://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html

● Presentations https://wiki.openjdk.java.net/display/HotSpot/Presentations

– HotSpot Glossary of Terms https://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html

Page 3: Péter Jeszenszky Faculty of Informatics, University of ...

3

Further Information (2)

● Books:– Scott Oaks. Java Performance: In-Depth Advice for

Tuning and Programming Java 8, 11, and Beyond. 2nd ed. O'Reilly, 2020.

● See Chapter 4, titled Working with the JIT Compiler.● Code:

https://github.com/ScottOaks/JavaPerformanceTuning

Page 4: Péter Jeszenszky Faculty of Informatics, University of ...

4

HotSpot VM Implementations (1)

● 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 -minimal to the java command, respectively.

● Later JDKs and JREs provide only one implementation, namely, the server VM.

● See:– Java SE 8 Documentation – Java Virtual Machine Technology

https://docs.oracle.com/javase/8/docs/technotes/guides/vm/– Java Virtual Machine Guide, Release 15 – Java Virtual Machine Technology

Overview https://docs.oracle.com/en/java/javase/15/vm/java-virtual-machine-technology-overview.html

Page 5: Péter Jeszenszky Faculty of Informatics, University of ...

5

HotSpot VM Implementations (2)

● Information about the JVM:– See also the java.vm.name system property.

$ java -versionopenjdk version "15.0.2" 2021-01-19OpenJDK Runtime Environment (build 15.0.2+7-27)OpenJDK 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)

Page 6: Péter Jeszenszky Faculty of Informatics, University of ...

6

Launching the JVM

● The java command launches a Java application starting the JVM.– Usage: JDK 15 Tool Specifications – The java

Command https://docs.oracle.com/en/java/javase/15/docs/specs/man/java.html

Page 7: Péter Jeszenszky Faculty of Informatics, University of ...

7

Extra and Advanced JVM Options (1)

● These options aren't guaranteed to be supported by all JVM implementations and are subject to change.– Extra options:

● General purpose options that are specific to the HotSpot JVM.● These options start with -X.

– Advanced options:● Developer options used for tuning specific areas of the

HotSpot JVM operation.● These options start with -XX.

Page 8: Péter Jeszenszky Faculty of Informatics, University of ...

8

Extra and Advanced JVM Options (2)

● Examples for using extra options:

$ java --help-extra

$ java -XshowSettings -version$ java -XshowSettings:locale -version$ java -XshowSettings:properties -version$ java -XshowSettings:system -version$ java -XshowSettings:vm -version

$ java -Xmx8g -jar MemoryIntensiveApplication.jar

Page 9: Péter Jeszenszky Faculty of Informatics, University of ...

9

Extra and Advanced JVM Options (3)

● Boolean advanced options are used to either enable or disable a feature and are specified in the form -XX:+OptionName or -XX:-OptionName.

● Certain advanced VM options are available only if they are enabled with specifying either the-XX:+UnlockDiagnosticVMOptions or the -XX:+UnlockExperimentalVMOptions option.

Page 10: Péter Jeszenszky Faculty of Informatics, University of ...

10

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

● Translating Java Virtual Machine code at load-time or during execution into the native instruction set of the host CPU.– See: The Java Virtual Machine Specification, Java

SE 15 Edition. 2020. https://docs.oracle.com/javase/specs/

Page 11: Péter Jeszenszky Faculty of Informatics, University of ...

11

Just-in-time (JIT) Compilation (2)

● Empirical observation: programs spend the majority of their time executing a small piece of their code.– See: Donald E. Knuth. An empirical study of

FORTRAN programs. Software: Practice and Experience, 1(2):105–133, 1971. https://doi.org/10.1002/spe.4380010203

● These frequently executed parts of the code are called hot spots.

Page 12: Péter Jeszenszky Faculty of Informatics, University of ...

12

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

● Tradeoff: resource usage vs. performance of generated code

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

Page 13: Péter Jeszenszky Faculty of Informatics, University of ...

13

Just-in-time (JIT) Compilation (4)

● “Rather than compiling method by method, just in time, the Java HotSpot VM 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 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 parts of the program, without necessarily increasing the overall compilation time. This hot spot monitoring is continued dynamically as the program runs, so that it literally adapts its performance on the fly to the user's needs.”

● See:– HotSpot Runtime Overview

https://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html– The Java HotSpot Performance Engine Architecture

https://www.oracle.com/java/technologies/whitepaper.html

Page 14: Péter Jeszenszky Faculty of Informatics, University of ...

14

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

● The HotSpot JVM contains two JIT compilers:– 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 C++ and can be found under the src/hotspot/share/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.

Page 15: Péter Jeszenszky Faculty of Informatics, University of ...

15

Just-in-time (JIT) Compilation (6)

● Tiered compilation combines the C1 and C2 JIT compilers.– It was introduced in Java SE 7.

● See: Java HotSpot Virtual Machine Performance Enhancements https://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html

– It is the default mode for the server VM since Java SE 8.● See: Java HotSpot Virtual Machine Performance

Enhancements https://docs.oracle.com/javase/8/docs/technotes/guides/vm/performance-enhancements-7.html

Page 16: Péter Jeszenszky Faculty of Informatics, University of ...

16

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

● GraalVM compiler:– A high-performance JIT compiler written in Java that

integrates with the HotSpot JVM.– A component of GraalVM. – It was introduced in JDK 10.

● See: JDK 10 https://openjdk.java.net/projects/jdk/10/– JEP 317: Experimental Java-Based JIT Compiler

https://openjdk.java.net/jeps/317

– It is an experimental feature and is currently supported only on Linux x64 systems.

Page 17: Péter Jeszenszky Faculty of Informatics, University of ...

17

Just-in-time (JIT) Compilation (8)

● GraalVM compiler (continued):– It is part of the JDK build and it is delivered as an

internal module, jdk.internal.vm.compiler.

– It communicates with the JVM using the JVM Compiler Interface (JVMCI).

● The JVMCI is also part of the JDK build and it is contained within the internal module: jdk.internal.vm.ci.

– See: Java Virtual Machine Guide, Release 15 – Graal: a Java-Based JIT Compiler https://docs.oracle.com/en/java/javase/15/vm/java-hotspot-virtual-machine-performance-enhancements.html

Page 18: Péter Jeszenszky Faculty of Informatics, University of ...

18

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

● GraalVM compiler (continued):– Further information:

● https://github.com/oracle/graal/blob/master/compiler/README.md

● Oracle GraalVM Enterprise Edition 21 Guide – GraalVM Compiler https://docs.oracle.com/en/graalvm/enterprise/21/docs/reference-manual/compiler/

Page 19: Péter Jeszenszky Faculty of Informatics, University of ...

19

Just-in-time (JIT) Compilation (10)

● Disabling JIT compilation:– The -Xint option of the java command runs the

application in interpreted-only mode.– Compilation to native code is disabled, and all

bytecode is executed by the interpreter.– The performance benefits offered by the JIT

compiler are not available in this mode.

Page 20: Péter Jeszenszky Faculty of Informatics, University of ...

20

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

● Specifying the JIT compiler to be used:

JIT Compiler VM Option

C1 -client

C2 -serverTiered -XX:+TieredCompilationGraal -XX:+UnlockExperimentalVMOptions

-XX:+EnableJVMCI-XX:+UseJVMCICompiler

- -Xint

Page 21: Péter Jeszenszky Faculty of Informatics, University of ...

21

Just-in-time (JIT) Compilation (12)

● Showing the methods that are compiled to native code:– How to interpret the output:

● Stephen Fox. Quick reference for -XX:+PrintCompilation JVM flag. 29 December 2018. https://foxstephen.net/quick-ref-print-compilation

$ java -XX:+PrintCompilation pkg.Main

Page 22: Péter Jeszenszky Faculty of Informatics, University of ...

22

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

● Showing assembly code generated by the JIT compiler:– Requires the hsdis JVM plugin:

http://hg.openjdk.java.net/jdk/jdk/file/tip/src/utils/hsdis/

● The hsdis-amd64.so/hsdis-amd64.dll file must be copied to the lib/server/ directory of the JDK.

$ java -XX:+UnlockDiagnosticVMOptions \ -XX:+PrintAssembly pkg.Main

Page 23: Péter Jeszenszky Faculty of Informatics, University of ...

23

Just-in-time (JIT) Compilation (14)

● Logging of compilation activity:– Output is written to hotspot_pid<pid>.log.

$ java -XX:+UnlockDiagnosticVMOptions \ -XX:+LogCompilation pkg.Main

Page 24: Péter Jeszenszky Faculty of Informatics, University of ...

24

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

● Tools:– JITWatch (license: Simplified BSD License)

https://github.com/AdoptOpenJDK/jitwatch/● A tool for analyzing and visualizing the compilation

activity of the HotSpot JIT compiler.● Instructions:

https://github.com/AdoptOpenJDK/jitwatch/wiki/Instructions

Page 25: Péter Jeszenszky Faculty of Informatics, University of ...

25

Just-in-time (JIT) Compilation (16)

● JIT compilation in other JVMs:– Eclipse OpenJ9:

● The JIT compiler https://www.eclipse.org/openj9/docs/jit/● -Xjit/-Xnojit https://www.eclipse.org/openj9/docs/xjit/

Page 26: Péter Jeszenszky Faculty of Informatics, University of ...

26

Ahead-of-Time Compilation (1)

● Ahead-of-Time (AOT) compilation means compiling Java classes to native code prior to launching the virtual machine.

● AOT compilation was introduced in JDK 9.● It is an experimental feature and is currently

supported only on Linux x64 systems.● See:

– JEP 295: Ahead-of-Time Compilation https://openjdk.java.net/jeps/295

Page 27: Péter Jeszenszky Faculty of Informatics, University of ...

27

Ahead-of-Time Compilation (2)

● AOT compilation is done by the jaotc command line tool.– See: JDK 15 Tool Specifications – The jaotc

Command 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.

Page 28: Péter Jeszenszky Faculty of Informatics, University of ...

28

Ahead-of-Time Compilation (3)

● Example:

// Hello.java:public class Hello {

public static String getGreeting(String name) { return String.format("Hello, %s!", name); }

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

}

Page 29: Péter Jeszenszky Faculty of Informatics, University of ...

29

Ahead-of-Time Compilation (4)

● Example (continued):

$ javac Hello.java$ jaotc --output libHello.so Hello.class$ java -XX:AOTLibrary=./libHello.so HelloHello, World!

Page 30: Péter Jeszenszky Faculty of Informatics, University of ...

30

Ahead-of-Time Compilation (5)

● Example (continued):$ java -XX:+PrintAOT -XX:AOTLibrary=./libHello.so Hello 5 1 loaded ./libHello.so aot library[Found [B in ./libHello.so] 32 1 aot[ 1] Hello.<init>()V 32 2 aot[ 1] Hello.main([Ljava/lang/String;)V 32 3 aot[ 1] Hello.getGreeting(Ljava/lang/String;)Ljava/lang/StringHello, World!

Page 31: Péter Jeszenszky Faculty of Informatics, University of ...

31

Ahead-of-Time Compilation (6)

● Example (continued):– If class bytecode changes but the corresponding

AOT library is not updated, the native code for that particular class is not used during execution.

# 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!

Page 32: Péter Jeszenszky Faculty of Informatics, University of ...

32

Ahead-of-Time Compilation (7)

● Example (continued):– Modules can also be AOT-compiled:

$ jaotc --output libjava.base.so --module java.base$ java -XX:AOTLibrary=./libHello.so,./libjava.base.so HelloHello, World!

Page 33: Péter Jeszenszky Faculty of Informatics, University of ...

33

Ahead-of-Time Compilation (8)

● Further recommended reading:– Igor Veresov, Vladimir Kozlov. Ahead of Time

Compilation. 2018. http://cr.openjdk.java.net/~thartmann/offsite_2018/AOT_offsite_2018.pdf

Page 34: Péter Jeszenszky Faculty of Informatics, University of ...

34

Monitoring the JVM

● jconsole https://docs.oracle.com/en/java/javase/15/docs/specs/man/jconsole.html– It is part of the JDK.

● JDK Mission Control (written in: Java; license: 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

– 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


Recommended