Skip to content

Graal VM native‐image

Alexander Weigl edited this page Sep 23, 2023 · 1 revision

Hej,

Oracle is working on a new VM:

GraalVM is a high-performance, embeddable, polyglot Virtual Machine for running applications written in >JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages such > as C and C++.

The best thing on Graal is support for compiling Bytecode down to native code!

I have done this with KeY and want to share first impressions. Some, rather small, additions/changes to KeY were required--- mostly handling of resources names.

tl;dr; The generated native image is always faster in CPU time (94%, 69%, 46%) and mostly slower in real time (wall clock) (84%,-14%, -76%).

Details:

Performance Measurements:

(Computer: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz)

Runtime: IntegerUtils.java (as delivered in key.core.example)

/usr/bin/time java -jar ../key.core/build/libs/key-2.7-exe.jar > /dev/null 11.03user 0.31system 0:02.96elapsed 383%CPU (0avgtext+0avgdata 589940maxresident)k 0inputs+40outputs (0major+136647minor)pagefaults 0swaps

/usr/bin/time ../key-2.7-exe > /dev/null 0.64user 0.08system 0:00.84elapsed 85%CPU (0avgtext+0avgdata 313716maxresident)k 0inputs+56outputs (0major+71795minor)pagefaults 0swaps

CPU time = 0.64 to 11.03 seconds Real time = 0.84 to 2.96 seconds

Runtime: BinarySearch

[weigl@i57pc4 06-BinarySearch]$ /usr/bin/time ~/work/key/key/key-2.7-exe . > /dev/null 6.08user 0.11system 0:06.29elapsed 98%CPU (0avgtext+0avgdata 375464maxresident)k 0inputs+56outputs (0major+91793minor)pagefaults 0swaps

[weigl@i57pc4 06-BinarySearch]$ /usr/bin/time java -jar ~/work/key/key/key.core/build/libs/key-2.7-exe.jar . >/dev/null 19.73user 0.40system 0:05.47elapsed 367%CPU (0avgtext+0avgdata 687480maxresident)k 0inputs+40outputs (0major+161857minor)pagefaults 0swaps

CPU time = 6.08 to 19.73 seconds Real time = 6.29 to 5.47 seconds

Runtime: SITA

[weigl@i57pc4 10-SITA]$ /usr/bin/time ~/work/key/key/key-2.7-exe . > /dev/null 12.56user 0.12system 0:12.96elapsed 97%CPU (0avgtext+0avgdata 397748maxresident)k 0inputs+56outputs (0major+97445minor)pagefaults 0swaps

[weigl@i57pc4 10-SITA]$ /usr/bin/time java -jar ~/work/key/key/key.core/build/libs/key-2.7-exe.jar . >/dev/null 23.32user 0.40system 0:07.33elapsed 323%CPU (0avgtext+0avgdata 757060maxresident)k 0inputs+40outputs (0major+179519minor)pagefaults 0swaps

CPU time = 12.54 to 23.32 seconds Real time = 12.96 to 7.33 seconds

Steps to setup Graal

  1. Either download Graal VM directly: https://github.com/graalvm/graalvm-ce-builds/releases or use SDK manager like https://sdkman.io/.

  2. Install the support for native-image:

    $ bin/gu install native-image ^^^^^^ -> is part of the Graal package

  3. Go into your KeY folder:

    a. Checkout the branch with fixes for graal:

    $ git checkout weigl/graaltest

    b. Compile key.core to an executable Jar file.

    $ gradle :key.core:shadowJar --parallel

  4. Compile the created Jar file to an executable:

    $ native-image --no-fallback --verbose -jar key.core/build/libs/key-2.7-exe.jar ^^^^^^^^^^^ should be in the bin/ folder of the graal installation

  5. You now should have an executable key-2.7-exe

    -rwxrwxr-x. 1 weigl weigl 36M Nov 26 15:08 key-2.7-exe

This executable is statically linked:

i57pc4 ~/w/k/key % ldd key-2.7-exe linux-vdso.so.1 (0x00007ffc685ad000) libm.so.6 => /lib64/libm.so.6 (0x00007f226c577000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f226c555000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f226c54e000) libz.so.1 => /lib64/libz.so.1 (0x00007f226c534000) librt.so.1 => /lib64/librt.so.1 (0x00007f226c529000) libc.so.6 => /lib64/libc.so.6 (0x00007f226c360000) /lib64/ld-linux-x86-64.so.2 (0x00007f226c6db000)

Hint: If the native-image complains that certain classes are undefined, you need to add them to the META-INF/native-image configurations. The easiest way is to let KeY (Java version) run on the problem and let an agent profile it:

java -agentlib:native-image-agent=config-output-dir=/key/key.core/src/main/resources/META-INF/native-image/ -jar /key/key.core/build/libs/key-2.7-exe.jar .

Regards,

Alexander

-- Karlsruhe Institute of Technology (KIT) Institute for Theoretical Informatics Application-oriented Formal Verification

Alexander Weigl Researcher/PhD Student

Am Fasanengarten 5, Building 50.34, Room 225 D-76131 Karlsruhe, GERMANY

Phone: +49 721 608-44324 Fax: +49 721 608-43088 E-mail: weigl@kit.edu http://formal.iti.kit.edu/~weigl/

KIT – The Research University in the Helmholtz Association