SlideShare una empresa de Scribd logo
1 de 89
Descargar para leer sin conexión
Douglas Q. Hawkins
VM Engineer
VM MechanicsWhen Does the JVM JIT & Deoptimize?
J
https://github.com/dougqh/jvm-mechanics
About Azul Systems
Zulu®
Multi-Platform OpenJDK
Cloud Support including Docker and Azure
Embedded Support
Zing®
Highly ScalableVM
Continuously Concurrent Compacting Collector
ReadyNow! for Low Latency Applications
http://www.azulsystems.com/
HotSpot Lifecycle
1 2
34
Interpret Profile
Just-in-Time
CompilationDeoptimize
?Why?
public class SimpleProgram {
static final int CHUNK_SIZE = 1_000;
public static void main(String[] args) {
for ( int i = 0; i < 250; ++i ) {
long startTime = System.nanoTime();
for ( int j = 0; j < CHUNK_SIZE; ++j ) {
new Object();
}
long endTime = System.nanoTime();
System.out.printf("%dt%d%n", i, endTime - startTime);
}
}
}
A Simple Program
example01a.SimpleProgram
Code Reference
0
400000
800000
1200000
1600000
0
13
26
39
52
65
78
91
104
117
130
143
156
169
182
195
208
221
234
247
Simple Program Performance
GC Pauses!
1
1000
1000000
0
12
24
36
48
60
72
84
96
108
120
132
144
156
168
180
192
204
216
228
240
Interpreter 1st JIT 2nd JIT
Log Scale
public class NotSoSimpleProgram {
static final int CHUNK_SIZE = 1_000;
public static void main(String[] args) {
Object trap = null;
for ( int i = 0; i < 250; ++i ) {
long startTime = System.nanoTime();
for ( int j = 0; j < CHUNK_SIZE; ++j ) {
new Object();
if ( trap != null ) {
System.out.println("trap!");
trap = null;
}
}
if ( i == 200 ) trap = new Object();
long endTime = System.nanoTime();
System.out.printf("%dt%d%n", i, endTime - startTime);
}
}
}
Not So Simple Program
example01b.NotSoSimpleProgram
Deoptimization
1
1000
1000000
0
12
24
36
48
60
72
84
96
108
120
132
144
156
168
180
192
204
216
228
240
behavioral change,
deoptimize!
Dynamically Generated
Threaded Interpreter
Identify “Hot Spots”
Slow!
10000x
Interpreter
http://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html
Invocation Counter
example02.InvocationCounter
public class InvocationCounter {
public static void main(final String[] args)
throws InterruptedException
{
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
System.out.println("Waiting for compiler...");
Thread.sleep(5_000);
}
static void hotMethod() {}
}
-XX:+PrintCompilation
example02.InvocationCounter
299 1 % java.lang.String::indexOf @ 37 (70 bytes)
332 2 java.lang.String::indexOf (70 bytes)
364 3 example02.InvocationCounter::hotMethod (1 bytes)
365 4 % example02.InvocationCounter::main @ 5 (33 bytes)
Waiting for compiler...
Invocation Counter
Compilation Log
5328 50 n java.io.FileOutputStream::writeBytes (native)
5330 27 java.nio.CharBuffer::wrap (20 bytes)
5333 31 s java.io.BufferedOutputStream::flush (12 bytes)
5477 56 % CompilationExample::main @ 37 (82 bytes)
timestamp (since VM start)
compilation ID method name method size
native
exception handler
synchronized method
on-stack replacement loop bytecode index
!
synchronized is try/finally
synchronized ( foo.getBar() ) {
...
}
tmp = foo.getBar();
monitor_enter(tmp);
try {
...
} finally {
monitor_exit(tmp);
}
Duplicate Methods &
Overloading
5208 4 java.nio.Buffer::position (5 bytes)
...
5223 8 java.nio.Buffer::position (43 bytes)
Invisible Overloads
via Bridge Methods
public interface Supplier<T> {
public abstract T get();
}
new Supplier<String>() {
public final String get() { return “foo”; }
};
145 4 InvisibleOverload$1::get (5 bytes)
145 5 InvisibleOverload$1::get (3 bytes)
public class BackedgeCounter {
public static void main(final String[] args)
throws InterruptedException
{
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
System.out.println("Waiting for compiler...");
Thread.sleep(5_000);
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
System.out.println("Waiting for compiler...");
Thread.sleep(5_000);
}
static void hotMethod() {}
}
Backedge Counter
example03.BackedgeCounter
163 1 java.lang.String::charAt (29 bytes)
166 2 java.lang.String::hashCode (55 bytes)
171 3 java.lang.String::indexOf (70 bytes)
193 4 example03.BackedgeCounter::hotMethod (1 bytes)
194 5 % example03.BackedgeCounter::main @ 5 (65 bytes)
Waiting for compiler...
5196 6 % example03.BackedgeCounter::main @ 37 (65 bytes)
Waiting for compiler...
-XX:+PrintCompilation
example03.BackedgeCounter
Backedge Counter
On-Stack Replacement
eventLoop
...
...
main
eventLoop @ 20
...
...
main
interpreter frame
compiled frame
Both Counters
public class BothCounters {
public static void main(final String[] args)
throws InterruptedException
{
for ( int i = 0; i < 2; ++i ) {
outerMethod();
}
System.out.println("Waiting for compiler...");
Thread.sleep(5000);
}
static void outerMethod() {
for ( int i = 0; i < 10_000; ++i ) {
innerMethod();
}
}
static void innerMethod() {}
}
example04.BothCounters
-XX:+PrintCompilation
example04.BothCounters
Both Counters
115 1 % java.lang.String::indexOf @ 37 (70 bytes)
123 2 example04.BothCounters::innerMethod (1 bytes)
124 3 example04.BothCounters::outerMethod (19 bytes)
125 4 % example04.BothCounters::outerMethod @ 5 (19 bytes)
Waiting for compiler...
HotSpot:
A Tale of Two Compilers
C1client C2 server
C1 ClientVM
The Fast Acting Compiler
Compiles with Count > 1,000
(2,000 in Tiered)
Produces Compilations Quickly
Generated Code Runs Relatively Slowly
Compiles with Count > 10,000
(15,000 in Tiered)
Profile Guided
C2 ServerVM
The Smart compiler
Speculative
2xSpeed!
Produces Compilations Slowly
Generated Code Runs Fast
Tiered Compilation
Available in Java 7 - default in Java 8
Best of Both Worlds - C1 & C2
Interpreter
> 2,000 > 15,000
C1 C2
http://www.slideshare.net/maddocig/tiered
0
0
0
interpreter
3 4
2 3 4
1 3
c1 c2
none
basic
counters detailed
common
c2 busy
trivial
method
Tiered Compilation
public final class TieredCompilation {
public static final void main(final String[] args)
throws InterruptedException
{
for ( int i = 0; i < 3_000; ++i ) {
method();
}
System.out.println("Waiting for the compiler...");
Thread.sleep(5_000);
for ( int i = 0; i < 20_000; ++i ) {
method();
}
System.out.println("Waiting for the compiler...");
Thread.sleep(5_000);
}
private static final void method() {
// Do something while doing nothing.
System.out.print('0');
}
}
example05.TieredCompilation
Tiered Compilation
183 69 3 sun...SingleByte$Encoder::encodeArrayLoop (236 bytes)
...
5237 101 4 sun...SingleByte$Encoder::encodeArrayLoop (236 bytes)
5255 69 3 sun...SingleByte$Encoder::encodeArrayLoop (236 bytes) made not entrant
131 3 3 java.lang.Object::<init> (1 bytes)
...
140 15 1 java.lang.Object::<init> (1 bytes)
141 3 3 java.lang.Object::<init> (1 bytes) made not entrant
126 6 n 0 java.lang.System::arraycopy (native) (static)
-XX:+TieredCompilation
-XX:+PrintCompilation
example05.TieredCompilation
Tiered Compilation
tier
1
1000
1000000
0
12
24
36
48
60
72
84
96
108
120
132
144
156
168
180
192
204
216
228
240
Interpreter Tier 3 (c1) Tier 4 (c2)
Tiered Compilation
Optimizations
Intrinsics
Special code built-into theVM for a particular method
Built-in to theVM - not written in Java (usually)
For Example...
System.arraycopy
Math.sin / cos / tan
Often use special hardware capabilities:
MMX,AVX2, etc
http://en.wikipedia.org/wiki/Common_subexpression_elimination
Common Sub-Expression
Elimination
int a = b * c + g;
int d = b * c * e;
int tmp = b * c;
int a = tmp + g;
int d = tmp * e;
boolean isDebugEnabled = LOGGER.isDebugEnabled();
for ( User user: users ) {
...do something...
if ( isDebugEnabled ) {
LOGGER.debug(user.getName());
}
}
boolean isDebugEnabled = LOGGER.isDebugEnabled();
if ( isDebugEnabled ) {
for ( User user: users ) {
...do something...
LOGGER.debug(user.getName());
}
} else {
for ( User user: users ) {
...do something...
}
}
Loop Unswitching
http://en.wikipedia.org/wiki/Loop_unswitching
Lock Coarsening
StringBuffer buffer = ...
buffer.append(“Hello”);
buffer.append(name);
buffer.append(“n”);
StringBuffer buffer = ...
lock(buffer); buffer.append(“Hello”); unlock(buffer);
lock(buffer); buffer.append(name); unlock(buffer);
lock(buffer); buffer.append(“n”); unlock(buffer);
StringBuffer buffer = ...
lock(buffer);
buffer.append(“Hello”);
buffer.append(name);
buffer.append(“n”);
unlock(buffer);
http://www.ibm.com/developerworks/library/j-jtp10185/index.html
Dead Code Elimination
http://en.wikipedia.org/wiki/Dead_code_elimination
public int ArrayList::indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
Inlining
“The mother of all optimizations.”
IPO: Inter-procedural Optimization
Intrinsic Inlining
public class Intrinsics {
public static void main(String[] args)
throws InterruptedException
{
int[] data = randomInts(100_000);
int min = Integer.MAX_VALUE;
for ( int x: data ) {
min = Math.min(min, x);
}
Thread.sleep(5_000);
System.out.println(min);
}
static final int[] randomInts(int size) {
…
}
}
example06.Intrinsics
-XX:+PrintCompilation
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintInlining
376 8 % example06.Intrinsics::randomInts @ 13 (30 bytes)
@ 16 java.util.concurrent.ThreadLocalRandom::nextInt (8 bytes) inline (hot)
@ 1 java.util.concurrent.ThreadLocalRandom::nextSeed (32 bytes) inline (hot)
@ 3 java.lang.Thread::currentThread (0 bytes) (intrinsic)
@ 18 sun.misc.Unsafe::getLong (0 bytes) (intrinsic)
@ 27 sun.misc.Unsafe::putLong (0 bytes) (intrinsic)
@ 4 java.util.concurrent.ThreadLocalRandom::mix32 (26 bytes) inline (hot)
379 9 java.lang.Math::min (11 bytes)
379 10 % example06.Intrinsics::main @ 22 (58 bytes)
@ 30 java.lang.Math::min (11 bytes) (intrinsic)
Intrinsic Inlining
Direct Call Inlining
public class Inlining {
public static void main(String[] args)
throws InterruptedException
{
System.setOut(new NullPrintStream());
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
Thread.sleep(5_000);
}
public static void hotMethod() {
System.out.println(square(7));
System.out.println(square(9));
}
static int square(int x) {
return x * x;
}
}
public static void hotMethod() {
System.out.println(7 * 7);
System.out.println(9 * 9);
}
example07.DirectInlining
static, private, constructor calls
Direct Call Inlining
226 53 example07.DirectInlining::hotMethod (23 bytes)
@ 5 example07.DirectInlining::square (4 bytes) inline (hot)
!m @ 8 java.io.PrintStream::println (24 bytes) already compiled into a big method
@ 16 example07.DirectInlining::square (4 bytes) inline (hot)
!m @ 19 java.io.PrintStream::println (24 bytes) already compiled into a big method
228 54 % example07.DirectInlining::main @ 15 (35 bytes)
@ 15 example07.DirectInlining::hotMethod (23 bytes) inline (hot)
@ 5 example07.DirectInlining::square (4 bytes) inline (hot)
!m @ 8 java.io.PrintStream::println (24 bytes) already compiled into a big method
@ 16 example07.DirectInlining::square (4 bytes) inline (hot)
!m @ 19 java.io.PrintStream::println (24 bytes) already compiled into a big method
-XX:+PrintCompilation
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintInlining
Printing Assembly
for a Method
Download hsdis-amd64 dynamic library
Copy to jre/lib directory
Run HotSpot with...
-XX:+UnlockDiagnosticVMOptions
-XX:CompileCommand=print,{package/Class::method}
0x0000000106c57843: mov $0x31,%edx
0x0000000106c57848: data32 xchg %ax,%ax
0x0000000106c5784b: callq 0x0000000106c10b60
; OopMap{rbp=Oop off=48}
;*invokevirtual println
; - DirectInlining::hotMethod@7 (line 17)
; {optimized virtual_call}
0x0000000106c5785d: mov $0x51,%edx
0x0000000106c57862: nop
0x0000000106c57863: callq 0x0000000106c10b60
; OopMap{off=72}
;*invokevirtual println
; - DirectInlining::hotMethod@17 (line 18)
; {optimized virtual_call}
-XX:+UnlockDiagnosticVMOptions
-XX:CompileCommand=print,example07/DirectInlining::hotMethod
Direct Call Inlining
Escape Analysis
long sum = 0;
for ( Long x: list ) {
sum += iter.next();
}
long sum = 0;
for ( Iterator<Long> iter = list.iterator();
iter.hasNext(); )
{
sum += iter.next();
}
Escape Analysis
http://psy-lob-saw.blogspot.ru/2014/12/the-escape-of-arraylistiterator.html
long sum = 0;
// ArrayList$Itr.<init>
int iter$size = list.size();
int iter$cursor = 0;
int iter$lastRet = -1;


