8316969: Improve CDS module graph support for --module option

Reviewed-by: iklam, alanb
This commit is contained in:
Calvin Cheung 2023-11-02 16:03:14 +00:00
parent 7a7b1e5a92
commit e318cd25cb
13 changed files with 296 additions and 21 deletions

View file

@ -44,10 +44,12 @@ public class CDS {
private static final boolean isDumpingClassList;
private static final boolean isDumpingArchive;
private static final boolean isSharingEnabled;
private static final boolean isDumpingStaticArchive;
static {
isDumpingClassList = isDumpingClassList0();
isDumpingArchive = isDumpingArchive0();
isSharingEnabled = isSharingEnabled0();
isDumpingStaticArchive = isDumpingArchive && !isSharingEnabled;
}
/**
@ -71,6 +73,13 @@ public class CDS {
return isSharingEnabled;
}
/**
* Is dumping static archive.
*/
public static boolean isDumpingStaticArchive() {
return isDumpingStaticArchive;
}
private static native boolean isDumpingClassList0();
private static native boolean isDumpingArchive0();
private static native boolean isSharingEnabled0();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,7 @@
*/
package jdk.internal.module;
import java.util.Objects;
import java.util.function.Function;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
@ -41,17 +42,20 @@ class ArchivedModuleGraph {
private final ModuleFinder finder;
private final Configuration configuration;
private final Function<String, ClassLoader> classLoaderFunction;
private final String mainModule;
private ArchivedModuleGraph(boolean hasSplitPackages,
boolean hasIncubatorModules,
ModuleFinder finder,
Configuration configuration,
Function<String, ClassLoader> classLoaderFunction) {
Function<String, ClassLoader> classLoaderFunction,
String mainModule) {
this.hasSplitPackages = hasSplitPackages;
this.hasIncubatorModules = hasIncubatorModules;
this.finder = finder;
this.configuration = configuration;
this.classLoaderFunction = classLoaderFunction;
this.mainModule = mainModule;
}
ModuleFinder finder() {
@ -79,8 +83,7 @@ class ArchivedModuleGraph {
*/
static ArchivedModuleGraph get(String mainModule) {
ArchivedModuleGraph graph = archivedModuleGraph;
// We only allow the unnamed module (default) case for now
if (mainModule == null) {
if ((graph != null) && Objects.equals(graph.mainModule, mainModule)) {
return graph;
} else {
return null;
@ -94,12 +97,14 @@ class ArchivedModuleGraph {
boolean hasIncubatorModules,
ModuleFinder finder,
Configuration configuration,
Function<String, ClassLoader> classLoaderFunction) {
Function<String, ClassLoader> classLoaderFunction,
String mainModule) {
archivedModuleGraph = new ArchivedModuleGraph(hasSplitPackages,
hasIncubatorModules,
finder,
configuration,
classLoaderFunction);
classLoaderFunction,
mainModule);
}
static {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -141,7 +141,6 @@ public final class ModuleBootstrap {
return getProperty("jdk.module.upgrade.path") == null &&
getProperty("jdk.module.path") == null &&
getProperty("jdk.module.patch.0") == null && // --patch-module
getProperty("jdk.module.main") == null && // --module
getProperty("jdk.module.addmods.0") == null && // --add-modules
getProperty("jdk.module.limitmods") == null && // --limit-modules
getProperty("jdk.module.addreads.0") == null && // --add-reads
@ -228,7 +227,8 @@ public final class ModuleBootstrap {
systemModules = SystemModuleFinders.systemModules(mainModule);
if (systemModules != null && !isPatched) {
needResolution = (traceOutput != null);
canArchive = true;
if (CDS.isDumpingStaticArchive())
canArchive = true;
}
}
if (systemModules == null) {
@ -469,14 +469,30 @@ public final class ModuleBootstrap {
limitedFinder = new SafeModuleFinder(finder);
}
// If -Xshare:dump and mainModule are specified, check if the mainModule
// is in the runtime image and not on the upgrade module path. If so,
// set canArchive to true so that the module graph can be archived.
if (CDS.isDumpingStaticArchive() && mainModule != null) {
String scheme = systemModuleFinder.find(mainModule)
.stream()
.map(ModuleReference::location)
.flatMap(Optional::stream)
.findAny()
.map(URI::getScheme)
.orElse(null);
if ("jrt".equalsIgnoreCase(scheme)) {
canArchive = true;
}
}
// Archive module graph and boot layer can be archived at CDS dump time.
// Only allow the unnamed module case for now.
if (canArchive && (mainModule == null)) {
if (canArchive) {
ArchivedModuleGraph.archive(hasSplitPackages,
hasIncubatorModules,
systemModuleFinder,
cf,
clf);
clf,
mainModule);
if (!hasSplitPackages && !hasIncubatorModules) {
ArchivedBootLayer.archive(bootLayer);
}