1 | /* |
2 | * Copyright (c) 2001, 2019, Oracle and/or its affiliates. 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 | #include "classfile/classLoaderDataGraph.hpp" |
27 | #include "gc/g1/g1Analytics.hpp" |
28 | #include "gc/g1/g1CollectedHeap.inline.hpp" |
29 | #include "gc/g1/g1ConcurrentMark.inline.hpp" |
30 | #include "gc/g1/g1ConcurrentMarkThread.inline.hpp" |
31 | #include "gc/g1/g1MMUTracker.hpp" |
32 | #include "gc/g1/g1Policy.hpp" |
33 | #include "gc/g1/g1RemSet.hpp" |
34 | #include "gc/g1/g1VMOperations.hpp" |
35 | #include "gc/shared/concurrentGCPhaseManager.hpp" |
36 | #include "gc/shared/gcId.hpp" |
37 | #include "gc/shared/gcTrace.hpp" |
38 | #include "gc/shared/gcTraceTime.inline.hpp" |
39 | #include "gc/shared/suspendibleThreadSet.hpp" |
40 | #include "logging/log.hpp" |
41 | #include "memory/resourceArea.hpp" |
42 | #include "runtime/handles.inline.hpp" |
43 | #include "runtime/vmThread.hpp" |
44 | #include "utilities/debug.hpp" |
45 | |
46 | // ======= Concurrent Mark Thread ======== |
47 | |
48 | // Check order in EXPAND_CURRENT_PHASES |
49 | STATIC_ASSERT(ConcurrentGCPhaseManager::UNCONSTRAINED_PHASE < |
50 | ConcurrentGCPhaseManager::IDLE_PHASE); |
51 | |
52 | #define EXPAND_CONCURRENT_PHASES(expander) \ |
53 | expander(ANY, = ConcurrentGCPhaseManager::UNCONSTRAINED_PHASE, NULL) \ |
54 | expander(IDLE, = ConcurrentGCPhaseManager::IDLE_PHASE, NULL) \ |
55 | expander(CONCURRENT_CYCLE,, "Concurrent Cycle") \ |
56 | expander(CLEAR_CLAIMED_MARKS,, "Concurrent Clear Claimed Marks") \ |
57 | expander(SCAN_ROOT_REGIONS,, "Concurrent Scan Root Regions") \ |
58 | expander(CONCURRENT_MARK,, "Concurrent Mark") \ |
59 | expander(MARK_FROM_ROOTS,, "Concurrent Mark From Roots") \ |
60 | expander(PRECLEAN,, "Concurrent Preclean") \ |
61 | expander(,, NULL) \ |
62 | expander(,, NULL) \ |
63 | expander(REBUILD_REMEMBERED_SETS,, "Concurrent Rebuild Remembered Sets") \ |
64 | expander(CLEANUP_FOR_NEXT_MARK,, "Concurrent Cleanup for Next Mark") \ |
65 | /* */ |
66 | |
67 | class G1ConcurrentPhase : public AllStatic { |
68 | public: |
69 | enum { |
70 | #define CONCURRENT_PHASE_ENUM(tag, value, ignore_title) tag value, |
71 | EXPAND_CONCURRENT_PHASES(CONCURRENT_PHASE_ENUM) |
72 | #undef CONCURRENT_PHASE_ENUM |
73 | PHASE_ID_LIMIT |
74 | }; |
75 | }; |
76 | |
77 | G1ConcurrentMarkThread::G1ConcurrentMarkThread(G1ConcurrentMark* cm) : |
78 | ConcurrentGCThread(), |
79 | _vtime_start(0.0), |
80 | _vtime_accum(0.0), |
81 | _vtime_mark_accum(0.0), |
82 | _cm(cm), |
83 | _state(Idle), |
84 | _phase_manager_stack() { |
85 | |
86 | set_name("G1 Main Marker" ); |
87 | create_and_start(); |
88 | } |
89 | |
90 | class : public VoidClosure { |
91 | G1ConcurrentMark* ; |
92 | public: |
93 | (G1ConcurrentMark* cm) : _cm(cm) {} |
94 | |
95 | void (){ |
96 | _cm->remark(); |
97 | } |
98 | }; |
99 | |
100 | class CMCleanup : public VoidClosure { |
101 | G1ConcurrentMark* _cm; |
102 | public: |
103 | CMCleanup(G1ConcurrentMark* cm) : _cm(cm) {} |
104 | |
105 | void do_void(){ |
106 | _cm->cleanup(); |
107 | } |
108 | }; |
109 | |
110 | double G1ConcurrentMarkThread::mmu_sleep_time(G1Policy* g1_policy, bool ) { |
111 | // There are 3 reasons to use SuspendibleThreadSetJoiner. |
112 | // 1. To avoid concurrency problem. |
113 | // - G1MMUTracker::add_pause(), when_sec() and its variation(when_ms() etc..) can be called |
114 | // concurrently from ConcurrentMarkThread and VMThread. |
115 | // 2. If currently a gc is running, but it has not yet updated the MMU, |
116 | // we will not forget to consider that pause in the MMU calculation. |
117 | // 3. If currently a gc is running, ConcurrentMarkThread will wait it to be finished. |
118 | // And then sleep for predicted amount of time by delay_to_keep_mmu(). |
119 | SuspendibleThreadSetJoiner sts_join; |
120 | |
121 | const G1Analytics* analytics = g1_policy->analytics(); |
122 | double now = os::elapsedTime(); |
123 | double prediction_ms = remark ? analytics->predict_remark_time_ms() |
124 | : analytics->predict_cleanup_time_ms(); |
125 | G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker(); |
126 | return mmu_tracker->when_ms(now, prediction_ms); |
127 | } |
128 | |
129 | void G1ConcurrentMarkThread::delay_to_keep_mmu(G1Policy* g1_policy, bool ) { |
130 | if (g1_policy->use_adaptive_young_list_length()) { |
131 | jlong sleep_time_ms = mmu_sleep_time(g1_policy, remark); |
132 | if (!_cm->has_aborted() && sleep_time_ms > 0) { |
133 | os::sleep(this, sleep_time_ms, false); |
134 | } |
135 | } |
136 | } |
137 | |
138 | class G1ConcPhaseTimer : public GCTraceConcTimeImpl<LogLevel::Info, LOG_TAGS(gc, marking)> { |
139 | G1ConcurrentMark* _cm; |
140 | |
141 | public: |
142 | G1ConcPhaseTimer(G1ConcurrentMark* cm, const char* title) : |
143 | GCTraceConcTimeImpl<LogLevel::Info, LogTag::_gc, LogTag::_marking>(title), |
144 | _cm(cm) |
145 | { |
146 | _cm->gc_timer_cm()->register_gc_concurrent_start(title); |
147 | } |
148 | |
149 | ~G1ConcPhaseTimer() { |
150 | _cm->gc_timer_cm()->register_gc_concurrent_end(); |
151 | } |
152 | }; |
153 | |
154 | static const char* const concurrent_phase_names[] = { |
155 | #define CONCURRENT_PHASE_NAME(tag, ignore_value, ignore_title) XSTR(tag), |
156 | EXPAND_CONCURRENT_PHASES(CONCURRENT_PHASE_NAME) |
157 | #undef CONCURRENT_PHASE_NAME |
158 | NULL // terminator |
159 | }; |
160 | // Verify dense enum assumption. +1 for terminator. |
161 | STATIC_ASSERT(G1ConcurrentPhase::PHASE_ID_LIMIT + 1 == |
162 | ARRAY_SIZE(concurrent_phase_names)); |
163 | |
164 | // Returns the phase number for name, or a negative value if unknown. |
165 | static int lookup_concurrent_phase(const char* name) { |
166 | const char* const* names = concurrent_phase_names; |
167 | for (uint i = 0; names[i] != NULL; ++i) { |
168 | if (strcmp(name, names[i]) == 0) { |
169 | return static_cast<int>(i); |
170 | } |
171 | } |
172 | return -1; |
173 | } |
174 | |
175 | // The phase must be valid and must have a title. |
176 | static const char* lookup_concurrent_phase_title(int phase) { |
177 | static const char* const titles[] = { |
178 | #define CONCURRENT_PHASE_TITLE(ignore_tag, ignore_value, title) title, |
179 | EXPAND_CONCURRENT_PHASES(CONCURRENT_PHASE_TITLE) |
180 | #undef CONCURRENT_PHASE_TITLE |
181 | }; |
182 | // Verify dense enum assumption. |
183 | STATIC_ASSERT(G1ConcurrentPhase::PHASE_ID_LIMIT == ARRAY_SIZE(titles)); |
184 | |
185 | assert(0 <= phase, "precondition" ); |
186 | assert((uint)phase < ARRAY_SIZE(titles), "precondition" ); |
187 | const char* title = titles[phase]; |
188 | assert(title != NULL, "precondition" ); |
189 | return title; |
190 | } |
191 | |
192 | class G1ConcPhaseManager : public StackObj { |
193 | G1ConcurrentMark* _cm; |
194 | ConcurrentGCPhaseManager _manager; |
195 | |
196 | public: |
197 | G1ConcPhaseManager(int phase, G1ConcurrentMarkThread* thread) : |
198 | _cm(thread->cm()), |
199 | _manager(phase, thread->phase_manager_stack()) |
200 | { } |
201 | |
202 | ~G1ConcPhaseManager() { |
203 | // Deactivate the manager if marking aborted, to avoid blocking on |
204 | // phase exit when the phase has been requested. |
205 | if (_cm->has_aborted()) { |
206 | _manager.deactivate(); |
207 | } |
208 | } |
209 | |
210 | void set_phase(int phase, bool force) { |
211 | _manager.set_phase(phase, force); |
212 | } |
213 | }; |
214 | |
215 | // Combine phase management and timing into one convenient utility. |
216 | class G1ConcPhase : public StackObj { |
217 | G1ConcPhaseTimer _timer; |
218 | G1ConcPhaseManager _manager; |
219 | |
220 | public: |
221 | G1ConcPhase(int phase, G1ConcurrentMarkThread* thread) : |
222 | _timer(thread->cm(), lookup_concurrent_phase_title(phase)), |
223 | _manager(phase, thread) |
224 | { } |
225 | }; |
226 | |
227 | bool G1ConcurrentMarkThread::request_concurrent_phase(const char* phase_name) { |
228 | int phase = lookup_concurrent_phase(phase_name); |
229 | if (phase < 0) return false; |
230 | |
231 | while (!ConcurrentGCPhaseManager::wait_for_phase(phase, |
232 | phase_manager_stack())) { |
233 | assert(phase != G1ConcurrentPhase::ANY, "Wait for ANY phase must succeed" ); |
234 | if ((phase != G1ConcurrentPhase::IDLE) && !during_cycle()) { |
235 | // If idle and the goal is !idle, start a collection. |
236 | G1CollectedHeap::heap()->collect(GCCause::_wb_conc_mark); |
237 | } |
238 | } |
239 | return true; |
240 | } |
241 | |
242 | void G1ConcurrentMarkThread::run_service() { |
243 | _vtime_start = os::elapsedVTime(); |
244 | |
245 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
246 | G1Policy* policy = g1h->policy(); |
247 | |
248 | G1ConcPhaseManager cpmanager(G1ConcurrentPhase::IDLE, this); |
249 | |
250 | while (!should_terminate()) { |
251 | // wait until started is set. |
252 | sleep_before_next_cycle(); |
253 | if (should_terminate()) { |
254 | break; |
255 | } |
256 | |
257 | cpmanager.set_phase(G1ConcurrentPhase::CONCURRENT_CYCLE, false /* force */); |
258 | |
259 | GCIdMark gc_id_mark; |
260 | |
261 | _cm->concurrent_cycle_start(); |
262 | |
263 | GCTraceConcTime(Info, gc) tt("Concurrent Cycle" ); |
264 | { |
265 | ResourceMark rm; |
266 | HandleMark hm; |
267 | double cycle_start = os::elapsedVTime(); |
268 | |
269 | { |
270 | G1ConcPhase p(G1ConcurrentPhase::CLEAR_CLAIMED_MARKS, this); |
271 | ClassLoaderDataGraph::clear_claimed_marks(); |
272 | } |
273 | |
274 | // We have to ensure that we finish scanning the root regions |
275 | // before the next GC takes place. To ensure this we have to |
276 | // make sure that we do not join the STS until the root regions |
277 | // have been scanned. If we did then it's possible that a |
278 | // subsequent GC could block us from joining the STS and proceed |
279 | // without the root regions have been scanned which would be a |
280 | // correctness issue. |
281 | |
282 | { |
283 | G1ConcPhase p(G1ConcurrentPhase::SCAN_ROOT_REGIONS, this); |
284 | _cm->scan_root_regions(); |
285 | } |
286 | |
287 | // It would be nice to use the G1ConcPhase class here but |
288 | // the "end" logging is inside the loop and not at the end of |
289 | // a scope. Also, the timer doesn't support nesting. |
290 | // Mimicking the same log output instead. |
291 | { |
292 | G1ConcPhaseManager mark_manager(G1ConcurrentPhase::CONCURRENT_MARK, this); |
293 | jlong mark_start = os::elapsed_counter(); |
294 | const char* cm_title = lookup_concurrent_phase_title(G1ConcurrentPhase::CONCURRENT_MARK); |
295 | log_info(gc, marking)("%s (%.3fs)" , |
296 | cm_title, |
297 | TimeHelper::counter_to_seconds(mark_start)); |
298 | for (uint iter = 1; !_cm->has_aborted(); ++iter) { |
299 | // Concurrent marking. |
300 | { |
301 | G1ConcPhase p(G1ConcurrentPhase::MARK_FROM_ROOTS, this); |
302 | _cm->mark_from_roots(); |
303 | } |
304 | if (_cm->has_aborted()) { |
305 | break; |
306 | } |
307 | |
308 | if (G1UseReferencePrecleaning) { |
309 | G1ConcPhase p(G1ConcurrentPhase::PRECLEAN, this); |
310 | _cm->preclean(); |
311 | } |
312 | |
313 | // Provide a control point before remark. |
314 | { |
315 | G1ConcPhaseManager p(G1ConcurrentPhase::BEFORE_REMARK, this); |
316 | } |
317 | if (_cm->has_aborted()) { |
318 | break; |
319 | } |
320 | |
321 | // Delay remark pause for MMU. |
322 | double mark_end_time = os::elapsedVTime(); |
323 | jlong mark_end = os::elapsed_counter(); |
324 | _vtime_mark_accum += (mark_end_time - cycle_start); |
325 | delay_to_keep_mmu(policy, true /* remark */); |
326 | if (_cm->has_aborted()) { |
327 | break; |
328 | } |
329 | |
330 | // Pause Remark. |
331 | log_info(gc, marking)("%s (%.3fs, %.3fs) %.3fms" , |
332 | cm_title, |
333 | TimeHelper::counter_to_seconds(mark_start), |
334 | TimeHelper::counter_to_seconds(mark_end), |
335 | TimeHelper::counter_to_millis(mark_end - mark_start)); |
336 | mark_manager.set_phase(G1ConcurrentPhase::REMARK, false); |
337 | CMRemark cl(_cm); |
338 | VM_G1Concurrent op(&cl, "Pause Remark" ); |
339 | VMThread::execute(&op); |
340 | if (_cm->has_aborted()) { |
341 | break; |
342 | } else if (!_cm->restart_for_overflow()) { |
343 | break; // Exit loop if no restart requested. |
344 | } else { |
345 | // Loop to restart for overflow. |
346 | mark_manager.set_phase(G1ConcurrentPhase::CONCURRENT_MARK, false); |
347 | log_info(gc, marking)("%s Restart for Mark Stack Overflow (iteration #%u)" , |
348 | cm_title, iter); |
349 | } |
350 | } |
351 | } |
352 | |
353 | if (!_cm->has_aborted()) { |
354 | G1ConcPhase p(G1ConcurrentPhase::REBUILD_REMEMBERED_SETS, this); |
355 | _cm->rebuild_rem_set_concurrently(); |
356 | } |
357 | |
358 | double end_time = os::elapsedVTime(); |
359 | // Update the total virtual time before doing this, since it will try |
360 | // to measure it to get the vtime for this marking. |
361 | _vtime_accum = (end_time - _vtime_start); |
362 | |
363 | if (!_cm->has_aborted()) { |
364 | delay_to_keep_mmu(policy, false /* cleanup */); |
365 | } |
366 | |
367 | if (!_cm->has_aborted()) { |
368 | CMCleanup cl_cl(_cm); |
369 | VM_G1Concurrent op(&cl_cl, "Pause Cleanup" ); |
370 | VMThread::execute(&op); |
371 | } |
372 | |
373 | // We now want to allow clearing of the marking bitmap to be |
374 | // suspended by a collection pause. |
375 | // We may have aborted just before the remark. Do not bother clearing the |
376 | // bitmap then, as it has been done during mark abort. |
377 | if (!_cm->has_aborted()) { |
378 | G1ConcPhase p(G1ConcurrentPhase::CLEANUP_FOR_NEXT_MARK, this); |
379 | _cm->cleanup_for_next_mark(); |
380 | } |
381 | } |
382 | |
383 | // Update the number of full collections that have been |
384 | // completed. This will also notify the FullGCCount_lock in case a |
385 | // Java thread is waiting for a full GC to happen (e.g., it |
386 | // called System.gc() with +ExplicitGCInvokesConcurrent). |
387 | { |
388 | SuspendibleThreadSetJoiner sts_join; |
389 | g1h->increment_old_marking_cycles_completed(true /* concurrent */); |
390 | |
391 | _cm->concurrent_cycle_end(); |
392 | } |
393 | |
394 | cpmanager.set_phase(G1ConcurrentPhase::IDLE, _cm->has_aborted() /* force */); |
395 | } |
396 | _cm->root_regions()->cancel_scan(); |
397 | } |
398 | |
399 | void G1ConcurrentMarkThread::stop_service() { |
400 | MutexLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag); |
401 | CGC_lock->notify_all(); |
402 | } |
403 | |
404 | |
405 | void G1ConcurrentMarkThread::sleep_before_next_cycle() { |
406 | // We join here because we don't want to do the "shouldConcurrentMark()" |
407 | // below while the world is otherwise stopped. |
408 | assert(!in_progress(), "should have been cleared" ); |
409 | |
410 | MonitorLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag); |
411 | while (!started() && !should_terminate()) { |
412 | ml.wait(); |
413 | } |
414 | |
415 | if (started()) { |
416 | set_in_progress(); |
417 | } |
418 | } |
419 | |