// ArrayList$Itr.hasNext
for ( ; iter$cursor != iter$size; ) {
// ArrayList$Itr.next
int i = iter$cursor;
Object[] elementData = list.elementData;
iter$cursor = i + 1;
Long x = (Long)elementData[iter$lastRet = i];
sum += x;
}
public class SimpleProgram {
static final int CHUNK_SIZE = 1_000;
public static void main(String[] args) {
for ( int i = 0; i < 250; ++i ) {
long startTime = System.nanoTime();
for ( int j = 0; j < CHUNK_SIZE; ++j ) {
//new Object

obj = calloc(sizeof(Object));
obj.<init>(); // call ctor - empty
}
long endTime = System.nanoTime();
System.out.printf(
"%dt%d%n", i, endTime - startTime);
}
}
}
Simple Program Revisited
1: inlined
2: escape analysis /
dead store
3: empty loop /
eliminate loop
example01a.SimpleProgram
1
1000
1000000
0
12
24
36
48
60
72
84
96
108
120
132
144
156
168
180
192
204
216
228
240
Simple Program Revisited
Interpreter Tier 3 (c1) Tier 4 (c2)
That’s Boring!
Speculative
Optimizations
!
Implicit Null Check
public class NullCheck {
public static void main(String[] args)
throws InterruptedException
{
for ( int i = 0; i < 20_000; ++i ) {
hotMethod("hello");
}
Thread.sleep(5_000);
for ( int i = 0; i < 10; ++i ) {
System.out.printf("tempting fate %d%n", i);
try {
hotMethod(null);
} catch ( NullPointerException e ) {
// ignore
}
}
}
static final void hotMethod(final Object value) {
value.hashCode();
}
}
example08a.NullCheckhttps://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/signals.html
-XX:+UnlockDiagnosticVMOptions
-XX:CompileCommand=print,example08a/NullCheck::hotMethod
0x000000010795f9c0: mov %eax,-0x14000(%rsp)
0x000000010795f9c7: push %rbp
0x000000010795f9c8: sub $0x50,%rsp
;*synchronization entry
example08a.NullCheck::hotMethod@-1 (line 26)
0x000000010795f9cc: mov 0x8(%rsi),%r10d
; implicit exception: dispatches to 0x000000010795fe1d
0x000000010795f9d0: movabs $0x7eaa80d10,%r11
; {oop(a 'java/lang/Class' = 'java/lang/System')}
0x000000010795f9da: mov 0x74(%r11),%r11d
;*getstatic out
Implicit Null Check
value.toString(); Possible, but
improbable NPE
if ( value == null ) {
throw new NullPointerException();
}
SEGVderef
value
signal
handler
throw
NPE
Implicit Null Check
Null Check Deoptimization
-XX:+PrintCompilation
124 1 java.lang.String::hashCode (55 bytes)
138 2 example08a.NullCheck::hotMethod (6 bytes)
138 3 % ! example08a.NullCheck::main @ 5 (69 bytes)
tempting fate 0
tempting fate 1
tempting fate 2
5147 2 example08a.NullCheck::hotMethod (6 bytes) made not entrant
tempting fate 3
tempting fate 4
tempting fate 5
tempting fate 6
tempting fate 7
tempting fate 8
tempting fate 9
Not Thrown Away
the First Time
Bail to Interpreter
Keep Compile?
Yes No, recompile when...
Now Later (Profile More)
If Less Than
3 Deopts?
Null Proportion: 0.100000 Caught: 10057 Unique: 2015
Null Proportion: 0.500000 Caught: 50096 Unique: 7191
Null Proportion: 0.900000 Caught: 89929 Unique: 11030
int caughtCount = 0;
Set<NullPointerException> nullPointerExceptions =
new HashSet<>();
for ( Object object : objects ) {
try {
object.toString();
} catch ( NullPointerException e ) {
nullPointerExceptions.add( e );
caughtCount += 1;
}
}
Hot Exception Optimization
example08b.HotExceptionDemohttp://www.javaspecialists.eu/archive/Issue187.html
Hot Exceptions
int caughtCount = 0;
HashSet<NullPointerException> nullPointerExceptions =
new HashSet<>();
for ( Object object : objects ) {
try {
object.toString();
} catch ( NullPointerException e ) {
boolean added = nullPointerExceptions.add(e);
if ( !added ) e.printStackTrace();
caughtCount += 1;
}
}
java.lang.NullPointerException
No StackTrace???
public class Unreached {
public static volatile Object thing = null;
public static void main(final String[] args)
throws InterruptedException
{
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
Thread.sleep(5_000);
thing = new Object();
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
Thread.sleep(5_000);
}
static final void hotMethod() {
if ( thing == null )
System.out.print("");
else
System.out.print("");
}
}
Unreached Deoptimization
example09.Unreached
phase change
static final void hotMethod() {
if ( thing == null )
System.out.print("");
else
uncommon_trap(unreached);
}
-XX:+PrintCompilation
Unreached Deoptimization
217 1 java.lang.String::hashCode (55 bytes)
235 2 java.lang.String::indexOf (70 bytes)
238 3 java.io.BufferedWriter::ensureOpen (18 bytes)
244 4 java.lang.String::length (6 bytes)
244 5 java.lang.String::indexOf (7 bytes)
245 6 java.nio.Buffer::position (5 bytes)
245 7 example09.Unreached::hotMethod (26 bytes)
…
265 14 java.io.OutputStreamWriter::flushBuffer (8 bytes)
265 15 ! sun.nio.cs.StreamEncoder::flushBuffer (42 bytes)
267 16 sun.nio.cs.StreamEncoder::isOpen (5 bytes)
267 17 sun.nio.cs.StreamEncoder::implFlushBuffer (15 bytes)
267 18 % example09.Unreached::main @ 5 (59 bytes)
5255 7 example09.Unreached::hotMethod (26 bytes) made not entrant
5257 19 example09.Unreached::hotMethod (26 bytes)
5257 20 % example09.Unreached::main @ 39 (59 bytes)
Bail to Interpreter
hotMethod
...
...
main
hotMethod
...
...
main
interpreter frame
compiled frame
Why Speculate?
y *= 2;
x = 0;
y = 25;
y = -20;
x = 0;
y = 25;
y = 30; trap!y = 15;
Not So Simple Program Revisited
1
1000
1000000
0
12
24
36
48
60
72
84
96
108
120
132
144
156
168
180
192
204
216
228
240
unreached deopt,
bail to interpreter!
Virtual Call Inlining
Func
+apply(double):double
Square
+apply(double):double
Sqrt
+apply(double):double
Class Hierarchy Analysis (CHA)
Type Profile
http://shipilev.net/blog/2015/black-magic-method-dispatch/
public class Monomorphic {
public static void main(String[] args)
throws InterruptedException
{
Func func = new Square();
for ( int i = 0; i < 20_000; ++i ) {
apply(func, i);
}
Thread.sleep(5_000);
}
static double apply(Func func, int x) {
return func.apply(x);
}
}
Monomorphic
example10a.Monomorphic
217 1 java.lang.String::hashCode (55 bytes)
234 3 example10.support.Square::apply (4 bytes)
234 2 example10a.Monomorphic::apply (7 bytes)
@ 3 example10.support.Square::apply (4 bytes) inline (hot)
234 4 % example10a.Monomorphic::main @ 13 (30 bytes)
@ 15 example10a.Monomorphic::apply (7 bytes) inline (hot)
@ 3 example10.support.Square::apply (4 bytes) inline (hot)
Monomorphic
-XX:+PrintCompilation
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintInlining
Beyond Monomorphic
Func func = …
double result = func.apply(20);
Func func = …
//no type guard!
double result = 20 * 20;
More Types?
example10b.ChaStorm
public class ChaStorm {
public static void main(String[] args)
throws InterruptedException
{
Func func = new Square();
for ( int i = 0; i < 10_000; ++i ) {
apply1(func, i);
…
apply8(func, i);
}
System.out.println("Waiting for compiler...");
Thread.sleep(5_000);
System.out.println("Deoptimize...");
System.out.println(Sqrt.class);
Thread.sleep(5_000);
}
}
Potential for Deopt Storm
Potential for Deopt Storm
152 1 java.lang.String::hashCode (55 bytes)
166 2 example10.support.Square::apply (4 bytes)
173 3 example10b.ChaStorm::apply1 (7 bytes)
173 4 example10b.ChaStorm::apply2 (7 bytes)
Waiting for compiler...
174 5 example10b.ChaStorm::apply3 (7 bytes)
174 6 example10b.ChaStorm::apply4 (7 bytes)
174 7 example10b.ChaStorm::apply5 (7 bytes)
174 8 example10b.ChaStorm::apply6 (7 bytes)
174 9 example10b.ChaStorm::apply7 (7 bytes)
174 10 example10b.ChaStorm::apply8 (7 bytes)
Deoptimize...
5176 9 example10b.ChaStorm::apply7 (7 bytes) made not entrant
5176 8 example10b.ChaStorm::apply6 (7 bytes) made not entrant
5176 7 example10b.ChaStorm::apply5 (7 bytes) made not entrant
5176 5 example10b.ChaStorm::apply3 (7 bytes) made not entrant
5176 6 example10b.ChaStorm::apply4 (7 bytes) made not entrant
5176 4 example10b.ChaStorm::apply2 (7 bytes) made not entrant
5176 3 example10b.ChaStorm::apply1 (7 bytes) made not entrant
5176 10 example10b.ChaStorm::apply8 (7 bytes) made not entrant
class example10.support.Sqrt
-XX:+PrintCompilation
Another Way to Deopt
Bail to Interpreter
Keep Compile?
Yes No, recompile when...
Now Later (Profile More)
class load!
stop the world,
deopt now!
Total time for which application threads were stopped: 0.0001010 seconds
5096 10 example10b.ChaStorm::apply8 (7 bytes) made not entrant
5096 8 example10b.ChaStorm::apply6 (7 bytes) made not entrant
5096 7 example10b.ChaStorm::apply5 (7 bytes) made not entrant
5096 5 example10b.ChaStorm::apply3 (7 bytes) made not entrant
5096 6 example10b.ChaStorm::apply4 (7 bytes) made not entrant
5096 4 example10b.ChaStorm::apply2 (7 bytes) made not entrant
5096 9 example10b.ChaStorm::apply7 (7 bytes) made not entrant
5096 3 example10b.ChaStorm::apply1 (7 bytes) made not entrant
vmop [threads: total initially_running wait_to_block] …
5.096: Deoptimize [ 7 0 0 ] …
Another Way to Deopt
-XX:+PrintCompilation
-XX+PrintSafepointStatistics
-XX:PrintSafepointStatisticsCount=1
http://blog.ragozin.info/2012/10/safepoints-in-hotspot-jvm.html
Bimorphic
Func func = …
double result = func.apply(20);
Func func = …
//no type guard!
double result = 20 * 20;
add another type
Func func = …
double result;
if ( func.getClass().equals(Square.class) ) {
result = 20 * 20;
} else {
uncommon_trap(class_check);
}
give up!
public class ClassDevirtualization {
public static void main(String[] args) throws InterruptedException {
System.out.println("Using Square...");
Func func = new Square();
for ( int i = 0; i < 20_000; ++i ) {
apply1(func, i);
apply2(func, i);
}
Thread.sleep(5_000);
System.out.printf("Loading %s to Deopt Now!%n”, Sqrt.class);
System.out.println("Keep using Square in apply1...");
func = new Square();
for ( int i = 0; i < 20_000; ++i ) apply1(func, i);
Thread.sleep(5_000);
System.out.println("Use AlsoSquare in apply1...");
func = new AlsoSquare();
for ( int i = 0; i < 20_000; ++i ) apply1(func, i);
Thread.sleep(5_000);
System.out.println("Use AnotherSquare in apply1...");
func = new AnotherSquare();
for ( int i = 0; i < 20_000; ++i ) apply1(func, i);
Thread.sleep(5_000);
…after 3 types no more deopts…
}
}
stop the world,
deopt now!
class check
deopt!
bimorphic
deopt!
example10c.ClassDevirtualization
Class Devirtualization
89 1 java.lang.String::hashCode (55 bytes)
Using Square...
104 2 example10.support.Square::apply (4 bytes)
105 3 example10c.ClassDevirtualization::apply1 (7 bytes)
105 4 example10c.ClassDevirtualization::apply2 (7 bytes)
106 5 % example10c.ClassDevirtualization::main @ 21 (240 bytes)
5116 5 % example10c.ClassDevirtualization::main @ -2 (240 bytes) made not entrant
5116 4 example10c.ClassDevirtualization::apply2 (7 bytes) made not entrant
5116 3 example10c.ClassDevirtualization::apply1 (7 bytes) made not entrant
Loading class example10.support.Sqrt to Deoptimize Now!
Keep using Square in apply1...
5128 6 example10c.ClassDevirtualization::apply1 (7 bytes)
5128 7 % example10c.ClassDevirtualization::main @ 88 (240 bytes)
Use AlsoSquare in apply1...
10131 6 example10c.ClassDevirtualization::apply1 (7 bytes) made not entrant
10131 8 example10c.ClassDevirtualization::apply1 (7 bytes)
10132 9 % example10c.ClassDevirtualization::main @ 131 (240 bytes)
Use AnotherSquare in apply1...
15134 8 example10c.ClassDevirtualization::apply1 (7 bytes) made not entrant
15134 10 example10c.ClassDevirtualization::apply1 (7 bytes)
15135 11 % example10c.ClassDevirtualization::main @ 174 (240 bytes)
Use YetAnotherSquare in apply1...
20139 12 % example10c.ClassDevirtualization::main @ 217 (240 bytes)
Class Devirtualization
-XX:+PrintCompilation
WorseYet...
class AlsoSquare extends Square {}
class AnotherSquare extends Square {}
class Square extends Func {
double final apply(double x) {
return x * x;
}
}
classYetAnotherSquare extends Square {}
Unintentional
Megamorphism
new HashMap<String, Integer>() {{
put(“foo”, 20);
put(“bar”, 30);
}};
Func
+apply(double):double
Square
+apply(double):double
Sqrt
+apply(double):double
IFunc
+apply(double):double
No CHA for Interfaces
example10d.InterfaceDevirtualization
68 1 java.lang.String::hashCode (55 bytes)
Using Square...
79 2 example10.support.Square::apply (4 bytes)
80 3 example10d.InterfaceDevirtualization::apply1 (9 bytes)
80 4 example10d.InterfaceDevirtualization::apply2 (9 bytes)
81 5 % example10d.InterfaceDevirtualization::main @ 21 (240 bytes)
Loading class example10.support.Sqrt - no CHA for interfaces!
Keep using Square in apply1...
5090 6 % example10d.InterfaceDevirtualization::main @ 88 (240 bytes)
Use AlsoSquare in apply1...
10094 5 % example10d.InterfaceDevirtualization::main @ -2 (240 bytes) made not entrant
10094 6 % example10d.InterfaceDevirtualization::main @ -2 (240 bytes) made not entrant
10094 3 example10d.InterfaceDevirtualization::apply1 (9 bytes) made not entrant
10095 7 example10d.InterfaceDevirtualization::apply1 (9 bytes)
10095 8 % example10d.InterfaceDevirtualization::main @ 131 (240 bytes)
Use AnotherSquare in apply1...
15101 7 example10d.InterfaceDevirtualization::apply1 (9 bytes) made not entrant
15102 9 example10d.InterfaceDevirtualization::apply1 (9 bytes)
15102 10 % example10d.InterfaceDevirtualization::main @ 174 (240 bytes
No CHA for Interfaces
-XX:+PrintCompilation
MaxTrivialSize 6
MaxInlineSize 35
FreqInlineSize 325
MaxInlineLevel 9
MaxRecursiveInlineLevel 1
MinInliningThreshold 250
Tier1MaxInlineSize 8
Tier1FreqInlineSize 35
Inlining Numbers to Remember...
`java -XX:+PrintFlagsFinal`
“Fun” with Unloaded
public class UnloadedForever {
public static void main(String[] args) {
for ( int i = 0; i < 100_000; ++i ) {
try {
factory();
} catch ( Throwable t ) {
// ignore
}
}
}
static DoesNotExist factory() {
return new DoesNotExist();
}
}
example11a.UnloadedForever
-XX:+PrintCompilation
Unloaded Forever
72 1 java.lang.String::hashCode (55 bytes)
156 2 java.lang.Object::<init> (1 bytes)
183 3 s java.lang.Throwable::fillInStackTrace (29 bytes)
183 4 n java.lang.Throwable::fillInStackTrace (native)
183 5 java.lang.LinkageError::<init> (6 bytes)
184 6 java.lang.Error::<init> (6 bytes)
184 7 java.lang.Throwable::<init> (34 bytes)
185 8 example11a.UnloadedForever::factory (8 bytes)
186 9 java.lang.NoClassDefFoundError::<init> (6 bytes)
186 8 example11a.UnloadedForever::factory (8 bytes) made not entrant
223 10 % ! example11a.UnloadedForever::main @ 5 (23 bytes)
273 11 example11a.UnloadedForever::factory (8 bytes)
274 11 example11a.UnloadedForever::factory (8 bytes) made not entrant
358 12 example11a.UnloadedForever::factory (8 bytes)
358 12 example11a.UnloadedForever::factory (8 bytes) made not entrant
441 13 example11a.UnloadedForever::factory (8 bytes)
442 13 example11a.UnloadedForever::factory (8 bytes) made not entrant
528 14 example11a.UnloadedForever::factory (8 bytes)
978 8 example11a.UnloadedForever::factory (8 bytes) made zombie
“Fun” with Uninitialized
public class UninitializedForever {
static class Uninitialized {
static {
if ( true ) throw new RuntimeException();
}
}
public static void main(String[] args) {
for ( int i = 0; i < 100_000; ++i ) {
try {
new Uninitialized();
} catch ( Throwable t ) {
// ignore
}
}
}
}
example11b.UnintializedForever
-XX:+PrintCompilation
74 1 java.lang.String::hashCode (55 bytes)
162 2 java.lang.Object::<init> (1 bytes)
188 3 s java.lang.Throwable::fillInStackTrace (29 bytes)
189 4 n java.lang.Throwable::fillInStackTrace (native)
189 5 java.lang.LinkageError::<init> (6 bytes)
190 6 java.lang.Error::<init> (6 bytes)
190 7 java.lang.Throwable::<init> (34 bytes)
191 8 java.lang.NoClassDefFoundError::<init> (6 bytes)
233 9 % ! example11b.UninitializedForever::main @ 5 (25 bytes)
241 9 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant
252 10 % ! example11b.UninitializedForever::main @ 5 (25 bytes)
263 10 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant
272 11 % ! example11b.UninitializedForever::main @ 5 (25 bytes)
281 11 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant
290 12 % ! example11b.UninitializedForever::main @ 5 (25 bytes)
299 12 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant
308 13 % ! example11b.UninitializedForever::main @ 5 (25 bytes)
318 13 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant
328 14 % ! example11b.UninitializedForever::main @ 5 (25 bytes)
337 14 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant
Uninitialized Forever
null_check unexpected null or zero divisor
null_assert unexpected non-null or non-zero
range_check unexpected array index
class_check unexpected object class
array_check unexpected array class
intrinsic unexpected operand to intrinsic
bimorphic unexpected object class in bimorphic inlining
unloaded unloaded class or constant pool entry
uninitialized bad class state (uninitialized)
unreached code is not reached, compiler
unhandled arbitrary compiler limitation
constraint arbitrary runtime constraint violated
div0_check a null_check due to division by zero
age nmethod too old; tier threshold reached
predicate compiler generated predicate failed
loop_limit_check compiler generated loop limits check failed
Reasons for Deoptimizing...
none just interpret, do not invalidate nmethod
maybe_recompile recompile the nmethod; need not invalidate
make_not_entrant invalidate the nmethod, recompile (probably)
reinterpret invalidate the nmethod, reset IC, maybe recompile
make_not_compilable invalidate the nmethod and do not compile
Actions When
Deoptimizing...
Bail to Interpreter
Keep Compile?
Yes, recompile? No, recompile when...
Now Later (Profile More)No Yes
none
maybe
recompile
make
not entrant
reinterpret
class load!
stop the world,
deopt now!
uncommon trap!
null_check make_not_entrant
null_assert make_not_entrant
range_check make_not_entrant
class_check maybe_recompile
array_check maybe_recompile
intrinsic maybe_recompile, make_not_entrant
bimorphic maybe_recompile
unloaded reinterpret
uninitialized reinterpret
unreached reinterpret
unhandled none
constraint CHA - deopt now!
div0_check make_not_entrant
age maybe_recompile
predicate none?
loop_limit_check none?
Reasons & Actions
Does This Matter?
thread1
thread2
inlinedMethod2
inlinedMethod1
hotMethod
code cache
interpreter frame
compiled frame
inlinedMethod2
inlinedMethod1
hotMethod
...
...
run
inlinedMethod2
inlinedMethod1
hotMethod
...
...
run
ReadyNow!
http://www.azulsystems.com/solutions/zing/readynow
Recommending Reading
http://www.javaspecialists.eu
Brian Goetz
http://www.ibm.com/developerworks/views/java/libraryview.jsp?
contentarea_by=Java+technology&search_by=brian+goetz
Dr Heinz M Kabutz
https://wiki.openjdk.java.net/display/HotSpot
VM Developer Blogs
Aleksey Shipilëv
http://shipilev.net/
http://psy-lob-saw.blogspot.com/
Nitsan Wakart
https://twitter.com/maddocig
IgorVeresov
JITWatch
https://github.com/AdoptOpenJDK/jitwatch/
?
Questions?
Douglas Q. Hawkins
VM Engineer

Más contenido relacionado

La actualidad más candente

Jersey framework
Jersey frameworkJersey framework
Jersey framework
knight1128
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
knight1128
 
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerabilityCsw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
CanSecWest
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011
julien.ponge
 

La actualidad más candente (20)

Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
Multithreading done right
Multithreading done rightMultithreading done right
Multithreading done right
 
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
 
Jersey framework
Jersey frameworkJersey framework
Jersey framework
 
分散式系統
分散式系統分散式系統
分散式系統
 
Node.js System: The Landing
Node.js System: The LandingNode.js System: The Landing
Node.js System: The Landing
 
Non-blocking synchronization — what is it and why we (don't?) need it
Non-blocking synchronization — what is it and why we (don't?) need itNon-blocking synchronization — what is it and why we (don't?) need it
Non-blocking synchronization — what is it and why we (don't?) need it
 
Kotlin coroutine - the next step for RxJava developer?
Kotlin coroutine - the next step for RxJava developer?Kotlin coroutine - the next step for RxJava developer?
Kotlin coroutine - the next step for RxJava developer?
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
 
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Bridge TensorFlow to run on Intel nGraph backends (v0.4)Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
 
Работа с реляционными базами данных в C++
Работа с реляционными базами данных в C++Работа с реляционными базами данных в C++
Работа с реляционными базами данных в C++
 
bluespec talk
bluespec talkbluespec talk
bluespec talk
 
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerabilityCsw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
 
Java 7 JUG Summer Camp
Java 7 JUG Summer CampJava 7 JUG Summer Camp
Java 7 JUG Summer Camp
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
 
Current State of Coroutines
Current State of CoroutinesCurrent State of Coroutines
Current State of Coroutines
 
Reactive server with netty
Reactive server with nettyReactive server with netty
Reactive server with netty
 

Similar a Silicon Valley JUG: JVM Mechanics

Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
Chris Ramsdale
 
服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript
Qiangning Hong
 

Similar a Silicon Valley JUG: JVM Mechanics (20)

JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
What the CRaC - Superfast JVM startup
What the CRaC - Superfast JVM startupWhat the CRaC - Superfast JVM startup
What the CRaC - Superfast JVM startup
 
nullcon 2010 - Intelligent debugging and in memory fuzzing
nullcon 2010 - Intelligent debugging and in memory fuzzingnullcon 2010 - Intelligent debugging and in memory fuzzing
nullcon 2010 - Intelligent debugging and in memory fuzzing
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Apache Thrift
Apache ThriftApache Thrift
Apache Thrift
 
Playing With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.jsPlaying With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.js
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
 
Node.js - As a networking tool
Node.js - As a networking toolNode.js - As a networking tool
Node.js - As a networking tool
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Java
JavaJava
Java
 
Spring Boot Revisited with KoFu and JaFu
Spring Boot Revisited with KoFu and JaFuSpring Boot Revisited with KoFu and JaFu
Spring Boot Revisited with KoFu and JaFu
 

Más de Azul Systems, Inc.

Más de Azul Systems, Inc. (10)

Unleash the Power of Apache Cassandra
Unleash the Power of Apache CassandraUnleash the Power of Apache Cassandra
Unleash the Power of Apache Cassandra
 
How NOT to Measure Latency
How NOT to Measure LatencyHow NOT to Measure Latency
How NOT to Measure Latency
 
Enabling Java in Latency Sensitive Environments - Dallas JUG April 2015
Enabling Java in Latency Sensitive Environments - Dallas JUG April 2015Enabling Java in Latency Sensitive Environments - Dallas JUG April 2015
Enabling Java in Latency Sensitive Environments - Dallas JUG April 2015
 
Enabling Java in Latency-Sensitive Environments - Austin JUG April 2015
Enabling Java in Latency-Sensitive Environments - Austin JUG April 2015Enabling Java in Latency-Sensitive Environments - Austin JUG April 2015
Enabling Java in Latency-Sensitive Environments - Austin JUG April 2015
 
QCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVM
QCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVMQCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVM
QCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVM
 
JVM Language Summit: Object layout presentation
JVM Language Summit: Object layout presentationJVM Language Summit: Object layout presentation
JVM Language Summit: Object layout presentation
 
JVM Language Summit: Object layout workshop
JVM Language Summit: Object layout workshopJVM Language Summit: Object layout workshop
JVM Language Summit: Object layout workshop
 
What's New in the JVM in Java 8?
What's New in the JVM in Java 8?What's New in the JVM in Java 8?
What's New in the JVM in Java 8?
 
DC JUG: Understanding Java Garbage Collection
DC JUG: Understanding Java Garbage CollectionDC JUG: Understanding Java Garbage Collection
DC JUG: Understanding Java Garbage Collection
 
Winning With Java at Market Open
Winning With Java at Market OpenWinning With Java at Market Open
Winning With Java at Market Open
 

Último

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Silicon Valley JUG: JVM Mechanics

  • 1. Douglas Q. Hawkins VM Engineer VM MechanicsWhen Does the JVM JIT & Deoptimize? J https://github.com/dougqh/jvm-mechanics
  • 2. About Azul Systems Zulu® Multi-Platform OpenJDK Cloud Support including Docker and Azure Embedded Support Zing® Highly ScalableVM Continuously Concurrent Compacting Collector ReadyNow! for Low Latency Applications http://www.azulsystems.com/
  • 3. HotSpot Lifecycle 1 2 34 Interpret Profile Just-in-Time CompilationDeoptimize
  • 5. public class SimpleProgram { static final int CHUNK_SIZE = 1_000; public static void main(String[] args) { for ( int i = 0; i < 250; ++i ) { long startTime = System.nanoTime(); for ( int j = 0; j < CHUNK_SIZE; ++j ) { new Object(); } long endTime = System.nanoTime(); System.out.printf("%dt%d%n", i, endTime - startTime); } } } A Simple Program example01a.SimpleProgram Code Reference
  • 8. public class NotSoSimpleProgram { static final int CHUNK_SIZE = 1_000; public static void main(String[] args) { Object trap = null; for ( int i = 0; i < 250; ++i ) { long startTime = System.nanoTime(); for ( int j = 0; j < CHUNK_SIZE; ++j ) { new Object(); if ( trap != null ) { System.out.println("trap!"); trap = null; } } if ( i == 200 ) trap = new Object(); long endTime = System.nanoTime(); System.out.printf("%dt%d%n", i, endTime - startTime); } } } Not So Simple Program example01b.NotSoSimpleProgram
  • 10. Dynamically Generated Threaded Interpreter Identify “Hot Spots” Slow! 10000x Interpreter http://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html
  • 11. Invocation Counter example02.InvocationCounter public class InvocationCounter { public static void main(final String[] args) throws InterruptedException { for ( int i = 0; i < 20_000; ++i ) { hotMethod(); } System.out.println("Waiting for compiler..."); Thread.sleep(5_000); } static void hotMethod() {} }
  • 12. -XX:+PrintCompilation example02.InvocationCounter 299 1 % java.lang.String::indexOf @ 37 (70 bytes) 332 2 java.lang.String::indexOf (70 bytes) 364 3 example02.InvocationCounter::hotMethod (1 bytes) 365 4 % example02.InvocationCounter::main @ 5 (33 bytes) Waiting for compiler... Invocation Counter
  • 13. Compilation Log 5328 50 n java.io.FileOutputStream::writeBytes (native) 5330 27 java.nio.CharBuffer::wrap (20 bytes) 5333 31 s java.io.BufferedOutputStream::flush (12 bytes) 5477 56 % CompilationExample::main @ 37 (82 bytes) timestamp (since VM start) compilation ID method name method size native exception handler synchronized method on-stack replacement loop bytecode index !
  • 14. synchronized is try/finally synchronized ( foo.getBar() ) { ... } tmp = foo.getBar(); monitor_enter(tmp); try { ... } finally { monitor_exit(tmp); }
  • 15. Duplicate Methods & Overloading 5208 4 java.nio.Buffer::position (5 bytes) ... 5223 8 java.nio.Buffer::position (43 bytes)
  • 16. Invisible Overloads via Bridge Methods public interface Supplier<T> { public abstract T get(); } new Supplier<String>() { public final String get() { return “foo”; } }; 145 4 InvisibleOverload$1::get (5 bytes) 145 5 InvisibleOverload$1::get (3 bytes)
  • 17. public class BackedgeCounter { public static void main(final String[] args) throws InterruptedException { for ( int i = 0; i < 20_000; ++i ) { hotMethod(); } System.out.println("Waiting for compiler..."); Thread.sleep(5_000); for ( int i = 0; i < 20_000; ++i ) { hotMethod(); } System.out.println("Waiting for compiler..."); Thread.sleep(5_000); } static void hotMethod() {} } Backedge Counter example03.BackedgeCounter
  • 18. 163 1 java.lang.String::charAt (29 bytes) 166 2 java.lang.String::hashCode (55 bytes) 171 3 java.lang.String::indexOf (70 bytes) 193 4 example03.BackedgeCounter::hotMethod (1 bytes) 194 5 % example03.BackedgeCounter::main @ 5 (65 bytes) Waiting for compiler... 5196 6 % example03.BackedgeCounter::main @ 37 (65 bytes) Waiting for compiler... -XX:+PrintCompilation example03.BackedgeCounter Backedge Counter
  • 19. On-Stack Replacement eventLoop ... ... main eventLoop @ 20 ... ... main interpreter frame compiled frame
  • 20. Both Counters public class BothCounters { public static void main(final String[] args) throws InterruptedException { for ( int i = 0; i < 2; ++i ) { outerMethod(); } System.out.println("Waiting for compiler..."); Thread.sleep(5000); } static void outerMethod() { for ( int i = 0; i < 10_000; ++i ) { innerMethod(); } } static void innerMethod() {} } example04.BothCounters
  • 21. -XX:+PrintCompilation example04.BothCounters Both Counters 115 1 % java.lang.String::indexOf @ 37 (70 bytes) 123 2 example04.BothCounters::innerMethod (1 bytes) 124 3 example04.BothCounters::outerMethod (19 bytes) 125 4 % example04.BothCounters::outerMethod @ 5 (19 bytes) Waiting for compiler...
  • 22. HotSpot: A Tale of Two Compilers C1client C2 server
  • 23. C1 ClientVM The Fast Acting Compiler Compiles with Count > 1,000 (2,000 in Tiered) Produces Compilations Quickly Generated Code Runs Relatively Slowly
  • 24. Compiles with Count > 10,000 (15,000 in Tiered) Profile Guided C2 ServerVM The Smart compiler Speculative 2xSpeed! Produces Compilations Slowly Generated Code Runs Fast
  • 25. Tiered Compilation Available in Java 7 - default in Java 8 Best of Both Worlds - C1 & C2 Interpreter > 2,000 > 15,000 C1 C2 http://www.slideshare.net/maddocig/tiered
  • 26. 0 0 0 interpreter 3 4 2 3 4 1 3 c1 c2 none basic counters detailed common c2 busy trivial method Tiered Compilation
  • 27. public final class TieredCompilation { public static final void main(final String[] args) throws InterruptedException { for ( int i = 0; i < 3_000; ++i ) { method(); } System.out.println("Waiting for the compiler..."); Thread.sleep(5_000); for ( int i = 0; i < 20_000; ++i ) { method(); } System.out.println("Waiting for the compiler..."); Thread.sleep(5_000); } private static final void method() { // Do something while doing nothing. System.out.print('0'); } } example05.TieredCompilation Tiered Compilation
  • 28. 183 69 3 sun...SingleByte$Encoder::encodeArrayLoop (236 bytes) ... 5237 101 4 sun...SingleByte$Encoder::encodeArrayLoop (236 bytes) 5255 69 3 sun...SingleByte$Encoder::encodeArrayLoop (236 bytes) made not entrant 131 3 3 java.lang.Object::<init> (1 bytes) ... 140 15 1 java.lang.Object::<init> (1 bytes) 141 3 3 java.lang.Object::<init> (1 bytes) made not entrant 126 6 n 0 java.lang.System::arraycopy (native) (static) -XX:+TieredCompilation -XX:+PrintCompilation example05.TieredCompilation Tiered Compilation tier
  • 31. Intrinsics Special code built-into theVM for a particular method Built-in to theVM - not written in Java (usually) For Example... System.arraycopy Math.sin / cos / tan Often use special hardware capabilities: MMX,AVX2, etc
  • 32. http://en.wikipedia.org/wiki/Common_subexpression_elimination Common Sub-Expression Elimination int a = b * c + g; int d = b * c * e; int tmp = b * c; int a = tmp + g; int d = tmp * e;
  • 33. boolean isDebugEnabled = LOGGER.isDebugEnabled(); for ( User user: users ) { ...do something... if ( isDebugEnabled ) { LOGGER.debug(user.getName()); } } boolean isDebugEnabled = LOGGER.isDebugEnabled(); if ( isDebugEnabled ) { for ( User user: users ) { ...do something... LOGGER.debug(user.getName()); } } else { for ( User user: users ) { ...do something... } } Loop Unswitching http://en.wikipedia.org/wiki/Loop_unswitching
  • 34. Lock Coarsening StringBuffer buffer = ... buffer.append(“Hello”); buffer.append(name); buffer.append(“n”); StringBuffer buffer = ... lock(buffer); buffer.append(“Hello”); unlock(buffer); lock(buffer); buffer.append(name); unlock(buffer); lock(buffer); buffer.append(“n”); unlock(buffer); StringBuffer buffer = ... lock(buffer); buffer.append(“Hello”); buffer.append(name); buffer.append(“n”); unlock(buffer); http://www.ibm.com/developerworks/library/j-jtp10185/index.html
  • 35. Dead Code Elimination http://en.wikipedia.org/wiki/Dead_code_elimination public int ArrayList::indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
  • 36. Inlining “The mother of all optimizations.” IPO: Inter-procedural Optimization
  • 37. Intrinsic Inlining public class Intrinsics { public static void main(String[] args) throws InterruptedException { int[] data = randomInts(100_000); int min = Integer.MAX_VALUE; for ( int x: data ) { min = Math.min(min, x); } Thread.sleep(5_000); System.out.println(min); } static final int[] randomInts(int size) { … } } example06.Intrinsics
  • 38. -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining 376 8 % example06.Intrinsics::randomInts @ 13 (30 bytes) @ 16 java.util.concurrent.ThreadLocalRandom::nextInt (8 bytes) inline (hot) @ 1 java.util.concurrent.ThreadLocalRandom::nextSeed (32 bytes) inline (hot) @ 3 java.lang.Thread::currentThread (0 bytes) (intrinsic) @ 18 sun.misc.Unsafe::getLong (0 bytes) (intrinsic) @ 27 sun.misc.Unsafe::putLong (0 bytes) (intrinsic) @ 4 java.util.concurrent.ThreadLocalRandom::mix32 (26 bytes) inline (hot) 379 9 java.lang.Math::min (11 bytes) 379 10 % example06.Intrinsics::main @ 22 (58 bytes) @ 30 java.lang.Math::min (11 bytes) (intrinsic) Intrinsic Inlining
  • 39. Direct Call Inlining public class Inlining { public static void main(String[] args) throws InterruptedException { System.setOut(new NullPrintStream()); for ( int i = 0; i < 20_000; ++i ) { hotMethod(); } Thread.sleep(5_000); } public static void hotMethod() { System.out.println(square(7)); System.out.println(square(9)); } static int square(int x) { return x * x; } } public static void hotMethod() { System.out.println(7 * 7); System.out.println(9 * 9); } example07.DirectInlining static, private, constructor calls
  • 40. Direct Call Inlining 226 53 example07.DirectInlining::hotMethod (23 bytes) @ 5 example07.DirectInlining::square (4 bytes) inline (hot) !m @ 8 java.io.PrintStream::println (24 bytes) already compiled into a big method @ 16 example07.DirectInlining::square (4 bytes) inline (hot) !m @ 19 java.io.PrintStream::println (24 bytes) already compiled into a big method 228 54 % example07.DirectInlining::main @ 15 (35 bytes) @ 15 example07.DirectInlining::hotMethod (23 bytes) inline (hot) @ 5 example07.DirectInlining::square (4 bytes) inline (hot) !m @ 8 java.io.PrintStream::println (24 bytes) already compiled into a big method @ 16 example07.DirectInlining::square (4 bytes) inline (hot) !m @ 19 java.io.PrintStream::println (24 bytes) already compiled into a big method -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining
  • 41. Printing Assembly for a Method Download hsdis-amd64 dynamic library Copy to jre/lib directory Run HotSpot with... -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,{package/Class::method}
  • 42. 0x0000000106c57843: mov $0x31,%edx 0x0000000106c57848: data32 xchg %ax,%ax 0x0000000106c5784b: callq 0x0000000106c10b60 ; OopMap{rbp=Oop off=48} ;*invokevirtual println ; - DirectInlining::hotMethod@7 (line 17) ; {optimized virtual_call} 0x0000000106c5785d: mov $0x51,%edx 0x0000000106c57862: nop 0x0000000106c57863: callq 0x0000000106c10b60 ; OopMap{off=72} ;*invokevirtual println ; - DirectInlining::hotMethod@17 (line 18) ; {optimized virtual_call} -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,example07/DirectInlining::hotMethod Direct Call Inlining
  • 43. Escape Analysis long sum = 0; for ( Long x: list ) { sum += iter.next(); } long sum = 0; for ( Iterator<Long> iter = list.iterator(); iter.hasNext(); ) { sum += iter.next(); }
  • 44. Escape Analysis http://psy-lob-saw.blogspot.ru/2014/12/the-escape-of-arraylistiterator.html long sum = 0; // ArrayList$Itr.<init> int iter$size = list.size(); int iter$cursor = 0; int iter$lastRet = -1; 
 // ArrayList$Itr.hasNext for ( ; iter$cursor != iter$size; ) { // ArrayList$Itr.next int i = iter$cursor; Object[] elementData = list.elementData; iter$cursor = i + 1; Long x = (Long)elementData[iter$lastRet = i]; sum += x; }
  • 45. public class SimpleProgram { static final int CHUNK_SIZE = 1_000; public static void main(String[] args) { for ( int i = 0; i < 250; ++i ) { long startTime = System.nanoTime(); for ( int j = 0; j < CHUNK_SIZE; ++j ) { //new Object
 obj = calloc(sizeof(Object)); obj.<init>(); // call ctor - empty } long endTime = System.nanoTime(); System.out.printf( "%dt%d%n", i, endTime - startTime); } } } Simple Program Revisited 1: inlined 2: escape analysis / dead store 3: empty loop / eliminate loop example01a.SimpleProgram
  • 48. Implicit Null Check public class NullCheck { public static void main(String[] args) throws InterruptedException { for ( int i = 0; i < 20_000; ++i ) { hotMethod("hello"); } Thread.sleep(5_000); for ( int i = 0; i < 10; ++i ) { System.out.printf("tempting fate %d%n", i); try { hotMethod(null); } catch ( NullPointerException e ) { // ignore } } } static final void hotMethod(final Object value) { value.hashCode(); } } example08a.NullCheckhttps://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/signals.html
  • 49. -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,example08a/NullCheck::hotMethod 0x000000010795f9c0: mov %eax,-0x14000(%rsp) 0x000000010795f9c7: push %rbp 0x000000010795f9c8: sub $0x50,%rsp ;*synchronization entry example08a.NullCheck::hotMethod@-1 (line 26) 0x000000010795f9cc: mov 0x8(%rsi),%r10d ; implicit exception: dispatches to 0x000000010795fe1d 0x000000010795f9d0: movabs $0x7eaa80d10,%r11 ; {oop(a 'java/lang/Class' = 'java/lang/System')} 0x000000010795f9da: mov 0x74(%r11),%r11d ;*getstatic out Implicit Null Check
  • 50. value.toString(); Possible, but improbable NPE if ( value == null ) { throw new NullPointerException(); } SEGVderef value signal handler throw NPE Implicit Null Check
  • 51. Null Check Deoptimization -XX:+PrintCompilation 124 1 java.lang.String::hashCode (55 bytes) 138 2 example08a.NullCheck::hotMethod (6 bytes) 138 3 % ! example08a.NullCheck::main @ 5 (69 bytes) tempting fate 0 tempting fate 1 tempting fate 2 5147 2 example08a.NullCheck::hotMethod (6 bytes) made not entrant tempting fate 3 tempting fate 4 tempting fate 5 tempting fate 6 tempting fate 7 tempting fate 8 tempting fate 9
  • 52. Not Thrown Away the First Time Bail to Interpreter Keep Compile? Yes No, recompile when... Now Later (Profile More) If Less Than 3 Deopts?
  • 53. Null Proportion: 0.100000 Caught: 10057 Unique: 2015 Null Proportion: 0.500000 Caught: 50096 Unique: 7191 Null Proportion: 0.900000 Caught: 89929 Unique: 11030 int caughtCount = 0; Set<NullPointerException> nullPointerExceptions = new HashSet<>(); for ( Object object : objects ) { try { object.toString(); } catch ( NullPointerException e ) { nullPointerExceptions.add( e ); caughtCount += 1; } } Hot Exception Optimization example08b.HotExceptionDemohttp://www.javaspecialists.eu/archive/Issue187.html
  • 54. Hot Exceptions int caughtCount = 0; HashSet<NullPointerException> nullPointerExceptions = new HashSet<>(); for ( Object object : objects ) { try { object.toString(); } catch ( NullPointerException e ) { boolean added = nullPointerExceptions.add(e); if ( !added ) e.printStackTrace(); caughtCount += 1; } } java.lang.NullPointerException No StackTrace???
  • 55. public class Unreached { public static volatile Object thing = null; public static void main(final String[] args) throws InterruptedException { for ( int i = 0; i < 20_000; ++i ) { hotMethod(); } Thread.sleep(5_000); thing = new Object(); for ( int i = 0; i < 20_000; ++i ) { hotMethod(); } Thread.sleep(5_000); } static final void hotMethod() { if ( thing == null ) System.out.print(""); else System.out.print(""); } } Unreached Deoptimization example09.Unreached phase change static final void hotMethod() { if ( thing == null ) System.out.print(""); else uncommon_trap(unreached); }
  • 56. -XX:+PrintCompilation Unreached Deoptimization 217 1 java.lang.String::hashCode (55 bytes) 235 2 java.lang.String::indexOf (70 bytes) 238 3 java.io.BufferedWriter::ensureOpen (18 bytes) 244 4 java.lang.String::length (6 bytes) 244 5 java.lang.String::indexOf (7 bytes) 245 6 java.nio.Buffer::position (5 bytes) 245 7 example09.Unreached::hotMethod (26 bytes) … 265 14 java.io.OutputStreamWriter::flushBuffer (8 bytes) 265 15 ! sun.nio.cs.StreamEncoder::flushBuffer (42 bytes) 267 16 sun.nio.cs.StreamEncoder::isOpen (5 bytes) 267 17 sun.nio.cs.StreamEncoder::implFlushBuffer (15 bytes) 267 18 % example09.Unreached::main @ 5 (59 bytes) 5255 7 example09.Unreached::hotMethod (26 bytes) made not entrant 5257 19 example09.Unreached::hotMethod (26 bytes) 5257 20 % example09.Unreached::main @ 39 (59 bytes)
  • 58. Why Speculate? y *= 2; x = 0; y = 25; y = -20; x = 0; y = 25; y = 30; trap!y = 15;
  • 59. Not So Simple Program Revisited 1 1000 1000000 0 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228 240 unreached deopt, bail to interpreter!
  • 60. Virtual Call Inlining Func +apply(double):double Square +apply(double):double Sqrt +apply(double):double Class Hierarchy Analysis (CHA) Type Profile http://shipilev.net/blog/2015/black-magic-method-dispatch/
  • 61. public class Monomorphic { public static void main(String[] args) throws InterruptedException { Func func = new Square(); for ( int i = 0; i < 20_000; ++i ) { apply(func, i); } Thread.sleep(5_000); } static double apply(Func func, int x) { return func.apply(x); } } Monomorphic example10a.Monomorphic
  • 62. 217 1 java.lang.String::hashCode (55 bytes) 234 3 example10.support.Square::apply (4 bytes) 234 2 example10a.Monomorphic::apply (7 bytes) @ 3 example10.support.Square::apply (4 bytes) inline (hot) 234 4 % example10a.Monomorphic::main @ 13 (30 bytes) @ 15 example10a.Monomorphic::apply (7 bytes) inline (hot) @ 3 example10.support.Square::apply (4 bytes) inline (hot) Monomorphic -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining
  • 63. Beyond Monomorphic Func func = … double result = func.apply(20); Func func = … //no type guard! double result = 20 * 20; More Types?
  • 64. example10b.ChaStorm public class ChaStorm { public static void main(String[] args) throws InterruptedException { Func func = new Square(); for ( int i = 0; i < 10_000; ++i ) { apply1(func, i); … apply8(func, i); } System.out.println("Waiting for compiler..."); Thread.sleep(5_000); System.out.println("Deoptimize..."); System.out.println(Sqrt.class); Thread.sleep(5_000); } } Potential for Deopt Storm
  • 65. Potential for Deopt Storm 152 1 java.lang.String::hashCode (55 bytes) 166 2 example10.support.Square::apply (4 bytes) 173 3 example10b.ChaStorm::apply1 (7 bytes) 173 4 example10b.ChaStorm::apply2 (7 bytes) Waiting for compiler... 174 5 example10b.ChaStorm::apply3 (7 bytes) 174 6 example10b.ChaStorm::apply4 (7 bytes) 174 7 example10b.ChaStorm::apply5 (7 bytes) 174 8 example10b.ChaStorm::apply6 (7 bytes) 174 9 example10b.ChaStorm::apply7 (7 bytes) 174 10 example10b.ChaStorm::apply8 (7 bytes) Deoptimize... 5176 9 example10b.ChaStorm::apply7 (7 bytes) made not entrant 5176 8 example10b.ChaStorm::apply6 (7 bytes) made not entrant 5176 7 example10b.ChaStorm::apply5 (7 bytes) made not entrant 5176 5 example10b.ChaStorm::apply3 (7 bytes) made not entrant 5176 6 example10b.ChaStorm::apply4 (7 bytes) made not entrant 5176 4 example10b.ChaStorm::apply2 (7 bytes) made not entrant 5176 3 example10b.ChaStorm::apply1 (7 bytes) made not entrant 5176 10 example10b.ChaStorm::apply8 (7 bytes) made not entrant class example10.support.Sqrt -XX:+PrintCompilation
  • 66. Another Way to Deopt Bail to Interpreter Keep Compile? Yes No, recompile when... Now Later (Profile More) class load! stop the world, deopt now!
  • 67. Total time for which application threads were stopped: 0.0001010 seconds 5096 10 example10b.ChaStorm::apply8 (7 bytes) made not entrant 5096 8 example10b.ChaStorm::apply6 (7 bytes) made not entrant 5096 7 example10b.ChaStorm::apply5 (7 bytes) made not entrant 5096 5 example10b.ChaStorm::apply3 (7 bytes) made not entrant 5096 6 example10b.ChaStorm::apply4 (7 bytes) made not entrant 5096 4 example10b.ChaStorm::apply2 (7 bytes) made not entrant 5096 9 example10b.ChaStorm::apply7 (7 bytes) made not entrant 5096 3 example10b.ChaStorm::apply1 (7 bytes) made not entrant vmop [threads: total initially_running wait_to_block] … 5.096: Deoptimize [ 7 0 0 ] … Another Way to Deopt -XX:+PrintCompilation -XX+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1 http://blog.ragozin.info/2012/10/safepoints-in-hotspot-jvm.html
  • 68. Bimorphic Func func = … double result = func.apply(20); Func func = … //no type guard! double result = 20 * 20; add another type Func func = … double result; if ( func.getClass().equals(Square.class) ) { result = 20 * 20; } else { uncommon_trap(class_check); } give up!
  • 69. public class ClassDevirtualization { public static void main(String[] args) throws InterruptedException { System.out.println("Using Square..."); Func func = new Square(); for ( int i = 0; i < 20_000; ++i ) { apply1(func, i); apply2(func, i); } Thread.sleep(5_000); System.out.printf("Loading %s to Deopt Now!%n”, Sqrt.class); System.out.println("Keep using Square in apply1..."); func = new Square(); for ( int i = 0; i < 20_000; ++i ) apply1(func, i); Thread.sleep(5_000); System.out.println("Use AlsoSquare in apply1..."); func = new AlsoSquare(); for ( int i = 0; i < 20_000; ++i ) apply1(func, i); Thread.sleep(5_000); System.out.println("Use AnotherSquare in apply1..."); func = new AnotherSquare(); for ( int i = 0; i < 20_000; ++i ) apply1(func, i); Thread.sleep(5_000); …after 3 types no more deopts… } } stop the world, deopt now! class check deopt! bimorphic deopt! example10c.ClassDevirtualization Class Devirtualization
  • 70. 89 1 java.lang.String::hashCode (55 bytes) Using Square... 104 2 example10.support.Square::apply (4 bytes) 105 3 example10c.ClassDevirtualization::apply1 (7 bytes) 105 4 example10c.ClassDevirtualization::apply2 (7 bytes) 106 5 % example10c.ClassDevirtualization::main @ 21 (240 bytes) 5116 5 % example10c.ClassDevirtualization::main @ -2 (240 bytes) made not entrant 5116 4 example10c.ClassDevirtualization::apply2 (7 bytes) made not entrant 5116 3 example10c.ClassDevirtualization::apply1 (7 bytes) made not entrant Loading class example10.support.Sqrt to Deoptimize Now! Keep using Square in apply1... 5128 6 example10c.ClassDevirtualization::apply1 (7 bytes) 5128 7 % example10c.ClassDevirtualization::main @ 88 (240 bytes) Use AlsoSquare in apply1... 10131 6 example10c.ClassDevirtualization::apply1 (7 bytes) made not entrant 10131 8 example10c.ClassDevirtualization::apply1 (7 bytes) 10132 9 % example10c.ClassDevirtualization::main @ 131 (240 bytes) Use AnotherSquare in apply1... 15134 8 example10c.ClassDevirtualization::apply1 (7 bytes) made not entrant 15134 10 example10c.ClassDevirtualization::apply1 (7 bytes) 15135 11 % example10c.ClassDevirtualization::main @ 174 (240 bytes) Use YetAnotherSquare in apply1... 20139 12 % example10c.ClassDevirtualization::main @ 217 (240 bytes) Class Devirtualization -XX:+PrintCompilation
  • 71. WorseYet... class AlsoSquare extends Square {} class AnotherSquare extends Square {} class Square extends Func { double final apply(double x) { return x * x; } } classYetAnotherSquare extends Square {}
  • 72. Unintentional Megamorphism new HashMap<String, Integer>() {{ put(“foo”, 20); put(“bar”, 30); }};
  • 74. 68 1 java.lang.String::hashCode (55 bytes) Using Square... 79 2 example10.support.Square::apply (4 bytes) 80 3 example10d.InterfaceDevirtualization::apply1 (9 bytes) 80 4 example10d.InterfaceDevirtualization::apply2 (9 bytes) 81 5 % example10d.InterfaceDevirtualization::main @ 21 (240 bytes) Loading class example10.support.Sqrt - no CHA for interfaces! Keep using Square in apply1... 5090 6 % example10d.InterfaceDevirtualization::main @ 88 (240 bytes) Use AlsoSquare in apply1... 10094 5 % example10d.InterfaceDevirtualization::main @ -2 (240 bytes) made not entrant 10094 6 % example10d.InterfaceDevirtualization::main @ -2 (240 bytes) made not entrant 10094 3 example10d.InterfaceDevirtualization::apply1 (9 bytes) made not entrant 10095 7 example10d.InterfaceDevirtualization::apply1 (9 bytes) 10095 8 % example10d.InterfaceDevirtualization::main @ 131 (240 bytes) Use AnotherSquare in apply1... 15101 7 example10d.InterfaceDevirtualization::apply1 (9 bytes) made not entrant 15102 9 example10d.InterfaceDevirtualization::apply1 (9 bytes) 15102 10 % example10d.InterfaceDevirtualization::main @ 174 (240 bytes No CHA for Interfaces -XX:+PrintCompilation
  • 75. MaxTrivialSize 6 MaxInlineSize 35 FreqInlineSize 325 MaxInlineLevel 9 MaxRecursiveInlineLevel 1 MinInliningThreshold 250 Tier1MaxInlineSize 8 Tier1FreqInlineSize 35 Inlining Numbers to Remember... `java -XX:+PrintFlagsFinal`
  • 76. “Fun” with Unloaded public class UnloadedForever { public static void main(String[] args) { for ( int i = 0; i < 100_000; ++i ) { try { factory(); } catch ( Throwable t ) { // ignore } } } static DoesNotExist factory() { return new DoesNotExist(); } } example11a.UnloadedForever
  • 77. -XX:+PrintCompilation Unloaded Forever 72 1 java.lang.String::hashCode (55 bytes) 156 2 java.lang.Object::<init> (1 bytes) 183 3 s java.lang.Throwable::fillInStackTrace (29 bytes) 183 4 n java.lang.Throwable::fillInStackTrace (native) 183 5 java.lang.LinkageError::<init> (6 bytes) 184 6 java.lang.Error::<init> (6 bytes) 184 7 java.lang.Throwable::<init> (34 bytes) 185 8 example11a.UnloadedForever::factory (8 bytes) 186 9 java.lang.NoClassDefFoundError::<init> (6 bytes) 186 8 example11a.UnloadedForever::factory (8 bytes) made not entrant 223 10 % ! example11a.UnloadedForever::main @ 5 (23 bytes) 273 11 example11a.UnloadedForever::factory (8 bytes) 274 11 example11a.UnloadedForever::factory (8 bytes) made not entrant 358 12 example11a.UnloadedForever::factory (8 bytes) 358 12 example11a.UnloadedForever::factory (8 bytes) made not entrant 441 13 example11a.UnloadedForever::factory (8 bytes) 442 13 example11a.UnloadedForever::factory (8 bytes) made not entrant 528 14 example11a.UnloadedForever::factory (8 bytes) 978 8 example11a.UnloadedForever::factory (8 bytes) made zombie
  • 78. “Fun” with Uninitialized public class UninitializedForever { static class Uninitialized { static { if ( true ) throw new RuntimeException(); } } public static void main(String[] args) { for ( int i = 0; i < 100_000; ++i ) { try { new Uninitialized(); } catch ( Throwable t ) { // ignore } } } } example11b.UnintializedForever
  • 79. -XX:+PrintCompilation 74 1 java.lang.String::hashCode (55 bytes) 162 2 java.lang.Object::<init> (1 bytes) 188 3 s java.lang.Throwable::fillInStackTrace (29 bytes) 189 4 n java.lang.Throwable::fillInStackTrace (native) 189 5 java.lang.LinkageError::<init> (6 bytes) 190 6 java.lang.Error::<init> (6 bytes) 190 7 java.lang.Throwable::<init> (34 bytes) 191 8 java.lang.NoClassDefFoundError::<init> (6 bytes) 233 9 % ! example11b.UninitializedForever::main @ 5 (25 bytes) 241 9 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant 252 10 % ! example11b.UninitializedForever::main @ 5 (25 bytes) 263 10 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant 272 11 % ! example11b.UninitializedForever::main @ 5 (25 bytes) 281 11 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant 290 12 % ! example11b.UninitializedForever::main @ 5 (25 bytes) 299 12 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant 308 13 % ! example11b.UninitializedForever::main @ 5 (25 bytes) 318 13 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant 328 14 % ! example11b.UninitializedForever::main @ 5 (25 bytes) 337 14 % ! example11b.UninitializedForever::main @ -2 (25 bytes) made not entrant Uninitialized Forever
  • 80. null_check unexpected null or zero divisor null_assert unexpected non-null or non-zero range_check unexpected array index class_check unexpected object class array_check unexpected array class intrinsic unexpected operand to intrinsic bimorphic unexpected object class in bimorphic inlining unloaded unloaded class or constant pool entry uninitialized bad class state (uninitialized) unreached code is not reached, compiler unhandled arbitrary compiler limitation constraint arbitrary runtime constraint violated div0_check a null_check due to division by zero age nmethod too old; tier threshold reached predicate compiler generated predicate failed loop_limit_check compiler generated loop limits check failed Reasons for Deoptimizing...
  • 81. none just interpret, do not invalidate nmethod maybe_recompile recompile the nmethod; need not invalidate make_not_entrant invalidate the nmethod, recompile (probably) reinterpret invalidate the nmethod, reset IC, maybe recompile make_not_compilable invalidate the nmethod and do not compile Actions When Deoptimizing...
  • 82. Bail to Interpreter Keep Compile? Yes, recompile? No, recompile when... Now Later (Profile More)No Yes none maybe recompile make not entrant reinterpret class load! stop the world, deopt now! uncommon trap!
  • 83. null_check make_not_entrant null_assert make_not_entrant range_check make_not_entrant class_check maybe_recompile array_check maybe_recompile intrinsic maybe_recompile, make_not_entrant bimorphic maybe_recompile unloaded reinterpret uninitialized reinterpret unreached reinterpret unhandled none constraint CHA - deopt now! div0_check make_not_entrant age maybe_recompile predicate none? loop_limit_check none? Reasons & Actions
  • 84. Does This Matter? thread1 thread2 inlinedMethod2 inlinedMethod1 hotMethod code cache interpreter frame compiled frame inlinedMethod2 inlinedMethod1 hotMethod ... ... run inlinedMethod2 inlinedMethod1 hotMethod ... ... run
  • 87. VM Developer Blogs Aleksey Shipilëv http://shipilev.net/ http://psy-lob-saw.blogspot.com/ Nitsan Wakart https://twitter.com/maddocig IgorVeresov