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   SATBMarkQueue           _satb_mark_queue;
 45   PLAB* _gclab;
 46   size_t _gclab_size;
 47   double _paced_time;
 48 
 49   ShenandoahThreadLocalData() :
 50     _gc_state(0),
 51     _oom_scope_nesting_level(0),
 52     _oom_during_evac(false),
 53     _satb_mark_queue(&ShenandoahBarrierSet::satb_mark_queue_set()),
 54     _gclab(NULL),
 55     _gclab_size(0),
 56     _paced_time(0) {
 57   }
 58 
 59   ~ShenandoahThreadLocalData() {
 60     if (_gclab != NULL) {
 61       delete _gclab;
 62     }
 63   }
 64 
 65   static ShenandoahThreadLocalData* data(Thread* thread) {
 66     assert(UseShenandoahGC, "Sanity");
 67     return thread->gc_data<ShenandoahThreadLocalData>();
 68   }
 69 
 70   static ByteSize satb_mark_queue_offset() {
 71     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _satb_mark_queue);
 72   }
 73 
 74 public:
 75   static void create(Thread* thread) {
 76     new (data(thread)) ShenandoahThreadLocalData();
 77   }
 78 
 79   static void destroy(Thread* thread) {
 80     data(thread)->~ShenandoahThreadLocalData();
 81   }
 82 
 83   static SATBMarkQueue& satb_mark_queue(Thread* thread) {
 84     return data(thread)->_satb_mark_queue;
 85   }
 86 
 87   static void set_gc_state(Thread* thread, char gc_state) {
 88     data(thread)->_gc_state = gc_state;
 89   }
 90 
 91   static char gc_state(Thread* thread) {
 92     return data(thread)->_gc_state;
 93   }
 94 
 95   static void initialize_gclab(Thread* thread) {
 96     assert (thread->is_Java_thread() || thread->is_Worker_thread(), "Only Java and GC worker threads are allowed to get GCLABs");
 97     assert(data(thread)->_gclab == NULL, "Only initialize once");
 98     data(thread)->_gclab = new PLAB(PLAB::min_size());
 99     data(thread)->_gclab_size = 0;
100   }
101 
102   static PLAB* gclab(Thread* thread) {
103     return data(thread)->_gclab;
104   }
105 
106   static size_t gclab_size(Thread* thread) {
107     return data(thread)->_gclab_size;
108   }
109 
110   static void set_gclab_size(Thread* thread, size_t v) {
111     data(thread)->_gclab_size = v;
112   }
113 
114   static void add_paced_time(Thread* thread, double v) {
115     data(thread)->_paced_time += v;
116   }
117 
118   static double paced_time(Thread* thread) {
119     return data(thread)->_paced_time;
120   }
121 
122   static void reset_paced_time(Thread* thread) {
123     data(thread)->_paced_time = 0;
124   }
125 
126   // Evacuation OOM handling
127   static bool is_oom_during_evac(Thread* thread) {
128     return data(thread)->_oom_during_evac;
129   }
130 
131   static void set_oom_during_evac(Thread* thread, bool oom) {
132     data(thread)->_oom_during_evac = oom;
133   }
134 
135   static uint8_t evac_oom_scope_level(Thread* thread) {
136     return data(thread)->_oom_scope_nesting_level;
137   }
138 
139   // Push the scope one level deeper, return previous level
140   static uint8_t push_evac_oom_scope(Thread* thread) {
141     uint8_t level = evac_oom_scope_level(thread);
142     assert(level < 254, "Overflow nesting level"); // UINT8_MAX = 255
143     data(thread)->_oom_scope_nesting_level = level + 1;
144     return level;
145   }
146 
147   // Pop the scope by one level, return previous level
148   static uint8_t pop_evac_oom_scope(Thread* thread) {
149     uint8_t level = evac_oom_scope_level(thread);
150     assert(level > 0, "Underflow nesting level");
151     data(thread)->_oom_scope_nesting_level = level - 1;
152     return level;
153   }
154 
155   static bool is_evac_allowed(Thread* thread) {
156     return evac_oom_scope_level(thread) > 0;
157   }
158 
159   // Offsets
160   static ByteSize satb_mark_queue_active_offset() {
161     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active();
162   }
163 
164   static ByteSize satb_mark_queue_index_offset() {
165     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_index();
166   }
167 
168   static ByteSize satb_mark_queue_buffer_offset() {
169     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_buf();
170   }
171 
172   static ByteSize gc_state_offset() {
173     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state);
174   }
175 };
176 
177 STATIC_ASSERT(sizeof(ShenandoahThreadLocalData) <= sizeof(GCThreadLocalData));
178 
179 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP