Profile Guided Optimization
After we restored tracing capability in Tracing in native image - Datadog
Let's take a look at Profile guided optimization (PGO).
this technique allows you to generate a profile at runtime of optimizations that the JVM applies and bring them to build time for your next native image build.
You will need GraalVM Enterprise Edition for PGO builds.
Setup
the first thing we need is to build a native image capable of collecting runtime data
As in previous articles, we use the native image maven plugin to pass the arguement <arg>--pgo-instrument</arg>
:
Build with
Check the logs for Active PGO instrumentation:
![pgo_active.png pgo_active.png](img/pgo_active.png)
Notably
I ran out of memory trying to build a PGO instrumented native image:
The process took longer from the usual 8 minutes I was getting with -Ob quick build mode:
![pgo_build_success.png pgo_build_success.png](img/pgo_build_success.png)
The binary generated was larger:
![pgo_vs_nonpgo.png pgo_vs_nonpgo.png](img/pgo_vs_nonpgo.png)
Collect
During this phase, you would ideally use workloads similar to your production environment.
I will just send hundreds of queries through Postman's Collection runs
this generates a profile default.iprof
that we will use in the next step:
![default_iprof.png default_iprof.png](img/default_iprof.png)
Pass to Native Image the profile
Pass the --pgo
argument passed to the native image build tool:
Rebuild:
Check for Active PGO:
![pgo_active_build.png pgo_active_build.png](img/pgo_active_build.png)