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