1 /*
  2  * Copyright (c) 2015, 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 #include "precompiled.hpp"
 26 
 27 #include "classfile/classLoaderData.hpp"
 28 #include "code/nmethod.hpp"
 29 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 30 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 32 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 33 #include "gc/shenandoah/shenandoahStackWatermark.hpp"
 34 #include "memory/iterator.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "runtime/stackWatermarkSet.inline.hpp"
 37 #include "runtime/thread.hpp"
 38 
 39 ShenandoahJavaThreadsIterator::ShenandoahJavaThreadsIterator(ShenandoahPhaseTimings::Phase phase, uint n_workers) :
 40   _threads(),
 41   _length(_threads.length()),
 42   _stride(MAX2(1u, _length / n_workers / _chunks_per_worker)),
 43   _claimed(0),
 44   _phase(phase) {
 45 }
 46 
 47 uint ShenandoahJavaThreadsIterator::claim() {
 48   return Atomic::fetch_and_add(&_claimed, _stride, memory_order_relaxed);
 49 }
 50 
 51 void ShenandoahJavaThreadsIterator::threads_do(ThreadClosure* cl, uint worker_id) {
 52   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 53   for (uint i = claim(); i < _length; i = claim()) {
 54     for (uint t = i; t < MIN2(_length, i + _stride); t++) {
 55       cl->do_thread(thread_at(t));
 56     }
 57   }
 58 }
 59 
 60 ShenandoahThreadRoots::ShenandoahThreadRoots(ShenandoahPhaseTimings::Phase phase, bool is_par) :
 61   _phase(phase), _is_par(is_par) {
 62   Threads::change_thread_claim_token();
 63 }
 64 
 65 void ShenandoahThreadRoots::oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id) {
 66   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 67   ResourceMark rm;
 68   Threads::possibly_parallel_oops_do(_is_par, oops_cl, code_cl);
 69 }
 70 
 71 void ShenandoahThreadRoots::threads_do(ThreadClosure* tc, uint worker_id) {
 72   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 73   ResourceMark rm;
 74   Threads::possibly_parallel_threads_do(_is_par, tc);
 75 }
 76 
 77 ShenandoahThreadRoots::~ShenandoahThreadRoots() {
 78   Threads::assert_all_threads_claimed();
 79 }
 80 
 81 ShenandoahCodeCacheRoots::ShenandoahCodeCacheRoots(ShenandoahPhaseTimings::Phase phase) : _phase(phase) {
 82   nmethod::oops_do_marking_prologue();
 83 }
 84 
 85 void ShenandoahCodeCacheRoots::code_blobs_do(CodeBlobClosure* blob_cl, uint worker_id) {
 86   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
 87   _coderoots_iterator.possibly_parallel_blobs_do(blob_cl);
 88 }
 89 
 90 ShenandoahCodeCacheRoots::~ShenandoahCodeCacheRoots() {
 91   nmethod::oops_do_marking_epilogue();
 92 }
 93 
 94 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) :
 95   _heap(ShenandoahHeap::heap()),
 96   _phase(phase),
 97   _worker_phase(phase) {
 98 }
 99 
100 ShenandoahRootScanner::ShenandoahRootScanner(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
101   ShenandoahRootProcessor(phase),
102   _thread_roots(phase, n_workers > 1) {
103   nmethod::oops_do_marking_prologue();
104 }
105 
106 ShenandoahRootScanner::~ShenandoahRootScanner() {
107   nmethod::oops_do_marking_epilogue();
108 }
109 
110 void ShenandoahRootScanner::roots_do(uint worker_id, OopClosure* oops) {
111   MarkingCodeBlobClosure blobs_cl(oops, !CodeBlobToOopClosure::FixRelocations, true /*FIXME*/);
112   roots_do(worker_id, oops, &blobs_cl);
113 }
114 
115 void ShenandoahRootScanner::roots_do(uint worker_id, OopClosure* oops, CodeBlobClosure* code, ThreadClosure *tc) {
116   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
117 
118   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, code, tc);
119   ResourceMark rm;
120   _thread_roots.threads_do(&tc_cl, worker_id);
121 }
122 
123 ShenandoahSTWRootScanner::ShenandoahSTWRootScanner(ShenandoahPhaseTimings::Phase phase) :
124    ShenandoahRootProcessor(phase),
125    _thread_roots(phase, ShenandoahHeap::heap()->workers()->active_workers() > 1),
126    _code_roots(phase),
127    _cld_roots(phase, ShenandoahHeap::heap()->workers()->active_workers(), false /*heap iteration*/),
128    _vm_roots(phase),
129    _unload_classes(ShenandoahHeap::heap()->unload_classes()) {
130 }
131 
132 class ShenandoahConcurrentMarkThreadClosure : public ThreadClosure {
133 private:
134   OopClosure* const _oops;
135 
136 public:
137   ShenandoahConcurrentMarkThreadClosure(OopClosure* oops);
138   void do_thread(Thread* thread);
139 };
140 
141 ShenandoahConcurrentMarkThreadClosure::ShenandoahConcurrentMarkThreadClosure(OopClosure* oops) :
142   _oops(oops) {
143 }
144 
145 void ShenandoahConcurrentMarkThreadClosure::do_thread(Thread* thread) {
146   assert(thread->is_Java_thread(), "Must be");
147   JavaThread* const jt = JavaThread::cast(thread);
148 
149   StackWatermarkSet::finish_processing(jt, _oops, StackWatermarkKind::gc);
150 }
151 
152 ShenandoahConcurrentRootScanner::ShenandoahConcurrentRootScanner(uint n_workers,
153                                                                  ShenandoahPhaseTimings::Phase phase) :
154    ShenandoahRootProcessor(phase),
155    _java_threads(phase, n_workers),
156   _vm_roots(phase),
157   _cld_roots(phase, n_workers, false /*heap iteration*/),
158   _codecache_snapshot(NULL),
159   _phase(phase) {
160   if (!ShenandoahHeap::heap()->unload_classes()) {
161     CodeCache_lock->lock_without_safepoint_check();
162     _codecache_snapshot = ShenandoahCodeRoots::table()->snapshot_for_iteration();
163   }
164   update_tlab_stats();
165   assert(!ShenandoahHeap::heap()->has_forwarded_objects(), "Not expecting forwarded pointers during concurrent marking");
166 }
167 
168 ShenandoahConcurrentRootScanner::~ShenandoahConcurrentRootScanner() {
169   if (!ShenandoahHeap::heap()->unload_classes()) {
170     ShenandoahCodeRoots::table()->finish_iteration(_codecache_snapshot);
171     CodeCache_lock->unlock();
172   }
173 }
174 
175 void ShenandoahConcurrentRootScanner::roots_do(OopClosure* oops, uint worker_id) {
176   ShenandoahHeap* const heap = ShenandoahHeap::heap();
177   CLDToOopClosure clds_cl(oops, ClassLoaderData::_claim_strong);
178 
179   // Process light-weight/limited parallel roots then
180   _vm_roots.oops_do(oops, worker_id);
181 
182   if (heap->unload_classes()) {
183     _cld_roots.always_strong_cld_do(&clds_cl, worker_id);
184   } else {
185     _cld_roots.cld_do(&clds_cl, worker_id);
186 
187     {
188       ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
189       CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
190       _codecache_snapshot->parallel_blobs_do(&blobs);
191     }
192   }
193 
194   // Process heavy-weight/fully parallel roots the last
195   ShenandoahConcurrentMarkThreadClosure thr_cl(oops);
196   _java_threads.threads_do(&thr_cl, worker_id);
197 }
198 
199 void ShenandoahConcurrentRootScanner::update_tlab_stats() {
200   if (UseTLAB) {
201     ThreadLocalAllocStats total;
202     for (uint i = 0; i < _java_threads.length(); i ++) {
203       Thread* thr = _java_threads.thread_at(i);
204       if (thr->is_Java_thread()) {
205         ShenandoahStackWatermark* wm = StackWatermarkSet::get<ShenandoahStackWatermark>(JavaThread::cast(thr), StackWatermarkKind::gc);
206         total.update(wm->stats());
207       }
208     }
209     total.publish();
210   }
211 }
212 
213 ShenandoahRootUpdater::ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
214   ShenandoahRootProcessor(phase),
215   _vm_roots(phase),
216   _cld_roots(phase, n_workers, false /*heap iteration*/),
217   _thread_roots(phase, n_workers > 1),
218   _weak_roots(phase),
219   _code_roots(phase) {
220 }
221 
222 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
223   ShenandoahRootProcessor(phase),
224   _vm_roots(phase),
225   _cld_roots(phase, n_workers, false /*heap iteration*/),
226   _thread_roots(phase, n_workers > 1),
227   _weak_roots(phase),
228   _code_roots(phase) {
229   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
230 }
231 
232 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
233   CodeBlobToOopClosure code_blob_cl(oops, CodeBlobToOopClosure::FixRelocations);
234   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops);
235   CodeBlobToOopClosure* adjust_code_closure = (ClassUnloading && ShenandoahNMethodBarrier) ?
236                                               static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
237                                               static_cast<CodeBlobToOopClosure*>(&code_blob_cl);
238   CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong);
239   AlwaysTrueClosure always_true;
240 
241   // Process light-weight/limited parallel roots then
242   _vm_roots.oops_do(oops, worker_id);
243   _weak_roots.oops_do<OopClosure>(oops, worker_id);
244   _cld_roots.cld_do(&adjust_cld_closure, worker_id);
245 
246   // Process heavy-weight/fully parallel roots the last
247   _code_roots.code_blobs_do(adjust_code_closure, worker_id);
248   _thread_roots.oops_do(oops, NULL, worker_id);
249 }
250 
251 ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner(uint n_workers) :
252    ShenandoahRootProcessor(ShenandoahPhaseTimings::heap_iteration_roots),
253    _thread_roots(ShenandoahPhaseTimings::heap_iteration_roots, false /*is par*/),
254    _vm_roots(ShenandoahPhaseTimings::heap_iteration_roots),
255    _cld_roots(ShenandoahPhaseTimings::heap_iteration_roots, n_workers, true /*heap iteration*/),
256    _weak_roots(ShenandoahPhaseTimings::heap_iteration_roots),
257    _code_roots(ShenandoahPhaseTimings::heap_iteration_roots) {
258  }
259 
260  void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) {
261    // Must use _claim_other to avoid interfering with concurrent CLDG iteration
262    CLDToOopClosure clds(oops, ClassLoaderData::_claim_other);
263    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations, true /*FIXME*/);
264    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
265    AlwaysTrueClosure always_true;
266 
267    ResourceMark rm;
268 
269    // Process light-weight/limited parallel roots then
270    _vm_roots.oops_do(oops, 0);
271    _weak_roots.oops_do<OopClosure>(oops, 0);
272    _cld_roots.cld_do(&clds, 0);
273 
274    // Process heavy-weight/fully parallel roots the last
275    _code_roots.code_blobs_do(&code, 0);
276    _thread_roots.threads_do(&tc_cl, 0);
277  }