1 /* 2 * Copyright (c) 2015, 2019, Red Hat, Inc. 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 */ 24 25 #include "precompiled.hpp" 26 27 #include "classfile/classLoaderDataGraph.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "code/codeCache.hpp" 31 #include "gc/shenandoah/shenandoahClosures.inline.hpp" 32 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp" 33 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp" 34 #include "gc/shenandoah/shenandoahHeap.hpp" 35 #include "gc/shenandoah/shenandoahPhaseTimings.hpp" 36 #include "gc/shenandoah/shenandoahStringDedup.hpp" 37 #include "gc/shenandoah/shenandoahTimingTracker.hpp" 38 #include "gc/shenandoah/shenandoahVMOperations.hpp" 39 #include "jfr/jfr.hpp" 40 #include "memory/iterator.hpp" 41 #include "memory/resourceArea.hpp" 42 #include "memory/universe.hpp" 43 #include "runtime/thread.hpp" 44 #include "services/management.hpp" 45 46 ShenandoahSerialRoot::ShenandoahSerialRoot(ShenandoahSerialRoot::OopsDo oops_do, ShenandoahPhaseTimings::GCParPhases phase) : 47 _oops_do(oops_do), _phase(phase) { 48 } 49 50 void ShenandoahSerialRoot::oops_do(OopClosure* cl, uint worker_id) { 51 if (_claimed.try_set()) { 52 ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times(); 53 ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id); 54 _oops_do(cl); 55 } 56 } 57 58 // Overwrite the second argument for SD::oops_do, don't include vm global oop storage. 59 static void system_dictionary_oops_do(OopClosure* cl) { 60 SystemDictionary::oops_do(cl, false); 61 } 62 63 ShenandoahSerialRoots::ShenandoahSerialRoots() : 64 _universe_root(&Universe::oops_do, ShenandoahPhaseTimings::UniverseRoots), 65 _object_synchronizer_root(&ObjectSynchronizer::oops_do, ShenandoahPhaseTimings::ObjectSynchronizerRoots), 66 _management_root(&Management::oops_do, ShenandoahPhaseTimings::ManagementRoots), 67 _system_dictionary_root(&system_dictionary_oops_do, ShenandoahPhaseTimings::SystemDictionaryRoots), 68 _jvmti_root(&JvmtiExport::oops_do, ShenandoahPhaseTimings::JVMTIRoots) { 69 } 70 71 void ShenandoahSerialRoots::oops_do(OopClosure* cl, uint worker_id) { 72 _universe_root.oops_do(cl, worker_id); 73 _object_synchronizer_root.oops_do(cl, worker_id); 74 _management_root.oops_do(cl, worker_id); 75 _system_dictionary_root.oops_do(cl, worker_id); 76 _jvmti_root.oops_do(cl, worker_id); 77 } 78 79 ShenandoahWeakSerialRoot::ShenandoahWeakSerialRoot(ShenandoahWeakSerialRoot::WeakOopsDo weak_oops_do, ShenandoahPhaseTimings::GCParPhases phase) : 80 _weak_oops_do(weak_oops_do), _phase(phase) { 81 } 82 83 void ShenandoahWeakSerialRoot::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) { 84 if (_claimed.try_set()) { 85 ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times(); 86 ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id); 87 _weak_oops_do(is_alive, keep_alive); 88 } 89 } 90 91 #if INCLUDE_JVMTI 92 ShenandoahJVMTIWeakRoot::ShenandoahJVMTIWeakRoot() : 93 ShenandoahWeakSerialRoot(&JvmtiExport::weak_oops_do, ShenandoahPhaseTimings::JVMTIWeakRoots) { 94 } 95 #endif // INCLUDE_JVMTI 96 97 #if INCLUDE_JFR 98 ShenandoahJFRWeakRoot::ShenandoahJFRWeakRoot() : 99 ShenandoahWeakSerialRoot(&Jfr::weak_oops_do, ShenandoahPhaseTimings::JFRWeakRoots) { 100 } 101 #endif // INCLUDE_JFR 102 103 void ShenandoahSerialWeakRoots::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) { 104 JVMTI_ONLY(_jvmti_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);) 105 JFR_ONLY(_jfr_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);) 106 } 107 108 void ShenandoahSerialWeakRoots::weak_oops_do(OopClosure* cl, uint worker_id) { 109 AlwaysTrueClosure always_true; 110 weak_oops_do(&always_true, cl, worker_id); 111 } 112 113 ShenandoahThreadRoots::ShenandoahThreadRoots(bool is_par) : _is_par(is_par) { 114 Threads::change_thread_claim_token(); 115 } 116 117 void ShenandoahThreadRoots::oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id) { 118 ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times(); 119 ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id); 120 ResourceMark rm; 121 Threads::possibly_parallel_oops_do(_is_par, oops_cl, code_cl); 122 } 123 124 void ShenandoahThreadRoots::threads_do(ThreadClosure* tc, uint worker_id) { 125 ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times(); 126 ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id); 127 ResourceMark rm; 128 Threads::possibly_parallel_threads_do(_is_par, tc); 129 } 130 131 ShenandoahThreadRoots::~ShenandoahThreadRoots() { 132 Threads::assert_all_threads_claimed(); 133 } 134 135 ShenandoahStringDedupRoots::ShenandoahStringDedupRoots() { 136 if (ShenandoahStringDedup::is_enabled()) { 137 StringDedup::gc_prologue(false); 138 } 139 } 140 141 ShenandoahStringDedupRoots::~ShenandoahStringDedupRoots() { 142 if (ShenandoahStringDedup::is_enabled()) { 143 StringDedup::gc_epilogue(); 144 } 145 } 146 147 void ShenandoahStringDedupRoots::oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) { 148 if (ShenandoahStringDedup::is_enabled()) { 149 ShenandoahStringDedup::parallel_oops_do(is_alive, keep_alive, worker_id); 150 } 151 } 152 153 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) : 154 _heap(ShenandoahHeap::heap()), 155 _phase(phase) { 156 assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint"); 157 _heap->phase_timings()->record_workers_start(_phase); 158 } 159 160 ShenandoahRootProcessor::~ShenandoahRootProcessor() { 161 assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint"); 162 _heap->phase_timings()->record_workers_end(_phase); 163 } 164 165 ShenandoahRootEvacuator::ShenandoahRootEvacuator(uint n_workers, 166 ShenandoahPhaseTimings::Phase phase, 167 bool include_concurrent_roots, 168 bool include_concurrent_code_roots) : 169 ShenandoahRootProcessor(phase), 170 _thread_roots(n_workers > 1), 171 _include_concurrent_roots(include_concurrent_roots), 172 _include_concurrent_code_roots(include_concurrent_code_roots) { 173 } 174 175 void ShenandoahRootEvacuator::roots_do(uint worker_id, OopClosure* oops) { 176 MarkingCodeBlobClosure blobsCl(oops, CodeBlobToOopClosure::FixRelocations); 177 ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops); 178 CodeBlobToOopClosure* codes_cl = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ? 179 static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) : 180 static_cast<CodeBlobToOopClosure*>(&blobsCl); 181 AlwaysTrueClosure always_true; 182 183 _serial_roots.oops_do(oops, worker_id); 184 _serial_weak_roots.weak_oops_do(oops, worker_id); 185 if (_include_concurrent_roots) { 186 CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong); 187 _vm_roots.oops_do<OopClosure>(oops, worker_id); 188 _cld_roots.cld_do(&clds, worker_id); 189 _weak_roots.oops_do<OopClosure>(oops, worker_id); 190 } 191 192 if (_include_concurrent_code_roots) { 193 _code_roots.code_blobs_do(codes_cl, worker_id); 194 _thread_roots.oops_do(oops, NULL, worker_id); 195 } else { 196 _thread_roots.oops_do(oops, codes_cl, worker_id); 197 } 198 199 _dedup_roots.oops_do(&always_true, oops, worker_id); 200 } 201 202 ShenandoahRootUpdater::ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase) : 203 ShenandoahRootProcessor(phase), 204 _thread_roots(n_workers > 1) { 205 } 206 207 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) : 208 ShenandoahRootProcessor(phase), 209 _thread_roots(n_workers > 1) { 210 assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only"); 211 } 212 213 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) { 214 CodeBlobToOopClosure code_blob_cl(oops, CodeBlobToOopClosure::FixRelocations); 215 ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops); 216 CodeBlobToOopClosure* adjust_code_closure = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ? 217 static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) : 218 static_cast<CodeBlobToOopClosure*>(&code_blob_cl); 219 CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong); 220 AlwaysTrueClosure always_true; 221 222 _serial_roots.oops_do(oops, worker_id); 223 _vm_roots.oops_do(oops, worker_id); 224 225 _thread_roots.oops_do(oops, NULL, worker_id); 226 _cld_roots.cld_do(&adjust_cld_closure, worker_id); 227 _code_roots.code_blobs_do(adjust_code_closure, worker_id); 228 229 _serial_weak_roots.weak_oops_do(oops, worker_id); 230 _weak_roots.oops_do<OopClosure>(oops, worker_id); 231 _dedup_roots.oops_do(&always_true, oops, worker_id); 232 } 233 234 ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() : 235 ShenandoahRootProcessor(ShenandoahPhaseTimings::_num_phases), 236 _thread_roots(false /*is par*/) { 237 } 238 239 void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) { 240 assert(Thread::current()->is_VM_thread(), "Only by VM thread"); 241 // Must use _claim_none to avoid interfering with concurrent CLDG iteration 242 CLDToOopClosure clds(oops, ClassLoaderData::_claim_none); 243 MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations); 244 ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL); 245 AlwaysTrueClosure always_true; 246 ResourceMark rm; 247 248 _serial_roots.oops_do(oops, 0); 249 _vm_roots.oops_do(oops, 0); 250 _cld_roots.cld_do(&clds, 0); 251 _thread_roots.threads_do(&tc_cl, 0); 252 _code_roots.code_blobs_do(&code, 0); 253 254 _serial_weak_roots.weak_oops_do(oops, 0); 255 _weak_roots.oops_do<OopClosure>(oops, 0); 256 _dedup_roots.oops_do(&always_true, oops, 0); 257 } 258 259 void ShenandoahHeapIterationRootScanner::strong_roots_do(OopClosure* oops) { 260 assert(Thread::current()->is_VM_thread(), "Only by VM thread"); 261 // Must use _claim_none to avoid interfering with concurrent CLDG iteration 262 CLDToOopClosure clds(oops, ClassLoaderData::_claim_none); 263 MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations); 264 ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL); 265 ResourceMark rm; 266 267 _serial_roots.oops_do(oops, 0); 268 _vm_roots.oops_do(oops, 0); 269 _cld_roots.always_strong_cld_do(&clds, 0); 270 _thread_roots.threads_do(&tc_cl, 0); 271 }