Skip to content

Commit

Permalink
[GR-58490] Adapt PartialConfigurationWithOrigins to reachability-meta…
Browse files Browse the repository at this point in the history
…data.json

PullRequest: graal/18892
  • Loading branch information
loicottet committed Jan 7, 2025
2 parents 2de29a9 + ddbeaae commit 05c2fd1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.Assert;
import org.junit.Test;

import com.oracle.svm.configure.ConfigurationBase;
import com.oracle.svm.configure.config.ConfigurationFileCollection;
import com.oracle.svm.configure.config.ConfigurationSet;
import com.oracle.svm.core.configure.ConfigurationFile;
Expand Down Expand Up @@ -70,9 +71,14 @@ private static String getConfigurationJSON(ConfigurationSet config) throws IOExc
StringWriter sw = new StringWriter();
try (JsonWriter writer = new JsonWriter(sw)) {
for (ConfigurationFile file : ConfigurationFile.agentGeneratedFiles()) {
if (!config.getConfiguration(file).isEmpty()) {
ConfigurationBase<?, ?> configuration = config.getConfiguration(file);
if (!configuration.isEmpty()) {
sw.append("\n").append(file.getName()).append("\n");
config.getConfiguration(file).printJson(writer);
if (configuration.supportsCombinedFile()) {
configuration.printJson(writer);
} else {
configuration.printLegacyJson(writer);
}
}
}
return sw.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,7 @@ public static List<Path> writeConfigurationToAllPaths(Function<ConfigurationFile
} else {
writer.appendSeparator();
}
if (!configFile.equals(ConfigurationFile.RESOURCES)) {
/*
* Resources are printed at the top level of the object, not in a defined
* field
*/
writer.quote(configFile.getFieldName()).appendFieldSeparator();
}
configSupplier.apply(configFile).printJson(writer);
printConfigurationToCombinedFile(configSupplier.apply(configFile), configFile, writer);
}
}
writer.appendObjectEnd();
Expand All @@ -205,6 +198,16 @@ public static List<Path> writeConfigurationToAllPaths(Function<ConfigurationFile
return writtenFiles;
}

public static void printConfigurationToCombinedFile(JsonPrintable config, ConfigurationFile configFile, JsonWriter writer) throws IOException {
if (!configFile.equals(ConfigurationFile.RESOURCES)) {
/*
* Resources are printed at the top level of the object, not in a defined field
*/
writer.quote(configFile.getFieldName()).appendFieldSeparator();
}
config.printJson(writer);
}

