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