1 /*
   2  * Copyright (c) 2015, Red Hat, Inc. and/or its affiliates.
   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_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTMARK_INLINE_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTMARK_INLINE_HPP
  26 
  27 #include "gc_implementation/shenandoah/brooksPointer.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahAsserts.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahBarrierSet.inline.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahConcurrentMark.hpp"
  31 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"
  32 #include "gc_implementation/shenandoah/shenandoahTaskqueue.inline.hpp"
  33 #include "memory/iterator.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/prefetch.inline.hpp"
  36 
  37 template <class T, bool COUNT_LIVENESS>
  38 void ShenandoahConcurrentMark::do_task(ShenandoahObjToScanQueue* q, T* cl, jushort* live_data, ShenandoahMarkTask* task) {
  39   oop obj = task->obj();
  40 
  41   shenandoah_assert_not_forwarded(NULL, obj);
  42   shenandoah_assert_marked_next(NULL, obj);
  43   shenandoah_assert_not_in_cset_except(NULL, obj, _heap->cancelled_concgc());
  44 
  45   if (task->is_not_chunked()) {
  46     if (COUNT_LIVENESS) count_liveness(live_data, obj);
  47     if (obj->is_instance()) {
  48       // Case 1: Normal oop, process as usual.
  49       obj->oop_iterate(cl);
  50     } else if (obj->is_objArray()) {
  51       // Case 2: Object array instance and no chunk is set. Must be the first
  52       // time we visit it, start the chunked processing.
  53       do_chunked_array_start<T>(q, cl, obj);
  54     } else {
  55       // Case 3: Primitive array. Do nothing, no oops there. We use the same
  56       // performance tweak TypeArrayKlass::oop_oop_iterate_impl is using:
  57       // We skip iterating over the klass pointer since we know that
  58       // Universe::TypeArrayKlass never moves.
  59       assert (obj->is_typeArray(), "should be type array");
  60     }
  61   } else {
  62     // Case 4: Array chunk, has sensible chunk id. Process it.
  63     do_chunked_array<T>(q, cl, obj, task->chunk(), task->pow());
  64   }
  65 }
  66 
  67 inline void ShenandoahConcurrentMark::count_liveness(jushort* live_data, oop obj) {
  68   size_t region_idx = _heap->heap_region_index_containing(obj);
  69   ShenandoahHeapRegion* region = _heap->get_region(region_idx);
  70   if (!region->is_humongous_start()) {
  71     assert(!region->is_humongous(), "Cannot have continuations here");
  72     jushort cur = live_data[region_idx];
  73     size_t size = obj->size() + BrooksPointer::word_size();
  74     size_t max = (1 << (sizeof(jushort) * 8)) - 1;
  75     if (size >= max) {
  76       // too big, add to region data directly
  77       region->increase_live_data_gc_words(size);
  78     } else {
  79       size_t new_val = cur + size;
  80       if (new_val >= max) {
  81         // overflow, flush to region data
  82         region->increase_live_data_gc_words(new_val);
  83         live_data[region_idx] = 0;
  84       } else {
  85         // still good, remember in locals
  86         live_data[region_idx] = (jushort) new_val;
  87       }
  88     }
  89   } else {
  90     count_liveness_humongous(obj);
  91   }
  92 }
  93 
  94 inline void ShenandoahConcurrentMark::count_liveness_humongous(oop obj) {
  95   shenandoah_assert_in_correct_region(NULL, obj);
  96   size_t region_idx = _heap->heap_region_index_containing(obj);
  97   size_t size = obj->size() + BrooksPointer::word_size();
  98   size_t num_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
  99 
 100   for (size_t i = region_idx; i < region_idx + num_regions; i++) {
 101     ShenandoahHeapRegion* chain_reg = _heap->get_region(i);
 102     assert(chain_reg->is_humongous(), "Expecting a humongous region");
 103     chain_reg->increase_live_data_gc_words(chain_reg->used() >> LogHeapWordSize);
 104   }
 105 }
 106 
 107 template <class T>
 108 inline void ShenandoahConcurrentMark::do_chunked_array_start(ShenandoahObjToScanQueue* q, T* cl, oop obj) {
 109   assert(obj->is_objArray(), "expect object array");
 110   objArrayOop array = objArrayOop(obj);
 111   int len = array->length();
 112 
 113   if (len <= (int) ObjArrayMarkingStride*2) {
 114     // A few slices only, process directly
 115     array->oop_iterate_range(cl, 0, len);
 116   } else {
 117     int bits = log2_long(len);
 118     // Compensate for non-power-of-two arrays, cover the array in excess:
 119     if (len != (1 << bits)) bits++;
 120 
 121     // Only allow full chunks on the queue. This frees do_chunked_array() from checking from/to
 122     // boundaries against array->length(), touching the array header on every chunk.
 123     //
 124     // To do this, we cut the prefix in full-sized chunks, and submit them on the queue.
 125     // If the array is not divided in chunk sizes, then there would be an irregular tail,
 126     // which we will process separately.
 127 
 128     int last_idx = 0;
 129 
 130     int chunk = 1;
 131     int pow = bits;
 132 
 133     // Handle overflow
 134     if (pow >= 31) {
 135       assert (pow == 31, "sanity");
 136       pow--;
 137       chunk = 2;
 138       last_idx = (1 << pow);
 139       bool pushed = q->push(ShenandoahMarkTask(array, 1, pow));
 140       assert(pushed, "overflow queue should always succeed pushing");
 141     }
 142 
 143     // Split out tasks, as suggested in ObjArrayChunkedTask docs. Record the last
 144     // successful right boundary to figure out the irregular tail.
 145     while ((1 << pow) > (int)ObjArrayMarkingStride &&
 146            (chunk*2 < ShenandoahMarkTask::chunk_size())) {
 147       pow--;
 148       int left_chunk = chunk*2 - 1;
 149       int right_chunk = chunk*2;
 150       int left_chunk_end = left_chunk * (1 << pow);
 151       if (left_chunk_end < len) {
 152         bool pushed = q->push(ShenandoahMarkTask(array, left_chunk, pow));
 153         assert(pushed, "overflow queue should always succeed pushing");
 154         chunk = right_chunk;
 155         last_idx = left_chunk_end;
 156       } else {
 157         chunk = left_chunk;
 158       }
 159     }
 160 
 161     // Process the irregular tail, if present
 162     int from = last_idx;
 163     if (from < len) {
 164       array->oop_iterate_range(cl, from, len);
 165     }
 166   }
 167 }
 168 
 169 template <class T>
 170 inline void ShenandoahConcurrentMark::do_chunked_array(ShenandoahObjToScanQueue* q, T* cl, oop obj, int chunk, int pow) {
 171   assert(obj->is_objArray(), "expect object array");
 172   objArrayOop array = objArrayOop(obj);
 173 
 174   assert (ObjArrayMarkingStride > 0, "sanity");
 175 
 176   // Split out tasks, as suggested in ObjArrayChunkedTask docs. Avoid pushing tasks that
 177   // are known to start beyond the array.
 178   while ((1 << pow) > (int)ObjArrayMarkingStride && (chunk*2 < ShenandoahMarkTask::chunk_size())) {
 179     pow--;
 180     chunk *= 2;
 181     bool pushed = q->push(ShenandoahMarkTask(array, chunk - 1, pow));
 182     assert(pushed, "overflow queue should always succeed pushing");
 183   }
 184 
 185   int chunk_size = 1 << pow;
 186 
 187   int from = (chunk - 1) * chunk_size;
 188   int to = chunk * chunk_size;
 189 
 190 #ifdef ASSERT
 191   int len = array->length();
 192   assert (0 <= from && from < len, err_msg("from is sane: %d/%d", from, len));
 193   assert (0 < to && to <= len, err_msg("to is sane: %d/%d", to, len));
 194 #endif
 195 
 196   array->oop_iterate_range(cl, from, to);
 197 }
 198 
 199 inline bool ShenandoahConcurrentMark::try_queue(ShenandoahObjToScanQueue* q, ShenandoahMarkTask &task) {
 200   return (q->pop_buffer(task) ||
 201           q->pop_local(task) ||
 202           q->pop_overflow(task));
 203 }
 204 
 205 class ShenandoahSATBBufferClosure : public SATBBufferClosure {
 206 private:
 207   ShenandoahObjToScanQueue* _queue;
 208   ShenandoahHeap* _heap;
 209 public:
 210   ShenandoahSATBBufferClosure(ShenandoahObjToScanQueue* q) :
 211     _queue(q), _heap(ShenandoahHeap::heap())
 212   {
 213   }
 214 
 215   void do_buffer(void** buffer, size_t size) {
 216     for (size_t i = 0; i < size; ++i) {
 217       oop* p = (oop*) &buffer[i];
 218       ShenandoahConcurrentMark::mark_through_ref<oop, RESOLVE>(p, _heap, _queue);
 219     }
 220   }
 221 };
 222 
 223 inline bool ShenandoahConcurrentMark::try_draining_satb_buffer(ShenandoahObjToScanQueue *q, ShenandoahMarkTask &task) {
 224   ShenandoahSATBBufferClosure cl(q);
 225   SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
 226   bool had_refs = satb_mq_set.apply_closure_to_completed_buffer(&cl);
 227   return had_refs && try_queue(q, task);
 228 }
 229 
 230 template<class T, UpdateRefsMode UPDATE_REFS>
 231 inline void ShenandoahConcurrentMark::mark_through_ref(T *p, ShenandoahHeap* heap, ShenandoahObjToScanQueue* q) {
 232   T o = oopDesc::load_heap_oop(p);
 233   if (! oopDesc::is_null(o)) {
 234     oop obj = oopDesc::decode_heap_oop_not_null(o);
 235     switch (UPDATE_REFS) {
 236     case NONE:
 237       break;
 238     case RESOLVE:
 239       obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 240       break;
 241     case SIMPLE:
 242       // We piggy-back reference updating to the marking tasks.
 243       obj = heap->update_with_forwarded_not_null(p, obj);
 244       break;
 245     case CONCURRENT:
 246       obj = heap->maybe_update_with_forwarded_not_null(p, obj);
 247       break;
 248     default:
 249       ShouldNotReachHere();
 250     }
 251 
 252     // Note: Only when concurrently updating references can obj become NULL here.
 253     // It happens when a mutator thread beats us by writing another value. In that
 254     // case we don't need to do anything else.
 255     if (UPDATE_REFS != CONCURRENT || !oopDesc::is_null(obj)) {
 256       shenandoah_assert_not_forwarded(p, obj);
 257       shenandoah_assert_not_in_cset_except(p, obj, heap->cancelled_concgc());
 258 
 259       if (heap->mark_next(obj)) {
 260         bool pushed = q->push(ShenandoahMarkTask(obj));
 261         assert(pushed, "overflow queue should always succeed pushing");
 262       }
 263 
 264       shenandoah_assert_marked_next(p, obj);
 265     }
 266   }
 267 }
 268 
 269 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTMARK_INLINE_HPP