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