1 /*
   2  * Copyright (c) 2018, 2019, 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/shenandoah/shenandoahBarrierSet.hpp"
  30 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  31 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
  32 #include "runtime/thread.hpp"
  33 #include "utilities/debug.hpp"
  34 #include "utilities/sizes.hpp"
  35 
  36 class ShenandoahThreadLocalData {
  37 public:
  38   static const uint INVALID_WORKER_ID = uint(-1);
  39 
  40 private:
  41   char _gc_state;
  42   char _oom_during_evac;
  43   ShenandoahSATBMarkQueue _satb_mark_queue;
  44   PLAB* _gclab;
  45   size_t _gclab_size;
  46   uint  _worker_id;
  47   bool _force_satb_flush;
  48   int  _disarmed_value;
  49 
  50   ShenandoahThreadLocalData() :
  51     _gc_state(0),
  52     _oom_during_evac(0),
  53     _satb_mark_queue(&ShenandoahBarrierSet::satb_mark_queue_set()),
  54     _gclab(NULL),
  55     _gclab_size(0),
  56     _worker_id(INVALID_WORKER_ID),
  57     _force_satb_flush(false),
  58     _disarmed_value(ShenandoahCodeRoots::disarmed_value()) {
  59   }
  60 
  61   ~ShenandoahThreadLocalData() {
  62     if (_gclab != NULL) {
  63       delete _gclab;
  64     }
  65   }
  66 
  67   static ShenandoahThreadLocalData* data(Thread* thread) {
  68     assert(UseShenandoahGC, "Sanity");
  69     return thread->gc_data<ShenandoahThreadLocalData>();
  70   }
  71 
  72   static ByteSize satb_mark_queue_offset() {
  73     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _satb_mark_queue);
  74   }
  75 
  76 public:
  77   static void create(Thread* thread) {
  78     new (data(thread)) ShenandoahThreadLocalData();
  79   }
  80 
  81   static void destroy(Thread* thread) {
  82     data(thread)->~ShenandoahThreadLocalData();
  83   }
  84 
  85   static SATBMarkQueue& satb_mark_queue(Thread* thread) {
  86     return data(thread)->_satb_mark_queue;
  87   }
  88 
  89   static bool is_oom_during_evac(Thread* thread) {
  90     return (data(thread)->_oom_during_evac & 1) == 1;
  91   }
  92 
  93   static void set_oom_during_evac(Thread* thread, bool oom) {
  94     if (oom) {
  95       data(thread)->_oom_during_evac |= 1;
  96     } else {
  97       data(thread)->_oom_during_evac &= ~1;
  98     }
  99   }
 100 
 101   static void set_gc_state(Thread* thread, char gc_state) {
 102     data(thread)->_gc_state = gc_state;
 103   }
 104 
 105   static char gc_state(Thread* thread) {
 106     return data(thread)->_gc_state;
 107   }
 108 
 109   static void set_worker_id(Thread* thread, uint id) {
 110     assert(thread->is_Worker_thread(), "Must be a worker thread");
 111     data(thread)->_worker_id = id;
 112   }
 113 
 114   static uint worker_id(Thread* thread) {
 115     assert(thread->is_Worker_thread(), "Must be a worker thread");
 116     return data(thread)->_worker_id;
 117   }
 118 
 119   static void set_force_satb_flush(Thread* thread, bool v) {
 120     data(thread)->_force_satb_flush = v;
 121   }
 122 
 123   static bool is_force_satb_flush(Thread* thread) {
 124     return data(thread)->_force_satb_flush;
 125   }
 126 
 127   static void initialize_gclab(Thread* thread) {
 128     assert (thread->is_Java_thread() || thread->is_Worker_thread(), "Only Java and GC worker threads are allowed to get GCLABs");
 129     assert(data(thread)->_gclab == NULL, "Only initialize once");
 130     data(thread)->_gclab = new PLAB(PLAB::min_size());
 131     data(thread)->_gclab_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 void set_disarmed_value(Thread* thread, int value) {
 147     data(thread)->_disarmed_value = value;
 148   }
 149 
 150 #ifdef ASSERT
 151   static void set_evac_allowed(Thread* thread, bool evac_allowed) {
 152     if (evac_allowed) {
 153       data(thread)->_oom_during_evac |= 2;
 154     } else {
 155       data(thread)->_oom_during_evac &= ~2;
 156     }
 157   }
 158 
 159   static bool is_evac_allowed(Thread* thread) {
 160     return (data(thread)->_oom_during_evac & 2) == 2;
 161   }
 162 #endif
 163 
 164   // Offsets
 165   static ByteSize satb_mark_queue_active_offset() {
 166     return satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active();
 167   }
 168 
 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 disarmed_value_offset() {
 182     return Thread::gc_data_offset() + byte_offset_of(ShenandoahThreadLocalData, _disarmed_value);
 183   }
 184 };
 185 
 186 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHTHREADLOCALDATA_HPP