90
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. The Java HotSpot VM Under the Hood Tobias Hartmann Zoltán Majó HotSpot Compiler Team Oracle Corpora6on

The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

TheJavaHotSpotVMUndertheHood

TobiasHartmannZoltánMajóHotSpotCompilerTeamOracleCorpora6on

Page 2: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Aboutus•  TobiasHartmann–  MScETHZurich,Switzerland–  LivesinRheinfelden,Germany

•  ZoltánMajó–  PhDETHZurich,Switzerland–  GrewupinCluj,Romania

•  Bothofus:@Oraclesince2014–  CompilerteamfortheJavaHotSpotVirtualMachine

2

Page 3: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirecRon.ItisintendedforinformaRonpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfuncRonality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andRmingofanyfeaturesorfuncRonalitydescribedforOracle’sproductsremainsatthesolediscreRonofOracle.

3

Page 4: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Atypicalcompu6ngplaJorm

4

Hardware

Opera6ngsystem

JavaVirtualMachine

UserApplica6ons

JavaEEJavaSE

Applica'onSo,ware

SystemSo,ware

Page 5: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Atypicalcompu6ngplaJorm

5

Hardware

Opera6ngsystem

JavaVirtualMachine

UserApplica6ons

JavaEEJavaSE

Applica'onSo,ware

SystemSo,ware

Page 6: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Atypicalcompu6ngplaJorm

6

Hardware

Opera6ngsystem

JavaVirtualMachine

UserApplica6ons

JavaEEJavaSE

Applica'onSo,ware

SystemSo,ware

Page 7: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Outline• Whyvirtualmachines?•  TheJavaHotSpotVM–  Just-in-RmecompilaRon–  OpRmisRccompileropRmizaRons–  TieredcompilaRon–  Recentprojects:SegmentedCodeCache,CompactStrings–  Future:AOT,JVMCI

•  Conclusions

7

Page 8: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Programminglanguageimplementa6on

C

Windows

Intelx86

8

Opera6ngsystem

Languageimplementa6on

Hardware

Programminglanguage

CompilerStandardlibrariesDebuggerMemorymanagement

Linux

Intelx86

CompilerStandardlibrariesDebuggerMemorymanagement

Linux

ARM

CompilerStandardlibrariesDebuggerMemorymanagement

Solaris

SPARC

CompilerStandardlibrariesDebuggerMemorymanagement

Page 9: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 9

(Language)virtualmachine

Java

Windows

Intelx86

Opera6ngsystem

Virtualmachine

Hardware

Programminglanguage

HotSpotVM

PPC ARM SPARC

MacOSX SolarisLinux

JavaScript Scala Python

Page 10: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

TheVM:Anapplica6ondeveloper’sview

Javasourcecodeinti=0;do{i++;}while(i<f());

Bytecodes0:iconst_01:istore_12:iinc5:iload_16:invokestaticf9:if_icmplt212:return

compileHotSpotJavaVMexecute

• Ahead-of-6me• Usingjavac

• Instruc6onsforanabstractmachine• Stack-basedmachine(noregisters)

10

Page 11: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

TheVM:AVMengineer’sview

11

Bytecodes0:iconst_01:istore_12:iinc5:iload_16:invokestaticf9:if_icmplt212:return

HotSpotJavaVM

Garbagecollector

manage

Interpreter

executeHeap

Stack

access

access

Compila6onsystem

compile

C1

C2

Compiledmethodproduce

Machinecode

Debuginfo

Objectmaps

Page 12: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

MajorcomponentsofHotSpot•  Run6me–  Interpreter–  Threadmanagement–  SynchronizaRon–  Classloading

•  Heapmanagement–  Garbagecollectors

•  Compila6onsystem

12

Page 13: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Interpreta6onvs.compila6oninHotSpot•  Template-basedinterpreter–  GeneratedatVMstartup(beforeprogramexecuRon)–  Mapsawell-definedmachinecodesequencetoeverybytecodeinstrucRon

–  OpRmizaRon:cachetop-of-stackvalueinaregistertoreduce#ofmemoryaccesses

•  Compila6onsystem–  SpeeduprelaRvetointerpretaRon:~100X–  Twojust-in-'mecompilers(C1,C2)–  AggressiveopRmisRcopRmizaRons

13

Bytecodes0:iconst_01:istore_12:iinc5:iload_16:invokestaticf9:if_icmplt212:return

Machinecodemov-0x8(%r14),%eaxmovzbl0x1(%r13),%ebxinc%r13mov$0xff40,%r10jmpq*(%r10,%rbx,8)

Loadlocalvariable1

DispatchnextinstrucRon

Page 14: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Ahead-of-6mevs.just-in-6mecompila6on•  AOT:Beforeprogramexecu6on•  JIT:Duringprogramexecu6on

•  Tradeoff:Resourceusagevs.performanceofgeneratedcode

14

Performance

Amountofcompila6onInterpreta'on Compileeverything

Badperformanceduetointerpreta6on

Badperformanceduetocompila6onoverhead

Goodperformanceduetogoodselec6onofcompiledmethodsandofappliedop6miza6ons

Page 15: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Balancingresourceusageandperformance•  Gecngtothe“sweetspot”•  Carefullyselec6ng

1.  Methodstocompile2.  AppliedcompileropRmizaRons

15

Page 16: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

1.Selec6ngmethodtocompile•  Hotmethods(frequentlyexecutedmethods)•  Profilemethodexecu6on–  #ofmethodinvocaRons,#ofbackedges

•  Amethod’slife6meintheVM

16

Interpreter Compiler(C1orC2) Codecache

GatherprofilinginformaRon CompilebytecodetonaRvecode Storemachinecode

#methodinvocaRons>THRESHOLD1#ofbackedges>THRESHOLD2

DeopRmizaRon

Compiler’sopRmisRcassumpRonsprovenwrong

Page 17: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Virtualcallinlining

17

classA{voidbar(){S1;}}

classBextendsA{voidbar(){S2;}}

voidfoo(){Aa=create();//returnAorBa.bar();}

Classhierarchy Methodtobecompiled

loaded

notloaded

Compiler:Inlinecall?Yes.

Page 18: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Virtualcallinlining

•  Benefitsofinlining–  Virtualcallavoided–  Codelocality

•  Op6mis6cassump6on:onlyAisloaded–  Notedependenceonclasshierarchy–  DeopRmizeifhierarchychanges

18

classA{voidbar(){S1;}}

classBextendsA{voidbar(){S2;}}

voidfoo(){Aa=create();//returnAorBS1;}

Classhierarchy Methodtobecompiled

loaded

notloaded

Compiler:Inlinecall?Yes.

Page 19: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Virtualcallinlining

19

classA{voidbar(){S1;}}

classBextendsA{voidbar(){S2;}}

voidfoo(){Aa=create();//returnAorBS1;}

Classhierarchy Methodtobecompiled

loaded

notloaded

Page 20: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Virtualcallinlining

20

classA{voidbar(){S1;}}

classBextendsA{voidbar(){S2;}}

voidfoo(){Aa=create();//returnAorBS1;}

Classhierarchy Methodtobecompiled

loaded

loaded

Page 21: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Virtualcallinlining

21

classA{voidbar(){S1;}}

classBextendsA{voidbar(){S2;}}

voidfoo(){Aa=create();//returnAorBa.bar();}

Classhierarchy Methodtobecompiled

loaded

loaded

Compiler:Inlinecall?No.

Page 22: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

False

Controlflowgraph Generatedcode

Hotpathcompila6on

S1;S2;S3;if(x>3)

S4; S5;S6;S7;

S8;S9;

10’000 0

guard(x>3)S1;S2;S3;S4;S5;

Uncommontrap

22

True

Page 23: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Deop6miza6on•  Compiler’sop6mis6cassump6onprovenwrong–  AssumpRonsaboutclasshierarchy–  ProfileinformaRondoesnotmatchmethodbehavior

•  Switchexecu6onfromcompiledcodetointerpreta6on–  ReconstructstateofinterpreteratrunRme–  CompleximplementaRon

•  Compiledcode–  Possiblythrownaway–  Possiblyreprofiledandrecompiled

23

Page 24: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Performanceeffectofdeop6miza6on•  Followthevaria6onofasinglemethod’sperformance

24

Performance

TimeVMStartup VMTeardown

Interpreted Compiled Interpreted Compiled

Compila>on Deop>miza>on Compila>on

Page 25: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

2.Selec6ngcompilerop6miza6ons•  C1compiler–  LimitedsetofopRmizaRons–  FastcompilaRon–  Smallfootprint

•  C2compiler–  AggressiveopRmisRcopRmizaRons–  Highresourcedemands–  High-performancecode

•  Graal–  Experimentalcompiler–  NotpartofHotSpot

25

ClientVM

ServerVM

Tieredcompila6on(enabledsinceJDK8)

Page 26: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Balancingresourceusageandperformance1.   Selec6ngmethodstocompile–  “Hot”methods–  ControlledbyinvocaRonandbackedgethreshold

2.   Choosingcompilerop6miza6ons–  C1:moderatelyop'mizingandfastcompiler–  C2:highlyop'mizingandslowcompiler–  LimitaRon(beforeJDK8):SinglecompilerintheVM(clientorserver)–  StarRngwithJDK8:BothcompilersenabledatthesameRme(ReredcompilaRon)

26

Page 27: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Outline• Whyvirtualmachines?•  TheJavaHotSpotVM–  Just-in-RmecompilaRon–  OpRmisRccompileropRmizaRons–  TieredcompilaRon–  Recentprojects:SegmentedCodeCache,CompactStrings–  Future:AOT,JVMCI

•  Conclusions

27

Page 28: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Tieredcompila6on•  Combinethebenefitsof–  Interpreter:Faststartup–  C1:Fastwarmup–  C2:Highpeakperformance–  SRllwithinthesweetspotofresourceusage/performancetradeoff

28

Page 29: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Benefitsof6eredcompila6on(ar6st’sconcept)

29

Performance

TimeVMStartup VMTeardown

Interpreted C1-compiled

Methodwarm-upRme

ClientVM(C1only)

Page 30: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Benefitsof6eredcompila6on(ar6st’sconcept)

30

Performance

TimeVMStartup VMTeardown

Interpreted C2-compiled

Methodwarm-upRme

ServerVM(C2only)

Page 31: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Benefitsof6eredcompila6on(ar6st’sconcept)

31

Performance

TimeVMStartup VMTeardown

Interpreted C1-compiled

Methodwarm-upRme

Tieredcompila6on

C2-compiled

Page 32: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Tieredcompila6on•  Combinedbenefitsofinterpreter,C1,andC2•  Addi6onalbenefits–  MoreaccurateprofilinginformaRon

32

Page 33: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Moreaccurateprofiling

Rme

Interpreter C1(profiled) C2(non-profiled)

Interpreter

Profilingwithout6eredcompila6on

Profilingwith6eredcompila6on

C2(non-profiled)

300samples

100samples 1000samples

100samples 200samples

w/6eredcompila6on:1’100samplesgatheredw/o6eredcompila6on:300samplesgathered

33

Page 34: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Tieredcompila6on•  Combinedbenefitsofinterpreter,C1,andC2•  Addi6onalbenefits–  MoreaccurateprofilinginformaRon

•  Drawbacks–  CompleximplementaRon–  CarefultuningofcompilaRonthresholdsneeded–  Morepressureoncodecache–Tobiaswilltellyoumoreaboutthat

34

Page 35: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Amethod’slife6me(w/6eredcompila6on)

Interpreter C1 C2

Codecache

CollectprofilinginformaRon GeneratecodequicklyConRnuecollecRngprofilinginformaRon

Generatehigh-qualitycodeUseprofilinginformaRon

DeopRmizaRon

35

Page 36: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Performanceofasinglemethod(w/6eredcompila6on)

36

Performance

TimeVMStartup VMTeardown

Interpreted C1compiled Interpreted C2compiled

Compila>on

Deop>miza>on

Compila>on

Interpreted C2compiled

Deop>miza>on

Compila>on

Page 37: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Compila6onlevels(detailedview)

Interpreter

C1:noprofiling

C1:limitedprofiling

C1:fullprofiling

C2

0

1

2

3

4

CompilaRo

nlevel

TypicalcompilaRonsequence

Associatedthresholds:Tier3InvokeNoRfyFreqLogTier3BackedgeNoRfyFreqLogTier3InvocaRonThresholdTier3MinInvocaRonThresholdTier3BackEdgeThresholdTier3CompileThreshold

Associatedthresholds:Tier4InvocaRonThresholdTier4MinInvocaRonThresholdTier4CompileThresholdTier4BackEdgeThreshold

37

Page 38: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Outline• Whyvirtualmachines?•  TheJavaHotSpotVM–  Just-in-RmecompilaRon–  OpRmisRccompileropRmizaRons–  TieredcompilaRon–  Recentprojects:SegmentedCodeCache,CompactStrings–  Future:AOT,JVMCI

•  Conclusions

38

Page 39: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Part1:SegmentedCodeCacheImprovingthelayoutofJITgeneratedcode

39

Page 40: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

Background

Challenges

Design

Evalua6on

Conclusion

1

2

3

4

40

5

Page 41: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

Background

Challenges

Design

Evalua6on

Conclusion

1

2

3

4

41

5

Page 42: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Whatisacodecache?•  StorescodegeneratedbyJITcompilers•  Con6nuouschunkofmemory–  Fixedsize-XX:ReservedCodeCacheSize–  BumppointerallocaRonwithfreelist

• Memorymanagedbysweeper–  Coldmethodsareevicted–  Hotmethodsremain

• WhyshouldIcare?–  EssenRalforperformance

42

RunRme

Debugging

Compiler

GC

Serviceability

Sweeper

Page 43: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Codecacheusage:JDK6and7

freespaceVMinternalscompiledcode

43

Page 44: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Codecacheusage:JDK8(TieredCompila6on)

44

freespace

VMinternals

C1compiled(profiled)

C2compiled(non-profiled)

Page 45: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Codecacheusage:JDK9

45

freespace

VMinternals

C1compiled(profiled)

C2compiled(non-profiled)

Page 46: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

Background

Challenges

Design

Evalua6on

Conclusion

1

2

3

4

46

5

Page 47: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Challenges•  Tieredcompila6onincreasesamountofcodeby2-4X•  Allcodeisstoredinasinglecodecache–  DifferenttypeswithdifferentcharacterisRcs–  Differentusagefrequencies(hotness)–  AccesstospecificcoderequiresfulliteraRon

•  Highfragmenta6onandbadlocality

47

Page 48: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Proper6esofcompiledcode

Op6miza6onlevel Size

Costofcompila6on Life6me

48

Page 49: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Typesofcompiledcode

Non-methodcode opRmized small cheap immortal

Profiledcode(C1) instrumented medium cheap limited

Non-profiledcode(C2) highlyopRmized large expensive long

49

Page 50: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Codecacheusage

50

freespace

VMinternals

C1compiled(profiled)

C2compiled(non-profiled)

Page 51: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 51

Codecacheusage:Reality

profiledcodenon-profiledcode

freespace

Page 52: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 52

Codecacheusage:Reality

profiledcodenon-profiledcode

freespace

hotness

Page 53: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

Background

Challenges

Design

Evalua6on

Conclusion

1

2

3

4

53

5

Page 54: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

• WithoutSegmentedCodeCache • WithSegmentedCodeCache

54

Design

CodeCache

non-profiledmethods

profiledmethods

non-methods

Page 55: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

non-profiledmethodsprofiledmethods

55

SegmentedCodeCache:Reality

profiledcodenon-profiledcode

freespace

Page 56: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 56

SegmentedCodeCache:Reality

non-profiledmethodsprofiledmethods hotness

Page 57: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

Background

Challenges

Design

Evalua6on

Conclusion

1

2

3

4

57

5

Page 58: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Evalua6on:Codelocality•  Instruc6onCache(ICache)–  14%lessICachemisses

•  Instruc6onTransla6onLookasideBuffer(ITLB1)–  44%lessITLBmisses–  9%speedupwithmicrobenchmark

58

1cachesvirtualtophysicaladdressmappingstoavoidslowpagewalks

Page 59: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Evalua6on:Sweeper

59

0

5

10

15

20

25

30

35

40

#fullsweeps CleanuppauseRme SweepRme

Redu

c6on

in%

Page 60: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Evalua6on:Run6me

60

0

2

4

6

8

10

12

14

SPECjbb2005 SPECjbb2013 JMH-Javac Octane(Typescript) Octane(Gbemu)

Improvem

entin%

Page 61: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

Background

Challenges

Design

Evalua6on

Conclusion

1

2

3

4

61

5

Page 62: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Conclusion•  Codelayoutmaters–  Significantimpactonperformance–  CodelocalityreducesiTLBmisses

•  SegmentedCodeCachehelps–  Lesssweeperoverhead–  ReducedfragmentaRon

•  Baseforfutureextensions–  Newcodetypes–  SeparaRonofcodeandmetadata

62

Page 63: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Part2:CompactStringsImproveVMinternalhandlingofStrings

63

Page 64: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

JavaStrings

ProjectGoals

Design

Evalua6on

Conclusion

1

2

3

4

64

5

Page 65: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

JavaStrings

ProjectGoals

Design

Evalua6on

Conclusion

1

2

3

4

65

5

Page 66: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

publicclassHelloWorld{publicstaticvoidmain(String[]args){

StringmyString="HELLO";System.out.println(myString);

}}

JavaStrings

66

Page 67: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 67

publicclassHelloWorld{publicstaticvoidmain(String[]args){

StringmyString="HELLO";System.out.println(myString);

}}

JavaStrings

publicfinalclassString{privatefinalcharvalue[];...}

Page 68: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 68

publicclassHelloWorld{publicstaticvoidmain(String[]args){

StringmyString="HELLO";System.out.println(myString);

}}

JavaStrings

publicfinalclassString{privatefinalcharvalue[];...}

charvalue[]=

H0x0048 0x0045 0x004C 0x004C 0x004F

2bytes

E L L OUTF-16encoded

Page 69: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

“PerfecRonisachieved,notwhenthereisnothingmoretoadd,butwhenthereisnothingmoretotakeaway.”–  AntoinedeSaintExupéry

69

Page 70: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Thereisalottotakeawayhere..•  UTF-16encodedStringsalwaysoccupytwobytesperchar• WastedmemoryifonlyLa6n-1(one-byte)charactersused:

70

charvalue[]=

H0x0048 0x0045 0x004C 0x004C 0x004F

2bytes

E L L O

Page 71: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Thereisalottotakeawayhere..•  UTF-16encodedStringsalwaysoccupytwobytesperchar• WastedmemoryifonlyLa6n-1(one-byte)charactersused:

•  Butisthisaprobleminreallife?

71

charvalue[]=

H0x0048 0x0045 0x004C 0x004C 0x004F

2bytes

E L L O

Page 72: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Reallifeanalysis:char[]footprint•  950heapdumpsfromavarietyofapplica6ons–  char[]footprintmakesup10%-45%oflivedata–  Majorityofcharactersaresinglebyte–  75%ofStringsaresmallerthan35characters–  75%ofCharactersareinStringsoflength<250

•  Predictedfootprintreduc6onof5%-10%

72

Page 73: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

JavaStrings

ProjectGoals

Design

Evalua6on

Conclusion

1

2

3

4

73

5

Page 74: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProjectGoals• Memoryfootprintreduc6onbyimprovingspaceefficiencyofStrings• MeetorbeatthroughputperformanceofbaselineJDK9

•  Fullcompa6bilitywithrelatedJavaandna6veinterfaces

•  FullplaJormsupport–  x86/x64,SPARC,ARM–  Linux,Solaris,Windows,MacOSX

74

Page 75: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

JavaStrings

ProjectGoals

Design

Evalua6on

Conclusion

1

2

3

4

75

5

Page 76: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Design•  Stringclassnowusesabyte[]insteadofachar[]

•  Addi6onal'coder'fieldindicateswhichencodingisused

76

publicfinalclassString{privatefinalbytevalue[];privatefinalbytecoder;...}

H E L L Obytevalue[]= 0x00 0x48 0x00 0x45 0x00 0x4C 0x00 0x4C 0x00 0x4F

bytevalue[]= 0x48 0x45 0x4C 0x4C 0x4F

UTF-16encoded

La6n-1encoded

H E L L O

Page 77: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Design•  Ifallcharactershaveazeroupperbyte

→ StringiscompressedtoLa6n-1bystrippingoffhighorderbytes

•  IfAcharacterhasanon-zeroupperbyte→ StringcannotbecompressedandisstoredUTF-16encoded

77

bytevalue[]= 0x00 0x48 0x00 0x45 0x00 0x4C 0x00 0x4C 0x00 0x4F

bytevalue[]=0x48 0x45 0x4C0x4C 0x4F

UTF-16encoded

La6n-1encoded

Infla6onCompression

Page 78: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Design•  Compression/infla6onneedstofast•  RequiresHotSpotsupportinaddi6ontoJavaclasslibrarychanges–  JITcompilers:IntrinsicsandStringconcatenaRonopRmizaRons–  RunRme:Stringobjectconstructors,JNI,JVMTI–  GC:StringdeduplicaRon

•  KillswitchtoenforceUTF-16encoding(-XX:-CompactStrings)–  ForapplicaRonsthatextensivelyuseUTF-16characters

78

Page 79: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

JavaStrings

ProjectGoals

Design

Evalua6on

Conclusion

1

2

3

4

79

5

Page 80: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Evalua6on•  Newandexis6ngunitests• MicrobenchmarksattheStringAPIlevel

•  Largebenchmarkstomeasureoverallperformance

80

Page 81: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved. 81

publicclassLogLineBench{intsize;Stringmethod=generateString(size);publicStringwork()throwsExceptions{return"["+System.nanoTime()+"]"+Thread.currentThread().getName()+"Callinganapplicationmethod\""+method+"\"withoutfearandprejudice.";}

Microbenchmark:LogLineBench

Page 82: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

LogLineBenchresultsPerformancens/op Allocatedb/op

1 10 100 1 10 100

Baseline 149 153 231 888 904 1680

CSdisabled 152 150 230 888 904 1680

CSenabled 142 139 169 504 512 904

82

•  Killswitchworks(noregression)•  27%performanceimprovementand46%footprintreducRon

Page 83: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Largeworkloads•  SPECjbb2005–  21%footprintreducRon–  27%lessGCs–  5%throughputimprovement

•  SPECjbb2015–  7%footprintreducRon–  11%criRcal-jOpsimprovement

• Weblogic(startup)–  10%footprintreducRon–  5%startupRmeimprovement

83

Page 84: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

ProgramAgenda

JavaStrings

ProjectGoals

Design

Evalua6on

Conclusion

1

2

3

4

84

5

Page 85: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Conclusion

CompactStringshelpsourapplica6onsalot.

Ongoingeffort:IndifyStringConcat,FusedStrings

TryoutJDK9earlyaccess:jdk9.java.net/download/

..andtellushowitperformswithyourapplica6ons!

85

Page 86: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Future•  AOT:Ahead-of-6mecompila6on–  CompiletonaRvecode(nottoJavabytecodes)–  MoreinformaRon:hvps://www.youtube.com/watch?v=Xybzyv8qbOc(45-minutetalkfromJVMLS’15)

•  JVMCI:JavaVirtualMachineCompilerInterface–  CurrentcompilerswriveninC/C++–  JVMCI:InterfacetoallowJavacodetointerceptJVMacRvityandplug-innaRvecode–  Experimentalfeature,GraalandSubstrateVMuseit

86

Page 87: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Conclusions•  Java–avibrantplaJorm–  Newfeatures:SegmentedCodeCache,CompactStrings,JVMCI–  …andmanyotherfeaturestobereleasedwithJDK9–  Staytuned!

•  ThefutureoftheJavaplaJorm

"OurSaaSproductsarebuiltontopofJavaandtheOracleDB—that’stheplaMorm.”LarryEllison,OracleCTO

87

Page 88: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Thankyouforyouraten6on!

88

Page 89: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code
Page 90: The Java HotSpot VM Under the Hoodcr.openjdk.java.net/~thartmann/talks/2016-Hotspot... · – Generated at VM startup (before program execuRon) – Maps a well-defined machine code

Copyright©2016,Oracleand/oritsaffiliates.Allrightsreserved.

Backupslides

90