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