1 /* 2 * Copyright (c) 2015, 2020, Red Hat, Inc. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP 27 28 #include "classfile/javaClasses.inline.hpp" 29 #include "gc/shared/markBitMap.inline.hpp" 30 #include "gc/shared/threadLocalAllocBuffer.inline.hpp" 31 #include "gc/shared/suspendibleThreadSet.hpp" 32 #include "gc/shenandoah/shenandoahAsserts.hpp" 33 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp" 34 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp" 35 #include "gc/shenandoah/shenandoahForwarding.inline.hpp" 36 #include "gc/shenandoah/shenandoahWorkGroup.hpp" 37 #include "gc/shenandoah/shenandoahHeap.hpp" 38 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp" 39 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 40 #include "gc/shenandoah/shenandoahControlThread.hpp" 41 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp" 42 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 43 #include "oops/compressedOops.inline.hpp" 44 #include "oops/oop.inline.hpp" 45 #include "runtime/atomic.hpp" 46 #include "runtime/prefetch.inline.hpp" 47 #include "runtime/thread.hpp" 48 #include "utilities/copy.hpp" 49 #include "utilities/globalDefinitions.hpp" 50 51 inline ShenandoahHeap* ShenandoahHeap::heap() { 52 assert(_heap != NULL, "Heap is not initialized yet"); 53 return _heap; 54 } 55 56 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() { 57 size_t new_index = Atomic::add(&_index, (size_t) 1); 58 // get_region() provides the bounds-check and returns NULL on OOB. 59 return _heap->get_region(new_index - 1); 60 } 61 62 inline bool ShenandoahHeap::has_forwarded_objects() const { 63 return _gc_state.is_set(HAS_FORWARDED); 64 } 65 66 inline WorkGang* ShenandoahHeap::workers() const { 67 return _workers; 68 } 69 70 inline WorkGang* ShenandoahHeap::get_safepoint_workers() { 71 return _safepoint_workers; 72 } 73 74 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const { 75 uintptr_t region_start = ((uintptr_t) addr); 76 uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift(); 77 assert(index < num_regions(), "Region index is in bounds: " PTR_FORMAT, p2i(addr)); 78 return index; 79 } 80 81 inline ShenandoahHeapRegion* const ShenandoahHeap::heap_region_containing(const void* addr) const { 82 size_t index = heap_region_index_containing(addr); 83 ShenandoahHeapRegion* const result = get_region(index); 84 assert(addr >= result->bottom() && addr < result->end(), "Heap region contains the address: " PTR_FORMAT, p2i(addr)); 85 return result; 86 } 87 88 template <class T> 89 inline oop ShenandoahHeap::update_with_forwarded_not_null(T* p, oop obj) { 90 if (in_collection_set(obj)) { 91 shenandoah_assert_forwarded_except(p, obj, is_full_gc_in_progress() || cancelled_gc() || is_degenerated_gc_in_progress()); 92 obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj); 93 RawAccess<IS_NOT_NULL>::oop_store(p, obj); 94 } 95 #ifdef ASSERT 96 else { 97 shenandoah_assert_not_forwarded(p, obj); 98 } 99 #endif 100 return obj; 101 } 102 103 template <class T> 104 inline oop ShenandoahHeap::maybe_update_with_forwarded(T* p) { 105 T o = RawAccess<>::oop_load(p); 106 if (!CompressedOops::is_null(o)) { 107 oop obj = CompressedOops::decode_not_null(o); 108 return maybe_update_with_forwarded_not_null(p, obj); 109 } else { 110 return NULL; 111 } 112 } 113 114 template <class T> 115 inline oop ShenandoahHeap::evac_update_with_forwarded(T* p) { 116 T o = RawAccess<>::oop_load(p); 117 if (!CompressedOops::is_null(o)) { 118 oop heap_oop = CompressedOops::decode_not_null(o); 119 if (in_collection_set(heap_oop)) { 120 oop forwarded_oop = ShenandoahBarrierSet::resolve_forwarded_not_null(heap_oop); 121 if (forwarded_oop == heap_oop) { 122 forwarded_oop = evacuate_object(heap_oop, Thread::current()); 123 } 124 oop prev = cas_oop(forwarded_oop, p, heap_oop); 125 if (prev == heap_oop) { 126 return forwarded_oop; 127 } else { 128 return NULL; 129 } 130 } 131 return heap_oop; 132 } else { 133 return NULL; 134 } 135 } 136 137 inline oop ShenandoahHeap::cas_oop(oop n, oop* addr, oop c) { 138 assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr)); 139 return (oop) Atomic::cmpxchg(addr, c, n); 140 } 141 142 inline oop ShenandoahHeap::cas_oop(oop n, narrowOop* addr, narrowOop c) { 143 assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr)); 144 narrowOop val = CompressedOops::encode(n); 145 return CompressedOops::decode((narrowOop) Atomic::cmpxchg(addr, c, val)); 146 } 147 148 inline oop ShenandoahHeap::cas_oop(oop n, narrowOop* addr, oop c) { 149 assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr)); 150 narrowOop cmp = CompressedOops::encode(c); 151 narrowOop val = CompressedOops::encode(n); 152 return CompressedOops::decode((narrowOop) Atomic::cmpxchg(addr, cmp, val)); 153 } 154 155 template <class T> 156 inline oop ShenandoahHeap::maybe_update_with_forwarded_not_null(T* p, oop heap_oop) { 157 shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || is_full_gc_in_progress() || is_degenerated_gc_in_progress()); 158 shenandoah_assert_correct(p, heap_oop); 159 160 if (in_collection_set(heap_oop)) { 161 oop forwarded_oop = ShenandoahBarrierSet::resolve_forwarded_not_null(heap_oop); 162 if (forwarded_oop == heap_oop) { 163 // E.g. during evacuation. 164 return forwarded_oop; 165 } 166 167 shenandoah_assert_forwarded_except(p, heap_oop, is_full_gc_in_progress() || is_degenerated_gc_in_progress()); 168 shenandoah_assert_not_forwarded(p, forwarded_oop); 169 shenandoah_assert_not_in_cset_except(p, forwarded_oop, cancelled_gc()); 170 171 // If this fails, another thread wrote to p before us, it will be logged in SATB and the 172 // reference be updated later. 173 oop witness = cas_oop(forwarded_oop, p, heap_oop); 174 175 if (witness != heap_oop) { 176 // CAS failed, someone had beat us to it. Normally, we would return the failure witness, 177 // because that would be the proper write of to-space object, enforced by strong barriers. 178 // However, there is a corner case with arraycopy. It can happen that a Java thread 179 // beats us with an arraycopy, which first copies the array, which potentially contains 180 // from-space refs, and only afterwards updates all from-space refs to to-space refs, 181 // which leaves a short window where the new array elements can be from-space. 182 // In this case, we can just resolve the result again. As we resolve, we need to consider 183 // the contended write might have been NULL. 184 oop result = ShenandoahBarrierSet::resolve_forwarded(witness); 185 shenandoah_assert_not_forwarded_except(p, result, (result == NULL)); 186 shenandoah_assert_not_in_cset_except(p, result, (result == NULL) || cancelled_gc()); 187 return result; 188 } else { 189 // Success! We have updated with known to-space copy. We have already asserted it is sane. 190 return forwarded_oop; 191 } 192 } else { 193 shenandoah_assert_not_forwarded(p, heap_oop); 194 return heap_oop; 195 } 196 } 197 198 inline bool ShenandoahHeap::cancelled_gc() const { 199 return _cancelled_gc.get() == CANCELLED; 200 } 201 202 inline bool ShenandoahHeap::check_cancelled_gc_and_yield(bool sts_active) { 203 if (! (sts_active && ShenandoahSuspendibleWorkers)) { 204 return cancelled_gc(); 205 } 206 207 jbyte prev = _cancelled_gc.cmpxchg(NOT_CANCELLED, CANCELLABLE); 208 if (prev == CANCELLABLE || prev == NOT_CANCELLED) { 209 if (SuspendibleThreadSet::should_yield()) { 210 SuspendibleThreadSet::yield(); 211 } 212 213 // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets 214 // to restore to CANCELLABLE. 215 if (prev == CANCELLABLE) { 216 _cancelled_gc.set(CANCELLABLE); 217 } 218 return false; 219 } else { 220 return true; 221 } 222 } 223 224 inline void ShenandoahHeap::clear_cancelled_gc() { 225 _cancelled_gc.set(CANCELLABLE); 226 _oom_evac_handler.clear(); 227 } 228 229 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) { 230 assert(UseTLAB, "TLABs should be enabled"); 231 232 PLAB* gclab = ShenandoahThreadLocalData::gclab(thread); 233 if (gclab == NULL) { 234 assert(!thread->is_Java_thread() && !thread->is_Worker_thread(), 235 "Performance: thread should have GCLAB: %s", thread->name()); 236 // No GCLABs in this thread, fallback to shared allocation 237 return NULL; 238 } 239 HeapWord* obj = gclab->allocate(size); 240 if (obj != NULL) { 241 return obj; 242 } 243 // Otherwise... 244 return allocate_from_gclab_slow(thread, size); 245 } 246 247 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) { 248 if (ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) { 249 // This thread went through the OOM during evac protocol and it is safe to return 250 // the forward pointer. It must not attempt to evacuate any more. 251 return ShenandoahBarrierSet::resolve_forwarded(p); 252 } 253 254 assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope"); 255 256 size_t size = p->size(); 257 258 assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects"); 259 260 bool alloc_from_gclab = true; 261 HeapWord* copy = NULL; 262 263 #ifdef ASSERT 264 if (ShenandoahOOMDuringEvacALot && 265 (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call 266 copy = NULL; 267 } else { 268 #endif 269 if (UseTLAB) { 270 copy = allocate_from_gclab(thread, size); 271 } 272 if (copy == NULL) { 273 ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size); 274 copy = allocate_memory(req); 275 alloc_from_gclab = false; 276 } 277 #ifdef ASSERT 278 } 279 #endif 280 281 if (copy == NULL) { 282 control_thread()->handle_alloc_failure_evac(size); 283 284 _oom_evac_handler.handle_out_of_memory_during_evacuation(); 285 286 return ShenandoahBarrierSet::resolve_forwarded(p); 287 } 288 289 // Copy the object: 290 Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size); 291 292 // Try to install the new forwarding pointer. 293 oop copy_val = oop(copy); 294 oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val); 295 if (result == copy_val) { 296 // Successfully evacuated. Our copy is now the public one! 297 shenandoah_assert_correct(NULL, copy_val); 298 return copy_val; 299 } else { 300 // Failed to evacuate. We need to deal with the object that is left behind. Since this 301 // new allocation is certainly after TAMS, it will be considered live in the next cycle. 302 // But if it happens to contain references to evacuated regions, those references would 303 // not get updated for this stale copy during this cycle, and we will crash while scanning 304 // it the next cycle. 305 // 306 // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next 307 // object will overwrite this stale copy, or the filler object on LAB retirement will 308 // do this. For non-GCLAB allocations, we have no way to retract the allocation, and 309 // have to explicitly overwrite the copy with the filler object. With that overwrite, 310 // we have to keep the fwdptr initialized and pointing to our (stale) copy. 311 if (alloc_from_gclab) { 312 ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size); 313 } else { 314 fill_with_object(copy, size); 315 shenandoah_assert_correct(NULL, copy_val); 316 } 317 shenandoah_assert_correct(NULL, result); 318 return result; 319 } 320 } 321 322 template<bool RESOLVE> 323 inline bool ShenandoahHeap::requires_marking(const void* entry) const { 324 oop obj = oop(entry); 325 if (RESOLVE) { 326 obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj); 327 } 328 return !_marking_context->is_marked(obj); 329 } 330 331 inline bool ShenandoahHeap::in_collection_set(oop p) const { 332 assert(collection_set() != NULL, "Sanity"); 333 return collection_set()->is_in(p); 334 } 335 336 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const { 337 assert(collection_set() != NULL, "Sanity"); 338 return collection_set()->is_in_loc(p); 339 } 340 341 inline bool ShenandoahHeap::is_stable() const { 342 return _gc_state.is_clear(); 343 } 344 345 inline bool ShenandoahHeap::is_idle() const { 346 return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS); 347 } 348 349 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const { 350 return _gc_state.is_set(MARKING); 351 } 352 353 inline bool ShenandoahHeap::is_evacuation_in_progress() const { 354 return _gc_state.is_set(EVACUATION); 355 } 356 357 inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const { 358 return _gc_state.is_set(mask); 359 } 360 361 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const { 362 return _degenerated_gc_in_progress.is_set(); 363 } 364 365 inline bool ShenandoahHeap::is_full_gc_in_progress() const { 366 return _full_gc_in_progress.is_set(); 367 } 368 369 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const { 370 return _full_gc_move_in_progress.is_set(); 371 } 372 373 inline bool ShenandoahHeap::is_update_refs_in_progress() const { 374 return _gc_state.is_set(UPDATEREFS); 375 } 376 377 inline bool ShenandoahHeap::is_stw_gc_in_progress() const { 378 return is_full_gc_in_progress() || is_degenerated_gc_in_progress(); 379 } 380 381 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const { 382 return _concurrent_strong_root_in_progress.is_set(); 383 } 384 385 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const { 386 return _concurrent_weak_root_in_progress.is_set(); 387 } 388 389 template<class T> 390 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) { 391 marked_object_iterate(region, cl, region->top()); 392 } 393 394 template<class T> 395 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) { 396 assert(! region->is_humongous_continuation(), "no humongous continuation regions here"); 397 398 ShenandoahMarkingContext* const ctx = complete_marking_context(); 399 assert(ctx->is_complete(), "sanity"); 400 401 MarkBitMap* mark_bit_map = ctx->mark_bit_map(); 402 HeapWord* tams = ctx->top_at_mark_start(region); 403 404 size_t skip_bitmap_delta = 1; 405 HeapWord* start = region->bottom(); 406 HeapWord* end = MIN2(tams, region->end()); 407 408 // Step 1. Scan below the TAMS based on bitmap data. 409 HeapWord* limit_bitmap = MIN2(limit, tams); 410 411 // Try to scan the initial candidate. If the candidate is above the TAMS, it would 412 // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2. 413 HeapWord* cb = mark_bit_map->get_next_marked_addr(start, end); 414 415 intx dist = ShenandoahMarkScanPrefetch; 416 if (dist > 0) { 417 // Batched scan that prefetches the oop data, anticipating the access to 418 // either header, oop field, or forwarding pointer. Not that we cannot 419 // touch anything in oop, while it still being prefetched to get enough 420 // time for prefetch to work. This is why we try to scan the bitmap linearly, 421 // disregarding the object size. However, since we know forwarding pointer 422 // preceeds the object, we can skip over it. Once we cannot trust the bitmap, 423 // there is no point for prefetching the oop contents, as oop->size() will 424 // touch it prematurely. 425 426 // No variable-length arrays in standard C++, have enough slots to fit 427 // the prefetch distance. 428 static const int SLOT_COUNT = 256; 429 guarantee(dist <= SLOT_COUNT, "adjust slot count"); 430 HeapWord* slots[SLOT_COUNT]; 431 432 int avail; 433 do { 434 avail = 0; 435 for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) { 436 Prefetch::read(cb, oopDesc::mark_offset_in_bytes()); 437 slots[avail++] = cb; 438 cb += skip_bitmap_delta; 439 if (cb < limit_bitmap) { 440 cb = mark_bit_map->get_next_marked_addr(cb, limit_bitmap); 441 } 442 } 443 444 for (int c = 0; c < avail; c++) { 445 assert (slots[c] < tams, "only objects below TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams)); 446 assert (slots[c] < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit)); 447 oop obj = oop(slots[c]); 448 assert(oopDesc::is_oop(obj), "sanity"); 449 assert(ctx->is_marked(obj), "object expected to be marked"); 450 cl->do_object(obj); 451 } 452 } while (avail > 0); 453 } else { 454 while (cb < limit_bitmap) { 455 assert (cb < tams, "only objects below TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams)); 456 assert (cb < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit)); 457 oop obj = oop(cb); 458 assert(oopDesc::is_oop(obj), "sanity"); 459 assert(ctx->is_marked(obj), "object expected to be marked"); 460 cl->do_object(obj); 461 cb += skip_bitmap_delta; 462 if (cb < limit_bitmap) { 463 cb = mark_bit_map->get_next_marked_addr(cb, limit_bitmap); 464 } 465 } 466 } 467 468 // Step 2. Accurate size-based traversal, happens past the TAMS. 469 // This restarts the scan at TAMS, which makes sure we traverse all objects, 470 // regardless of what happened at Step 1. 471 HeapWord* cs = tams; 472 while (cs < limit) { 473 assert (cs >= tams, "only objects past TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams)); 474 assert (cs < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit)); 475 oop obj = oop(cs); 476 assert(oopDesc::is_oop(obj), "sanity"); 477 assert(ctx->is_marked(obj), "object expected to be marked"); 478 int size = obj->size(); 479 cl->do_object(obj); 480 cs += size; 481 } 482 } 483 484 template <class T> 485 class ShenandoahObjectToOopClosure : public ObjectClosure { 486 T* _cl; 487 public: 488 ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {} 489 490 void do_object(oop obj) { 491 obj->oop_iterate(_cl); 492 } 493 }; 494 495 template <class T> 496 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure { 497 T* _cl; 498 MemRegion _bounds; 499 public: 500 ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) : 501 _cl(cl), _bounds(bottom, top) {} 502 503 void do_object(oop obj) { 504 obj->oop_iterate(_cl, _bounds); 505 } 506 }; 507 508 template<class T> 509 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) { 510 if (region->is_humongous()) { 511 HeapWord* bottom = region->bottom(); 512 if (top > bottom) { 513 region = region->humongous_start_region(); 514 ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top); 515 marked_object_iterate(region, &objs); 516 } 517 } else { 518 ShenandoahObjectToOopClosure<T> objs(cl); 519 marked_object_iterate(region, &objs, top); 520 } 521 } 522 523 inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const { 524 if (region_idx < _num_regions) { 525 return _regions[region_idx]; 526 } else { 527 return NULL; 528 } 529 } 530 531 inline void ShenandoahHeap::mark_complete_marking_context() { 532 _marking_context->mark_complete(); 533 } 534 535 inline void ShenandoahHeap::mark_incomplete_marking_context() { 536 _marking_context->mark_incomplete(); 537 } 538 539 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const { 540 assert (_marking_context->is_complete()," sanity"); 541 return _marking_context; 542 } 543 544 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const { 545 return _marking_context; 546 } 547 548 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP