Example programs and measurements of the HappyCoders.eu articles on the AOT cache of Project Leyden:
- "Ahead-of-Time Class Loading & Linking" – JEP 483, 514, 515 and 516 (English, German)
- "Ahead-of-Time Code Compilation" – JEP 544, Java 28 (not yet published)
- "Ahead-of-Time Method Profiling" – JEP 515, Java 25 (in preparation)
All three programs are compact source files with an instance main() method, so they need Java 25 or newer. They are in the unnamed package, since a compact source file cannot declare a package.
AotTest– prints the current date and time; the example of the class-loading article. It starts within a few dozen milliseconds, so it shows what the AOT cache saves on a tiny program.AotLambda– calls a lambda for each element of an array, 100,000 times per round, and prints the duration of each round (argument: number of rounds, default 10). It shows on a small scale why C2 inlines less with the method profiles of JEP 515: when the JVM writes the training data into the cache, it removes the lambda classes of the training run from the type profiles and sets the counter of the call site to 0. C2 then considers the call site cold and does not inline the lambda. The program waits half a second after initializing the class with the hot loop, so that the JVM compiles it from the training data before it runs. On Java 28, the interpreter fills the profile again without the wait before C2 compiles the loop, and the effect does not show. On Java 25, it shows without the wait too: C2 compilesSteps.step()right after its first calls, when the interpreter has counted only a few thousand calls at the call site (lambda-pause.txt).AotCompile– compiles 50 generated source files with the Java compiler, in memory, and prints the duration of each round. Its only argument is the number of rounds (default 10). It loads, links and compiles enough code for the effect of AOT code to show in the first round and in the warmup that follows – the same idea as thejavacbenchmark in JEP 544.
Ahead-of-time code compilation needs Java 28 (early-access builds: jdk.java.net/28). In src/main/java, compile the program and pack it into a JAR file – the JVM refuses to create an AOT cache when the class path contains a non-empty directory:
javac -d classes AotCompile.java
jar cf AotCompile.jar -C classes .
Create the AOT cache in a training run of 20 rounds:
java -XX:AOTCacheOutput=AotCompile.aot -cp AotCompile.jar AotCompile 20
Then compare a start without and with the cache:
java -cp AotCompile.jar AotCompile 20
java -XX:AOTCache=AotCompile.aot -cp AotCompile.jar AotCompile 20
To compare with a cache that contains loaded classes and profiles but no AOT code, create a second cache with the diagnostic option -XX:-AOTCodeCaching:
java -XX:+UnlockDiagnosticVMOptions -XX:-AOTCodeCaching \
-XX:AOTCacheOutput=AotCompile-without-code.aot -cp AotCompile.jar AotCompile 20
-XX:+PrintCompilation shows which methods are loaded from the cache: AOT code carries the flag A, AOT code with class initialization barriers additionally P.
scripts/run-code-compilation-measurements.sh <path-to-jdk-28> <machine-id> reproduces all measurements of the code-compilation article in about 30 minutes and writes them to results/code-compilation/<machine-id>/:
environment.txt– date, CPU and JDK buildcache-creation.txt– duration of training run plus cache creation, and the sizes of all cachesaottest.csv,aottest-summary.txt– 50 starts ofAotTesteach without a cache, with a cache without AOT code, and with a cache with AOT codecompile-100.csv,compile-100-summary.txt– 30 runs ofAotCompilewith 100 rounds each, same three variants: the first rounds show the head start of the AOT code, the later ones where the variants meetcompile-training.csv,compile-training-summary.txt– caches from training runs of 5, 20 and 60 rounds, and a cache whose AOT code has no invocation counters (-XX:-UseAOTCodeCountersduring cache creation), 8 runs with 60 rounds eachprofiles.csv,profiles-summary.txt– where the gap to the run without a cache after 20 rounds comes from, 15 runs with 100 rounds each: without a cache, with the cache without AOT code, with the same cache but-XX:-AOTReplayTraining(the JVM ignores the method profiles), and with a cache created with-XX:-AOTClassLinking(loaded classes only, no linking, no profiles, no code)compilation-<variant>.txt,compilation-<variant>.log.gz–-XX:+PrintCompilationlogs of 40 rounds and their evaluation byscripts/analyze-compilation.py;compilation-nocounters.*is a run of 60 rounds with the cache whose AOT code has no countersaot-code-entries.txt– how much AOT code the caches from training runs of 5, 20 and 60 rounds contain (-Xlog:aot)gc-mismatch.txt– what happens when the cache was created with G1 and the application starts with the Serial, Parallel or Z garbage collector
scripts/run-code-compilation-training-repeat.sh <path-to-jdk-28> <machine-id> <build-name> repeats the training-length measurement with three independently created caches per training length, in about 10 minutes. The article's table on the training length comes from the run on 28-ea+20:
compile-training-repeat-<build>.csv,compile-training-repeat-summary-<build>.txt– 9 caches (5, 20 and 60 training rounds, three each), 8 runs with 60 rounds per cachetraining-repeat-caches-<build>.txt,training-repeat-environment-<build>.txt– cache sizes, JDK build and loadtraining-repeat-aot-code-ea20.txt– how much AOT code each of the nine caches on 28-ea+20 contains (-Xlog:aot)
scripts/run-method-profiling-measurements.sh <path-to-jdk-28> <path-to-jdk-25> <machine-id> measures what the method profiles in the AOT cache do to the warmup and the peak performance, in about 30 minutes, and writes the results to results/method-profiling/<machine-id>/. All Java 28 caches are created with -XX:-AOTCodeCaching, so they contain classes and profiles but no AOT code – like a Java 25 cache. In the variants, noreplay is the cache from 20 training iterations started with -XX:+UnlockDiagnosticVMOptions -XX:-AOTReplayTraining: the JVM uses the loaded and linked classes, but not the profiles.
environment.txt,cache-creation.txt– as above; caches from training runs of 5, 20, 60 and 100 iterationstraining-length.csv,training-length-summary.txt– Java 28, 15 runs of 100 iterations: without a cache,noreplay, and the four training lengthslong-run.csv,long-run-summary.txt– Java 28, 10 runs of 200 iterations: without a cache,noreplay, and the caches from 20, 60 and 100 training iterations – whether the variants meet once the iterations have passed the training runjava25.csv,java25-summary.txt– Java 25, 15 runs of 100 iterations: without a cache,noreplay, and the cache from 20 training iterationshello-java28.csv,hello-java25.csvand their summaries – the example program of JEP 515 (scripts/HelloStreamWarmup.java), 30 starts each without a cache, with a cache but without profiles, and with profilescompilations-<java>-<variant>.txt,compilation-<java>-<variant>.log.gz– one run of 100 iterations with-XX:+PrintCompilation -XX:+CITime: the compilations per iteration and level (scripts/compilations-per-iteration.py) and the accumulated compiler timeslogcompilation-java28-<variant>.txt– one run of 100 iterations with-XX:+LogCompilation, evaluated byscripts/analyze-logcompilation.py: the level each method ends with, the compile queue per level and time window – including the compilations the JVM queues without an invocation (reasonmust_be_compiled) –, and for the C2 compilations the trap history in the profiles, the speculations C2 drops because of it, the type profiles of the virtual call sites and the inlining decisions; the logs themselves are about 100 MB each and not part of the resultsjfr-samples-java28.txt– JFR execution samples from second 4 to the end of 5 runs each, with and without the profiles, evaluated byscripts/analyze-jfr-samples.py: where the time goes, as shares per leaf method, and the bytes allocated by the main threadinlining-comparison-java28.txt– the C2 inlining decisions of one run with and one without the profiles, compared call site by call site (scripts/compare-inlining.py <log-a> <log-b>): how many call sites C2 inlines only in one of the two runs, by the reason it gives in the other, and how many of them lead through lambda, method handle or string concatenation classes
Ten follow-up scripts use the programs and caches of the main script and add to the same directory:
scripts/run-method-profiling-followup.sh <path-to-jdk-28> <path-to-jdk-25> <machine-id>(about 10 minutes):gc-heap-sizing.txt– heap sizing per variant from-Xlog:gc(scripts/analyze-gc-log.py);fixed-heap.csvand its summary – Java 28 with-Xms2g -Xmx2g -Xmn512m, 15 runs of 100 iterations, to rule out the heap sizing as the cause of the lasting gap;logcompilation-java25-<variant>.txt– the compile queue on Java 25scripts/run-method-profiling-eager.sh <path-to-jdk-28> <path-to-jdk-25> <machine-id>(about 11 minutes): the diagnostic optionAOTCompileEagerly, which Java 28 switches on whenever the AOT code cache is in use – also for a cache without AOT code – and which makes the JVM compile the methods from the training run as soon as their classes are initialized.eager-flag.txtshows the value in both versions,eager-java28.csvswitches it off in Java 28,eager-java25.csvon in Java 25 (15 runs of 100 iterations each), andlogcompilation-java28-training20-noeager.txtandlogcompilation-java25-training20-eager.txtshow the compile queuesscripts/run-method-profiling-counters.sh <path-to-jdk-28> <machine-id>(macOS with Xcode, no root rights needed, about 10 minutes): CPU performance counters of the main thread from Instruments (template "CPU Counters", mode "CPU Bottlenecks"), 4 runs each without a cache, without profiles and with profiles.cpu-counters.txt– cycles, instructions and IPC per iteration in the last 2 seconds (scripts/analyze-pmc-samples.py);cpu-bottlenecks.txt– the shares of useful work and of the instruction-processing, instruction-delivery and discarded bottlenecks (scripts/analyze-cpu-bottlenecks.py)scripts/run-method-profiling-lambda.sh <path-to-jdk-28> <path-to-jdk-25> <machine-id>(about 4 minutes):AotLambdawith and without the profiles.lambda-purged.txt– the profile rows the JVM removes while writing the training data (-Xlog:aot+training=debug);lambda-java28.csv,lambda-java25.csvand their summaries – 10 runs of 30 rounds each without a cache, without profiles and with profiles;lambda-inlining.txt– C2’s decisions on the lambda call (-XX:+PrintInlining);aotcompile-purged.txt– the same removal forAotCompile, summarized byscripts/summarize-purged.py.scripts/run-method-profiling-traps.sh <path-to-jdk-28> <machine-id>(about 8 minutes): a cache from a training run with-XX:PerMethodTrapLimit=0 -XX:PerMethodSpecTrapLimit=0, whose profiles carry hardly any trap history –trap-history.csvand its summary (15 runs of 100 iterations) andlogcompilation-java28-training20-notraps.txtscripts/run-method-profiling-iterative.sh <path-to-jdk-28> <machine-id>(JDK 28-ea+23 or newer, uses the caches of the lambda script too, about 15 minutes): iterative training (JDK-8353598), where a training run loads an existing cache with-XX:AOTCacheand writes a new one with-XX:AOTCacheOutput.iterative-purged.txt– the profile rows removed in the first and in the second training run, forAotLambdaand forAotCompile(20 iterations, then 20 more on top, and 40 in one run for comparison);iterative-lambda.csv,iterative-compile.csvand their summaries – the iteratively trained caches against the single ones (10 runs of 30 and 15 runs of 100 iterations);iterative-inlining.txt– the inlining comparison for the iteratively trained cachescripts/run-method-profiling-builds.sh <path-to-jdk-a> <name-a> <path-to-jdk-b> <name-b> <machine-id>(about 15 minutes): two Java 28 builds measured alternately in one series, 10 runs of 200 iterations each without a cache and with the caches from 20 and 100 training iterations –builds.csv,builds-summary.txt,builds-versions.txt; it shows whether a difference between two evenings comes from the builds or from the seriesscripts/run-method-profiling-exclusion.sh <path-to-jdk-28> <machine-id>(JDK 28-ea+23 or newer, a few seconds):lambda-exclusion.txt– why the training run drops the lambda class ofAotLambda, also when it loads the class from an existing cache:-Xlog:aot=debugshows it as “Unreferenced hidden class” right before the profile rows are purged, and the assembly phase generates it anewscripts/run-method-profiling-aot-code.sh <path-to-jdk-28> <machine-id>(about 4 minutes):AotLambdawith a cache that contains AOT code –lambda-aot-code.csvand its summary (10 runs of 30 iterations without a cache, without profiles, with profiles but without AOT code, and with profiles and AOT code) andlambda-aot-code.txt(the purged rows, the compilations ofStepsand C2’s decision on the lambda call)scripts/run-method-profiling-long-run-java25.sh <path-to-jdk-25> <machine-id>(about 5 minutes): the 200-iteration series oflong-run.csvon Java 25, without a cache and with a cache from 100 training iterations –long-run-java25.csvand its summary (10 runs each); it shows whether Java 25, which delays lukewarm methods by a fixed factor instead of their training count, falls behind at the same point as Java 28scripts/run-method-profiling-pause.sh <path-to-jdk-28> <path-to-jdk-25> <machine-id>(about 2 minutes):lambda-pause.txt–AotLambdawith and without the pause before the first call ofSteps.run(), on both versions: 3 runs of 10 iterations each without a cache, without profiles and with profiles (mean of iterations 6 to 10), and one run each with-XX:+PrintCompilation -XX:+PrintInliningfor the order of the compilations ofStepsand C2's decision on the lambda call
scripts/measure.py starts the variants alternately, measures the wall time of each java process and collects the duration of each round. The summaries give the first quartile, the median and the third quartile.