Skip to content

Commit a01c22f

Browse files
committed
[GR-64282] Espresso CDS: Module layer caching.
PullRequest: graal/20898
2 parents 82009ec + 805131a commit a01c22f

File tree

26 files changed

+2024
-75
lines changed

26 files changed

+2024
-75
lines changed

espresso-shared/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/tables/AbstractModuleTable.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.oracle.truffle.espresso.classfile.tables;
2626

2727
import java.util.ArrayList;
28+
import java.util.List;
2829
import java.util.concurrent.locks.ReadWriteLock;
2930

3031
import com.oracle.truffle.espresso.classfile.descriptors.Name;
@@ -36,11 +37,11 @@ public AbstractModuleTable(ReadWriteLock lock) {
3637
}
3738

3839
public ME createAndAddEntry(Symbol<Name> name, String version, String location, boolean isOpen, M module) {
39-
return createAndAddEntry(name, new ModuleData<>(version, location, module, isOpen));
40+
return createAndAddEntry(name, new ModuleData<>(version, location, module, 0, isOpen));
4041
}
4142

4243
public ME createUnnamedModuleEntry(M module) {
43-
ME result = createEntry(null, new ModuleData<>(null, null, module, true));
44+
ME result = createEntry(null, new ModuleData<>(null, null, module, 0, true));
4445
result.setCanReadAllUnnamed();
4546
return result;
4647
}
@@ -50,22 +51,36 @@ public static final class ModuleData<M> {
5051
private final String location;
5152
private final boolean isOpen;
5253
private final M module;
54+
private final int archivedModuleRefId;
5355

54-
public ModuleData(String version, String location, M module, boolean isOpen) {
56+
public ModuleData(String version, String location, M module, int archivedModuleRefId, boolean isOpen) {
5557
this.version = version;
5658
this.location = location;
5759
this.isOpen = isOpen;
5860
this.module = module;
61+
this.archivedModuleRefId = archivedModuleRefId;
5962
}
6063
}
6164

6265
public abstract static class AbstractModuleEntry<M> extends EntryTable.NamedEntry {
6366
private final boolean isOpen;
6467
private M module;
68+
69+
public int getArchivedModuleRefId() {
70+
return archivedModuleRefId;
71+
}
72+
73+
private int archivedModuleRefId;
6574
private String version;
6675
private String location;
76+
77+
public boolean canReadAllUnnamed() {
78+
return canReadAllUnnamed;
79+
}
80+
6781
private boolean canReadAllUnnamed;
68-
private ArrayList<AbstractModuleEntry<M>> reads;
82+
83+
protected List<AbstractModuleEntry<M>> reads;
6984
private volatile boolean hasDefaultReads;
7085

7186
protected AbstractModuleEntry(Symbol<Name> name, ModuleData<M> data) {
@@ -74,6 +89,7 @@ protected AbstractModuleEntry(Symbol<Name> name, ModuleData<M> data) {
7489
this.location = data.location;
7590
this.isOpen = data.isOpen;
7691
this.module = data.module;
92+
this.archivedModuleRefId = data.archivedModuleRefId;
7793
}
7894

7995
public void addReads(AbstractModuleEntry<M> from) {

espresso-shared/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/tables/AbstractPackageTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected AbstractPackageEntry(Symbol<Name> name, ME module) {
4242
}
4343

4444
private final ME module;
45-
private ArrayList<ME> exports;
45+
protected ArrayList<ME> exports;
4646
private boolean isUnqualifiedExported;
4747
private boolean isExportedAllUnnamed;
4848
private String bootClasspathLocation;

espresso-shared/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/tables/EntryTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
3737

3838
public abstract class EntryTable<T extends EntryTable.NamedEntry, K> {
39-
private final HashMap<Symbol<Name>, T> entries = new HashMap<>();
39+
protected final HashMap<Symbol<Name>, T> entries = new HashMap<>();
4040

4141
private final BlockLock readBlock;
4242
private final BlockLock writeBlock;

espresso/mx.espresso/mx_espresso.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ def _run_java_truffle(args=None, cwd=None, nonZeroIsFatal=True, out=None, err=No
176176

177177
def _run_espresso(args=None, cwd=None, nonZeroIsFatal=True, out=None, err=None, timeout=None):
178178
if _has_native_espresso_standalone():
179-
_run_java_truffle(args, cwd, nonZeroIsFatal, out, err, timeout)
179+
return _run_java_truffle(args, cwd, nonZeroIsFatal, out, err, timeout)
180180
else:
181-
_run_espresso_launcher(args, cwd, nonZeroIsFatal, out, err, timeout)
181+
return _run_espresso_launcher(args, cwd, nonZeroIsFatal, out, err, timeout)
182182

183183

184184
def _run_espresso_meta(args, nonZeroIsFatal=True, timeout=None):

espresso/src/com.oracle.truffle.espresso.launcher/src/com/oracle/truffle/espresso/launcher/EspressoLauncher.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,6 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
272272
case "-Xint":
273273
espressoOptions.put("engine.Compilation", "false");
274274
break;
275-
case "-Xshare:auto":
276-
case "-Xshare:off":
277-
// ignore
278-
break;
279275

280276
case "-XX:+PauseOnExit":
281277
pauseOnExit = true;
@@ -320,6 +316,9 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
320316
espressoOptions.put(AGENT_PATH + split[0], split[1]);
321317
} else if (arg.startsWith("-Xmn") || arg.startsWith("-Xms") || arg.startsWith("-Xmx") || arg.startsWith("-Xss") || arg.startsWith("-XX:MaxHeapSize=")) {
322318
unrecognized.add("--vm." + arg.substring(1));
319+
} else if (arg.startsWith("-Xshare:")) {
320+
String value = arg.substring("-Xshare:".length());
321+
espressoOptions.put("java.CDS", value);
323322
} else if (arg.startsWith("-XX:")) {
324323
handleXXArg(arg, unrecognized);
325324
} else
@@ -428,6 +427,7 @@ private void parseArgFile(String pathArg, List<String> expanded) {
428427
}
429428

430429
private static final Set<String> knownPassThroughOptions = Set.of(
430+
"SharedArchiveFile",
431431
"WhiteBoxAPI",
432432
"EnableJVMCI");
433433

@@ -644,6 +644,12 @@ protected void launch(Builder contextBuilder) {
644644
}
645645
}
646646

647+
boolean dumpSharedSpaces = "dump".equals(espressoOptions.get("java.CDS")); // -Xshare:dump
648+
if (dumpSharedSpaces) {
649+
context.initialize("java");
650+
exit();
651+
}
652+
647653
if (mainClassName == null) {
648654
throw abort(usage());
649655
}

espresso/src/com.oracle.truffle.espresso.libjavavm/src/com/oracle/truffle/espresso/libjavavm/Arguments.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ public static int setupContext(Context.Builder builder, JNIJavaVMInitArgs args,
218218
builder.option("engine.CompileImmediately", "true");
219219
} else if (optionString.startsWith("-Xint") || "-XX:TieredStopAtLevel=0".equals(optionString)) {
220220
builder.option("engine.Compilation", "false");
221-
} else if ("-Xshare:auto".equals(optionString) || "-Xshare:off".equals(optionString)) {
222-
// ignore
221+
} else if (optionString.startsWith("-Xshare:")) {
222+
String value = optionString.substring("-Xshare:".length());
223+
builder.option("java.CDS", value);
223224
} else if (optionString.startsWith("-XX:")) {
224225
handler.handleXXArg(optionString);
225226
} else if (optionString.startsWith("--help:")) {

espresso/src/com.oracle.truffle.espresso.mokapot/src/mokapot.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,15 @@ JNIEXPORT void JNICALL JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env, jcl
15691569
jobject implMethodMember,
15701570
jobject instantiatedMethodType,
15711571
jclass lambdaProxyClass) {
1572-
UNIMPLEMENTED(JVM_RegisterLambdaProxyClassForArchiving);
1572+
IMPLEMENTED(JVM_RegisterLambdaProxyClassForArchiving);
1573+
(*getEnv())->JVM_RegisterLambdaProxyClassForArchiving(env,
1574+
caller,
1575+
invokedName,
1576+
invokedType,
1577+
methodType,
1578+
implMethodMember,
1579+
instantiatedMethodType,
1580+
lambdaProxyClass);
15731581
return;
15741582
}
15751583

@@ -1579,8 +1587,14 @@ JNIEXPORT jclass JNICALL JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env, jcla
15791587
jobject methodType,
15801588
jobject implMethodMember,
15811589
jobject instantiatedMethodType) {
1582-
UNIMPLEMENTED(JVM_LookupLambdaProxyClassFromArchive);
1583-
return NULL;
1590+
IMPLEMENTED(JVM_LookupLambdaProxyClassFromArchive);
1591+
return (*getEnv())->JVM_LookupLambdaProxyClassFromArchive(env,
1592+
caller,
1593+
invokedName,
1594+
invokedType,
1595+
methodType,
1596+
implMethodMember,
1597+
instantiatedMethodType);
15841598
}
15851599

15861600
JNIEXPORT jboolean JNICALL JVM_IsCDSDumpingEnabled(JNIEnv* env) {

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/EspressoOptions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,25 @@ public enum GuestFieldOffsetStrategyEnum {
725725
stability = OptionStability.EXPERIMENTAL, //
726726
usageSyntax = "false|true") public static final OptionKey<Boolean> EagerFrameAnalysis = new OptionKey<>(false);
727727

728+
public enum XShareOption {
729+
auto,
730+
on,
731+
off,
732+
dump
733+
}
734+
735+
@Option(help = "Sets the class data sharing (CDS) mode.", //
736+
category = OptionCategory.EXPERT, //
737+
stability = OptionStability.EXPERIMENTAL, //
738+
usageSyntax = "auto|on|off|dump") //
739+
public static final OptionKey<XShareOption> CDS = new OptionKey<>(XShareOption.auto);
740+
741+
@Option(help = "Overrides the default path to the (static) CDS archive.", //
742+
category = OptionCategory.EXPERT, //
743+
stability = OptionStability.EXPERIMENTAL, //
744+
usageSyntax = "<path>") //
745+
public static final OptionKey<Path> SharedArchiveFile = new OptionKey<>(EMPTY, PATH_OPTION_TYPE);
746+
728747
/**
729748
* Property used to force liveness analysis to also be applied by the interpreter. For testing
730749
* purpose only. Use a host property rather than an option. An option would slow interpreter
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package com.oracle.truffle.espresso.cds;
24+
25+
import com.oracle.truffle.espresso.impl.ModuleTable;
26+
import com.oracle.truffle.espresso.impl.PackageTable;
27+
28+
public final class ArchivedRegistryData {
29+
PackageTable packageTable;
30+
ModuleTable moduleTable;
31+
ModuleTable.ModuleEntry unnamedModule;
32+
33+
public ArchivedRegistryData(PackageTable packageTable, ModuleTable moduleTable, ModuleTable.ModuleEntry unnamedModule) {
34+
this.packageTable = packageTable;
35+
this.moduleTable = moduleTable;
36+
this.unnamedModule = unnamedModule;
37+
}
38+
39+
public PackageTable packageTable() {
40+
return packageTable;
41+
}
42+
43+
public ModuleTable moduleTable() {
44+
return moduleTable;
45+
}
46+
47+
public ModuleTable.ModuleEntry unnamedModule() {
48+
return unnamedModule;
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package com.oracle.truffle.espresso.cds;
24+
25+
interface CDSArchiveFormat {
26+
27+
int TAG_NULL = 1;
28+
int TAG_REFERENCE = 2;
29+
int TAG_STRING = 3;
30+
int TAG_SYMBOL = 4;
31+
int TAG_CLASS_REGISTRY_DATA = 5;
32+
int TAG_MODULE_ENTRY = 6;
33+
int TAG_PACKAGE_ENTRY = 7;
34+
int TAG_ARRAY_LIST = 8;
35+
36+
int TAG_GUEST_NULL = 31;
37+
int TAG_GUEST_ARRAY = 32;
38+
int TAG_GUEST_CLASS = 33;
39+
int TAG_GUEST_STRING = 34;
40+
int TAG_GUEST_OBJECT = 35;
41+
int TAG_GUEST_CLASS_LOADER = 36;
42+
43+
int MAGIC = 0x35973550;
44+
45+
int MAJOR_VERSION = 1;
46+
int MINOR_VERSION = 1;
47+
}

0 commit comments

Comments
 (0)