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 #ifndef SHARE_VM_GC_SHENANDOAHUTILS_HPP
  25 #define SHARE_VM_GC_SHENANDOAHUTILS_HPP
  26 
  27 #include "gc/shared/gcCause.hpp"
  28 #include "gc/shared/vmGCOperations.hpp"
  29 #include "gc/shared/isGCActiveMark.hpp"
  30 #include "gc/shared/suspendibleThreadSet.hpp"
  31 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  33 #include "jfr/jfrEvents.hpp"
  34 #include "memory/allocation.hpp"
  35 #include "runtime/safepoint.hpp"
  36 #include "runtime/vmThread.hpp"
  37 #include "runtime/vm_operations.hpp"
  38 #include "services/memoryService.hpp"
  39 
  40 class GCTimer;
  41 class GCTracer;
  42 
  43 class ShenandoahGCSession : public StackObj {
  44 private:
  45   ShenandoahHeap* const _heap;
  46   GCTimer*  const _timer;
  47   GCTracer* const _tracer;
  48 
  49   TraceMemoryManagerStats _trace_cycle;
  50 public:
  51   ShenandoahGCSession(GCCause::Cause cause);
  52   ~ShenandoahGCSession();
  53 };
  54 
  55 class ShenandoahGCPhase : public StackObj {
  56 private:
  57   static const ShenandoahPhaseTimings::Phase _invalid_phase = ShenandoahPhaseTimings::_num_phases;
  58   static ShenandoahPhaseTimings::Phase       _current_phase;
  59 
  60   ShenandoahHeap* const _heap;
  61   const ShenandoahPhaseTimings::Phase   _phase;
  62   ShenandoahPhaseTimings::Phase         _parent_phase;
  63 public:
  64   ShenandoahGCPhase(ShenandoahPhaseTimings::Phase phase);
  65   ~ShenandoahGCPhase();
  66 
  67   static ShenandoahPhaseTimings::Phase current_phase() { return _current_phase; }
  68 
  69   static bool is_valid_phase(ShenandoahPhaseTimings::Phase phase);
  70   static bool is_current_phase_valid() { return is_valid_phase(current_phase()); }
  71   static bool is_root_work_phase();
  72 };
  73 
  74 // Aggregates all the things that should happen before/after the pause.
  75 class ShenandoahGCPauseMark : public StackObj {
  76 private:
  77   ShenandoahHeap* const _heap;
  78   const GCIdMark                _gc_id_mark;
  79   const SvcGCMarker             _svc_gc_mark;
  80   const IsGCActiveMark          _is_gc_active_mark;
  81   TraceMemoryManagerStats       _trace_pause;
  82 
  83 public:
  84   ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type);
  85   ~ShenandoahGCPauseMark();
  86 };
  87 
  88 class ShenandoahAllocTrace : public StackObj {
  89 private:
  90   double _start;
  91   size_t _size;
  92   ShenandoahAllocRequest::Type _alloc_type;
  93 public:
  94   ShenandoahAllocTrace(size_t words_size, ShenandoahAllocRequest::Type alloc_type);
  95   ~ShenandoahAllocTrace();
  96 };
  97 
  98 class ShenandoahSafepoint : public AllStatic {
  99 public:
 100   // check if Shenandoah GC safepoint is in progress
 101   static inline bool is_at_shenandoah_safepoint() {
 102     if (!SafepointSynchronize::is_at_safepoint()) return false;
 103 
 104     VM_Operation* vm_op = VMThread::vm_operation();
 105     if (vm_op == NULL) return false;
 106 
 107     VM_Operation::VMOp_Type type = vm_op->type();
 108     return type == VM_Operation::VMOp_ShenandoahInitMark ||
 109            type == VM_Operation::VMOp_ShenandoahFinalMarkStartEvac ||
 110            type == VM_Operation::VMOp_ShenandoahFinalEvac ||
 111            type == VM_Operation::VMOp_ShenandoahInitTraversalGC ||
 112            type == VM_Operation::VMOp_ShenandoahFinalTraversalGC ||
 113            type == VM_Operation::VMOp_ShenandoahInitUpdateRefs ||
 114            type == VM_Operation::VMOp_ShenandoahFinalUpdateRefs ||
 115            type == VM_Operation::VMOp_ShenandoahFullGC ||
 116            type == VM_Operation::VMOp_ShenandoahDegeneratedGC;
 117   }
 118 };
 119 
 120 class ShenandoahWorkerSession : public StackObj {
 121 protected:
 122   uint _worker_id;
 123 
 124   ShenandoahWorkerSession(uint worker_id);
 125   ~ShenandoahWorkerSession();
 126 public:
 127   static inline uint worker_id() {
 128     Thread* thr = Thread::current();
 129     uint id = ShenandoahThreadLocalData::worker_id(thr);
 130     assert(id != ShenandoahThreadLocalData::INVALID_WORKER_ID, "Worker session has not been created");
 131     return id;
 132   }
 133 };
 134 
 135 class ShenandoahConcurrentWorkerSession : public ShenandoahWorkerSession {
 136 public:
 137   ShenandoahConcurrentWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
 138   ~ShenandoahConcurrentWorkerSession();
 139 };
 140 
 141 class ShenandoahParallelWorkerSession : public ShenandoahWorkerSession {
 142 public:
 143   ShenandoahParallelWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
 144   ~ShenandoahParallelWorkerSession();
 145 };
 146 
 147 class ShenandoahSuspendibleThreadSetJoiner {
 148 private:
 149   SuspendibleThreadSetJoiner _joiner;
 150 public:
 151   ShenandoahSuspendibleThreadSetJoiner(bool active = true) : _joiner(active) {
 152     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be joined before evac scope");
 153   }
 154   ~ShenandoahSuspendibleThreadSetJoiner() {
 155     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be left after evac scope");
 156   }
 157 };
 158 
 159 class ShenandoahSuspendibleThreadSetLeaver {
 160 private:
 161   SuspendibleThreadSetLeaver _leaver;
 162 public:
 163   ShenandoahSuspendibleThreadSetLeaver(bool active = true) : _leaver(active) {
 164     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be left after evac scope");
 165   }
 166   ~ShenandoahSuspendibleThreadSetLeaver() {
 167     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be joined before evac scope");
 168   }
 169 };
 170 
 171 #endif // SHARE_VM_GC_SHENANDOAHUTILS_HPP