1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.
   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 
  26 #include "jfr/jfrEvents.hpp"
  27 #include "gc/shared/gcCause.hpp"
  28 #include "gc/shared/gcTimer.hpp"
  29 #include "gc/shared/gcTrace.hpp"
  30 #include "gc/shared/gcWhen.hpp"
  31 #include "gc/shenandoah/shenandoahAllocTracker.hpp"
  32 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  33 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  34 #include "gc/shenandoah/shenandoahHeap.hpp"
  35 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  36 #include "gc/shenandoah/shenandoahUtils.hpp"
  37 
  38 ShenandoahPhaseTimings::Phase ShenandoahGCPhase::_current_phase = ShenandoahGCPhase::_invalid_phase;
  39 
  40 ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause) :
  41   _heap(ShenandoahHeap::heap()),
  42   _timer(_heap->gc_timer()),
  43   _tracer(_heap->tracer()) {
  44   assert(!ShenandoahGCPhase::is_valid_phase(ShenandoahGCPhase::current_phase()),
  45     "No current GC phase");
  46 
  47   _timer->register_gc_start();
  48   _tracer->report_gc_start(cause, _timer->gc_start());
  49   _heap->trace_heap(GCWhen::BeforeGC, _tracer);
  50 
  51   _heap->shenandoah_policy()->record_cycle_start();
  52   _heap->heuristics()->record_cycle_start();
  53   _trace_cycle.initialize(_heap->cycle_memory_manager(), _heap->gc_cause(),
  54           /* allMemoryPoolsAffected */    true,
  55           /* recordGCBeginTime = */       true,
  56           /* recordPreGCUsage = */        true,
  57           /* recordPeakUsage = */         true,
  58           /* recordPostGCUsage = */       true,
  59           /* recordAccumulatedGCTime = */ true,
  60           /* recordGCEndTime = */         true,
  61           /* countCollection = */         true
  62   );
  63 }
  64 
  65 ShenandoahGCSession::~ShenandoahGCSession() {
  66   _heap->heuristics()->record_cycle_end();
  67   _timer->register_gc_end();
  68   _heap->trace_heap(GCWhen::AfterGC, _tracer);
  69   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
  70   assert(!ShenandoahGCPhase::is_valid_phase(ShenandoahGCPhase::current_phase()),
  71     "No current GC phase");
  72 }
  73 
  74 ShenandoahGCPauseMark::ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type) :
  75   _heap(ShenandoahHeap::heap()), _gc_id_mark(gc_id), _svc_gc_mark(type), _is_gc_active_mark() {
  76 
  77   // FIXME: It seems that JMC throws away level 0 events, which are the Shenandoah
  78   // pause events. Create this pseudo level 0 event to push real events to level 1.
  79   _heap->gc_timer()->register_gc_phase_start("Shenandoah", Ticks::now());
  80   _trace_pause.initialize(_heap->stw_memory_manager(), _heap->gc_cause(),
  81           /* allMemoryPoolsAffected */    true,
  82           /* recordGCBeginTime = */       true,
  83           /* recordPreGCUsage = */        false,
  84           /* recordPeakUsage = */         false,
  85           /* recordPostGCUsage = */       false,
  86           /* recordAccumulatedGCTime = */ true,
  87           /* recordGCEndTime = */         true,
  88           /* countCollection = */         true
  89   );
  90 
  91   _heap->heuristics()->record_gc_start();
  92 }
  93 
  94 ShenandoahGCPauseMark::~ShenandoahGCPauseMark() {
  95   _heap->gc_timer()->register_gc_phase_end(Ticks::now());
  96   _heap->heuristics()->record_gc_end();
  97 }
  98 
  99 ShenandoahGCPhase::ShenandoahGCPhase(const ShenandoahPhaseTimings::Phase phase) :
 100   _heap(ShenandoahHeap::heap()), _phase(phase) {
 101   assert(Thread::current()->is_VM_thread() ||
 102          Thread::current()->is_ConcurrentGC_thread(),
 103         "Must be set by these threads");
 104   _parent_phase = _current_phase;
 105   _current_phase = phase;
 106 
 107   _heap->phase_timings()->record_phase_start(_phase);
 108 }
 109 
 110 ShenandoahGCPhase::~ShenandoahGCPhase() {
 111   _heap->phase_timings()->record_phase_end(_phase);
 112   _current_phase = _parent_phase;
 113 }
 114 
 115 bool ShenandoahGCPhase::is_valid_phase(ShenandoahPhaseTimings::Phase phase) {
 116   return phase >= 0 && phase < ShenandoahPhaseTimings::_num_phases;
 117 }
 118 
 119 bool ShenandoahGCPhase::is_root_work_phase() {
 120   switch(current_phase()) {
 121     case ShenandoahPhaseTimings::scan_roots:
 122     case ShenandoahPhaseTimings::update_roots:
 123     case ShenandoahPhaseTimings::init_evac:
 124     case ShenandoahPhaseTimings::final_update_refs_roots:
 125     case ShenandoahPhaseTimings::degen_gc_update_roots:
 126     case ShenandoahPhaseTimings::init_traversal_gc_work:
 127     case ShenandoahPhaseTimings::final_traversal_gc_work:
 128     case ShenandoahPhaseTimings::final_traversal_update_roots:
 129     case ShenandoahPhaseTimings::full_gc_roots:
 130       return true;
 131     default:
 132       return false;
 133   }
 134 }
 135 
 136 ShenandoahAllocTrace::ShenandoahAllocTrace(size_t words_size, ShenandoahAllocRequest::Type alloc_type) {
 137   if (ShenandoahAllocationTrace) {
 138     _start = os::elapsedTime();
 139     _size = words_size;
 140     _alloc_type = alloc_type;
 141   } else {
 142     _start = 0;
 143     _size = 0;
 144     _alloc_type = ShenandoahAllocRequest::Type(0);
 145   }
 146 }
 147 
 148 ShenandoahAllocTrace::~ShenandoahAllocTrace() {
 149   if (ShenandoahAllocationTrace) {
 150     double stop = os::elapsedTime();
 151     double duration_sec = stop - _start;
 152     double duration_us = duration_sec * 1000000;
 153     ShenandoahAllocTracker* tracker = ShenandoahHeap::heap()->alloc_tracker();
 154     assert(tracker != NULL, "Must be");
 155     tracker->record_alloc_latency(_size, _alloc_type, duration_us);
 156     if (duration_us > ShenandoahAllocationStallThreshold) {
 157       log_warning(gc)("Allocation stall: %.0f us (threshold: " INTX_FORMAT " us)",
 158                       duration_us, ShenandoahAllocationStallThreshold);
 159     }
 160   }
 161 }
 162 
 163 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
 164   Thread* thr = Thread::current();
 165   assert(ShenandoahThreadLocalData::worker_id(thr) == ShenandoahThreadLocalData::INVALID_WORKER_ID, "Already set");
 166   ShenandoahThreadLocalData::set_worker_id(thr, worker_id);
 167 }
 168 
 169 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
 170   _event.commit(GCId::current(), ShenandoahPhaseTimings::phase_name(ShenandoahGCPhase::current_phase()));
 171 }
 172 
 173 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
 174   _event.commit(GCId::current(), _worker_id, ShenandoahPhaseTimings::phase_name(ShenandoahGCPhase::current_phase()));
 175 }
 176 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 177 #ifdef ASSERT
 178   Thread* thr = Thread::current();
 179   assert(ShenandoahThreadLocalData::worker_id(thr) != ShenandoahThreadLocalData::INVALID_WORKER_ID, "Must be set");
 180   ShenandoahThreadLocalData::set_worker_id(thr, ShenandoahThreadLocalData::INVALID_WORKER_ID);
 181 #endif
 182 }