public List<Path> writeConfiguration(Function<ConfigurationFile, Path> configFilePathResolver) throws IOException {
return writeConfiguration(configFilePathResolver, this::getConfiguration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Set;

import com.oracle.svm.configure.ConfigurationBase;
import com.oracle.svm.core.configure.ConfigurationFile;

import jdk.graal.compiler.util.json.JsonPrintable;
Expand Down Expand Up @@ -65,8 +66,14 @@ private void printNode(JsonWriter writer, String prefix, MethodCallNode node) th
writer.append(prefix).append(node.methodInfo.toString());
if (node.hasConfig(configFile)) {
writer.append(" - ");
ConfigurationBase<?, ?> config = node.configuration.getConfiguration(configFile);
StringWriter sw = new StringWriter();
node.configuration.getConfiguration(configFile).printJson(new JsonWriter(sw));
JsonWriter jw = new JsonWriter(sw);
if (config.supportsCombinedFile()) {
config.printJson(jw);
} else {
config.printLegacyJson(jw);
}
writer.append(sw.toString().replace("\n", " "));
}
writer.newline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.MapCursor;

import com.oracle.svm.configure.ConfigurationBase;
import com.oracle.svm.configure.config.ConfigurationSet;
import com.oracle.svm.core.configure.ConfigurationFile;
import com.oracle.svm.core.configure.ConfigurationParser;
Expand Down Expand Up @@ -143,22 +144,34 @@ private void parseMethodEntry(MethodCallNode parent, EconomicMap<String, ?> meth

private static void printConfigurationSet(JsonWriter writer, ConfigurationSet configurationSet) throws IOException {
if (!configurationSet.isEmpty()) {
writer.quote("config").append(": {").indent().newline();
writer.quote("config").appendFieldSeparator().appendObjectStart();

/* Print combined file */
writer.quote(ConfigurationFile.REACHABILITY_METADATA.getName()).appendFieldSeparator().appendObjectStart();
boolean first = true;
for (ConfigurationFile file : ConfigurationFile.agentGeneratedFiles()) {
if (!configurationSet.getConfiguration(file).isEmpty()) {
ConfigurationBase<?, ?> config = configurationSet.getConfiguration(file);
if (!config.isEmpty() && config.supportsCombinedFile()) {
if (first) {
first = false;
} else {
writer.append(",").newline();
writer.appendSeparator();
}
ConfigurationSet.printConfigurationToCombinedFile(config, file, writer);
}
}
writer.appendObjectEnd();

writer.quote(file.getName()).append(": ");
configurationSet.getConfiguration(file).printJson(writer);
/* Print legacy files */
for (ConfigurationFile file : ConfigurationFile.agentGeneratedFiles()) {
ConfigurationBase<?, ?> config = configurationSet.getConfiguration(file);
if (!config.isEmpty() && !config.supportsCombinedFile()) {
writer.appendSeparator();
writer.quote(file.getName()).appendFieldSeparator();
config.printLegacyJson(writer);
}
}
writer.unindent().newline()
.append("}");
writer.appendObjectEnd();
}
}

Expand All @@ -170,7 +183,13 @@ private static void parseConfigurationSet(EconomicMap<String, ?> configJson, Con
if (configType == null) {
throw new JsonParserException("Invalid configuration type: " + configName);
}
configurationSet.getConfiguration(configType).createParser(false).parseAndRegister(cursor.getValue(), origin);
if (configType == ConfigurationFile.REACHABILITY_METADATA) {
for (ConfigurationFile file : ConfigurationFile.combinedFileConfigurations()) {
configurationSet.getConfiguration(file).createParser(true).parseAndRegister(cursor.getValue(), origin);
}
} else {
configurationSet.getConfiguration(configType).createParser(false).parseAndRegister(cursor.getValue(), origin);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@

public enum ConfigurationFile {
/* Combined file */
REACHABILITY_METADATA("reachability-metadata", null, true, true),
REACHABILITY_METADATA("reachability-metadata", null, true, false),
/* Main metadata categories (order matters) */
REFLECTION("reflect", REFLECTION_KEY, true, false),
RESOURCES("resource", RESOURCES_KEY, true, false),
SERIALIZATION("serialization", SERIALIZATION_KEY, true, false),
JNI("jni", JNI_KEY, true, false),
REFLECTION("reflect", REFLECTION_KEY, true, true),
RESOURCES("resource", RESOURCES_KEY, true, true),
SERIALIZATION("serialization", SERIALIZATION_KEY, true, true),
JNI("jni", JNI_KEY, true, true),
/* Deprecated metadata categories */
DYNAMIC_PROXY("proxy", null, true, false),
PREDEFINED_CLASSES_NAME("predefined-classes", null, true, false),
Expand All @@ -51,20 +51,21 @@ public enum ConfigurationFile {
private final String name;
private final String fieldName;
private final boolean canAgentGenerate;
private final boolean combinedFile;
private final boolean inCombinedFile;

public static final String LOCK_FILE_NAME = ".lock";
public static final String PREDEFINED_CLASSES_AGENT_EXTRACTED_SUBDIR = "agent-extracted-predefined-classes";
public static final String PREDEFINED_CLASSES_AGENT_EXTRACTED_NAME_SUFFIX = ".classdata";
public static final String PARTIAL_CONFIGURATION_WITH_ORIGINS = "partial-config-with-origins.json";

private static final ConfigurationFile[] agentGeneratedFiles = computeAgentGeneratedFiles();
private static final ConfigurationFile[] combinedFileConfigurations = computeCombinedFileConfigurations();

ConfigurationFile(String name, String fieldName, boolean canAgentGenerate, boolean combinedFile) {
ConfigurationFile(String name, String fieldName, boolean canAgentGenerate, boolean inCombinedFile) {
this.name = name;
this.fieldName = fieldName;
this.canAgentGenerate = canAgentGenerate;
this.combinedFile = combinedFile;
this.inCombinedFile = inCombinedFile;
}

public String getName() {
Expand All @@ -76,15 +77,15 @@ public String getFieldName() {
}

public String getFileName() {
return name + (combinedFile ? COMBINED_FILE_NAME_SUFFIX : LEGACY_FILE_NAME_SUFFIX);
return name + (this == REACHABILITY_METADATA ? COMBINED_FILE_NAME_SUFFIX : LEGACY_FILE_NAME_SUFFIX);
}

public String getFileName(String suffix) {
return name + suffix;
}

public boolean canBeGeneratedByAgent() {
return canAgentGenerate && !combinedFile;
return canAgentGenerate && this != REACHABILITY_METADATA;
}

public static ConfigurationFile getByName(String name) {
Expand All @@ -103,4 +104,12 @@ public static ConfigurationFile[] agentGeneratedFiles() {
private static ConfigurationFile[] computeAgentGeneratedFiles() {
return Arrays.stream(values()).filter(f -> f.canBeGeneratedByAgent()).toArray(ConfigurationFile[]::new);
}

public static ConfigurationFile[] combinedFileConfigurations() {
return combinedFileConfigurations;
}

private static ConfigurationFile[] computeCombinedFileConfigurations() {
return Arrays.stream(values()).filter(f -> f.inCombinedFile).toArray(ConfigurationFile[]::new);
}
}

0 comments on commit 05c2fd1

Please sign in to comment.