1 /*
  2  * Copyright (c) 2017, 2019, 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/shenandoah/shenandoahPhaseTimings.hpp"
 34 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 35 #include "jfr/jfrEvents.hpp"
 36 #include "memory/allocation.hpp"
 37 #include "runtime/safepoint.hpp"
 38 #include "runtime/vmThread.hpp"
 39 #include "runtime/vmOperations.hpp"
 40 #include "services/memoryService.hpp"
 41 
 42 class GCTimer;
 43 class ShenandoahGeneration;
 44 
 45 class ShenandoahGCSession : public StackObj {
 46 private:
 47   ShenandoahHeap* const _heap;
 48   ShenandoahGeneration* const _generation;
 49   GCTimer*  const _timer;
 50   GCTracer* const _tracer;
 51 
 52   TraceMemoryManagerStats _trace_cycle;
 53 public:
 54   ShenandoahGCSession(GCCause::Cause cause, ShenandoahGeneration* generation);
 55   ~ShenandoahGCSession();
 56 };
 57 
 58 /*
 59  * ShenandoahGCPhaseTiming tracks Shenandoah specific timing information
 60  * of a GC phase
 61  */
 62 class ShenandoahTimingsTracker : public StackObj {
 63 private:
 64   static ShenandoahPhaseTimings::Phase  _current_phase;
 65 
 66   ShenandoahPhaseTimings* const         _timings;
 67   const ShenandoahPhaseTimings::Phase   _phase;
 68   ShenandoahPhaseTimings::Phase         _parent_phase;
 69   double _start;
 70 
 71 public:
 72   ShenandoahTimingsTracker(ShenandoahPhaseTimings::Phase phase);
 73   ~ShenandoahTimingsTracker();
 74 
 75   static ShenandoahPhaseTimings::Phase current_phase() { return _current_phase; }
 76 
 77   static bool is_current_phase_valid();
 78 };
 79 
 80 /*
 81  * ShenandoahPausePhase tracks a STW pause and emits Shenandoah timing and
 82  * a corresponding JFR event
 83  */
 84 class ShenandoahPausePhase : public ShenandoahTimingsTracker {
 85 private:
 86   GCTraceTimeWrapper<LogLevel::Info, LOG_TAGS(gc)> _tracer;
 87   ConcurrentGCTimer* const _timer;
 88 
 89 public:
 90   ShenandoahPausePhase(const char* title, ShenandoahPhaseTimings::Phase phase, bool log_heap_usage = false);
 91   ~ShenandoahPausePhase();
 92 };
 93 
 94 /*
 95  * ShenandoahConcurrentPhase tracks a concurrent GC phase and emits Shenandoah timing and
 96  * a corresponding JFR event
 97  */
 98 class ShenandoahConcurrentPhase : public ShenandoahTimingsTracker {
 99 private:
100   GCTraceTimeWrapper<LogLevel::Info, LOG_TAGS(gc)> _tracer;
101   ConcurrentGCTimer* const _timer;
102 
103 public:
104   ShenandoahConcurrentPhase(const char* title, ShenandoahPhaseTimings::Phase phase, bool log_heap_usage = false);
105   ~ShenandoahConcurrentPhase();
106 };
107 
108 /*
109  * ShenandoahGCPhase tracks Shenandoah specific timing information
110  * and emits a corresponding JFR event of a GC phase
111  */
112 class ShenandoahGCPhase : public ShenandoahTimingsTracker {
113 private:
114   ConcurrentGCTimer* const _timer;
115 
116 public:
117   ShenandoahGCPhase(ShenandoahPhaseTimings::Phase phase);
118   ~ShenandoahGCPhase();
119 };
120 
121 class ShenandoahGCWorkerPhase : public StackObj {
122 private:
123   ShenandoahPhaseTimings* const       _timings;
124   const ShenandoahPhaseTimings::Phase _phase;
125 public:
126   ShenandoahGCWorkerPhase(ShenandoahPhaseTimings::Phase phase);
127   ~ShenandoahGCWorkerPhase();
128 };
129 
130 // Aggregates all the things that should happen before/after the pause.
131 class ShenandoahGCPauseMark : public StackObj {
132 private:
133   ShenandoahHeap* const _heap;
134   const GCIdMark                _gc_id_mark;
135   const SvcGCMarker             _svc_gc_mark;
136   const IsGCActiveMark          _is_gc_active_mark;
137   TraceMemoryManagerStats       _trace_pause;
138 
139 public:
140   ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type);
141 };
142 
143 class ShenandoahSafepoint : public AllStatic {
144 public:
145   // Check if Shenandoah GC safepoint is in progress. This is nominally
146   // equivalent to calling SafepointSynchronize::is_at_safepoint(), but
147   // it also checks the Shenandoah specifics, when it can.
148   static inline bool is_at_shenandoah_safepoint() {
149     if (!SafepointSynchronize::is_at_safepoint()) return false;
150 
151     Thread* const thr = Thread::current();
152     // Shenandoah GC specific safepoints are scheduled by control thread.
153     // So if we are enter here from control thread, then we are definitely not
154     // at Shenandoah safepoint, but at something else.
155     if (thr == ShenandoahHeap::heap()->control_thread()) return false;
156 
157     // This is not VM thread, cannot see what VM thread is doing,
158     // so pretend this is a proper Shenandoah safepoint
159     if (!thr->is_VM_thread()) return true;
160 
161     // Otherwise check we are at proper operation type
162     VM_Operation* vm_op = VMThread::vm_operation();
163     if (vm_op == NULL) return false;
164 
165     VM_Operation::VMOp_Type type = vm_op->type();
166     return type == VM_Operation::VMOp_ShenandoahInitMark ||
167            type == VM_Operation::VMOp_ShenandoahFinalMarkStartEvac ||
168            type == VM_Operation::VMOp_ShenandoahInitUpdateRefs ||
169            type == VM_Operation::VMOp_ShenandoahFinalUpdateRefs ||
170            type == VM_Operation::VMOp_ShenandoahFinalRoots ||
171            type == VM_Operation::VMOp_ShenandoahFullGC ||
172            type == VM_Operation::VMOp_ShenandoahDegeneratedGC;
173   }
174 };
175 
176 class ShenandoahWorkerSession : public StackObj {
177 protected:
178   uint _worker_id;
179 
180   ShenandoahWorkerSession(uint worker_id);
181   ~ShenandoahWorkerSession();
182 public:
183   static inline uint worker_id() {
184     Thread* thr = Thread::current();
185     uint id = ShenandoahThreadLocalData::worker_id(thr);
186     assert(id != ShenandoahThreadLocalData::INVALID_WORKER_ID, "Worker session has not been created");
187     return id;
188   }
189 };
190 
191 class ShenandoahConcurrentWorkerSession : public ShenandoahWorkerSession {
192 private:
193   EventGCPhaseConcurrent _event;
194 
195 public:
196   ShenandoahConcurrentWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
197   ~ShenandoahConcurrentWorkerSession();
198 };
199 
200 class ShenandoahParallelWorkerSession : public ShenandoahWorkerSession {
201 private:
202   EventGCPhaseParallel _event;
203 
204 public:
205   ShenandoahParallelWorkerSession(uint worker_id) : ShenandoahWorkerSession(worker_id) { }
206   ~ShenandoahParallelWorkerSession();
207 };
208 
209 class ShenandoahSuspendibleThreadSetJoiner {
210 private:
211   SuspendibleThreadSetJoiner _joiner;
212 public:
213   ShenandoahSuspendibleThreadSetJoiner(bool active = true) : _joiner(active) {
214     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be joined before evac scope");
215   }
216   ~ShenandoahSuspendibleThreadSetJoiner() {
217     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be left after evac scope");
218   }
219 };
220 
221 class ShenandoahSuspendibleThreadSetLeaver {
222 private:
223   SuspendibleThreadSetLeaver _leaver;
224 public:
225   ShenandoahSuspendibleThreadSetLeaver(bool active = true) : _leaver(active) {
226     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be left after evac scope");
227   }
228   ~ShenandoahSuspendibleThreadSetLeaver() {
229     assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "STS should be joined before evac scope");
230   }
231 };
232 
233 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHUTILS_HPP