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