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