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 "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   _timer->register_gc_start();
  49   _tracer->report_gc_start(cause, _timer->gc_start());
  50   _heap->trace_heap(GCWhen::BeforeGC, _tracer);
  51 
  52   _heap->shenandoah_policy()->record_cycle_start();
  53   _heap->heuristics()->record_cycle_start();
  54   _trace_cycle.initialize(false, _heap->gc_cause(),
  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   ShenandoahHeap::heap()->heuristics()->record_cycle_end();
  67   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
  68   _timer->register_gc_end();
  69 
  70   assert(!ShenandoahGCPhase::is_valid_phase(ShenandoahGCPhase::current_phase()),
  71          "No current GC phase");
  72 }
  73 
  74 ShenandoahGCPauseMark::ShenandoahGCPauseMark(SvcGCMarker::reason_type type) :
  75         _heap(ShenandoahHeap::heap()), _svc_gc_mark(type), _is_gc_active_mark() {
  76   // FIXME: It seems that JMC throws away level 0 events, which are the Shenandoah
  77   // pause events. Create this pseudo level 0 event to push real events to level 1.
  78   _heap->gc_timer()->register_gc_phase_start("Shenandoah", Ticks::now());
  79   _trace_pause.initialize(true, _heap->gc_cause(),
  80           /* recordGCBeginTime = */       true,
  81           /* recordPreGCUsage = */        false,
  82           /* recordPeakUsage = */         false,
  83           /* recordPostGCUsage = */       false,
  84           /* recordAccumulatedGCTime = */ true,
  85           /* recordGCEndTime = */         true,
  86           /* countCollection = */         true
  87   );
  88 
  89   _heap->heuristics()->record_gc_start();
  90 }
  91 
  92 ShenandoahGCPauseMark::~ShenandoahGCPauseMark() {
  93   _heap->gc_timer()->register_gc_phase_end(Ticks::now());
  94   _heap->heuristics()->record_gc_end();
  95 }
  96 
  97 ShenandoahGCPhase::ShenandoahGCPhase(const ShenandoahPhaseTimings::Phase phase) :
  98   _heap(ShenandoahHeap::heap()), _phase(phase) {
  99   assert(Thread::current()->is_VM_thread() ||
 100          Thread::current()->is_ConcurrentGC_thread(),
 101         "Must be set by these threads");
 102   _parent_phase = _current_phase;
 103   _current_phase = phase;
 104 
 105   _heap->phase_timings()->record_phase_start(_phase);
 106 }
 107 
 108 ShenandoahGCPhase::~ShenandoahGCPhase() {
 109   _heap->phase_timings()->record_phase_end(_phase);
 110   _current_phase = _parent_phase;
 111 }
 112 
 113 bool ShenandoahGCPhase::is_valid_phase(ShenandoahPhaseTimings::Phase phase) {
 114   return phase >= 0 && phase < ShenandoahPhaseTimings::_num_phases;
 115 }
 116 
 117 bool ShenandoahGCPhase::is_root_work_phase() {
 118   switch(current_phase()) {
 119     case ShenandoahPhaseTimings::scan_roots:
 120     case ShenandoahPhaseTimings::update_roots:
 121     case ShenandoahPhaseTimings::init_evac:
 122     case ShenandoahPhaseTimings::final_update_refs_roots:
 123     case ShenandoahPhaseTimings::degen_gc_update_roots:
 124     case ShenandoahPhaseTimings::full_gc_roots:
 125       return true;
 126     default:
 127       return false;
 128   }
 129 }
 130 
 131 ShenandoahAllocTrace::ShenandoahAllocTrace(size_t words_size, ShenandoahAllocRequest::Type alloc_type) {
 132   if (ShenandoahAllocationTrace) {
 133     _start = os::elapsedTime();
 134     _size = words_size;
 135     _alloc_type = alloc_type;
 136   } else {
 137     _start = 0;
 138     _size = 0;
 139     _alloc_type = ShenandoahAllocRequest::Type(0);
 140   }
 141 }
 142 
 143 ShenandoahAllocTrace::~ShenandoahAllocTrace() {
 144   if (ShenandoahAllocationTrace) {
 145     double stop = os::elapsedTime();
 146     double duration_sec = stop - _start;
 147     double duration_us = duration_sec * 1000000;
 148     ShenandoahAllocTracker* tracker = ShenandoahHeap::heap()->alloc_tracker();
 149     assert(tracker != NULL, "Must be");
 150     tracker->record_alloc_latency(_size, _alloc_type, duration_us);
 151     if (duration_us > ShenandoahAllocationStallThreshold) {
 152       log_warning(gc)("Allocation stall: %.0f us (threshold: " INTX_FORMAT " us)",
 153                       duration_us, ShenandoahAllocationStallThreshold);
 154     }
 155   }
 156 }
 157 
 158 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
 159   Thread* thr = Thread::current();
 160   assert(thr->worker_id() == INVALID_WORKER_ID, "Already set");
 161   thr->set_worker_id(worker_id);
 162 }
 163 
 164 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
 165   // Do nothing. Per-worker events are not supported in this JDK.
 166 }
 167 
 168 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
 169   // Do nothing. Per-worker events are not supported in this JDK.
 170 }
 171 
 172 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 173 #ifdef ASSERT
 174   Thread* thr = Thread::current();
 175   assert(thr->worker_id() != INVALID_WORKER_ID, "Must be set");
 176   thr->set_worker_id(INVALID_WORKER_ID);
 177 #endif
 178 }