# HG changeset patch # User fmatte # Date 1532070469 14400 # Node ID 6a4a6c499e89c1c4aa36cdaf0ca9b6926b2b7e86 # Parent 0ef29304c2b4cbdbbbe4df183380405178551762 8114823: G1 doesn't honor request to disable class unloading Reviewed-by: tschatzl diff -r 0ef29304c2b4 -r 6a4a6c499e89 src/share/vm/gc_implementation/g1/g1MarkSweep.cpp --- a/src/share/vm/gc_implementation/g1/g1MarkSweep.cpp Tue Jul 17 17:07:44 2018 +0200 +++ b/src/share/vm/gc_implementation/g1/g1MarkSweep.cpp Fri Jul 20 03:07:49 2018 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2018, 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 @@ -135,9 +135,16 @@ MarkingCodeBlobClosure follow_code_closure(&GenMarkSweep::follow_root_closure, !CodeBlobToOopClosure::FixRelocations); { G1RootProcessor root_processor(g1h); - 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); + } } // Process reference objects found during marking @@ -158,15 +165,17 @@ // This is the point where the entire marking should have completed. assert(GenMarkSweep::_marking_stack.is_empty(), "Marking should have completed"); - // Unload classes and purge the SystemDictionary. - bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive); + if (ClassUnloading) { - // Unload nmethods. - CodeCache::do_unloading(&GenMarkSweep::is_alive, purged_class); + // Unload classes and purge the SystemDictionary. + bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive); - // Prune dead klasses from subklass/sibling/implementor lists. - Klass::clean_weak_klass_links(&GenMarkSweep::is_alive); + // Unload nmethods. + CodeCache::do_unloading(&GenMarkSweep::is_alive, purged_class); + // Prune dead klasses from subklass/sibling/implementor lists. + Klass::clean_weak_klass_links(&GenMarkSweep::is_alive); + } // Delete entries for dead interned string and clean up unreferenced symbols in symbol table. G1CollectedHeap::heap()->unlink_string_and_symbol_table(&GenMarkSweep::is_alive); diff -r 0ef29304c2b4 -r 6a4a6c499e89 src/share/vm/gc_implementation/g1/g1RootProcessor.cpp --- a/src/share/vm/gc_implementation/g1/g1RootProcessor.cpp Tue Jul 17 17:07:44 2018 +0200 +++ b/src/share/vm/gc_implementation/g1/g1RootProcessor.cpp Fri Jul 20 03:07:49 2018 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2018, 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 @@ -156,7 +156,7 @@ } process_vm_roots(strong_roots, weak_roots, phase_times, worker_i); - + process_string_table_roots(weak_roots, phase_times, worker_i); { // Now the CM ref_processor roots. G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CMRefRoots, worker_i); @@ -223,18 +223,34 @@ void G1RootProcessor::process_all_roots(OopClosure* oops, CLDClosure* clds, - CodeBlobClosure* blobs) { + CodeBlobClosure* blobs, + bool process_string_table) { process_java_roots(oops, NULL, clds, clds, NULL, NULL, 0); process_vm_roots(oops, oops, 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(oops, NULL, 0); + } + process_code_cache_roots(blobs, NULL, 0); _process_strong_tasks.all_tasks_completed(); } +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(OopClosure* strong_roots, CLDClosure* thread_stack_clds, CLDClosure* strong_clds, @@ -311,14 +327,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(OopClosure* weak_roots, G1GCPhaseTimes* phase_times, + uint worker_i) { + assert(weak_roots != 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(weak_roots); +} + +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); } } diff -r 0ef29304c2b4 -r 6a4a6c499e89 src/share/vm/gc_implementation/g1/g1RootProcessor.hpp --- a/src/share/vm/gc_implementation/g1/g1RootProcessor.hpp Tue Jul 17 17:07:44 2018 +0200 +++ b/src/share/vm/gc_implementation/g1/g1RootProcessor.hpp Fri Jul 20 03:07:49 2018 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2018, 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 @@ -34,6 +34,7 @@ class G1CollectedHeap; class G1GCPhaseTimes; class G1ParPushHeapRSClosure; +class G1RootClosures; class Monitor; class OopClosure; class SubTasksDone; @@ -71,6 +72,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(OopClosure* scan_non_heap_roots, CLDClosure* thread_stack_clds, CLDClosure* scan_strong_clds, @@ -84,6 +90,14 @@ G1GCPhaseTimes* phase_times, uint worker_i); + void process_string_table_roots(OopClosure* scan_non_heap_weak_roots, + G1GCPhaseTimes* phase_times, + uint worker_i); + + void process_code_cache_roots(CodeBlobClosure* code_closure, + G1GCPhaseTimes* phase_times, + uint worker_i); + public: G1RootProcessor(G1CollectedHeap* g1h); @@ -114,6 +128,13 @@ OopClosure* scan_non_heap_weak_roots, uint worker_i); + // 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); + // Inform the root processor about the number of worker threads void set_num_workers(int active_workers); }; diff -r 0ef29304c2b4 -r 6a4a6c499e89 src/share/vm/runtime/arguments.cpp --- a/src/share/vm/runtime/arguments.cpp Tue Jul 17 17:07:44 2018 +0200 +++ b/src/share/vm/runtime/arguments.cpp Fri Jul 20 03:07:49 2018 -0400 @@ -1377,11 +1377,6 @@ CFLS_LAB::modify_initialization(OldPLABSize, OldPLABWeight); } - if (!ClassUnloading) { - FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false); - FLAG_SET_CMDLINE(bool, ExplicitGCInvokesConcurrentAndUnloadsClasses, false); - } - if (PrintGCDetails && Verbose) { tty->print_cr("MarkStackSize: %uk MarkStackSizeMax: %uk", (unsigned int) (MarkStackSize / K), (uint) (MarkStackSizeMax / K)); @@ -1748,6 +1743,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); + } #else // INCLUDE_ALL_GCS assert(verify_serial_gc_flags(), "SerialGC unset"); #endif // INCLUDE_ALL_GCS diff -r 0ef29304c2b4 -r 6a4a6c499e89 test/gc/class_unloading/TestClassUnloadingDisabled.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/gc/class_unloading/TestClassUnloadingDisabled.java Fri Jul 20 03:07:49 2018 -0400 @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2018, 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 + * 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 /testlibrary /testlibrary/whitebox + * @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 com.oracle.java.testlibrary.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"; + + Asserts.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); + + Asserts.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(); + Asserts.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 { +}