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/gcTrace.hpp"
  29 #include "gc/shared/gcWhen.hpp"
  30 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  31 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  33 #include "gc/shenandoah/shenandoahUtils.hpp"
  34 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  35 #include "utilities/debug.hpp"
  36 
  37 ShenandoahPhaseTimings::Phase ShenandoahGCPhase::_current_phase = ShenandoahPhaseTimings::_invalid_phase;
  38 
  39 ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause) :
  40   _heap(ShenandoahHeap::heap()),
  41   _timer(_heap->gc_timer()),
  42   _tracer(_heap->tracer()) {
  43   assert(!ShenandoahGCPhase::is_current_phase_valid(), "No current GC phase");
  44 
  45   _heap->set_gc_cause(cause);
  46   _timer->register_gc_start();
  47   _tracer->report_gc_start(cause, _timer->gc_start());
  48   _heap->trace_heap_before_gc(_tracer);
  49 
  50   _heap->shenandoah_policy()->record_cycle_start();
  51   _heap->heuristics()->record_cycle_start();
  52   _trace_cycle.initialize(_heap->cycle_memory_manager(), cause,
  53           /* allMemoryPoolsAffected */    true,
  54           /* recordGCBeginTime = */       true,
  55           /* recordPreGCUsage = */        true,
  56           /* recordPeakUsage = */         true,
  57           /* recordPostGCUsage = */       true,
  58           /* recordAccumulatedGCTime = */ true,
  59           /* recordGCEndTime = */         true,
  60           /* countCollection = */         true
  61   );
  62 }
  63 
  64 ShenandoahGCSession::~ShenandoahGCSession() {
  65   _heap->heuristics()->record_cycle_end();
  66   _timer->register_gc_end();
  67   _heap->trace_heap_after_gc(_tracer);
  68   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
  69   assert(!ShenandoahGCPhase::is_current_phase_valid(), "No current GC phase");
  70   _heap->set_gc_cause(GCCause::_no_gc);
  71 }
  72 
  73 ShenandoahGCPauseMark::ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type) :
  74   _heap(ShenandoahHeap::heap()), _gc_id_mark(gc_id), _svc_gc_mark(type), _is_gc_active_mark() {
  75 
  76   _trace_pause.initialize(_heap->stw_memory_manager(), _heap->gc_cause(),
  77           /* allMemoryPoolsAffected */    true,
  78           /* recordGCBeginTime = */       true,
  79           /* recordPreGCUsage = */        false,
  80           /* recordPeakUsage = */         false,
  81           /* recordPostGCUsage = */       false,
  82           /* recordAccumulatedGCTime = */ true,
  83           /* recordGCEndTime = */         true,
  84           /* countCollection = */         true
  85   );
  86 }
  87 
  88 ShenandoahPausePhase::ShenandoahPausePhase(const char* title, bool log_heap_usage) :
  89   _tracer(title, NULL, GCCause::_no_gc, log_heap_usage),
  90   _timer(ShenandoahHeap::heap()->gc_timer()) {
  91   _timer->register_gc_pause_start(title);
  92 }
  93 
  94 ShenandoahPausePhase::~ShenandoahPausePhase() {
  95   _timer->register_gc_pause_end();
  96 }
  97 
  98 ShenandoahConcurrentPhase::ShenandoahConcurrentPhase(const char* title, bool log_heap_usage) :
  99         _tracer(title, NULL, GCCause::_no_gc, log_heap_usage),
 100         _timer(ShenandoahHeap::heap()->gc_timer()) {
 101   _timer->register_gc_concurrent_start(title);
 102 }
 103 
 104 ShenandoahConcurrentPhase::~ShenandoahConcurrentPhase() {
 105   _timer->register_gc_concurrent_end();
 106 }
 107 
 108 ShenandoahGCPhase::ShenandoahGCPhase(ShenandoahPhaseTimings::Phase phase) :
 109   _timings(ShenandoahHeap::heap()->phase_timings()), _phase(phase) {
 110   assert(!Thread::current()->is_Worker_thread() &&
 111               (Thread::current()->is_VM_thread() ||
 112                Thread::current()->is_ConcurrentGC_thread()),
 113           "Must be set by these threads");
 114   _parent_phase = _current_phase;
 115   _current_phase = phase;
 116   _start = os::elapsedTime();
 117 }
 118 
 119 ShenandoahGCPhase::~ShenandoahGCPhase() {
 120   _timings->record_phase_time(_phase, os::elapsedTime() - _start);
 121   _current_phase = _parent_phase;
 122 }
 123 
 124 bool ShenandoahGCPhase::is_current_phase_valid() {
 125   return _current_phase < ShenandoahPhaseTimings::_num_phases;
 126 }
 127 
 128 ShenandoahGCSubPhase::ShenandoahGCSubPhase(ShenandoahPhaseTimings::Phase phase) :
 129   ShenandoahGCPhase(phase),
 130   _timer(ShenandoahHeap::heap()->gc_timer()) {
 131   _timer->register_gc_phase_start(ShenandoahPhaseTimings::phase_name(phase), Ticks::now());
 132 }
 133 
 134 ShenandoahGCSubPhase::~ShenandoahGCSubPhase() {
 135   _timer->register_gc_phase_end(Ticks::now());
 136 }
 137 
 138 ShenandoahGCWorkerPhase::ShenandoahGCWorkerPhase(const ShenandoahPhaseTimings::Phase phase) :
 139     _timings(ShenandoahHeap::heap()->phase_timings()), _phase(phase) {
 140   _timings->record_workers_start(_phase);
 141 }
 142 
 143 ShenandoahGCWorkerPhase::~ShenandoahGCWorkerPhase() {
 144   _timings->record_workers_end(_phase);
 145 }
 146 
 147 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
 148   Thread* thr = Thread::current();
 149   assert(ShenandoahThreadLocalData::worker_id(thr) == ShenandoahThreadLocalData::INVALID_WORKER_ID, "Already set");
 150   ShenandoahThreadLocalData::set_worker_id(thr, worker_id);
 151 }
 152 
 153 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
 154   // Do nothing. Per-worker events are not supported in this JDK.
 155 }
 156 
 157 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
 158   // Do nothing. Per-worker events are not supported in this JDK.
 159 }
 160 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 161 #ifdef ASSERT
 162   Thread* thr = Thread::current();
 163   assert(ShenandoahThreadLocalData::worker_id(thr) != ShenandoahThreadLocalData::INVALID_WORKER_ID, "Must be set");
 164   ShenandoahThreadLocalData::set_worker_id(thr, ShenandoahThreadLocalData::INVALID_WORKER_ID);
 165 #endif
 166 }
 167 
 168 size_t ShenandoahUtils::round_up_power_of_2(size_t value) {
 169   assert(value != 0, "Invalid value");
 170 
 171   if (is_power_of_2(value)) {
 172     return value;
 173   }
 174 
 175   return (size_t)1 << (log2_intptr(value) + 1);
 176 }
 177