1 /*
   2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. 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 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "gc/shared/allocTracer.hpp"
  28 #include "gc/shared/barrierSet.inline.hpp"
  29 #include "gc/shared/collectedHeap.hpp"
  30 #include "gc/shared/collectedHeap.inline.hpp"
  31 #include "gc/shared/gcHeapSummary.hpp"
  32 #include "gc/shared/gcTrace.hpp"
  33 #include "gc/shared/gcTraceTime.inline.hpp"
  34 #include "gc/shared/gcWhen.hpp"
  35 #include "gc/shared/vmGCOperations.hpp"
  36 #include "logging/log.hpp"
  37 #include "memory/metaspace.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/instanceMirrorKlass.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "runtime/init.hpp"
  42 #include "runtime/thread.inline.hpp"
  43 #include "services/heapDumper.hpp"
  44 
  45 
  46 #ifdef ASSERT
  47 int CollectedHeap::_fire_out_of_memory_count = 0;
  48 #endif
  49 
  50 size_t CollectedHeap::_filler_array_max_size = 0;
  51 
  52 template <>
  53 void EventLogBase<GCMessage>::print(outputStream* st, GCMessage& m) {
  54   st->print_cr("GC heap %s", m.is_before ? "before" : "after");
  55   st->print_raw(m);
  56 }
  57 
  58 void GCHeapLog::log_heap(CollectedHeap* heap, bool before) {
  59   if (!should_log()) {
  60     return;
  61   }
  62 
  63   double timestamp = fetch_timestamp();
  64   MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
  65   int index = compute_log_index();
  66   _records[index].thread = NULL; // Its the GC thread so it's not that interesting.
  67   _records[index].timestamp = timestamp;
  68   _records[index].data.is_before = before;
  69   stringStream st(_records[index].data.buffer(), _records[index].data.size());
  70 
  71   st.print_cr("{Heap %s GC invocations=%u (full %u):",
  72                  before ? "before" : "after",
  73                  heap->total_collections(),
  74                  heap->total_full_collections());
  75 
  76   heap->print_on(&st);
  77   st.print_cr("}");
  78 }
  79 
  80 VirtualSpaceSummary CollectedHeap::create_heap_space_summary() {
  81   size_t capacity_in_words = capacity() / HeapWordSize;
  82 
  83   return VirtualSpaceSummary(
  84     reserved_region().start(), reserved_region().start() + capacity_in_words, reserved_region().end());
  85 }
  86 
  87 GCHeapSummary CollectedHeap::create_heap_summary() {
  88   VirtualSpaceSummary heap_space = create_heap_space_summary();
  89   return GCHeapSummary(heap_space, used());
  90 }
  91 
  92 MetaspaceSummary CollectedHeap::create_metaspace_summary() {
  93   const MetaspaceSizes meta_space(
  94       MetaspaceAux::committed_bytes(),
  95       MetaspaceAux::used_bytes(),
  96       MetaspaceAux::reserved_bytes());
  97   const MetaspaceSizes data_space(
  98       MetaspaceAux::committed_bytes(Metaspace::NonClassType),
  99       MetaspaceAux::used_bytes(Metaspace::NonClassType),
 100       MetaspaceAux::reserved_bytes(Metaspace::NonClassType));
 101   const MetaspaceSizes class_space(
 102       MetaspaceAux::committed_bytes(Metaspace::ClassType),
 103       MetaspaceAux::used_bytes(Metaspace::ClassType),
 104       MetaspaceAux::reserved_bytes(Metaspace::ClassType));
 105 
 106   const MetaspaceChunkFreeListSummary& ms_chunk_free_list_summary =
 107     MetaspaceAux::chunk_free_list_summary(Metaspace::NonClassType);
 108   const MetaspaceChunkFreeListSummary& class_chunk_free_list_summary =
 109     MetaspaceAux::chunk_free_list_summary(Metaspace::ClassType);
 110 
 111   return MetaspaceSummary(MetaspaceGC::capacity_until_GC(), meta_space, data_space, class_space,
 112                           ms_chunk_free_list_summary, class_chunk_free_list_summary);
 113 }
 114 
 115 void CollectedHeap::print_heap_before_gc() {
 116   Universe::print_heap_before_gc();
 117   if (_gc_heap_log != NULL) {
 118     _gc_heap_log->log_heap_before(this);
 119   }
 120 }
 121 
 122 void CollectedHeap::print_heap_after_gc() {
 123   Universe::print_heap_after_gc();
 124   if (_gc_heap_log != NULL) {
 125     _gc_heap_log->log_heap_after(this);
 126   }
 127 }
 128 
 129 void CollectedHeap::print_on_error(outputStream* st) const {
 130   st->print_cr("Heap:");
 131   print_extended_on(st);
 132   st->cr();
 133 
 134   _barrier_set->print_on(st);
 135 }
 136 
 137 void CollectedHeap::register_nmethod(nmethod* nm) {
 138   assert_locked_or_safepoint(CodeCache_lock);
 139 }
 140 
 141 void CollectedHeap::unregister_nmethod(nmethod* nm) {
 142   assert_locked_or_safepoint(CodeCache_lock);
 143 }
 144 
 145 void CollectedHeap::trace_heap(GCWhen::Type when, const GCTracer* gc_tracer) {
 146   const GCHeapSummary& heap_summary = create_heap_summary();
 147   gc_tracer->report_gc_heap_summary(when, heap_summary);
 148 
 149   const MetaspaceSummary& metaspace_summary = create_metaspace_summary();
 150   gc_tracer->report_metaspace_summary(when, metaspace_summary);
 151 }
 152 
 153 void CollectedHeap::trace_heap_before_gc(const GCTracer* gc_tracer) {
 154   trace_heap(GCWhen::BeforeGC, gc_tracer);
 155 }
 156 
 157 void CollectedHeap::trace_heap_after_gc(const GCTracer* gc_tracer) {
 158   trace_heap(GCWhen::AfterGC, gc_tracer);
 159 }
 160 
 161 // Memory state functions.
 162 
 163 
 164 CollectedHeap::CollectedHeap() :
 165   _barrier_set(NULL),
 166   _is_gc_active(false),
 167   _total_collections(0),
 168   _total_full_collections(0),
 169   _gc_cause(GCCause::_no_gc),
 170   _gc_lastcause(GCCause::_no_gc),
 171   _defer_initial_card_mark(false) // strengthened by subclass in pre_initialize() below.
 172 {
 173   const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
 174   const size_t elements_per_word = HeapWordSize / sizeof(jint);
 175   _filler_array_max_size = align_object_size(filler_array_hdr_size() +
 176                                              max_len / elements_per_word);
 177 
 178   NOT_PRODUCT(_promotion_failure_alot_count = 0;)
 179   NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
 180 
 181   if (UsePerfData) {
 182     EXCEPTION_MARK;
 183 
 184     // create the gc cause jvmstat counters
 185     _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
 186                              80, GCCause::to_string(_gc_cause), CHECK);
 187 
 188     _perf_gc_lastcause =
 189                 PerfDataManager::create_string_variable(SUN_GC, "lastCause",
 190                              80, GCCause::to_string(_gc_lastcause), CHECK);
 191   }
 192 
 193   // Create the ring log
 194   if (LogEvents) {
 195     _gc_heap_log = new GCHeapLog();
 196   } else {
 197     _gc_heap_log = NULL;
 198   }
 199 }
 200 
 201 // This interface assumes that it's being called by the
 202 // vm thread. It collects the heap assuming that the
 203 // heap lock is already held and that we are executing in
 204 // the context of the vm thread.
 205 void CollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
 206   assert(Thread::current()->is_VM_thread(), "Precondition#1");
 207   assert(Heap_lock->is_locked(), "Precondition#2");
 208   GCCauseSetter gcs(this, cause);
 209   switch (cause) {
 210     case GCCause::_heap_inspection:
 211     case GCCause::_heap_dump:
 212     case GCCause::_metadata_GC_threshold : {
 213       HandleMark hm;
 214       do_full_collection(false);        // don't clear all soft refs
 215       break;
 216     }
 217     case GCCause::_last_ditch_collection: {
 218       HandleMark hm;
 219       do_full_collection(true);         // do clear all soft refs
 220       break;
 221     }
 222     default:
 223       ShouldNotReachHere(); // Unexpected use of this function
 224   }
 225 }
 226 
 227 void CollectedHeap::set_barrier_set(BarrierSet* barrier_set) {
 228   _barrier_set = barrier_set;
 229   oopDesc::set_bs(_barrier_set);
 230 }
 231 
 232 void CollectedHeap::pre_initialize() {
 233   // Used for ReduceInitialCardMarks (when COMPILER2 is used);
 234   // otherwise remains unused.
 235 #if defined(COMPILER2) || INCLUDE_JVMCI
 236   _defer_initial_card_mark =    ReduceInitialCardMarks && can_elide_tlab_store_barriers()
 237                              && (DeferInitialCardMark || card_mark_must_follow_store());
 238 #else
 239   assert(_defer_initial_card_mark == false, "Who would set it?");
 240 #endif
 241 }
 242 
 243 #ifndef PRODUCT
 244 void CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) {
 245   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 246     for (size_t slot = 0; slot < size; slot += 1) {
 247       assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal),
 248              "Found badHeapWordValue in post-allocation check");
 249     }
 250   }
 251 }
 252 
 253 void CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) {
 254   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 255     for (size_t slot = 0; slot < size; slot += 1) {
 256       assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal),
 257              "Found non badHeapWordValue in pre-allocation check");
 258     }
 259   }
 260 }
 261 #endif // PRODUCT
 262 
 263 #ifdef ASSERT
 264 void CollectedHeap::check_for_valid_allocation_state() {
 265   Thread *thread = Thread::current();
 266   // How to choose between a pending exception and a potential
 267   // OutOfMemoryError?  Don't allow pending exceptions.
 268   // This is a VM policy failure, so how do we exhaustively test it?
 269   assert(!thread->has_pending_exception(),
 270          "shouldn't be allocating with pending exception");
 271   if (StrictSafepointChecks) {
 272     assert(thread->allow_allocation(),
 273            "Allocation done by thread for which allocation is blocked "
 274            "by No_Allocation_Verifier!");
 275     // Allocation of an oop can always invoke a safepoint,
 276     // hence, the true argument
 277     thread->check_for_valid_safepoint_state(true);
 278   }
 279 }
 280 #endif
 281 
 282 HeapWord* CollectedHeap::allocate_from_tlab_slow(KlassHandle klass, Thread* thread, size_t size) {
 283 
 284   // Retain tlab and allocate object in shared space if
 285   // the amount free in the tlab is too large to discard.
 286   if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
 287     thread->tlab().record_slow_allocation(size);
 288     return NULL;
 289   }
 290 
 291   // Discard tlab and allocate a new one.
 292   // To minimize fragmentation, the last TLAB may be smaller than the rest.
 293   size_t new_tlab_size = thread->tlab().compute_size(size);
 294 
 295   thread->tlab().clear_before_allocation();
 296 
 297   if (new_tlab_size == 0) {
 298     return NULL;
 299   }
 300 
 301   // Allocate a new TLAB...
 302   HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
 303   if (obj == NULL) {
 304     return NULL;
 305   }
 306 
 307   AllocTracer::send_allocation_in_new_tlab_event(klass, new_tlab_size * HeapWordSize, size * HeapWordSize);
 308 
 309   if (ZeroTLAB) {
 310     // ..and clear it.
 311     Copy::zero_to_words(obj, new_tlab_size);
 312   } else {
 313     // ...and zap just allocated object.
 314 #ifdef ASSERT
 315     // Skip mangling the space corresponding to the object header to
 316     // ensure that the returned space is not considered parsable by
 317     // any concurrent GC thread.
 318     size_t hdr_size = oopDesc::header_size();
 319     Copy::fill_to_words(obj + hdr_size, new_tlab_size - hdr_size, badHeapWordVal);
 320 #endif // ASSERT
 321   }
 322   thread->tlab().fill(obj, obj + size, new_tlab_size);
 323   return obj;
 324 }
 325 
 326 void CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
 327   MemRegion deferred = thread->deferred_card_mark();
 328   if (!deferred.is_empty()) {
 329     assert(_defer_initial_card_mark, "Otherwise should be empty");
 330     {
 331       // Verify that the storage points to a parsable object in heap
 332       DEBUG_ONLY(oop old_obj = oop(deferred.start());)
 333       assert(is_in(old_obj), "Not in allocated heap");
 334       assert(!can_elide_initializing_store_barrier(old_obj),
 335              "Else should have been filtered in new_store_pre_barrier()");
 336       assert(old_obj->is_oop(true), "Not an oop");
 337       assert(deferred.word_size() == (size_t)(old_obj->size()),
 338              "Mismatch: multiple objects?");
 339     }
 340     BarrierSet* bs = barrier_set();
 341     assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
 342     bs->write_region(deferred);
 343     // "Clear" the deferred_card_mark field
 344     thread->set_deferred_card_mark(MemRegion());
 345   }
 346   assert(thread->deferred_card_mark().is_empty(), "invariant");
 347 }
 348 
 349 size_t CollectedHeap::max_tlab_size() const {
 350   // TLABs can't be bigger than we can fill with a int[Integer.MAX_VALUE].
 351   // This restriction could be removed by enabling filling with multiple arrays.
 352   // If we compute that the reasonable way as
 353   //    header_size + ((sizeof(jint) * max_jint) / HeapWordSize)
 354   // we'll overflow on the multiply, so we do the divide first.
 355   // We actually lose a little by dividing first,
 356   // but that just makes the TLAB  somewhat smaller than the biggest array,
 357   // which is fine, since we'll be able to fill that.
 358   size_t max_int_size = typeArrayOopDesc::header_size(T_INT) +
 359               sizeof(jint) *
 360               ((juint) max_jint / (size_t) HeapWordSize);
 361   return align_size_down(max_int_size, MinObjAlignment);
 362 }
 363 
 364 // Helper for ReduceInitialCardMarks. For performance,
 365 // compiled code may elide card-marks for initializing stores
 366 // to a newly allocated object along the fast-path. We
 367 // compensate for such elided card-marks as follows:
 368 // (a) Generational, non-concurrent collectors, such as
 369 //     GenCollectedHeap(ParNew,DefNew,Tenured) and
 370 //     ParallelScavengeHeap(ParallelGC, ParallelOldGC)
 371 //     need the card-mark if and only if the region is
 372 //     in the old gen, and do not care if the card-mark
 373 //     succeeds or precedes the initializing stores themselves,
 374 //     so long as the card-mark is completed before the next
 375 //     scavenge. For all these cases, we can do a card mark
 376 //     at the point at which we do a slow path allocation
 377 //     in the old gen, i.e. in this call.
 378 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
 379 //     in addition that the card-mark for an old gen allocated
 380 //     object strictly follow any associated initializing stores.
 381 //     In these cases, the memRegion remembered below is
 382 //     used to card-mark the entire region either just before the next
 383 //     slow-path allocation by this thread or just before the next scavenge or
 384 //     CMS-associated safepoint, whichever of these events happens first.
 385 //     (The implicit assumption is that the object has been fully
 386 //     initialized by this point, a fact that we assert when doing the
 387 //     card-mark.)
 388 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
 389 //     G1 concurrent marking is in progress an SATB (pre-write-)barrier is
 390 //     is used to remember the pre-value of any store. Initializing
 391 //     stores will not need this barrier, so we need not worry about
 392 //     compensating for the missing pre-barrier here. Turning now
 393 //     to the post-barrier, we note that G1 needs a RS update barrier
 394 //     which simply enqueues a (sequence of) dirty cards which may
 395 //     optionally be refined by the concurrent update threads. Note
 396 //     that this barrier need only be applied to a non-young write,
 397 //     but, like in CMS, because of the presence of concurrent refinement
 398 //     (much like CMS' precleaning), must strictly follow the oop-store.
 399 //     Thus, using the same protocol for maintaining the intended
 400 //     invariants turns out, serendepitously, to be the same for both
 401 //     G1 and CMS.
 402 //
 403 // For any future collector, this code should be reexamined with
 404 // that specific collector in mind, and the documentation above suitably
 405 // extended and updated.
 406 oop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
 407   // If a previous card-mark was deferred, flush it now.
 408   flush_deferred_store_barrier(thread);
 409   if (can_elide_initializing_store_barrier(new_obj) ||
 410       new_obj->is_typeArray()) {
 411     // Arrays of non-references don't need a pre-barrier.
 412     // The deferred_card_mark region should be empty
 413     // following the flush above.
 414     assert(thread->deferred_card_mark().is_empty(), "Error");
 415   } else {
 416     MemRegion mr((HeapWord*)new_obj, new_obj->size());
 417     assert(!mr.is_empty(), "Error");
 418     if (_defer_initial_card_mark) {
 419       // Defer the card mark
 420       thread->set_deferred_card_mark(mr);
 421     } else {
 422       // Do the card mark
 423       BarrierSet* bs = barrier_set();
 424       assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
 425       bs->write_region(mr);
 426     }
 427   }
 428   return new_obj;
 429 }
 430 
 431 size_t CollectedHeap::filler_array_hdr_size() {
 432   return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long
 433 }
 434 
 435 size_t CollectedHeap::filler_array_min_size() {
 436   return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
 437 }
 438 
 439 #ifdef ASSERT
 440 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
 441 {
 442   assert(words >= min_fill_size(), "too small to fill");
 443   assert(words % MinObjAlignment == 0, "unaligned size");
 444   assert(Universe::heap()->is_in_reserved(start), "not in heap");
 445   assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
 446 }
 447 
 448 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
 449 {
 450   if (ZapFillerObjects && zap) {
 451     Copy::fill_to_words(start + filler_array_hdr_size(),
 452                         words - filler_array_hdr_size(), 0XDEAFBABE);
 453   }
 454 }
 455 #endif // ASSERT
 456 
 457 void
 458 CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
 459 {
 460   assert(words >= filler_array_min_size(), "too small for an array");
 461   assert(words <= filler_array_max_size(), "too big for a single object");
 462 
 463   const size_t payload_size = words - filler_array_hdr_size();
 464   const size_t len = payload_size * HeapWordSize / sizeof(jint);
 465   assert((int)len >= 0, "size too large " SIZE_FORMAT " becomes %d", words, (int)len);
 466 
 467   // Set the length first for concurrent GC.
 468   ((arrayOop)start)->set_length((int)len);
 469   post_allocation_setup_common(Universe::intArrayKlassObj(), start);
 470   DEBUG_ONLY(zap_filler_array(start, words, zap);)
 471 }
 472 
 473 void
 474 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
 475 {
 476   assert(words <= filler_array_max_size(), "too big for a single object");
 477 
 478   if (words >= filler_array_min_size()) {
 479     fill_with_array(start, words, zap);
 480   } else if (words > 0) {
 481     assert(words == min_fill_size(), "unaligned size");
 482     post_allocation_setup_common(SystemDictionary::Object_klass(), start);
 483   }
 484 }
 485 
 486 void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
 487 {
 488   DEBUG_ONLY(fill_args_check(start, words);)
 489   HandleMark hm;  // Free handles before leaving.
 490   fill_with_object_impl(start, words, zap);
 491 }
 492 
 493 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
 494 {
 495   DEBUG_ONLY(fill_args_check(start, words);)
 496   HandleMark hm;  // Free handles before leaving.
 497 
 498   // Multiple objects may be required depending on the filler array maximum size. Fill
 499   // the range up to that with objects that are filler_array_max_size sized. The
 500   // remainder is filled with a single object.
 501   const size_t min = min_fill_size();
 502   const size_t max = filler_array_max_size();
 503   while (words > max) {
 504     const size_t cur = (words - max) >= min ? max : max - min;
 505     fill_with_array(start, cur, zap);
 506     start += cur;
 507     words -= cur;
 508   }
 509 
 510   fill_with_object_impl(start, words, zap);
 511 }
 512 
 513 void CollectedHeap::post_initialize() {
 514   collector_policy()->post_heap_initialize();
 515 }
 516 
 517 HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
 518   guarantee(false, "thread-local allocation buffers not supported");
 519   return NULL;
 520 }
 521 
 522 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
 523   // The second disjunct in the assertion below makes a concession
 524   // for the start-up verification done while the VM is being
 525   // created. Callers be careful that you know that mutators
 526   // aren't going to interfere -- for instance, this is permissible
 527   // if we are still single-threaded and have either not yet
 528   // started allocating (nothing much to verify) or we have
 529   // started allocating but are now a full-fledged JavaThread
 530   // (and have thus made our TLAB's) available for filling.
 531   assert(SafepointSynchronize::is_at_safepoint() ||
 532          !is_init_completed(),
 533          "Should only be called at a safepoint or at start-up"
 534          " otherwise concurrent mutator activity may make heap "
 535          " unparsable again");
 536   const bool use_tlab = UseTLAB;
 537   const bool deferred = _defer_initial_card_mark;
 538   // The main thread starts allocating via a TLAB even before it
 539   // has added itself to the threads list at vm boot-up.
 540   assert(!use_tlab || Threads::first() != NULL,
 541          "Attempt to fill tlabs before main thread has been added"
 542          " to threads list is doomed to failure!");
 543   for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
 544      if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
 545 #if defined(COMPILER2) || INCLUDE_JVMCI
 546      // The deferred store barriers must all have been flushed to the
 547      // card-table (or other remembered set structure) before GC starts
 548      // processing the card-table (or other remembered set).
 549      if (deferred) flush_deferred_store_barrier(thread);
 550 #else
 551      assert(!deferred, "Should be false");
 552      assert(thread->deferred_card_mark().is_empty(), "Should be empty");
 553 #endif
 554   }
 555 }
 556 
 557 void CollectedHeap::accumulate_statistics_all_tlabs() {
 558   if (UseTLAB) {
 559     assert(SafepointSynchronize::is_at_safepoint() ||
 560          !is_init_completed(),
 561          "should only accumulate statistics on tlabs at safepoint");
 562 
 563     ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
 564   }
 565 }
 566 
 567 void CollectedHeap::resize_all_tlabs() {
 568   if (UseTLAB) {
 569     assert(SafepointSynchronize::is_at_safepoint() ||
 570          !is_init_completed(),
 571          "should only resize tlabs at safepoint");
 572 
 573     ThreadLocalAllocBuffer::resize_all_tlabs();
 574   }
 575 }
 576 
 577 void CollectedHeap::full_gc_dump(GCTimer* timer, bool before) {
 578   assert(timer != NULL, "timer is null");
 579   if ((HeapDumpBeforeFullGC && before) || (HeapDumpAfterFullGC && !before)) {
 580     GCTraceTime(Info, gc) tm(before ? "Heap Dump (before full gc)" : "Heap Dump (after full gc)", timer);
 581     HeapDumper::dump_heap();
 582   }
 583 
 584   LogHandle(gc, classhisto) log;
 585   if (log.is_trace()) {
 586     GCTraceTime(Trace, gc, classhisto) tm(before ? "Class Histogram (before full gc)" : "Class Histogram (after full gc)", timer);
 587     ResourceMark rm;
 588     VM_GC_HeapInspection inspector(log.trace_stream(), false /* ! full gc */);
 589     inspector.doit();
 590   }
 591 }
 592 
 593 void CollectedHeap::pre_full_gc_dump(GCTimer* timer) {
 594   full_gc_dump(timer, true);
 595 }
 596 
 597 void CollectedHeap::post_full_gc_dump(GCTimer* timer) {
 598   full_gc_dump(timer, false);
 599 }
 600 
 601 void CollectedHeap::initialize_reserved_region(HeapWord *start, HeapWord *end) {
 602   // It is important to do this in a way such that concurrent readers can't
 603   // temporarily think something is in the heap.  (Seen this happen in asserts.)
 604   _reserved.set_word_size(0);
 605   _reserved.set_start(start);
 606   _reserved.set_end(end);
 607 }
 608 
 609 /////////////// Unit tests ///////////////
 610 
 611 #ifndef PRODUCT
 612 void CollectedHeap::test_is_in() {
 613   CollectedHeap* heap = Universe::heap();
 614 
 615   uintptr_t epsilon    = (uintptr_t) MinObjAlignment;
 616   uintptr_t heap_start = (uintptr_t) heap->_reserved.start();
 617   uintptr_t heap_end   = (uintptr_t) heap->_reserved.end();
 618 
 619   // Test that NULL is not in the heap.
 620   assert(!heap->is_in(NULL), "NULL is unexpectedly in the heap");
 621 
 622   // Test that a pointer to before the heap start is reported as outside the heap.
 623   assert(heap_start >= ((uintptr_t)NULL + epsilon), "sanity");
 624   void* before_heap = (void*)(heap_start - epsilon);
 625   assert(!heap->is_in(before_heap),
 626          "before_heap: " PTR_FORMAT " is unexpectedly in the heap", p2i(before_heap));
 627 
 628   // Test that a pointer to after the heap end is reported as outside the heap.
 629   assert(heap_end <= ((uintptr_t)-1 - epsilon), "sanity");
 630   void* after_heap = (void*)(heap_end + epsilon);
 631   assert(!heap->is_in(after_heap),
 632          "after_heap: " PTR_FORMAT " is unexpectedly in the heap", p2i(after_heap));
 633 }
 634 
 635 void CollectedHeap_test() {
 636   CollectedHeap::test_is_in();
 637 }
 638 #endif