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 "gc_implementation/shenandoah/shenandoahAllocTracker.hpp"
  27 #include "gc_implementation/shenandoah/shenandoahCollectorPolicy.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahMarkCompact.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahHeuristics.hpp"
  31 #include "gc_implementation/shenandoah/shenandoahUtils.hpp"
  32 #include "gc_implementation/shenandoah/shenandoahLogging.hpp"
  33 #include "gc_interface/gcCause.hpp"
  34 #include "gc_implementation/shared/gcTimer.hpp"
  35 #include "gc_implementation/shared/gcWhen.hpp"
  36 #include "gc_implementation/shared/gcTrace.hpp"
  37 
  38 ShenandoahPhaseTimings::Phase ShenandoahGCPhase::_current_phase = ShenandoahGCPhase::_invalid_phase;
  39 
  40 ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause) :
  41   _heap(ShenandoahHeap::heap()),
  42   _timer(ShenandoahHeap::heap()->gc_timer()),
  43   _tracer(ShenandoahHeap::heap()->tracer()) {
  44 
  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(false, cause,
  56           /* recordGCBeginTime = */       true,
  57           /* recordPreGCUsage = */        true,
  58           /* recordPeakUsage = */         true,
  59           /* recordPostGCUsage = */       true,
  60           /* recordAccumulatedGCTime = */ true,
  61           /* recordGCEndTime = */         true,
  62           /* countCollection = */         true
  63   );
  64 }
  65 
  66 ShenandoahGCSession::~ShenandoahGCSession() {
  67   ShenandoahHeap::heap()->heuristics()->record_cycle_end();
  68   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
  69   _timer->register_gc_end();
  70 
  71   assert(!ShenandoahGCPhase::is_valid_phase(ShenandoahGCPhase::current_phase()),
  72          "No current GC phase");
  73   _heap->set_gc_cause(GCCause::_no_gc);
  74 }
  75 
  76 ShenandoahGCPauseMark::ShenandoahGCPauseMark(SvcGCMarker::reason_type type) :
  77         _heap(ShenandoahHeap::heap()), _svc_gc_mark(type), _is_gc_active_mark() {
  78   // FIXME: It seems that JMC throws away level 0 events, which are the Shenandoah
  79   // pause events. Create this pseudo level 0 event to push real events to level 1.
  80   _heap->gc_timer()->register_gc_phase_start("Shenandoah", Ticks::now());
  81   _trace_pause.initialize(true, _heap->gc_cause(),
  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::full_gc_roots:
 127       return true;
 128     default:
 129       return false;
 130   }
 131 }
 132 
 133 ShenandoahAllocTrace::ShenandoahAllocTrace(size_t words_size, ShenandoahAllocRequest::Type alloc_type) {
 134   if (ShenandoahAllocationTrace) {
 135     _start = os::elapsedTime();
 136     _size = words_size;
 137     _alloc_type = alloc_type;
 138   } else {
 139     _start = 0;
 140     _size = 0;
 141     _alloc_type = ShenandoahAllocRequest::Type(0);
 142   }
 143 }
 144 
 145 ShenandoahAllocTrace::~ShenandoahAllocTrace() {
 146   if (ShenandoahAllocationTrace) {
 147     double stop = os::elapsedTime();
 148     double duration_sec = stop - _start;
 149     double duration_us = duration_sec * 1000000;
 150     ShenandoahAllocTracker* tracker = ShenandoahHeap::heap()->alloc_tracker();
 151     assert(tracker != NULL, "Must be");
 152     tracker->record_alloc_latency(_size, _alloc_type, duration_us);
 153     if (duration_us > ShenandoahAllocationStallThreshold) {
 154       log_warning(gc)("Allocation stall: %.0f us (threshold: " INTX_FORMAT " us)",
 155                       duration_us, ShenandoahAllocationStallThreshold);
 156     }
 157   }
 158 }
 159 
 160 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
 161   Thread* thr = Thread::current();
 162   assert(thr->worker_id() == INVALID_WORKER_ID, "Already set");
 163   thr->set_worker_id(worker_id);
 164 }
 165 
 166 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
 167   // Do nothing. Per-worker events are not supported in this JDK.
 168 }
 169 
 170 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
 171   // Do nothing. Per-worker events are not supported in this JDK.
 172 }
 173 
 174 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 175 #ifdef ASSERT
 176   Thread* thr = Thread::current();
 177   assert(thr->worker_id() != INVALID_WORKER_ID, "Must be set");
 178   thr->set_worker_id(INVALID_WORKER_ID);
 179 #endif
 180 }