Top Banner
Hotspot & AOT Now it’s time to compile Dmitry Chuyko Java SE Performance Team September 22, 2016 Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
48

Hotspot & AOT

Apr 21, 2017

Download

Software

Dmitry Chuyko
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hotspot & AOT

Hotspot & AOTNow it’s time to compile

Dmitry ChuykoJava SE Performance TeamSeptember 22, 2016

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

Page 2: Hotspot & AOT

Contents

1. Introduction

2. The Current Situation

3. Ahead-of-time Compilation

4. Graal

5. JVM Compiler Interface

6. Artifacts

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 2/44

Page 3: Hotspot & AOT

Safe Harbor StatementThe following is intended to outline our general product direction. It is intendedfor information purposes only, and may not be incorporated into any contract. Itis not a commitment to deliver any material, code, or functionality, and shouldnot be relied upon in making purchasing decisions. The development, release, andtiming of any features or functionality described for Oracle’s products remains atthe sole discretion of Oracle.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 3/44

Page 4: Hotspot & AOT

Introduction

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 4/44

Page 5: Hotspot & AOT

Reminder: It’s 2016‚ JDK 9 Early Access

https://jdk9.java.net/‚ JDK 8u‚ JDK 7 End of Public Updates in April 2015

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 5/44

Page 6: Hotspot & AOT

Overview: ComputingA long time ago in a galaxy far, far away...

‚ Pre-computer machines appeared‚ Computers and their machine codes‚ Languages and compilers‚ Scripts‚ Computer science

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 6/44

Page 7: Hotspot & AOT

Overview: Java‚ Is a language‚ Set of specifications‚ Used to be called slow

’́Because it’s interpreted”(not true)

‚ ”Write once, run anywhere”(true)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 7/44

Page 8: Hotspot & AOT

Overview: JVM‚ Is a code itself‚ Can dynamically execute arbitrary correct bytecode

‚ May be written in anything‚ May produce native code and re-use the result

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8/44

Page 9: Hotspot & AOT

Overview: JVM‚ Is a code itself‚ Can dynamically execute arbitrary correct bytecode‚ May be written in anything‚ May produce native code and re-use the result

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8/44

Page 10: Hotspot & AOT

The Current Situation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 9/44

Page 11: Hotspot & AOT

Overview: Hospot

‚ Is a JVM‚ Written in C++‚ Native shared libraries (libjvm)‚ Produces bytecode dynamically for its own purposes‚ Does just-in-time compilation‚ Supports many modes

– Garbage collectors– Pointers encoding– etc.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 10/44

Page 12: Hotspot & AOT

Overview: JIT in Hotspot‚ Tiered compilation

– Level 0. Interpreter– Level 1. C1 without profiling (optimized), terminal– Level 2. C1 with basic profiling– Level 3. C1 with full profiling– Level 4. C2, terminal, expensive

‚ Unused method versions are thrown away to save footprint‚ Optimizations, resource constraints

– ñ de-optimizations to level 0‚ All modes (if not switched off), CPU instruction set

– Custom code

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 11/44

Page 13: Hotspot & AOT

Problem: Application Warm-up

Iterative workload‚ Startup time‚ Time to

performance

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 12/44

Page 14: Hotspot & AOT

Problem: Startup Time

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 13/44

Page 15: Hotspot & AOT

Problem: Time to Performance

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 14/44

Page 16: Hotspot & AOT

Problem: Time to PerformancePeak Performance

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 15/44

Page 17: Hotspot & AOT

Problem: Time to PerformanceSum of Iterations

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 16/44

Page 18: Hotspot & AOT

Problem: Application Latency

Iterative workload‚ Interpreter is

slow‚ Level 1 (C1) is

relatively alsoslow

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 17/44

Page 19: Hotspot & AOT

Problem: Application Latency

‚ Wish it to be HFT. . .@Transactional void buyOrSell(Quote quote)

– De-optimization when flow changes– Training workloads

‚ And you meetvoid buy_or_sell [[db:transactional]] (Quote* quote)

CFLAGS_ALL += -O3

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 18/44

Page 20: Hotspot & AOT

Problem: Application Latency

‚ Wish it to be HFT. . .@Transactional void buyOrSell(Quote quote)

– De-optimization when flow changes– Training workloads

‚ And you meetvoid buy_or_sell [[db:transactional]] (Quote* quote)

CFLAGS_ALL += -O3

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 18/44

Page 21: Hotspot & AOT

Problem: BootstrappingMeta-circular implementations

‚ It’s possible to write JVM in Java, Scala or JavaScript‚ ”My dear JVM existing as bytecode image, please start and

make yourself efficient in execution of bytecode. Quickly”‚ Actually the 3 problems above but doubled

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 19/44

Page 22: Hotspot & AOT

Problem: Bootstrapping

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 20/44

Page 23: Hotspot & AOT

Ahead-of-time Compilation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 21/44

Page 24: Hotspot & AOT

Solution: Startup time‚ Pre-compile initialization code

– No interpreter for class loading, reflection etc.– No resources for compilation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 22/44

Page 25: Hotspot & AOT

Solution: Time to performance‚ Pre-compile critical code

– Start with much better than interpreter performance– No resources for compilation

‚ Reach peak performance– Collect same profiling info– JIT with profile-guided optimizations

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 23/44

Page 26: Hotspot & AOT

Solution: Latency‚ Pre-compile critical code

– High and stable performance‚ Optimizations

