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 #ifndef SHARE_VM_GC_SHENANDOAHUTILS_HPP
  25 #define SHARE_VM_GC_SHENANDOAHUTILS_HPP
  26 
  27 #include "runtime/vmThread.hpp"
  28 #include "gc_interface/gcCause.hpp"
  29 #include "gc_implementation/shared/isGCActiveMark.hpp"
  30 #include "gc_implementation/shared/vmGCOperations.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "gc_implementation/shenandoah/shenandoahPhaseTimings.hpp"
  33 
  34 class GCTimer;
  35 class GCTracer;
  36 
  37 class ShenandoahGCSession : public StackObj {
  38 private:
  39   ShenandoahHeap* const _heap;
  40   GCTimer*  const _timer;
  41   GCTracer* const _tracer;
  42 
  43   TraceMemoryManagerStats _trace_cycle;
  44 public:
  45   ShenandoahGCSession(GCCause::Cause cause);
  46   ~ShenandoahGCSession();
  47 };
  48 
  49 class ShenandoahGCPhase : public StackObj {
  50 private:
  51   static ShenandoahPhaseTimings::Phase  _current_phase;
  52 
  53   ShenandoahPhaseTimings* const         _timings;
  54   const ShenandoahPhaseTimings::Phase   _phase;
  55   ShenandoahPhaseTimings::Phase         _parent_phase;
  56   double _start;
  57 
  58 public:
  59   ShenandoahGCPhase(ShenandoahPhaseTimings::Phase phase);
  60   ~ShenandoahGCPhase();
  61 
  62   static ShenandoahPhaseTimings::Phase current_phase() { return _current_phase; }
  63 
  64   static bool is_current_phase_valid();
  65 };
  66 
  67 class ShenandoahGCWorkerPhase : public StackObj {
  68 private:
  69   ShenandoahPhaseTimings* const       _timings;
  70   const ShenandoahPhaseTimings::Phase _phase;
  71 public:
  72   ShenandoahGCWorkerPhase(ShenandoahPhaseTimings::Phase phase);
  73   ~ShenandoahGCWorkerPhase();
  74 };
  75 
  76 // Aggregates all the things that should happen before/after the pause.
  77 class ShenandoahGCPauseMark : public StackObj {
  78 private:
  79   ShenandoahHeap* const _heap;
  80   const SvcGCMarker       _svc_gc_mark;
  81   const IsGCActiveMark    _is_gc_active_mark;
  82   TraceMemoryManagerStats _trace_pause;
  83 public:
  84   ShenandoahGCPauseMark(SvcGCMarker::reason_type type);
  85   ~ShenandoahGCPauseMark();
  86 };
  87 
  88 class ShenandoahSafepoint : public AllStatic {
  89 public:
  90   // Check if Shenandoah GC safepoint is in progress. This is nominally
  91   // equivalent to calling SafepointSynchronize::is_at_safepoint(), but
  92   // it also checks the Shenandoah specifics, when it can.
  93   static inline bool is_at_shenandoah_safepoint() {
  94     if (!SafepointSynchronize::is_at_safepoint()) return false;
  95 
  96     Thread* const thr = Thread::current();
  97     // Shenandoah GC specific safepoints are scheduled by control thread.
  98     // So if we are enter here from control thread, then we are definitely not
  99     // at Shenandoah safepoint, but at something else.
 100     if (thr == ShenandoahHeap::heap()->control_thread()) return false;
 101 
 102     // This is not VM thread, cannot see what VM thread is doing,
 103     // so pretend this is a proper Shenandoah safepoint
 104     if (!thr->is_VM_thread()) return true;
 105 
 106     // Otherwise check we are at proper operation type
 107     VM_Operation* vm_op = VMThread::vm_operation();
 108     if (vm_op == NULL) return false;
 109 
 110     VM_Operation::VMOp_Type type = vm_op->type();
 111     return type == VM_Operation::VMOp_ShenandoahInitMark ||
 112            type == VM_Operation::VMOp_ShenandoahFinalMarkStartEvac ||
 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   static const uint INVALID_WORKER_ID = uint(-1);
 122 protected:
 123   uint _worker_id;
 124 
 125   ShenandoahWorkerSession(uint worker_id);
 126   ~ShenandoahWorkerSession();
 127 
 128 public:
 129   static inline uint worker_id() {
 130     Thread* thr = Thread::current();
 131     uint id = thr->worker_id();
 132     assert(id != INVALID_WORKER_ID, "Worker session has not been created");
 133     return id;
 134   }
 135 };
 136 
 137 class ShenandoahConcurrentWorkerSession : public ShenandoahWorkerSession {
 138 public:
 139   ShenandoahConcurrentWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
 140   ~ShenandoahConcurrentWorkerSession();
 141 };
 142 
 143 class ShenandoahParallelWorkerSession : public ShenandoahWorkerSession {
 144 public:
 145   ShenandoahParallelWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
 146   ~ShenandoahParallelWorkerSession();
 147 };
 148 
 149 class ShenandoahUtils {
 150 public:
 151   static size_t round_up_power_of_2(size_t value);
 152 };
 153 
 154 #endif // SHARE_VM_GC_SHENANDOAHUTILS_HPP