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 "gc/shenandoah/shenandoahHeap.hpp" 29 30 #include "classfile/javaClasses.inline.hpp" 31 #include "gc/shared/markBitMap.inline.hpp" 32 #include "gc/shared/threadLocalAllocBuffer.inline.hpp" 33 #include "gc/shared/suspendibleThreadSet.hpp" 34 #include "gc/shared/tlab_globals.hpp" 35 #include "gc/shenandoah/shenandoahAsserts.hpp" 36 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp" 37 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp" 38 #include "gc/shenandoah/shenandoahForwarding.inline.hpp" 39 #include "gc/shenandoah/shenandoahWorkGroup.hpp" 40 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp" 41 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 42 #include "gc/shenandoah/shenandoahControlThread.hpp" 43 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp" 44 #include "gc/shenandoah/shenandoahObjectUtils.inline.hpp" 45 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 46 #include "oops/compressedOops.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "runtime/atomic.hpp" 49 #include "runtime/prefetch.inline.hpp" 50 #include "runtime/thread.hpp" 51 #include "utilities/copy.hpp" 52 #include "utilities/globalDefinitions.hpp" 53 54 inline ShenandoahHeap* ShenandoahHeap::heap() { 55 return named_heap<ShenandoahHeap>(CollectedHeap::Shenandoah); 56 } 57 58 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() { 59 size_t new_index = Atomic::add(&_index, (size_t) 1, memory_order_relaxed); 60 // get_region() provides the bounds-check and returns NULL on OOB. 61 return _heap->get_region(new_index - 1); 62 } 63 64 inline bool ShenandoahHeap::has_forwarded_objects() const { 65 return _gc_state.is_set(HAS_FORWARDED); 66 } 67 68 inline WorkGang* ShenandoahHeap::workers() const { 69 return _workers; 70 } 71 72 inline WorkGang* ShenandoahHeap::safepoint_workers() { 73 return _safepoint_workers; 74 } 75 76 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const { 77 uintptr_t region_start = ((uintptr_t) addr); 78 uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift(); 79 assert(index < num_regions(), "Region index is in bounds: " PTR_FORMAT, p2i(addr)); 80 return index; 81 } 82 83 inline ShenandoahHeapRegion* const ShenandoahHeap::heap_region_containing(const void* addr) const { 84 size_t index = heap_region_index_containing(addr); 85 ShenandoahHeapRegion* const result = get_region(index); 86 assert(addr >= result->bottom() && addr < result->end(), "Heap region contains the address: " PTR_FORMAT, p2i(addr)); 87 return result; 88 } 89 90 inline void ShenandoahHeap::enter_evacuation(Thread* t) { 91 _oom_evac_handler.enter_evacuation(t); 92 } 93 94 inline void ShenandoahHeap::leave_evacuation(Thread* t) { 95 _oom_evac_handler.leave_evacuation(t); 96 } 97 98 template <class T> 99 inline void ShenandoahHeap::update_with_forwarded(T* p) { 100 T o = RawAccess<>::oop_load(p); 101 if (!CompressedOops::is_null(o)) { 102 oop obj = CompressedOops::decode_not_null(o); 103 if (in_collection_set(obj)) { 104 // Corner case: when evacuation fails, there are objects in collection 105 // set that are not really forwarded. We can still go and try and update them 106 // (uselessly) to simplify the common path. 107 shenandoah_assert_forwarded_except(p, obj, cancelled_gc()); 108 oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj); 109 shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc()); 110 111 // Unconditionally store the update: no concurrent updates expected. 112 RawAccess<IS_NOT_NULL>::oop_store(p, fwd); 113 } 114 } 115 } 116 117 template <class T> 118 inline void ShenandoahHeap::conc_update_with_forwarded(T* p) { 119 T o = RawAccess<>::oop_load(p); 120 if (!CompressedOops::is_null(o)) { 121 oop obj = CompressedOops::decode_not_null(o); 122 if (in_collection_set(obj)) { 123 // Corner case: when evacuation fails, there are objects in collection 124 // set that are not really forwarded. We can still go and try CAS-update them 125 // (uselessly) to simplify the common path. 126 shenandoah_assert_forwarded_except(p, obj, cancelled_gc()); 127 oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj); 128 shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc()); 129 130 // Sanity check: we should not be updating the cset regions themselves, 131 // unless we are recovering from the evacuation failure. 132 shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || cancelled_gc()); 133 134 // Either we succeed in updating the reference, or something else gets in our way. 135 // We don't care if that is another concurrent GC update, or another mutator update. 136 // We only check that non-NULL store still updated with non-forwarded reference. 137 oop witness = cas_oop(fwd, p, obj); 138 shenandoah_assert_not_forwarded_except(p, witness, (witness == NULL) || (witness == obj)); 139 } 140 } 141 } 142 143 inline oop ShenandoahHeap::cas_oop(oop n, oop* addr, oop c) { 144 assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr)); 145 return (oop) Atomic::cmpxchg(addr, c, n); 146 } 147 148 inline oop ShenandoahHeap::cas_oop(oop n, narrowOop* addr, narrowOop c) { 149 assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr)); 150 narrowOop val = CompressedOops::encode(n); 151 return CompressedOops::decode(Atomic::cmpxchg(addr, c, val)); 152 } 153 154 inline oop ShenandoahHeap::cas_oop(oop n, narrowOop* addr, oop c) { 155 assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr)); 156 narrowOop cmp = CompressedOops::encode(c); 157 narrowOop val = CompressedOops::encode(n); 158 return CompressedOops::decode(Atomic::cmpxchg(addr, cmp, val)); 159 } 160 161 inline bool ShenandoahHeap::cancelled_gc() const { 162 return _cancelled_gc.get() == CANCELLED; 163 } 164 165 inline bool ShenandoahHeap::check_cancelled_gc_and_yield(bool sts_active) { 166 if (! (sts_active && ShenandoahSuspendibleWorkers)) { 167 return cancelled_gc(); 168 } 169 170 jbyte prev = _cancelled_gc.cmpxchg(NOT_CANCELLED, CANCELLABLE); 171 if (prev == CANCELLABLE || prev == NOT_CANCELLED) { 172 if (SuspendibleThreadSet::should_yield()) { 173 SuspendibleThreadSet::yield(); 174 } 175 176 // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets 177 // to restore to CANCELLABLE. 178 if (prev == CANCELLABLE) { 179 _cancelled_gc.set(CANCELLABLE); 180 } 181 return false; 182 } else { 183 return true; 184 } 185 } 186 187 inline void ShenandoahHeap::clear_cancelled_gc() { 188 _cancelled_gc.set(CANCELLABLE); 189 _oom_evac_handler.clear(); 190 } 191 192 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) { 193 assert(UseTLAB, "TLABs should be enabled"); 194 195 PLAB* gclab = ShenandoahThreadLocalData::gclab(thread); 196 if (gclab == NULL) { 197 assert(!thread->is_Java_thread() && !thread->is_Worker_thread(), 198 "Performance: thread should have GCLAB: %s", thread->name()); 199 // No GCLABs in this thread, fallback to shared allocation 200 return NULL; 201 } 202 HeapWord* obj = gclab->allocate(size); 203 if (obj != NULL) { 204 return obj; 205 } 206 // Otherwise... 207 return allocate_from_gclab_slow(thread, size); 208 } 209 210 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) { 211 if (ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) { 212 // This thread went through the OOM during evac protocol and it is safe to return 213 // the forward pointer. It must not attempt to evacuate any more. 214 return ShenandoahBarrierSet::resolve_forwarded(p); 215 } 216 217 assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope"); 218 219 size_t size = ShenandoahObjectUtils::size(p); 220 221 assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects"); 222 223 bool alloc_from_gclab = true; 224 HeapWord* copy = NULL; 225 226 #ifdef ASSERT 227 if (ShenandoahOOMDuringEvacALot && 228 (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call 229 copy = NULL; 230 } else { 231 #endif 232 if (UseTLAB) { 233 copy = allocate_from_gclab(thread, size); 234 } 235 if (copy == NULL) { 236 ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size); 237 copy = allocate_memory(req); 238 alloc_from_gclab = false; 239 } 240 #ifdef ASSERT 241 } 242 #endif 243 244 if (copy == NULL) { 245 control_thread()->handle_alloc_failure_evac(size); 246 247 _oom_evac_handler.handle_out_of_memory_during_evacuation(); 248 249 return ShenandoahBarrierSet::resolve_forwarded(p); 250 } 251 252 // Copy the object: 253 Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size); 254 255 // Try to install the new forwarding pointer. 256 oop copy_val = cast_to_oop(copy); 257 oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val); 258 if (result == copy_val) { 259 // Successfully evacuated. Our copy is now the public one! 260 shenandoah_assert_correct(NULL, copy_val); 261 return copy_val; 262 } else { 263 // Failed to evacuate. We need to deal with the object that is left behind. Since this 264 // new allocation is certainly after TAMS, it will be considered live in the next cycle. 265 // But if it happens to contain references to evacuated regions, those references would 266 // not get updated for this stale copy during this cycle, and we will crash while scanning 267 // it the next cycle. 268 // 269 // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next 270 // object will overwrite this stale copy, or the filler object on LAB retirement will 271 // do this. For non-GCLAB allocations, we have no way to retract the allocation, and 272 // have to explicitly overwrite the copy with the filler object. With that overwrite, 273 // we have to keep the fwdptr initialized and pointing to our (stale) copy. 274 if (alloc_from_gclab) { 275 ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size); 276 } else { 277 fill_with_object(copy, size); 278 shenandoah_assert_correct(NULL, copy_val); 279 } 280 shenandoah_assert_correct(NULL, result); 281 return result; 282 } 283 } 284 285 inline bool ShenandoahHeap::requires_marking(const void* entry) const { 286 oop obj = cast_to_oop(entry); 287 return !_marking_context->is_marked_strong(obj); 288 } 289 290 inline bool ShenandoahHeap::in_collection_set(oop p) const { 291 assert(collection_set() != NULL, "Sanity"); 292 return collection_set()->is_in(p); 293 } 294 295 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const { 296 assert(collection_set() != NULL, "Sanity"); 297 return collection_set()->is_in_loc(p); 298 } 299 300 inline bool ShenandoahHeap::is_stable() const { 301 return _gc_state.is_clear(); 302 } 303 304 inline bool ShenandoahHeap::is_idle() const { 305 return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS); 306 } 307 308 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const { 309 return _gc_state.is_set(MARKING); 310 } 311 312 inline bool ShenandoahHeap::is_evacuation_in_progress() const { 313 return _gc_state.is_set(EVACUATION); 314 } 315 316 inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const { 317 return _gc_state.is_set(mask); 318 } 319 320 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const { 321 return _degenerated_gc_in_progress.is_set(); 322 } 323 324 inline bool ShenandoahHeap::is_full_gc_in_progress() const { 325 return _full_gc_in_progress.is_set(); 326 } 327 328 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const { 329 return _full_gc_move_in_progress.is_set(); 330 } 331 332 inline bool ShenandoahHeap::is_update_refs_in_progress() const { 333 return _gc_state.is_set(UPDATEREFS); 334 } 335 336 inline bool ShenandoahHeap::is_stw_gc_in_progress() const { 337 return is_full_gc_in_progress() || is_degenerated_gc_in_progress(); 338 } 339 340 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const { 341 return _concurrent_strong_root_in_progress.is_set(); 342 } 343 344 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const { 345 return _gc_state.is_set(WEAK_ROOTS); 346 } 347 348 template<class T> 349 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) { 350 marked_object_iterate(region, cl, region->top()); 351 } 352 353 template<class T> 354 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) { 355 assert(! region->is_humongous_continuation(), "no humongous continuation regions here"); 356 357 ShenandoahMarkingContext* const ctx = complete_marking_context(); 358 assert(ctx->is_complete(), "sanity"); 359 360 HeapWord* tams = ctx->top_at_mark_start(region); 361 362 size_t skip_bitmap_delta = 1; 363 HeapWord* start = region->bottom(); 364 HeapWord* end = MIN2(tams, region->end()); 365 366 // Step 1. Scan below the TAMS based on bitmap data. 367 HeapWord* limit_bitmap = MIN2(limit, tams); 368 369 // Try to scan the initial candidate. If the candidate is above the TAMS, it would 370 // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2. 371 HeapWord* cb = ctx->get_next_marked_addr(start, end); 372 373 intx dist = ShenandoahMarkScanPrefetch; 374 if (dist > 0) { 375 // Batched scan that prefetches the oop data, anticipating the access to 376 // either header, oop field, or forwarding pointer. Not that we cannot 377 // touch anything in oop, while it still being prefetched to get enough 378 // time for prefetch to work. This is why we try to scan the bitmap linearly, 379 // disregarding the object size. However, since we know forwarding pointer 380 // preceeds the object, we can skip over it. Once we cannot trust the bitmap, 381 // there is no point for prefetching the oop contents, as oop->size() will 382 // touch it prematurely. 383 384 // No variable-length arrays in standard C++, have enough slots to fit 385 // the prefetch distance. 386 static const int SLOT_COUNT = 256; 387 guarantee(dist <= SLOT_COUNT, "adjust slot count"); 388 HeapWord* slots[SLOT_COUNT]; 389 390 int avail; 391 do { 392 avail = 0; 393 for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) { 394 Prefetch::read(cb, oopDesc::mark_offset_in_bytes()); 395 slots[avail++] = cb; 396 cb += skip_bitmap_delta; 397 if (cb < limit_bitmap) { 398 cb = ctx->get_next_marked_addr(cb, limit_bitmap); 399 } 400 } 401 402 for (int c = 0; c < avail; c++) { 403 assert (slots[c] < tams, "only objects below TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams)); 404 assert (slots[c] < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit)); 405 oop obj = cast_to_oop(slots[c]); 406 assert(oopDesc::is_oop(obj), "sanity"); 407 assert(ctx->is_marked(obj), "object expected to be marked"); 408 cl->do_object(obj); 409 } 410 } while (avail > 0); 411 } else { 412 while (cb < limit_bitmap) { 413 assert (cb < tams, "only objects below TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams)); 414 assert (cb < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit)); 415 oop obj = cast_to_oop(cb); 416 assert(oopDesc::is_oop(obj), "sanity"); 417 assert(ctx->is_marked(obj), "object expected to be marked"); 418 cl->do_object(obj); 419 cb += skip_bitmap_delta; 420 if (cb < limit_bitmap) { 421 cb = ctx->get_next_marked_addr(cb, limit_bitmap); 422 } 423 } 424 } 425 426 // Step 2. Accurate size-based traversal, happens past the TAMS. 427 // This restarts the scan at TAMS, which makes sure we traverse all objects, 428 // regardless of what happened at Step 1. 429 HeapWord* cs = tams; 430 while (cs < limit) { 431 assert (cs >= tams, "only objects past TAMS here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams)); 432 assert (cs < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit)); 433 oop obj = cast_to_oop(cs); 434 assert(oopDesc::is_oop(obj), "sanity"); 435 assert(ctx->is_marked(obj), "object expected to be marked"); 436 size_t size = ShenandoahObjectUtils::size(obj); 437 cl->do_object(obj); 438 cs += size; 439 } 440 } 441 442 template <class T> 443 class ShenandoahObjectToOopClosure : public ObjectClosure { 444 T* _cl; 445 public: 446 ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {} 447 448 void do_object(oop obj) { 449 obj->oop_iterate(_cl); 450 } 451 }; 452 453 template <class T> 454 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure { 455 T* _cl; 456 MemRegion _bounds; 457 public: 458 ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) : 459 _cl(cl), _bounds(bottom, top) {} 460 461 void do_object(oop obj) { 462 obj->oop_iterate(_cl, _bounds); 463 } 464 }; 465 466 template<class T> 467 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) { 468 if (region->is_humongous()) { 469 HeapWord* bottom = region->bottom(); 470 if (top > bottom) { 471 region = region->humongous_start_region(); 472 ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top); 473 marked_object_iterate(region, &objs); 474 } 475 } else { 476 ShenandoahObjectToOopClosure<T> objs(cl); 477 marked_object_iterate(region, &objs, top); 478 } 479 } 480 481 inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const { 482 if (region_idx < _num_regions) { 483 return _regions[region_idx]; 484 } else { 485 return NULL; 486 } 487 } 488 489 inline void ShenandoahHeap::mark_complete_marking_context() { 490 _marking_context->mark_complete(); 491 } 492 493 inline void ShenandoahHeap::mark_incomplete_marking_context() { 494 _marking_context->mark_incomplete(); 495 } 496 497 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const { 498 assert (_marking_context->is_complete()," sanity"); 499 return _marking_context; 500 } 501 502 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const { 503 return _marking_context; 504 } 505 506 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP