1 /*
2 * Copyright (c) 2021, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26
27 #include "code/nmethod.hpp"
28 #include "gc/shared/taskTerminator.hpp"
29 #include "gc/shared/workerThread.hpp"
30 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
31 #include "gc/shenandoah/shenandoahGeneration.hpp"
32 #include "gc/shenandoah/shenandoahGenerationType.hpp"
33 #include "gc/shenandoah/shenandoahMark.inline.hpp"
34 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
35 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
36 #include "gc/shenandoah/shenandoahSTWMark.hpp"
37 #include "gc/shenandoah/shenandoahVerifier.hpp"
38 #include "runtime/threads.hpp"
39
40 class ShenandoahSTWMarkTask : public WorkerTask {
41 private:
42 ShenandoahSTWMark* const _mark;
43 NMethodMarkingScope _nmethod_marking_scope;
44 ThreadsClaimTokenScope _threads_claim_token_scope;
45
46 public:
47 ShenandoahSTWMarkTask(ShenandoahSTWMark* mark);
48 void work(uint worker_id);
49 };
50
51 ShenandoahSTWMarkTask::ShenandoahSTWMarkTask(ShenandoahSTWMark* mark) :
52 WorkerTask("Shenandoah STW mark"),
53 _mark(mark),
54 _nmethod_marking_scope(),
55 _threads_claim_token_scope() {
56 }
57
58 void ShenandoahSTWMarkTask::work(uint worker_id) {
59 ShenandoahParallelWorkerSession worker_session(worker_id);
60 _mark->mark_roots(worker_id);
61 _mark->finish_mark(worker_id);
62 }
63
64 ShenandoahSTWMark::ShenandoahSTWMark(ShenandoahGeneration* generation, bool full_gc) :
65 ShenandoahMark(generation),
66 _root_scanner(full_gc ? ShenandoahPhaseTimings::full_gc_mark : ShenandoahPhaseTimings::degen_gc_stw_mark),
67 _terminator(ShenandoahHeap::heap()->workers()->active_workers(), task_queues()),
68 _full_gc(full_gc) {
69 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a Shenandoah safepoint");
70 }
71
72 void ShenandoahSTWMark::mark() {
73 ShenandoahHeap* const heap = ShenandoahHeap::heap();
74
75 // Arm all nmethods. Even though this is STW mark, some marking code
76 // piggybacks on nmethod barriers for special instances.
77 ShenandoahCodeRoots::arm_nmethods();
78
79 // Weak reference processing
80 ShenandoahReferenceProcessor* rp = _generation->ref_processor();
81 rp->reset_thread_locals();
82
83 // Init mark, do not expect forwarded pointers in roots
84 if (ShenandoahVerify) {
85 assert(Thread::current()->is_VM_thread(), "Must be");
86 heap->verifier()->verify_roots_no_forwarded(_generation);
87 }
88
89 start_mark();
90
91 uint nworkers = heap->workers()->active_workers();
92 task_queues()->reserve(nworkers);
93
94 TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats());
95
96 {
97 // Mark
98 if (_generation->is_young()) {
99 // But only scan the remembered set for young generation.
100 _generation->scan_remembered_set(false /* is_concurrent */);
101 }
102
103 ShenandoahSTWMarkTask task(this);
104 heap->workers()->run_task(&task);
105
106 assert(task_queues()->is_empty(), "Should be empty");
107 }
108
109 _generation->set_mark_complete();
110 end_mark();
111
112 // Mark is finished, can disarm the nmethods now.
113 ShenandoahCodeRoots::disarm_nmethods();
114
115 assert(task_queues()->is_empty(), "Should be empty");
116 TASKQUEUE_STATS_ONLY(task_queues()->print_and_reset_taskqueue_stats(""));
117 }
118
119 void ShenandoahSTWMark::mark_roots(uint worker_id) {
120 ShenandoahReferenceProcessor* rp = _generation->ref_processor();
121 auto queue = task_queues()->queue(worker_id);
122 switch (_generation->type()) {
123 case NON_GEN: {
124 ShenandoahMarkRefsClosure<NON_GEN> init_mark(queue, rp, nullptr);
125 _root_scanner.roots_do(&init_mark, worker_id);
126 break;
127 }
128 case GLOBAL: {
129 ShenandoahMarkRefsClosure<GLOBAL> init_mark(queue, rp, nullptr);
130 _root_scanner.roots_do(&init_mark, worker_id);
131 break;
132 }
133 case YOUNG: {
134 ShenandoahMarkRefsClosure<YOUNG> init_mark(queue, rp, nullptr);
135 _root_scanner.roots_do(&init_mark, worker_id);
136 break;
137 }
138 case OLD:
139 // We never exclusively mark the old generation on a safepoint. This would be encompassed
140 // by a 'global' collection. Note that both GLOBAL and NON_GEN mark the entire heap, but
141 // the GLOBAL closure is specialized for the generational mode.
142 default:
143 ShouldNotReachHere();
144 }
145 }
146
147 void ShenandoahSTWMark::finish_mark(uint worker_id) {
148 ShenandoahPhaseTimings::Phase phase = _full_gc ? ShenandoahPhaseTimings::full_gc_mark : ShenandoahPhaseTimings::degen_gc_stw_mark;
149 ShenandoahWorkerTimingsTracker timer(phase, ShenandoahPhaseTimings::ParallelMark, worker_id);
150 StringDedup::Requests requests;
151
152 mark_loop(worker_id, &_terminator, _generation->type(), false /* not cancellable */,
153 ShenandoahStringDedup::is_enabled() ? ALWAYS_DEDUP : NO_DEDUP, &requests);
154 }