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