1 /*
   2  * Copyright (c) 2017, 2019, 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 #include "utilities/debug.hpp"
  38 
  39 ShenandoahPhaseTimings::Phase ShenandoahGCPhase::_current_phase = ShenandoahGCPhase::_invalid_phase;
  40 
  41 ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause) :
  42   _heap(ShenandoahHeap::heap()),
  43   _timer(_heap->gc_timer()),
  44   _tracer(_heap->tracer()) {
  45   assert(!ShenandoahGCPhase::is_valid_phase(ShenandoahGCPhase::current_phase()),
  46     "No current GC phase");
  47 
  48   _heap->set_gc_cause(cause);
  49   _timer->register_gc_start();
  50   _tracer->report_gc_start(cause, _timer->gc_start());
  51   _heap->trace_heap(GCWhen::BeforeGC, _tracer);
  52 
  53   _heap->shenandoah_policy()->record_cycle_start();
  54   _heap->heuristics()->record_cycle_start();
  55   _trace_cycle.initialize(_heap->cycle_memory_manager(), cause,
  56           /* allMemoryPoolsAffected */    true,
  57           /* recordGCBeginTime = */       true,
  58           /* recordPreGCUsage = */        true,
  59           /* recordPeakUsage = */         true,
  60           /* recordPostGCUsage = */       true,
  61           /* recordAccumulatedGCTime = */ true,
  62           /* recordGCEndTime = */         true,
  63           /* countCollection = */         true
  64   );
  65 }
  66 
  67 ShenandoahGCSession::~ShenandoahGCSession() {
  68   _heap->heuristics()->record_cycle_end();
  69   _timer->register_gc_end();
  70   _heap->trace_heap(GCWhen::AfterGC, _tracer);
  71   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
  72   assert(!ShenandoahGCPhase::is_valid_phase(ShenandoahGCPhase::current_phase()),
  73     "No current GC phase");
  74   _heap->set_gc_cause(GCCause::_no_gc);
  75 }
  76 
  77 ShenandoahGCPauseMark::ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type) :
  78   _heap(ShenandoahHeap::heap()), _gc_id_mark(gc_id), _svc_gc_mark(type), _is_gc_active_mark() {
  79 
  80   // FIXME: It seems that JMC throws away level 0 events, which are the Shenandoah
  81   // pause events. Create this pseudo level 0 event to push real events to level 1.
  82   _heap->gc_timer()->register_gc_phase_start("Shenandoah", Ticks::now());
  83   _trace_pause.initialize(_heap->stw_memory_manager(), _heap->gc_cause(),
  84           /* allMemoryPoolsAffected */    true,
  85           /* recordGCBeginTime = */       true,
  86           /* recordPreGCUsage = */        false,
  87           /* recordPeakUsage = */         false,
  88           /* recordPostGCUsage = */       false,
  89           /* recordAccumulatedGCTime = */ true,
  90           /* recordGCEndTime = */         true,
  91           /* countCollection = */         true
  92   );
  93 
  94   _heap->heuristics()->record_gc_start();
  95 }
  96 
  97 ShenandoahGCPauseMark::~ShenandoahGCPauseMark() {
  98   _heap->gc_timer()->register_gc_phase_end(Ticks::now());
  99   _heap->heuristics()->record_gc_end();
 100 }
 101 
 102 ShenandoahGCPhase::ShenandoahGCPhase(const ShenandoahPhaseTimings::Phase phase) :
 103   _heap(ShenandoahHeap::heap()), _phase(phase) {
 104    assert(!Thread::current()->is_Worker_thread() &&
 105               (Thread::current()->is_VM_thread() ||
 106                Thread::current()->is_ConcurrentGC_thread()),
 107           "Must be set by these threads");
 108   _parent_phase = _current_phase;
 109   _current_phase = phase;
 110 
 111   _heap->phase_timings()->record_phase_start(_phase);
 112 }
 113 
 114 ShenandoahGCPhase::~ShenandoahGCPhase() {
 115   _heap->phase_timings()->record_phase_end(_phase);
 116   _current_phase = _parent_phase;
 117 }
 118 
 119 bool ShenandoahGCPhase::is_valid_phase(ShenandoahPhaseTimings::Phase phase) {
 120   return phase >= 0 && phase < ShenandoahPhaseTimings::_num_phases;
 121 }
 122 
 123 bool ShenandoahGCPhase::is_root_work_phase() {
 124   switch(current_phase()) {
 125     case ShenandoahPhaseTimings::scan_roots:
 126     case ShenandoahPhaseTimings::update_roots:
 127     case ShenandoahPhaseTimings::init_evac:
 128     case ShenandoahPhaseTimings::final_update_refs_roots:
 129     case ShenandoahPhaseTimings::degen_gc_update_roots:
 130     case ShenandoahPhaseTimings::init_traversal_gc_work:
 131     case ShenandoahPhaseTimings::final_traversal_gc_work:
 132     case ShenandoahPhaseTimings::final_traversal_update_roots:
 133     case ShenandoahPhaseTimings::full_gc_roots:
 134       return true;
 135     default:
 136       return false;
 137   }
 138 }
 139 
 140 ShenandoahAllocTrace::ShenandoahAllocTrace(size_t words_size, ShenandoahAllocRequest::Type alloc_type) {
 141   if (ShenandoahAllocationTrace) {
 142     _start = os::elapsedTime();
 143     _size = words_size;
 144     _alloc_type = alloc_type;
 145   } else {
 146     _start = 0;
 147     _size = 0;
 148     _alloc_type = ShenandoahAllocRequest::Type(0);
 149   }
 150 }
 151 
 152 ShenandoahAllocTrace::~ShenandoahAllocTrace() {
 153   if (ShenandoahAllocationTrace) {
 154     double stop = os::elapsedTime();
 155     double duration_sec = stop - _start;
 156     double duration_us = duration_sec * 1000000;
 157     ShenandoahAllocTracker* tracker = ShenandoahHeap::heap()->alloc_tracker();
 158     assert(tracker != NULL, "Must be");
 159     tracker->record_alloc_latency(_size, _alloc_type, duration_us);
 160     if (duration_us > ShenandoahAllocationStallThreshold) {
 161       log_warning(gc)("Allocation stall: %.0f us (threshold: " INTX_FORMAT " us)",
 162                       duration_us, ShenandoahAllocationStallThreshold);
 163     }
 164   }
 165 }
 166 
 167 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
 168   Thread* thr = Thread::current();
 169   assert(ShenandoahThreadLocalData::worker_id(thr) == ShenandoahThreadLocalData::INVALID_WORKER_ID, "Already set");
 170   ShenandoahThreadLocalData::set_worker_id(thr, worker_id);
 171 }
 172 
 173 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
 174   _event.commit(GCId::current(), ShenandoahPhaseTimings::phase_name(ShenandoahGCPhase::current_phase()));
 175 }
 176 
 177 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
 178   _event.commit(GCId::current(), _worker_id, ShenandoahPhaseTimings::phase_name(ShenandoahGCPhase::current_phase()));
 179 }
 180 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 181 #ifdef ASSERT
 182   Thread* thr = Thread::current();
 183   assert(ShenandoahThreadLocalData::worker_id(thr) != ShenandoahThreadLocalData::INVALID_WORKER_ID, "Must be set");
 184   ShenandoahThreadLocalData::set_worker_id(thr, ShenandoahThreadLocalData::INVALID_WORKER_ID);
 185 #endif
 186 }
 187 
 188 struct PhaseMap {
 189    WeakProcessorPhases::Phase            _weak_processor_phase;
 190    ShenandoahPhaseTimings::GCParPhases   _shenandoah_phase;
 191 };
 192 
 193 static const struct PhaseMap phase_mapping[] = {
 194 #if INCLUDE_JVMTI
 195   {WeakProcessorPhases::jvmti,                 ShenandoahPhaseTimings::JVMTIWeakRoots},
 196 #endif
 197 #if INCLUDE_JFR
 198   {WeakProcessorPhases::jfr,                   ShenandoahPhaseTimings::JFRWeakRoots},
 199 #endif
 200   {WeakProcessorPhases::jni,                   ShenandoahPhaseTimings::JNIWeakRoots},
 201   {WeakProcessorPhases::stringtable,           ShenandoahPhaseTimings::StringTableRoots},
 202   {WeakProcessorPhases::resolved_method_table, ShenandoahPhaseTimings::ResolvedMethodTableRoots},
 203   {WeakProcessorPhases::vm,                    ShenandoahPhaseTimings::VMWeakRoots},
 204   {WeakProcessorPhases::nmethod_keepalive,     ShenandoahPhaseTimings::NMethodKeepAliveRoots}
 205 };
 206 
 207 STATIC_ASSERT(sizeof(phase_mapping) / sizeof(PhaseMap) == WeakProcessorPhases::phase_count);
 208 
 209 void ShenandoahTimingConverter::weak_processing_timing_to_shenandoah_timing(WeakProcessorPhaseTimes* weak_processing_timings,
 210                                                                             ShenandoahWorkerTimings* sh_worker_times) {
 211   assert(weak_processing_timings->max_threads() == weak_processing_timings->max_threads(), "Must match");
 212   for (uint index = 0; index < WeakProcessorPhases::phase_count; index ++) {
 213     weak_processing_phase_to_shenandoah_phase(phase_mapping[index]._weak_processor_phase,
 214                                               weak_processing_timings,
 215                                               phase_mapping[index]._shenandoah_phase,
 216                                               sh_worker_times);
 217   }
 218 }
 219 
 220 void ShenandoahTimingConverter::weak_processing_phase_to_shenandoah_phase(WeakProcessorPhases::Phase wpp,
 221                                                                           WeakProcessorPhaseTimes* weak_processing_timings,
 222                                                                           ShenandoahPhaseTimings::GCParPhases spp,
 223                                                                           ShenandoahWorkerTimings* sh_worker_times) {
 224   if (WeakProcessorPhases::is_serial(wpp)) {
 225     sh_worker_times->record_time_secs(spp, 0, weak_processing_timings->phase_time_sec(wpp));
 226   } else {
 227     for (uint index = 0; index < weak_processing_timings->max_threads(); index ++) {
 228       sh_worker_times->record_time_secs(spp, index, weak_processing_timings->worker_time_sec(index, wpp));
 229     }
 230   }
 231 }