1 /*
  2  * Copyright (c) 2018, 2022, 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_SHENANDOAHTHREADLOCALDATA_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP
 28 
 29 #include "gc/shared/plab.hpp"
 30 #include "gc/shared/gcThreadLocalData.hpp"
 31 #include "gc/shared/gc_globals.hpp"
 32 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 33 #include "gc/shenandoah/shenandoahCardTable.hpp"
 34 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
 35 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
 36 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 37 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
 38 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 39 #include "runtime/javaThread.hpp"
 40 #include "utilities/debug.hpp"
 41 #include "utilities/sizes.hpp"
 42 
 43 class ShenandoahThreadLocalData {
 44 private:
 45   char _gc_state;
 46   // Evacuation OOM state
 47   uint8_t                 _oom_scope_nesting_level;
 48   bool                    _oom_during_evac;
 49 
 50   SATBMarkQueue           _satb_mark_queue;
 51 
 52   // Thread-local allocation buffer for object evacuations.
 53   // In generational mode, it is exclusive to the young generation.
 54   PLAB* _gclab;
 55   size_t _gclab_size;
 56 
 57   double _paced_time;
 58 
 59   // Thread-local allocation buffer only used in generational mode.
 60   // Used both by mutator threads and by GC worker threads
 61   // for evacuations within the old generation and
 62   // for promotions from the young generation into the old generation.
 63   PLAB* _plab;
 64 
 65   // Heuristics will grow the desired size of plabs.
 66   size_t _plab_desired_size;
 67 
 68   // Once the plab has been allocated, and we know the actual size, we record it here.
 69   size_t _plab_actual_size;
 70 
 71   // As the plab is used for promotions, this value is incremented. When the plab is
 72   // retired, the difference between 'actual_size' and 'promoted' will be returned to
 73   // the old generation's promotion reserve (i.e., it will be 'unexpended').
 74   size_t _plab_promoted;
 75 
 76   // If false, no more promotion by this thread during this evacuation phase.
 77   bool   _plab_allows_promotion;
 78 
 79   // If true, evacuations may attempt to allocate a smaller plab if the original size fails.
 80   bool   _plab_retries_enabled;
 81 
 82   ShenandoahEvacuationStats* _evacuation_stats;
 83 
 84   ShenandoahThreadLocalData();
 85   ~ShenandoahThreadLocalData();
 86 
 87   static ShenandoahThreadLocalData* data(Thread* thread) {
 88     assert(UseShenandoahGC, "Sanity");
 89     return thread->gc_data<ShenandoahThreadLocalData>();
 90   }
 91 
 92   static ByteSize satb_mark_queue_offset() {
 93     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _satb_mark_queue);
 94   }
 95 
 96 public:
 97   static void create(Thread* thread) {
 98     new (data(thread)) ShenandoahThreadLocalData();
 99   }
100 
101   static void destroy(Thread* thread) {
102     data(thread)->~ShenandoahThreadLocalData();
103   }
104 
105   static SATBMarkQueue& satb_mark_queue(Thread* thread) {
106     return data(thread)->_satb_mark_queue;
107   }
108 
109   static void set_gc_state(Thread* thread, char gc_state) {
110     data(thread)->_gc_state = gc_state;
111   }
112 
113   static char gc_state(Thread* thread) {
114     return data(thread)->_gc_state;
115   }
116 
117   static bool is_gc_state(Thread* thread, ShenandoahHeap::GCState state) {
118     return (gc_state(thread) & state) != 0;
119   }
120 
121   static bool is_gc_state(ShenandoahHeap::GCState state) {
122     return is_gc_state(Thread::current(), state);
123   }
124 
125   static void initialize_gclab(Thread* thread) {
126     assert(data(thread)->_gclab == nullptr, "Only initialize once");
127     data(thread)->_gclab = new PLAB(PLAB::min_size());
128     data(thread)->_gclab_size = 0;
129 
130     if (ShenandoahHeap::heap()->mode()->is_generational()) {
131       data(thread)->_plab = new PLAB(align_up(PLAB::min_size(), CardTable::card_size_in_words()));
132       data(thread)->_plab_desired_size = 0;
133     }
134   }
135 
136   static PLAB* gclab(Thread* thread) {
137     return data(thread)->_gclab;
138   }
139 
140   static size_t gclab_size(Thread* thread) {
141     return data(thread)->_gclab_size;
142   }
143 
144   static void set_gclab_size(Thread* thread, size_t v) {
145     data(thread)->_gclab_size = v;
146   }
147 
148   static void begin_evacuation(Thread* thread, size_t bytes) {
149     data(thread)->_evacuation_stats->begin_evacuation(bytes);
150   }
151 
152   static void end_evacuation(Thread* thread, size_t bytes) {
153     data(thread)->_evacuation_stats->end_evacuation(bytes);
154   }
155 
156   static void record_age(Thread* thread, size_t bytes, uint age) {
157     data(thread)->_evacuation_stats->record_age(bytes, age);
158   }
159 
160   static ShenandoahEvacuationStats* evacuation_stats(Thread* thread) {
161     shenandoah_assert_generational();
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_desired_size;
171   }
172 
173   static void set_plab_size(Thread* thread, size_t v) {
174     data(thread)->_plab_desired_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_promoted(Thread* thread) {
202     data(thread)->_plab_promoted = 0;
203   }
204 
205   static void add_to_plab_promoted(Thread* thread, size_t increment) {
206     data(thread)->_plab_promoted += increment;
207   }
208 
209   static void subtract_from_plab_promoted(Thread* thread, size_t increment) {
210     assert(data(thread)->_plab_promoted >= increment, "Cannot subtract more than remaining promoted");
211     data(thread)->_plab_promoted -= increment;
212   }
213 
214   static size_t get_plab_promoted(Thread* thread) {
215     return data(thread)->_plab_promoted;
216   }
217 
218   static void set_plab_actual_size(Thread* thread, size_t value) {
219     data(thread)->_plab_actual_size = value;
220   }
221 
222   static size_t get_plab_actual_size(Thread* thread) {
223     return data(thread)->_plab_actual_size;
224   }
225 
226   static void add_paced_time(Thread* thread, double v) {
227     data(thread)->_paced_time += v;
228   }
229 
230   static double paced_time(Thread* thread) {
231     return data(thread)->_paced_time;
232   }
233 
234   static void reset_paced_time(Thread* thread) {
235     data(thread)->_paced_time = 0;
236   }
237 
238   // Evacuation OOM handling
239   static bool is_oom_during_evac(Thread* thread) {
240     return data(thread)->_oom_during_evac;
241   }
242 
243   static void set_oom_during_evac(Thread* thread, bool oom) {
244     data(thread)->_oom_during_evac = oom;
245   }
246 
247   static uint8_t evac_oom_scope_level(Thread* thread) {
248     return data(thread)->_oom_scope_nesting_level;
249   }
250 
251   // Push the scope one level deeper, return previous level
252   static uint8_t push_evac_oom_scope(Thread* thread) {
253     uint8_t level = evac_oom_scope_level(thread);
254     assert(level < 254, "Overflow nesting level"); // UINT8_MAX = 255
255     data(thread)->_oom_scope_nesting_level = level + 1;
256     return level;
257   }
258 
259   // Pop the scope by one level, return previous level
260   static uint8_t pop_evac_oom_scope(Thread* thread) {
261     uint8_t level = evac_oom_scope_level(thread);
262     assert(level > 0, "Underflow nesting level");
263     data(thread)->_oom_scope_nesting_level = level - 1;
264     return level;
265   }
266 
267   static bool is_evac_allowed(Thread* thread) {
268     return evac_oom_scope_level(thread) > 0;
269   }
270 
271   // Offsets
272   static ByteSize satb_mark_queue_active_offset() {
273     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active();
274   }
275 
276   static ByteSize satb_mark_queue_index_offset() {
277     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_index();
278   }
279 
280   static ByteSize satb_mark_queue_buffer_offset() {
281     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_buf();
282   }
283 
284   static ByteSize gc_state_offset() {
285     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state);
286   }
287 };
288 
289 STATIC_ASSERT(sizeof(ShenandoahThreadLocalData) <= sizeof(GCThreadLocalData));
290 
291 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP