eclipse day india 2015 - java bytecode analysis and jit

24

Upload: eclipse-day-india

Post on 09-Apr-2017

244 views

Category:

Technology


0 download

TRANSCRIPT

Java ByteCode AnalysisEducate yourself with “Java Bytecode”

Vaibhav Choudhary (@vaibhav_c)[email protected]

http://blogs.oracle.com/vaibhav

Agenda

• Java Bytecode Basics• Be familiar with Bytecode tools• Bytecode evaluation• Language Feature vs Bytecode• Dive deeper• Changes happened in JDK7 and onwards• QA

3

Java Bytecode Basics

• Why to know Java Bytecode:-

– It will help you understand your code better.– Knowing the kitchen from where food is coming– Clear the myths

4

Java Bytecode Tools

● javap -help● javap -c● javap -p ● javap -verbose● javap -c -p -verbose → Its a good option

5

Bytecode Evaluation

● Write Once, Run Anywhere (WORA)● Provides more safety and security – Managed

env.● Optimizations can be done at runtime● Its hard to deal with CPU instrs.

6

JVM Principals for Bytecode

● All operations are stack based.● Parameters, locals – All uses stack● Push/Pop/Dup/Dup2● Stack slot – 32 bit (how to handle

long/double ?)● Classes - fully qualified name

7

Class file format

8

ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count];}

Invoke* method calls

● invokestatic – invokes class (static) method● invokespecial - Invoke instance method;

special handling for superclass, private, and instance initialization method invocations

● invokeinterface – invoke interface method● invokevirutal - Invoke instance method;

dispatch based on class

9

JDK 7 onwards - InvokeDynmaic

Get rid of Myths

● Java don't have GOTO statements.– Why we need GOTO at first place ?

● Use StringBuilders if your string is changing.– What string concat do in java.

● How inner classes work– Do it mock on encapsulation.

● Many mores …

10

Execution Process

11

.java .classjavac

Just In TimeCompiler

Just In TimeCompilerNative CodeNative OS

JavaInterpreter

JavaInterpreter

Java VirtualMachine

Java VirtualMachine

Flavors of Just In Time (JIT) Compiler

• JIT comes in two flavors• C1 (Client compiler)

– -client option• C2 (Server compiler)

– -server option• -XX:+TieredCompilation

– Better decision of compilers.• What happens if: -client -XX:+TieredCompilation

12

JIT optimizations – Expression Optimization

• Eliminate dead codes.• Expression optimization.

13

int someCalculation(int x1, int x2, int x3) { int res1 = x1+x2;int res2 = x1-x2; int res3 = x1+x3;

return (res1+res2)/2; }

int someCalculation(int x1, int x2, int x3) {return x1; }

JIT Optimization – Inline Method

• Inline method– Substitute body of the method (<35 bytes of JVM bytecode)– This provides the best optimization by JIT– A better inline that C++

– Reduction of function calling overhead. Something else ?

14

int compute(int var) {int result;if(var > 5) { result = computeFurther(var); }else { result = 100;}return result;}

myVal = compute(3);

myVal = 100;

JIT Optimization – Caching Technique

• Caching read calls– Store it for future request.– major performance impact in loops

15

Point findMid(Point p1, Point p2) {Point p;p.x = (p1.x + p2.x)/2;p.y = (p1.y + p2.y)/2;return p; ......x – p1.x;y – p1.y;

while(!monitor.flag) ….....….....

boolean flag = monitor.flag

JIT Optimization – Uncommon Trap

• De-optimization – UncommonTrap– Scrap the optimization.

16

int compute(int var) {int result;if(var > 5) { result = computeFurther(var); }else { result = 100;}return result;}

int compute(int var) {if(var > 5) uncommonTrap(); return 100;}

JIT Optimization – Monomorphic dispatch

• Class Hierarchy Analysis

– Optimization decision based on assumptions.– Re-evaluate assumption when new class loaded.

17

public class Animal {private String color;public String getColor() {return color;} }

myColor = animal.getColor();

public class Animal {String color;}

myColor = animal.color;

JIT Optimization – Null Checks

• Implicit Null Check

– Virtual machine believe code will not throw null pointer.

– What if VM got a null pointer ?

18

x = point.x;y = point.y;

Equivalents to:

if(point==null) throw new NullPointerException();else {x = point.x;y = point.y;}

For each reference in your code, VMchecks null pointer. If VM assumes thatYour code will not throw NPE, it will remove the check. If happened, de-optimize.Handle the SEGV.

JIT Optimization – Multi-threading Env.

• Eliminate locks if monitor is not reachable from other threads

• Join adjacent synchronized blocks on the same object• Do check options:

– XX:+UseBiasedLocking

19

JIT Optimization – Loop Optimization

• Combining loops – Two loops can be combined if taking equivalent time.

• Inversion loops – Change while into do-while.• Tiling loops – Re-organize loop so that it will fix in

cache.

20

JVM Options

• Xint – Interpreter mode• Xcomp – Compiled mode• Xmixed – Interpreter + Compiler• -server → C2 compiler• -client → C1 compiler• -XX:+TieredCompilation → C1 + C2 (used by 32/64 bit

mode)• -d32/-d64 options

21

Benchmarking Difficulties

• Garbage collect can distort benchmarking.• Benchmarking can be distorted by unintentionally

dead code• Improper warm-up can distort benchmarking.• Dynamic De-optimziation can distort benchmarking.• Momomorphic call can distort benchmarking.• Inlining of code can distort benchmarking.

22

Logging Options

• -XX:+UnlockDiagnosticVMOptions• -XX:+LogCompilation• -XX:LogFile=<path to file>• -XX:MaxInlineSize=<size>• -XX:FreqInlineSize=<size>

• Many more ...

23

References

• Subscribe with - http://www.javaperformancetuning.com/

• Java Performance Documentation - Link• Oracle JIT Compilation Documentation - Link • Understand JIT Compilation with JITWatch - Link• My Blog – http://blogs.oracle.com/vaibhav

24