1 /*
  2  * Copyright (c) 2018, 2022, 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_SHENANDOAHTHREADLOCALDATA_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP
 27 
 28 #include "gc/shared/plab.hpp"
 29 #include "gc/shared/gcThreadLocalData.hpp"
 30 #include "gc/shared/gc_globals.hpp"
 31 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 32 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
 33 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 34 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
 35 #include "runtime/javaThread.hpp"
 36 #include "utilities/debug.hpp"
 37 #include "utilities/sizes.hpp"
 38 
 39 class ShenandoahThreadLocalData {
 40 private:
 41   char _gc_state;
 42   // Evacuation OOM state
 43   uint8_t                 _oom_scope_nesting_level;
 44   bool                    _oom_during_evac;
 45 
 46   SATBMarkQueue           _satb_mark_queue;
 47 
 48   // Thread-local allocation buffer for object evacuations.
 49   // In generational mode, it is exclusive to the young generation.
 50   PLAB* _gclab;
 51   size_t _gclab_size;
 52 
 53   double _paced_time;
 54 
 55   // Thread-local allocation buffer only used in generational mode.
 56   // Used both by mutator threads and by GC worker threads
 57   // for evacuations within the old generation and
 58   // for promotions from the young generation into the old generation.
 59   PLAB* _plab;
 60   size_t _plab_size;
 61 
 62   size_t _plab_evacuated;
 63   size_t _plab_promoted;
 64   size_t _plab_preallocated_promoted;
 65   bool   _plab_allows_promotion; // If false, no more promotion by this thread during this evacuation phase.
 66   bool   _plab_retries_enabled;
 67 
 68   ShenandoahEvacuationStats* _evacuation_stats;
 69 
 70   ShenandoahThreadLocalData() :
 71     _gc_state(0),
 72     _oom_scope_nesting_level(0),
 73     _oom_during_evac(false),
 74     _satb_mark_queue(&ShenandoahBarrierSet::satb_mark_queue_set()),
 75     _gclab(NULL),
 76     _gclab_size(0),
 77     _paced_time(0),
 78     _plab(NULL),
 79     _plab_size(0),
 80     _plab_evacuated(0),
 81     _plab_promoted(0),
 82     _plab_preallocated_promoted(0),
 83     _plab_allows_promotion(true),
 84     _plab_retries_enabled(true),
 85     _evacuation_stats(new ShenandoahEvacuationStats()) {
 86   }
 87 
 88   ~ShenandoahThreadLocalData() {
 89     if (_gclab != NULL) {
 90       delete _gclab;
 91     }
 92     if (_plab != NULL) {
 93       ShenandoahHeap::heap()->retire_plab(_plab);
 94       delete _plab;
 95     }
 96 
 97     // TODO: Preserve these stats somewhere for mutator threads.
 98     delete _evacuation_stats;
 99     _evacuation_stats = nullptr;
100   }
101 
102   static ShenandoahThreadLocalData* data(Thread* thread) {
103     assert(UseShenandoahGC, "Sanity");
104     return thread->gc_data<ShenandoahThreadLocalData>();
105   }
106 
107   static ByteSize satb_mark_queue_offset() {
108     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _satb_mark_queue);
109   }
110 
111 public:
112   static void create(Thread* thread) {
113     new (data(thread)) ShenandoahThreadLocalData();
114   }
115 
116   static void destroy(Thread* thread) {
117     data(thread)->~ShenandoahThreadLocalData();
118   }
119 
120   static SATBMarkQueue& satb_mark_queue(Thread* thread) {
121     return data(thread)->_satb_mark_queue;
122   }
123 
124   static void set_gc_state(Thread* thread, char gc_state) {
125     data(thread)->_gc_state = gc_state;
126   }
127 
128   static char gc_state(Thread* thread) {
129     return data(thread)->_gc_state;
130   }
131 
132   static void initialize_gclab(Thread* thread) {
133     assert (thread->is_Java_thread() || thread->is_Worker_thread(), "Only Java and GC worker threads are allowed to get GCLABs");
134     assert(data(thread)->_gclab == NULL, "Only initialize once");
135     data(thread)->_gclab = new PLAB(PLAB::min_size());
136     data(thread)->_gclab_size = 0;
137     data(thread)->_plab = new PLAB(PLAB::min_size());
138     data(thread)->_plab_size = 0;
139   }
140 
141   static PLAB* gclab(Thread* thread) {
142     return data(thread)->_gclab;
143   }
144 
145   static size_t gclab_size(Thread* thread) {
146     return data(thread)->_gclab_size;
147   }
148 
149   static void set_gclab_size(Thread* thread, size_t v) {
150     data(thread)->_gclab_size = v;
151   }
152 
153   static void begin_evacuation(Thread* thread, size_t bytes) {
154     data(thread)->_evacuation_stats->begin_evacuation(bytes);
155   }
156 
157   static void end_evacuation(Thread* thread, size_t bytes, uint age) {
158     data(thread)->_evacuation_stats->end_evacuation(bytes, age);
159   }
160 
161   static ShenandoahEvacuationStats* evacuation_stats(Thread* thread) {
162     return data(thread)->_evacuation_stats;
163   }
164 
165   static PLAB* plab(Thread* thread) {
166     return data(thread)->_plab;
167   }
168 
169   static size_t plab_size(Thread* thread) {
170     return data(thread)->_plab_size;
171   }
172 
173   static void set_plab_size(Thread* thread, size_t v) {
174     data(thread)->_plab_size = v;
175   }
176 
177   static void enable_plab_retries(Thread* thread) {
178     data(thread)->_plab_retries_enabled = true;
179   }
180 
181   static void disable_plab_retries(Thread* thread) {
182     data(thread)->_plab_retries_enabled = false;
183   }
184 
185   static bool plab_retries_enabled(Thread* thread) {
186     return data(thread)->_plab_retries_enabled;
187   }
188 
189   static void enable_plab_promotions(Thread* thread) {
190     data(thread)->_plab_allows_promotion = true;
191   }
192 
193   static void disable_plab_promotions(Thread* thread) {
194     data(thread)->_plab_allows_promotion = false;
195   }
196 
197   static bool allow_plab_promotions(Thread* thread) {
198     return data(thread)->_plab_allows_promotion;
199   }
200 
201   static void reset_plab_evacuated(Thread* thread) {
202     data(thread)->_plab_evacuated = 0;
203   }
204 
205   static void add_to_plab_evacuated(Thread* thread, size_t increment) {
206     data(thread)->_plab_evacuated += increment;
207   }
208 
209   static void subtract_from_plab_evacuated(Thread* thread, size_t increment) {
210     data(thread)->_plab_evacuated -= increment;
211   }
212 
213   static size_t get_plab_evacuated(Thread* thread) {
214     return data(thread)->_plab_evacuated;
215   }
216 
217   static void reset_plab_promoted(Thread* thread) {
218     data(thread)->_plab_promoted = 0;
219   }
220 
221   static void add_to_plab_promoted(Thread* thread, size_t increment) {
222     data(thread)->_plab_promoted += increment;
223   }
224 
225   static void subtract_from_plab_promoted(Thread* thread, size_t increment) {
226     data(thread)->_plab_promoted -= increment;
227   }
228 
229   static size_t get_plab_promoted(Thread* thread) {
230     return data(thread)->_plab_promoted;
231   }
232 
233   static void set_plab_preallocated_promoted(Thread* thread, size_t value) {
234     data(thread)->_plab_preallocated_promoted = value;
235   }
236 
237   static size_t get_plab_preallocated_promoted(Thread* thread) {
238     return data(thread)->_plab_preallocated_promoted;
239   }
240 
241   static void add_paced_time(Thread* thread, double v) {
242     data(thread)->_paced_time += v;
243   }
244 
245   static double paced_time(Thread* thread) {
246     return data(thread)->_paced_time;
247   }
248 
249   static void reset_paced_time(Thread* thread) {
250     data(thread)->_paced_time = 0;
251   }
252 
253   // Evacuation OOM handling
254   static bool is_oom_during_evac(Thread* thread) {
255     return data(thread)->_oom_during_evac;
256   }
257 
258   static void set_oom_during_evac(Thread* thread, bool oom) {
259     data(thread)->_oom_during_evac = oom;
260   }
261 
262   static uint8_t evac_oom_scope_level(Thread* thread) {
263     return data(thread)->_oom_scope_nesting_level;
264   }
265 
266   // Push the scope one level deeper, return previous level
267   static uint8_t push_evac_oom_scope(Thread* thread) {
268     uint8_t level = evac_oom_scope_level(thread);
269     assert(level < 254, "Overflow nesting level"); // UINT8_MAX = 255
270     data(thread)->_oom_scope_nesting_level = level + 1;
271     return level;
272   }
273 
274   // Pop the scope by one level, return previous level
275   static uint8_t pop_evac_oom_scope(Thread* thread) {
276     uint8_t level = evac_oom_scope_level(thread);
277     assert(level > 0, "Underflow nesting level");
278     data(thread)->_oom_scope_nesting_level = level - 1;
279     return level;
280   }
281 
282   static bool is_evac_allowed(Thread* thread) {
283     return evac_oom_scope_level(thread) > 0;
284   }
285 
286   // Offsets
287   static ByteSize satb_mark_queue_active_offset() {
288     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active();
289   }
290 
291   static ByteSize satb_mark_queue_index_offset() {
292     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_index();
293   }
294 
295   static ByteSize satb_mark_queue_buffer_offset() {
296     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_buf();
297   }
298 
299   static ByteSize gc_state_offset() {
300     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state);
301   }
302 };
303 
304 STATIC_ASSERT(sizeof(ShenandoahThreadLocalData) <= sizeof(GCThreadLocalData));
305 
306 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP