Another compilation method in java - AOT (Ahead of Time) compilation

34
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. Another compilation method in Java AOT (Ahead of Time) Compilation Akihiro Nishikawa Oracle Corporation Japan 1

Transcript of Another compilation method in java - AOT (Ahead of Time) compilation

Page 1: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

AnothercompilationmethodinJavaAOT(AheadofTime)Compilation

AkihiroNishikawaOracleCorporationJapan

1

Page 2: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.

2

Page 3: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

Agenda

JIT(Just-in-Time)Compilation

AOT(Ahead-of-Time)Compilation

Limitations

Summary

1

2

3

4

3

Page 4: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 4

Inthissession,ImainlycoverHotSpot.

Page 5: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 5

JIT(JustinTime)Compilation

SourceCode(*.java)

javac

Classfile(*.class)

HotSpot

Compile

Page 6: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 6

ForimprovementofwarmuptimeTieredcompilation

C1(-client/clientuse) C2(-server/serveruse)

• Longercompilationtimeandlongerstartuptime,butgeneratedcoderunsfaster.

• Heavyoptimization

• Incaseofprioritizingperformanceafterinvocation...

• Shortercompilationtimeandshorterstartuptime,butgeneratedcoderunsslow.

• Lessoptimization

• Incaseofprioritizingstartuptime...

Page 7: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 7

CompilationLevel

• 4:C2• 0:Interpreter

C2Interpreter C1

• 1:C1fulloptimization(noprofiling)• 2:C1withprofilingaboutinvocationandback-edgeonly• 3:C1fullprofiling(level2+MDO)

1,500 10,000

Page 8: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

AOT– AheadOfTimecompilation•Generatenativecodeinadvance

• JEP295

•BasedonGraal

8

Page 9: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 9

AOTCompilation

SourceCode(*.java)

javac

Classfile(*.class)

HotSpot

jaotc

Sharedobjectfile(*.so)

Page 10: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 10

*.java

javac

*.class HotSpot

jaotc

*.so

javac Compile

Page 11: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

*.so

11

Abletoshare“*.so”fileamongJVMs

HotSpot

HotSpot

HotSpot

HotSpot

Page 12: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 12

AOTlookslikeCDS/AppCDS…

!

Page 13: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

CDS/AppCDS (ClassDataSharing)• CDSistheschemeforsharingJavaSEclasslibraryonly.• IncaseofAppCDS,applicationclassesarealsoincluded.

13

ClassDataclasses.jsa

HotSpot

HotSpot

HotSpot

HotSpot

Page 14: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

Worksfor...• Quickerwarmup• Lowertotalmemoryfootprint

Doesnotworkfor...• Nofeaturetopersistorsharemachine(native)code.

14

CDS/AppCDS

Page 15: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 15

IsAOTanewcompilationmethod?

!

Page 16: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 16

$ gcc -o HelloAoT HelloAoT.c

IncaseofC...

Page 17: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 17

HowaboutotherJavaimplementations?

!

Page 18: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

AOTJavaCompilers• IBMJavaSDKforAIX

• IBMJavaSDKforz/OS

• OracleJavaMEEmbeddedClient

•WebSphereRealTime

• GluonVMetc.

18

Page 19: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 19

DoesAOTsupporttieredcompilation(C1/C2)?

!

Page 20: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

AOT SupportsTieredCompilation• Non-tieredcompilationmode(default)–SimilartoC1inClientVM–Nocollectingprofilinginformation–NoJITrecompilationifAOTcodeisnotdeoptimized.

• Tiedcompilationmode(--compile-for-tiered)–ProfilinglevelisassameasC1Level2.– IfhittingAOTinvocationthresholds,methodsarerecompiledbyC1atLevel3firstforgatheringfullprofilinginformation.

20

Page 21: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 21

WhatdoesAOTworkfor?

!

Page 22: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

