8214858: Improve module graph archiving
authorredestad
Thu, 06 Dec 2018 12:51:13 +0100
changeset 52872 357d2b80748d
parent 52871 c09bff7928e8
child 52873 6e7db888f04c
8214858: Improve module graph archiving Reviewed-by: jiangli, alanb
src/hotspot/share/memory/heapShared.cpp
src/java.base/share/classes/jdk/internal/module/ArchivedModuleGraph.java
src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
--- a/src/hotspot/share/memory/heapShared.cpp	Thu Dec 06 12:34:28 2018 +0100
+++ b/src/hotspot/share/memory/heapShared.cpp	Thu Dec 06 12:51:13 2018 +0100
@@ -71,10 +71,7 @@
 };
 // Entry fields for subgraphs archived in the open archive heap region.
 static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = {
-  {"jdk/internal/module/ArchivedModuleGraph",  "archivedSystemModules"},
-  {"jdk/internal/module/ArchivedModuleGraph",  "archivedModuleFinder"},
-  {"jdk/internal/module/ArchivedModuleGraph",  "archivedMainModule"},
-  {"jdk/internal/module/ArchivedModuleGraph",  "archivedConfiguration"},
+  {"jdk/internal/module/ArchivedModuleGraph",  "archivedModuleGraph"},
   {"java/util/ImmutableCollections$ListN",     "EMPTY_LIST"},
   {"java/util/ImmutableCollections$MapN",      "EMPTY_MAP"},
   {"java/util/ImmutableCollections$SetN",      "EMPTY_SET"},
--- a/src/java.base/share/classes/jdk/internal/module/ArchivedModuleGraph.java	Thu Dec 06 12:34:28 2018 +0100
+++ b/src/java.base/share/classes/jdk/internal/module/ArchivedModuleGraph.java	Thu Dec 06 12:51:13 2018 +0100
@@ -27,32 +27,40 @@
 
 import java.lang.module.Configuration;
 import java.lang.module.ModuleFinder;
+import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
+
 import jdk.internal.misc.VM;
 
 /**
  * Used by ModuleBootstrap to obtain the archived system modules and finder.
  */
 final class ArchivedModuleGraph {
-    private static String archivedMainModule;
-    private static SystemModules archivedSystemModules;
-    private static ModuleFinder archivedModuleFinder;
-    private static Configuration archivedConfiguration;
+    private static ArchivedModuleGraph archivedModuleGraph;
 
-    private final SystemModules systemModules;
+    private final String mainModule;
+    private final boolean hasSplitPackages;
+    private final boolean hasIncubatorModules;
     private final ModuleFinder finder;
     private final Configuration configuration;
+    private final Map<String, Set<String>> concealedPackagesToOpen;
+    private final Map<String, Set<String>> exportedPackagesToOpen;
 
-    private ArchivedModuleGraph(SystemModules modules,
+    private ArchivedModuleGraph(String mainModule,
+                                boolean hasSplitPackages,
+                                boolean hasIncubatorModules,
                                 ModuleFinder finder,
-                                Configuration configuration) {
-        this.systemModules = modules;
+                                Configuration configuration,
+                                Map<String, Set<String>> concealedPackagesToOpen,
+                                Map<String, Set<String>> exportedPackagesToOpen) {
+        this.mainModule = mainModule;
+        this.hasSplitPackages = hasSplitPackages;
+        this.hasIncubatorModules = hasIncubatorModules;
         this.finder = finder;
         this.configuration = configuration;
-    }
-
-    SystemModules systemModules() {
-        return systemModules;
+        this.concealedPackagesToOpen = concealedPackagesToOpen;
+        this.exportedPackagesToOpen = exportedPackagesToOpen;
     }
 
     ModuleFinder finder() {
@@ -63,32 +71,54 @@
         return configuration;
     }
 
-    // A factory method that ModuleBootstrap can use to obtain the
-    // ArchivedModuleGraph.
+    Map<String, Set<String>> concealedPackagesToOpen() {
+        return concealedPackagesToOpen;
+    }
+
+    Map<String, Set<String>> exportedPackagesToOpen() {
+        return exportedPackagesToOpen;
+    }
+
+    boolean hasSplitPackages() {
+        return hasSplitPackages;
+    }
+
+    boolean hasIncubatorModules() {
+        return hasIncubatorModules;
+    }
+
+    /**
+     * Returns the ArchivedModuleGraph for the given initial module.
+     */
     static ArchivedModuleGraph get(String mainModule) {
-        if (Objects.equals(mainModule, archivedMainModule)
-                && archivedSystemModules != null
-                && archivedModuleFinder != null
-                && archivedConfiguration != null) {
-            return new ArchivedModuleGraph(archivedSystemModules,
-                                           archivedModuleFinder,
-                                           archivedConfiguration);
+        ArchivedModuleGraph graph = archivedModuleGraph;
+        if (graph != null && Objects.equals(mainModule, graph.mainModule)) {
+            return graph;
         } else {
             return null;
         }
     }
 
-    // Used at CDS dump time
+    /**
+     * Archive the module graph for the given initial module.
+     */
     static void archive(String mainModule,
-                        SystemModules systemModules,
+                        boolean hasSplitPackages,
+                        boolean hasIncubatorModules,
                         ModuleFinder finder,
-                        Configuration configuration) {
-        if (archivedMainModule != null)
+                        Configuration configuration,
+                        Map<String, Set<String>> concealedPackagesToOpen,
+                        Map<String, Set<String>> exportedPackagesToOpen) {
+        if (mainModule != null) {
             throw new UnsupportedOperationException();
-        archivedMainModule = mainModule;
-        archivedSystemModules = systemModules;
-        archivedModuleFinder = finder;
-        archivedConfiguration = configuration;
+        }
+        archivedModuleGraph = new ArchivedModuleGraph(mainModule,
+                                                      hasSplitPackages,
+                                                      hasIncubatorModules,
+                                                      finder,
+                                                      configuration,
+                                                      concealedPackagesToOpen,
+                                                      exportedPackagesToOpen);
     }
 
     static {
--- a/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Thu Dec 06 12:34:28 2018 +0100
+++ b/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Thu Dec 06 12:51:13 2018 +0100
@@ -172,6 +172,8 @@
         boolean haveModulePath = (appModulePath != null || upgradeModulePath != null);
         boolean needResolution = true;
         boolean canArchive = false;
+        boolean hasSplitPackages;
+        boolean hasIncubatorModules;
 
         // If the java heap was archived at CDS dump time and the environment
         // at dump time matches the current environment then use the archived
@@ -182,8 +184,9 @@
                 && addModules.isEmpty()
                 && limitModules.isEmpty()
                 && !isPatched) {
-            systemModules = archivedModuleGraph.systemModules();
             systemModuleFinder = archivedModuleGraph.finder();
+            hasSplitPackages = archivedModuleGraph.hasSplitPackages();
+            hasIncubatorModules = archivedModuleGraph.hasIncubatorModules();
             needResolution = (traceOutput != null);
         } else {
             if (!haveModulePath && addModules.isEmpty() && limitModules.isEmpty()) {
@@ -205,6 +208,11 @@
                 systemModules = new ExplodedSystemModules();
                 systemModuleFinder = SystemModuleFinders.ofSystem();
             }
+
+            hasSplitPackages = systemModules.hasSplitPackages();
+            hasIncubatorModules = systemModules.hasIncubatorModules();
+            // not using the archived module graph - avoid accidental use
+            archivedModuleGraph = null;
         }
 
         Counters.add("jdk.module.boot.1.systemModulesTime", t1);
@@ -395,7 +403,7 @@
         }
 
         // check for split packages in the modules mapped to the built-in loaders
-        if (systemModules.hasSplitPackages() || isPatched || haveModulePath) {
+        if (hasSplitPackages || isPatched || haveModulePath) {
             checkSplitPackages(cf, clf);
         }
 
@@ -415,7 +423,7 @@
         // Step 7: Miscellaneous
 
         // check incubating status
-        if (systemModules.hasIncubatorModules() || haveModulePath) {
+        if (hasIncubatorModules || haveModulePath) {
             checkIncubatingStatus(cf);
         }
 
@@ -423,7 +431,21 @@
         long t7 = System.nanoTime();
         addExtraReads(bootLayer);
         boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
-        addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
+
+        Map<String, Set<String>> concealedPackagesToOpen;
+        Map<String, Set<String>> exportedPackagesToOpen;
+        if (archivedModuleGraph != null) {
+            concealedPackagesToOpen = archivedModuleGraph.concealedPackagesToOpen();
+            exportedPackagesToOpen = archivedModuleGraph.exportedPackagesToOpen();
+        } else {
+            concealedPackagesToOpen = systemModules.concealedPackagesToOpen();
+            exportedPackagesToOpen = systemModules.exportedPackagesToOpen();
+        }
+        addIllegalAccess(upgradeModulePath,
+                         concealedPackagesToOpen,
+                         exportedPackagesToOpen,
+                         bootLayer,
+                         extraExportsOrOpens);
         Counters.add("jdk.module.boot.7.adjustModulesTime", t7);
 
         // save module finders for later use
@@ -436,8 +458,13 @@
         // Module graph can be archived at CDS dump time. Only allow the
         // unnamed module case for now.
         if (canArchive && (mainModule == null)) {
-            ArchivedModuleGraph.archive(mainModule, systemModules,
-                                        systemModuleFinder, cf);
+            ArchivedModuleGraph.archive(mainModule,
+                                        hasSplitPackages,
+                                        hasIncubatorModules,
+                                        systemModuleFinder,
+                                        cf,
+                                        concealedPackagesToOpen,
+                                        exportedPackagesToOpen);
         }
 
         // total time to initialize
@@ -738,7 +765,8 @@
      * of system modules in the boot layer to code in unnamed modules.
      */
     private static void addIllegalAccess(ModuleFinder upgradeModulePath,
-                                         SystemModules systemModules,
+                                         Map<String, Set<String>> concealedPackagesToOpen,
+                                         Map<String, Set<String>> exportedPackagesToOpen,
                                          ModuleLayer bootLayer,
                                          boolean extraExportsOrOpens) {
         String value = getAndRemoveProperty("jdk.module.illegalAccess");
@@ -764,13 +792,11 @@
         IllegalAccessLogger.Builder builder
             = new IllegalAccessLogger.Builder(mode, System.err);
 
-        Map<String, Set<String>> map1 = systemModules.concealedPackagesToOpen();
-        Map<String, Set<String>> map2 = systemModules.exportedPackagesToOpen();
-        if (map1.isEmpty() && map2.isEmpty()) {
+        if (concealedPackagesToOpen.isEmpty() && exportedPackagesToOpen.isEmpty()) {
             // need to generate (exploded build)
             IllegalAccessMaps maps = IllegalAccessMaps.generate(limitedFinder());
-            map1 = maps.concealedPackagesToOpen();
-            map2 = maps.exportedPackagesToOpen();
+            concealedPackagesToOpen = maps.concealedPackagesToOpen();
+            exportedPackagesToOpen = maps.exportedPackagesToOpen();
         }
 
         // open specific packages in the system modules
@@ -789,8 +815,8 @@
                 continue;
             }
 
-            Set<String> concealedPackages = map1.getOrDefault(name, Set.of());
-            Set<String> exportedPackages = map2.getOrDefault(name, Set.of());
+            Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, Set.of());
+            Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, Set.of());
 
             // refresh the set of concealed and exported packages if needed
             if (extraExportsOrOpens) {