The Java HotSpot Virtual Machine Client Compiler...

45
2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and The Java HotSpot Virtual Machine Client Compiler: Technology and Application Robert Griesemer Srdjan Mitrovic Senior Staff Engineers Sun Microsystems, Inc.

Transcript of The Java HotSpot Virtual Machine Client Compiler...

Page 1: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application

The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application

Robert GriesemerSrdjan MitrovicSenior Staff EngineersSun Microsystems, Inc.

Page 2: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 2

Overall Presentation Goal

Learn about “Java HotSpot” compilation in the Java HotSpot™ Virtual Machine, and the Client Compiler

Understand how the Client Compiler deals with specific Java programming language features

Get to know tuning and trouble-shooting techniques and compiler version differences

Page 3: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 3

Learning Objectives

As a result of this presentation, you will be able to: Understand “Java HotSpot compilation” Write better code in the Java programming

language Improve the performance of your applications Try work-arounds in case of compiler issues Understand the impact of different versions

Page 4: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 4

Speakers’ Qualifications

Robert Griesemer is a principal architect of the Java HotSpot VM and the Client Compiler Robert has more than a decade of experience

with programming language implementation He has been with the Java HotSpot team since

its inception in 1994

Srdjan Mitrovic is a principal architect of the Java HotSpot Client Compiler Srdjan has more than a decade of experience

with compilers and run-time systems He has been with the Java HotSpot

team since 1996

Page 5: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 5

Presentation Agenda

Compilation in the Java HotSpot™ VM

Structure of the Client Compiler

Implications for Code written in the Java™ Programming Language

Miscellaneous

Summary, Demo, and Q&A

Page 6: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 6

Compilation in the Java HotSpot™ VM

VM configurations

Compilation steps

On-stack replacement

Deoptimization

Quick summary

Page 7: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 7

VM Configurations

libjvm.so / jvm.dll

Core VM ?

Java OS Libraries

OSHardware

Client Compiler

Server Compiler

javajava -hotspot

java -server

topic of this talk

Typi

cal J

ava

VM S

oftw

are

Stac

k

Page 8: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 8

Compilation Steps

Every method is interpreted first

Hot methods are scheduled for compilation Method invocations Loops

Compilation can be foreground/background Foreground compilation default for Client VM Background compilation in parallel

Page 9: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 9

On-Stack Replacement (1)

Choice between interpreted/compiled execution

Problem with long-running interpreted methods Loops!

Need to switch to compiled method in the middle of interpreted method execution On-Stack Replacement (OSR)

Page 10: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 10

On-Stack Replacement (2)

StackBefore OSR

