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