1 /*
   2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP
  26 
  27 #include "gc/shared/plab.hpp"
  28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  30 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
  31 #include "runtime/thread.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/sizes.hpp"
  34 
  35 class ShenandoahThreadLocalData {
  36 public:
  37   static const uint INVALID_WORKER_ID = uint(-1);
  38 
  39 private:
  40   char _gc_state;
  41   char _oom_during_evac;
  42   ShenandoahSATBMarkQueue _satb_mark_queue;
  43   PLAB* _gclab;
  44   size_t _gclab_size;
  45   uint  _worker_id;
  46   bool _force_satb_flush;
  47   int  _disarmed_value;
  48 
  49   ShenandoahThreadLocalData() :
  50     _gc_state(0),
  51     _oom_during_evac(0),
  52     _satb_mark_queue(&ShenandoahBarrierSet::satb_mark_queue_set()),
  53     _gclab(NULL),
  54     _gclab_size(0),
  55     _worker_id(INVALID_WORKER_ID),
  56     _force_satb_flush(false) {
  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 bool is_oom_during_evac(Thread* thread) {
  88     return (data(thread)->_oom_during_evac & 1) == 1;
  89   }
  90 
  91   static void set_oom_during_evac(Thread* thread, bool oom) {
  92     if (oom) {
  93       data(thread)->_oom_during_evac |= 1;
  94     } else {
  95       data(thread)->_oom_during_evac &= ~1;
  96     }
  97   }
  98 
  99   static void set_gc_state(Thread* thread, char gc_state) {
 100     data(thread)->_gc_state = gc_state;
 101   }
 102 
 103   static char gc_state(Thread* thread) {
 104     return data(thread)->_gc_state;
 105   }
 106 
 107   static void set_worker_id(Thread* thread, uint id) {
 108     assert(thread->is_Worker_thread(), "Must be a worker thread");
 109     data(thread)->_worker_id = id;
 110   }
 111 
 112   static uint worker_id(Thread* thread) {
 113     assert(thread->is_Worker_thread(), "Must be a worker thread");
 114     return data(thread)->_worker_id;
 115   }
 116 
 117   static void set_force_satb_flush(Thread* thread, bool v) {
 118     data(thread)->_force_satb_flush = v;
 119   }
 120 
 121   static bool is_force_satb_flush(Thread* thread) {
 122     return data(thread)->_force_satb_flush;
 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)->_disarmed_value = ShenandoahCodeRoots::disarmed_value();
 131   }
 132 
 133   static PLAB* gclab(Thread* thread) {
 134     return data(thread)->_gclab;
 135   }
 136 
 137   static size_t gclab_size(Thread* thread) {
 138     return data(thread)->_gclab_size;
 139   }
 140 
 141   static void set_gclab_size(Thread* thread, size_t v) {
 142     data(thread)->_gclab_size = v;
 143   }
 144 
 145   static void set_disarmed_value(Thread* thread, int value) {
 146     data(thread)->_disarmed_value = value;
 147   }
 148 
 149 #ifdef ASSERT
 150   static void set_evac_allowed(Thread* thread, bool evac_allowed) {
 151     if (evac_allowed) {
 152       data(thread)->_oom_during_evac |= 2;
 153     } else {
 154       data(thread)->_oom_during_evac &= ~2;
 155     }
 156   }
 157 
 158   static bool is_evac_allowed(Thread* thread) {
 159     return (data(thread)->_oom_during_evac & 2) == 2;
 160   }
 161 #endif
 162 
 163   // Offsets
 164   static ByteSize satb_mark_queue_active_offset() {
 165     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active();
 166   }
 167 
 168   static ByteSize satb_mark_queue_index_offset() {
 169     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_index();
 170   }
 171 
 172   static ByteSize satb_mark_queue_buffer_offset() {
 173     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_buf();
 174   }
 175 
 176   static ByteSize gc_state_offset() {
 177     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _gc_state);
 178   }
 179 
 180   static ByteSize disarmed_value_offset() {
 181     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _disarmed_value);
 182   }
 183 };
 184 
 185 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP