Running Spring Boot Applications
as GraalVM Native Images
Andy Clement, Sébastien Deleuze
October 7–10, 2019
Austin Convention Center
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Safe Harbor Statement
The following is intended to outline the general direction of Pivotal's offerings. It is intended for information
purposes only and may not be incorporated into any contract. Any information regarding pre-release of
Pivotal offerings, future updates or other planned modifications is subject to ongoing evaluation by Pivotal
and is subject to change. This information is provided without warranty or any kind, express or implied, and
is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making
purchasing decisions regarding Pivotal's offerings. These purchasing decisions should only be based on
features currently available. The development, release, and timing of any features or functionality described
for Pivotal's offerings in this presentation remain at the sole discretion of Pivotal. Pivotal has no obligation to
update forward looking information in this presentation.
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Runtime efficiency
3
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What is GraalVM?
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What is GraalVM?
GraalVM is an umbrella project that can be used for various purposes
● Seamless interoperability between languages
● Run JVM code as native images
● Embeddable high-performance JIT compiler
5
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
CE versus EE
GraalVM Community Edition
● Code source and issue tracker on GitHub
● GPL with classpath exception
GraalVM Enterprise Edition
● Faster performance and smaller footprint
● Enhanced security features
● Managed capabilities for native code
● Commercial license
6
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How to generate a native image
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Creating a native-image - DEMO
1. Compile application as normal
2. Run the native-image command, configuring it via any mix of:
● configuration options directly passed to the command
● resource files in your application
● dynamic configuration via a Feature - we’ll get to that…
3. Wait a while - building a native-image is RAM hungry
4. Run resultant self contained executable
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Limitations
https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md
Supported
● Class Initializers
● Lambda Expressions
● Threads
Mostly supported
● Java Native Interface (JNI)
● Unsafe Memory Access
● References
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Limitations
Supported with configuration
● Reflection
● Dynamic Proxies (JDK)
● Resource access
Not supported
● Dynamic Class Loading / Unloading
● InvokeDynamic Bytecode and Method Handles
● Finalizers
● Security Manager
● JVMTI, other native VM interfaces
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Configuring reflection access
Class.forName() / clazz.getDeclaredMethod()
Need to tell native-image what you will reflect on at runtime
● it can prepare the answers and include them in the image
Native-image will infer what it can (e.g. string literals or constants)
Static configuration provided via .json file
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 12
[
{
"name" : "com.example.Something",
"allDeclaredConstructors" : true,
"allPublicMethods" : true,
}, {
"name" : "com.example.SomethingElse",
"fields" : [ { "name" : "value"} ],
"methods" : [
{ "name" : "<init>", "parameterTypes" : ["char[]"] }
]
}
]
Sample reflection configuration
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Configuring resource access
Which files (e.g. .properties files or even .class files) might be accessed at runtime
via Class.getResource() / Class.getResourceAsStream()
Expressed via patterns (wildcards allowed)
Static configuration provided via .json file
{
"resources": [
{"pattern": "application.properties"},
{"pattern": "META-INF/spring.components"},
{"pattern": "org/example/DataSourcePublisher.Registrar.class"}
]
}
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Configuring dynamic proxy usage
Which groups of types does the application create dynamic proxies for at runtime
Specifying these groups at native image build time, the proxies are pre-built then
available at runtime
CGLIB not supported
[
["java.util.Comparator"],
["java.util.List"],
["java.lang.AutoCloseable", "java.util.Comparator"]
]
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Configuring class initialization - DEMO
Ideally initialize classes at native image build time and not runtime
List the classes/packages and when they should be initialized
Gets tricky when a group of types pull in other types unexpectedly
// Command line:
// Eagerly initialize classes in package p at build time
--initialize-at-build-time=p
// Initialize class C1 at run time
--initialize-at-run-time=p.C1
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Coping with limitations via substitutions
Configuration not enough? Try a substitution
Modify a class just before the native image is built
● Adjust any method or field (almost…)
Feel like they should be temporary in our use cases
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Static configuration
Point at the config files directly with native-image options:
-H:ReflectionConfigurationResources=myList.json
Or, place files in well known locations on the classpath:
META-INF/native-image/
META-INF/native-image/<groupId>/<artifactId>
native-image.properties at that location can specify options, merged with any
others discovered/supplied
Args=-H:ReflectionConfigurationResources=${.}/reflection-config.json 
-H:DynamicProxyConfigurationResources=${.}/dynamic-proxies.json
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Native image agent
An agent can assist in generating these files, available with GraalVM
java -agentlib:native-image-agent=config-output-dir=META-INF/native-image Demo
Run application with agent attached to produce .json files
Exercise all code paths (manually or via tests) - producing (merging) many of these
Doesn’t address initialization though
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Dynamic configuration via a Feature
Create a class implementing the Graal Feature interface
● Accessible through the com.oracle.substratevm:graal-hotspot-library library
Class called back to participate in image building processes
● Can register new entries for reflection/resource/proxies/initialization handling
Add @AutomaticFeature to ensure picked up from classpath automatically
Able to change existing (or create new) resources as image constructed
Does not prevent static configuration also being supplied
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Boot applications
as native images
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Timeline
What we talked about in 2018
Dave Syer: “How fast is Spring?”
Some functional registration based Spring apps compiled to native images
Sébastien Deleuze: “Spring, the Kotlin and Functional Way”
Kofu native-image built projects
And now 2019
Spring Boot 2.2
Lots of performance work in general, unrelated to native-images
Regular Spring apps now buildable into native-images
Native-image building is a work in progress
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
spring-graal-native
https://github.com/spring-projects-experimental/spring-graal-native
Provides a Graal @AutomaticFeature
Includes some static configuration via JSON for elements common to all spring apps
Dynamically analyses specific application to add other configuration
Lots of sample projects showing Spring in action with a variety of technologies
Webflux, netty, tomcat, Spring MVC, rabbit, thymeleaf, jpa, grpc
Even PetClinic
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Feature handling for Spring capabilities
Spring typically scans a subset of the classpath
Use spring-content-indexer to avoid the need for runtime classpath scanning
Feature will synthesize a spring.components file if not found
A new resource added to the image!
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
</dependency>
META-INF/spring.components:
com.example.foo.Thing=org.springframework.stereotype.Component
com.example.foo.ThingApplication=org.springframework.stereotype.Component
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Feature handling for Spring capabilities
Spring generates classes at runtime
JDK Proxies are fine, just need to register that those will get created
The feature will register necessary proxies
CGLIB proxies are actually no longer necessary
Boot 2.2 introduced proxyBeanMethods option to avoid CGLIB processing
Boot already uses this internally
@SpringBootApplication(proxyBeanMethods=false)
public class MyApplication {
…
}
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Feature handling for Spring capabilities
Spring uses reflection
That’s ok! GraalVM has great reflective support
Feature knows what kinds of annotation or framework feature usage lead to
reflection needs at runtime
Feature chases down annotations (e.g. @Import)
Spring accesses resources
Not only .properties files but also walks .class bytecode so they must be
accessible as resources
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Feature handling for Spring capabilities
Spring Boot has autoconfiguration enabled by presence of entries on classpath
As image built, complete classpath is known.
Feature analyses configuration checks (e.g. @ConditionalOnClass)
A deeper look...
Auto configuration is listed in spring.factories
With a closed world, @ConditionalOnClass check can be evaluated
If it won’t pass, remove it from the spring.factories list => even faster startup
Resource modification as image constructed
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.example.FooAutoConfiguration,com.example.BarAutoConfiguration
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Examples!
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Examples: CommandLineRunner
CommandLineRunner example on Boot 2.2.0.RC1
Simple app implementing CommandLineRunner interface
See compile script for native-image invocation
Or use maven...
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Examples: webflux-netty
Bigger sample using more Spring facilities including embedded netty
Netty includes their own configuration
https://github.com/netty/netty/tree/4.1/transport/src/main/resources/META-INF/native
-image/io.netty/transport
https://github.com/netty/netty/blob/4.1/common/src/main/resources/META-INF/nativ
e-image/io.netty/common/native-image.properties
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Examples: springmvc-tomcat
Standard web app with embedded tomcat
Tomcat also include their own configuration
https://github.com/apache/tomcat/tree/master/res/tomcat-maven
https://ci.apache.org/projects/tomcat/tomcat9/docs/graal.html
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Examples: webmvc-kotlin
Kotlin web app with Spring MVC and embedded tomcat
Note: WebFlux + Coroutines does not work yet due to oracle/graal#366
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Examples: PetClinic
PetClinic Flavour: webflux/netty
There are benefits just upgrading boot:
Spring PetClinic 2.1
Benchmark Mode Cnt Score Error Units
MainBenchmark.jar avgt 10 3.040 ± 0.096 s/op
MainBenchmark.main avgt 10 2.242 ± 0.033 s/op
Spring PetClinic 2.2
Benchmark Mode Cnt Score Error Units
MainBenchmark.jar avgt 10 2.675 ± 0.045 s/op
MainBenchmark.main avgt 10 2.111 ± 0.028 s/op
Spring PetClinic 2.2 (with -Ds)
Benchmark Mode Cnt Score Error Units
MainBenchmark.jar avgt 10 2.249 ± 0.017 s/op
MainBenchmark.main avgt 10 1.731 ± 0.021 s/op
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Want to try out the feature?
https://github.com/spring-projects-experimental/spring-graal-native
Can I just apply the feature to my project and it will work straightaway?
● mmmmmmmmmmaaaaybbbbeeeeeee
Feel free to raise issues and work with us to explore them
Project is experimental, a work in progress
Feature is not yet the certain option, purely static configuration approach instead?
Or mixed approach?
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
GraalVM native versus OpenJDK JIT
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
GraalVM native allows to start applications faster
3
5

Spring Boot 2.2 with Spring MVC startup time
GraalVM native
OpenJDK JIT
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Beware of truncated point of views
3
6
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Make sure to evaluate all the criteria
3
7

JIT
GraalVM native CE
GraalVM native EE
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
And anticipate the evolution
3
8

Late
2020
projection
JIT
GraalVM native CE
GraalVM native EE
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Benchmarks
● Simple “hello world” HTTP endpoint
● Spring MVC with Tomcat and Spring WebFlux with Netty
● 2 min warmup, 30 seconds measurement
● Dell P5520 i7-7820HQ CPU @ 2.90GHz 32G Ubuntu 19.04
● Heap settings : -Xms8g -Xmx8g
● wrk2 -t4 -c200 -d30s -R20000 --latency <host>
● Log scale due to the high tail latency
3
9
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring MVC: native versus JIT
4
0

With the upcoming new GC and WIP
optimization with Spring workload,
GraalVM native EE could reach in the
future OpenJDK JIT performances.
GraalVM native CE lags behind, but it
enables scale to 0 applications and is
suitable for memory constraint
environment.
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring WebFlux: native versus JIT
4
1

More gap than with Spring MVC, and PGO
not effective for that use case.
Ongoing work with GraalVM team.
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring MVC: OpenJDK JIT versus GraalVM JIT
4
2

5% lower median latency with GraalVM JIT EE.
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring WebFlux: OpenJDK JIT versus GraalVM JIT
4
3

GraalVM JIT EE provides a significant boost on
Reactor based stack, median latency is 10%
lower with OpenJDK JIT.
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
4
4

OpenJDK JIT with low Xmx
Spring MVC: relative CPU and memory profiles
GraalVM native
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Roadmap
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
3rd party libraries
GraalVM native support needs to be sustainable and maintainable, that’s why we do
not want to maintain fragile patches for the whole JVM ecosystem.
The ecosystem of libraries needs to support it natively like Netty or Tomcat do
Spring team plans to collaborate with GraalVM team actively on that (contact open
source project maintainers, help for guidelines, even provide initial PR)
In a nutshell, Spring should be a good GraalVM native citizen for its own codebase,
and should support GraalVM native compatible libraries.
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Collaborating with the Graal team
Graal team has been very responsive on issues and enhancements.
Collaboration is going to increase in upcoming months on:
- Optimizing both GraalVM native and JIT for optimal performance with Spring
workload
- Improving JVM ecosystem compatibility with GraalVM native
- Tooling
- Testing
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring roadmap
Spring Framework 5.3 should be able to configure reflection, proxies, resources, etc.
dynamically with GraalVM native.
Spring Framework test suite running on GraalVM native for supported features.
Not decided yet
● Dedicated project or part of Spring Framework core?
● Level of support on Spring Boot side
● Tooling (Maven, Gradle) for running tests and building native images
Stay Connected.
https://github.com/spring-projects-experimental/spring-graal-native
https://github.com/spring-projects/spring-framework/wiki/GraalVM-native-image-support
#springone@s1p

Running Spring Boot Applications as GraalVM Native Images

  • 1.
    Running Spring BootApplications as GraalVM Native Images Andy Clement, Sébastien Deleuze October 7–10, 2019 Austin Convention Center
  • 2.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Safe Harbor Statement The following is intended to outline the general direction of Pivotal's offerings. It is intended for information purposes only and may not be incorporated into any contract. Any information regarding pre-release of Pivotal offerings, future updates or other planned modifications is subject to ongoing evaluation by Pivotal and is subject to change. This information is provided without warranty or any kind, express or implied, and is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions regarding Pivotal's offerings. These purchasing decisions should only be based on features currently available. The development, release, and timing of any features or functionality described for Pivotal's offerings in this presentation remain at the sole discretion of Pivotal. Pivotal has no obligation to update forward looking information in this presentation.
  • 3.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Runtime efficiency 3
  • 4.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is GraalVM?
  • 5.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is GraalVM? GraalVM is an umbrella project that can be used for various purposes ● Seamless interoperability between languages ● Run JVM code as native images ● Embeddable high-performance JIT compiler 5
  • 6.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ CE versus EE GraalVM Community Edition ● Code source and issue tracker on GitHub ● GPL with classpath exception GraalVM Enterprise Edition ● Faster performance and smaller footprint ● Enhanced security features ● Managed capabilities for native code ● Commercial license 6
  • 7.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How to generate a native image
  • 8.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Creating a native-image - DEMO 1. Compile application as normal 2. Run the native-image command, configuring it via any mix of: ● configuration options directly passed to the command ● resource files in your application ● dynamic configuration via a Feature - we’ll get to that… 3. Wait a while - building a native-image is RAM hungry 4. Run resultant self contained executable
  • 9.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Limitations https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md Supported ● Class Initializers ● Lambda Expressions ● Threads Mostly supported ● Java Native Interface (JNI) ● Unsafe Memory Access ● References
  • 10.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Limitations Supported with configuration ● Reflection ● Dynamic Proxies (JDK) ● Resource access Not supported ● Dynamic Class Loading / Unloading ● InvokeDynamic Bytecode and Method Handles ● Finalizers ● Security Manager ● JVMTI, other native VM interfaces
  • 11.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Configuring reflection access Class.forName() / clazz.getDeclaredMethod() Need to tell native-image what you will reflect on at runtime ● it can prepare the answers and include them in the image Native-image will infer what it can (e.g. string literals or constants) Static configuration provided via .json file
  • 12.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 12 [ { "name" : "com.example.Something", "allDeclaredConstructors" : true, "allPublicMethods" : true, }, { "name" : "com.example.SomethingElse", "fields" : [ { "name" : "value"} ], "methods" : [ { "name" : "<init>", "parameterTypes" : ["char[]"] } ] } ] Sample reflection configuration
  • 13.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Configuring resource access Which files (e.g. .properties files or even .class files) might be accessed at runtime via Class.getResource() / Class.getResourceAsStream() Expressed via patterns (wildcards allowed) Static configuration provided via .json file { "resources": [ {"pattern": "application.properties"}, {"pattern": "META-INF/spring.components"}, {"pattern": "org/example/DataSourcePublisher.Registrar.class"} ] }
  • 14.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Configuring dynamic proxy usage Which groups of types does the application create dynamic proxies for at runtime Specifying these groups at native image build time, the proxies are pre-built then available at runtime CGLIB not supported [ ["java.util.Comparator"], ["java.util.List"], ["java.lang.AutoCloseable", "java.util.Comparator"] ]
  • 15.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Configuring class initialization - DEMO Ideally initialize classes at native image build time and not runtime List the classes/packages and when they should be initialized Gets tricky when a group of types pull in other types unexpectedly // Command line: // Eagerly initialize classes in package p at build time --initialize-at-build-time=p // Initialize class C1 at run time --initialize-at-run-time=p.C1
  • 16.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Coping with limitations via substitutions Configuration not enough? Try a substitution Modify a class just before the native image is built ● Adjust any method or field (almost…) Feel like they should be temporary in our use cases
  • 17.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Static configuration Point at the config files directly with native-image options: -H:ReflectionConfigurationResources=myList.json Or, place files in well known locations on the classpath: META-INF/native-image/ META-INF/native-image/<groupId>/<artifactId> native-image.properties at that location can specify options, merged with any others discovered/supplied Args=-H:ReflectionConfigurationResources=${.}/reflection-config.json -H:DynamicProxyConfigurationResources=${.}/dynamic-proxies.json
  • 18.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Native image agent An agent can assist in generating these files, available with GraalVM java -agentlib:native-image-agent=config-output-dir=META-INF/native-image Demo Run application with agent attached to produce .json files Exercise all code paths (manually or via tests) - producing (merging) many of these Doesn’t address initialization though
  • 19.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dynamic configuration via a Feature Create a class implementing the Graal Feature interface ● Accessible through the com.oracle.substratevm:graal-hotspot-library library Class called back to participate in image building processes ● Can register new entries for reflection/resource/proxies/initialization handling Add @AutomaticFeature to ensure picked up from classpath automatically Able to change existing (or create new) resources as image constructed Does not prevent static configuration also being supplied
  • 20.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Boot applications as native images
  • 21.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Timeline What we talked about in 2018 Dave Syer: “How fast is Spring?” Some functional registration based Spring apps compiled to native images Sébastien Deleuze: “Spring, the Kotlin and Functional Way” Kofu native-image built projects And now 2019 Spring Boot 2.2 Lots of performance work in general, unrelated to native-images Regular Spring apps now buildable into native-images Native-image building is a work in progress
  • 22.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ spring-graal-native https://github.com/spring-projects-experimental/spring-graal-native Provides a Graal @AutomaticFeature Includes some static configuration via JSON for elements common to all spring apps Dynamically analyses specific application to add other configuration Lots of sample projects showing Spring in action with a variety of technologies Webflux, netty, tomcat, Spring MVC, rabbit, thymeleaf, jpa, grpc Even PetClinic
  • 23.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Feature handling for Spring capabilities Spring typically scans a subset of the classpath Use spring-content-indexer to avoid the need for runtime classpath scanning Feature will synthesize a spring.components file if not found A new resource added to the image! <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> </dependency> META-INF/spring.components: com.example.foo.Thing=org.springframework.stereotype.Component com.example.foo.ThingApplication=org.springframework.stereotype.Component
  • 24.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Feature handling for Spring capabilities Spring generates classes at runtime JDK Proxies are fine, just need to register that those will get created The feature will register necessary proxies CGLIB proxies are actually no longer necessary Boot 2.2 introduced proxyBeanMethods option to avoid CGLIB processing Boot already uses this internally @SpringBootApplication(proxyBeanMethods=false) public class MyApplication { … }
  • 25.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Feature handling for Spring capabilities Spring uses reflection That’s ok! GraalVM has great reflective support Feature knows what kinds of annotation or framework feature usage lead to reflection needs at runtime Feature chases down annotations (e.g. @Import) Spring accesses resources Not only .properties files but also walks .class bytecode so they must be accessible as resources
  • 26.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Feature handling for Spring capabilities Spring Boot has autoconfiguration enabled by presence of entries on classpath As image built, complete classpath is known. Feature analyses configuration checks (e.g. @ConditionalOnClass) A deeper look... Auto configuration is listed in spring.factories With a closed world, @ConditionalOnClass check can be evaluated If it won’t pass, remove it from the spring.factories list => even faster startup Resource modification as image constructed org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.example.FooAutoConfiguration,com.example.BarAutoConfiguration
  • 27.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Examples!
  • 28.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Examples: CommandLineRunner CommandLineRunner example on Boot 2.2.0.RC1 Simple app implementing CommandLineRunner interface See compile script for native-image invocation Or use maven...
  • 29.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Examples: webflux-netty Bigger sample using more Spring facilities including embedded netty Netty includes their own configuration https://github.com/netty/netty/tree/4.1/transport/src/main/resources/META-INF/native -image/io.netty/transport https://github.com/netty/netty/blob/4.1/common/src/main/resources/META-INF/nativ e-image/io.netty/common/native-image.properties
  • 30.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Examples: springmvc-tomcat Standard web app with embedded tomcat Tomcat also include their own configuration https://github.com/apache/tomcat/tree/master/res/tomcat-maven https://ci.apache.org/projects/tomcat/tomcat9/docs/graal.html
  • 31.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Examples: webmvc-kotlin Kotlin web app with Spring MVC and embedded tomcat Note: WebFlux + Coroutines does not work yet due to oracle/graal#366
  • 32.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Examples: PetClinic PetClinic Flavour: webflux/netty There are benefits just upgrading boot: Spring PetClinic 2.1 Benchmark Mode Cnt Score Error Units MainBenchmark.jar avgt 10 3.040 ± 0.096 s/op MainBenchmark.main avgt 10 2.242 ± 0.033 s/op Spring PetClinic 2.2 Benchmark Mode Cnt Score Error Units MainBenchmark.jar avgt 10 2.675 ± 0.045 s/op MainBenchmark.main avgt 10 2.111 ± 0.028 s/op Spring PetClinic 2.2 (with -Ds) Benchmark Mode Cnt Score Error Units MainBenchmark.jar avgt 10 2.249 ± 0.017 s/op MainBenchmark.main avgt 10 1.731 ± 0.021 s/op
  • 33.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Want to try out the feature? https://github.com/spring-projects-experimental/spring-graal-native Can I just apply the feature to my project and it will work straightaway? ● mmmmmmmmmmaaaaybbbbeeeeeee Feel free to raise issues and work with us to explore them Project is experimental, a work in progress Feature is not yet the certain option, purely static configuration approach instead? Or mixed approach?
  • 34.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ GraalVM native versus OpenJDK JIT
  • 35.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ GraalVM native allows to start applications faster 3 5  Spring Boot 2.2 with Spring MVC startup time GraalVM native OpenJDK JIT
  • 36.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Beware of truncated point of views 3 6
  • 37.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Make sure to evaluate all the criteria 3 7  JIT GraalVM native CE GraalVM native EE
  • 38.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ And anticipate the evolution 3 8  Late 2020 projection JIT GraalVM native CE GraalVM native EE
  • 39.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Benchmarks ● Simple “hello world” HTTP endpoint ● Spring MVC with Tomcat and Spring WebFlux with Netty ● 2 min warmup, 30 seconds measurement ● Dell P5520 i7-7820HQ CPU @ 2.90GHz 32G Ubuntu 19.04 ● Heap settings : -Xms8g -Xmx8g ● wrk2 -t4 -c200 -d30s -R20000 --latency <host> ● Log scale due to the high tail latency 3 9
  • 40.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring MVC: native versus JIT 4 0  With the upcoming new GC and WIP optimization with Spring workload, GraalVM native EE could reach in the future OpenJDK JIT performances. GraalVM native CE lags behind, but it enables scale to 0 applications and is suitable for memory constraint environment.
  • 41.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring WebFlux: native versus JIT 4 1  More gap than with Spring MVC, and PGO not effective for that use case. Ongoing work with GraalVM team.
  • 42.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring MVC: OpenJDK JIT versus GraalVM JIT 4 2  5% lower median latency with GraalVM JIT EE.
  • 43.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring WebFlux: OpenJDK JIT versus GraalVM JIT 4 3  GraalVM JIT EE provides a significant boost on Reactor based stack, median latency is 10% lower with OpenJDK JIT.
  • 44.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4 4  OpenJDK JIT with low Xmx Spring MVC: relative CPU and memory profiles GraalVM native
  • 45.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Roadmap
  • 46.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3rd party libraries GraalVM native support needs to be sustainable and maintainable, that’s why we do not want to maintain fragile patches for the whole JVM ecosystem. The ecosystem of libraries needs to support it natively like Netty or Tomcat do Spring team plans to collaborate with GraalVM team actively on that (contact open source project maintainers, help for guidelines, even provide initial PR) In a nutshell, Spring should be a good GraalVM native citizen for its own codebase, and should support GraalVM native compatible libraries.
  • 47.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Collaborating with the Graal team Graal team has been very responsive on issues and enhancements. Collaboration is going to increase in upcoming months on: - Optimizing both GraalVM native and JIT for optimal performance with Spring workload - Improving JVM ecosystem compatibility with GraalVM native - Tooling - Testing
  • 48.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring roadmap Spring Framework 5.3 should be able to configure reflection, proxies, resources, etc. dynamically with GraalVM native. Spring Framework test suite running on GraalVM native for supported features. Not decided yet ● Dedicated project or part of Spring Framework core? ● Level of support on Spring Boot side ● Tooling (Maven, Gradle) for running tests and building native images
  • 49.