1 /*
  2  * Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHUTILS_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHUTILS_HPP
 27 
 28 #include "gc/shared/gcCause.hpp"
 29 #include "gc/shared/gcTraceTime.inline.hpp"
 30 #include "gc/shared/gcVMOperations.hpp"
 31 #include "gc/shared/isGCActiveMark.hpp"
 32 #include "gc/shared/suspendibleThreadSet.hpp"
 33 #include "gc/shared/workerThread.hpp"
 34 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 35 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 36 #include "jfr/jfrEvents.hpp"
 37 #include "memory/allocation.hpp"
 38 #include "runtime/safepoint.hpp"
 39 #include "runtime/vmThread.hpp"
 40 #include "runtime/vmOperations.hpp"
 41 #include "services/memoryService.hpp"
 42 
 43 class GCTimer;
 44 class ShenandoahGeneration;
 45 
 46 class ShenandoahGCSession : public StackObj {
 47 private:
 48   ShenandoahHeap* const _heap;
 49   ShenandoahGeneration* const _generation;
 50   GCTimer*  const _timer;
 51   GCTracer* const _tracer;
 52 
 53   TraceMemoryManagerStats _trace_cycle;
 54 public:
 55   ShenandoahGCSession(GCCause::Cause cause, ShenandoahGeneration* generation);
 56   ~ShenandoahGCSession();
 57 };
 58 
 59 /*
 60  * ShenandoahGCPhaseTiming tracks Shenandoah specific timing information
 61  * of a GC phase
 62  */
 63 class ShenandoahTimingsTracker : public StackObj {
 64 private:
 65   static ShenandoahPhaseTimings::Phase  _current_phase;
 66 
 67   ShenandoahPhaseTimings* const         _timings;
 68   const ShenandoahPhaseTimings::Phase   _phase;
 69   ShenandoahPhaseTimings::Phase         _parent_phase;
 70   double _start;
 71 
 72 public:
 73   ShenandoahTimingsTracker(ShenandoahPhaseTimings::Phase phase);
 74   ~ShenandoahTimingsTracker();
 75 
 76   static ShenandoahPhaseTimings::Phase current_phase() { return _current_phase; }
 77 
 78   static bool is_current_phase_valid();
 79 };
 80 
 81 /*
 82  * ShenandoahPausePhase tracks a STW pause and emits Shenandoah timing and
 83  * a corresponding JFR event
 84  */
 85 class ShenandoahPausePhase : public ShenandoahTimingsTracker {
 86 private:
 87   GCTraceTimeWrapper<LogLevel::Info, LOG_TAGS(gc)> _tracer;
 88   ConcurrentGCTimer* const _timer;
 89 
 90 public:
 91   ShenandoahPausePhase(const char* title, ShenandoahPhaseTimings::Phase phase, bool log_heap_usage = false);
 92   ~ShenandoahPausePhase();
 93 };
 94 
 95 /*
 96  * ShenandoahConcurrentPhase tracks a concurrent GC phase and emits Shenandoah timing and
 97  * a corresponding JFR event
 98  */
 99 class ShenandoahConcurrentPhase : public ShenandoahTimingsTracker {
100 private:
101   GCTraceTimeWrapper<LogLevel::Info, LOG_TAGS(gc)> _tracer;
102   ConcurrentGCTimer* const _timer;
103 
104 public:
105   ShenandoahConcurrentPhase(const char* title, ShenandoahPhaseTimings::Phase phase, bool log_heap_usage = false);
106   ~ShenandoahConcurrentPhase();
107 };
108 
109 /*
110  * ShenandoahGCPhase tracks Shenandoah specific timing information
111  * and emits a corresponding JFR event of a GC phase
112  */
113 class ShenandoahGCPhase : public ShenandoahTimingsTracker {
114 private:
115   ConcurrentGCTimer* const _timer;
116 
117 public:
118   ShenandoahGCPhase(ShenandoahPhaseTimings::Phase phase);
119   ~ShenandoahGCPhase();
120 };
121 
122 class ShenandoahGCWorkerPhase : public StackObj {
123 private:
124   ShenandoahPhaseTimings* const       _timings;
125   const ShenandoahPhaseTimings::Phase _phase;
126 public:
127   ShenandoahGCWorkerPhase(ShenandoahPhaseTimings::Phase phase);
128   ~ShenandoahGCWorkerPhase();
129 };
130 
131 // Aggregates all the things that should happen before/after the pause.
132 class ShenandoahGCPauseMark : public StackObj {
133 private:
134   ShenandoahHeap* const _heap;
135   const GCIdMark                _gc_id_mark;
136   const SvcGCMarker             _svc_gc_mark;
137   const IsGCActiveMark          _is_gc_active_mark;
138   TraceMemoryManagerStats       _trace_pause;
139 
140 public:
141   ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type);
142 };
143 
144 class ShenandoahSafepoint : public AllStatic {
145 public:
146   // Check if Shenandoah GC safepoint is in progress. This is nominally
147   // equivalent to calling SafepointSynchronize::is_at_safepoint(), but
148   // it also checks the Shenandoah specifics, when it can.
149   static inline bool is_at_shenandoah_safepoint() {
150     if (!SafepointSynchronize::is_at_safepoint()) return false;
151 
152     Thread* const thr = Thread::current();
153     // Shenandoah GC specific safepoints are scheduled by control thread.
154     // So if we are enter here from control thread, then we are definitely not
155     // at Shenandoah safepoint, but at something else.
156     if (thr == ShenandoahHeap::heap()->control_thread()) return false;
157 
158     // This is not VM thread, cannot see what VM thread is doing,
159     // so pretend this is a proper Shenandoah safepoint
160     if (!thr->is_VM_thread()) return true;
161 
162     // Otherwise check we are at proper operation type
163     VM_Operation* vm_op = VMThread::vm_operation();
164     if (vm_op == NULL) return false;
165 
166     VM_Operation::VMOp_Type type = vm_op->type();
167     return type == VM_Operation::VMOp_ShenandoahInitMark ||
168            type == VM_Operation::VMOp_ShenandoahFinalMarkStartEvac ||
169            type == VM_Operation::VMOp_ShenandoahInitUpdateRefs ||
170            type == VM_Operation::VMOp_ShenandoahFinalUpdateRefs ||
171            type == VM_Operation::VMOp_ShenandoahFinalRoots ||
172            type == VM_Operation::VMOp_ShenandoahFullGC ||
173            type == VM_Operation::VMOp_ShenandoahDegeneratedGC;
174   }
175 };
176 
177 class ShenandoahWorkerSession : public StackObj {
178 protected:
179   ShenandoahWorkerSession(uint worker_id);
180 public:
181   static inline uint worker_id() {
182     return WorkerThread::worker_id();
183   }
184 };
185 
186 class ShenandoahConcurrentWorkerSession : public ShenandoahWorkerSession {
187 private:
188   EventGCPhaseConcurrent _event;
189 
190 public:
191   ShenandoahConcurrentWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
192   ~ShenandoahConcurrentWorkerSession();
193 };
194 
195 class ShenandoahParallelWorkerSession : public ShenandoahWorkerSession {
196 private:
197   EventGCPhaseParallel _event;
198 
199 public:
200   ShenandoahParallelWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
201   ~ShenandoahParallelWorkerSession();
202 };
203 
204 class ShenandoahSuspendibleThreadSetJoiner {
205 private:
206   SuspendibleThreadSetJoiner _joiner;
207 public:
208   ShenandoahSuspendibleThreadSetJoiner(bool active = true) : _joiner(active) {
209     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be joined before evac scope");
210   }
211   ~ShenandoahSuspendibleThreadSetJoiner() {
212     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be left after evac scope");
213   }
214 };
215 
216 class ShenandoahSuspendibleThreadSetLeaver {
217 private:
218   SuspendibleThreadSetLeaver _leaver;
219 public:
220   ShenandoahSuspendibleThreadSetLeaver(bool active = true) : _leaver(active) {
221     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be left after evac scope");
222   }
223   ~ShenandoahSuspendibleThreadSetLeaver() {
224     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be joined before evac scope");
225   }
226 };
227 
228 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHUTILS_HPP