Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SpringとGrarlVM Native Image -2019/12-

417 views

Published on

2019/12/18 Spring Fest 2019 @ソラシティカンファレンスセンター の講演で使用した資料です

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

SpringとGrarlVM Native Image -2019/12-

  1. 1. Spring GraalVM Native Image - 2019/12 - 2019 12 18 Spring Fest 2019 NTT
  2. 2. • • NTT 2 • Software Engineering, Computer Science • Spring – JSUG – (Spring I/O) (SpringOne) 2 5 – – Spring
  3. 3. • Native Image – Native Image • 3 – Native Image • Spring Native Image – Spring Native Image 2019/12
  4. 4. Native Image
  5. 5. GraalVM Native Image GraalVM Native Image allows you to ahead-of-time compile Java code to a standalone executable, called a native image. https://graalvm.org/docs/reference-manual/native-image • Java JVM • 5 5 – linux : .so – macOS : .dylib – Windows : .dll 2019 12 Early Adopter Technology $ javac Hello.java $ native-image -cp . Hello $ ./hello Hello world!
  6. 6. AOT JIT(Just-in-Time) AOT(Ahead-of-Time) 6 6 JIT 6 Java 6 AOT Java .java.java.java .class.class.class javac JVM JIT 6 6 6 6 6 6 6 6 6 6 66 .java.java.java .class.class.class javac AOT 6 6 6 6 6
  7. 7. Native Image DEMO
  8. 8. Native Image – Native Image
  9. 9. Native Image – import org.graalvm.nativeimage.IsolateThread; import org.graalvm.nativeimage.c.function.CEntryPoint; public class Shared{ @CEntryPoint(name="increment") public static int increment(IsolateThread thread, int num){ return num+1; } } $ ls Shared.class Shared.java libincrement.dylib libincrement.h libincrement_dynamic.h graal_isolate.h graal_isolate_dynamic.h Native Image #include<libincrement> C
  10. 10. Native Image JVM • JIT • AOT * * GraalVM Enterprise Edition Profile-guided Optimizations 0 AdoptOpenJDK 11.0.5 Native Image (GraalVM EE 19.3.0) https://nowokay.hatenablog.com/entry/2019/06/27/045352 (GraalVM 1 – Hatena)
  11. 11. CE EE 0 10 20 30 40 50 60 70 80 AdoptOpenJDK GraalVMEE (J) GraalVMCE (J) GraalVMEE (A) GraalVMCE (A) MacBook Pro (13-inch, late 2016, 2.9GHz Core i5, 16GB RAM) 3 JIT AOT (Native Image) CE Native Image EE Native Image JIT
  12. 12. Pros – JVM – – – 2 Cons – – 1 – 2 Native Image Pros/Cons
  13. 13. 1 4 4 … • 4 • Java1 • JVM AOT !
  14. 14. Closed-World Assumption To be able to build a highly optimized native executable, we run an aggressive static analysis that requires a closed-world assumption, … https://graalvm.org/docs/reference-manual/native-image Closed-World Assumption 5 1 1
  15. 15. Native image 6 • 1 • 6 6 • CPU OS
  16. 16. Native image 7 7 1 7 • 7 •
  17. 17. -H:+PrintAnalysisCallTree 8 – Substrate VM* – 1 • Override – * JVM GC
  18. 18. Native image 1 • 9 1 • • static • Native Image
  19. 19. Native image 2 0 CC 0
  20. 20. Native image 1 1 1 2
  21. 21. Java (1/2) https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md 2 JNI Unsafe InvokeDynamic / MethodHandler Lambda
  22. 22. Java (2/2) https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md Synchronized 2 Reference identifyHashCode 3 JVMTI, JMX, VM JCA
  23. 23. 4 4 4 Class Method 4 2 2 4 4 → “ReflectTarget” (“getMessage()”) Class[] classes = new Class[0]; String message = (String)Class.forName("ReflectTarget") .getMethod("getMessage",classes) .invoke(null);
  24. 24. [ { "name" : "java.lang.Class", "allDeclaredConstructors" : true, "allPublicConstructors" : true, "allDeclaredMethods" : true, "allPublicMethods" : true, "allDeclaredClasses" : true, "allPublicClasses" : true }, { "name" : "java.lang.String", "fields" : [ { "name" : "value", "allowWrite" : true }, { "name" : "hash" } ], 2 2 2 2 2 -H:ReflectionConfigurationFiles json 5
  25. 25. java.lang.reflect.Proxy Class[] ifs = {IF.class}; Proxy.getProxyClass(Main.class.getClassLoader(), ifs); 2 6 2 → “IF”
  26. 26. 2 2 7 → 2 2 -H:DynamicProxyConfigurationFiles json [ ["java.lang.AutoCloseable", "java.util.Comparator"], ["java.util.Comparator"], ["java.util.List"] ] 7
  27. 27. static 8 8 public class Hoge{ static { … } Native Image static 8Native Image 8 Native Image 1. Native Image 8 2. * 2 8 * --initialize-at-run-time= xxx.yyy.Hoge, … 3. 2. 8 2 8
  28. 28. Class / ClassLoader jar Hoge.class .getClassLoader() .getResource("fuga.txt"); 2 Native Image getResource null -H:ResourceConfigurationFiles 9 json "resources": [ {"pattern": ”hoge/.*"}, {"pattern": ”path/to/class/Hoge.class"}, …
  29. 29. • 0 • Native Image Java • 0 03 0
  30. 30. Spring Native Image
  31. 31. Spring Spring @Component("someBean") public class Hoge{ public @interface Component { String value() default ""; 3 Bean 2 2
  32. 32. Spring Spring <<Interface>> BeanIF @Component BeanImpl @Component Consumer @Autowired BeanIFProxy Advice1 Advice2 Advice3 Bean 3 Bean weaving
  33. 33. Native Image https://github.com/spring-projects/spring-framework/issues/22968 Create a Graal native-image support feature for Spring #22968
  34. 34. Feature org.graalvm.nativeimage.hosted.Feature duringSetup beforeAnalysis duringAnalysis 5 afterAnalysis beforeCompilation 3 5 afterCompilation 5 afterHeapLayout Native Image beforeImageWrite Native Image afterImageWrite Native Image cleanup • Native Image 5 3 •
  35. 35. Spring 3 Native Image Spring Feature Spring Feature https://github.com/spring-projects-experimental/spring-graal-native Experimental 4 6 • Reflection Handler • Dynamic Proxy Handler • Initialization Handler • Resource Handler
  36. 36. Spring 1. Native Image Generator 7 3 2. Handler Reflection Handler reflect.json Spring Feature … { "name": "org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator", "allDeclaredConstructors": true, "allDeclaredMethods": true }, …
  37. 37. Dynamic Proxy Handler Spring 1. Native Image Generator 8 3 2. Handler 8 3 proxies.json Spring Feature 3 8 [ … ["org.springframework.web.bind.annotation.RequestMapping"], ["org.springframework.web.bind.annotation.ResponseStatus"], ["org.springframework.web.bind.annotation.RequestBody"], … ] 8 8 3 3
  38. 38. Initialization Handler Spring Native Image Generator 9 3 3 initialization.json Spring Feature 3 { "runtimeInitialization": [ … {"package": "io.netty.handler.codec.http2"}, {"class": "reactor.netty.tcp.TcpClientSecure"}, … ], "buildTimeInitialization": [ {"package": "org.springframework.core.env"}, {"class": "org.springframework.util.unit.DataUnit"}, …
  39. 39. Resource Handler Spring 1. Native Image Generator 4 0 2. spring.factories 3. spring.components resources.json Spring Feature 04 { "resources": [ {"pattern": "db/.*"}, {"pattern": "messages/.*"}, {"pattern": "static/.*"}, {"pattern": "templates/.*"}, … Spring Boot 4
  40. 40. Resource Handler spring.factories spring.factories Spring Boot 4 Auto Configuration 1 # Auto Configuration Import Filters org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=¥ org.springframework.boot.autoconfigure.condition.OnBeanCondition,¥ org.springframework.boot.autoconfigure.condition.OnClassCondition,¥ org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=¥ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,¥ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,¥ • spring.factories Reflection Handler • spring.factories Auto Configuration • Auto Configuration spring.factories Resource Handler
  41. 41. Resource Handler spring.components spring.components Spring-Context-Indexer 4 • spring.components 2 spring.components • spring.components Reflection Handler • spring.components Dynamic Proxy Handler 2 4 Resource Handler com.example.commandlinerunner.CLR=org.springframework.stereotype.Component com.example.commandlinerunner.CommandlinerunnerApplication=org.springframework.stereotype.Component
  42. 42. Spring Feature Spring Boot uber-jar class Spring Feature Native Image 3 4 class Spring Feature
  43. 43. Spring Native Image DEMO
  44. 44. • Native Image 4 • Spring Java • Spring Native Image 5 – Spring Framework 5.3

×