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/shenandoahCollectorPolicy.hpp"
  27 #include "gc_implementation/shenandoah/shenandoahMarkCompact.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahUtils.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahLogging.hpp"
  31 #include "gc_implementation/shenandoah/heuristics/shenandoahHeuristics.hpp"
  32 #include "gc_interface/gcCause.hpp"
  33 #include "gc_implementation/shared/gcTimer.hpp"
  34 #include "gc_implementation/shared/gcWhen.hpp"
  35 #include "gc_implementation/shared/gcTrace.hpp"
  36 #include "utilities/debug.hpp"
  37 
  38 ShenandoahPhaseTimings::Phase ShenandoahGCPhase::_current_phase = ShenandoahPhaseTimings::_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_current_phase_valid(), "No current GC phase");
  46 
  47   _heap->set_gc_cause(cause);
  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, cause,
  55           /* allMemoryPoolsAffected */    true,
  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_current_phase_valid(), "No current GC phase");
  72   _heap->set_gc_cause(GCCause::_no_gc);
  73 }
  74 
  75 ShenandoahGCPauseMark::ShenandoahGCPauseMark(SvcGCMarker::reason_type type) :
  76         _heap(ShenandoahHeap::heap()), _svc_gc_mark(type), _is_gc_active_mark() {
  77   // FIXME: It seems that JMC throws away level 0 events, which are the Shenandoah
  78   // pause events. Create this pseudo level 0 event to push real events to level 1.
  79   _heap->gc_timer()->register_gc_phase_start("Shenandoah", Ticks::now());
  80   _trace_pause.initialize(true, _heap->gc_cause(),
  81           /* allMemoryPoolsAffected */    true,
  82           /* recordGCBeginTime = */       true,
  83           /* recordPreGCUsage = */        false,
  84           /* recordPeakUsage = */         false,
  85           /* recordPostGCUsage = */       false,
  86           /* recordAccumulatedGCTime = */ true,
  87           /* recordGCEndTime = */         true,
  88           /* countCollection = */         true
  89   );
  90 }
  91 
  92 ShenandoahGCPauseMark::~ShenandoahGCPauseMark() {
  93   _heap->gc_timer()->register_gc_phase_end(Ticks::now());
  94 }
  95 
  96 ShenandoahGCPhase::ShenandoahGCPhase(const ShenandoahPhaseTimings::Phase phase) :
  97   _timings(ShenandoahHeap::heap()->phase_timings()), _phase(phase) {
  98   assert(Thread::current()->is_VM_thread() ||
  99          Thread::current()->is_ConcurrentGC_thread(),
 100         "Must be set by these threads");
 101   _parent_phase = _current_phase;
 102   _current_phase = phase;
 103   _start = os::elapsedTime();
 104 }
 105 
 106 ShenandoahGCPhase::~ShenandoahGCPhase() {
 107   _timings->record_phase_time(_phase, os::elapsedTime() - _start);
 108   _current_phase = _parent_phase;
 109 }
 110 
 111 bool ShenandoahGCPhase::is_current_phase_valid() {
 112   return _current_phase < ShenandoahPhaseTimings::_num_phases;
 113 }
 114 
 115 ShenandoahGCWorkerPhase::ShenandoahGCWorkerPhase(const ShenandoahPhaseTimings::Phase phase) :
 116     _timings(ShenandoahHeap::heap()->phase_timings()), _phase(phase) {
 117   _timings->record_workers_start(_phase);
 118 }
 119 
 120 ShenandoahGCWorkerPhase::~ShenandoahGCWorkerPhase() {
 121   _timings->record_workers_end(_phase);
 122 }
 123 
 124 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
 125   Thread* thr = Thread::current();
 126   assert(thr->worker_id() == INVALID_WORKER_ID, "Already set");
 127   thr->set_worker_id(worker_id);
 128 }
 129 
 130 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
 131   // Do nothing. Per-worker events are not supported in this JDK.
 132 }
 133 
 134 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
 135   // Do nothing. Per-worker events are not supported in this JDK.
 136 }
 137 
 138 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 139 #ifdef ASSERT
 140   Thread* thr = Thread::current();
 141   assert(thr->worker_id() != INVALID_WORKER_ID, "Must be set");
 142   thr->set_worker_id(INVALID_WORKER_ID);
 143 #endif
 144 }
 145 
 146 size_t ShenandoahUtils::round_up_power_of_2(size_t value) {
 147   assert(value != 0, "Invalid value");
 148 
 149   if (is_power_of_2(value)) {
 150     return value;
 151   }
 152 
 153   return (size_t)1 << (log2_intptr(value) + 1);
 154 }