1 /*
   2  * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shared/gcTraceTime.inline.hpp"
  26 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  27 #include "gc/shenandoah/shenandoahConcurrentThread.hpp"
  28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  31 #include "gc/shenandoah/vm_operations_shenandoah.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "memory/universe.hpp"
  34 #include "runtime/vmThread.hpp"
  35 
  36 ShenandoahConcurrentThread::ShenandoahConcurrentThread() :
  37   ConcurrentGCThread(),
  38   _full_gc_lock(Mutex::leaf, "ShenandoahFullGC_lock", true, Monitor::_safepoint_check_always),
  39   _do_full_gc(false),
  40   _graceful_shutdown(0)
  41 {
  42   create_and_start();
  43 }
  44 
  45 ShenandoahConcurrentThread::~ShenandoahConcurrentThread() {
  46   // This is here so that super is called.
  47 }
  48 
  49 void ShenandoahConcurrentThread::run_service() {
  50   ShenandoahHeap* heap = ShenandoahHeap::heap();
  51 
  52   while (!should_terminate()) {
  53     if (in_graceful_shutdown()) {
  54       break;
  55     } else if (is_full_gc()) {
  56       service_fullgc_cycle();
  57     } else if (heap->shenandoahPolicy()->should_start_partial_gc()) {
  58       service_partial_cycle();
  59     } else if (heap->shenandoahPolicy()->should_start_concurrent_mark(heap->used(), heap->capacity())) {
  60       service_normal_cycle();
  61       if (heap->is_evacuation_in_progress()) {
  62         heap->set_evacuation_in_progress_concurrently(false);
  63       }
  64       if (heap->is_update_refs_in_progress()) {
  65         heap->set_update_refs_in_progress(false);
  66       }
  67     } else {
  68       Thread::current()->_ParkEvent->park(10);
  69     }
  70     heap->monitoring_support()->update_counters();
  71 
  72     // Make sure the _do_full_gc flag changes are seen.
  73     OrderAccess::storeload();
  74   }
  75 
  76   // Wait for the actual stop(), can't leave run_service() earlier.
  77   while (!should_terminate()) {
  78     Thread::current()->_ParkEvent->park(10);
  79   }
  80 }
  81 
  82 void ShenandoahConcurrentThread::service_partial_cycle() {
  83   GCIdMark gc_id_mark;
  84   ShenandoahHeap* heap = ShenandoahHeap::heap();
  85 
  86   VM_ShenandoahPartialGC partial_gc;
  87   heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross);
  88   heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::partial_gc_gross);
  89   VMThread::execute(&partial_gc);
  90   heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::partial_gc_gross);
  91   heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross);
  92 }
  93 
  94 void ShenandoahConcurrentThread::service_normal_cycle() {
  95   if (check_cancellation()) return;
  96 
  97   ShenandoahHeap* heap = ShenandoahHeap::heap();
  98 
  99   GCTimer* gc_timer = heap->gc_timer();
 100 
 101   gc_timer->register_gc_start();
 102 
 103   heap->shenandoahPolicy()->increase_cycle_counter();
 104 
 105   GCIdMark gc_id_mark;
 106   TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
 107   TraceMemoryManagerStats tmms(false, GCCause::_no_cause_specified);
 108 
 109   // Start initial mark under STW:
 110   {
 111     // Workers are setup by VM_ShenandoahInitMark
 112     TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
 113     VM_ShenandoahInitMark initMark;
 114     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross);
 115     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::init_mark_gross);
 116     VMThread::execute(&initMark);
 117     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::init_mark_gross);
 118     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross);
 119   }
 120 
 121   if (check_cancellation()) return;
 122 
 123   // Continue concurrent mark:
 124   {
 125     // Setup workers for concurrent marking phase
 126     WorkGang* workers = heap->workers();
 127     uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_conc_marking(workers->active_workers(),
 128                                                                               (uint) Threads::number_of_non_daemon_threads());
 129     ShenandoahWorkerScope scope(workers, n_workers);
 130 
 131     GCTraceTime(Info, gc) time("Concurrent marking", gc_timer, GCCause::_no_gc, true);
 132     TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
 133     ShenandoahHeap::heap()->concurrentMark()->mark_from_roots();
 134   }
 135 
 136   // Possibly hand over remaining marking work to final-mark phase.
 137   bool clear_full_gc = false;
 138   if (heap->cancelled_concgc()) {
 139     heap->shenandoahPolicy()->record_cm_cancelled();
 140     if (_full_gc_cause == GCCause::_allocation_failure &&
 141         heap->shenandoahPolicy()->handover_cancelled_marking()) {
 142       heap->clear_cancelled_concgc();
 143       clear_full_gc = true;
 144       heap->shenandoahPolicy()->record_cm_degenerated();
 145     } else {
 146       heap->gc_timer()->register_gc_end();
 147       return;
 148     }
 149   } else {
 150     heap->shenandoahPolicy()->record_cm_success();
 151   }
 152 
 153   // Proceed to complete marking under STW, and start evacuation:
 154   {
 155     // Workers are setup by VM_ShenandoahFinalMarkStartEvac
 156     TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters());
 157     VM_ShenandoahFinalMarkStartEvac finishMark;
 158     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross);
 159     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::final_mark_gross);
 160     VMThread::execute(&finishMark);
 161     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::final_mark_gross);
 162     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross);
 163   }
 164 
 165   if (check_cancellation()) return;
 166 
 167   // If we handed off remaining marking work above, we need to kick off waiting Java threads
 168   if (clear_full_gc) {
 169     reset_full_gc();
 170   }
 171 
 172   // Continue concurrent evacuation:
 173   {
 174     // Setup workers for concurrent evacuation phase
 175     WorkGang* workers = heap->workers();
 176     uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_conc_evacuation(workers->active_workers(),
 177                                                                                  (uint) Threads::number_of_non_daemon_threads());
 178     ShenandoahWorkerScope scope(workers, n_workers);
 179 
 180     GCTraceTime(Info, gc) time("Concurrent evacuation ", gc_timer, GCCause::_no_gc, true);
 181     TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
 182     heap->do_evacuation();
 183   }
 184 
 185   // Do an update-refs phase if required.
 186   if (check_cancellation()) return;
 187 
 188   if (heap->shenandoahPolicy()->update_refs_early()) {
 189 
 190     VM_ShenandoahInitUpdateRefs init_update_refs;
 191     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross);
 192     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::init_update_refs_gross);
 193     VMThread::execute(&init_update_refs);
 194     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::init_update_refs_gross);
 195     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross);
 196 
 197     {
 198       GCTraceTime(Info, gc) time("Concurrent update references ", gc_timer, GCCause::_no_gc, true);
 199       heap->concurrent_update_heap_references();
 200     }
 201 
 202     clear_full_gc = false;
 203     if (heap->cancelled_concgc()) {
 204       heap->shenandoahPolicy()->record_uprefs_cancelled();
 205       if (_full_gc_cause == GCCause::_allocation_failure &&
 206           heap->shenandoahPolicy()->handover_cancelled_uprefs()) {
 207         clear_full_gc = true;
 208         heap->shenandoahPolicy()->record_uprefs_degenerated();
 209       } else {
 210         heap->gc_timer()->register_gc_end();
 211         return;
 212       }
 213     } else {
 214       heap->shenandoahPolicy()->record_uprefs_success();
 215     }
 216 
 217     VM_ShenandoahFinalUpdateRefs final_update_refs;
 218 
 219     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross);
 220     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::final_update_refs_gross);
 221     VMThread::execute(&final_update_refs);
 222     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::final_update_refs_gross);
 223     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross);
 224   }
 225 
 226   // Prepare for the next normal cycle:
 227   if (check_cancellation()) return;
 228 
 229   if (clear_full_gc) {
 230     reset_full_gc();
 231   }
 232 
 233   {
 234     GCTraceTime(Info, gc) time("Concurrent reset bitmaps", gc_timer, GCCause::_no_gc);
 235     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::reset_bitmaps);
 236     WorkGang* workers = heap->workers();
 237     ShenandoahPushWorkerScope scope(workers, heap->max_workers());
 238     heap->reset_next_mark_bitmap(workers);
 239     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::reset_bitmaps);
 240   }
 241 
 242   gc_timer->register_gc_end();
 243 }
 244 
 245 bool ShenandoahConcurrentThread::check_cancellation() {
 246   ShenandoahHeap* heap = ShenandoahHeap::heap();
 247   if (heap->cancelled_concgc()) {
 248     assert (is_full_gc() || in_graceful_shutdown(), "Cancel GC either for Full GC, or gracefully exiting");
 249     heap->gc_timer()->register_gc_end();
 250     return true;
 251   }
 252   return false;
 253 }
 254 
 255 
 256 void ShenandoahConcurrentThread::stop_service() {
 257   // Nothing to do here.
 258 }
 259 
 260 void ShenandoahConcurrentThread::service_fullgc_cycle() {
 261   GCIdMark gc_id_mark;
 262   ShenandoahHeap* heap = ShenandoahHeap::heap();
 263 
 264   {
 265     if (_full_gc_cause == GCCause::_allocation_failure) {
 266       heap->shenandoahPolicy()->record_allocation_failure_gc();
 267     } else {
 268       heap->shenandoahPolicy()->record_user_requested_gc();
 269     }
 270 
 271     TraceCollectorStats tcs(heap->monitoring_support()->full_collection_counters());
 272     TraceMemoryManagerStats tmms(true, _full_gc_cause);
 273     VM_ShenandoahFullGC full_gc(_full_gc_cause);
 274     VMThread::execute(&full_gc);
 275   }
 276 
 277   reset_full_gc();
 278 }
 279 
 280 void ShenandoahConcurrentThread::do_full_gc(GCCause::Cause cause) {
 281   assert(Thread::current()->is_Java_thread(), "expect Java thread here");
 282 
 283   if (try_set_full_gc()) {
 284     _full_gc_cause = cause;
 285 
 286     // Now that full GC is scheduled, we can abort everything else
 287     ShenandoahHeap::heap()->cancel_concgc(cause);
 288   } else {
 289     if (_full_gc_cause != cause) {
 290       log_info(gc)("Full GC is already pending with cause: %s; new cause is %s",
 291                    GCCause::to_string(_full_gc_cause),
 292                    GCCause::to_string(cause));
 293     }
 294   }
 295 
 296   MonitorLockerEx ml(&_full_gc_lock);
 297   while (is_full_gc()) {
 298     ml.wait();
 299   }
 300   assert(!is_full_gc(), "expect full GC to have completed");
 301 }
 302 
 303 void ShenandoahConcurrentThread::reset_full_gc() {
 304   OrderAccess::release_store_fence(&_do_full_gc, 0);
 305   MonitorLockerEx ml(&_full_gc_lock);
 306   ml.notify_all();
 307 }
 308 
 309 bool ShenandoahConcurrentThread::try_set_full_gc() {
 310   jbyte old = Atomic::cmpxchg(1, &_do_full_gc, 0);
 311   return old == 0; // success
 312 }
 313 
 314 bool ShenandoahConcurrentThread::is_full_gc() {
 315   return OrderAccess::load_acquire(&_do_full_gc) == 1;
 316 }
 317 
 318 void ShenandoahConcurrentThread::print() const {
 319   print_on(tty);
 320 }
 321 
 322 void ShenandoahConcurrentThread::print_on(outputStream* st) const {
 323   st->print("Shenandoah Concurrent Thread");
 324   Thread::print_on(st);
 325   st->cr();
 326 }
 327 
 328 void ShenandoahConcurrentThread::sleepBeforeNextCycle() {
 329   assert(false, "Wake up in the GC thread that never sleeps :-)");
 330 }
 331 
 332 void ShenandoahConcurrentThread::start() {
 333   create_and_start();
 334 }
 335 
 336 void ShenandoahConcurrentThread::prepare_for_graceful_shutdown() {
 337   OrderAccess::release_store_fence(&_graceful_shutdown, 1);
 338 }
 339 
 340 bool ShenandoahConcurrentThread::in_graceful_shutdown() {
 341   return OrderAccess::load_acquire(&_graceful_shutdown) == 1;
 342 }