void m1() { ... while (i < n-1) { // OSR here ... } ...}

StackAfter OSR

OSR

m1interpreted

frame

m0 frame m0 frame

m1 compiled

frame

dead m1 frame

Page 11: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 11

Deoptimization (1)

Compile-time assumptions may become invalid over time Class loading

Debugging of program desired Single-stepping

Active compiled methods become invalid Need to switch to interpreted method in the

middle of compiled method execution Deoptimization

Page 12: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 12

Deoptimization (2)

T3 m3(...) { ... // Deopt. here}

T2 m2(...) { m3(...);}

T1 m1() { ... m2(...); ...}

Stack

Deopt.

m0 frame

m1interpreted

m2interpreted

m3 interpr.

m0 frame

comp. m1w/ inlinedm2, m3frame

Page 13: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 13

Quick Summary

Client VM differs from Server VM in compiler

Hotspots trigger compilation Compiled method invocation OSR

Class loading, debugging changes compile-time assumptions Deoptimization

Page 14: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 14

Presentation Agenda

Compilation in the Java HotSpot™ VM

Structure of the Client Compiler

Implications for Code written in the Java™ Programming Language

Miscellaneous

Summary, Demo, and Q&A

Page 15: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 15

Structure of the Client Compiler

Frontend

Optimizations

Backend

Quick summary

Page 16: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 16

Front-End

Parsing Reading and analyzing method bytecodes

Intermediate Representation (IR) Internal representation for a method Control Flow Graph (CFG) Retain as much bytecode info as possible

Optimizations Code Order

Reorder CFG for code generation

Page 17: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 17

Intermediate Representation

Bytecodes

0 aload_1 1 bipush 46 3 invokevirtual #139 6 istore_2 7 iload_2 8 ifgt 1811 aload_012 getfield #215 invokevirtual #9318 aload_119 iconst_020 ...

basic block

instructions

successor(s)

CFG

Page 18: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 18

Optimizations

Constant folding

Simple form of value numbering

Load elimination

Dead code elimination

Block elimination

Null check elimination

Inlining

Page 19: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 19

Class Hierarchy Analysis (1)

Dynamic pruning of receiver class set Static calls instead of virtual calls Inlining across virtual calls Faster type tests

Class Hierarchy Analysis (CHA) Analysis of loaded classes Can change over time Effective

Page 20: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 20

Class Hierarchy Analysis (2)

Avirtual m

Bvirtual m

Dvirtual n

Cvirtual m

Evirtual n

F

extends

loadedunloaded

Gvirtual m

A a;B b;C c;D d;E e;F f;G g;

a.m A.m, B.mb.m B.md.m A.md.n D.nf.m A.mf.n D.n

Page 21: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 21

Backend

Register Allocation

Low-Level IR (LIR) JDK™ 1.4 release only Some additional optimizations

Code Generation

Page 22: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 22

Code Generation

Machine code:

pushl %ebpmovl %esp,%ebpsubl 40,%espmovl %eax,-4(%esp,1)movl %eax,-8(%esp,1)movl 8(%ebp),%esicmpl 24(%ebp),%esijge 0xaeeff5movl 8(%ebp),%esicmpl 0,%esijge 0xaeef03movl 0x0,8(%ebp)...

basic block

instructions

successor(s)

CFG

Page 23: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 23

Quick Summary

Frontend does Parse and analyze bytecodes Build and optimize IR Use CHA for very effective OO optimizations Reorder CFG for code generation

Backend does LIR (JDK™ 1.4 release only) Register allocation, low-level optimizations Code generation

Page 24: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 24

Presentation Agenda

Compilation in the Java HotSpot™ VM

Structure of the Client Compiler

Implications for Code written in the Java™ Programming Language

Miscellaneous

Summary, Demo, and Q&A

Page 25: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 25

Implications for Code Written inthe Java™ Programming Language

Accessors

Usage of final

Object allocation (new)

Exception handling

Other issues

Quick summary

Page 26: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 26

Accessors

Use accessors x_type get_x() { return _x; }

void set_x(x_type x) { _x = x; }

Abstracts from implementation

Easier to maintain

No performance penalty Inlining!

Page 27: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 27

Usage of final

Don’t use final for performance tuning

CHA will do the work Where CHA can’t do it, final doesn’t

help either

Keep software extensible

No performance penalty Static calls Inlining

Page 28: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 28

Object Allocation (new)

Object allocation (new) inlined

Works in most cases

Extremely fast (~ 10–20 clock cycles)

Do not manage memory yourself

GC will slow down

Larger memory footprint

Keep software simple

Page 29: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 29

Exception Handling

Exception object creation is very expensive

Stack trace

Exception handling is not optimized

Use it for exceptional situations

Don’t use it as programming paradigm

Don’t use instead of regular return

Exception handling costs only when used

Safe to declare exceptions

Page 30: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 30

Other Issues

Client Compiler optimized for clean OO code “Hand-tuning” often counterproductive Generated code can be problematic

Obfuscators

Do not optimize prematurely Use profiling information

Page 31: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 31

Quick Summary

Write clean OO code Use accessors Use final by design only Use new for object allocation Use exception handling for exceptional cases

Keep it simple, keep it clean

Page 32: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 32

Presentation Agenda

Compilation in the Java HotSpot™ VM

Structure of the Client Compiler

Implications for Code written in the Java™ Programming Language

Miscellaneous

Summary, Demo, and Q&A

Page 33: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 33

Miscellaneous

Flags

Built-in profiler

Version differences

When to use the client compiler

Quick summary

Page 34: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 34

Flags (1)

No flag tuning for compiler required Use standard command line flags Special situations

-Xint -XX:+PrintCompilation

JDK™ 1.3.1, JDK™ 1.4 technology .hotspotrc .hotspot_compiler

Page 35: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 35

Flags (2)

Usage -XX:+FlagName, -XX:-FlagName

Flags and default setting -BackgroundCompilation

Foreground/background compilation +UseCompilerSafepoints

May help in presence of crashes +StackTraceInThrowable

Disable to turn off stack traces in exceptions

Page 36: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 36

Built-in Profiler

Option: -Xprof E.g.: java -Xprof -jar Java2Demo.jar

Statistical (sampling) flat profiler Not hierarchical

Per thread Output when thread terminates

Page 37: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 37

Sample Profiler Output

Flat profile of 27.38 secs (2574 total ticks): AWT-EventQueue-0

Interpreted + native Method 7.2% 0 + 90 sun.java2d.loops.Blit.Blit 0.7% 0 + 9 sun.awt.windows.Win32BlitLoops.Blit ... 19.8% 72 + 174 Total interpreted (including elided)

Compiled + native Method 9.2% 115 + 0 java.awt.GradientPaintContext.clip... ... 15.0% 179 + 8 Total compiled (including elided)

Thread-local ticks: 51.7% 1330 Blocked (of total) 0.2% 2 Class loader 0.3% 4 Interpreter 10.0% 124 Compilation 0.5% 6 Unknown: running frame 0.2% 2 Unknown: thread_state

Page 38: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 38

Version Differences

Corresponding JDK™ 1.3 1.3.1 1.4Unified source base n/y* yes yesOSR yes yes yesSimple inlining yes yes noFull inlining no no yesDeoptimization no no yesMore optimizations no no yes

* SPARC™ processor implementation only

Page 39: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 39

When to Use the Client Compiler

Client Compiler characteristics Fast compilation

Quick startup time Small footprint

Use for apps with same expectations Recommendation

Try client and server, choose best java -hotspot

java -server

Page 40: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 40

Quick Summary

No flag tuning required Help for special situations

Profiler for program tuning

Minor version differences only Faster code with JDK™ 1.4 release

Try both compilers for optimal solution

Page 41: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 41

Presentation Agenda

Compilation in the Java HotSpot™ VM

Structure of the Client Compiler

Implications for Code written in the Java™ Programming Language

Miscellaneous

Summary, Demo, and Q&A

Page 42: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 42

Overall Summary

Java HotSpot™ compilation

Client compiler internals

Programming and tuning hints

More information at the BOFs BOF-2697 BOF-2639

Page 43: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 43

References

Robert Griesemer, Srdjan Mitrovic: “A Compiler for the Java HotSpot™ Virtual Machine”, in The School of Niklaus Wirth—The Art of Simplicity, Morgan Kaufmann Publishers, 2000, ISBN 1-55860-723-4

Java HotSpot™ Technology Documentshttp://java.sun.com/products/hotspot/2.0/docs.html

Page 44: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application

Page 45: The Java HotSpot Virtual Machine Client Compiler ...reins.altervista.org/java/javaOne2001_2696.pdf · 2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and 3 Application

2696, The Java HotSpot™ Virtual Machine Client Compiler: Technology and Application 45