1 /*
   2  * Copyright (c) 2001, 2017, 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 // WhiteBox API support for concurrent collectors.  These are the
 162 // default implementations, for collectors which don't support this
 163 // feature.
 164 bool CollectedHeap::supports_concurrent_phase_control() const {
 165   return false;
 166 }
 167 
 168 bool CollectedHeap::request_concurrent_phase(const char* phase) {
 169   return false;
 170 }
 171 
 172 // Memory state functions.
 173 
 174 
 175 CollectedHeap::CollectedHeap() :
 176   _barrier_set(NULL),
 177   _is_gc_active(false),
 178   _total_collections(0),
 179   _total_full_collections(0),
 180   _gc_cause(GCCause::_no_gc),
 181   _gc_lastcause(GCCause::_no_gc),
 182   _defer_initial_card_mark(false) // strengthened by subclass in pre_initialize() below.
 183 {
 184   const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
 185   const size_t elements_per_word = HeapWordSize / sizeof(jint);
 186   _filler_array_max_size = align_object_size(filler_array_hdr_size() +
 187                                              max_len / elements_per_word);
 188 
 189   NOT_PRODUCT(_promotion_failure_alot_count = 0;)
 190   NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
 191 
 192   if (UsePerfData) {
 193     EXCEPTION_MARK;
 194 
 195     // create the gc cause jvmstat counters
 196     _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
 197                              80, GCCause::to_string(_gc_cause), CHECK);
 198 
 199     _perf_gc_lastcause =
 200                 PerfDataManager::create_string_variable(SUN_GC, "lastCause",
 201                              80, GCCause::to_string(_gc_lastcause), CHECK);
 202   }
 203 
 204   // Create the ring log
 205   if (LogEvents) {
 206     _gc_heap_log = new GCHeapLog();
 207   } else {
 208     _gc_heap_log = NULL;
 209   }
 210 }
 211 
 212 // This interface assumes that it's being called by the
 213 // vm thread. It collects the heap assuming that the
 214 // heap lock is already held and that we are executing in
 215 // the context of the vm thread.
 216 void CollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
 217   assert(Thread::current()->is_VM_thread(), "Precondition#1");
 218   assert(Heap_lock->is_locked(), "Precondition#2");
 219   GCCauseSetter gcs(this, cause);
 220   switch (cause) {
 221     case GCCause::_heap_inspection:
 222     case GCCause::_heap_dump:
 223     case GCCause::_metadata_GC_threshold : {
 224       HandleMark hm;
 225       do_full_collection(false);        // don't clear all soft refs
 226       break;
 227     }
 228     case GCCause::_metadata_GC_clear_soft_refs: {
 229       HandleMark hm;
 230       do_full_collection(true);         // do clear all soft refs
 231       break;
 232     }
 233     default:
 234       ShouldNotReachHere(); // Unexpected use of this function
 235   }
 236 }
 237 
 238 void CollectedHeap::set_barrier_set(BarrierSet* barrier_set) {
 239   _barrier_set = barrier_set;
 240   oopDesc::set_bs(_barrier_set);
 241 }
 242 
 243 void CollectedHeap::pre_initialize() {
 244   // Used for ReduceInitialCardMarks (when COMPILER2 is used);
 245   // otherwise remains unused.
 246 #if defined(COMPILER2) || INCLUDE_JVMCI
 247   _defer_initial_card_mark =    ReduceInitialCardMarks && can_elide_tlab_store_barriers()
 248                              && (DeferInitialCardMark || card_mark_must_follow_store());
 249 #else
 250   assert(_defer_initial_card_mark == false, "Who would set it?");
 251 #endif
 252 }
 253 
 254 #ifndef PRODUCT
 255 void CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) {
 256   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 257     for (size_t slot = 0; slot < size; slot += 1) {
 258       assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal),
 259              "Found badHeapWordValue in post-allocation check");
 260     }
 261   }
 262 }
 263 
 264 void CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) {
 265   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 266     for (size_t slot = 0; slot < size; slot += 1) {
 267       assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal),
 268              "Found non badHeapWordValue in pre-allocation check");
 269     }
 270   }
 271 }
 272 #endif // PRODUCT
 273 
 274 #ifdef ASSERT
 275 void CollectedHeap::check_for_valid_allocation_state() {
 276   Thread *thread = Thread::current();
 277   // How to choose between a pending exception and a potential
 278   // OutOfMemoryError?  Don't allow pending exceptions.
 279   // This is a VM policy failure, so how do we exhaustively test it?
 280   assert(!thread->has_pending_exception(),
 281          "shouldn't be allocating with pending exception");
 282   if (StrictSafepointChecks) {
 283     assert(thread->allow_allocation(),
 284            "Allocation done by thread for which allocation is blocked "
 285            "by No_Allocation_Verifier!");
 286     // Allocation of an oop can always invoke a safepoint,
 287     // hence, the true argument
 288     thread->check_for_valid_safepoint_state(true);
 289   }
 290 }
 291 #endif
 292 
 293 HeapWord* CollectedHeap::allocate_from_tlab_slow(KlassHandle klass, Thread* thread, size_t size) {
 294 
 295   // Retain tlab and allocate object in shared space if
 296   // the amount free in the tlab is too large to discard.
 297   if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
 298     thread->tlab().record_slow_allocation(size);
 299     return NULL;
 300   }
 301 
 302   // Discard tlab and allocate a new one.
 303   // To minimize fragmentation, the last TLAB may be smaller than the rest.
 304   size_t new_tlab_size = thread->tlab().compute_size(size);
 305 
 306   thread->tlab().clear_before_allocation();
 307 
 308   if (new_tlab_size == 0) {
 309     return NULL;
 310   }
 311 
 312   // Allocate a new TLAB...
 313   HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
 314   if (obj == NULL) {
 315     return NULL;
 316   }
 317 
 318   AllocTracer::send_allocation_in_new_tlab_event(klass, new_tlab_size * HeapWordSize, size * HeapWordSize);
 319 
 320   if (ZeroTLAB) {
 321     // ..and clear it.
 322     Copy::zero_to_words(obj, new_tlab_size);
 323   } else {
 324     // ...and zap just allocated object.
 325 #ifdef ASSERT
 326     // Skip mangling the space corresponding to the object header to
 327     // ensure that the returned space is not considered parsable by
 328     // any concurrent GC thread.
 329     size_t hdr_size = oopDesc::header_size();
 330     Copy::fill_to_words(obj + hdr_size, new_tlab_size - hdr_size, badHeapWordVal);
 331 #endif // ASSERT
 332   }
 333   thread->tlab().fill(obj, obj + size, new_tlab_size);
 334   return obj;
 335 }
 336 
 337 void CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
 338   MemRegion deferred = thread->deferred_card_mark();
 339   if (!deferred.is_empty()) {
 340     assert(_defer_initial_card_mark, "Otherwise should be empty");
 341     {
 342       // Verify that the storage points to a parsable object in heap
 343       DEBUG_ONLY(oop old_obj = oop(deferred.start());)
 344       assert(is_in(old_obj), "Not in allocated heap");
 345       assert(!can_elide_initializing_store_barrier(old_obj),
 346              "Else should have been filtered in new_store_pre_barrier()");
 347       assert(old_obj->is_oop(true), "Not an oop");
 348       assert(deferred.word_size() == (size_t)(old_obj->size()),
 349              "Mismatch: multiple objects?");
 350     }
 351     BarrierSet* bs = barrier_set();
 352     assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
 353     bs->write_region(deferred);
 354     // "Clear" the deferred_card_mark field
 355     thread->set_deferred_card_mark(MemRegion());
 356   }
 357   assert(thread->deferred_card_mark().is_empty(), "invariant");
 358 }
 359 
 360 size_t CollectedHeap::max_tlab_size() const {
 361   // TLABs can't be bigger than we can fill with a int[Integer.MAX_VALUE].
 362   // This restriction could be removed by enabling filling with multiple arrays.
 363   // If we compute that the reasonable way as
 364   //    header_size + ((sizeof(jint) * max_jint) / HeapWordSize)
 365   // we'll overflow on the multiply, so we do the divide first.
 366   // We actually lose a little by dividing first,
 367   // but that just makes the TLAB  somewhat smaller than the biggest array,
 368   // which is fine, since we'll be able to fill that.
 369   size_t max_int_size = typeArrayOopDesc::header_size(T_INT) +
 370               sizeof(jint) *
 371               ((juint) max_jint / (size_t) HeapWordSize);
 372   return align_size_down(max_int_size, MinObjAlignment);
 373 }
 374 
 375 // Helper for ReduceInitialCardMarks. For performance,
 376 // compiled code may elide card-marks for initializing stores
 377 // to a newly allocated object along the fast-path. We
 378 // compensate for such elided card-marks as follows:
 379 // (a) Generational, non-concurrent collectors, such as
 380 //     GenCollectedHeap(ParNew,DefNew,Tenured) and
 381 //     ParallelScavengeHeap(ParallelGC, ParallelOldGC)
 382 //     need the card-mark if and only if the region is
 383 //     in the old gen, and do not care if the card-mark
 384 //     succeeds or precedes the initializing stores themselves,
 385 //     so long as the card-mark is completed before the next
 386 //     scavenge. For all these cases, we can do a card mark
 387 //     at the point at which we do a slow path allocation
 388 //     in the old gen, i.e. in this call.
 389 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
 390 //     in addition that the card-mark for an old gen allocated
 391 //     object strictly follow any associated initializing stores.
 392 //     In these cases, the memRegion remembered below is
 393 //     used to card-mark the entire region either just before the next
 394 //     slow-path allocation by this thread or just before the next scavenge or
 395 //     CMS-associated safepoint, whichever of these events happens first.
 396 //     (The implicit assumption is that the object has been fully
 397 //     initialized by this point, a fact that we assert when doing the
 398 //     card-mark.)
 399 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
 400 //     G1 concurrent marking is in progress an SATB (pre-write-)barrier
 401 //     is used to remember the pre-value of any store. Initializing
 402 //     stores will not need this barrier, so we need not worry about
 403 //     compensating for the missing pre-barrier here. Turning now
 404 //     to the post-barrier, we note that G1 needs a RS update barrier
 405 //     which simply enqueues a (sequence of) dirty cards which may
 406 //     optionally be refined by the concurrent update threads. Note
 407 //     that this barrier need only be applied to a non-young write,
 408 //     but, like in CMS, because of the presence of concurrent refinement
 409 //     (much like CMS' precleaning), must strictly follow the oop-store.
 410 //     Thus, using the same protocol for maintaining the intended
 411 //     invariants turns out, serendepitously, to be the same for both
 412 //     G1 and CMS.
 413 //
 414 // For any future collector, this code should be reexamined with
 415 // that specific collector in mind, and the documentation above suitably
 416 // extended and updated.
 417 oop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
 418   // If a previous card-mark was deferred, flush it now.
 419   flush_deferred_store_barrier(thread);
 420   if (can_elide_initializing_store_barrier(new_obj) ||
 421       new_obj->is_typeArray()) {
 422     // Arrays of non-references don't need a pre-barrier.
 423     // The deferred_card_mark region should be empty
 424     // following the flush above.
 425     assert(thread->deferred_card_mark().is_empty(), "Error");
 426   } else {
 427     MemRegion mr((HeapWord*)new_obj, new_obj->size());
 428     assert(!mr.is_empty(), "Error");
 429     if (_defer_initial_card_mark) {
 430       // Defer the card mark
 431       thread->set_deferred_card_mark(mr);
 432     } else {
 433       // Do the card mark
 434       BarrierSet* bs = barrier_set();
 435       assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
 436       bs->write_region(mr);
 437     }
 438   }
 439   return new_obj;
 440 }
 441 
 442 size_t CollectedHeap::filler_array_hdr_size() {
 443   return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long
 444 }
 445 
 446 size_t CollectedHeap::filler_array_min_size() {
 447   return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
 448 }
 449 
 450 #ifdef ASSERT
 451 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
 452 {
 453   assert(words >= min_fill_size(), "too small to fill");
 454   assert(words % MinObjAlignment == 0, "unaligned size");
 455   assert(Universe::heap()->is_in_reserved(start), "not in heap");
 456   assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
 457 }
 458 
 459 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
 460 {
 461   if (ZapFillerObjects && zap) {
 462     Copy::fill_to_words(start + filler_array_hdr_size(),
 463                         words - filler_array_hdr_size(), 0XDEAFBABE);
 464   }
 465 }
 466 #endif // ASSERT
 467 
 468 void
 469 CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
 470 {
 471   assert(words >= filler_array_min_size(), "too small for an array");
 472   assert(words <= filler_array_max_size(), "too big for a single object");
 473 
 474   const size_t payload_size = words - filler_array_hdr_size();
 475   const size_t len = payload_size * HeapWordSize / sizeof(jint);
 476   assert((int)len >= 0, "size too large " SIZE_FORMAT " becomes %d", words, (int)len);
 477 
 478   // Set the length first for concurrent GC.
 479   ((arrayOop)start)->set_length((int)len);
 480   post_allocation_setup_common(Universe::intArrayKlassObj(), start);
 481   DEBUG_ONLY(zap_filler_array(start, words, zap);)
 482 }
 483 
 484 void
 485 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
 486 {
 487   assert(words <= filler_array_max_size(), "too big for a single object");
 488 
 489   if (words >= filler_array_min_size()) {
 490     fill_with_array(start, words, zap);
 491   } else if (words > 0) {
 492     assert(words == min_fill_size(), "unaligned size");
 493     post_allocation_setup_common(SystemDictionary::Object_klass(), start);
 494   }
 495 }
 496 
 497 void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
 498 {
 499   DEBUG_ONLY(fill_args_check(start, words);)
 500   HandleMark hm;  // Free handles before leaving.
 501   fill_with_object_impl(start, words, zap);
 502 }
 503 
 504 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
 505 {
 506   DEBUG_ONLY(fill_args_check(start, words);)
 507   HandleMark hm;  // Free handles before leaving.
 508 
 509   // Multiple objects may be required depending on the filler array maximum size. Fill
 510   // the range up to that with objects that are filler_array_max_size sized. The
 511   // remainder is filled with a single object.
 512   const size_t min = min_fill_size();
 513   const size_t max = filler_array_max_size();
 514   while (words > max) {
 515     const size_t cur = (words - max) >= min ? max : max - min;
 516     fill_with_array(start, cur, zap);
 517     start += cur;
 518     words -= cur;
 519   }
 520 
 521   fill_with_object_impl(start, words, zap);
 522 }
 523 
 524 HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
 525   guarantee(false, "thread-local allocation buffers not supported");
 526   return NULL;
 527 }
 528 
 529 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
 530   // The second disjunct in the assertion below makes a concession
 531   // for the start-up verification done while the VM is being
 532   // created. Callers be careful that you know that mutators
 533   // aren't going to interfere -- for instance, this is permissible
 534   // if we are still single-threaded and have either not yet
 535   // started allocating (nothing much to verify) or we have
 536   // started allocating but are now a full-fledged JavaThread
 537   // (and have thus made our TLAB's) available for filling.
 538   assert(SafepointSynchronize::is_at_safepoint() ||
 539          !is_init_completed(),
 540          "Should only be called at a safepoint or at start-up"
 541          " otherwise concurrent mutator activity may make heap "
 542          " unparsable again");
 543   const bool use_tlab = UseTLAB;
 544   const bool deferred = _defer_initial_card_mark;
 545   // The main thread starts allocating via a TLAB even before it
 546   // has added itself to the threads list at vm boot-up.
 547   assert(!use_tlab || Threads::first() != NULL,
 548          "Attempt to fill tlabs before main thread has been added"
 549          " to threads list is doomed to failure!");
 550   for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
 551      if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
 552 #if defined(COMPILER2) || INCLUDE_JVMCI
 553      // The deferred store barriers must all have been flushed to the
 554      // card-table (or other remembered set structure) before GC starts
 555      // processing the card-table (or other remembered set).
 556      if (deferred) flush_deferred_store_barrier(thread);
 557 #else
 558      assert(!deferred, "Should be false");
 559      assert(thread->deferred_card_mark().is_empty(), "Should be empty");
 560 #endif
 561   }
 562 }
 563 
 564 void CollectedHeap::accumulate_statistics_all_tlabs() {
 565   if (UseTLAB) {
 566     assert(SafepointSynchronize::is_at_safepoint() ||
 567          !is_init_completed(),
 568          "should only accumulate statistics on tlabs at safepoint");
 569 
 570     ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
 571   }
 572 }
 573 
 574 void CollectedHeap::resize_all_tlabs() {
 575   if (UseTLAB) {
 576     assert(SafepointSynchronize::is_at_safepoint() ||
 577          !is_init_completed(),
 578          "should only resize tlabs at safepoint");
 579 
 580     ThreadLocalAllocBuffer::resize_all_tlabs();
 581   }
 582 }
 583 
 584 void CollectedHeap::full_gc_dump(GCTimer* timer, bool before) {
 585   assert(timer != NULL, "timer is null");
 586   if ((HeapDumpBeforeFullGC && before) || (HeapDumpAfterFullGC && !before)) {
 587     GCTraceTime(Info, gc) tm(before ? "Heap Dump (before full gc)" : "Heap Dump (after full gc)", timer);
 588     HeapDumper::dump_heap();
 589   }
 590 
 591   Log(gc, classhisto) log;
 592   if (log.is_trace()) {
 593     GCTraceTime(Trace, gc, classhisto) tm(before ? "Class Histogram (before full gc)" : "Class Histogram (after full gc)", timer);
 594     ResourceMark rm;
 595     VM_GC_HeapInspection inspector(log.trace_stream(), false /* ! full gc */);
 596     inspector.doit();
 597   }
 598 }
 599 
 600 void CollectedHeap::pre_full_gc_dump(GCTimer* timer) {
 601   full_gc_dump(timer, true);
 602 }
 603 
 604 void CollectedHeap::post_full_gc_dump(GCTimer* timer) {
 605   full_gc_dump(timer, false);
 606 }
 607 
 608 void CollectedHeap::initialize_reserved_region(HeapWord *start, HeapWord *end) {
 609   // It is important to do this in a way such that concurrent readers can't
 610   // temporarily think something is in the heap.  (Seen this happen in asserts.)
 611   _reserved.set_word_size(0);
 612   _reserved.set_start(start);
 613   _reserved.set_end(end);
 614 }