1 /* 2 * Copyright (c) 2015, 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "classfile/classLoaderData.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "code/codeCache.hpp" 29 #include "gc_implementation/shenandoah/shenandoahClosures.inline.hpp" 30 #include "gc_implementation/shenandoah/shenandoahRootProcessor.inline.hpp" 31 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 32 #include "gc_implementation/shenandoah/shenandoahFreeSet.hpp" 33 #include "gc_implementation/shenandoah/shenandoahCollectorPolicy.hpp" 34 #include "gc_implementation/shenandoah/shenandoahPhaseTimings.hpp" 35 #include "gc_implementation/shenandoah/shenandoahStringDedup.hpp" 36 #include "gc_implementation/shenandoah/shenandoahSynchronizerIterator.hpp" 37 #include "gc_implementation/shenandoah/shenandoahWorkGroup.hpp" 38 #include "memory/allocation.inline.hpp" 39 #include "memory/resourceArea.hpp" 40 #include "runtime/fprofiler.hpp" 41 #include "runtime/thread.hpp" 42 #include "services/management.hpp" 43 44 #if INCLUDE_JFR 45 #include "jfr/leakprofiler/leakProfiler.hpp" 46 #endif 47 48 ShenandoahSerialRoot::ShenandoahSerialRoot(ShenandoahSerialRoot::OopsDo oops_do, ShenandoahPhaseTimings::Phase phase, ShenandoahPhaseTimings::ParPhase par_phase) : 49 _claimed(0), _oops_do(oops_do), _phase(phase), _par_phase(par_phase) { 50 } 51 52 void ShenandoahSerialRoot::oops_do(OopClosure* cl, uint worker_id) { 53 if (_claimed == 0 && Atomic::cmpxchg(1, &_claimed, 0) == 0) { 54 ShenandoahWorkerTimingsTracker timer(_phase, _par_phase, worker_id); 55 _oops_do(cl); 56 } 57 } 58 59 static void universe_oops_do(OopClosure* closure) { 60 Universe::oops_do(closure); 61 } 62 63 ShenandoahSerialRoots::ShenandoahSerialRoots(ShenandoahPhaseTimings::Phase phase) : 64 _phase(phase), 65 _universe_roots(&universe_oops_do, phase, ShenandoahPhaseTimings::UniverseRoots), 66 _management_roots(&Management::oops_do, phase, ShenandoahPhaseTimings::ManagementRoots), 67 _jvmti_roots(&JvmtiExport::oops_do, phase, ShenandoahPhaseTimings::JVMTIRoots), 68 _jni_handle_roots(&JNIHandles::oops_do, phase, ShenandoahPhaseTimings::JNIRoots), 69 _flat_profiler_roots(&FlatProfiler::oops_do, phase, ShenandoahPhaseTimings::FlatProfilerRoots) { 70 } 71 72 void ShenandoahSerialRoots::oops_do(OopClosure* cl, uint worker_id) { 73 _universe_roots.oops_do(cl, worker_id); 74 _management_roots.oops_do(cl, worker_id); 75 _jvmti_roots.oops_do(cl, worker_id); 76 _jni_handle_roots.oops_do(cl, worker_id); 77 _flat_profiler_roots.oops_do(cl, worker_id); 78 79 { 80 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ObjectSynchronizerRoots, worker_id); 81 while(_om_iterator.parallel_oops_do(cl)); 82 } 83 } 84 85 ShenandoahSystemDictionaryRoots::ShenandoahSystemDictionaryRoots(ShenandoahPhaseTimings::Phase phase) : 86 _phase(phase), _claimed(0) { 87 } 88 89 void ShenandoahSystemDictionaryRoots::strong_oops_do(OopClosure* oops, uint worker_id) { 90 if (_claimed == 0 && Atomic::cmpxchg(1, &_claimed, 0) == 0) { 91 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::SystemDictionaryRoots, worker_id); 92 SystemDictionary::roots_oops_do(oops, NULL); 93 } 94 } 95 96 void ShenandoahSystemDictionaryRoots::oops_do(OopClosure* oops, uint worker_id) { 97 if (_claimed == 0 && Atomic::cmpxchg(1, &_claimed, 0) == 0) { 98 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::SystemDictionaryRoots, worker_id); 99 SystemDictionary::roots_oops_do(oops, oops); 100 } 101 } 102 103 ShenandoahStringTableRoots::ShenandoahStringTableRoots(ShenandoahPhaseTimings::Phase phase) : 104 _phase(phase) 105 {} 106 107 void ShenandoahStringTableRoots::oops_do(OopClosure* oops, uint worker_id) { 108 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::StringTableRoots, worker_id); 109 StringTable::possibly_parallel_oops_do_shenandoah(oops); 110 } 111 112 ShenandoahThreadRoots::ShenandoahThreadRoots(ShenandoahPhaseTimings::Phase phase) : 113 _phase(phase) { 114 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 115 heap->set_par_threads(heap->workers()->active_workers()); 116 } 117 118 void ShenandoahThreadRoots::oops_do(OopClosure* oops_cl, CLDClosure* cld_cl, CodeBlobClosure* code_cl, uint worker_id) { 119 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id); 120 ResourceMark rm; 121 Threads::possibly_parallel_oops_do(oops_cl, cld_cl, code_cl); 122 } 123 124 ShenandoahWeakRoot::ShenandoahWeakRoot(ShenandoahPhaseTimings::Phase phase, ShenandoahPhaseTimings::ParPhase par_phase, ShenandoahWeakRoot::WeakOopsDo oops_do) : 125 _phase(phase), _par_phase(par_phase), _claimed(0), _weak_oops_do(oops_do) { 126 } 127 128 void ShenandoahWeakRoot::oops_do(OopClosure* keep_alive, uint worker_id) { 129 AlwaysTrueClosure always_true; 130 weak_oops_do(&always_true, keep_alive, worker_id); 131 } 132 133 void ShenandoahWeakRoot::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) { 134 if (_claimed == 0 && Atomic::cmpxchg(1, &_claimed, 0) == 0) { 135 ShenandoahWorkerTimingsTracker t(_phase, _par_phase, worker_id); 136 _weak_oops_do(is_alive, keep_alive); 137 } 138 } 139 140 ShenandoahWeakRoots::ShenandoahWeakRoots(ShenandoahPhaseTimings::Phase phase) : 141 #if INCLUDE_JFR 142 _jfr_weak_roots(phase, ShenandoahPhaseTimings::JFRWeakRoots, &LeakProfiler::oops_do), 143 #endif // INCLUDE_JFR 144 _jni_weak_roots(phase, ShenandoahPhaseTimings::JNIWeakRoots, &JNIHandles::weak_oops_do) { 145 } 146 147 void ShenandoahWeakRoots::oops_do(OopClosure* keep_alive, uint worker_id) { 148 JFR_ONLY(_jfr_weak_roots.oops_do(keep_alive, worker_id);) 149 _jni_weak_roots.oops_do(keep_alive, worker_id); 150 } 151 152 void ShenandoahWeakRoots::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) { 153 JFR_ONLY(_jfr_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);) 154 _jni_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id); 155 } 156 157 ShenandoahStringDedupRoots::ShenandoahStringDedupRoots(ShenandoahPhaseTimings::Phase phase) : _phase(phase) { 158 if (ShenandoahStringDedup::is_enabled()) { 159 ShenandoahStringDedup::clear_claimed(); 160 } 161 } 162 163 void ShenandoahStringDedupRoots::oops_do(OopClosure* oops, uint worker_id) { 164 if (ShenandoahStringDedup::is_enabled()) { 165 ShenandoahStringDedup::parallel_oops_do(_phase, oops, worker_id); 166 } 167 } 168 169 ShenandoahClassLoaderDataRoots::ShenandoahClassLoaderDataRoots(ShenandoahPhaseTimings::Phase phase) : 170 _phase(phase) { 171 ClassLoaderDataGraph::clear_claimed_marks(); 172 } 173 174 void ShenandoahClassLoaderDataRoots::cld_do(CLDClosure* clds, uint worker_id) { 175 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CLDGRoots, worker_id); 176 ClassLoaderDataGraph::roots_cld_do(clds, clds); 177 } 178 179 void ShenandoahClassLoaderDataRoots::always_strong_cld_do(CLDClosure* clds, uint worker_id) { 180 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CLDGRoots, worker_id); 181 ClassLoaderDataGraph::always_strong_cld_do(clds); 182 } 183 184 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) : 185 _srs(ShenandoahHeap::heap()), 186 _heap(ShenandoahHeap::heap()), 187 _phase(phase) { 188 assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint"); 189 _heap->phase_timings()->record_workers_start(_phase); 190 } 191 192 ShenandoahRootProcessor::~ShenandoahRootProcessor() { 193 assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint"); 194 _heap->phase_timings()->record_workers_end(_phase); 195 } 196 197 ShenandoahRootEvacuator::ShenandoahRootEvacuator(ShenandoahPhaseTimings::Phase phase) : 198 ShenandoahRootProcessor(phase), 199 _serial_roots(phase), 200 _dict_roots(phase), 201 _cld_roots(phase), 202 _thread_roots(phase), 203 _weak_roots(phase), 204 _dedup_roots(phase), 205 _string_table_roots(phase), 206 _code_roots(phase) 207 {} 208 209 ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() : 210 ShenandoahRootProcessor(ShenandoahPhaseTimings::heap_iteration_roots), 211 _serial_roots(ShenandoahPhaseTimings::heap_iteration_roots), 212 _dict_roots(ShenandoahPhaseTimings::heap_iteration_roots), 213 _thread_roots(ShenandoahPhaseTimings::heap_iteration_roots), 214 _cld_roots(ShenandoahPhaseTimings::heap_iteration_roots), 215 _weak_roots(ShenandoahPhaseTimings::heap_iteration_roots), 216 _dedup_roots(ShenandoahPhaseTimings::heap_iteration_roots), 217 _string_table_roots(ShenandoahPhaseTimings::heap_iteration_roots), 218 _code_roots(ShenandoahPhaseTimings::heap_iteration_roots) 219 {} 220 221 void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) { 222 assert(Thread::current()->is_VM_thread(), "Only by VM thread"); 223 // Must use _claim_none to avoid interfering with concurrent CLDG iteration 224 CLDToOopClosure clds(oops, false /* must claim */); 225 MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations); 226 ResourceMark rm; 227 228 _serial_roots.oops_do(oops, 0); 229 _dict_roots.oops_do(oops, 0); 230 _cld_roots.cld_do(&clds, 0); 231 _thread_roots.oops_do(oops, NULL, NULL, 0); 232 _code_roots.code_blobs_do(&code, 0); 233 234 _weak_roots.oops_do(oops, 0); 235 _string_table_roots.oops_do(oops, 0); 236 _dedup_roots.oops_do(oops, 0); 237 } 238 239 void ShenandoahRootEvacuator::roots_do(uint worker_id, OopClosure* oops) { 240 { 241 // Evacuate the PLL here so that the SurrogateLockerThread doesn't 242 // have to. SurrogateLockerThread can execute write barrier in VMOperation 243 // prolog. If the SLT runs into OOM during that evacuation, the VMOperation 244 // may deadlock. Doing this evacuation the first thing makes that critical 245 // OOM less likely to happen. It is a bit excessive to perform WB by all 246 // threads, but this guarantees the very first evacuation would be the PLL. 247 // 248 // This pre-evac can still silently fail with OOME here, and PLL would not 249 // get evacuated. This would mean next VMOperation would try to evac PLL in 250 // SLT thread. We make additional effort to recover from that OOME in SLT, 251 // see ShenandoahHeap::oom_during_evacuation(). It seems to be the lesser evil 252 // to do there, because we cannot trigger Full GC right here, when we are 253 // in another VMOperation. 254 255 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 256 assert(heap->is_evacuation_in_progress(), "only when evacuating"); 257 HeapWord* pll_addr = java_lang_ref_Reference::pending_list_lock_addr(); 258 oop pll; 259 if (UseCompressedOops) { 260 pll = oopDesc::load_decode_heap_oop((narrowOop *)pll_addr); 261 } else { 262 pll = oopDesc::load_decode_heap_oop((oop*)pll_addr); 263 } 264 if (!oopDesc::is_null(pll) && heap->in_collection_set(pll)) { 265 oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(pll); 266 if (pll == fwd) { 267 Thread *t = Thread::current(); 268 heap->evacuate_object(pll, t); 269 } 270 } 271 } 272 273 MarkingCodeBlobClosure blobsCl(oops, CodeBlobToOopClosure::FixRelocations); 274 CLDToOopClosure clds(oops); 275 276 _serial_roots.oops_do(oops, worker_id); 277 _dict_roots.oops_do(oops, worker_id); 278 _thread_roots.oops_do(oops, NULL, NULL, worker_id); 279 _cld_roots.cld_do(&clds, worker_id); 280 _code_roots.code_blobs_do(&blobsCl, worker_id); 281 282 _weak_roots.oops_do(oops, worker_id); 283 _dedup_roots.oops_do(oops, worker_id); 284 _string_table_roots.oops_do(oops, worker_id); 285 } 286 287 ShenandoahRootUpdater::ShenandoahRootUpdater(ShenandoahPhaseTimings::Phase phase) : 288 ShenandoahRootProcessor(phase), 289 _serial_roots(phase), 290 _dict_roots(phase), 291 _cld_roots(phase), 292 _thread_roots(phase), 293 _weak_roots(phase), 294 _dedup_roots(phase), 295 _string_table_roots(phase), 296 _code_roots(phase) 297 {} 298 299 void ShenandoahRootUpdater::roots_do(uint worker_id, BoolObjectClosure* is_alive, OopClosure* keep_alive) { 300 CodeBlobToOopClosure update_blobs(keep_alive, CodeBlobToOopClosure::FixRelocations); 301 CLDToOopClosure clds(keep_alive); 302 303 _serial_roots.oops_do(keep_alive, worker_id); 304 _dict_roots.oops_do(keep_alive, worker_id); 305 _thread_roots.oops_do(keep_alive, &clds, NULL, worker_id); 306 _cld_roots.cld_do(&clds, worker_id); 307 308 _code_roots.code_blobs_do(&update_blobs, worker_id); 309 310 _weak_roots.weak_oops_do(is_alive, keep_alive, worker_id); 311 _dedup_roots.oops_do(keep_alive, worker_id); 312 _string_table_roots.oops_do(keep_alive, worker_id); 313 } 314 315 316 ShenandoahRootAdjuster::ShenandoahRootAdjuster(ShenandoahPhaseTimings::Phase phase) : 317 ShenandoahRootProcessor(phase), 318 _serial_roots(phase), 319 _dict_roots(phase), 320 _cld_roots(phase), 321 _thread_roots(phase), 322 _weak_roots(phase), 323 _dedup_roots(phase), 324 _string_table_roots(phase), 325 _code_roots(phase) 326 { 327 assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only"); 328 } 329 330 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) { 331 CodeBlobToOopClosure adjust_code_closure(oops, CodeBlobToOopClosure::FixRelocations); 332 CLDToOopClosure adjust_cld_closure(oops); 333 334 _serial_roots.oops_do(oops, worker_id); 335 _dict_roots.oops_do(oops, worker_id); 336 _thread_roots.oops_do(oops, NULL, NULL, worker_id); 337 _cld_roots.always_strong_cld_do(&adjust_cld_closure, worker_id); 338 _code_roots.code_blobs_do(&adjust_code_closure, worker_id); 339 340 _weak_roots.oops_do(oops, worker_id); 341 _dedup_roots.oops_do(oops, worker_id); 342 _string_table_roots.oops_do(oops, worker_id); 343 }