8114823: G1 doesn't honor request to disable class unloading
authorsjohanss
Mon, 12 Sep 2016 16:34:36 +0200
changeset 41077 19f70464986e
parent 41076 8c6aa0873dd1
child 41078 db508643daab
child 41079 c8ea0d4203d6
8114823: G1 doesn't honor request to disable class unloading Reviewed-by: tschatzl, mgerdin
hotspot/src/share/vm/gc/g1/g1MarkSweep.cpp
hotspot/src/share/vm/gc/g1/g1RootProcessor.cpp
hotspot/src/share/vm/gc/g1/g1RootProcessor.hpp
hotspot/src/share/vm/runtime/arguments.cpp
hotspot/test/gc/class_unloading/TestClassUnloadingDisabled.java
--- a/hotspot/src/share/vm/gc/g1/g1MarkSweep.cpp	Thu Sep 08 12:30:38 2016 +0200
+++ b/hotspot/src/share/vm/gc/g1/g1MarkSweep.cpp	Mon Sep 12 16:34:36 2016 +0200
@@ -132,9 +132,16 @@
   MarkingCodeBlobClosure follow_code_closure(&GenMarkSweep::follow_root_closure, !CodeBlobToOopClosure::FixRelocations);
   {
     G1RootProcessor root_processor(g1h, 1);
-    root_processor.process_strong_roots(&GenMarkSweep::follow_root_closure,
-                                        &GenMarkSweep::follow_cld_closure,
-                                        &follow_code_closure);
+    if (ClassUnloading) {
+      root_processor.process_strong_roots(&GenMarkSweep::follow_root_closure,
+                                          &GenMarkSweep::follow_cld_closure,
+                                          &follow_code_closure);
+    } else {
+      root_processor.process_all_roots_no_string_table(
+                                          &GenMarkSweep::follow_root_closure,
+                                          &GenMarkSweep::follow_cld_closure,
+                                          &follow_code_closure);
+    }
   }
 
   {
@@ -157,7 +164,7 @@
   // This is the point where the entire marking should have completed.
   assert(GenMarkSweep::_marking_stack.is_empty(), "Marking should have completed");
 
-  {
+  if (ClassUnloading) {
     GCTraceTime(Debug, gc, phases) trace("Class Unloading", gc_timer());
 
     // Unload classes and purge the SystemDictionary.
--- a/hotspot/src/share/vm/gc/g1/g1RootProcessor.cpp	Thu Sep 08 12:30:38 2016 +0200
+++ b/hotspot/src/share/vm/gc/g1/g1RootProcessor.cpp	Mon Sep 12 16:34:36 2016 +0200
@@ -83,6 +83,7 @@
   }
 
   process_vm_roots(closures, phase_times, worker_i);
+  process_string_table_roots(closures, phase_times, worker_i);
 
   {
     // Now the CM ref_processor roots.
@@ -191,19 +192,34 @@
 
 void G1RootProcessor::process_all_roots(OopClosure* oops,
                                         CLDClosure* clds,
-                                        CodeBlobClosure* blobs) {
+                                        CodeBlobClosure* blobs,
+                                        bool process_string_table) {
   AllRootsClosures closures(oops, clds);
 
   process_java_roots(&closures, NULL, 0);
   process_vm_roots(&closures, NULL, 0);
 
-  if (!_process_strong_tasks.is_task_claimed(G1RP_PS_CodeCache_oops_do)) {
-    CodeCache::blobs_do(blobs);
+  if (process_string_table) {
+    process_string_table_roots(&closures, NULL, 0);
   }
+  process_code_cache_roots(blobs, NULL, 0);
 
   _process_strong_tasks.all_tasks_completed(n_workers());
 }
 
+void G1RootProcessor::process_all_roots(OopClosure* oops,
+                                        CLDClosure* clds,
+                                        CodeBlobClosure* blobs) {
+  process_all_roots(oops, clds, blobs, true);
+}
+
+void G1RootProcessor::process_all_roots_no_string_table(OopClosure* oops,
+                                                        CLDClosure* clds,
+                                                        CodeBlobClosure* blobs) {
+  assert(!ClassUnloading, "Should only be used when class unloading is disabled");
+  process_all_roots(oops, clds, blobs, false);
+}
+
 void G1RootProcessor::process_java_roots(G1RootClosures* closures,
                                          G1GCPhaseTimes* phase_times,
                                          uint worker_i) {
@@ -280,14 +296,23 @@
       SystemDictionary::roots_oops_do(strong_roots, weak_roots);
     }
   }
+}
 
-  {
-    G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::StringTableRoots, worker_i);
-    // All threads execute the following. A specific chunk of buckets
-    // from the StringTable are the individual tasks.
-    if (weak_roots != NULL) {
-      StringTable::possibly_parallel_oops_do(weak_roots);
-    }
+void G1RootProcessor::process_string_table_roots(G1RootClosures* closures,
+                                                 G1GCPhaseTimes* phase_times,
+                                                 uint worker_i) {
+  assert(closures->weak_oops() != NULL, "Should only be called when all roots are processed");
+  G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::StringTableRoots, worker_i);
+  // All threads execute the following. A specific chunk of buckets
+  // from the StringTable are the individual tasks.
+  StringTable::possibly_parallel_oops_do(closures->weak_oops());
+}
+
+void G1RootProcessor::process_code_cache_roots(CodeBlobClosure* code_closure,
+                                               G1GCPhaseTimes* phase_times,
+                                               uint worker_i) {
+  if (!_process_strong_tasks.is_task_claimed(G1RP_PS_CodeCache_oops_do)) {
+    CodeCache::blobs_do(code_closure);
   }
 }
 
--- a/hotspot/src/share/vm/gc/g1/g1RootProcessor.hpp	Thu Sep 08 12:30:38 2016 +0200
+++ b/hotspot/src/share/vm/gc/g1/g1RootProcessor.hpp	Mon Sep 12 16:34:36 2016 +0200
@@ -73,6 +73,11 @@
   void worker_has_discovered_all_strong_classes();
   void wait_until_all_strong_classes_discovered();
 
+  void process_all_roots(OopClosure* oops,
+                         CLDClosure* clds,
+                         CodeBlobClosure* blobs,
+                         bool process_string_table);
+
   void process_java_roots(G1RootClosures* closures,
                           G1GCPhaseTimes* phase_times,
                           uint worker_i);
@@ -81,6 +86,14 @@
                         G1GCPhaseTimes* phase_times,
                         uint worker_i);
 
+  void process_string_table_roots(G1RootClosures* closures,
+                                  G1GCPhaseTimes* phase_times,
+                                  uint worker_i);
+
+  void process_code_cache_roots(CodeBlobClosure* code_closure,
+                                G1GCPhaseTimes* phase_times,
+                                uint worker_i);
+
 public:
   G1RootProcessor(G1CollectedHeap* g1h, uint n_workers);
 
@@ -99,6 +112,13 @@
                          CLDClosure* clds,
                          CodeBlobClosure* blobs);
 
+  // Apply oops, clds and blobs to strongly and weakly reachable roots in the system,
+  // the only thing different from process_all_roots is that we skip the string table
+  // to avoid keeping every string live when doing class unloading.
+  void process_all_roots_no_string_table(OopClosure* oops,
+                                         CLDClosure* clds,
+                                         CodeBlobClosure* blobs);
+
   // Number of worker threads used by the root processor.
   uint n_workers() const;
 };
--- a/hotspot/src/share/vm/runtime/arguments.cpp	Thu Sep 08 12:30:38 2016 +0200
+++ b/hotspot/src/share/vm/runtime/arguments.cpp	Mon Sep 12 16:34:36 2016 +0200
@@ -1665,11 +1665,6 @@
     CompactibleFreeListSpaceLAB::modify_initialization(OldPLABSize, OldPLABWeight);
   }
 
-  if (!ClassUnloading) {
-    FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false);
-    FLAG_SET_CMDLINE(bool, ExplicitGCInvokesConcurrentAndUnloadsClasses, false);
-  }
-
   log_trace(gc)("MarkStackSize: %uk  MarkStackSizeMax: %uk", (unsigned int) (MarkStackSize / K), (uint) (MarkStackSizeMax / K));
 }
 #endif // INCLUDE_ALL_GCS
@@ -1997,6 +1992,13 @@
     // Keeping the heap 100% free is hard ;-) so limit it to 99%.
     FLAG_SET_ERGO(uintx, MinHeapFreeRatio, 99);
   }
+
+  // If class unloading is disabled, also disable concurrent class unloading.
+  if (!ClassUnloading) {
+    FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false);
+    FLAG_SET_CMDLINE(bool, ClassUnloadingWithConcurrentMark, false);
+    FLAG_SET_CMDLINE(bool, ExplicitGCInvokesConcurrentAndUnloadsClasses, false);
+  }
 #endif // INCLUDE_ALL_GCS
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/gc/class_unloading/TestClassUnloadingDisabled.java	Mon Sep 12 16:34:36 2016 +0200
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reqserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @key gc
+ * @bug 8114823
+ * @requires vm.gc == null
+ * @requires vm.opt.ExplicitGCInvokesConcurrent != true
+ * @requires vm.opt.ClassUnloading != true
+ * @library /test/lib
+ * @modules java.base/jdk.internal.misc
+ *          java.management
+ * @build sun.hotspot.WhiteBox
+ * @run main ClassFileInstaller sun.hotspot.WhiteBox
+ *                              sun.hotspot.WhiteBox$WhiteBoxPermission
+ *
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
+ *                   -XX:-ClassUnloading -XX:+UseG1GC TestClassUnloadingDisabled
+ *
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
+ *                   -XX:-ClassUnloading -XX:+UseSerialGC TestClassUnloadingDisabled
+ *
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
+ *                   -XX:-ClassUnloading -XX:+UseParallelGC TestClassUnloadingDisabled
+ *
+ * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
+ *                   -XX:-ClassUnloading -XX:+UseConcMarkSweepGC TestClassUnloadingDisabled
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import sun.hotspot.WhiteBox;
+
+import static jdk.test.lib.Asserts.*;
+
+public class TestClassUnloadingDisabled {
+    public static void main(String args[]) throws Exception {
+        final WhiteBox wb = WhiteBox.getWhiteBox();
+        // Fetch the dir where the test class and the class
+        // to be loaded resides.
+        String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath();
+        String className = "ClassToLoadUnload";
+
+        assertFalse(wb.isClassAlive(className), "Should not be loaded yet");
+
+        // The NoPDClassLoader handles loading classes in the test directory
+        // and loads them without a protection domain, which in some cases
+        // keeps the class live regardless of marking state.
+        NoPDClassLoader nopd = new NoPDClassLoader(classDir);
+        nopd.loadClass(className);
+
+        assertTrue(wb.isClassAlive(className), "Class should be loaded");
+
+        // Clear the class-loader, class and object references to make
+        // class unloading possible.
+        nopd = null;
+
+        System.gc();
+        assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded");
+    }
+}
+
+class NoPDClassLoader extends ClassLoader {
+    String path;
+
+    NoPDClassLoader(String path) {
+        this.path = path;
+    }
+
+    public Class<?> loadClass(String name) throws ClassNotFoundException {
+        byte[] cls = null;
+        File f = new File(path,name + ".class");
+
+        // Delegate class loading if class not present in the given
+        // directory.
+        if (!f.exists()) {
+            return super.loadClass(name);
+        }
+
+        try {
+            Path path = Paths.get(f.getAbsolutePath());
+            cls = Files.readAllBytes(path);
+        } catch (IOException e) {
+            throw new ClassNotFoundException(name);
+        }
+
+        // Define class with no protection domain and resolve it.
+        return defineClass(name, cls, 0, cls.length, null);
+    }
+}
+
+class ClassToLoadUnload {
+}