– No de-optimization (almost)– No re-compilation (almost)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 24/44

Page 27: Hotspot & AOT

Solution: Density, Power ConsumptionFor free

‚ Some critical code is pre-compiled‚ Share it

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 25/44

Page 28: Hotspot & AOT

Pre-compilation: Different Solutions Exist‚ AOT whole application to native executable

– Native exe/elf– Trial runs for better image layout– Bundled or shared VM– Deep dependency analysis– Pre-defined mode– JIT is secondary

‚ VM with JIT and AOT compilers– Optional cache for class data and code– Trial runs for methods filtering

‚ Replay recorded compilations and optimizations

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 26/44

Page 29: Hotspot & AOT

Pre-compilation: For Hotspot‚ Need to generate code

– Mostly no de-optimizations– Better than C1

‚ No tight time budget‚ Need to resolve and load generated code

‚ How about one more compiler?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 27/44

Page 30: Hotspot & AOT

Pre-compilation: For Hotspot‚ Need to generate code

– Mostly no de-optimizations– Better than C1

‚ No tight time budget‚ Need to resolve and load generated code‚ How about one more compiler?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 27/44

Page 31: Hotspot & AOT

Graal

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 28/44

Page 32: Hotspot & AOT

Graal: Project

‚ Experimental dynamic compiler written in Java‚ Supports Java‚ OpenJDK project

http://openjdk.java.net/projects/graal/‚ Oracle Labs team‚ GraalVM based on Hotspot

http://www.oracle.com/technetwork/oracle-labs/program-languages/overview/index.html

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 29/44

Page 33: Hotspot & AOT

Graal: For AOT‚ It proven to work

– SubstrateVM‚ Flexible and handy

– Modular– Annotation based way

‚ Possible to avoid most de-optimizations– No speculative optimizations– Compile all paths

‚ Focused on performance

‚ How does it interact with Hotspot?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 30/44

Page 34: Hotspot & AOT

Graal: For AOT‚ It proven to work

– SubstrateVM‚ Flexible and handy

– Modular– Annotation based way

‚ Possible to avoid most de-optimizations– No speculative optimizations– Compile all paths

‚ Focused on performance‚ How does it interact with Hotspot?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 30/44

Page 35: Hotspot & AOT

JVM Compiler Interface

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 31/44

Page 36: Hotspot & AOT

JEP 243: Java-Level JVM Compiler Interface

‚ OpenJDK feature, already in 9http://openjdk.java.net/jeps/243

‚ Experimental feature

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 32/44

Page 37: Hotspot & AOT

JEP 243: Goals‚ Allow the JVM to load Java plug-in code to examine and

intercept JVM JIT activity.‚ Record events related to compilation, including counter

overflow, compilation requests, speculation failure, anddeoptimization.

‚ Allow queries to relevant metadata, including loaded classes,method definitions, profile data, dependencies (speculativeassertions), and compiled code cache.

‚ Allow an external module to capture compilation requests andproduce code to be used for compiled methods.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 33/44

Page 38: Hotspot & AOT

JVMCI: Graal as C2 Replacement-XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler

[-Djvmci.Compiler=graal]

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 34/44

Page 39: Hotspot & AOT

JVMCI: Details‚ Not used for C1, C2‚ Special module jdk.vm.ci‚ Familiar extension patterns

– CompilerFactory, StartupEventListener,HotSpotJVMCIBackendFactory, HotSpotVMEventListener. . .

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 35/44

Page 40: Hotspot & AOT

JVMCI: How it worksHotspot

‚ Compilation Queue‚ Metaspace‚ Code Cache

JVMCI Compiler‚ Compilation Request‚ jdk.vm.ci.meta‚ byte[]

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 36/44

Page 41: Hotspot & AOT

JVMCI: How about this?Hotspot

‚ Queue‚ Metaspace‚ Code Cache

Proxy Network Proxy

Compilation Server‚ Request‚ jdk.vm.ci.meta‚ byte[]

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 37/44

Page 42: Hotspot & AOT

Artifacts

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 38/44

Page 43: Hotspot & AOT

Code: AOT Modes‚ Targeted at problem

– Tiered. Similar to Level 2– Non-Tiered – Latency

‚ Targeted at VM mode‚ Defined by Graal/AOT options (profiling, thresholds etc.)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 39/44

Page 44: Hotspot & AOT

Code: AOT & Tired‚ Tiered

– AOT Ñ level 3 Ñ AOT Ñ level 4‚ Non-Tiered

– AOT

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 40/44

Page 45: Hotspot & AOT

Code: Libraries‚ Native shared library (ELF DSO)

– OS knows how to treat it right– Compatible with tools– Specific to mode– Same runtime

‚ Modified Hotspot that works with compiled methods fromshared libraries

‚ New jaotc tool for compilation– Modules– Jars– Classes

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 41/44

Page 46: Hotspot & AOT

Code: libjava.base.so, 240 MB

55%

12% 33%

‚ Code‚ RW‚ Other

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 42/44

Page 47: Hotspot & AOT

Packaging: Self-contained Apps

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 43/44

Page 48: Hotspot & AOT

Packaging: Self-contained Apps‚ Java Packager

– Prepares fancy .dmg for shiny Mac– Bundled with 100 Mb JRE

‚ JEP 275: Modular Java Application Packaginghttp://openjdk.java.net/jeps/275

– jlink helps to generate a JRE image with the required modules only– Extensions– AOT libs can be created and added

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 44/44