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/shenandoahCollectorPolicy.hpp"
  27 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  28 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahPartialGC.hpp"
  31 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
  32 #include "gc/shenandoah/vm_operations_shenandoah.hpp"
  33 
  34 void VM_ShenandoahInitMark::doit() {
  35   GCIdMark gc_id_mark(_gc_id);
  36   ShenandoahHeap *sh = (ShenandoahHeap*) Universe::heap();
  37   WorkGang*       workers = sh->workers();
  38 
  39   // Calculate workers for initial marking
  40   uint nworkers = ShenandoahCollectorPolicy::calc_workers_for_init_marking(
  41     workers->active_workers(), (uint)Threads::number_of_non_daemon_threads());
  42 
  43   ShenandoahWorkerScope scope(workers, nworkers);
  44 
  45   GCTraceTime(Info, gc) time("Pause Init Mark", sh->gc_timer());
  46   sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause);
  47   sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::init_mark);
  48 
  49   assert(sh->is_next_bitmap_clear(), "need clear marking bitmap");
  50 
  51   sh->start_concurrent_marking();
  52   if (UseTLAB) {
  53     sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::resize_tlabs);
  54     sh->resize_all_tlabs();
  55     sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::resize_tlabs);
  56   }
  57 
  58   sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::init_mark);
  59   sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause);
  60 
  61 }
  62 
  63 void VM_ShenandoahFullGC::doit() {
  64   GCIdMark gc_id_mark(_gc_id);
  65   ShenandoahHeap *sh = ShenandoahHeap::heap();
  66   sh->shenandoahPolicy()->record_gc_start();
  67   ShenandoahMarkCompact::do_mark_compact(_gc_cause);
  68   if (UseTLAB) {
  69     sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::resize_tlabs);
  70     sh->resize_all_tlabs();
  71     sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::resize_tlabs);
  72   }
  73   sh->shenandoahPolicy()->record_gc_end();
  74 }
  75 
  76 bool VM_ShenandoahReferenceOperation::doit_prologue() {
  77   Heap_lock->lock();
  78   return true;
  79 }
  80 
  81 void VM_ShenandoahReferenceOperation::doit_epilogue() {
  82   if (Universe::has_reference_pending_list()) {
  83     Heap_lock->notify_all();
  84   }
  85   Heap_lock->unlock();
  86 }
  87 
  88 void VM_ShenandoahStartEvacuation::doit() {
  89   GCIdMark gc_id_mark(_gc_id);
  90 
  91   ShenandoahHeap *sh = ShenandoahHeap::heap();
  92   sh->shenandoahPolicy()->record_gc_start();
  93 
  94   // It is critical that we
  95   // evacuate roots right after finishing marking, so that we don't
  96   // get unmarked objects in the roots.
  97   // Setup workers for final marking
  98   WorkGang* workers = sh->workers();
  99   uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_final_marking(workers->active_workers(),
 100                                                                              (uint)Threads::number_of_non_daemon_threads());
 101   ShenandoahWorkerScope scope(workers, n_workers);
 102 
 103   if (! sh->cancelled_concgc()) {
 104     GCTraceTime(Info, gc) time("Pause Final Mark", sh->gc_timer(), GCCause::_no_gc, true);
 105     sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause);
 106     sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::final_mark);
 107     sh->concurrentMark()->finish_mark_from_roots();
 108     sh->stop_concurrent_marking();
 109     sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::final_mark);
 110     sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause);
 111 
 112     sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::prepare_evac);
 113     sh->prepare_for_concurrent_evacuation();
 114     sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::prepare_evac);
 115 
 116     sh->set_evacuation_in_progress_at_safepoint(true);
 117 
 118     // From here on, we need to update references.
 119     sh->set_need_update_refs(true);
 120 
 121     sh->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::init_evac);
 122     sh->evacuate_and_update_roots();
 123     sh->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::init_evac);
 124 
 125   } else {
 126     GCTraceTime(Info, gc) time("Cancel Concurrent Mark", sh->gc_timer(), GCCause::_no_gc, true);
 127     sh->concurrentMark()->cancel();
 128     sh->stop_concurrent_marking();
 129   }
 130 
 131   sh->shenandoahPolicy()->record_gc_end();
 132 }
 133 
 134 void VM_ShenandoahPartialGC::doit() {
 135   GCIdMark gc_id_mark(_gc_id);
 136   ShenandoahHeap *sh = ShenandoahHeap::heap();
 137   sh->shenandoahPolicy()->record_gc_start();
 138   ShenandoahHeap::heap()->do_partial_collection();
 139   sh->shenandoahPolicy()->record_gc_end();
 140 }
 141 
 142 void VM_ShenandoahVerifyHeapAfterEvacuation::doit() {
 143   GCIdMark gc_id_mark(_gc_id);
 144   ShenandoahHeap *sh = ShenandoahHeap::heap();
 145   sh->verify_heap_after_evacuation();
 146 }