Skip to content

Commit

Permalink
[GR-57507] Pass some options directly and only to isolate creation.
Browse files Browse the repository at this point in the history
PullRequest: graal/18640
  • Loading branch information
gilles-duboscq committed Aug 27, 2024
2 parents 3d2c30b + b77b195 commit 8039fbf
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@

import java.io.File;
import java.io.PrintStream;
import java.io.Serial;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -78,7 +79,7 @@ private Arguments() {
private static final Map<String, String> MAPPED_XX_OPTIONS = Map.of(
"TieredCompilation", "engine.MultiTier");

public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args) {
public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args, BitSet ignoredArgs) {
Pointer p = (Pointer) args.getOptions();
int count = args.getNOptions();
String classpath = null;
Expand All @@ -88,8 +89,7 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args)
ArgumentsHandler handler = new ArgumentsHandler(builder, IGNORED_XX_OPTIONS, MAPPED_XX_OPTIONS, args);
List<String> jvmArgs = new ArrayList<>();

boolean ignoreUnrecognized = false;
boolean autoAdjustHeapSize = true;
boolean ignoreUnrecognized = args.getIgnoreUnrecognized();
List<String> xOptions = new ArrayList<>();

for (int i = 0; i < count; i++) {
Expand All @@ -99,6 +99,10 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args)
if (str.isNonNull()) {
String optionString = CTypeConversion.toJavaString(option.getOptionString());
buildJvmArg(jvmArgs, optionString);
if (ignoredArgs.get(i)) {
// this argument participates in jvmArgs but should otherwise be ignored
continue;
}
if (optionString.startsWith("-Xbootclasspath:")) {
bootClasspathPrepend = null;
bootClasspathAppend = null;
Expand Down Expand Up @@ -128,7 +132,7 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args)
builder.option(AGENT_PATH + split[0], split[1]);
} else if (optionString.startsWith("-D")) {
String key = optionString.substring("-D".length());
int splitAt = key.indexOf("=");
int splitAt = key.indexOf('=');
String value = "";
if (splitAt >= 0) {
value = key.substring(splitAt + 1);
Expand Down Expand Up @@ -181,10 +185,6 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args)
builder.option(JAVA_PROPS + "jdk.module.limitmods", optionString.substring("--limit-modules=".length()));
} else if (optionString.equals("--enable-preview")) {
builder.option("java.EnablePreview", "true");
} else if (optionString.equals("-XX:-AutoAdjustHeapSize")) {
autoAdjustHeapSize = false;
} else if (optionString.equals("-XX:+AutoAdjustHeapSize")) {
autoAdjustHeapSize = true;
} else if (isXOption(optionString)) {
xOptions.add(optionString);
} else if (optionString.equals("-XX:+IgnoreUnrecognizedVMOptions")) {
Expand Down Expand Up @@ -233,11 +233,7 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args)
}

for (String xOption : xOptions) {
var opt = xOption;
if (autoAdjustHeapSize) {
opt = maybeAdjustMaxHeapSize(xOption);
}
RuntimeOptions.set(opt.substring(2 /* drop the -X */), null);
RuntimeOptions.set(xOption.substring(2 /* drop the -X */), null);
}

if (bootClasspathPrepend != null) {
Expand All @@ -259,45 +255,6 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args)
return JNIErrors.JNI_OK();
}

private static String maybeAdjustMaxHeapSize(String optionString) {
// (Jun 2024) Espresso uses more memory than HotSpot does, so if the user has set a very
// small heap size that would work on HotSpot then we have to bump it up. 64mb is too small
// to run Gradle's wrapper program which is required to use Espresso with Gradle, so, we
// go to the next power of two beyond that. This number can be reduced in future when
// memory efficiency is better.
if (!optionString.startsWith("-Xmx")) {
return optionString;
}
long maxHeapSizeBytes = parseLong(optionString.substring(4));
final int floorMB = 128;
if (maxHeapSizeBytes < floorMB * 1024 * 1024) {
return "-Xmx" + floorMB + "m";
} else {
return optionString;
}
}

private static long parseLong(String v) {
String valueString = v.trim().toLowerCase(Locale.ROOT);
long scale = 1;
if (valueString.endsWith("k")) {
scale = 1024L;
} else if (valueString.endsWith("m")) {
scale = 1024L * 1024L;
} else if (valueString.endsWith("g")) {
scale = 1024L * 1024L * 1024L;
} else if (valueString.endsWith("t")) {
scale = 1024L * 1024L * 1024L * 1024L;
}

if (scale != 1) {
/* Remove trailing scale character. */
valueString = valueString.substring(0, valueString.length() - 1);
}

return Long.parseLong(valueString) * scale;
}

