Skip to content

Commit

Permalink
Fix tests and benchmarks in GraalVM Native Images
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 2, 2024
1 parent a2ca93f commit 0332cc8
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 33 deletions.
36 changes: 33 additions & 3 deletions .github/workflows/kt-bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,34 @@ jobs:
name: ${{ matrix.target }}-benchmarks
path: kt/aoc2024-exe/build/reports/benchmarks

graalvm:
needs: [ assemble, get-inputs ]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: graalvm/setup-graalvm@v1
with:
java-version: 21
github-token: ${{ secrets.GITHUB_TOKEN }}
- uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: true
- run: ./gradlew --no-configuration-cache -Pagent nativeBenchmarkRun
working-directory: kt
env:
AOC2024_DATADIR: ${{ github.workspace }}/inputs
- uses: actions/upload-artifact@v4
with:
name: graalvm-benchmarks
path: kt/graalvm/build/reports/benchmarks

docs:
needs: [ jmh-visualizer, build ]
needs: [ jmh-visualizer, build, graalvm ]
runs-on: ubuntu-latest

steps:
Expand All @@ -97,13 +123,17 @@ jobs:
with:
name: linuxX64-benchmarks
path: benchmarks
- uses: actions/download-artifact@v4
with:
name: graalvm-benchmarks
path: benchmarks
- run: rm -rf jmh-visualizer
- run: unzip -d jmh-visualizer jmh-visualizer.zip
- name: Create provided.js
run: |
shopt -s failglob
shopt -s failglob globstar
names=() jsonargs=()
for file in benchmarks/main/*/*.json; do
for file in benchmarks/**/*.json; do
name=${file##*/}
name=${name%.*}
name=${name%Bench}
Expand Down
31 changes: 19 additions & 12 deletions .github/workflows/kt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,13 @@ jobs:
with:
distribution: temurin
java-version: 21
- uses: graalvm/setup-graalvm@v1
with:
java-version: 21
github-token: ${{ secrets.GITHUB_TOKEN }}
set-java-home: false
components: native-image
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew build nativeCompile distZip
- run: ./gradlew build distZip
working-directory: kt
- uses: actions/upload-artifact@v4
with:
name: aoc2024-exe
path: kt/aoc2024-exe/build/distributions/*.zip
- uses: actions/upload-artifact@v4
with:
name: aoc2024-native
path: kt/graalvm/build/native/nativeCompile/*
- uses: actions/upload-artifact@v4
with:
name: aoc2024-kexe
Expand All @@ -57,6 +47,23 @@ jobs:
name: aoc2024-js
path: kt/build/js/packages/aoc2024-aoc2024-exe/kotlin/*

graalvm:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: graalvm/setup-graalvm@v1
with:
java-version: 21
github-token: ${{ secrets.GITHUB_TOKEN }}
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew --no-configuration-cache nativeTest nativeCompile
working-directory: kt
- uses: actions/upload-artifact@v4
with:
name: aoc2024-native
path: kt/graalvm/build/native/nativeCompile/*

run-jvm:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest
Expand All @@ -79,7 +86,7 @@ jobs:
AOC2024_DATADIR: inputs

run-graalvm:
needs: [ get-inputs, build ]
needs: [ get-inputs, graalvm ]
runs-on: ubuntu-latest

steps:
Expand Down
22 changes: 22 additions & 0 deletions kt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ Run all checks, including [Detekt](https://detekt.github.io/) static code analys
```sh
./gradlew check
```

## [GraalVM](https://www.graalvm.org/)

Run the test suite as a GraalVM Native Image:

```sh
export GRAALVM_HOME=...
./gradlew --no-configuration-cache :graalvm:nativeTest
```

Run [JMH](https://openjdk.java.net/projects/code-tools/jmh/) benchmarks as a GraalVM Native Image:

```sh
export GRAALVM_HOME=...
./gradlew --no-configuration-cache -Pagent :graalvm:nativeBenchmarkRun
```

Print solutions for the inputs provided in local data files as a GraalVM Native Image:

```sh
./gradlew --no-configuration-cache :graalvm:nativeRun
```
72 changes: 55 additions & 17 deletions kt/graalvm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import org.graalvm.buildtools.gradle.internal.agent.AgentConfigurationFactory
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.function.Predicate

plugins {
application
alias(libs.plugins.native.image)
Expand All @@ -7,7 +12,53 @@ application {
mainClass.set("com.github.ephemient.aoc2024.exe.Main")
}

val benchmarkDir = layout.buildDirectory
.dir(
"reports/benchmarks/main/" +
LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME).replace(':', '.')
)

val benchmark by sourceSets.creating
val benchmarkRun by tasks.registering(JavaExec::class) {
System.getenv("GRAALVM_HOME")?.ifEmpty { null }?.let { setExecutable("$it/bin/java") }
mainClass = "org.openjdk.jmh.Main"
classpath(benchmark.output, benchmark.runtimeClasspath)
args("-f", 0, "-wi", 1, "-w", "0s", "-r", "0s", "-bm", "avgt", "-tu", "us", "-i", 1)
args("-rf", "json", "-rff", "/dev/null")
outputs.dir(AgentConfigurationFactory.getAgentOutputDirectoryForTask(layout, name))
}
val syncBenchmarkRunMetadata by tasks.registering(Sync::class) {
into(layout.buildDirectory.dir("generated/resources/benchmark"))
from(benchmarkRun) {
into("META-INF/native-image/com.github.ephemient.aoc2024/benchmark")
}
}
graalvmNative {
binaries {
getByName("main") {
imageName = "aoc2024-native"
}
create("benchmark") {
imageName = "aoc2024-native-benchmark"
mainClass = "org.openjdk.jmh.Main"
classpath(syncBenchmarkRunMetadata, benchmark.output, benchmark.runtimeClasspath)
runtimeArgs("-f", 0, "-wi", 1, "-w", "1s", "-r", "1s", "-bm", "avgt", "-tu", "us")
val benchmarkFile = benchmarkDir.get().file("graalvmBench.json")
runtimeArgs("-rf", "json", "-rff", benchmarkFile.asFile)
findProperty("benchmarkInclude")?.let { runtimeArgs("-e", it) }
findProperty("benchmarkExclude")?.let { runtimeArgs(it) }
}
}
agent {
tasksToInstrumentPredicate.set(Predicate { it.name == "benchmarkRun" })
}
}
tasks.named("nativeBenchmarkRun") {
val benchmarkDir = benchmarkDir.get()
outputs.file(benchmarkDir.file("graalvmBench.json"))
doFirst { benchmarkDir.asFile.mkdirs() }
}

val externalTestClasses = configurations.dependencyScope("externalTestClasses")
val externalTestClasspath = configurations.resolvable("externalTestClasspath") {
extendsFrom(externalTestClasses.get())
Expand All @@ -22,6 +73,10 @@ val externalTestClasspath = configurations.resolvable("externalTestClasspath") {
configurations.testImplementation {
extendsFrom(externalTestClasses.get())
}
tasks.named<Test>("test") {
testClassesDirs = files(testClassesDirs, externalTestClasspath.get())
useJUnitPlatform()
}

dependencies {
implementation(projects.aoc2024Exe)
Expand All @@ -37,20 +92,3 @@ dependencies {
}
}
}

graalvmNative {
binaries {
getByName("main") {
imageName = "aoc2024-native"
}
create("benchmark") {
mainClass = "org.openjdk.jmh.Main"
classpath(benchmark.runtimeClasspath)
}
}
}

tasks.withType<Test>().configureEach {
testClassesDirs = files(testClassesDirs, externalTestClasspath.get())
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Args = --initialize-at-build-time=org.openjdk.jmh.infra,org.openjdk.jmh.util.Utils,org.openjdk.jmh.runner.InfraControl,org.openjdk.jmh.runner.InfraControlL0,org.openjdk.jmh.runner.InfraControlL1,org.openjdk.jmh.runner.InfraControlL2,org.openjdk.jmh.runner.InfraControlL3,org.openjdk.jmh.runner.InfraControlL4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Args = --initialize-at-build-time=kotlin.annotation.AnnotationRetention,kotlin.annotation.AnnotationTarget
2 changes: 1 addition & 1 deletion kt/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
org.gradle.caching=true
#org.gradle.configuration-cache=true
org.gradle.configuration-cache=true
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true

0 comments on commit 0332cc8

Please sign in to comment.