Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you tell if your Java program is running in a GraalVM AOT context?

Tags:

graalvm

I have a little Java program. I build a binary using Graal's native-image (i.e. GraalVM AOT aka SubstrateVM).

My program can be executed either with a Java runtime or from the native-image binary. What's the best way to tell which context I'm running in?

(This might be a bad practice in general but I believe it's inevitable/necessary in certain not-uncommon circumstances.)

like image 728
tksfz Avatar asked May 10 '18 02:05

tksfz


People also ask

Is GraalVM a JVM?

GraalVM is a tool for developers to write and execute Java code. Specifically, GraalVM is a Java Virtual Machine (JVM) and Java Development Kit (JDK) created by Oracle. It is a high-performance runtime that provides improvements in application performance and efficiency.

Is GraalVM part of OpenJDK?

GraalVM is a Java VM and JDK based on HotSpot/OpenJDK, implemented in Java. It supports additional programming languages and execution modes, like ahead-of-time compilation of Java applications for fast startup and low memory footprint. The first production-ready version, GraalVM 19.0, was released in May 2019.

Is GraalVM faster than OpenJDK?

Run Java FasterGraalVM can run in the context of OpenJDK to make Java applications run faster with a new just-in-time compilation technology. GraalVM takes over the compilation of Java bytecode to machine code.


1 Answers

Edit: There is now an API for that. See user7983712's answer.

The way it's done in the GraalVM is by capturing the com.oracle.graalvm.isaot system property: it is set to true while building AOT images. If you combine that with the fact that static initializers run during image generation, you can use

static final boolean IS_AOT = Boolean.getBoolean("com.oracle.graalvm.isaot")

This boolean will remain true when running the native image.

This is also useful to cut-off paths that you don't want in the final output: for example if you have some code that uses a feature that SVM doesn't support (e.g., dynamic class-loading) you can predicate it with !IS_AOT.

like image 81
Gilles D. Avatar answered Sep 19 '22 20:09

Gilles D.