1 /*
2 * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2021, 2022, Red Hat, Inc. All rights reserved.
4 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27
28 #include "gc/shared/barrierSetNMethod.hpp"
29 #include "gc/shared/collectorCounters.hpp"
30 #include "gc/shared/continuationGCSupport.inline.hpp"
31 #include "gc/shenandoah/shenandoahBreakpoint.hpp"
32 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
34 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
35 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
36 #include "gc/shenandoah/shenandoahFreeSet.hpp"
37 #include "gc/shenandoah/shenandoahGeneration.hpp"
38 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
39 #include "gc/shenandoah/shenandoahLock.hpp"
40 #include "gc/shenandoah/shenandoahMark.inline.hpp"
41 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
42 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
43 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
44 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
45 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
46 #include "gc/shenandoah/shenandoahStackWatermark.hpp"
47 #include "gc/shenandoah/shenandoahUtils.hpp"
48 #include "gc/shenandoah/shenandoahVerifier.hpp"
49 #include "gc/shenandoah/shenandoahVMOperations.hpp"
50 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
51 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
52 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
53 #include "memory/allocation.hpp"
54 #include "prims/jvmtiTagMap.hpp"
55 #include "runtime/vmThread.hpp"
56 #include "utilities/events.hpp"
57
58 // Breakpoint support
59 class ShenandoahBreakpointGCScope : public StackObj {
60 private:
61 const GCCause::Cause _cause;
62 public:
63 ShenandoahBreakpointGCScope(GCCause::Cause cause) : _cause(cause) {
64 if (cause == GCCause::_wb_breakpoint) {
65 ShenandoahBreakpoint::start_gc();
66 ShenandoahBreakpoint::at_before_gc();
67 }
68 }
69
70 ~ShenandoahBreakpointGCScope() {
71 if (_cause == GCCause::_wb_breakpoint) {
72 ShenandoahBreakpoint::at_after_gc();
73 }
74 }
75 };
76
77 class ShenandoahBreakpointMarkScope : public StackObj {
78 private:
79 const GCCause::Cause _cause;
80 public:
81 ShenandoahBreakpointMarkScope(GCCause::Cause cause) : _cause(cause) {
82 if (_cause == GCCause::_wb_breakpoint) {
83 ShenandoahBreakpoint::at_after_marking_started();
84 }
85 }
86
87 ~ShenandoahBreakpointMarkScope() {
88 if (_cause == GCCause::_wb_breakpoint) {
89 ShenandoahBreakpoint::at_before_marking_completed();
90 }
91 }
92 };
93
94 ShenandoahConcurrentGC::ShenandoahConcurrentGC(ShenandoahGeneration* generation, bool do_old_gc_bootstrap) :
95 ShenandoahGC(generation),
96 _mark(generation),
97 _degen_point(ShenandoahDegenPoint::_degenerated_unset),
98 _abbreviated(false),
99 _do_old_gc_bootstrap(do_old_gc_bootstrap) {
100 }
101
102 ShenandoahGC::ShenandoahDegenPoint ShenandoahConcurrentGC::degen_point() const {
103 return _degen_point;
104 }
105
106 void ShenandoahConcurrentGC::entry_concurrent_update_refs_prepare(ShenandoahHeap* const heap) {
107 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
108 const char* msg = conc_init_update_refs_event_message();
109 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_update_refs_prepare);
110 EventMark em("%s", msg);
111
112 heap->try_inject_pin();
113 // Evacuation is complete, retire gc labs and change gc state
114 heap->concurrent_prepare_for_update_refs();
115 }
116
117 void ShenandoahConcurrentGC::entry_update_card_table() {
118 ShenandoahHeap* const heap = ShenandoahHeap::heap();
119 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
120
121 static const char* msg = "Concurrent update cards";
122 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_update_card_table);
123 EventMark em("%s", msg);
124
125 ShenandoahWorkerScope scope(heap->workers(),
126 ShenandoahWorkerPolicy::calc_workers_for_conc_evac(),
127 "concurrent update cards");
128
129 heap->try_inject_pin();
130 // Heap needs to be parsable here.
131 // Also, parallel heap region iterate must have a phase set.
132 assert(ShenandoahTimingsTracker::is_current_phase_valid(), "Current phase must be set");
133 ShenandoahGenerationalHeap::heap()->old_generation()->update_card_table();
134 }
135
136 bool ShenandoahConcurrentGC::collect(GCCause::Cause cause) {
137 ShenandoahHeap* const heap = ShenandoahHeap::heap();
138 _generation->ref_processor()->set_soft_reference_policy(
139 GCCause::should_clear_all_soft_refs(cause));
140
141 ShenandoahBreakpointGCScope breakpoint_gc_scope(cause);
142
143 // Reset for upcoming marking
144 entry_reset();
145
146 // Start initial mark under STW
147 vmop_entry_init_mark();
148
149 {
150 ShenandoahBreakpointMarkScope breakpoint_mark_scope(cause);
151
152 // Reset task queue stats here, rather than in mark_concurrent_roots,
153 // because remembered set scan will `push` oops into the queues and
154 // resetting after this happens will lose those counts.
155 TASKQUEUE_STATS_ONLY(_mark.task_queues()->reset_taskqueue_stats());
156
157 // Concurrent remembered set scanning
158 entry_scan_remembered_set();
159
160 // Concurrent mark roots
161 entry_mark_roots();
162 if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_roots)) {
163 return false;
164 }
165
166 // Continue concurrent mark
167 entry_mark();
168 if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_mark)) {
169 return false;
170 }
171 }
172
173 // Complete marking under STW, and start evacuation
174 vmop_entry_final_mark();
175
176 // If the GC was cancelled before final mark, nothing happens on the safepoint. We are still
177 // in the marking phase and must resume the degenerated cycle from there. If the GC was cancelled
178 // after final mark, then we've entered the evacuation phase and must resume the degenerated cycle
179 // from that phase.
180 if (_generation->is_concurrent_mark_in_progress()) {
181 bool cancelled = check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_mark);
182 assert(cancelled, "GC must have been cancelled between concurrent and final mark");
183 return false;
184 }
185
186 assert(heap->is_concurrent_weak_root_in_progress(), "Must be doing weak roots now");
187
188 // Finish all thread/stack roots if needed. This completes stack watermark processing.
189 if (heap->is_evacuation_in_progress()) {
190 entry_thread_roots();
191 }
192
193 // Process weak roots that might still point to regions that would be broken by cleanup.
194 // We cannot recycle regions because weak roots need to know what is marked in trashed regions.
195 entry_weak_refs();
196 entry_weak_roots();
197
198 // Perform concurrent class unloading before any regions get recycled. Class unloading may
199 // need to inspect unmarked objects in trashed regions.
200 if (heap->unload_classes()) {
201 entry_class_unloading();
202 }
203
204 // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
205 // the space. This would be the last action if there is nothing to evacuate. Note that
206 // we will not age young-gen objects in the case that we skip evacuation.
207 entry_cleanup_early();
208
209 // Processing strong roots
210 // This may be skipped if there is nothing to update/evacuate.
211 // If so, strong_root_in_progress would be unset.
212 if (heap->is_concurrent_strong_root_in_progress()) {
213 entry_strong_roots();
214 }
215
216 // Roots processing is complete, put the weak roots flag down.
217 vmop_entry_final_roots();
218
219 // Continue the cycle with evacuation and optional update-refs.
220 // This may be skipped if there is nothing to evacuate.
221 // If so, evac_in_progress would be unset by collection set preparation code.
222 if (heap->is_evacuation_in_progress()) {
223 // Concurrently evacuate
224 entry_evacuate();
225 if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_evac)) {
226 return false;
227 }
228
229 // Perform update-refs phase.
230 entry_concurrent_update_refs_prepare(heap);
231
232 if (ShenandoahHeap::heap()->mode()->is_generational()) {
233 entry_update_card_table();
234 }
235
236 if (ShenandoahVerify) {
237 vmop_entry_init_update_refs();
238 }
239
240 entry_update_refs();
241 if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_update_refs)) {
242 return false;
243 }
244
245 // Concurrent update thread roots
246 entry_update_thread_roots();
247 if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_update_refs)) {
248 return false;
249 }
250
251 vmop_entry_final_update_refs();
252
253 // Update references freed up collection set, kick the cleanup to reclaim the space.
254 entry_cleanup_complete();
255 } else {
256 _abbreviated = true;
257
258 if (heap->mode()->is_generational()) {
259 entry_complete_abbreviated_cycle();
260
261 // If the promote-in-place operation was cancelled, we can have the degenerated
262 // cycle complete the operation. It will see that no evacuations are in progress,
263 // and that there are regions wanting promotion. The risk with not handling the
264 // cancellation would be failing to restore top for these regions and leaving
265 // them unable to serve allocations for the old generation.
266 if (check_cancellation_and_abort(ShenandoahDegenPoint::_degenerated_evac)) {
267 return false;
268 }
269 }
270
271 // In normal cycle, final-update-refs would verify at the end of the cycle.
272 // In abbreviated cycle, we need to verify separately.
273 if (ShenandoahVerify) {
274 vmop_entry_final_verify();
275 }
276 }
277
278 // We defer generation resizing actions until after cset regions have been recycled. We do this even following an
279 // abbreviated cycle.
280 if (heap->mode()->is_generational()) {
281 ShenandoahGenerationalHeap::heap()->complete_concurrent_cycle();
282 }
283
284 // Instead of always resetting immediately before the start of a new GC, we can often reset at the end of the
285 // previous GC. This allows us to start the next GC cycle more quickly after a trigger condition is detected,
286 // reducing the likelihood that GC will degenerate.
287 entry_reset_after_collect();
288
289 return true;
290 }
291
292 void ShenandoahConcurrentGC::entry_complete_abbreviated_cycle() {
293 shenandoah_assert_generational();
294
295 ShenandoahGenerationalHeap* const heap = ShenandoahGenerationalHeap::heap();
296
297 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
298 static const char* msg = "Concurrent complete abbreviated cycle";
299 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::complete_abbreviated);
300 EventMark em("%s", msg);
301
302 ShenandoahWorkerScope scope(heap->workers(),
303 ShenandoahWorkerPolicy::calc_workers_for_conc_evac(),
304 msg);
305
306 heap->try_inject_pin();
307 // We chose not to evacuate because we found sufficient immediate garbage.
308 // However, there may still be regions to promote in place, so do that now.
309 if (heap->old_generation()->has_in_place_promotions()) {
310 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::complete_abbreviated_promote_in_place);
311 ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::complete_abbreviated_promote_in_place);
312 heap->promote_regions_in_place(_generation, true);
313 }
314
315 // At this point, the cycle is effectively complete. If the cycle has been cancelled here,
316 // the control thread will detect it on its next iteration and run a degenerated young cycle.
317 if (!heap->cancelled_gc() && !_generation->is_old()) {
318 ShenandoahTimingsTracker tracker(ShenandoahPhaseTimings::complete_abbreviated_update_region_ages);
319 heap->update_region_ages(_generation->complete_marking_context());
320 }
321 }
322
323 void ShenandoahConcurrentGC::vmop_entry_init_mark() {
324 ShenandoahHeap* const heap = ShenandoahHeap::heap();
325 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
326 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::init_mark_gross);
327
328 heap->try_inject_alloc_failure();
329 VM_ShenandoahInitMark op(this);
330 VMThread::execute(&op); // jump to entry_init_mark() under safepoint
331 }
332
333 void ShenandoahConcurrentGC::vmop_entry_final_mark() {
334 ShenandoahHeap* const heap = ShenandoahHeap::heap();
335 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
336 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_mark_gross);
337
338 heap->try_inject_alloc_failure();
339 VM_ShenandoahFinalMarkStartEvac op(this);
340 VMThread::execute(&op); // jump to entry_final_mark under safepoint
341 heap->try_inject_pin();
342 }
343
344 void ShenandoahConcurrentGC::vmop_entry_init_update_refs() {
345 ShenandoahHeap* const heap = ShenandoahHeap::heap();
346 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
347 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::init_update_refs_gross);
348
349 heap->try_inject_alloc_failure();
350 heap->try_inject_pin();
351 VM_ShenandoahInitUpdateRefs op(this);
352 VMThread::execute(&op);
353 }
354
355 void ShenandoahConcurrentGC::vmop_entry_final_update_refs() {
356 ShenandoahHeap* const heap = ShenandoahHeap::heap();
357 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
358 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_update_refs_gross);
359
360 heap->try_inject_alloc_failure();
361 heap->try_inject_pin();
362 VM_ShenandoahFinalUpdateRefs op(this);
363 VMThread::execute(&op);
364 }
365
366 void ShenandoahConcurrentGC::vmop_entry_final_roots() {
367 ShenandoahHeap* const heap = ShenandoahHeap::heap();
368 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
369 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_roots_gross);
370
371 // This phase does not use workers, no need for setup
372 heap->try_inject_alloc_failure();
373 VM_ShenandoahFinalRoots op(this);
374 VMThread::execute(&op);
375 }
376
377 void ShenandoahConcurrentGC::vmop_entry_final_verify() {
378 ShenandoahHeap* const heap = ShenandoahHeap::heap();
379 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
380 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_verify_gross);
381
382 // This phase does not use workers, no need for setup
383 heap->try_inject_alloc_failure();
384 heap->try_inject_pin();
385 VM_ShenandoahFinalVerify op(this);
386 VMThread::execute(&op);
387 }
388
389 void ShenandoahConcurrentGC::entry_init_mark() {
390 const char* msg = init_mark_event_message();
391 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::init_mark);
392 EventMark em("%s", msg);
393
394 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
395 ShenandoahWorkerPolicy::calc_workers_for_init_marking(),
396 "init marking");
397
398 op_init_mark();
399 }
400
401 void ShenandoahConcurrentGC::entry_final_mark() {
402 const char* msg = final_mark_event_message();
403 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::final_mark);
404 EventMark em("%s", msg);
405
406 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
407 ShenandoahWorkerPolicy::calc_workers_for_final_marking(),
408 "final marking");
409
410 op_final_mark();
411 }
412
413 void ShenandoahConcurrentGC::entry_init_update_refs() {
414 static const char* msg = "Pause Init Update Refs";
415 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::init_update_refs);
416 EventMark em("%s", msg);
417
418 // No workers used in this phase, no setup required
419 op_init_update_refs();
420 }
421
422 void ShenandoahConcurrentGC::entry_final_update_refs() {
423 static const char* msg = "Pause Final Update Refs";
424 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::final_update_refs);
425 EventMark em("%s", msg);
426
427 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
428 ShenandoahWorkerPolicy::calc_workers_for_final_update_ref(),
429 "final reference update");
430
431 op_final_update_refs();
432 }
433
434 void ShenandoahConcurrentGC::entry_final_verify() {
435 const char* msg = verify_final_event_message();
436 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::final_verify);
437 EventMark em("%s", msg);
438
439 op_verify_final();
440 }
441
442 void ShenandoahConcurrentGC::entry_reset() {
443 ShenandoahHeap* const heap = ShenandoahHeap::heap();
444 heap->release_injected_pins();
445 heap->try_inject_alloc_failure();
446
447 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
448 {
449 const char* msg = conc_reset_event_message();
450 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_reset);
451 EventMark em("%s", msg);
452
453 ShenandoahWorkerScope scope(heap->workers(),
454 ShenandoahWorkerPolicy::calc_workers_for_conc_reset(),
455 msg);
456 op_reset();
457 }
458 }
459
460 void ShenandoahConcurrentGC::entry_scan_remembered_set() {
461 if (_generation->is_young()) {
462 ShenandoahHeap* const heap = ShenandoahHeap::heap();
463 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
464 const char* msg = "Concurrent remembered set scanning";
465 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::init_scan_rset);
466 EventMark em("%s", msg);
467
468 ShenandoahWorkerScope scope(heap->workers(),
469 ShenandoahWorkerPolicy::calc_workers_for_rs_scanning(),
470 msg);
471
472 heap->try_inject_alloc_failure();
473 _generation->scan_remembered_set(true /* is_concurrent */);
474 }
475 }
476
477 void ShenandoahConcurrentGC::entry_mark_roots() {
478 ShenandoahHeap* const heap = ShenandoahHeap::heap();
479 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
480 const char* msg = "Concurrent marking roots";
481 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_mark_roots);
482 EventMark em("%s", msg);
483
484 ShenandoahWorkerScope scope(heap->workers(),
485 ShenandoahWorkerPolicy::calc_workers_for_conc_marking(),
486 "concurrent marking roots");
487
488 heap->try_inject_alloc_failure();
489 op_mark_roots();
490 }
491
492 void ShenandoahConcurrentGC::entry_mark() {
493 ShenandoahHeap* const heap = ShenandoahHeap::heap();
494 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
495 const char* msg = conc_mark_event_message();
496 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_mark);
497 EventMark em("%s", msg);
498
499 ShenandoahWorkerScope scope(heap->workers(),
500 ShenandoahWorkerPolicy::calc_workers_for_conc_marking(),
501 "concurrent marking");
502
503 heap->try_inject_alloc_failure();
504 op_mark();
505 heap->try_inject_pin();
506 }
507
508 void ShenandoahConcurrentGC::entry_thread_roots() {
509 ShenandoahHeap* const heap = ShenandoahHeap::heap();
510 static const char* msg = "Concurrent thread roots";
511 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_thread_roots);
512 EventMark em("%s", msg);
513
514 ShenandoahWorkerScope scope(heap->workers(),
515 ShenandoahWorkerPolicy::calc_workers_for_conc_root_processing(),
516 msg);
517
518 heap->try_inject_alloc_failure();
519 heap->try_inject_pin();
520 op_thread_roots();
521 }
522
523 void ShenandoahConcurrentGC::entry_weak_refs() {
524 ShenandoahHeap* const heap = ShenandoahHeap::heap();
525 const char* msg = conc_weak_refs_event_message();
526 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_weak_refs);
527 EventMark em("%s", msg);
528
529 ShenandoahWorkerScope scope(heap->workers(),
530 ShenandoahWorkerPolicy::calc_workers_for_conc_refs_processing(),
531 "concurrent weak references");
532
533 heap->try_inject_alloc_failure();
534 heap->try_inject_pin();
535 op_weak_refs();
536 }
537
538 void ShenandoahConcurrentGC::entry_weak_roots() {
539 ShenandoahHeap* const heap = ShenandoahHeap::heap();
540 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
541 const char* msg = conc_weak_roots_event_message();
542 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_weak_roots);
543 EventMark em("%s", msg);
544
545 ShenandoahWorkerScope scope(heap->workers(),
546 ShenandoahWorkerPolicy::calc_workers_for_conc_root_processing(),
547 "concurrent weak root");
548
549 heap->try_inject_alloc_failure();
550 heap->try_inject_pin();
551 op_weak_roots();
552 }
553
554 void ShenandoahConcurrentGC::entry_class_unloading() {
555 ShenandoahHeap* const heap = ShenandoahHeap::heap();
556 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
557 static const char* msg = "Concurrent class unloading";
558 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_class_unload);
559 EventMark em("%s", msg);
560
561 ShenandoahWorkerScope scope(heap->workers(),
562 ShenandoahWorkerPolicy::calc_workers_for_conc_root_processing(),
563 "concurrent class unloading");
564
565 heap->try_inject_alloc_failure();
566 heap->try_inject_pin();
567 op_class_unloading();
568 }
569
570 void ShenandoahConcurrentGC::entry_strong_roots() {
571 ShenandoahHeap* const heap = ShenandoahHeap::heap();
572 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
573 static const char* msg = "Concurrent strong roots";
574 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_strong_roots);
575 EventMark em("%s", msg);
576
577 ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_strong_roots);
578
579 ShenandoahWorkerScope scope(heap->workers(),
580 ShenandoahWorkerPolicy::calc_workers_for_conc_root_processing(),
581 "concurrent strong root");
582
583 heap->try_inject_alloc_failure();
584 heap->try_inject_pin();
585 op_strong_roots();
586 }
587
588 void ShenandoahConcurrentGC::entry_cleanup_early() {
589 ShenandoahHeap* const heap = ShenandoahHeap::heap();
590 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
591 const char* msg = conc_cleanup_event_message();
592 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_cleanup_early, true /* log_heap_usage */);
593 EventMark em("%s", msg);
594
595 // This phase does not use workers, no need for setup
596 heap->try_inject_alloc_failure();
597 heap->try_inject_pin();
598 op_cleanup_early();
599 if (!heap->is_evacuation_in_progress()) {
600 // This is an abbreviated cycle. Rebuild the freeset in order to establish reserves for the next GC cycle. Doing
601 // the rebuild ASAP also expedites availability of immediate trash, reducing the likelihood that we will degenerate
602 // during promote-in-place processing.
603 heap->rebuild_free_set(true /*concurrent*/);
604 }
605 }
606
607 void ShenandoahConcurrentGC::entry_evacuate() {
608 ShenandoahHeap* const heap = ShenandoahHeap::heap();
609 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
610
611 static const char* msg = "Concurrent evacuation";
612 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_evac);
613 EventMark em("%s", msg);
614
615 ShenandoahWorkerScope scope(heap->workers(),
616 ShenandoahWorkerPolicy::calc_workers_for_conc_evac(),
617 "concurrent evacuation");
618
619 heap->try_inject_alloc_failure();
620 heap->try_inject_pin();
621 op_evacuate();
622 }
623
624 void ShenandoahConcurrentGC::entry_update_thread_roots() {
625 ShenandoahHeap* const heap = ShenandoahHeap::heap();
626 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
627
628 static const char* msg = "Concurrent update thread roots";
629 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_update_thread_roots);
630 EventMark em("%s", msg);
631
632 // No workers used in this phase, no setup required
633 heap->try_inject_alloc_failure();
634 heap->try_inject_pin();
635 op_update_thread_roots();
636 }
637
638 void ShenandoahConcurrentGC::entry_update_refs() {
639 ShenandoahHeap* const heap = ShenandoahHeap::heap();
640 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
641 static const char* msg = "Concurrent update references";
642 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_update_refs);
643 EventMark em("%s", msg);
644
645 ShenandoahWorkerScope scope(heap->workers(),
646 ShenandoahWorkerPolicy::calc_workers_for_conc_update_ref(),
647 "concurrent reference update");
648
649 heap->try_inject_alloc_failure();
650 heap->try_inject_pin();
651 op_update_refs();
652 }
653
654 void ShenandoahConcurrentGC::entry_cleanup_complete() {
655 ShenandoahHeap* const heap = ShenandoahHeap::heap();
656 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
657 const char* msg = conc_cleanup_event_message();
658 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_cleanup_complete, true /* log_heap_usage */);
659 EventMark em("%s", msg);
660
661 // This phase does not use workers, no need for setup
662 heap->try_inject_alloc_failure();
663 op_cleanup_complete();
664 }
665
666 void ShenandoahConcurrentGC::entry_reset_after_collect() {
667 ShenandoahHeap* const heap = ShenandoahHeap::heap();
668 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
669 const char* msg = conc_reset_after_collect_event_message();
670 ShenandoahConcurrentPhase gc_phase(msg, ShenandoahPhaseTimings::conc_reset_after_collect);
671 EventMark em("%s", msg);
672
673 op_reset_after_collect();
674 }
675
676 void ShenandoahConcurrentGC::op_reset() {
677 ShenandoahHeap* const heap = ShenandoahHeap::heap();
678
679 // If it is old GC bootstrap cycle, always clear bitmap for global gen
680 // to ensure bitmap for old gen is clear for old GC cycle after this.
681 if (_do_old_gc_bootstrap) {
682 assert(!heap->is_prepare_for_old_mark_in_progress(), "Cannot reset old without making it parsable");
683 heap->global_generation()->prepare_gc();
684 } else {
685 _generation->prepare_gc();
686 }
687
688 if (heap->mode()->is_generational()) {
689 heap->old_generation()->card_scan()->mark_read_table_as_clean();
690 }
691 }
692
693 class ShenandoahInitMarkUpdateRegionStateClosure : public ShenandoahHeapRegionClosure {
694 private:
695 ShenandoahMarkingContext* const _ctx;
696 public:
697 ShenandoahInitMarkUpdateRegionStateClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
698
699 void heap_region_do(ShenandoahHeapRegion* r) {
700 assert(!r->has_live(), "Region %zu should have no live data", r->index());
701 if (r->is_active()) {
702 // Check if region needs updating its TAMS. We have updated it already during concurrent
703 // reset, so it is very likely we don't need to do another write here. Since most regions
704 // are not "active", this path is relatively rare.
705 if (_ctx->top_at_mark_start(r) != r->top()) {
706 _ctx->capture_top_at_mark_start(r);
707 }
708 } else {
709 assert(_ctx->top_at_mark_start(r) == r->top(),
710 "Region %zu should already have correct TAMS", r->index());
711 }
712 }
713
714 bool is_thread_safe() { return true; }
715 };
716
717 void ShenandoahConcurrentGC::start_mark() {
718 _mark.start_mark();
719 }
720
721 void ShenandoahConcurrentGC::op_init_mark() {
722 ShenandoahHeap* const heap = ShenandoahHeap::heap();
723 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at safepoint");
724 assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
725
726 assert(_generation->is_bitmap_clear(), "need clear marking bitmap");
727 assert(!_generation->is_mark_complete(), "should not be complete");
728 assert(!heap->has_forwarded_objects(), "No forwarded objects on this path");
729
730 if (heap->mode()->is_generational()) {
731 if (_generation->is_global()) {
732 heap->old_generation()->cancel_gc();
733 }
734
735 {
736 // After we swap card table below, the write-table is all clean, and the read table holds
737 // cards dirty prior to the start of GC. Young and bootstrap collection will update
738 // the write card table as a side effect of remembered set scanning. Global collection will
739 // update the card table as a side effect of global marking of old objects.
740 ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_swap_rset);
741 _generation->swap_card_tables();
742 }
743 }
744
745 if (ShenandoahVerify) {
746 ShenandoahTimingsTracker v(ShenandoahPhaseTimings::init_mark_verify);
747 heap->verifier()->verify_before_concmark(_generation);
748 }
749
750 if (VerifyBeforeGC) {
751 Universe::verify();
752 }
753
754 _generation->set_concurrent_mark_in_progress(true);
755
756 start_mark();
757
758 if (_do_old_gc_bootstrap) {
759 shenandoah_assert_generational();
760 // Update region state for both young and old regions
761 ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_update_region_states);
762 ShenandoahInitMarkUpdateRegionStateClosure cl;
763 heap->parallel_heap_region_iterate(&cl);
764 heap->old_generation()->ref_processor()->reset_thread_locals();
765 } else {
766 // Update region state for only young regions
767 ShenandoahGCPhase phase(ShenandoahPhaseTimings::init_update_region_states);
768 ShenandoahInitMarkUpdateRegionStateClosure cl;
769 _generation->parallel_heap_region_iterate(&cl);
770 }
771
772 // Weak reference processing
773 ShenandoahReferenceProcessor* rp = _generation->ref_processor();
774 rp->reset_thread_locals();
775
776 // Make above changes visible to worker threads
777 OrderAccess::fence();
778
779 // Arm nmethods/stack for concurrent processing
780 ShenandoahCodeRoots::arm_nmethods();
781
782 {
783 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::init_propagate_gc_state);
784 heap->propagate_gc_state_to_all_threads();
785 }
786 }
787
788 void ShenandoahConcurrentGC::op_mark_roots() {
789 _mark.mark_concurrent_roots();
790 }
791
792 void ShenandoahConcurrentGC::op_mark() {
793 _mark.concurrent_mark();
794 }
795
796 void ShenandoahConcurrentGC::op_final_mark() {
797 ShenandoahHeap* const heap = ShenandoahHeap::heap();
798 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at safepoint");
799 assert(!heap->has_forwarded_objects(), "No forwarded objects on this path");
800
801 if (ShenandoahVerify) {
802 heap->verifier()->verify_roots_no_forwarded(_generation);
803 }
804
805 if (!heap->cancelled_gc()) {
806 _mark.finish_mark();
807 assert(!heap->cancelled_gc(), "STW mark cannot OOM");
808
809 // Notify JVMTI that the tagmap table will need cleaning.
810 JvmtiTagMap::set_needs_cleaning();
811
812 // The collection set is chosen by prepare_regions_and_collection_set(). Additionally, certain parameters have been
813 // established to govern the evacuation efforts that are about to begin. Refer to comments on reserve members in
814 // ShenandoahGeneration and ShenandoahOldGeneration for more detail.
815 _generation->prepare_regions_and_collection_set(true /*concurrent*/);
816
817 // Has to be done after cset selection
818 heap->prepare_concurrent_roots();
819
820 if (!heap->collection_set()->is_empty()) {
821 LogTarget(Debug, gc, cset) lt;
822 if (lt.is_enabled()) {
823 ResourceMark rm;
824 LogStream ls(lt);
825 heap->collection_set()->print_on(&ls);
826 }
827
828 if (ShenandoahVerify) {
829 ShenandoahTimingsTracker v(ShenandoahPhaseTimings::final_mark_verify);
830 heap->verifier()->verify_before_evacuation(_generation);
831 }
832
833 heap->set_evacuation_in_progress(true);
834 // From here on, we need to update references.
835 heap->set_has_forwarded_objects(true);
836 } else {
837 if (ShenandoahVerify) {
838 ShenandoahTimingsTracker v(ShenandoahPhaseTimings::final_mark_verify);
839 if (has_in_place_promotions(heap)) {
840 heap->verifier()->verify_after_concmark_with_promotions(_generation);
841 } else {
842 heap->verifier()->verify_after_concmark(_generation);
843 }
844 }
845 }
846 }
847
848 // Arm nmethods/stack for concurrent processing
849 ShenandoahCodeRoots::arm_nmethods();
850
851 {
852 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_mark_propagate_gc_state);
853 heap->propagate_gc_state_to_all_threads();
854 }
855 }
856
857 bool ShenandoahConcurrentGC::has_in_place_promotions(ShenandoahHeap* heap) {
858 return heap->mode()->is_generational() && heap->old_generation()->has_in_place_promotions();
859 }
860
861 class ShenandoahConcurrentEvacThreadClosure : public ThreadClosure {
862 private:
863 OopClosure* const _oops;
864 public:
865 explicit ShenandoahConcurrentEvacThreadClosure(OopClosure* oops) : _oops(oops) {}
866
867 void do_thread(Thread* thread) override {
868 JavaThread* const jt = JavaThread::cast(thread);
869 StackWatermarkSet::finish_processing(jt, _oops, StackWatermarkKind::gc);
870 }
871 };
872
873 class ShenandoahConcurrentEvacUpdateThreadTask : public WorkerTask {
874 private:
875 ShenandoahJavaThreadsIterator _java_threads;
876
877 public:
878 explicit ShenandoahConcurrentEvacUpdateThreadTask(uint n_workers) :
879 WorkerTask("Shenandoah Evacuate/Update Concurrent Thread Roots"),
880 _java_threads(ShenandoahPhaseTimings::conc_thread_roots, n_workers) {
881 }
882
883 void work(uint worker_id) override {
884 ShenandoahContextEvacuateUpdateRootsClosure oops_cl;
885 ShenandoahConcurrentEvacThreadClosure thr_cl(&oops_cl);
886 _java_threads.threads_do(&thr_cl, worker_id);
887 }
888 };
889
890 void ShenandoahConcurrentGC::op_thread_roots() {
891 const ShenandoahHeap* const heap = ShenandoahHeap::heap();
892 assert(heap->is_evacuation_in_progress(), "Checked by caller");
893 ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_thread_roots);
894 ShenandoahConcurrentEvacUpdateThreadTask task(heap->workers()->active_workers());
895 heap->workers()->run_task(&task);
896 }
897
898 void ShenandoahConcurrentGC::op_weak_refs() {
899 ShenandoahHeap* const heap = ShenandoahHeap::heap();
900 assert(heap->is_concurrent_weak_root_in_progress(), "Only during this phase");
901 // Concurrent weak refs processing
902 ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_weak_refs);
903 if (heap->gc_cause() == GCCause::_wb_breakpoint) {
904 ShenandoahBreakpoint::at_after_reference_processing_started();
905 }
906 _generation->ref_processor()->process_references(ShenandoahPhaseTimings::conc_weak_refs, heap->workers(), true /* concurrent */);
907 }
908
909 class ShenandoahEvacUpdateCleanupOopStorageRootsClosure : public BasicOopIterateClosure {
910 private:
911 ShenandoahHeap* const _heap;
912 ShenandoahGeneration* const _generation;
913 ShenandoahMarkingContext* const _mark_context;
914 bool _evac_in_progress;
915 Thread* const _thread;
916
917 public:
918 explicit ShenandoahEvacUpdateCleanupOopStorageRootsClosure(ShenandoahGeneration* generation);
919 void do_oop(oop* p);
920 void do_oop(narrowOop* p);
921 };
922
923 ShenandoahEvacUpdateCleanupOopStorageRootsClosure::ShenandoahEvacUpdateCleanupOopStorageRootsClosure(ShenandoahGeneration* generation) :
924 _heap(ShenandoahHeap::heap()),
925 _generation(generation),
926 _mark_context(ShenandoahHeap::heap()->marking_context()),
927 _evac_in_progress(ShenandoahHeap::heap()->is_evacuation_in_progress()),
928 _thread(Thread::current()) {
929 }
930
931 void ShenandoahEvacUpdateCleanupOopStorageRootsClosure::do_oop(oop* p) {
932 const oop obj = RawAccess<>::oop_load(p);
933 if (!CompressedOops::is_null(obj)) {
934 if (!_mark_context->is_marked(obj)) {
935 if (_generation->contains(obj)) {
936 // Note: The obj is dead here. Do not touch it, just clear.
937 ShenandoahHeap::atomic_clear_oop(p, obj);
938 }
939 } else if (_evac_in_progress && _heap->in_collection_set(obj)) {
940 oop resolved = ShenandoahForwarding::get_forwardee(obj);
941 if (resolved == obj) {
942 resolved = _heap->evacuate_object(obj, _thread);
943 }
944 shenandoah_assert_not_in_cset_except(p, resolved, _heap->cancelled_gc());
945 ShenandoahHeap::atomic_update_oop(resolved, p, obj);
946 }
947 }
948 }
949
950 void ShenandoahEvacUpdateCleanupOopStorageRootsClosure::do_oop(narrowOop* p) {
951 ShouldNotReachHere();
952 }
953
954 class ShenandoahIsCLDAliveClosure : public CLDClosure {
955 public:
956 void do_cld(ClassLoaderData* cld) {
957 cld->is_alive();
958 }
959 };
960
961 class ShenandoahIsNMethodAliveClosure: public NMethodClosure {
962 public:
963 void do_nmethod(nmethod* n) {
964 n->is_unloading();
965 }
966 };
967
968 // This task not only evacuates/updates marked weak roots, but also "null"
969 // dead weak roots.
970 class ShenandoahConcurrentWeakRootsEvacUpdateTask : public WorkerTask {
971 private:
972 ShenandoahVMWeakRoots<true /*concurrent*/> _vm_roots;
973
974 // Roots related to concurrent class unloading
975 ShenandoahClassLoaderDataRoots<true /* concurrent */>
976 _cld_roots;
977 ShenandoahConcurrentNMethodIterator _nmethod_itr;
978 ShenandoahGeneration* _generation;
979 ShenandoahPhaseTimings::Phase _phase;
980
981 public:
982 ShenandoahConcurrentWeakRootsEvacUpdateTask(ShenandoahGeneration* generation, ShenandoahPhaseTimings::Phase phase) :
983 WorkerTask("Shenandoah Evacuate/Update Concurrent Weak Roots"),
984 _vm_roots(phase),
985 _cld_roots(phase, ShenandoahHeap::heap()->workers()->active_workers(), false /*heap iteration*/),
986 _nmethod_itr(ShenandoahCodeRoots::table()),
987 _generation(generation),
988 _phase(phase) {}
989
990 ~ShenandoahConcurrentWeakRootsEvacUpdateTask() {
991 // Notify runtime data structures of potentially dead oops
992 _vm_roots.report_num_dead();
993 }
994
995 void work(uint worker_id) override {
996 ShenandoahConcurrentWorkerSession worker_session(worker_id);
997 SuspendibleThreadSetJoiner sts_join;
998 {
999 // jni_roots and weak_roots are OopStorage backed roots, concurrent iteration
1000 // may race against OopStorage::release() calls.
1001 ShenandoahEvacUpdateCleanupOopStorageRootsClosure cl(_generation);
1002 _vm_roots.oops_do(&cl, worker_id);
1003 }
1004
1005 // If we are going to perform concurrent class unloading later on, we need to
1006 // clean up the weak oops in CLD and determine nmethod's unloading state, so that we
1007 // can clean up immediate garbage sooner.
1008 if (ShenandoahHeap::heap()->unload_classes()) {
1009 // Applies ShenandoahIsCLDAlive closure to CLDs, native barrier will either null the
1010 // CLD's holder or evacuate it.
1011 {
1012 ShenandoahIsCLDAliveClosure is_cld_alive;
1013 _cld_roots.cld_do(&is_cld_alive, worker_id);
1014 }
1015
1016 // Applies ShenandoahIsNMethodAliveClosure to registered nmethods.
1017 // The closure calls nmethod->is_unloading(). The is_unloading
1018 // state is cached, therefore, during concurrent class unloading phase,
1019 // we will not touch the metadata of unloading nmethods
1020 {
1021 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CodeCache, worker_id);
1022 ShenandoahIsNMethodAliveClosure is_nmethod_alive;
1023 _nmethod_itr.nmethods_do(&is_nmethod_alive);
1024 }
1025 }
1026 }
1027 };
1028
1029 void ShenandoahConcurrentGC::op_weak_roots() {
1030 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1031 assert(heap->is_concurrent_weak_root_in_progress(), "Only during this phase");
1032 {
1033 // Concurrent weak root processing
1034 ShenandoahGCWorkerPhase worker_phase(ShenandoahPhaseTimings::conc_weak_roots);
1035 ShenandoahConcurrentWeakRootsEvacUpdateTask task(_generation, ShenandoahPhaseTimings::conc_weak_roots);
1036 heap->workers()->run_task(&task);
1037 }
1038
1039 {
1040 // It is possible for mutators executing the load reference barrier to have
1041 // loaded an oop through a weak handle that has since been nulled out by
1042 // weak root processing. Handshaking here forces them to complete the
1043 // barrier before the GC cycle continues and does something that would
1044 // change the evaluation of the barrier (for example, resetting the TAMS
1045 // on trashed regions could make an oop appear to be marked _after_ the
1046 // region has been recycled).
1047 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_weak_roots_rendezvous);
1048 heap->rendezvous_threads("Shenandoah Concurrent Weak Roots");
1049 }
1050 }
1051
1052 void ShenandoahConcurrentGC::op_class_unloading() {
1053 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1054 assert (heap->is_concurrent_weak_root_in_progress() &&
1055 heap->unload_classes(),
1056 "Checked by caller");
1057 heap->do_class_unloading();
1058 }
1059
1060 class ShenandoahEvacUpdateCodeCacheClosure : public NMethodClosure {
1061 private:
1062 ShenandoahEvacuateUpdateMetadataClosure _cl;
1063
1064 public:
1065 ShenandoahEvacUpdateCodeCacheClosure() : _cl() {}
1066
1067 void do_nmethod(nmethod* n) {
1068 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(n);
1069 ShenandoahNMethodLocker locker(data->lock());
1070 data->oops_do(&_cl, /* fix_relocations = */ true);
1071 }
1072 };
1073
1074 class ShenandoahConcurrentRootsEvacUpdateTask : public WorkerTask {
1075 private:
1076 ShenandoahPhaseTimings::Phase _phase;
1077 ShenandoahVMRoots<true /*concurrent*/> _vm_roots;
1078 ShenandoahClassLoaderDataRoots<true /*concurrent*/>
1079 _cld_roots;
1080 ShenandoahConcurrentNMethodIterator _nmethod_itr;
1081
1082 public:
1083 ShenandoahConcurrentRootsEvacUpdateTask(ShenandoahPhaseTimings::Phase phase) :
1084 WorkerTask("Shenandoah Evacuate/Update Concurrent Strong Roots"),
1085 _phase(phase),
1086 _vm_roots(phase),
1087 _cld_roots(phase, ShenandoahHeap::heap()->workers()->active_workers(), false /*heap iteration*/),
1088 _nmethod_itr(ShenandoahCodeRoots::table()) {}
1089
1090 void work(uint worker_id) {
1091 ShenandoahConcurrentWorkerSession worker_session(worker_id);
1092 {
1093 {
1094 // vm_roots and weak_roots are OopStorage backed roots, concurrent iteration
1095 // may race against OopStorage::release() calls.
1096 ShenandoahContextEvacuateUpdateRootsClosure cl;
1097 _vm_roots.oops_do<ShenandoahContextEvacuateUpdateRootsClosure>(&cl, worker_id);
1098 }
1099
1100 {
1101 ShenandoahEvacuateUpdateMetadataClosure cl;
1102 CLDToOopClosure clds(&cl, ClassLoaderData::_claim_strong);
1103 _cld_roots.cld_do(&clds, worker_id);
1104 }
1105 }
1106
1107 if (!ShenandoahHeap::heap()->unload_classes()) {
1108 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CodeCache, worker_id);
1109 ShenandoahEvacUpdateCodeCacheClosure cl;
1110 _nmethod_itr.nmethods_do(&cl);
1111 }
1112 }
1113 };
1114
1115 void ShenandoahConcurrentGC::op_strong_roots() {
1116 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1117 assert(heap->is_concurrent_strong_root_in_progress(), "Checked by caller");
1118 ShenandoahConcurrentRootsEvacUpdateTask task(ShenandoahPhaseTimings::conc_strong_roots);
1119 heap->workers()->run_task(&task);
1120 heap->set_concurrent_strong_root_in_progress(false);
1121 }
1122
1123 void ShenandoahConcurrentGC::op_cleanup_early() {
1124 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
1125 ShenandoahWorkerPolicy::calc_workers_for_conc_cleanup(),
1126 "cleanup early.");
1127 ShenandoahHeap::heap()->recycle_trash();
1128 }
1129
1130 void ShenandoahConcurrentGC::op_evacuate() {
1131 ShenandoahHeap::heap()->evacuate_collection_set(_generation, true /*concurrent*/);
1132 }
1133
1134 void ShenandoahConcurrentGC::op_init_update_refs() {
1135 if (ShenandoahVerify) {
1136 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1137 ShenandoahTimingsTracker v(ShenandoahPhaseTimings::init_update_refs_verify);
1138 heap->verifier()->verify_before_update_refs(_generation);
1139 }
1140 }
1141
1142 void ShenandoahConcurrentGC::op_update_refs() {
1143 ShenandoahHeap::heap()->update_heap_references(_generation, true /*concurrent*/);
1144 }
1145
1146 class ShenandoahUpdateThreadHandshakeClosure : public HandshakeClosure {
1147 private:
1148 // This closure runs when thread is stopped for handshake, which means
1149 // we can use non-concurrent closure here, as long as it only updates
1150 // locations modified by the thread itself, i.e. stack locations.
1151 ShenandoahNonConcUpdateRefsClosure _cl;
1152 public:
1153 ShenandoahUpdateThreadHandshakeClosure();
1154 void do_thread(Thread* thread) override;
1155 };
1156
1157 ShenandoahUpdateThreadHandshakeClosure::ShenandoahUpdateThreadHandshakeClosure() :
1158 HandshakeClosure("Shenandoah Update Thread Roots") {
1159 }
1160
1161 void ShenandoahUpdateThreadHandshakeClosure::do_thread(Thread* thread) {
1162 if (thread->is_Java_thread()) {
1163 JavaThread* jt = JavaThread::cast(thread);
1164 ResourceMark rm;
1165 jt->oops_do(&_cl, nullptr);
1166 }
1167 }
1168
1169 class ShenandoahUpdateThreadRootsAndFlushOldSatbBuffers final : public HandshakeClosure {
1170 // When Shenandoah is marking the old generation, it is possible for the SATB barrier
1171 // to pick up overwritten pointers that point into a cset region. If these pointers
1172 // are accessed by mark threads, they will crash. Once update refs has completed, it is
1173 // no longer possible for a mutator thread to overwrite a pointer into a cset region.
1174 //
1175 // Therefore, at the end of update refs, we use this closure to update the thread roots
1176 // and 'complete' all the thread local SATB buffers. Completing these will filter out
1177 // anything that has already been marked or anything that points to a region which is
1178 // not old. We do not need to worry about ABA situations where a region may become old
1179 // after the pointer is enqueued but before it is filtered. There are only two ways a
1180 // region may become old:
1181 // 1. The region is promoted in place. This is safe because such regions will never
1182 // be in the collection set. If this happens, the pointer will be preserved, essentially
1183 // becoming part of the old snapshot.
1184 // 2. The region is allocated during evacuation of old. This is also not a concern because
1185 // we haven't yet finished marking old so no mixed evacuations will happen.
1186 ShenandoahUpdateThreadHandshakeClosure _update_roots;
1187 ShenandoahFlushSATB _flush_all_satb;
1188
1189 public:
1190 ShenandoahUpdateThreadRootsAndFlushOldSatbBuffers() :
1191 HandshakeClosure("Shenandoah Update Thread Roots and Flush SATB"),
1192 _flush_all_satb(ShenandoahBarrierSet::satb_mark_queue_set()) {
1193 assert(ShenandoahBarrierSet::satb_mark_queue_set().get_filter_out_young(),
1194 "Should be filtering pointers outside of old during old marking");
1195 }
1196
1197 void do_thread(Thread* thread) override {
1198 _update_roots.do_thread(thread);
1199 _flush_all_satb.do_thread(thread);
1200 }
1201 };
1202
1203 void ShenandoahConcurrentGC::op_update_thread_roots() {
1204 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1205 if (heap->is_concurrent_old_mark_in_progress()) {
1206 ShenandoahUpdateThreadRootsAndFlushOldSatbBuffers cl;
1207 Handshake::execute(&cl);
1208 } else {
1209 ShenandoahUpdateThreadHandshakeClosure cl;
1210 Handshake::execute(&cl);
1211 }
1212 }
1213
1214 void ShenandoahConcurrentGC::op_final_update_refs() {
1215 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1216 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at safepoint");
1217 assert(!heap->_update_refs_iterator.has_next(), "Should have finished update references");
1218
1219 heap->finish_concurrent_roots();
1220
1221 // Clear cancelled GC, if set. On cancellation path, the block before would handle
1222 // everything.
1223 if (heap->cancelled_gc()) {
1224 heap->clear_cancelled_gc();
1225 }
1226
1227 // Has to be done before cset is clear
1228 if (ShenandoahVerify) {
1229 heap->verifier()->verify_roots_in_to_space(_generation);
1230 }
1231
1232 // If we are running in generational mode, this will also age active regions that
1233 // haven't been used for allocation.
1234 heap->update_heap_region_states(true /*concurrent*/);
1235
1236 heap->set_update_refs_in_progress(false);
1237 heap->set_has_forwarded_objects(false);
1238
1239 if (ShenandoahVerify) {
1240 ShenandoahTimingsTracker v(ShenandoahPhaseTimings::final_update_refs_verify);
1241 heap->verifier()->verify_after_update_refs(_generation);
1242 }
1243
1244 if (VerifyAfterGC) {
1245 Universe::verify();
1246 }
1247
1248 heap->rebuild_free_set(true /*concurrent*/);
1249 _generation->heuristics()->start_idle_span();
1250
1251 // Final pause: update GC barriers to idle state.
1252 ShenandoahCodeRoots::arm_nmethods();
1253
1254 {
1255 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_update_refs_propagate_gc_state);
1256 heap->propagate_gc_state_to_all_threads();
1257 }
1258 }
1259
1260 void ShenandoahConcurrentGC::entry_final_roots() {
1261 const char* msg = final_roots_event_message();
1262 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::final_roots);
1263 EventMark em("%s", msg);
1264
1265 ShenandoahHeap::heap()->op_final_roots();
1266 }
1267
1268 void ShenandoahConcurrentGC::op_verify_final() {
1269 assert(ShenandoahVerify, "Should have been checked before");
1270 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1271 heap->verifier()->verify_after_gc(_generation);
1272 }
1273
1274 void ShenandoahConcurrentGC::op_cleanup_complete() {
1275 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
1276 ShenandoahWorkerPolicy::calc_workers_for_conc_cleanup(),
1277 "cleanup complete.");
1278 ShenandoahHeap::heap()->recycle_trash();
1279 }
1280
1281 void ShenandoahConcurrentGC::op_reset_after_collect() {
1282 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
1283 ShenandoahWorkerPolicy::calc_workers_for_conc_reset(),
1284 "reset after collection.");
1285
1286 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1287 if (heap->mode()->is_generational()) {
1288 // If we are in the midst of an old gc bootstrap or an old marking, we want to leave the mark bit map of
1289 // the young generation intact. In particular, reference processing in the old generation may potentially
1290 // need the reachability of a young generation referent of a Reference object in the old generation.
1291 if (!_do_old_gc_bootstrap && !heap->is_concurrent_old_mark_in_progress()) {
1292 heap->young_generation()->reset_mark_bitmap<false>();
1293 }
1294 } else {
1295 _generation->reset_mark_bitmap<false>();
1296 }
1297 }
1298
1299 bool ShenandoahConcurrentGC::check_cancellation_and_abort(ShenandoahDegenPoint point) {
1300 if (ShenandoahHeap::heap()->cancelled_gc()) {
1301 _degen_point = point;
1302 return true;
1303 }
1304 return false;
1305 }
1306
1307 const char* ShenandoahConcurrentGC::init_mark_event_message() const {
1308 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1309 assert(!heap->has_forwarded_objects(), "Should not have forwarded objects here");
1310 if (heap->unload_classes()) {
1311 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Init Mark", " (unload classes)");
1312 } else {
1313 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Init Mark", "");
1314 }
1315 }
1316
1317 const char* ShenandoahConcurrentGC::final_mark_event_message() const {
1318 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1319 assert(!heap->has_forwarded_objects() || heap->is_concurrent_old_mark_in_progress(),
1320 "Should not have forwarded objects during final mark, unless old gen concurrent mark is running");
1321
1322 if (heap->unload_classes()) {
1323 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Mark", " (unload classes)");
1324 } else {
1325 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Mark", "");
1326 }
1327 }
1328
1329 const char* ShenandoahConcurrentGC::conc_mark_event_message() const {
1330 ShenandoahHeap* const heap = ShenandoahHeap::heap();
1331 assert(!heap->has_forwarded_objects() || heap->is_concurrent_old_mark_in_progress(),
1332 "Should not have forwarded objects concurrent mark, unless old gen concurrent mark is running");
1333 if (heap->unload_classes()) {
1334 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent marking", " (unload classes)");
1335 } else {
1336 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent marking", "");
1337 }
1338 }
1339
1340 const char* ShenandoahConcurrentGC::conc_reset_event_message() const {
1341 if (ShenandoahHeap::heap()->unload_classes()) {
1342 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent reset", " (unload classes)");
1343 } else {
1344 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent reset", "");
1345 }
1346 }
1347
1348 const char* ShenandoahConcurrentGC::conc_reset_after_collect_event_message() const {
1349 if (ShenandoahHeap::heap()->unload_classes()) {
1350 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent reset after collect", " (unload classes)");
1351 } else {
1352 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent reset after collect", "");
1353 }
1354 }
1355
1356 const char* ShenandoahConcurrentGC::verify_final_event_message() const {
1357 if (ShenandoahHeap::heap()->unload_classes()) {
1358 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Verify Final", " (unload classes)");
1359 } else {
1360 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Verify Final", "");
1361 }
1362 }
1363
1364 const char* ShenandoahConcurrentGC::final_roots_event_message() const {
1365 if (ShenandoahHeap::heap()->unload_classes()) {
1366 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Roots", " (unload classes)");
1367 } else {
1368 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Final Roots", "");
1369 }
1370 }
1371
1372 const char* ShenandoahConcurrentGC::conc_weak_refs_event_message() const {
1373 if (ShenandoahHeap::heap()->unload_classes()) {
1374 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak references", " (unload classes)");
1375 } else {
1376 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak references", "");
1377 }
1378 }
1379
1380 const char* ShenandoahConcurrentGC::conc_weak_roots_event_message() const {
1381 if (ShenandoahHeap::heap()->unload_classes()) {
1382 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak roots", " (unload classes)");
1383 } else {
1384 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent weak roots", "");
1385 }
1386 }
1387
1388 const char* ShenandoahConcurrentGC::conc_cleanup_event_message() const {
1389 if (ShenandoahHeap::heap()->unload_classes()) {
1390 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent cleanup", " (unload classes)");
1391 } else {
1392 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent cleanup", "");
1393 }
1394 }
1395
1396 const char* ShenandoahConcurrentGC::conc_init_update_refs_event_message() const {
1397 if (ShenandoahHeap::heap()->unload_classes()) {
1398 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent Init Update Refs", " (unload classes)");
1399 } else {
1400 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Concurrent Init Update Refs", "");
1401 }
1402 }