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