src/share/vm/gc_implementation/g1/concurrentMarkThread.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>


  76 
  77 
  78 
  79 void ConcurrentMarkThread::run() {
  80   initialize_in_thread();
  81   _vtime_start = os::elapsedVTime();
  82   wait_for_universe_init();
  83 
  84   G1CollectedHeap* g1h = G1CollectedHeap::heap();
  85   G1CollectorPolicy* g1_policy = g1h->g1_policy();
  86   G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker();
  87   Thread *current_thread = Thread::current();
  88 
  89   while (!_should_terminate) {
  90     // wait until started is set.
  91     sleepBeforeNextCycle();
  92     {
  93       ResourceMark rm;
  94       HandleMark   hm;
  95       double cycle_start = os::elapsedVTime();
  96       char verbose_str[128];
  97 
  98       // We have to ensure that we finish scanning the root regions
  99       // before the next GC takes place. To ensure this we have to
 100       // make sure that we do not join the STS until the root regions
 101       // have been scanned. If we did then it's possible that a
 102       // subsequent GC could block us from joining the STS and proceed
 103       // without the root regions have been scanned which would be a
 104       // correctness issue.
 105 
 106       double scan_start = os::elapsedTime();
 107       if (!cm()->has_aborted()) {
 108         if (G1Log::fine()) {
 109           gclog_or_tty->date_stamp(PrintGCDateStamps);
 110           gclog_or_tty->stamp(PrintGCTimeStamps);
 111           gclog_or_tty->print_cr("[GC concurrent-root-region-scan-start]");
 112         }
 113 
 114         _cm->scanRootRegions();
 115 
 116         double scan_end = os::elapsedTime();


 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           if (G1Log::fine()) {
 151             gclog_or_tty->date_stamp(PrintGCDateStamps);
 152             gclog_or_tty->stamp(PrintGCTimeStamps);
 153             gclog_or_tty->print_cr("[GC concurrent-mark-end, %1.7lf secs]",
 154                                       mark_end_sec - mark_start_sec);
 155           }
 156 
 157           CMCheckpointRootsFinalClosure final_cl(_cm);
 158           sprintf(verbose_str, "GC remark");
 159           VM_CGC_Operation op(&final_cl, verbose_str, true /* needs_pll */);
 160           VMThread::execute(&op);
 161         }
 162         if (cm()->restart_for_overflow()) {
 163           if (G1TraceMarkStackOverflow) {
 164             gclog_or_tty->print_cr("Restarting conc marking because of MS overflow "
 165                                    "in remark (restart #%d).", iter);
 166           }
 167           if (G1Log::fine()) {
 168             gclog_or_tty->date_stamp(PrintGCDateStamps);
 169             gclog_or_tty->stamp(PrintGCTimeStamps);
 170             gclog_or_tty->print_cr("[GC concurrent-mark-restart-for-overflow]");
 171           }
 172         }
 173       } while (cm()->restart_for_overflow());
 174 
 175       double end_time = os::elapsedVTime();
 176       // Update the total virtual time before doing this, since it will try
 177       // to measure it to get the vtime for this marking.  We purposely
 178       // neglect the presumably-short "completeCleanup" phase here.
 179       _vtime_accum = (end_time - _vtime_start);
 180 
 181       if (!cm()->has_aborted()) {
 182         if (g1_policy->adaptive_young_list_length()) {
 183           double now = os::elapsedTime();
 184           double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms();
 185           jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms);
 186           os::sleep(current_thread, sleep_time_ms, false);
 187         }
 188 
 189         CMCleanUp cl_cl(_cm);
 190         sprintf(verbose_str, "GC cleanup");
 191         VM_CGC_Operation op(&cl_cl, verbose_str, 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         _sts.join();
 197         g1h->set_marking_complete();
 198         _sts.leave();
 199       }
 200 
 201       // Check if cleanup set the free_regions_coming flag. If it
 202       // hasn't, we can just skip the next step.
 203       if (g1h->free_regions_coming()) {
 204         // The following will finish freeing up any regions that we
 205         // found to be empty during cleanup. We'll do this part
 206         // without joining the suspendible set. If an evacuation pause
 207         // takes place, then we would carry on freeing regions in
 208         // case they are needed by the pause. If a Full GC takes
 209         // place, it would wait for us to process the regions
 210         // reclaimed by cleanup.
 211 


 275         if (G1Log::fine()) {
 276           gclog_or_tty->date_stamp(PrintGCDateStamps);
 277           gclog_or_tty->stamp(PrintGCTimeStamps);
 278           gclog_or_tty->print_cr("[GC concurrent-mark-abort]");
 279         }
 280       }
 281 
 282       // We now want to allow clearing of the marking bitmap to be
 283       // suspended by a collection pause.
 284       _sts.join();
 285       _cm->clearNextBitmap();
 286       _sts.leave();
 287     }
 288 
 289     // Update the number of full collections that have been
 290     // completed. This will also notify the FullGCCount_lock in case a
 291     // Java thread is waiting for a full GC to happen (e.g., it
 292     // called System.gc() with +ExplicitGCInvokesConcurrent).
 293     _sts.join();
 294     g1h->increment_old_marking_cycles_completed(true /* concurrent */);

 295     _sts.leave();
 296   }
 297   assert(_should_terminate, "just checking");
 298 
 299   terminate();
 300 }
 301 
 302 
 303 void ConcurrentMarkThread::yield() {
 304   _sts.yield("Concurrent Mark");
 305 }
 306 
 307 void ConcurrentMarkThread::stop() {
 308   // it is ok to take late safepoints here, if needed
 309   MutexLockerEx mu(Terminator_lock);
 310   _should_terminate = true;
 311   while (!_has_terminated) {
 312     Terminator_lock->wait();
 313   }
 314 }




  76 
  77 
  78 
  79 void ConcurrentMarkThread::run() {
  80   initialize_in_thread();
  81   _vtime_start = os::elapsedVTime();
  82   wait_for_universe_init();
  83 
  84   G1CollectedHeap* g1h = G1CollectedHeap::heap();
  85   G1CollectorPolicy* g1_policy = g1h->g1_policy();
  86   G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker();
  87   Thread *current_thread = Thread::current();
  88 
  89   while (!_should_terminate) {
  90     // wait until started is set.
  91     sleepBeforeNextCycle();
  92     {
  93       ResourceMark rm;
  94       HandleMark   hm;
  95       double cycle_start = os::elapsedVTime();

  96 
  97       // We have to ensure that we finish scanning the root regions
  98       // before the next GC takes place. To ensure this we have to
  99       // make sure that we do not join the STS until the root regions
 100       // have been scanned. If we did then it's possible that a
 101       // subsequent GC could block us from joining the STS and proceed
 102       // without the root regions have been scanned which would be a
 103       // correctness issue.
 104 
 105       double scan_start = os::elapsedTime();
 106       if (!cm()->has_aborted()) {
 107         if (G1Log::fine()) {
 108           gclog_or_tty->date_stamp(PrintGCDateStamps);
 109           gclog_or_tty->stamp(PrintGCTimeStamps);
 110           gclog_or_tty->print_cr("[GC concurrent-root-region-scan-start]");
 111         }
 112 
 113         _cm->scanRootRegions();
 114 
 115         double scan_end = os::elapsedTime();


 137 
 138         double mark_end_time = os::elapsedVTime();
 139         double mark_end_sec = os::elapsedTime();
 140         _vtime_mark_accum += (mark_end_time - cycle_start);
 141         if (!cm()->has_aborted()) {
 142           if (g1_policy->adaptive_young_list_length()) {
 143             double now = os::elapsedTime();
 144             double remark_prediction_ms = g1_policy->predict_remark_time_ms();
 145             jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms);
 146             os::sleep(current_thread, sleep_time_ms, false);
 147           }
 148 
 149           if (G1Log::fine()) {
 150             gclog_or_tty->date_stamp(PrintGCDateStamps);
 151             gclog_or_tty->stamp(PrintGCTimeStamps);
 152             gclog_or_tty->print_cr("[GC concurrent-mark-end, %1.7lf secs]",
 153                                       mark_end_sec - mark_start_sec);
 154           }
 155 
 156           CMCheckpointRootsFinalClosure final_cl(_cm);
 157           VM_CGC_Operation op(&final_cl, "GC remark", true /* needs_pll */);

 158           VMThread::execute(&op);
 159         }
 160         if (cm()->restart_for_overflow()) {
 161           if (G1TraceMarkStackOverflow) {
 162             gclog_or_tty->print_cr("Restarting conc marking because of MS overflow "
 163                                    "in remark (restart #%d).", iter);
 164           }
 165           if (G1Log::fine()) {
 166             gclog_or_tty->date_stamp(PrintGCDateStamps);
 167             gclog_or_tty->stamp(PrintGCTimeStamps);
 168             gclog_or_tty->print_cr("[GC concurrent-mark-restart-for-overflow]");
 169           }
 170         }
 171       } while (cm()->restart_for_overflow());
 172 
 173       double end_time = os::elapsedVTime();
 174       // Update the total virtual time before doing this, since it will try
 175       // to measure it to get the vtime for this marking.  We purposely
 176       // neglect the presumably-short "completeCleanup" phase here.
 177       _vtime_accum = (end_time - _vtime_start);
 178 
 179       if (!cm()->has_aborted()) {
 180         if (g1_policy->adaptive_young_list_length()) {
 181           double now = os::elapsedTime();
 182           double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms();
 183           jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms);
 184           os::sleep(current_thread, sleep_time_ms, false);
 185         }
 186 
 187         CMCleanUp cl_cl(_cm);
 188         VM_CGC_Operation op(&cl_cl, "GC cleanup", false /* needs_pll */);

 189         VMThread::execute(&op);
 190       } else {
 191         // We don't want to update the marking status if a GC pause
 192         // is already underway.
 193         _sts.join();
 194         g1h->set_marking_complete();
 195         _sts.leave();
 196       }
 197 
 198       // Check if cleanup set the free_regions_coming flag. If it
 199       // hasn't, we can just skip the next step.
 200       if (g1h->free_regions_coming()) {
 201         // The following will finish freeing up any regions that we
 202         // found to be empty during cleanup. We'll do this part
 203         // without joining the suspendible set. If an evacuation pause
 204         // takes place, then we would carry on freeing regions in
 205         // case they are needed by the pause. If a Full GC takes
 206         // place, it would wait for us to process the regions
 207         // reclaimed by cleanup.
 208 


 272         if (G1Log::fine()) {
 273           gclog_or_tty->date_stamp(PrintGCDateStamps);
 274           gclog_or_tty->stamp(PrintGCTimeStamps);
 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       _sts.join();
 282       _cm->clearNextBitmap();
 283       _sts.leave();
 284     }
 285 
 286     // Update the number of full collections that have been
 287     // completed. This will also notify the FullGCCount_lock in case a
 288     // Java thread is waiting for a full GC to happen (e.g., it
 289     // called System.gc() with +ExplicitGCInvokesConcurrent).
 290     _sts.join();
 291     g1h->increment_old_marking_cycles_completed(true /* concurrent */);
 292     g1h->register_concurrent_cycle_end();
 293     _sts.leave();
 294   }
 295   assert(_should_terminate, "just checking");
 296 
 297   terminate();
 298 }
 299 
 300 
 301 void ConcurrentMarkThread::yield() {
 302   _sts.yield("Concurrent Mark");
 303 }
 304 
 305 void ConcurrentMarkThread::stop() {
 306   // it is ok to take late safepoints here, if needed
 307   MutexLockerEx mu(Terminator_lock);
 308   _should_terminate = true;
 309   while (!_has_terminated) {
 310     Terminator_lock->wait();
 311   }
 312 }