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