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   SATBMarkQueue           _satb_mark_queue;
 51 
 52   // Current active CardTable's byte_map_base for this thread.
 53   CardTable::CardValue*   _card_table;
 54 
 55   // Thread-local allocation buffer for object evacuations.
 56   // In generational mode, it is exclusive to the young generation.
 57   PLAB* _gclab;
 58   size_t _gclab_size;
 59 
 60   // Thread-local allocation buffer only used in generational mode.
 61   // Used both by mutator threads and by GC worker threads
 62   // for evacuations within the old generation and
 63   // for promotions from the young generation into the old generation.
 64   ShenandoahPLAB* _shenandoah_plab;
 65 
 66   ShenandoahEvacuationStats* _evacuation_stats;
 67 
 68   Atomic<HeapWord*> _invisible_root;
 69   Atomic<size_t> _invisible_root_word_size;
 70 
 71   // Thread-local pin cache used to increment/decrement the pin count for
 72   // a region and flush the accumulated count to the shared pin counter.
 73   // This avoids contended atomic updates of the shared pin counter.
 74   size_t _pin_region_idx;
 75   size_t _pin_count;
 76 
 77   ShenandoahThreadLocalData();
 78   ~ShenandoahThreadLocalData();
 79 
 80   static ShenandoahThreadLocalData* data(Thread* thread) {
 81     assert(UseShenandoahGC, "Sanity");
 82     return thread->gc_data<ShenandoahThreadLocalData>();
 83   }
 84 
 85   static ByteSize satb_mark_queue_offset() {
 86     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _satb_mark_queue);
 87   }
 88 
 89 public:
 90   static void create(Thread* thread) {
 91     new (data(thread)) ShenandoahThreadLocalData();
 92   }
 93 
 94   static void destroy(Thread* thread) {
 95     data(thread)->~ShenandoahThreadLocalData();
 96   }
 97 
 98   static SATBMarkQueue& satb_mark_queue(Thread* thread) {
 99     return data(thread)->_satb_mark_queue;
100   }
101 
102   static void set_gc_state(Thread* thread, char gc_state) {
103     ShenandoahThreadLocalData* d = data(thread);
104     d->_gc_state = gc_state;
105   }
106 
107   static char gc_state(Thread* thread) {
108     return data(thread)->_gc_state;
109   }
110 
111   static bool is_gc_state(Thread* thread, ShenandoahHeap::GCState state) {
112     return (gc_state(thread) & state) != 0;
113   }
114 
115   static bool is_gc_state(ShenandoahHeap::GCState state) {
116     return is_gc_state(Thread::current(), state);
117   }
118 
119   static void set_card_table(Thread* thread, CardTable::CardValue* ct) {
120     assert(ct != nullptr, "trying to set thread local card_table pointer to nullptr.");
121     data(thread)->_card_table = ct;
122   }
123 
124   static CardTable::CardValue* card_table(Thread* thread) {
125     CardTable::CardValue* ct = data(thread)->_card_table;
126     assert(ct != nullptr, "returning a null thread local card_table pointer.");
127     return ct;
128   }
129 
130   static void initialize_gclab(Thread* thread) {
131     assert(data(thread)->_gclab == nullptr, "Only initialize once");
132     data(thread)->_gclab = new PLAB(PLAB::min_size());
133     data(thread)->_gclab_size = 0;
134 
135     if (ShenandoahHeap::heap()->mode()->is_generational()) {
136       data(thread)->_shenandoah_plab = new ShenandoahPLAB();
137     }
138   }
139 
140   static PLAB* gclab(Thread* thread) {
141     return data(thread)->_gclab;
142   }
143 
144   static size_t gclab_size(Thread* thread) {
145     return data(thread)->_gclab_size;
146   }
147 
148   static void set_gclab_size(Thread* thread, size_t v) {
149     data(thread)->_gclab_size = v;
150   }
151 
152   static void begin_evacuation(Thread* thread, size_t bytes, ShenandoahAffiliation from, ShenandoahAffiliation to) {
153     data(thread)->_evacuation_stats->begin_evacuation(bytes, from, to);
154   }
155 
156   static void end_evacuation(Thread* thread, size_t bytes, ShenandoahAffiliation from, ShenandoahAffiliation to) {
157     data(thread)->_evacuation_stats->end_evacuation(bytes, from, to);
158   }
159 
160   static ShenandoahEvacuationStats* evacuation_stats(Thread* thread) {
161     return data(thread)->_evacuation_stats;
162   }
163 
164   static ShenandoahPLAB* shenandoah_plab(Thread* thread) {
165     return data(thread)->_shenandoah_plab;
166   }
167 
168   // Offsets
169   static ByteSize satb_mark_queue_index_offset() {
170     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_index();
171   }
172 
173   static ByteSize satb_mark_queue_buffer_offset() {
174     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_buf();
175   }
176 
177   static ByteSize gc_state_offset() {
178     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state);
179   }
180 
181   static ByteSize card_table_offset() {
182     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _card_table);
183   }
184 
185   // invisible root are the partially initialized obj array set by ShenandoahObjArrayAllocator
186   static void set_invisible_root(Thread* thread, HeapWord* invisible_root, size_t word_size) {
187     data(thread)->_invisible_root.store_relaxed(invisible_root);
188     data(thread)->_invisible_root_word_size.store_relaxed(word_size);
189   }
190 
191   static void clear_invisible_root(Thread* thread) {
192     data(thread)->_invisible_root.store_relaxed(nullptr);
193     data(thread)->_invisible_root_word_size.store_relaxed(0);
194   }
195 
196   static HeapWord* get_invisible_root(Thread* thread) {
197     return data(thread)->_invisible_root.load_relaxed();
198   }
199 
200   static size_t get_invisible_root_word_size(Thread* thread) {
201     return data(thread)->_invisible_root_word_size.load_relaxed();
202   }
203 
204   static size_t pin_cache_region(Thread* thread) {
205     return data(thread)->_pin_region_idx;
206   }
207 
208   static size_t pin_cache_count(Thread* thread) {
209     return data(thread)->_pin_count;
210   }
211 
212   static void pin_cache_set_region(Thread* thread, size_t region_idx) {
213     data(thread)->_pin_region_idx = region_idx;
214   }
215 
216   static void pin_cache_set_count(Thread* thread, size_t new_count) {
217     data(thread)->_pin_count = new_count;
218   }
219 };
220 
221 STATIC_ASSERT(sizeof(ShenandoahThreadLocalData) <= sizeof(GCThreadLocalData));
222 
223 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP