61 CMCheckpointRootsFinalClosure(ConcurrentMark* cm) : 62 _cm(cm) {} 63 64 void do_void(){ 65 _cm->checkpointRootsFinal(false); // !clear_all_soft_refs 66 } 67 }; 68 69 class CMCleanUp: public VoidClosure { 70 ConcurrentMark* _cm; 71 public: 72 73 CMCleanUp(ConcurrentMark* cm) : 74 _cm(cm) {} 75 76 void do_void(){ 77 _cm->cleanup(); 78 } 79 }; 80 81 82 83 void ConcurrentMarkThread::run() { 84 initialize_in_thread(); 85 _vtime_start = os::elapsedVTime(); 86 wait_for_universe_init(); 87 88 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 89 G1CollectorPolicy* g1_policy = g1h->g1_policy(); 90 G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker(); 91 Thread *current_thread = Thread::current(); 92 93 while (!_should_terminate) { 94 // wait until started is set. 95 sleepBeforeNextCycle(); 96 if (_should_terminate) { 97 break; 98 } 99 100 { 101 ResourceMark rm; 102 HandleMark hm; 103 double cycle_start = os::elapsedVTime(); 104 105 // We have to ensure that we finish scanning the root regions 106 // before the next GC takes place. To ensure this we have to 107 // make sure that we do not join the STS until the root regions 108 // have been scanned. If we did then it's possible that a 109 // subsequent GC could block us from joining the STS and proceed 110 // without the root regions have been scanned which would be a 111 // correctness issue. 112 113 double scan_start = os::elapsedTime(); 114 if (!cm()->has_aborted()) { 115 if (G1Log::fine()) { 116 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 117 gclog_or_tty->print_cr("[GC concurrent-root-region-scan-start]"); 118 } 119 120 _cm->scanRootRegions(); 121 122 double scan_end = os::elapsedTime(); 123 if (G1Log::fine()) { 124 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 125 gclog_or_tty->print_cr("[GC concurrent-root-region-scan-end, %1.7lf secs]", 126 scan_end - scan_start); 127 } 128 } 129 130 double mark_start_sec = os::elapsedTime(); 131 if (G1Log::fine()) { 132 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 133 gclog_or_tty->print_cr("[GC concurrent-mark-start]"); 134 } 135 136 int iter = 0; 137 do { 138 iter++; 139 if (!cm()->has_aborted()) { 140 _cm->markFromRoots(); 141 } 142 143 double mark_end_time = os::elapsedVTime(); 144 double mark_end_sec = os::elapsedTime(); 145 _vtime_mark_accum += (mark_end_time - cycle_start); 146 if (!cm()->has_aborted()) { 147 if (g1_policy->adaptive_young_list_length()) { 148 double now = os::elapsedTime(); 149 double remark_prediction_ms = g1_policy->predict_remark_time_ms(); 150 jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms); 151 os::sleep(current_thread, sleep_time_ms, false); 152 } 153 154 if (G1Log::fine()) { 155 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 156 gclog_or_tty->print_cr("[GC concurrent-mark-end, %1.7lf secs]", 157 mark_end_sec - mark_start_sec); 158 } 159 160 CMCheckpointRootsFinalClosure final_cl(_cm); 161 VM_CGC_Operation op(&final_cl, "GC remark", true /* needs_pll */); 162 VMThread::execute(&op); 163 } 164 if (cm()->restart_for_overflow()) { 165 if (G1TraceMarkStackOverflow) { 166 gclog_or_tty->print_cr("Restarting conc marking because of MS overflow " 167 "in remark (restart #%d).", iter); 168 } 169 if (G1Log::fine()) { 170 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 171 gclog_or_tty->print_cr("[GC concurrent-mark-restart-for-overflow]"); 172 } 173 } 174 } while (cm()->restart_for_overflow()); 175 176 double end_time = os::elapsedVTime(); 177 // Update the total virtual time before doing this, since it will try 178 // to measure it to get the vtime for this marking. We purposely 179 // neglect the presumably-short "completeCleanup" phase here. 180 _vtime_accum = (end_time - _vtime_start); 181 182 if (!cm()->has_aborted()) { 183 if (g1_policy->adaptive_young_list_length()) { 184 double now = os::elapsedTime(); 185 double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms(); 186 jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms); 187 os::sleep(current_thread, sleep_time_ms, false); 188 } 189 190 CMCleanUp cl_cl(_cm); 191 VM_CGC_Operation op(&cl_cl, "GC cleanup", false /* needs_pll */); 192 VMThread::execute(&op); 193 } else { 194 // We don't want to update the marking status if a GC pause 195 // is already underway. 196 SuspendibleThreadSetJoiner sts_join; 197 g1h->collector_state()->set_mark_in_progress(false); 198 } 199 200 // Check if cleanup set the free_regions_coming flag. If it 201 // hasn't, we can just skip the next step. 202 if (g1h->free_regions_coming()) { 203 // The following will finish freeing up any regions that we 204 // found to be empty during cleanup. We'll do this part 205 // without joining the suspendible set. If an evacuation pause 206 // takes place, then we would carry on freeing regions in 207 // case they are needed by the pause. If a Full GC takes 208 // place, it would wait for us to process the regions 209 // reclaimed by cleanup. 210 211 double cleanup_start_sec = os::elapsedTime(); 212 if (G1Log::fine()) { 213 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 214 gclog_or_tty->print_cr("[GC concurrent-cleanup-start]"); 215 } 216 217 // Now do the concurrent cleanup operation. 218 _cm->completeCleanup(); 219 220 // Notify anyone who's waiting that there are no more free 221 // regions coming. We have to do this before we join the STS 222 // (in fact, we should not attempt to join the STS in the 223 // interval between finishing the cleanup pause and clearing 224 // the free_regions_coming flag) otherwise we might deadlock: 225 // a GC worker could be blocked waiting for the notification 226 // whereas this thread will be blocked for the pause to finish 227 // while it's trying to join the STS, which is conditional on 228 // the GC workers finishing. 229 g1h->reset_free_regions_coming(); 230 231 double cleanup_end_sec = os::elapsedTime(); 232 if (G1Log::fine()) { 233 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 234 gclog_or_tty->print_cr("[GC concurrent-cleanup-end, %1.7lf secs]", 235 cleanup_end_sec - cleanup_start_sec); 236 } 237 } 238 guarantee(cm()->cleanup_list_is_empty(), 239 "at this point there should be no regions on the cleanup list"); 240 241 // There is a tricky race before recording that the concurrent 242 // cleanup has completed and a potential Full GC starting around 243 // the same time. We want to make sure that the Full GC calls 244 // abort() on concurrent mark after 245 // record_concurrent_mark_cleanup_completed(), since abort() is 246 // the method that will reset the concurrent mark state. If we 247 // end up calling record_concurrent_mark_cleanup_completed() 248 // after abort() then we might incorrectly undo some of the work 249 // abort() did. Checking the has_aborted() flag after joining 250 // the STS allows the correct ordering of the two methods. There 251 // are two scenarios: 252 // 253 // a) If we reach here before the Full GC, the fact that we have 254 // joined the STS means that the Full GC cannot start until we 255 // leave the STS, so record_concurrent_mark_cleanup_completed() 256 // will complete before abort() is called. 257 // 258 // b) If we reach here during the Full GC, we'll be held up from 259 // joining the STS until the Full GC is done, which means that 260 // abort() will have completed and has_aborted() will return 261 // true to prevent us from calling 262 // record_concurrent_mark_cleanup_completed() (and, in fact, it's 263 // not needed any more as the concurrent mark state has been 264 // already reset). 265 { 266 SuspendibleThreadSetJoiner sts_join; 267 if (!cm()->has_aborted()) { 268 g1_policy->record_concurrent_mark_cleanup_completed(); 269 } 270 } 271 272 if (cm()->has_aborted()) { 273 if (G1Log::fine()) { 274 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 275 gclog_or_tty->print_cr("[GC concurrent-mark-abort]"); 276 } 277 } 278 279 // We now want to allow clearing of the marking bitmap to be 280 // suspended by a collection pause. 281 // We may have aborted just before the remark. Do not bother clearing the 282 // bitmap then, as it has been done during mark abort. 283 if (!cm()->has_aborted()) { 284 _cm->clearNextBitmap(); 285 } else { 286 assert(!G1VerifyBitmaps || _cm->nextMarkBitmapIsClear(), "Next mark bitmap must be clear"); 287 } 288 } 289 290 // Update the number of full collections that have been 291 // completed. This will also notify the FullGCCount_lock in case a 292 // Java thread is waiting for a full GC to happen (e.g., it 293 // called System.gc() with +ExplicitGCInvokesConcurrent). 294 { 295 SuspendibleThreadSetJoiner sts_join; | 61 CMCheckpointRootsFinalClosure(ConcurrentMark* cm) : 62 _cm(cm) {} 63 64 void do_void(){ 65 _cm->checkpointRootsFinal(false); // !clear_all_soft_refs 66 } 67 }; 68 69 class CMCleanUp: public VoidClosure { 70 ConcurrentMark* _cm; 71 public: 72 73 CMCleanUp(ConcurrentMark* cm) : 74 _cm(cm) {} 75 76 void do_void(){ 77 _cm->cleanup(); 78 } 79 }; 80 81 // We want to avoid that the logging from the concurrent thread is mixed 82 // with the logging from a STW GC. So, if necessary join the STS to ensure 83 // that the logging is done either before or after the STW logging. 84 void ConcurrentMarkThread::cm_log(bool doit, bool join_sts, const char* fmt, ...) { 85 if (doit) { 86 SuspendibleThreadSetJoiner sts_joiner(join_sts); 87 va_list args; 88 va_start(args, fmt); 89 gclog_or_tty->gclog_stamp(cm()->concurrent_gc_id()); 90 gclog_or_tty->vprint_cr(fmt, args); 91 va_end(args); 92 } 93 } 94 95 void ConcurrentMarkThread::run() { 96 initialize_in_thread(); 97 _vtime_start = os::elapsedVTime(); 98 wait_for_universe_init(); 99 100 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 101 G1CollectorPolicy* g1_policy = g1h->g1_policy(); 102 G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker(); 103 Thread *current_thread = Thread::current(); 104 105 while (!_should_terminate) { 106 // wait until started is set. 107 sleepBeforeNextCycle(); 108 if (_should_terminate) { 109 break; 110 } 111 112 { 113 ResourceMark rm; 114 HandleMark hm; 115 double cycle_start = os::elapsedVTime(); 116 117 // We have to ensure that we finish scanning the root regions 118 // before the next GC takes place. To ensure this we have to 119 // make sure that we do not join the STS until the root regions 120 // have been scanned. If we did then it's possible that a 121 // subsequent GC could block us from joining the STS and proceed 122 // without the root regions have been scanned which would be a 123 // correctness issue. 124 125 double scan_start = os::elapsedTime(); 126 if (!cm()->has_aborted()) { 127 cm_log(G1Log::fine(), false, "[GC concurrent-root-region-scan-start]"); 128 129 _cm->scanRootRegions(); 130 131 cm_log(G1Log::fine(), false, "[GC concurrent-root-region-scan-end, %1.7lf secs]", os::elapsedTime() - scan_start); 132 } 133 134 double mark_start_sec = os::elapsedTime(); 135 cm_log(G1Log::fine(), true, "[GC concurrent-mark-start]"); 136 137 int iter = 0; 138 do { 139 iter++; 140 if (!cm()->has_aborted()) { 141 _cm->markFromRoots(); 142 } 143 144 double mark_end_time = os::elapsedVTime(); 145 double mark_end_sec = os::elapsedTime(); 146 _vtime_mark_accum += (mark_end_time - cycle_start); 147 if (!cm()->has_aborted()) { 148 if (g1_policy->adaptive_young_list_length()) { 149 double now = os::elapsedTime(); 150 double remark_prediction_ms = g1_policy->predict_remark_time_ms(); 151 jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms); 152 os::sleep(current_thread, sleep_time_ms, false); 153 } 154 155 cm_log(G1Log::fine(), true, "[GC concurrent-mark-end, %1.7lf secs]", mark_end_sec - mark_start_sec); 156 157 CMCheckpointRootsFinalClosure final_cl(_cm); 158 VM_CGC_Operation op(&final_cl, "GC remark", true /* needs_pll */); 159 VMThread::execute(&op); 160 } 161 if (cm()->restart_for_overflow()) { 162 cm_log(G1TraceMarkStackOverflow, true, "Restarting conc marking because of MS overflow in remark (restart #%d).", iter); 163 cm_log(G1Log::fine(), true, "[GC concurrent-mark-restart-for-overflow]"); 164 } 165 } while (cm()->restart_for_overflow()); 166 167 double end_time = os::elapsedVTime(); 168 // Update the total virtual time before doing this, since it will try 169 // to measure it to get the vtime for this marking. We purposely 170 // neglect the presumably-short "completeCleanup" phase here. 171 _vtime_accum = (end_time - _vtime_start); 172 173 if (!cm()->has_aborted()) { 174 if (g1_policy->adaptive_young_list_length()) { 175 double now = os::elapsedTime(); 176 double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms(); 177 jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms); 178 os::sleep(current_thread, sleep_time_ms, false); 179 } 180 181 CMCleanUp cl_cl(_cm); 182 VM_CGC_Operation op(&cl_cl, "GC cleanup", false /* needs_pll */); 183 VMThread::execute(&op); 184 } else { 185 // We don't want to update the marking status if a GC pause 186 // is already underway. 187 SuspendibleThreadSetJoiner sts_join; 188 g1h->collector_state()->set_mark_in_progress(false); 189 } 190 191 // Check if cleanup set the free_regions_coming flag. If it 192 // hasn't, we can just skip the next step. 193 if (g1h->free_regions_coming()) { 194 // The following will finish freeing up any regions that we 195 // found to be empty during cleanup. We'll do this part 196 // without joining the suspendible set. If an evacuation pause 197 // takes place, then we would carry on freeing regions in 198 // case they are needed by the pause. If a Full GC takes 199 // place, it would wait for us to process the regions 200 // reclaimed by cleanup. 201 202 double cleanup_start_sec = os::elapsedTime(); 203 cm_log(G1Log::fine(), true, "[GC concurrent-cleanup-start]"); 204 205 // Now do the concurrent cleanup operation. 206 _cm->completeCleanup(); 207 208 // Notify anyone who's waiting that there are no more free 209 // regions coming. We have to do this before we join the STS 210 // (in fact, we should not attempt to join the STS in the 211 // interval between finishing the cleanup pause and clearing 212 // the free_regions_coming flag) otherwise we might deadlock: 213 // a GC worker could be blocked waiting for the notification 214 // whereas this thread will be blocked for the pause to finish 215 // while it's trying to join the STS, which is conditional on 216 // the GC workers finishing. 217 g1h->reset_free_regions_coming(); 218 219 double cleanup_end_sec = os::elapsedTime(); 220 cm_log(G1Log::fine(), true, "[GC concurrent-cleanup-end, %1.7lf secs]", cleanup_end_sec - cleanup_start_sec); 221 } 222 guarantee(cm()->cleanup_list_is_empty(), 223 "at this point there should be no regions on the cleanup list"); 224 225 // There is a tricky race before recording that the concurrent 226 // cleanup has completed and a potential Full GC starting around 227 // the same time. We want to make sure that the Full GC calls 228 // abort() on concurrent mark after 229 // record_concurrent_mark_cleanup_completed(), since abort() is 230 // the method that will reset the concurrent mark state. If we 231 // end up calling record_concurrent_mark_cleanup_completed() 232 // after abort() then we might incorrectly undo some of the work 233 // abort() did. Checking the has_aborted() flag after joining 234 // the STS allows the correct ordering of the two methods. There 235 // are two scenarios: 236 // 237 // a) If we reach here before the Full GC, the fact that we have 238 // joined the STS means that the Full GC cannot start until we 239 // leave the STS, so record_concurrent_mark_cleanup_completed() 240 // will complete before abort() is called. 241 // 242 // b) If we reach here during the Full GC, we'll be held up from 243 // joining the STS until the Full GC is done, which means that 244 // abort() will have completed and has_aborted() will return 245 // true to prevent us from calling 246 // record_concurrent_mark_cleanup_completed() (and, in fact, it's 247 // not needed any more as the concurrent mark state has been 248 // already reset). 249 { 250 SuspendibleThreadSetJoiner sts_join; 251 if (!cm()->has_aborted()) { 252 g1_policy->record_concurrent_mark_cleanup_completed(); 253 } else { 254 cm_log(G1Log::fine(), false, "[GC concurrent-mark-abort]"); 255 } 256 } 257 258 // We now want to allow clearing of the marking bitmap to be 259 // suspended by a collection pause. 260 // We may have aborted just before the remark. Do not bother clearing the 261 // bitmap then, as it has been done during mark abort. 262 if (!cm()->has_aborted()) { 263 _cm->clearNextBitmap(); 264 } else { 265 assert(!G1VerifyBitmaps || _cm->nextMarkBitmapIsClear(), "Next mark bitmap must be clear"); 266 } 267 } 268 269 // Update the number of full collections that have been 270 // completed. This will also notify the FullGCCount_lock in case a 271 // Java thread is waiting for a full GC to happen (e.g., it 272 // called System.gc() with +ExplicitGCInvokesConcurrent). 273 { 274 SuspendibleThreadSetJoiner sts_join; |