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