Benefitsmight begainedfromAOT...• Nocompilationoverheadatruntime.• Improvestartuptimeandabletoachievepeakperformancefaster.• Abletorunontheplatformwherenativecodecannotbenotgeneratedatruntime(e.g.iOS,embedded).• Densityimprovement- Abletosharenativecode.

22

Page 23: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 23

$ jaotc --output libHelloAOT.so HelloAOT.class

$ jaotc --output libjava.base.so --module java.base

$ jaotc --output libmyapp.so --jar myapp.jar

$ jaotc -J-XX:+UseCompressedOops --output libHelloAOT.so HelloAOT.class

Howtousejaotc

Page 24: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

jaotc doesnotresolveallreferencedclasses• jaotc doesnotresolvereferencedclasseswhicharenotsystemclassesorpartofcompiledclasses.1. Havetoaddreferencedclassestoclasspath.

2. Specifyadditionaljavamodules

24

jaotc --output=libfoo.so --jar foo.jar ¥-J-cp -J./

jaotc --output=libactivation.so --module ¥java.activation -J--add-module=java.se.ee

Page 25: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 25

$ java -XX:AOTLibrary=./libHelloAOT.so HelloAOTHello AOT!

$ java -XX:+PrintAOT ¥-XX:AOTLibrary=./libHelloAoT.so HelloAoT13 1 loaded ./libHelloAoT.so aot library76 1 aot[ 1] HelloAoT.<init>()V76 2 aot[ 1] HelloAoT.main([Ljava/lang/String;)V

Hello AOT!

RunAOTcompiledcode

Page 26: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

OnJVMstartup...• AOTinitializationcodelooksforwell-knownAOTlibrariesinwell-knownlocation($JAVA_HOME/lib)orlibrariesspecifiedusing-XX:AOTLibrary.• JVMknowsAOTlibrarynameforthefollowingJavamodules.–java.base–jdk.compiler (javac)–jdk.scripting.nashorn (Nashorn)–jdk.internal.vm.ci (JVMCI)–jdk.internal.vm.compiler (Graal)

26

Page 27: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

VMOptionsshouldbesynchronized.• libjava.base.so-XX:-UseCompressedOops -XX:+UseG1GC• libjava.base-coop.so-XX:+UseCompressedOops -XX:+UseG1GC• libjava.base-nong1.so-XX:-UseCompressedOops -XX:+UseParallelGC• libjava.base-coop-nong1.so-XX:+UseCompressedOops -XX:+UseParallelGC

27

Page 28: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved. 28

Page 29: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

Limitations

• Experimentalrelease• Nosupport• Noofficialdocument(exceptforJEP)

29

Firstofall...

Page 30: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

Limitations

• AvailableonlywhenusingeitherParallelor G1GConLinuxx64withlibelf.so.• AOTcompilationmustbeexecutedonthesamesystem orasystemwiththesameconfiguration onwhichAOTcodewillbeusedbyJavaapplication.• Unabletocompilejavacodeusingdynamicallygeneratedclassesandbytecode (lambdaexpressions,InvokeDynamic)

30

TheselimitationsareinJava9,andmaybeaddressedinfuturereleases.

Page 31: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

WhenandwheretouseAOT?

• Event-drivenservicessuchasFunctions• Embedded,IoT,mobile• Notofteninvokedclasses...(nothotcode)• Applicationcodewhichshouldbeprotectedfromdecompilers

...etc.

31

IfAOTisofficiallysupportedandisavailableonseveralplatforms...

Page 32: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

Summary• SinceJava9(HotSpot),AOTcompilationisavailable,butpositionedasexperimentalreleaseandnoofficialsupport.

• AOThaveseveralcharacteristicsandisexpectedtoimproveperformanceofapplications,especiallysuchshort-livedobjectsasfunctions

•WorkingtoremovecurrentlimitationandimproveAOT.

32

Page 33: Another compilation method in java - AOT (Ahead of Time) compilation

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.

SafeHarborStatementTheprecedingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.

33

Page 34: Another compilation method in java - AOT (Ahead of Time) compilation