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/gc_globals.hpp"
 30 #include "gc/shared/gcThreadLocalData.hpp"
 31 #include "gc/shared/plab.hpp"
 32 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 33 #include "gc/shenandoah/shenandoahAffiliation.hpp"
 34 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 35 #include "gc/shenandoah/shenandoahCardTable.hpp"
 36 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
 37 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 38 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
 39 #include "gc/shenandoah/shenandoahPLAB.hpp"
 40 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
 41 #include "runtime/javaThread.hpp"
 42 #include "utilities/debug.hpp"
 43 #include "utilities/sizes.hpp"
 44 
 45 class ShenandoahThreadLocalData {
 46 private:
 47   // Thread-local mirror for global GC state
 48   char _gc_state;
 49 
 50   // Quickened version of GC state.
 51   // This allows all architectures to quickly check the group of states by using a single byte load.
 52   enum FastGCState {
 53     FORWARDED               = ShenandoahHeap::HAS_FORWARDED,
 54     MARKING                 = ShenandoahHeap::MARKING,
 55     WEAK                    = ShenandoahHeap::WEAK_ROOTS,
 56     FORWARDED_MARKING       = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING,
 57     FORWARDED_WEAK          = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::WEAK_ROOTS,
 58     MARKING_WEAK            = ShenandoahHeap::MARKING       | ShenandoahHeap::WEAK_ROOTS,
 59     FORWARDED_MARKING_WEAK  = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING    | ShenandoahHeap::WEAK_ROOTS
 60   };
 61 
 62   enum FastGCStatePos {
 63     POS_FORWARDED               = 0,
 64     POS_MARKING                 = 1,
 65     POS_WEAK                    = 2,
 66     POS_FORWARDED_MARKING       = 3,
 67     POS_FORWARDED_WEAK          = 4,
 68     POS_MARKING_WEAK            = 5,
 69     POS_FORWARDED_MARKING_WEAK  = 6,
 70     POS_MAX
 71   };
 72 
 73   char _gc_state_fast_array[POS_MAX];
 74 
 75   SATBMarkQueue           _satb_mark_queue;
 76 
 77   // Current active CardTable's byte_map_base for this thread.
 78   CardTable::CardValue*   _card_table;
 79 
 80   // Thread-local allocation buffer for object evacuations.
 81   // In generational mode, it is exclusive to the young generation.
 82   PLAB* _gclab;
 83   size_t _gclab_size;
 84 
 85   // Thread-local allocation buffer only used in generational mode.
 86   // Used both by mutator threads and by GC worker threads
 87   // for evacuations within the old generation and
 88   // for promotions from the young generation into the old generation.
 89   ShenandoahPLAB* _shenandoah_plab;
 90 
 91   ShenandoahEvacuationStats* _evacuation_stats;
 92 
 93   Atomic<HeapWord*> _invisible_root;
 94   Atomic<size_t> _invisible_root_word_size;
 95 
 96   // Thread-local pin cache used to increment/decrement the pin count for
 97   // a region and flush the accumulated count to the shared pin counter.
 98   // This avoids contended atomic updates of the shared pin counter.
 99   size_t _pin_region_idx;
100   size_t _pin_count;
101 
102   ShenandoahThreadLocalData();
103   ~ShenandoahThreadLocalData();
104 
105   static ShenandoahThreadLocalData* data(Thread* thread) {
106     assert(UseShenandoahGC, "Sanity");
107     return thread->gc_data<ShenandoahThreadLocalData>();
108   }
109 
110   static ByteSize satb_mark_queue_offset() {
111     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _satb_mark_queue);
112   }
113 
114 public:
115   static void create(Thread* thread) {
116     new (data(thread)) ShenandoahThreadLocalData();
117   }
118 
119   static void destroy(Thread* thread) {
120     data(thread)->~ShenandoahThreadLocalData();
121   }
122 
123   static SATBMarkQueue& satb_mark_queue(Thread* thread) {
124     return data(thread)->_satb_mark_queue;
125   }
126 
127   static char gc_state_to_fast_array_index(char gc_state) {
128     if (gc_state == FORWARDED)              return POS_FORWARDED;
129     if (gc_state == MARKING)                return POS_MARKING;
130     if (gc_state == WEAK)                   return POS_WEAK;
131     if (gc_state == FORWARDED_MARKING)      return POS_FORWARDED_MARKING;
132     if (gc_state == FORWARDED_WEAK)         return POS_FORWARDED_WEAK;
133     if (gc_state == MARKING_WEAK)           return POS_MARKING_WEAK;
134     if (gc_state == FORWARDED_MARKING_WEAK) return POS_FORWARDED_MARKING_WEAK;
135     ShouldNotReachHere();
136     return 0;
137   }
138 
139   static void set_gc_state(Thread* thread, char gc_state) {
140     ShenandoahThreadLocalData* d = data(thread);
141     d->_gc_state = gc_state;
142     d->_gc_state_fast_array[POS_FORWARDED]              = (gc_state & FORWARDED) != 0;
143     d->_gc_state_fast_array[POS_MARKING]                = (gc_state & MARKING) != 0;
144     d->_gc_state_fast_array[POS_WEAK]                   = (gc_state & WEAK) != 0;
145     d->_gc_state_fast_array[POS_FORWARDED_MARKING]      = (gc_state & FORWARDED_MARKING) != 0;
146     d->_gc_state_fast_array[POS_FORWARDED_WEAK]         = (gc_state & FORWARDED_WEAK) != 0;
147     d->_gc_state_fast_array[POS_MARKING_WEAK]           = (gc_state & MARKING_WEAK) != 0;
148     d->_gc_state_fast_array[POS_FORWARDED_MARKING_WEAK] = (gc_state & FORWARDED_MARKING_WEAK) != 0;
149   }
150 
151   static char gc_state(Thread* thread) {
152     return data(thread)->_gc_state;
153   }
154 
155   static bool is_gc_state(Thread* thread, ShenandoahHeap::GCState state) {
156     return (gc_state(thread) & state) != 0;
157   }
158 
159   static bool is_gc_state(ShenandoahHeap::GCState state) {
160     return is_gc_state(Thread::current(), state);
161   }
162 
163   static void set_card_table(Thread* thread, CardTable::CardValue* ct) {
164     assert(ct != nullptr, "trying to set thread local card_table pointer to nullptr.");
165     data(thread)->_card_table = ct;
166   }
167 
168   static CardTable::CardValue* card_table(Thread* thread) {
169     CardTable::CardValue* ct = data(thread)->_card_table;
170     assert(ct != nullptr, "returning a null thread local card_table pointer.");
171     return ct;
172   }
173 
174   static void initialize_gclab(Thread* thread) {
175     assert(data(thread)->_gclab == nullptr, "Only initialize once");
176     data(thread)->_gclab = new PLAB(PLAB::min_size());
177     data(thread)->_gclab_size = 0;
178 
179     if (ShenandoahHeap::heap()->mode()->is_generational()) {
180       data(thread)->_shenandoah_plab = new ShenandoahPLAB();
181     }
182   }
183 
184   static PLAB* gclab(Thread* thread) {
185     return data(thread)->_gclab;
186   }
187 
188   static size_t gclab_size(Thread* thread) {
189     return data(thread)->_gclab_size;
190   }
191 
192   static void set_gclab_size(Thread* thread, size_t v) {
193     data(thread)->_gclab_size = v;
194   }
195 
196   static void begin_evacuation(Thread* thread, size_t bytes, ShenandoahAffiliation from, ShenandoahAffiliation to) {
197     data(thread)->_evacuation_stats->begin_evacuation(bytes, from, to);
198   }
199 
200   static void end_evacuation(Thread* thread, size_t bytes, ShenandoahAffiliation from, ShenandoahAffiliation to) {
201     data(thread)->_evacuation_stats->end_evacuation(bytes, from, to);
202   }
203 
204   static ShenandoahEvacuationStats* evacuation_stats(Thread* thread) {
205     return data(thread)->_evacuation_stats;
206   }
207 
208   static ShenandoahPLAB* shenandoah_plab(Thread* thread) {
209     return data(thread)->_shenandoah_plab;
210   }
211 
212   // Offsets
213   static ByteSize satb_mark_queue_index_offset() {
214     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_index();
215   }
216 
217   static ByteSize satb_mark_queue_buffer_offset() {
218     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_buf();
219   }
220 
221   static ByteSize gc_state_offset() {
222     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state);
223   }
224 
225   static ByteSize gc_state_fast_array_offset(char gc_state) {
226     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state_fast_array) + in_ByteSize(gc_state_to_fast_array_index(gc_state));
227   }
228 
229   static ByteSize card_table_offset() {
230     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _card_table);
231   }
232 
233   // invisible root are the partially initialized obj array set by ShenandoahObjArrayAllocator
234   static void set_invisible_root(Thread* thread, HeapWord* invisible_root, size_t word_size) {
235     data(thread)->_invisible_root.store_relaxed(invisible_root);
236     data(thread)->_invisible_root_word_size.store_relaxed(word_size);
237   }
238 
239   static void clear_invisible_root(Thread* thread) {
240     data(thread)->_invisible_root.store_relaxed(nullptr);
241     data(thread)->_invisible_root_word_size.store_relaxed(0);
242   }
243 
244   static HeapWord* get_invisible_root(Thread* thread) {
245     return data(thread)->_invisible_root.load_relaxed();
246   }
247 
248   static size_t get_invisible_root_word_size(Thread* thread) {
249     return data(thread)->_invisible_root_word_size.load_relaxed();
250   }
251 
252   static size_t pin_cache_region(Thread* thread) {
253     return data(thread)->_pin_region_idx;
254   }
255 
256   static size_t pin_cache_count(Thread* thread) {
257     return data(thread)->_pin_count;
258   }
259 
260   static void pin_cache_set_region(Thread* thread, size_t region_idx) {
261     data(thread)->_pin_region_idx = region_idx;
262   }
263 
264   static void pin_cache_set_count(Thread* thread, size_t new_count) {
265     data(thread)->_pin_count = new_count;
266   }
267 };
268 
269 STATIC_ASSERT(sizeof(ShenandoahThreadLocalData) <= sizeof(GCThreadLocalData));
270 
271 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP