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_SHENANDOAHHEAP_INLINE_HPP 25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP 26 27 #include "gc/shared/cmBitMap.inline.hpp" 28 #include "gc/shared/threadLocalAllocBuffer.inline.hpp" 29 #include "gc/shenandoah/brooksPointer.inline.hpp" 30 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp" 31 #include "gc/shenandoah/shenandoahConnectionMatrix.hpp" 32 #include "gc/shenandoah/shenandoahHeap.hpp" 33 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" 34 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 35 #include "oops/oop.inline.hpp" 36 #include "runtime/atomic.hpp" 37 #include "runtime/prefetch.hpp" 38 #include "runtime/prefetch.inline.hpp" 39 #include "utilities/copy.hpp" 40 41 template <class T> 42 void SCMUpdateRefsClosure::do_oop_work(T* p) { 43 T o = oopDesc::load_heap_oop(p); 44 if (! oopDesc::is_null(o)) { 45 oop obj = oopDesc::decode_heap_oop_not_null(o); 46 _heap->update_oop_ref_not_null(p, obj); 47 } 48 } 49 50 void SCMUpdateRefsClosure::do_oop(oop* p) { do_oop_work(p); } 51 void SCMUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); } 52 53 /* 54 * Marks the object. Returns true if the object has not been marked before and has 55 * been marked by this thread. Returns false if the object has already been marked, 56 * or if a competing thread succeeded in marking this object. 57 */ 58 inline bool ShenandoahHeap::mark_next(oop obj) const { 59 #ifdef ASSERT 60 if (! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))) { 61 tty->print_cr("heap region containing obj:"); 62 ShenandoahHeapRegion* obj_region = heap_region_containing(obj); 63 obj_region->print(); 64 tty->print_cr("heap region containing forwardee:"); 65 ShenandoahHeapRegion* forward_region = heap_region_containing(oopDesc::bs()->read_barrier(obj)); 66 forward_region->print(); 67 } 68 #endif 69 70 assert(oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj)), "only mark forwarded copy of objects"); 71 return mark_next_no_checks(obj); 72 } 73 74 inline bool ShenandoahHeap::mark_next_no_checks(oop obj) const { 75 HeapWord* addr = (HeapWord*) obj; 76 return (! allocated_after_next_mark_start(addr)) && _next_mark_bit_map->parMark(addr); 77 } 78 79 inline bool ShenandoahHeap::is_marked_next(oop obj) const { 80 HeapWord* addr = (HeapWord*) obj; 81 return allocated_after_next_mark_start(addr) || _next_mark_bit_map->isMarked(addr); 82 } 83 84 inline bool ShenandoahHeap::is_marked_complete(oop obj) const { 85 HeapWord* addr = (HeapWord*) obj; 86 return allocated_after_complete_mark_start(addr) || _complete_mark_bit_map->isMarked(addr); 87 } 88 89 inline bool ShenandoahHeap::need_update_refs() const { 90 return _need_update_refs; 91 } 92 93 inline uint ShenandoahHeap::heap_region_index_containing(const void* addr) const { 94 uintptr_t region_start = ((uintptr_t) addr); 95 uintptr_t index = (region_start - (uintptr_t) _first_region_bottom) >> ShenandoahHeapRegion::RegionSizeShift; 96 #ifdef ASSERT 97 if (!(index < _num_regions)) { 98 tty->print_cr("heap region does not contain address, first_region_bottom: "PTR_FORMAT \ 99 ", real bottom of first region: "PTR_FORMAT", num_regions: "SIZE_FORMAT", region_size: "SIZE_FORMAT, 100 p2i(_first_region_bottom), 101 p2i(_ordered_regions->get(0)->bottom()), 102 _num_regions, 103 ShenandoahHeapRegion::RegionSizeBytes); 104 } 105 #endif 106 assert(index < _num_regions, "heap region index must be in range"); 107 return index; 108 } 109 110 inline ShenandoahHeapRegion* ShenandoahHeap::heap_region_containing(const void* addr) const { 111 uint index = heap_region_index_containing(addr); 112 ShenandoahHeapRegion* result = _ordered_regions->get(index); 113 #ifdef ASSERT 114 if (!(addr >= result->bottom() && addr < result->end())) { 115 tty->print_cr("heap region does not contain address, first_region_bottom: "PTR_FORMAT \ 116 ", real bottom of first region: "PTR_FORMAT", num_regions: "SIZE_FORMAT, 117 p2i(_first_region_bottom), 118 p2i(_ordered_regions->get(0)->bottom()), 119 _num_regions); 120 } 121 #endif 122 assert(addr >= result->bottom() && addr < result->end(), "address must be in found region"); 123 return result; 124 } 125 126 template <class T> 127 inline oop ShenandoahHeap::update_oop_ref_not_null(T* p, oop obj) { 128 if (in_collection_set(obj)) { 129 oop forw = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 130 assert(! oopDesc::unsafe_equals(forw, obj) || is_full_gc_in_progress() || cancelled_concgc(), "expect forwarded object"); 131 obj = forw; 132 oopDesc::encode_store_heap_oop(p, obj); 133 } 134 #ifdef ASSERT 135 else { 136 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), "expect not forwarded"); 137 } 138 #endif 139 return obj; 140 } 141 142 template <class T> 143 inline oop ShenandoahHeap::maybe_update_oop_ref(T* p) { 144 T o = oopDesc::load_heap_oop(p); 145 if (! oopDesc::is_null(o)) { 146 oop obj = oopDesc::decode_heap_oop_not_null(o); 147 return maybe_update_oop_ref_not_null(p, obj); 148 } else { 149 return NULL; 150 } 151 } 152 153 inline oop ShenandoahHeap::atomic_compare_exchange_oop(oop n, oop* addr, oop c) { 154 return (oop) Atomic::cmpxchg_ptr(n, addr, c); 155 } 156 157 inline oop ShenandoahHeap::atomic_compare_exchange_oop(oop n, narrowOop* addr, oop c) { 158 narrowOop cmp = oopDesc::encode_heap_oop(c); 159 narrowOop val = oopDesc::encode_heap_oop(n); 160 return oopDesc::decode_heap_oop((narrowOop) Atomic::cmpxchg(val, addr, cmp)); 161 } 162 163 template <class T> 164 inline oop ShenandoahHeap::maybe_update_oop_ref_not_null(T* p, oop heap_oop) { 165 166 assert((! is_in(p)) || (! in_collection_set(p)) 167 || is_full_gc_in_progress(), 168 "never update refs in from-space, unless evacuation has been cancelled"); 169 170 #ifdef ASSERT 171 if (! is_in(heap_oop)) { 172 print_heap_regions(); 173 tty->print_cr("object not in heap: "PTR_FORMAT", referenced by: "PTR_FORMAT, p2i((HeapWord*) heap_oop), p2i(p)); 174 assert(is_in(heap_oop), "object must be in heap"); 175 } 176 #endif 177 assert(is_in(heap_oop), "only ever call this on objects in the heap"); 178 if (in_collection_set(heap_oop)) { 179 oop forwarded_oop = ShenandoahBarrierSet::resolve_oop_static_not_null(heap_oop); // read brooks ptr 180 if (oopDesc::unsafe_equals(forwarded_oop, heap_oop)) { 181 // E.g. during evacuation. 182 return forwarded_oop; 183 } 184 185 assert(! oopDesc::unsafe_equals(forwarded_oop, heap_oop) || is_full_gc_in_progress(), "expect forwarded object"); 186 187 log_develop_trace(gc)("Updating old ref: "PTR_FORMAT" pointing to "PTR_FORMAT" to new ref: "PTR_FORMAT, 188 p2i(p), p2i(heap_oop), p2i(forwarded_oop)); 189 190 assert(forwarded_oop->is_oop(), "oop required"); 191 assert(is_in(forwarded_oop), "forwardee must be in heap"); 192 assert(oopDesc::bs()->is_safe(forwarded_oop), "forwardee must not be in collection set"); 193 // If this fails, another thread wrote to p before us, it will be logged in SATB and the 194 // reference be updated later. 195 oop result = atomic_compare_exchange_oop(forwarded_oop, p, heap_oop); 196 197 if (oopDesc::unsafe_equals(result, heap_oop)) { // CAS successful. 198 return forwarded_oop; 199 } else { 200 return NULL; 201 } 202 } else { 203 assert(oopDesc::unsafe_equals(heap_oop, ShenandoahBarrierSet::resolve_oop_static_not_null(heap_oop)), 204 "expect not forwarded"); 205 return heap_oop; 206 } 207 } 208 209 inline bool ShenandoahHeap::cancelled_concgc() const { 210 return (jbyte) OrderAccess::load_acquire((jbyte*) &_cancelled_concgc); 211 } 212 213 inline bool ShenandoahHeap::try_cancel_concgc() const { 214 return Atomic::cmpxchg(true, (jbyte*) &_cancelled_concgc, false) == false; 215 } 216 217 inline void ShenandoahHeap::set_cancelled_concgc(bool v) { 218 OrderAccess::release_store_fence((jbyte*) &_cancelled_concgc, (jbyte) v); 219 } 220 221 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) { 222 if (UseTLAB) { 223 HeapWord* obj = thread->gclab().allocate(size); 224 if (obj != NULL) { 225 return obj; 226 } 227 // Otherwise... 228 return allocate_from_gclab_slow(thread, size); 229 } else { 230 return NULL; 231 } 232 } 233 234 class UpdateMatrixClosure : public ExtendedOopClosure { 235 236 private: 237 uint _from_idx; 238 ShenandoahHeap* _heap; 239 ShenandoahConnectionMatrix* _matrix; 240 241 template <class T> 242 inline void do_oop_nv(T* o) { 243 T t = oopDesc::load_heap_oop(o); 244 if (! oopDesc::is_null(t)) { 245 oop obj = oopDesc::decode_heap_oop_not_null(t); 246 uint to_idx = _heap->heap_region_index_containing(obj); 247 _matrix->set_connected(_from_idx, to_idx, true); 248 } 249 } 250 251 public: 252 253 UpdateMatrixClosure(uint from_idx) : 254 _from_idx(from_idx), 255 _heap(ShenandoahHeap::heap()), 256 _matrix(ShenandoahHeap::heap()->connection_matrix()) { 257 } 258 259 void do_oop(oop* o) { 260 do_oop_nv(o); 261 } 262 263 void do_oop(narrowOop* o) { 264 do_oop_nv(o); 265 } 266 }; 267 268 inline void ShenandoahHeap::copy_object(oop p, HeapWord* s, size_t words) { 269 assert(s != NULL, "allocation of brooks pointer must not fail"); 270 HeapWord* copy = s + BrooksPointer::word_size(); 271 272 guarantee(copy != NULL, "allocation of copy object must not fail"); 273 Copy::aligned_disjoint_words((HeapWord*) p, copy, words); 274 BrooksPointer::initialize(oop(copy)); 275 276 log_develop_trace(gc, compaction)("copy object from "PTR_FORMAT" to: "PTR_FORMAT, p2i((HeapWord*) p), p2i(copy)); 277 } 278 279 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) { 280 size_t required; 281 282 #ifdef ASSERT 283 ShenandoahHeapRegion* hr = NULL; 284 if (ShenandoahVerifyReadsToFromSpace) { 285 hr = heap_region_containing(p); 286 { 287 hr->memProtectionOff(); 288 required = BrooksPointer::word_size() + p->size(); 289 hr->memProtectionOn(); 290 } 291 } else { 292 required = BrooksPointer::word_size() + p->size(); 293 } 294 #else 295 required = BrooksPointer::word_size() + p->size(); 296 #endif 297 298 assert(! heap_region_containing(p)->is_humongous(), "never evacuate humongous objects"); 299 300 bool alloc_from_gclab = true; 301 HeapWord* filler = allocate_from_gclab(thread, required); 302 if (filler == NULL) { 303 filler = allocate_memory(required, true); 304 alloc_from_gclab = false; 305 } 306 307 #ifdef ASSERT 308 // Checking that current Java thread does not hold Threads_lock when we get here. 309 // If that ever be the case, we'd deadlock in oom_during_evacuation. 310 if ((! Thread::current()->is_GC_task_thread()) && (! Thread::current()->is_ConcurrentGC_thread())) { 311 assert(! Threads_lock->owned_by_self() 312 || SafepointSynchronize::is_at_safepoint(), "must not hold Threads_lock here"); 313 } 314 #endif 315 316 if (filler == NULL) { 317 oom_during_evacuation(); 318 // If this is a Java thread, it should have waited 319 // until all GC threads are done, and then we 320 // return the forwardee. 321 oop resolved = ShenandoahBarrierSet::resolve_oop_static(p); 322 return resolved; 323 } 324 325 HeapWord* copy = filler + BrooksPointer::word_size(); 326 327 #ifdef ASSERT 328 if (ShenandoahVerifyReadsToFromSpace) { 329 hr->memProtectionOff(); 330 copy_object(p, filler, required - BrooksPointer::word_size()); 331 hr->memProtectionOn(); 332 } else { 333 copy_object(p, filler, required - BrooksPointer::word_size()); 334 } 335 #else 336 copy_object(p, filler, required - BrooksPointer::word_size()); 337 #endif 338 339 oop copy_val = oop(copy); 340 oop result = BrooksPointer::try_update_forwardee(p, copy_val); 341 342 oop return_val; 343 if (oopDesc::unsafe_equals(result, p)) { 344 return_val = copy_val; 345 346 log_develop_trace(gc, compaction)("Copy of "PTR_FORMAT" to "PTR_FORMAT" succeeded \n", 347 p2i((HeapWord*) p), p2i(copy)); 348 349 #ifdef ASSERT 350 assert(return_val->is_oop(), "expect oop"); 351 assert(p->klass() == return_val->klass(), "Should have the same class p: "PTR_FORMAT", copy: "PTR_FORMAT, 352 p2i((HeapWord*) p), p2i((HeapWord*) copy)); 353 354 uint from_idx = heap_region_index_containing(copy_val); 355 UpdateMatrixClosure cl(from_idx); 356 copy_val->oop_iterate(&cl); 357 #endif 358 } else { 359 if (alloc_from_gclab) { 360 thread->gclab().rollback(required); 361 } 362 log_develop_trace(gc, compaction)("Copy of "PTR_FORMAT" to "PTR_FORMAT" failed, use other: "PTR_FORMAT, 363 p2i((HeapWord*) p), p2i(copy), p2i((HeapWord*) result)); 364 return_val = result; 365 } 366 367 return return_val; 368 } 369 370 inline bool ShenandoahHeap::requires_marking(const void* entry) const { 371 return ! is_marked_next(oop(entry)); 372 } 373 374 bool ShenandoahHeap::region_in_collection_set(size_t region_index) const { 375 return _in_cset_fast_test_base[region_index]; 376 } 377 378 bool ShenandoahHeap::in_collection_set(ShenandoahHeapRegion* r) const { 379 return region_in_collection_set(r->region_number()); 380 } 381 382 template <class T> 383 inline bool ShenandoahHeap::in_collection_set(T p) const { 384 HeapWord* obj = (HeapWord*) p; 385 assert(_in_cset_fast_test != NULL, "sanity"); 386 assert(is_in(obj), "should be in heap"); 387 388 // no need to subtract the bottom of the heap from obj, 389 // _in_cset_fast_test is biased 390 uintx index = ((uintx) obj) >> ShenandoahHeapRegion::RegionSizeShift; 391 return _in_cset_fast_test[index]; 392 } 393 394 inline bool ShenandoahHeap::concurrent_mark_in_progress() { 395 return _concurrent_mark_in_progress != 0; 396 } 397 398 inline address ShenandoahHeap::concurrent_mark_in_progress_addr() { 399 return (address) &(ShenandoahHeap::heap()->_concurrent_mark_in_progress); 400 } 401 402 inline bool ShenandoahHeap::is_evacuation_in_progress() { 403 return _evacuation_in_progress != 0; 404 } 405 406 inline bool ShenandoahHeap::allocated_after_next_mark_start(HeapWord* addr) const { 407 uintx index = ((uintx) addr) >> ShenandoahHeapRegion::RegionSizeShift; 408 HeapWord* top_at_mark_start = _next_top_at_mark_starts[index]; 409 bool alloc_after_mark_start = addr >= top_at_mark_start; 410 return alloc_after_mark_start; 411 } 412 413 inline bool ShenandoahHeap::allocated_after_complete_mark_start(HeapWord* addr) const { 414 uintx index = ((uintx) addr) >> ShenandoahHeapRegion::RegionSizeShift; 415 HeapWord* top_at_mark_start = _complete_top_at_mark_starts[index]; 416 bool alloc_after_mark_start = addr >= top_at_mark_start; 417 return alloc_after_mark_start; 418 } 419 420 template<class T> 421 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) { 422 assert(BrooksPointer::word_offset() < 0, "skip_delta calculation below assumes the forwarding ptr is before obj"); 423 424 CMBitMap* mark_bit_map = _complete_mark_bit_map; 425 HeapWord* top_at_mark_start = complete_top_at_mark_start(region->bottom()); 426 427 size_t skip_bitmap_delta = BrooksPointer::word_size() + 1; 428 size_t skip_objsize_delta = BrooksPointer::word_size() /* + actual obj.size() below */; 429 HeapWord* start = region->bottom() + BrooksPointer::word_size(); 430 431 HeapWord* limit = region->top(); 432 HeapWord* end = MIN2(top_at_mark_start + BrooksPointer::word_size(), _ordered_regions->end()); 433 HeapWord* addr = mark_bit_map->getNextMarkedWordAddress(start, end); 434 435 intx dist = ShenandoahMarkScanPrefetch; 436 if (dist > 0) { 437 // Batched scan that prefetches the oop data, anticipating the access to 438 // either header, oop field, or forwarding pointer. Not that we cannot 439 // touch anything in oop, while it still being prefetched to get enough 440 // time for prefetch to work. This is why we try to scan the bitmap linearly, 441 // disregarding the object size. However, since we know forwarding pointer 442 // preceeds the object, we can skip over it. Once we cannot trust the bitmap, 443 // there is no point for prefetching the oop contents, as oop->size() will 444 // touch it prematurely. 445 446 oop slots[dist]; 447 bool aborting = false; 448 int avail; 449 do { 450 avail = 0; 451 for (int c = 0; (c < dist) && (addr < limit); c++) { 452 Prefetch::read(addr, 1); 453 oop obj = oop(addr); 454 slots[avail++] = obj; 455 if (addr < top_at_mark_start) { 456 addr += skip_bitmap_delta; 457 addr = mark_bit_map->getNextMarkedWordAddress(addr, end); 458 } else { 459 // cannot trust mark bitmap anymore, finish the current stride, 460 // and switch to accurate traversal 461 addr += obj->size() + skip_objsize_delta; 462 aborting = true; 463 } 464 } 465 466 for (int c = 0; c < avail; c++) { 467 do_marked_object(mark_bit_map, cl, slots[c]); 468 } 469 } while (avail > 0 && !aborting); 470 471 // accurate traversal 472 while (addr < limit) { 473 oop obj = oop(addr); 474 int size = obj->size(); 475 do_marked_object(mark_bit_map, cl, obj); 476 addr += size + skip_objsize_delta; 477 } 478 } else { 479 while (addr < limit) { 480 oop obj = oop(addr); 481 int size = obj->size(); 482 do_marked_object(mark_bit_map, cl, obj); 483 addr += size + skip_objsize_delta; 484 if (addr < top_at_mark_start) { 485 addr = mark_bit_map->getNextMarkedWordAddress(addr, end); 486 } 487 } 488 } 489 } 490 491 template<class T> 492 inline void ShenandoahHeap::do_marked_object(CMBitMap* bitmap, T* cl, oop obj) { 493 #ifdef ASSERT 494 assert(!oopDesc::is_null(obj), "sanity"); 495 assert(obj->is_oop(), "sanity"); 496 assert(is_in(obj), "sanity"); 497 assert(bitmap == _complete_mark_bit_map, "only iterate completed mark bitmap"); 498 assert(is_marked_complete(obj), "object expected to be marked"); 499 #endif 500 cl->do_object(obj); 501 } 502 503 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP