1 /* 2 * Copyright (c) 2021, 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 26 #include "precompiled.hpp" 27 28 #include "gc/shared/strongRootsScope.hpp" 29 #include "gc/shared/taskTerminator.hpp" 30 #include "gc/shared/workerThread.hpp" 31 #include "gc/shenandoah/shenandoahClosures.inline.hpp" 32 #include "gc/shenandoah/shenandoahGeneration.hpp" 33 #include "gc/shenandoah/shenandoahMark.inline.hpp" 34 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp" 35 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp" 36 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp" 37 #include "gc/shenandoah/shenandoahSTWMark.hpp" 38 #include "gc/shenandoah/shenandoahVerifier.hpp" 39 40 template<GenerationMode GENERATION> 41 class ShenandoahInitMarkRootsClosure : public OopClosure { 42 private: 43 ShenandoahObjToScanQueue* const _queue; 44 ShenandoahMarkingContext* const _mark_context; 45 46 template <class T> 47 inline void do_oop_work(T* p); 48 49 public: 50 ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q); 51 52 void do_oop(narrowOop* p) { do_oop_work(p); } 53 void do_oop(oop* p) { do_oop_work(p); } 54 }; 55 56 template<GenerationMode GENERATION> 57 ShenandoahInitMarkRootsClosure<GENERATION>::ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) : 58 _queue(q), 59 _mark_context(ShenandoahHeap::heap()->marking_context()) { 60 } 61 62 template <GenerationMode GENERATION> 63 template <class T> 64 void ShenandoahInitMarkRootsClosure<GENERATION>::do_oop_work(T* p) { 65 // Only called from STW mark, should not be used to bootstrap old generation marking. 66 ShenandoahMark::mark_through_ref<T, GENERATION>(p, _queue, nullptr, _mark_context, false); 67 } 68 69 class ShenandoahSTWMarkTask : public WorkerTask { 70 private: 71 ShenandoahSTWMark* const _mark; 72 73 public: 74 ShenandoahSTWMarkTask(ShenandoahSTWMark* mark); 75 void work(uint worker_id); 76 }; 77 78 ShenandoahSTWMarkTask::ShenandoahSTWMarkTask(ShenandoahSTWMark* mark) : 79 WorkerTask("Shenandoah STW mark"), 80 _mark(mark) { 81 } 82 83 void ShenandoahSTWMarkTask::work(uint worker_id) { 84 ShenandoahParallelWorkerSession worker_session(worker_id); 85 _mark->mark_roots(worker_id); 86 _mark->finish_mark(worker_id); 87 } 88 89 ShenandoahSTWMark::ShenandoahSTWMark(ShenandoahGeneration* generation, bool full_gc) : 90 ShenandoahMark(generation), 91 _root_scanner(full_gc ? ShenandoahPhaseTimings::full_gc_mark : ShenandoahPhaseTimings::degen_gc_stw_mark), 92 _terminator(ShenandoahHeap::heap()->workers()->active_workers(), task_queues()), 93 _full_gc(full_gc) { 94 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a Shenandoah safepoint"); 95 } 96 97 void ShenandoahSTWMark::mark() { 98 // Weak reference processing 99 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 100 ShenandoahReferenceProcessor* rp = heap->active_generation()->ref_processor(); 101 rp->reset_thread_locals(); 102 rp->set_soft_reference_policy(heap->soft_ref_policy()->should_clear_all_soft_refs()); 103 104 // Init mark, do not expect forwarded pointers in roots 105 if (ShenandoahVerify) { 106 assert(Thread::current()->is_VM_thread(), "Must be"); 107 heap->verifier()->verify_roots_no_forwarded(); 108 } 109 110 uint nworkers = heap->workers()->active_workers(); 111 task_queues()->reserve(nworkers); 112 113 TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats()); 114 115 { 116 // Mark 117 if (_generation->generation_mode() == YOUNG) { 118 // But only scan the remembered set for young generation. 119 _generation->scan_remembered_set(); 120 } 121 122 StrongRootsScope scope(nworkers); 123 ShenandoahSTWMarkTask task(this); 124 heap->workers()->run_task(&task); 125 126 assert(task_queues()->is_empty(), "Should be empty"); 127 } 128 129 _generation->set_mark_complete(); 130 131 assert(task_queues()->is_empty(), "Should be empty"); 132 TASKQUEUE_STATS_ONLY(task_queues()->print_taskqueue_stats()); 133 TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats()); 134 } 135 136 void ShenandoahSTWMark::mark_roots(uint worker_id) { 137 switch (_generation->generation_mode()) { 138 case GLOBAL: { 139 ShenandoahInitMarkRootsClosure<GLOBAL> init_mark(task_queues()->queue(worker_id)); 140 _root_scanner.roots_do(&init_mark, worker_id); 141 break; 142 } 143 case YOUNG: { 144 ShenandoahInitMarkRootsClosure<YOUNG> init_mark(task_queues()->queue(worker_id)); 145 _root_scanner.roots_do(&init_mark, worker_id); 146 break; 147 } 148 default: 149 ShouldNotReachHere(); 150 } 151 } 152 153 void ShenandoahSTWMark::finish_mark(uint worker_id) { 154 ShenandoahPhaseTimings::Phase phase = _full_gc ? ShenandoahPhaseTimings::full_gc_mark : ShenandoahPhaseTimings::degen_gc_stw_mark; 155 ShenandoahWorkerTimingsTracker timer(phase, ShenandoahPhaseTimings::ParallelMark, worker_id); 156 ShenandoahReferenceProcessor* rp = ShenandoahHeap::heap()->active_generation()->ref_processor(); 157 StringDedup::Requests requests; 158 159 mark_loop(_generation->generation_mode(), 160 worker_id, &_terminator, rp, 161 false /* not cancellable */, 162 ShenandoahStringDedup::is_enabled() ? ALWAYS_DEDUP : NO_DEDUP, &requests); 163 } 164