Skip to content

Commit

Permalink
[GR-51828] Backport to 23.0: Add CompilationIsolateOptions.
Browse files Browse the repository at this point in the history
PullRequest: graal/16952
  • Loading branch information
hamzaGhaissi committed Feb 16, 2024
2 parents 2ccde4e + 3429c66 commit 3130c73
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ struct __graal_create_isolate_params_t {
/* Fields introduced in version 4 */
char _reserved_3; /* Internal usage, do not use. */
char _reserved_4; /* Internal usage, do not use. */

/* Fields introduced in version 5 */
char _reserved_5; /* Internal usage, do not use. */
};
typedef struct __graal_create_isolate_params_t graal_create_isolate_params_t;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ struct __graal_create_isolate_params_t {
/* Fields introduced in version 4 */
char _reserved_3; /* Internal usage, do not use. */
char _reserved_4; /* Internal usage, do not use. */

/* Fields introduced in version 5 */
char _reserved_5; /* Internal usage, do not use. */
};
typedef struct __graal_create_isolate_params_t graal_create_isolate_params_t;
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.charset.StandardCharsets;

import org.graalvm.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.StackValue;
Expand All @@ -44,6 +43,7 @@
import com.oracle.svm.core.c.CGlobalDataFactory;
import com.oracle.svm.core.c.function.CEntryPointCreateIsolateParameters;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.graal.RuntimeCompilation;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.option.RuntimeOptionKey;
import com.oracle.svm.core.util.VMError;
Expand All @@ -68,15 +68,12 @@ public class IsolateArgumentParser {
private static final long G = K * M;
private static final long T = K * G;

private static boolean isCompilationIsolate;

@Platforms(Platform.HOSTED_ONLY.class)
public IsolateArgumentParser() {
}

@Fold
public static IsolateArgumentParser singleton() {
return ImageSingletons.lookup(IsolateArgumentParser.class);
}

@Platforms(Platform.HOSTED_ONLY.class)
private static byte[] createOptionNames() {
StringBuilder options = new StringBuilder();
Expand Down Expand Up @@ -140,7 +137,7 @@ private static long toLong(Object value) {
@Uninterruptible(reason = "Still being initialized.")
public static void parse(CEntryPointCreateIsolateParameters parameters, CLongPointer parsedArgs) {
initialize(parsedArgs);
if (!LibC.isSupported()) {
if (!LibC.isSupported() || !shouldParseArguments(parameters)) {
// Without LibC support, argument parsing is disabled. So, we just use the build-time
// values of the runtime options.
return;
Expand Down Expand Up @@ -174,14 +171,35 @@ public static void parse(CEntryPointCreateIsolateParameters parameters, CLongPoi
}
}

/**
* Note that this logic must be in sync with
* {@link com.oracle.svm.core.option.RuntimeOptionParser#parseAndConsumeAllOptions}.
*/
@Uninterruptible(reason = "Thread state not yet set up.")
public static boolean shouldParseArguments(CEntryPointCreateIsolateParameters parameters) {
if (SubstrateOptions.ParseRuntimeOptions.getValue()) {
return true;
} else if (RuntimeCompilation.isEnabled() && SubstrateOptions.supportCompileInIsolates()) {
return isCompilationIsolate(parameters);
}
return false;
}

@Uninterruptible(reason = "Thread state not yet set up.")
public void persistOptions(CLongPointer parsedArgs) {
private static boolean isCompilationIsolate(CEntryPointCreateIsolateParameters parameters) {
return parameters.version() >= 5 && parameters.getIsCompilationIsolate();
}

@Uninterruptible(reason = "Thread state not yet set up.")
public static void persistOptions(CEntryPointCreateIsolateParameters parameters, CLongPointer parsedArgs) {
isCompilationIsolate = isCompilationIsolate(parameters);

for (int i = 0; i < OPTION_COUNT; i++) {
PARSED_OPTION_VALUES[i] = parsedArgs.read(i);
}
}

public void verifyOptionValues() {
public static void verifyOptionValues() {
for (int i = 0; i < OPTION_COUNT; i++) {
RuntimeOptionKey<?> option = OPTIONS[i];
if (shouldValidate(option)) {
Expand All @@ -201,6 +219,11 @@ private static boolean shouldValidate(RuntimeOptionKey<?> option) {
return true;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static boolean isCompilationIsolate() {
return isCompilationIsolate;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static boolean getBooleanOptionValue(int index) {
return PARSED_OPTION_VALUES[index] == 1;
Expand Down Expand Up @@ -362,27 +385,14 @@ private static boolean atojulong(CCharPointer s, CLongPointer result) {

long modifier;
switch (tail.read()) {
case 'T':
case 't':
modifier = T;
break;
case 'G':
case 'g':
modifier = G;
break;
case 'M':
case 'm':
modifier = M;
break;
case 'K':
case 'k':
modifier = K;
break;
case '\0':
modifier = 1;
break;
default:
case 'T', 't' -> modifier = T;
case 'G', 'g' -> modifier = G;
case 'M', 'm' -> modifier = M;
case 'K', 'k' -> modifier = K;
case '\0' -> modifier = 1;
default -> {
return false;
}
}

UnsignedWord value = n.multiply(WordFactory.unsigned(modifier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ public static boolean shouldCompileInIsolates() {
return supportCompileInIsolates() && ConcealedOptions.CompileInIsolates.getValue();
}

@Option(help = "Options that are passed to each compilation isolate. Individual arguments are separated by spaces. Arguments that contain spaces need to be enclosed by single quotes.") //
public static final RuntimeOptionKey<String> CompilationIsolateOptions = new RuntimeOptionKey<>(null);

@Option(help = "Size of the reserved address space of each compilation isolate (0: default for new isolates).") //
public static final RuntimeOptionKey<Long> CompilationIsolateAddressSpaceSize = new RuntimeOptionKey<>(0L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,11 @@ public interface CEntryPointCreateIsolateParameters extends PointerBase {

@CField("_reserved_4")
void setExitWhenArgumentParsingFails(boolean value);

/* fields below: version 5 */
@CField("_reserved_5")
boolean getIsCompilationIsolate();

@CField("_reserved_5")
void setIsCompilationIsolate(boolean value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public final class IsolateSupportImpl implements IsolateSupport {

@Override
public IsolateThread createIsolate(CreateIsolateParameters parameters) throws IsolateException {
return createIsolate(parameters, false);
}

public static IsolateThread createIsolate(CreateIsolateParameters parameters, boolean compilationIsolate) throws IsolateException {
if (!SubstrateOptions.SpawnIsolates.getValue()) {
throw new IsolateException(ISOLATES_DISABLED_MESSAGE);
}
Expand Down Expand Up @@ -100,11 +104,12 @@ public IsolateThread createIsolate(CreateIsolateParameters parameters) throws Is
params.setReservedSpaceSize(parameters.getReservedAddressSpaceSize());
params.setAuxiliaryImagePath(auxImagePath.get());
params.setAuxiliaryImageReservedSpaceSize(parameters.getAuxiliaryImageReservedSpaceSize());
params.setVersion(4);
params.setVersion(5);
params.setIgnoreUnrecognizedArguments(false);
params.setExitWhenArgumentParsingFails(false);
params.setArgc(argc);
params.setArgv(argv);
params.setIsCompilationIsolate(compilationIsolate);

// Try to create the isolate.
IsolateThreadPointer isolateThreadPtr = UnsafeStackValue.get(IsolateThreadPointer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,13 @@ private static int createIsolate(CEntryPointCreateIsolateParameters providedPara
setHeapBase(Isolates.getHeapBase(isolate));
}

return createIsolate0(parsedArgs, isolate);
return createIsolate0(isolate, parameters, parsedArgs);
}

@Uninterruptible(reason = "Thread state not yet set up.")
@NeverInline(value = "Ensure this code cannot rise above where heap base is set.")
private static int createIsolate0(CLongPointer parsedArgs, Isolate isolate) {

IsolateArgumentParser.singleton().persistOptions(parsedArgs);
private static int createIsolate0(Isolate isolate, CEntryPointCreateIsolateParameters parameters, CLongPointer parsedArgs) {
IsolateArgumentParser.persistOptions(parameters, parsedArgs);
IsolateListenerSupport.singleton().afterCreateIsolate(isolate);

CodeInfoTable.prepareImageCodeInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.graalvm.nativeimage.VMRuntime;
import org.graalvm.nativeimage.impl.VMRuntimeSupport;

import com.oracle.svm.core.IsolateArgumentParser;
import com.oracle.svm.core.Isolates;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.heap.HeapSizeVerifier;
Expand Down Expand Up @@ -93,6 +94,7 @@ public void initialize() {
boolean shouldInitialize = initializationState.compareAndSet(InitializationState.Uninitialized, InitializationState.InProgress);
if (shouldInitialize) {
// GR-35186: we should verify that none of the early parsed isolate arguments changed.
IsolateArgumentParser.verifyOptionValues();
HeapSizeVerifier.verifyHeapOptions();

executeHooks(startupHooks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@

import com.oracle.svm.common.option.CommonOptionParser.BooleanOptionFormat;
import com.oracle.svm.common.option.CommonOptionParser.OptionParseResult;
import com.oracle.svm.core.IsolateArgumentParser;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.graal.RuntimeCompilation;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.properties.RuntimePropertyParser;
import com.oracle.svm.core.util.ImageHeapMap;
Expand Down Expand Up @@ -74,12 +76,20 @@ public final class RuntimeOptionParser {
* Parse and consume all standard options and system properties supported by Substrate VM. The
* returned array contains all arguments that were not consumed, i.e., were not recognized as
* options.
*
* Note that this logic must be in sync with {@link IsolateArgumentParser#shouldParseArguments}.
*/
public static String[] parseAndConsumeAllOptions(String[] initialArgs, boolean ignoreUnrecognized) {
String[] args = initialArgs;
if (SubstrateOptions.ParseRuntimeOptions.getValue()) {
args = RuntimeOptionParser.singleton().parse(args, NORMAL_OPTION_PREFIX, GRAAL_OPTION_PREFIX, X_OPTION_PREFIX, ignoreUnrecognized);
args = RuntimePropertyParser.parse(args);
} else if (RuntimeCompilation.isEnabled() && SubstrateOptions.supportCompileInIsolates() && IsolateArgumentParser.isCompilationIsolate()) {
/*
* Compilation isolates always need to parse the Native Image options that the main
* isolate passes to them.
*/
args = RuntimeOptionParser.singleton().parse(args, NORMAL_OPTION_PREFIX, null, X_OPTION_PREFIX, ignoreUnrecognized);
}
return args;
}
Expand Down
Loading

0 comments on commit 3130c73

Please sign in to comment.