1 /* 2 * Copyright (c) 2001, 2020, 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/g1Trace.hpp" 35 #include "gc/g1/g1VMOperations.hpp" 36 #include "gc/shared/concurrentGCBreakpoints.hpp" 37 #include "gc/shared/gcId.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 #include "utilities/ticks.hpp" 46 47 // ======= Concurrent Mark Thread ======== 48 49 G1ConcurrentMarkThread::G1ConcurrentMarkThread(G1ConcurrentMark* cm) : 50 ConcurrentGCThread(), 51 _vtime_start(0.0), 52 _vtime_accum(0.0), 53 _vtime_mark_accum(0.0), 54 _cm(cm), 55 _state(Idle) 56 { 57 set_name("G1 Main Marker"); 58 create_and_start(); 59 } 60 61 class CMRemark : public VoidClosure { 62 G1ConcurrentMark* _cm; 63 public: 64 CMRemark(G1ConcurrentMark* cm) : _cm(cm) {} 65 66 void do_void(){ 67 _cm->remark(); 68 } 69 }; 70 71 class CMCleanup : public VoidClosure { 72 G1ConcurrentMark* _cm; 73 public: 74 CMCleanup(G1ConcurrentMark* cm) : _cm(cm) {} 75 76 void do_void(){ 77 _cm->cleanup(); 78 } 79 }; 80 81 double G1ConcurrentMarkThread::mmu_delay_end(G1Policy* g1_policy, bool remark) { 82 // There are 3 reasons to use SuspendibleThreadSetJoiner. 83 // 1. To avoid concurrency problem. 84 // - G1MMUTracker::add_pause(), when_sec() and its variation(when_ms() etc..) can be called 85 // concurrently from ConcurrentMarkThread and VMThread. 86 // 2. If currently a gc is running, but it has not yet updated the MMU, 87 // we will not forget to consider that pause in the MMU calculation. 88 // 3. If currently a gc is running, ConcurrentMarkThread will wait it to be finished. 89 // And then sleep for predicted amount of time by delay_to_keep_mmu(). 90 SuspendibleThreadSetJoiner sts_join; 91 92 const G1Analytics* analytics = g1_policy->analytics(); 93 double prediction_ms = remark ? analytics->predict_remark_time_ms() 94 : analytics->predict_cleanup_time_ms(); 95 double prediction = prediction_ms / MILLIUNITS; 96 G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker(); 97 double now = os::elapsedTime(); 98 return now + mmu_tracker->when_sec(now, prediction); 99 } 100 101 void G1ConcurrentMarkThread::delay_to_keep_mmu(G1Policy* g1_policy, bool remark) { 102 if (g1_policy->use_adaptive_young_list_length()) { 103 double delay_end_sec = mmu_delay_end(g1_policy, remark); 104 // Wait for timeout or thread termination request. 105 MonitorLocker ml(CGC_lock, Monitor::_no_safepoint_check_flag); 106 while (!_cm->has_aborted() && !should_terminate()) { 107 double sleep_time_sec = (delay_end_sec - os::elapsedTime()); 108 jlong sleep_time_ms = ceil(sleep_time_sec * MILLIUNITS); 109 if (sleep_time_ms <= 0) { 110 break; // Passed end time. 111 } else if (ml.wait(sleep_time_ms, Monitor::_no_safepoint_check_flag)) { 112 break; // Timeout => reached end time. 113 } 114 // Other (possibly spurious) wakeup. Retry with updated sleep time. 115 } 116 } 117 } 118 119 class G1ConcPhaseTimer : public GCTraceConcTimeImpl<LogLevel::Info, LOG_TAGS(gc, marking)> { 120 G1ConcurrentMark* _cm; 121 122 public: 123 G1ConcPhaseTimer(G1ConcurrentMark* cm, const char* title) : 124 GCTraceConcTimeImpl<LogLevel::Info, LogTag::_gc, LogTag::_marking>(title), 125 _cm(cm) 126 { 127 _cm->gc_timer_cm()->register_gc_concurrent_start(title); 128 } 129 130 ~G1ConcPhaseTimer() { 131 _cm->gc_timer_cm()->register_gc_concurrent_end(); 132 } 133 }; 134 135 void G1ConcurrentMarkThread::run_service() { 136 _vtime_start = os::elapsedVTime(); 137 138 while (!should_terminate()) { 139 if (wait_for_next_cycle()) { 140 break; 141 } 142 run_cycle(); 143 } 144 _cm->root_regions()->cancel_scan(); 145 } 146 147 void G1ConcurrentMarkThread::stop_service() { 148 MutexLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag); 149 CGC_lock->notify_all(); 150 } 151 152 bool G1ConcurrentMarkThread::wait_for_next_cycle() { 153 assert(!in_progress(), "should have been cleared"); 154 155 MonitorLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag); 156 while (!started() && !should_terminate()) { 157 ml.wait(); 158 } 159 160 if (started()) { 161 set_in_progress(); 162 } 163 164 return should_terminate(); 165 } 166 167 void G1ConcurrentMarkThread::run_cycle() { 168 enum ConcurrentCyclePhase { 169 CycleStart, 170 CLDClearClaimedMarks, 171 ScanRootRegions, 172 MarkFromRoots, 173 Preclean, 174 DelayToKeepMMUBeforeRemark, 175 PauseRemark, 176 RebuildRememberedSets, 177 DelayToKeepMMUBeforeCleanup, 178 PauseCleanup, 179 CleanupForNextMark, 180 CycleDone 181 }; 182 183 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 184 G1Policy* policy = g1h->policy(); 185 186 ConcurrentCyclePhase cur_state = CycleStart; 187 ConcurrentCyclePhase next_state; 188 189 GCIdMark gc_id_mark; 190 191 GCTraceConcTime(Info, gc) tt("Concurrent Cycle"); 192 double cycle_start; 193 194 Ticks mark_start; 195 Ticks mark_end; 196 uint iter = 1; 197 198 while (!_cm->has_aborted() && cur_state != CycleDone) { 199 HandleMark hm; 200 ResourceMark rm; 201 202 switch (cur_state) { 203 case CycleStart: { 204 concurrent_cycle_start(); 205 next_state = CLDClearClaimedMarks; 206 break; 207 } 208 case CLDClearClaimedMarks: { 209 G1ConcPhaseTimer p(_cm, "Concurrent Clear Claimed Marks"); 210 ClassLoaderDataGraph::clear_claimed_marks(); 211 next_state = ScanRootRegions; 212 break; 213 } 214 case ScanRootRegions: { 215 G1ConcPhaseTimer p(_cm, "Concurrent Scan Root Regions"); 216 _cm->scan_root_regions(); 217 218 // Note: ConcurrentGCBreakpoints before here risk deadlock, 219 // because a young GC must wait for root region scanning. 220 221 // It would be nice to use the G1ConcPhaseTimer class here but 222 // the "end" logging is inside the loop and not at the end of 223 // a scope. 224 cycle_start = os::elapsedVTime(); 225 mark_start = Ticks::now()(); 226 log_info(gc, marking)("Concurrent Mark (%.3fs)", mark_start.seconds()); 227 next_state = MarkFromRoots; 228 break; 229 } 230 case MarkFromRoots: { 231 ConcurrentGCBreakpoints::at("AFTER MARKING STARTED"); 232 G1ConcPhaseTimer p(_cm, "Concurrent Mark From Roots"); 233 _cm->mark_from_roots(); 234 next_state = G1UseReferencePrecleaning ? Preclean : DelayToKeepMMUBeforeRemark; 235 break; 236 } 237 case Preclean: { 238 G1ConcPhaseTimer p(_cm, "Concurrent Preclean"); 239 _cm->preclean(); 240 next_state = DelayToKeepMMUBeforeRemark; 241 break; 242 } 243 case DelayToKeepMMUBeforeRemark: { 244 double mark_end_time = os::elapsedVTime(); 245 mark_end = Ticks::now(); 246 _vtime_mark_accum += (mark_end_time - cycle_start); 247 delay_to_keep_mmu(policy, true /* remark */); 248 next_state = PauseRemark; 249 break; 250 } 251 case PauseRemark: { 252 // Pause Remark. 253 ConcurrentGCBreakpoints::at("BEFORE MARKING COMPLETED"); 254 log_info(gc, marking)("Concurrent Mark (%.3fs, %.3fs) %.3fms", 255 mark_start.seconds(), 256 mark_end.seconds(), 257 (mark_end - mark_start).seconds() * 1000.0); 258 CMRemark cl(_cm); 259 VM_G1Concurrent op(&cl, "Pause Remark"); 260 VMThread::execute(&op); 261 if (_cm->restart_for_overflow()) { 262 log_info(gc, marking)("Concurrent Mark Restart for Mark Stack Overflow (iteration #%u)", 263 iter); 264 iter++; 265 next_state = MarkFromRoots; 266 } else { 267 next_state = RebuildRememberedSets; 268 } 269 break; 270 } 271 case RebuildRememberedSets: { 272 G1ConcPhaseTimer p(_cm, "Concurrent Rebuild Remembered Sets"); 273 _cm->rebuild_rem_set_concurrently(); 274 next_state = DelayToKeepMMUBeforeCleanup; 275 break; 276 } 277 case DelayToKeepMMUBeforeCleanup: { 278 double end_time = os::elapsedVTime(); 279 // Update the total virtual time before doing this, since it will try 280 // to measure it to get the vtime for this marking. 281 _vtime_accum = (end_time - _vtime_start); 282 delay_to_keep_mmu(policy, false /* cleanup */); 283 next_state = PauseCleanup; 284 break; 285 } 286 case PauseCleanup: { 287 CMCleanup cl_cl(_cm); 288 VM_G1Concurrent op(&cl_cl, "Pause Cleanup"); 289 VMThread::execute(&op); 290 next_state = CleanupForNextMark; 291 break; 292 } 293 case CleanupForNextMark: { 294 G1ConcPhaseTimer p(_cm, "Concurrent Cleanup for Next Mark"); 295 _cm->cleanup_for_next_mark(); 296 next_state = CycleDone; 297 break; 298 } 299 default: { 300 guarantee(false, "Invalid state %u", cur_state); 301 break; 302 } 303 } 304 cur_state = next_state; 305 } 306 307 concurrent_cycle_end(); 308 } 309 310 void G1ConcurrentMarkThread::concurrent_cycle_start() { 311 _cm->concurrent_cycle_start(); 312 } 313 314 void G1ConcurrentMarkThread::concurrent_cycle_end() { 315 // Update the number of full collections that have been 316 // completed. This will also notify the G1OldGCCount_lock in case a 317 // Java thread is waiting for a full GC to happen (e.g., it 318 // called System.gc() with +ExplicitGCInvokesConcurrent). 319 SuspendibleThreadSetJoiner sts_join; 320 G1CollectedHeap::heap()->increment_old_marking_cycles_completed(true /* concurrent */, 321 !_cm->has_aborted() /* liveness_complete */); 322 323 _cm->concurrent_cycle_end(); 324 ConcurrentGCBreakpoints::notify_active_to_idle(); 325 }