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