The Java Hotspot VM Under the Hood
Total Page:16
File Type:pdf, Size:1020Kb
The Java HotSpot VM Under the Hood Tobias Hartmann Principal Member of Technical Staff Compiler Group – Java HotSpot Virtual Machine Oracle Corporation August 2017 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | About me • Joined Oracle in 2014 • Software engineer in the Java HotSpot VM Compiler Team – Based in Baden, Switzerland • German citizen • Master’s degree in Computer Science from ETH Zurich • Worked on various compiler-related projects – Currently working on future Value Type support for Java 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. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3 Outline • Intro: Why virtual machines? • Part 1: The Java HotSpot VM – JIT compilation in HotSpot – Tiered Compilation • Part 2: Projects – Segmented Code Cache – Compact Strings – Ahead-of-time Compilation – Minimal Value Types Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 4 A typical computing platform User Applications Application Software Java SE Java EE Java Virtual Machine System Software Operating system Hardware Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 5 A typical computing platform User Applications Application Software Java SE Java EE Java Virtual Machine System Software Operating system Hardware Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 6 A typical computing platform User Applications Application Software Java SE Java EE Java Virtual Machine System Software Operating system Hardware Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 Programming language implementation Programming language C Compiler CompilerCompiler Standard libraries StandardCompiler libraries Language StandardStandard libraries libraries implementation Debugger DebuggerDebugger Memory management MemoryDebugger management MemoryMemory management management Operating Windows Linux system LinuxSolaris Hardware Intel x86 Intel x86 ARMSPARC Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 8 (Language) virtual machine Programming language Java JavaScript Scala Python Virtual machine HotSpot VM Operating Windows Linux Mac OS X Solaris system Hardware Intel x86 PPC ARM SPARC Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 9 Outline • Intro: Why virtual machines? • Part 1: The Java HotSpot VM – JIT compilation in HotSpot – Tiered Compilation • Part 2: Projects – Segmented Code Cache – Compact Strings – Ahead-of-time Compilation – Minimal Value Types Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10 The JVM: An application developer’s view Java source code Bytecodes HotSpot int i = 0; compile 0: iconst_0 execute Java VM do { 1: istore_1 i++; 2: iinc } while (i < f()); 5: iload_1 6: invokestatic f 9: if_icmplt 2 12: return • Ahead-of-time • Using javac • Instructions for an abstract machine • Stack-based machine (no registers) Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11 The JVM: A VM engineer’s view Bytecodes HotSpot Java VM 0: iconst_0 1: istore_1 compile Compilation produce Compiled method Garbage 2: iinc system collector 5: iload_1 6: invokestatic f 9: if_icmplt 2 C1 Machine code 12: return C2 Debug info manage Object maps execute access Heap Interpreter access Stack Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12 Outline • Intro: Why virtual machines? • Part 1: The Java HotSpot VM – JIT compilation in HotSpot – Tiered Compilation • Part 2: Projects – Segmented Code Cache – Compact Strings – Minimal Value Types Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13 Interpretation vs. compilation in HotSpot • Template-based interpreter – Generated at VM startup (before program execution) – Maps a well-defined machine code sequence to every bytecode instruction Bytecodes 0: iconst_0 Machine code 1: istore_1 mov -0x8(%r14), %eax Load local variable 1 2: iinc movzbl 0x1(%r13), %ebx 5: iload_1 inc %r13 6: invokestatic f Dispatch next instruction mov $0xff40,%r10 9: if_icmplt 2 jpmq *(%r10, %rbx, 8) 12: return • Compilation system – Speedup relative to interpretation: ~100X – Two just-in-time compilers (C1, C2) – Aggressive optimistic optimizations Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14 Ahead-of-time vs. just-in-time compilation • AOT: Before program execution • JIT: During program execution – Tradeoff: Resource usage vs. performance of generated code Performance Bad performance Good performance Bad performance due to interpretation due to good selection of due to compilation compiled methods and overhead applied optimizations Amount of compilation Interpretation Compile everything Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 15 JIT compilation in HotSpot • Resource usage vs. performance – Getting to the “sweet spot” 1. Selecting methods to compile 2. Selecting compiler optimizations Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 16 1. Selecting methods to compile • Hot methods (frequently executed methods) • Profile method execution – # of method invocations, # of backedges • A method’s lifetime in the VM Compiler’s optimistic assumptions proven wrong Deoptimization # method invocations > THRESHOLD1 # of backedges > THRESHOLD2 Interpreter Compiler (C1 or C2) Code cache Gather profiling information Compile bytecode to native code Store machine code Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 17 Example optimization: Hot path compilation Control flow graph Generated code S1; guard(x > 3) S2; S1; S ; S3; 2 Deoptimize if (x > 3) S3; S4; S ; 10’000 0 8 True False S9; S4; S5; S6; S7; S8; S9; Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 18 Example optimization: Virtual call inlining Class hierarchy Method to be compiled class A { void foo() { void bar() { A a = create(); // return A or B Compiler: S1; loaded a.bar(); } } Inline call? } Yes. class B extends A { void bar() { S2; not loaded } } Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 19 Example optimization: Virtual call inlining Class hierarchy Method to be compiled class A { void foo() { void bar() { A a = create(); // return A or B Compiler: S1; loaded S1; } } Inline call? } Yes. class B extends A { • Benefits of inlining void bar() { – Virtual call avoided S2; not loaded } – Code locality } • Optimistic assumption: only A is loaded – Note dependence on class hierarchy – Deoptimize if hierarchy changes Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 20 Example optimization: Virtual call inlining Class hierarchy Method to be compiled class A { void foo() { void bar() { A a = create(); // return A or B Compiler: S1; loaded a.bar(); } } Inline call? } No. class B extends A { void bar() { S2; loaded } } Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 21 List of compiler optimizations* • compiler tactics • language-specific techniques • loop transformations – delayed compilation – class hierarchy analysis – loop unrolling – tiered compilation – devirtualization – loop peeling – on-stack replacement – symbolic constant propagation – safepoint elimination – delayed reoptimization – autobox elimination – iteration range splitting – program dependence graph rep. – escape analysis – range check elimination – static single assignment rep. – lock elision – loop vectorization • proof-based techniques – lock fusion • global code shaping – exact type inference – de-reflection – inlining (graph integration) – memory value inference • speculative (profile-based) techniques – global code motion – memory value tracking – optimistic nullness assertions – heat-based code layout – constant folding – optimistic type assertions – switch balancing – Reassociation – optimistic type strengthening – throw inlining – operator strength reduction – optimistic array length strengthening • control flow graph transformation – null check elimination – untaken branch pruning – local code scheduling – type test strength reduction – optimistic N-morphic inlining – local code bundling – type test elimination – branch frequency prediction – delay slot filling – algebraic simplification – call frequency prediction – graph-coloring register allocation – common subexpression elimination • memory and placement transformation – linear scan register allocation – integer range typing – expression hoisting – live range splitting • flow-sensitive rewrites – expression sinking – copy coalescing – conditional constant propagation – redundant store elimination – constant splitting – dominating test detection – adjacent store fusion – copy removal – flow-carried type narrowing – card-mark elimination – address mode matching – dead code elimination – merge-point splitting – instruction peepholing – DFA-based code generator *Incomplete list, courtesy of Vladimir Ivanov Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 22 Deoptimization • Compiler’s optimistic assumption proven wrong – Assumptions about class hierarchy