private static void buildJvmArg(List<String> jvmArgs, String optionString) {
for (String ignored : ignoredJvmArgs) {
if (optionString.startsWith(ignored)) {
Expand All @@ -321,16 +278,16 @@ private static boolean isXOption(String optionString) {
}

private static String appendPath(String paths, String toAppend) {
if (paths != null && paths.length() != 0) {
return toAppend != null && toAppend.length() != 0 ? paths + File.pathSeparator + toAppend : paths;
if (paths != null && !paths.isEmpty()) {
return toAppend != null && !toAppend.isEmpty() ? paths + File.pathSeparator + toAppend : paths;
} else {
return toAppend;
}
}

private static String prependPath(String toPrepend, String paths) {
if (paths != null && paths.length() != 0) {
return toPrepend != null && toPrepend.length() != 0 ? toPrepend + File.pathSeparator + paths : paths;
if (paths != null && !paths.isEmpty()) {
return toPrepend != null && !toPrepend.isEmpty() ? toPrepend + File.pathSeparator + paths : paths;
} else {
return toPrepend;
}
Expand All @@ -351,7 +308,7 @@ private static String[] splitEquals(String value) {
}

public static class ArgumentException extends RuntimeException {
private static final long serialVersionUID = 5430103471994299046L;
@Serial private static final long serialVersionUID = 5430103471994299046L;

private final boolean isExperimental;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
package com.oracle.truffle.espresso.libjavavm;

import java.io.PrintStream;
import java.util.BitSet;

import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.ObjectHandle;
import org.graalvm.nativeimage.ObjectHandles;
import org.graalvm.nativeimage.VMRuntime;
import org.graalvm.nativeimage.c.function.CEntryPoint;
import org.graalvm.nativeimage.c.type.CIntPointer;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Value;
Expand All @@ -48,12 +50,20 @@ public final class LibEspresso {
private LibEspresso() {
}

private static BitSet asBitSet(CIntPointer ignoredIndices, int nIgnoredIndices) {
BitSet result = new BitSet();
for (int i = 0; i < nIgnoredIndices; i++) {
result.set(ignoredIndices.read(i));
}
return result;
}

@CEntryPoint(name = "Espresso_CreateJavaVM")
static int createJavaVM(@SuppressWarnings("unused") IsolateThread thread, JNIJavaVMPointer javaVMPointer, JNIEnvironmentPointer penv, JNIJavaVMInitArgs args) {
if (args.getVersion() < JNIVersion.JNI_VERSION_1_2() || args.getVersion() > JNIVersion.JNI_VERSION_10) {
static int createJavaVM(@SuppressWarnings("unused") IsolateThread thread, JNIJavaVMPointer javaVMPointer, JNIEnvironmentPointer penv, JNIJavaVMInitArgs args, CIntPointer ignoredIndices,
int nIgnoredIndices) {
if (args.getVersion() < JNIVersion.JNI_VERSION_1_2() || args.getVersion() > JNIVersion.JNI_VERSION_21()) {
return JNIErrors.JNI_EVERSION();
}
// TODO use Launcher infra to parse graalvm specific options
Context.Builder builder = Context.newBuilder().allowAllAccess(true);

// These option need to be set before calling `Arguments.setupContext()` so that cmd line
Expand All @@ -63,7 +73,7 @@ static int createJavaVM(@SuppressWarnings("unused") IsolateThread thread, JNIJav
// checks and can use unsafe casts.
builder.option("engine.RelaxStaticObjectSafetyChecks", "true");

int result = Arguments.setupContext(builder, args);
int result = Arguments.setupContext(builder, args, asBitSet(ignoredIndices, nIgnoredIndices));
if (result != JNIErrors.JNI_OK()) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ public class JNIVersion {
@CConstant
public static native int JNI_VERSION_1_8();

// Unfortunately those are not always available and "default" values are not supported
// @CConstant
// public static native int JNI_VERSION_9();
public static int JNI_VERSION_9 = 0x00090000;
@CConstant
public static native int JNI_VERSION_9();

@CConstant
public static native int JNI_VERSION_10();

@CConstant
public static native int JNI_VERSION_19();

@CConstant
public static native int JNI_VERSION_20();

// @CConstant
// public static native int JNI_VERSION_10();
public static int JNI_VERSION_10 = 0x000A0000;
@CConstant
public static native int JNI_VERSION_21();

// Checkstyle: resume

Expand All @@ -66,10 +72,16 @@ public static String versionString(int version) {
return "1.6";
} else if (version == JNI_VERSION_1_8()) {
return "1.8";
} else if (version == JNI_VERSION_9) {
} else if (version == JNI_VERSION_9()) {
return "9";
} else if (version == JNI_VERSION_10) {
} else if (version == JNI_VERSION_10()) {
return "10";
} else if (version == JNI_VERSION_19()) {
return "19";
} else if (version == JNI_VERSION_20()) {
return "20";
} else if (version == JNI_VERSION_21()) {
return "21";
}
return "unknown";
}
Expand All @@ -85,10 +97,16 @@ public static int javaSpecVersion(int version) {
return 6;
} else if (version == JNI_VERSION_1_8()) {
return 8;
} else if (version == JNI_VERSION_9) {
} else if (version == JNI_VERSION_9()) {
return 9;
} else if (version == JNI_VERSION_10) {
} else if (version == JNI_VERSION_10()) {
return 10;
} else if (version == JNI_VERSION_19()) {
return 19;
} else if (version == JNI_VERSION_20()) {
return 20;
} else if (version == JNI_VERSION_21()) {
return 21;
}
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
extern "C" {
#endif

typedef int (*Espresso_CreateJavaVM_fn_t)(graal_isolatethread_t* thread, struct JavaVM_** javaVMPointer, struct JNIEnv_** penv, JavaVMInitArgs* args);
typedef int (*Espresso_CreateJavaVM_fn_t)(graal_isolatethread_t* thread, struct JavaVM_** javaVMPointer, struct JNIEnv_** penv, JavaVMInitArgs* args, int* ignoredIndices, int nIgnoredIndices);

typedef int (*Espresso_EnterContext_fn_t)(graal_isolatethread_t* thread, struct JavaVM_* javaVM);

Expand Down
Loading

0 comments on commit 8039fbf

Please sign in to comment.