< prev index next >

src/hotspot/share/gc/g1/g1ConcurrentMarkThread.cpp

Print this page
rev 60794 : imported patch 8247928-sjohanss-review


  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 
  46 // ======= Concurrent Mark Thread ========
  47 
  48 G1ConcurrentMarkThread::G1ConcurrentMarkThread(G1ConcurrentMark* cm) :
  49   ConcurrentGCThread(),
  50   _vtime_start(0.0),
  51   _vtime_accum(0.0),
  52   _vtime_mark_accum(0.0),
  53   _cm(cm),
  54   _state(Idle)
  55 {
  56   set_name("G1 Main Marker");
  57   create_and_start();
  58 }
  59 
  60 class CMRemark : public VoidClosure {
  61   G1ConcurrentMark* _cm;
  62 public:
  63   CMRemark(G1ConcurrentMark* cm) : _cm(cm) {}
  64 
  65   void do_void(){
  66     _cm->remark();
  67   }
  68 };
  69 
  70 class CMCleanup : public VoidClosure {
  71   G1ConcurrentMark* _cm;
  72 public:
  73   CMCleanup(G1ConcurrentMark* cm) : _cm(cm) {}
  74 
  75   void do_void(){
  76     _cm->cleanup();
  77   }
  78 };
  79 
  80 double G1ConcurrentMarkThread::mmu_delay_end(G1Policy* g1_policy, bool remark) {
  81   // There are 3 reasons to use SuspendibleThreadSetJoiner.
  82   // 1. To avoid concurrency problem.
  83   //    - G1MMUTracker::add_pause(), when_sec() and its variation(when_ms() etc..) can be called
  84   //      concurrently from ConcurrentMarkThread and VMThread.
  85   // 2. If currently a gc is running, but it has not yet updated the MMU,
  86   //    we will not forget to consider that pause in the MMU calculation.
  87   // 3. If currently a gc is running, ConcurrentMarkThread will wait it to be finished.
  88   //    And then sleep for predicted amount of time by delay_to_keep_mmu().
  89   SuspendibleThreadSetJoiner sts_join;
  90 
  91   const G1Analytics* analytics = g1_policy->analytics();
  92   double prediction_ms = remark ? analytics->predict_remark_time_ms()
  93                                 : analytics->predict_cleanup_time_ms();
  94   double prediction = prediction_ms / MILLIUNITS;
  95   G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker();
  96   double now = os::elapsedTime();
  97   return now + mmu_tracker->when_sec(now, prediction);
  98 }
  99 
 100 void G1ConcurrentMarkThread::delay_to_keep_mmu(G1Policy* g1_policy, bool remark) {
 101   if (g1_policy->use_adaptive_young_list_length()) {
 102     double delay_end_sec = mmu_delay_end(g1_policy, remark);


 103     // Wait for timeout or thread termination request.
 104     MonitorLocker ml(CGC_lock, Monitor::_no_safepoint_check_flag);
 105     while (!_cm->has_aborted()) {
 106       double sleep_time_sec = (delay_end_sec - os::elapsedTime());
 107       jlong sleep_time_ms = ceil(sleep_time_sec * MILLIUNITS);
 108       if (sleep_time_ms <= 0) {
 109         break;                  // Passed end time.
 110       } else if (ml.wait(sleep_time_ms, Monitor::_no_safepoint_check_flag)) {
 111         break;                  // Timeout => reached end time.
 112       } else if (should_terminate()) {
 113         break;                  // Wakeup for pending termination request.
 114       }
 115       // Other (possibly spurious) wakeup.  Retry with updated sleep time.
 116     }
 117   }
 118 }
 119 
 120 class G1ConcPhaseTimer : public GCTraceConcTimeImpl<LogLevel::Info, LOG_TAGS(gc, marking)> {
 121   G1ConcurrentMark* _cm;
 122 
 123  public:
 124   G1ConcPhaseTimer(G1ConcurrentMark* cm, const char* title) :
 125     GCTraceConcTimeImpl<LogLevel::Info,  LogTag::_gc, LogTag::_marking>(title),
 126     _cm(cm)
 127   {
 128     _cm->gc_timer_cm()->register_gc_concurrent_start(title);
 129   }
 130 
 131   ~G1ConcPhaseTimer() {
 132     _cm->gc_timer_cm()->register_gc_concurrent_end();
 133   }
 134 };
 135 
 136 void G1ConcurrentMarkThread::run_service() {
 137   _vtime_start = os::elapsedVTime();
 138 
 139   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 140   G1Policy* policy = g1h->policy();
 141 
 142   while (!should_terminate()) {
 143     // wait until started is set.
 144     sleep_before_next_cycle();
 145     if (should_terminate()) {
 146       break;
 147     }
 148 
 149     GCIdMark gc_id_mark;

 150 
 151     _cm->concurrent_cycle_start();


 152 
 153     GCTraceConcTime(Info, gc) tt("Concurrent Cycle");
 154     {
 155       ResourceMark rm;






 156 
 157       double cycle_start = os::elapsedVTime();

 158 
 159       {
 160         G1ConcPhaseTimer p(_cm, "Concurrent Clear Claimed Marks");
 161         ClassLoaderDataGraph::clear_claimed_marks();
 162       }
 163 
 164       // We have to ensure that we finish scanning the root regions
 165       // before the next GC takes place. To ensure this we have to
 166       // make sure that we do not join the STS until the root regions
 167       // have been scanned. If we did then it's possible that a
 168       // subsequent GC could block us from joining the STS and proceed
 169       // without the root regions have been scanned which would be a
 170       // correctness issue.
 171 
 172       {









 173         G1ConcPhaseTimer p(_cm, "Concurrent Scan Root Regions");
 174         _cm->scan_root_regions();















 175       }
 176 
 177       // Note: ConcurrentGCBreakpoints before here risk deadlock,
 178       // because a young GC must wait for root region scanning.
 179 
 180       // It would be nice to use the G1ConcPhaseTimer class here but
 181       // the "end" logging is inside the loop and not at the end of
 182       // a scope. Also, the timer doesn't support nesting.
 183       // Mimicking the same log output instead.
 184       jlong mark_start = os::elapsed_counter();
 185       log_info(gc, marking)("Concurrent Mark (%.3fs)",
 186                             TimeHelper::counter_to_seconds(mark_start));
 187       for (uint iter = 1; !_cm->has_aborted(); ++iter) {
 188         // Concurrent marking.
 189         {













 190           ConcurrentGCBreakpoints::at("AFTER MARKING STARTED");
 191           G1ConcPhaseTimer p(_cm, "Concurrent Mark From Roots");
 192           _cm->mark_from_roots();
 193         }
 194         if (_cm->has_aborted()) {
 195           break;
 196         }
 197 
 198         if (G1UseReferencePrecleaning) {
 199           G1ConcPhaseTimer p(_cm, "Concurrent Preclean");
 200           _cm->preclean();
 201         }
 202         if (_cm->has_aborted()) {
 203           break;
 204         }
 205 
 206         // Delay remark pause for MMU.
 207         double mark_end_time = os::elapsedVTime();
 208         jlong mark_end = os::elapsed_counter();
 209         _vtime_mark_accum += (mark_end_time - cycle_start);
 210         delay_to_keep_mmu(policy, true /* remark */);
 211         if (_cm->has_aborted()) {
 212           break;
 213         }
 214 
 215         // Pause Remark.
 216         ConcurrentGCBreakpoints::at("BEFORE MARKING COMPLETED");
 217         log_info(gc, marking)("Concurrent Mark (%.3fs, %.3fs) %.3fms",
 218                               TimeHelper::counter_to_seconds(mark_start),
 219                               TimeHelper::counter_to_seconds(mark_end),
 220                               TimeHelper::counter_to_millis(mark_end - mark_start));
 221         CMRemark cl(_cm);
 222         VM_G1Concurrent op(&cl, "Pause Remark");
 223         VMThread::execute(&op);
 224         if (_cm->has_aborted()) {
 225           break;
 226         } else if (!_cm->restart_for_overflow()) {
 227           break;                // Exit loop if no restart requested.
 228         } else {
 229           // Loop to restart for overflow.
 230           log_info(gc, marking)("Concurrent Mark Restart for Mark Stack Overflow (iteration #%u)",
 231                                 iter);
 232         }
 233       }
 234 
 235       if (!_cm->has_aborted()) {
 236         G1ConcPhaseTimer p(_cm, "Concurrent Rebuild Remembered Sets");
 237         _cm->rebuild_rem_set_concurrently();
 238       }

 239 
 240       double end_time = os::elapsedVTime();
 241       // Update the total virtual time before doing this, since it will try
 242       // to measure it to get the vtime for this marking.
 243       _vtime_accum = (end_time - _vtime_start);
 244 
 245       if (!_cm->has_aborted()) {
 246         delay_to_keep_mmu(policy, false /* cleanup */);
 247       }
 248 
 249       if (!_cm->has_aborted()) {
 250         CMCleanup cl_cl(_cm);
 251         VM_G1Concurrent op(&cl_cl, "Pause Cleanup");
 252         VMThread::execute(&op);
 253       }

 254 
 255       // We now want to allow clearing of the marking bitmap to be
 256       // suspended by a collection pause.
 257       // We may have aborted just before the remark. Do not bother clearing the
 258       // bitmap then, as it has been done during mark abort.
 259       if (!_cm->has_aborted()) {
 260         G1ConcPhaseTimer p(_cm, "Concurrent Cleanup for Next Mark");
 261         _cm->cleanup_for_next_mark();
 262       }
 263     }

















 264 

















 265     // Update the number of full collections that have been
 266     // completed. This will also notify the G1OldGCCount_lock in case a
 267     // Java thread is waiting for a full GC to happen (e.g., it
 268     // called System.gc() with +ExplicitGCInvokesConcurrent).
 269     {
 270       SuspendibleThreadSetJoiner sts_join;
 271       g1h->increment_old_marking_cycles_completed(true /* concurrent */,
 272                                                   !_cm->has_aborted() /* liveness_completed */);
 273 
 274       _cm->concurrent_cycle_end();
 275       ConcurrentGCBreakpoints::notify_active_to_idle();
 276     }
 277   }
 278   _cm->root_regions()->cancel_scan();
 279 }
 280 
 281 void G1ConcurrentMarkThread::stop_service() {
 282   MutexLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag);
 283   CGC_lock->notify_all();
 284 }
 285 
 286 
 287 void G1ConcurrentMarkThread::sleep_before_next_cycle() {
 288   // We join here because we don't want to do the "shouldConcurrentMark()"
 289   // below while the world is otherwise stopped.
 290   assert(!in_progress(), "should have been cleared");
 291 
 292   MonitorLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag);
 293   while (!started() && !should_terminate()) {
 294     ml.wait();
 295   }
 296 
 297   if (started()) {
 298     set_in_progress();
 299   }
 300 }


  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 G1ConcurrentMarkThread::G1ConcurrentMarkThread(G1ConcurrentMark* cm) :
  48   ConcurrentGCThread(),
  49   _vtime_start(0.0),
  50   _vtime_accum(0.0),

  51   _cm(cm),
  52   _state(Idle)
  53 {
  54   set_name("G1 Main Marker");
  55   create_and_start();
  56 }
  57 
  58 class CMRemark : public VoidClosure {
  59   G1ConcurrentMark* _cm;
  60 public:
  61   CMRemark(G1ConcurrentMark* cm) : _cm(cm) {}
  62 
  63   void do_void(){
  64     _cm->remark();
  65   }
  66 };
  67 
  68 class CMCleanup : public VoidClosure {
  69   G1ConcurrentMark* _cm;
  70 public:
  71   CMCleanup(G1ConcurrentMark* cm) : _cm(cm) {}
  72 
  73   void do_void(){
  74     _cm->cleanup();
  75   }
  76 };
  77 
  78 double G1ConcurrentMarkThread::mmu_delay_end(G1Policy* policy, bool remark) {
  79   // There are 3 reasons to use SuspendibleThreadSetJoiner.
  80   // 1. To avoid concurrency problem.
  81   //    - G1MMUTracker::add_pause(), when_sec() and its variation(when_ms() etc..) can be called
  82   //      concurrently from ConcurrentMarkThread and VMThread.
  83   // 2. If currently a gc is running, but it has not yet updated the MMU,
  84   //    we will not forget to consider that pause in the MMU calculation.
  85   // 3. If currently a gc is running, ConcurrentMarkThread will wait it to be finished.
  86   //    And then sleep for predicted amount of time by delay_to_keep_mmu().
  87   SuspendibleThreadSetJoiner sts_join;
  88 
  89   const G1Analytics* analytics = policy->analytics();
  90   double prediction_ms = remark ? analytics->predict_remark_time_ms()
  91                                 : analytics->predict_cleanup_time_ms();
  92   double prediction = prediction_ms / MILLIUNITS;
  93   G1MMUTracker *mmu_tracker = policy->mmu_tracker();
  94   double now = os::elapsedTime();
  95   return now + mmu_tracker->when_sec(now, prediction);
  96 }
  97 
  98 void G1ConcurrentMarkThread::delay_to_keep_mmu(bool remark) {
  99   G1Policy* policy = G1CollectedHeap::heap()->policy();
 100 
 101   if (policy->use_adaptive_young_list_length()) {
 102     double delay_end_sec = mmu_delay_end(policy, remark);
 103     // Wait for timeout or thread termination request.
 104     MonitorLocker ml(CGC_lock, Monitor::_no_safepoint_check_flag);
 105     while (!_cm->has_aborted() && !should_terminate()) {
 106       double sleep_time_sec = (delay_end_sec - os::elapsedTime());
 107       jlong sleep_time_ms = ceil(sleep_time_sec * MILLIUNITS);
 108       if (sleep_time_ms <= 0) {
 109         break;                  // Passed end time.
 110       } else if (ml.wait(sleep_time_ms, Monitor::_no_safepoint_check_flag)) {
 111         break;                  // Timeout => reached end time.


 112       }
 113       // Other (possibly spurious) wakeup.  Retry with updated sleep time.
 114     }
 115   }
 116 }
 117 
 118 class G1ConcPhaseTimer : public GCTraceConcTimeImpl<LogLevel::Info, LOG_TAGS(gc, marking)> {
 119   G1ConcurrentMark* _cm;
 120 
 121  public:
 122   G1ConcPhaseTimer(G1ConcurrentMark* cm, const char* title) :
 123     GCTraceConcTimeImpl<LogLevel::Info,  LogTag::_gc, LogTag::_marking>(title),
 124     _cm(cm)
 125   {
 126     _cm->gc_timer_cm()->register_gc_concurrent_start(title);
 127   }
 128 
 129   ~G1ConcPhaseTimer() {
 130     _cm->gc_timer_cm()->register_gc_concurrent_end();
 131   }
 132 };
 133 
 134 void G1ConcurrentMarkThread::run_service() {
 135   _vtime_start = os::elapsedVTime();
 136 



 137   while (!should_terminate()) {
 138     if (wait_for_next_cycle()) {


 139       break;
 140     }
 141 
 142     GCIdMark gc_id_mark;
 143     GCTraceConcTime(Info, gc) tt("Concurrent Cycle");
 144 
 145     concurrent_cycle_start();
 146     full_concurrent_cycle_do();
 147     concurrent_cycle_end();
 148 
 149     _vtime_accum = (os::elapsedVTime() - _vtime_start);
 150   }
 151   _cm->root_regions()->cancel_scan();
 152 }
 153 
 154 void G1ConcurrentMarkThread::stop_service() {
 155   MutexLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag);
 156   CGC_lock->notify_all();
 157 }
 158 
 159 bool G1ConcurrentMarkThread::wait_for_next_cycle() {
 160   assert(!in_progress(), "should have been cleared");
 161 
 162   MonitorLocker ml(CGC_lock, Mutex::_no_safepoint_check_flag);
 163   while (!started() && !should_terminate()) {
 164     ml.wait();
 165   }
 166 
 167   if (started()) {
 168     set_in_progress();
 169   }




 170 
 171   return should_terminate();
 172 }
 173 
 174 bool G1ConcurrentMarkThread::phase_clear_cld_claimed_marks() {
 175   G1ConcPhaseTimer p(_cm, "Concurrent Clear Claimed Marks");
 176   ClassLoaderDataGraph::clear_claimed_marks();
 177   return _cm->has_aborted();
 178 }
 179 
 180 bool G1ConcurrentMarkThread::phase_scan_root_regions() {
 181   G1ConcPhaseTimer p(_cm, "Concurrent Scan Root Regions");
 182   _cm->scan_root_regions();
 183   return _cm->has_aborted();
 184 }
 185 
 186 bool G1ConcurrentMarkThread::phase_mark_loop() {
 187   Ticks mark_start = Ticks::now();
 188   log_info(gc, marking)("Concurrent Mark (%.3fs)", mark_start.seconds());
 189 
 190   uint iter = 1;
 191   while (true) {
 192     // Subphase 1: Mark From Roots.
 193     if (subphase_mark_from_roots()) return true;
 194 
 195     // Subphase 2: Preclean (optional)
 196     if (G1UseReferencePrecleaning) {
 197       if (subphase_preclean()) return true;
 198     }
 199 
 200     // Subphase 3: Wait for Remark.
 201     if (subphase_delay_to_keep_mmu_before_remark()) return true;
 202 
 203     // Subphase 4: Remark pause
 204     if (subphase_remark()) return true;
 205 
 206     // Check if we need to restart the marking loop.
 207     if (!mark_loop_needs_restart()) break;
 208 
 209     log_info(gc, marking)("Concurrent Mark Restart for Mark Stack Overflow (iteration #%u)",
 210                           iter++);
 211   }
 212 
 213   Ticks mark_end = Ticks::now();
 214   log_info(gc, marking)("Concurrent Mark (%.3fs, %.3fs) %.3fms",
 215                         mark_start.seconds(), mark_end.seconds(),
 216                         (mark_end - mark_start).seconds() * 1000.0);
 217 
 218   return false;
 219 }
 220 
 221 bool G1ConcurrentMarkThread::mark_loop_needs_restart() const {
 222   return _cm->has_overflown();
 223 }
 224 
 225 bool G1ConcurrentMarkThread::subphase_mark_from_roots() {
 226   ConcurrentGCBreakpoints::at("AFTER MARKING STARTED");
 227   G1ConcPhaseTimer p(_cm, "Concurrent Mark From Roots");
 228   _cm->mark_from_roots();
 229   return _cm->has_aborted();
 230 }


 231 
 232 bool G1ConcurrentMarkThread::subphase_preclean() {
 233   G1ConcPhaseTimer p(_cm, "Concurrent Preclean");
 234   _cm->preclean();
 235   return _cm->has_aborted();
 236 }


 237 
 238 bool G1ConcurrentMarkThread::subphase_delay_to_keep_mmu_before_remark() {
 239   delay_to_keep_mmu(true /* remark */);
 240   return _cm->has_aborted();
 241 }




 242 
 243 bool G1ConcurrentMarkThread::subphase_remark() {
 244   ConcurrentGCBreakpoints::at("BEFORE MARKING COMPLETED");




 245   CMRemark cl(_cm);
 246   VM_G1Concurrent op(&cl, "Pause Remark");
 247   VMThread::execute(&op);
 248   return _cm->has_aborted();
 249 }








 250 
 251 bool G1ConcurrentMarkThread::phase_rebuild_remembered_sets() {
 252   G1ConcPhaseTimer p(_cm, "Concurrent Rebuild Remembered Sets");
 253   _cm->rebuild_rem_set_concurrently();
 254   return _cm->has_aborted();
 255 }
 256 
 257 bool G1ConcurrentMarkThread::phase_delay_to_keep_mmu_before_cleanup() {
 258   delay_to_keep_mmu(false /* cleanup */);
 259   return _cm->has_aborted();
 260 }




 261 
 262 bool G1ConcurrentMarkThread::phase_cleanup() {
 263   CMCleanup cl(_cm);
 264   VM_G1Concurrent op(&cl, "Pause Cleanup");
 265   VMThread::execute(&op);
 266   return _cm->has_aborted();
 267 }
 268 
 269 bool G1ConcurrentMarkThread::phase_clear_bitmap_for_next_mark() {




 270   G1ConcPhaseTimer p(_cm, "Concurrent Cleanup for Next Mark");
 271   _cm->cleanup_for_next_mark();
 272   return _cm->has_aborted();
 273 }
 274 
 275 void G1ConcurrentMarkThread::concurrent_cycle_start() {
 276   _cm->concurrent_cycle_start();
 277 }
 278 
 279 void G1ConcurrentMarkThread::full_concurrent_cycle_do() {
 280   HandleMark hm(Thread::current());
 281   ResourceMark rm;
 282 
 283   // Phase 1: Clear CLD claimed marks.
 284   phase_clear_cld_claimed_marks();
 285 
 286   // Do not return before the scan root regions phase as a GC waits for a
 287   // notification from it.
 288 
 289   // Phase 2: Scan root regions.
 290   if (phase_scan_root_regions()) return;
 291 
 292   // Phase 3: Actual mark loop.
 293   if (phase_mark_loop()) return;
 294 
 295   // Phase 4: Rebuild remembered sets.
 296   if (phase_rebuild_remembered_sets()) return;
 297 
 298   // Phase 5: Wait for Cleanup.
 299   if (phase_delay_to_keep_mmu_before_cleanup()) return;
 300 
 301   // Phase 6: Cleanup pause
 302   if (phase_cleanup()) return;
 303 
 304   // Phase 7: Clear bitmap for next mark.
 305   phase_clear_bitmap_for_next_mark();
 306 }
 307 
 308 void G1ConcurrentMarkThread::concurrent_cycle_end() {
 309   // Update the number of full collections that have been
 310   // completed. This will also notify the G1OldGCCount_lock in case a
 311   // Java thread is waiting for a full GC to happen (e.g., it
 312   // called System.gc() with +ExplicitGCInvokesConcurrent).

 313   SuspendibleThreadSetJoiner sts_join;
 314   G1CollectedHeap::heap()->increment_old_marking_cycles_completed(true /* concurrent */,
 315                                                                   !_cm->has_aborted());
 316 
 317   _cm->concurrent_cycle_end();
 318   ConcurrentGCBreakpoints::notify_active_to_idle();
























 319 }
< prev index next >