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     if (check_cancellation()) return;
 202 
 203     VM_ShenandoahFinalUpdateRefs final_update_refs;
 204 
 205     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross);
 206     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::final_update_refs_gross);
 207     VMThread::execute(&final_update_refs);
 208     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::final_update_refs_gross);
 209     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross);
 210   }
 211 
 212   // Prepare for the next normal cycle:
 213   if (check_cancellation()) return;
 214 
 215   {
 216     GCTraceTime(Info, gc) time("Concurrent reset bitmaps", gc_timer, GCCause::_no_gc);
 217     heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::reset_bitmaps);
 218     WorkGang* workers = heap->workers();
 219     ShenandoahPushWorkerScope scope(workers, heap->max_workers());
 220     heap->reset_next_mark_bitmap(workers);
 221     heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::reset_bitmaps);
 222   }
 223 
 224   gc_timer->register_gc_end();
 225 }
 226 
 227 bool ShenandoahConcurrentThread::check_cancellation() {
 228   ShenandoahHeap* heap = ShenandoahHeap::heap();
 229   if (heap->cancelled_concgc()) {
 230     assert (is_full_gc() || in_graceful_shutdown(), "Cancel GC either for Full GC, or gracefully exiting");
 231     heap->gc_timer()->register_gc_end();
 232     return true;
 233   }
 234   return false;
 235 }
 236 
 237 
 238 void ShenandoahConcurrentThread::stop_service() {
 239   // Nothing to do here.
 240 }
 241 
 242 void ShenandoahConcurrentThread::service_fullgc_cycle() {
 243   GCIdMark gc_id_mark;
 244   ShenandoahHeap* heap = ShenandoahHeap::heap();
 245 
 246   {
 247     if (_full_gc_cause == GCCause::_allocation_failure) {
 248       heap->shenandoahPolicy()->record_allocation_failure_gc();
 249     } else {
 250       heap->shenandoahPolicy()->record_user_requested_gc();
 251     }
 252 
 253     TraceCollectorStats tcs(heap->monitoring_support()->full_collection_counters());
 254     TraceMemoryManagerStats tmms(true, _full_gc_cause);
 255     VM_ShenandoahFullGC full_gc(_full_gc_cause);
 256     VMThread::execute(&full_gc);
 257   }
 258 
 259   reset_full_gc();
 260 }
 261 
 262 void ShenandoahConcurrentThread::do_full_gc(GCCause::Cause cause) {
 263   assert(Thread::current()->is_Java_thread(), "expect Java thread here");
 264 
 265   if (try_set_full_gc()) {
 266     _full_gc_cause = cause;
 267 
 268     // Now that full GC is scheduled, we can abort everything else
 269     ShenandoahHeap::heap()->cancel_concgc(cause);
 270   } else {
 271     if (_full_gc_cause != cause) {
 272       log_info(gc)("Full GC is already pending with cause: %s; new cause is %s",
 273                    GCCause::to_string(_full_gc_cause),
 274                    GCCause::to_string(cause));
 275     }
 276   }
 277 
 278   MonitorLockerEx ml(&_full_gc_lock);
 279   while (is_full_gc()) {
 280     ml.wait();
 281   }
 282   assert(!is_full_gc(), "expect full GC to have completed");
 283 }
 284 
 285 void ShenandoahConcurrentThread::reset_full_gc() {
 286   OrderAccess::release_store_fence(&_do_full_gc, 0);
 287   MonitorLockerEx ml(&_full_gc_lock);
 288   ml.notify_all();
 289 }
 290 
 291 bool ShenandoahConcurrentThread::try_set_full_gc() {
 292   jbyte old = Atomic::cmpxchg(1, &_do_full_gc, 0);
 293   return old == 0; // success
 294 }
 295 
 296 bool ShenandoahConcurrentThread::is_full_gc() {
 297   return OrderAccess::load_acquire(&_do_full_gc) == 1;
 298 }
 299 
 300 void ShenandoahConcurrentThread::print() const {
 301   print_on(tty);
 302 }
 303 
 304 void ShenandoahConcurrentThread::print_on(outputStream* st) const {
 305   st->print("Shenandoah Concurrent Thread");
 306   Thread::print_on(st);
 307   st->cr();
 308 }
 309 
 310 void ShenandoahConcurrentThread::sleepBeforeNextCycle() {
 311   assert(false, "Wake up in the GC thread that never sleeps :-)");
 312 }
 313 
 314 void ShenandoahConcurrentThread::start() {
 315   create_and_start();
 316 }
 317 
 318 void ShenandoahConcurrentThread::prepare_for_graceful_shutdown() {
 319   OrderAccess::release_store_fence(&_graceful_shutdown, 1);
 320 }
 321 
 322 bool ShenandoahConcurrentThread::in_graceful_shutdown() {
 323   return OrderAccess::load_acquire(&_graceful_shutdown) == 1;
 324 }