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