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 if (!cm()->has_aborted()) { 126 _cm->scanRootRegions(); 127 } 128 129 double mark_start_sec = os::elapsedTime(); 130 cm_log(G1Log::fine(), true, "[GC concurrent-mark-start]"); 131 132 int iter = 0; 133 do { 134 iter++; 135 if (!cm()->has_aborted()) { 136 _cm->markFromRoots(); 137 } 138 139 double mark_end_time = os::elapsedVTime(); 140 double mark_end_sec = os::elapsedTime(); 141 _vtime_mark_accum += (mark_end_time - cycle_start); 142 if (!cm()->has_aborted()) { 143 if (g1_policy->adaptive_young_list_length()) { 144 double now = os::elapsedTime(); 145 double remark_prediction_ms = g1_policy->predict_remark_time_ms(); 146 jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms); 147 os::sleep(current_thread, sleep_time_ms, false); 148 } 149 150 cm_log(G1Log::fine(), true, "[GC concurrent-mark-end, %1.7lf secs]", mark_end_sec - mark_start_sec); 151 152 CMCheckpointRootsFinalClosure final_cl(_cm); 153 VM_CGC_Operation op(&final_cl, "GC remark", true /* needs_pll */); 154 VMThread::execute(&op); 155 } 156 if (cm()->restart_for_overflow()) { 157 cm_log(G1TraceMarkStackOverflow, true, "Restarting conc marking because of MS overflow in remark (restart #%d).", iter); 158 cm_log(G1Log::fine(), true, "[GC concurrent-mark-restart-for-overflow]"); 159 } 160 } while (cm()->restart_for_overflow()); 161 162 double end_time = os::elapsedVTime(); 163 // Update the total virtual time before doing this, since it will try 164 // to measure it to get the vtime for this marking. We purposely 165 // neglect the presumably-short "completeCleanup" phase here. 166 _vtime_accum = (end_time - _vtime_start); 167 168 if (!cm()->has_aborted()) { 169 if (g1_policy->adaptive_young_list_length()) { 170 double now = os::elapsedTime(); 171 double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms(); 172 jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms); 173 os::sleep(current_thread, sleep_time_ms, false); 174 } 175 176 CMCleanUp cl_cl(_cm); 177 VM_CGC_Operation op(&cl_cl, "GC cleanup", false /* needs_pll */); 178 VMThread::execute(&op); 179 } else { 180 // We don't want to update the marking status if a GC pause 181 // is already underway. 182 SuspendibleThreadSetJoiner sts_join; 183 g1h->collector_state()->set_mark_in_progress(false); 184 } 185 186 // Check if cleanup set the free_regions_coming flag. If it 187 // hasn't, we can just skip the next step. 188 if (g1h->free_regions_coming()) { 189 // The following will finish freeing up any regions that we 190 // found to be empty during cleanup. We'll do this part 191 // without joining the suspendible set. If an evacuation pause 192 // takes place, then we would carry on freeing regions in 193 // case they are needed by the pause. If a Full GC takes 194 // place, it would wait for us to process the regions 195 // reclaimed by cleanup. 196 197 double cleanup_start_sec = os::elapsedTime(); 198 cm_log(G1Log::fine(), true, "[GC concurrent-cleanup-start]"); 199 200 // Now do the concurrent cleanup operation. 201 _cm->completeCleanup(); 202 203 // Notify anyone who's waiting that there are no more free 204 // regions coming. We have to do this before we join the STS 205 // (in fact, we should not attempt to join the STS in the 206 // interval between finishing the cleanup pause and clearing 207 // the free_regions_coming flag) otherwise we might deadlock: 208 // a GC worker could be blocked waiting for the notification 209 // whereas this thread will be blocked for the pause to finish 210 // while it's trying to join the STS, which is conditional on 211 // the GC workers finishing. 212 g1h->reset_free_regions_coming(); 213 214 double cleanup_end_sec = os::elapsedTime(); 215 cm_log(G1Log::fine(), true, "[GC concurrent-cleanup-end, %1.7lf secs]", cleanup_end_sec - cleanup_start_sec); 216 } 217 guarantee(cm()->cleanup_list_is_empty(), 218 "at this point there should be no regions on the cleanup list"); 219 220 // There is a tricky race before recording that the concurrent 221 // cleanup has completed and a potential Full GC starting around 222 // the same time. We want to make sure that the Full GC calls 223 // abort() on concurrent mark after 224 // record_concurrent_mark_cleanup_completed(), since abort() is 225 // the method that will reset the concurrent mark state. If we 226 // end up calling record_concurrent_mark_cleanup_completed() 227 // after abort() then we might incorrectly undo some of the work 228 // abort() did. Checking the has_aborted() flag after joining 229 // the STS allows the correct ordering of the two methods. There 230 // are two scenarios: 231 // 232 // a) If we reach here before the Full GC, the fact that we have 233 // joined the STS means that the Full GC cannot start until we 234 // leave the STS, so record_concurrent_mark_cleanup_completed() 235 // will complete before abort() is called. 236 // 237 // b) If we reach here during the Full GC, we'll be held up from 238 // joining the STS until the Full GC is done, which means that 239 // abort() will have completed and has_aborted() will return 240 // true to prevent us from calling 241 // record_concurrent_mark_cleanup_completed() (and, in fact, it's 242 // not needed any more as the concurrent mark state has been 243 // already reset). 244 { 245 SuspendibleThreadSetJoiner sts_join; 246 if (!cm()->has_aborted()) { 247 g1_policy->record_concurrent_mark_cleanup_completed(); 248 } else { 249 cm_log(G1Log::fine(), false, "[GC concurrent-mark-abort]"); 250 } 251 } 252 253 // We now want to allow clearing of the marking bitmap to be 254 // suspended by a collection pause. 255 // We may have aborted just before the remark. Do not bother clearing the 256 // bitmap then, as it has been done during mark abort. 257 if (!cm()->has_aborted()) { 258 _cm->clearNextBitmap(); 259 } else { 260 assert(!G1VerifyBitmaps || _cm->nextMarkBitmapIsClear(), "Next mark bitmap must be clear"); 261 } 262 } 263 264 // Update the number of full collections that have been 265 // completed. This will also notify the FullGCCount_lock in case a 266 // Java thread is waiting for a full GC to happen (e.g., it 267 // called System.gc() with +ExplicitGCInvokesConcurrent). 268 { 269 SuspendibleThreadSetJoiner sts_join; |