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/heapMonitoring.hpp"
  42 #include "runtime/init.hpp"
  43 #include "runtime/thread.inline.hpp"
  44 #include "runtime/threadSMR.hpp"
  45 #include "services/heapDumper.hpp"
  46 #include "utilities/align.hpp"
  47 
  48 
  49 #ifdef ASSERT
  50 int CollectedHeap::_fire_out_of_memory_count = 0;
  51 #endif
  52 
  53 size_t CollectedHeap::_filler_array_max_size = 0;
  54 
  55 template <>
  56 void EventLogBase<GCMessage>::print(outputStream* st, GCMessage& m) {
  57   st->print_cr("GC heap %s", m.is_before ? "before" : "after");
  58   st->print_raw(m);
  59 }
  60 
  61 void GCHeapLog::log_heap(CollectedHeap* heap, bool before) {
  62   if (!should_log()) {
  63     return;
  64   }
  65 
  66   double timestamp = fetch_timestamp();
  67   MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
  68   int index = compute_log_index();
  69   _records[index].thread = NULL; // Its the GC thread so it's not that interesting.
  70   _records[index].timestamp = timestamp;
  71   _records[index].data.is_before = before;
  72   stringStream st(_records[index].data.buffer(), _records[index].data.size());
  73 
  74   st.print_cr("{Heap %s GC invocations=%u (full %u):",
  75                  before ? "before" : "after",
  76                  heap->total_collections(),
  77                  heap->total_full_collections());
  78 
  79   heap->print_on(&st);
  80   st.print_cr("}");
  81 }
  82 
  83 VirtualSpaceSummary CollectedHeap::create_heap_space_summary() {
  84   size_t capacity_in_words = capacity() / HeapWordSize;
  85 
  86   return VirtualSpaceSummary(
  87     reserved_region().start(), reserved_region().start() + capacity_in_words, reserved_region().end());
  88 }
  89 
  90 GCHeapSummary CollectedHeap::create_heap_summary() {
  91   VirtualSpaceSummary heap_space = create_heap_space_summary();
  92   return GCHeapSummary(heap_space, used());
  93 }
  94 
  95 MetaspaceSummary CollectedHeap::create_metaspace_summary() {
  96   const MetaspaceSizes meta_space(
  97       MetaspaceAux::committed_bytes(),
  98       MetaspaceAux::used_bytes(),
  99       MetaspaceAux::reserved_bytes());
 100   const MetaspaceSizes data_space(
 101       MetaspaceAux::committed_bytes(Metaspace::NonClassType),
 102       MetaspaceAux::used_bytes(Metaspace::NonClassType),
 103       MetaspaceAux::reserved_bytes(Metaspace::NonClassType));
 104   const MetaspaceSizes class_space(
 105       MetaspaceAux::committed_bytes(Metaspace::ClassType),
 106       MetaspaceAux::used_bytes(Metaspace::ClassType),
 107       MetaspaceAux::reserved_bytes(Metaspace::ClassType));
 108 
 109   const MetaspaceChunkFreeListSummary& ms_chunk_free_list_summary =
 110     MetaspaceAux::chunk_free_list_summary(Metaspace::NonClassType);
 111   const MetaspaceChunkFreeListSummary& class_chunk_free_list_summary =
 112     MetaspaceAux::chunk_free_list_summary(Metaspace::ClassType);
 113 
 114   return MetaspaceSummary(MetaspaceGC::capacity_until_GC(), meta_space, data_space, class_space,
 115                           ms_chunk_free_list_summary, class_chunk_free_list_summary);
 116 }
 117 
 118 void CollectedHeap::print_heap_before_gc() {
 119   Universe::print_heap_before_gc();
 120   if (_gc_heap_log != NULL) {
 121     _gc_heap_log->log_heap_before(this);
 122   }
 123 }
 124 
 125 void CollectedHeap::print_heap_after_gc() {
 126   Universe::print_heap_after_gc();
 127   if (_gc_heap_log != NULL) {
 128     _gc_heap_log->log_heap_after(this);
 129   }
 130 }
 131 
 132 void CollectedHeap::print_on_error(outputStream* st) const {
 133   st->print_cr("Heap:");
 134   print_extended_on(st);
 135   st->cr();
 136 
 137   _barrier_set->print_on(st);
 138 }
 139 
 140 void CollectedHeap::trace_heap(GCWhen::Type when, const GCTracer* gc_tracer) {
 141   const GCHeapSummary& heap_summary = create_heap_summary();
 142   gc_tracer->report_gc_heap_summary(when, heap_summary);
 143 
 144   const MetaspaceSummary& metaspace_summary = create_metaspace_summary();
 145   gc_tracer->report_metaspace_summary(when, metaspace_summary);
 146 }
 147 
 148 void CollectedHeap::trace_heap_before_gc(const GCTracer* gc_tracer) {
 149   trace_heap(GCWhen::BeforeGC, gc_tracer);
 150 }
 151 
 152 void CollectedHeap::trace_heap_after_gc(const GCTracer* gc_tracer) {
 153   trace_heap(GCWhen::AfterGC, gc_tracer);
 154 }
 155 
 156 // WhiteBox API support for concurrent collectors.  These are the
 157 // default implementations, for collectors which don't support this
 158 // feature.
 159 bool CollectedHeap::supports_concurrent_phase_control() const {
 160   return false;
 161 }
 162 
 163 const char* const* CollectedHeap::concurrent_phases() const {
 164   static const char* const result[] = { NULL };
 165   return result;
 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   BarrierSet::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 COMPILER2_OR_JVMCI
 247   _defer_initial_card_mark = is_server_compilation_mode_vm() &&  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(Klass* klass, Thread* thread, size_t size) {
 294   HeapWord* obj = NULL;
 295 
 296   if (HeapMonitoring::enabled()) {
 297     // Try to allocate the sampled object from TLAB, it is possible a sample
 298     // point was put and the TLAB still has space.
 299     obj = thread->tlab().allocate_sampled_object(size);
 300 
 301     if (obj != NULL) {
 302       return obj;
 303     }
 304   }
 305 
 306   // Retain tlab and allocate object in shared space if
 307   // the amount free in the tlab is too large to discard.
 308   if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
 309     thread->tlab().record_slow_allocation(size);
 310     return NULL;
 311   }
 312 
 313   // Discard tlab and allocate a new one.
 314   // To minimize fragmentation, the last TLAB may be smaller than the rest.
 315   size_t new_tlab_size = thread->tlab().compute_size(size);
 316 
 317   thread->tlab().clear_before_allocation();
 318 
 319   if (new_tlab_size == 0) {
 320     return NULL;
 321   }
 322 
 323   // Allocate a new TLAB...
 324   obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
 325   if (obj == NULL) {
 326     return NULL;
 327   }
 328 
 329   AllocTracer::send_allocation_in_new_tlab(klass, obj, new_tlab_size * HeapWordSize, size * HeapWordSize, thread);
 330 
 331   if (ZeroTLAB) {
 332     // ..and clear it.
 333     Copy::zero_to_words(obj, new_tlab_size);
 334   } else {
 335     // ...and zap just allocated object.
 336 #ifdef ASSERT
 337     // Skip mangling the space corresponding to the object header to
 338     // ensure that the returned space is not considered parsable by
 339     // any concurrent GC thread.
 340     size_t hdr_size = oopDesc::header_size();
 341     Copy::fill_to_words(obj + hdr_size, new_tlab_size - hdr_size, badHeapWordVal);
 342 #endif // ASSERT
 343   }
 344 
 345   // Send the thread information about this allocation in case a sample is
 346   // requested.
 347   if (HeapMonitoring::enabled()) {
 348     size_t tlab_bytes_since_last_sample = thread->tlab().bytes_since_last_sample_point();
 349     thread->heap_sampler().check_for_sampling(obj, size, tlab_bytes_since_last_sample);
 350   }
 351 
 352   thread->tlab().fill(obj, obj + size, new_tlab_size);
 353   return obj;
 354 }
 355 
 356 void CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
 357   MemRegion deferred = thread->deferred_card_mark();
 358   if (!deferred.is_empty()) {
 359     assert(_defer_initial_card_mark, "Otherwise should be empty");
 360     {
 361       // Verify that the storage points to a parsable object in heap
 362       DEBUG_ONLY(oop old_obj = oop(deferred.start());)
 363       assert(is_in(old_obj), "Not in allocated heap");
 364       assert(!can_elide_initializing_store_barrier(old_obj),
 365              "Else should have been filtered in new_store_pre_barrier()");
 366       assert(oopDesc::is_oop(old_obj, true), "Not an oop");
 367       assert(deferred.word_size() == (size_t)(old_obj->size()),
 368              "Mismatch: multiple objects?");
 369     }
 370     BarrierSet* bs = barrier_set();
 371     bs->write_region(deferred);
 372     // "Clear" the deferred_card_mark field
 373     thread->set_deferred_card_mark(MemRegion());
 374   }
 375   assert(thread->deferred_card_mark().is_empty(), "invariant");
 376 }
 377 
 378 size_t CollectedHeap::max_tlab_size() const {
 379   // TLABs can't be bigger than we can fill with a int[Integer.MAX_VALUE].
 380   // This restriction could be removed by enabling filling with multiple arrays.
 381   // If we compute that the reasonable way as
 382   //    header_size + ((sizeof(jint) * max_jint) / HeapWordSize)
 383   // we'll overflow on the multiply, so we do the divide first.
 384   // We actually lose a little by dividing first,
 385   // but that just makes the TLAB  somewhat smaller than the biggest array,
 386   // which is fine, since we'll be able to fill that.
 387   size_t max_int_size = typeArrayOopDesc::header_size(T_INT) +
 388               sizeof(jint) *
 389               ((juint) max_jint / (size_t) HeapWordSize);
 390   return align_down(max_int_size, MinObjAlignment);
 391 }
 392 
 393 // Helper for ReduceInitialCardMarks. For performance,
 394 // compiled code may elide card-marks for initializing stores
 395 // to a newly allocated object along the fast-path. We
 396 // compensate for such elided card-marks as follows:
 397 // (a) Generational, non-concurrent collectors, such as
 398 //     GenCollectedHeap(ParNew,DefNew,Tenured) and
 399 //     ParallelScavengeHeap(ParallelGC, ParallelOldGC)
 400 //     need the card-mark if and only if the region is
 401 //     in the old gen, and do not care if the card-mark
 402 //     succeeds or precedes the initializing stores themselves,
 403 //     so long as the card-mark is completed before the next
 404 //     scavenge. For all these cases, we can do a card mark
 405 //     at the point at which we do a slow path allocation
 406 //     in the old gen, i.e. in this call.
 407 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
 408 //     in addition that the card-mark for an old gen allocated
 409 //     object strictly follow any associated initializing stores.
 410 //     In these cases, the memRegion remembered below is
 411 //     used to card-mark the entire region either just before the next
 412 //     slow-path allocation by this thread or just before the next scavenge or
 413 //     CMS-associated safepoint, whichever of these events happens first.
 414 //     (The implicit assumption is that the object has been fully
 415 //     initialized by this point, a fact that we assert when doing the
 416 //     card-mark.)
 417 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
 418 //     G1 concurrent marking is in progress an SATB (pre-write-)barrier
 419 //     is used to remember the pre-value of any store. Initializing
 420 //     stores will not need this barrier, so we need not worry about
 421 //     compensating for the missing pre-barrier here. Turning now
 422 //     to the post-barrier, we note that G1 needs a RS update barrier
 423 //     which simply enqueues a (sequence of) dirty cards which may
 424 //     optionally be refined by the concurrent update threads. Note
 425 //     that this barrier need only be applied to a non-young write,
 426 //     but, like in CMS, because of the presence of concurrent refinement
 427 //     (much like CMS' precleaning), must strictly follow the oop-store.
 428 //     Thus, using the same protocol for maintaining the intended
 429 //     invariants turns out, serendepitously, to be the same for both
 430 //     G1 and CMS.
 431 //
 432 // For any future collector, this code should be reexamined with
 433 // that specific collector in mind, and the documentation above suitably
 434 // extended and updated.
 435 oop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
 436   // If a previous card-mark was deferred, flush it now.
 437   flush_deferred_store_barrier(thread);
 438   if (can_elide_initializing_store_barrier(new_obj) ||
 439       new_obj->is_typeArray()) {
 440     // Arrays of non-references don't need a pre-barrier.
 441     // The deferred_card_mark region should be empty
 442     // following the flush above.
 443     assert(thread->deferred_card_mark().is_empty(), "Error");
 444   } else {
 445     MemRegion mr((HeapWord*)new_obj, new_obj->size());
 446     assert(!mr.is_empty(), "Error");
 447     if (_defer_initial_card_mark) {
 448       // Defer the card mark
 449       thread->set_deferred_card_mark(mr);
 450     } else {
 451       // Do the card mark
 452       BarrierSet* bs = barrier_set();
 453       bs->write_region(mr);
 454     }
 455   }
 456   return new_obj;
 457 }
 458 
 459 size_t CollectedHeap::filler_array_hdr_size() {
 460   return align_object_offset(arrayOopDesc::header_size(T_INT)); // align to Long
 461 }
 462 
 463 size_t CollectedHeap::filler_array_min_size() {
 464   return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
 465 }
 466 
 467 #ifdef ASSERT
 468 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
 469 {
 470   assert(words >= min_fill_size(), "too small to fill");
 471   assert(is_object_aligned(words), "unaligned size");
 472   assert(Universe::heap()->is_in_reserved(start), "not in heap");
 473   assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
 474 }
 475 
 476 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
 477 {
 478   if (ZapFillerObjects && zap) {
 479     Copy::fill_to_words(start + filler_array_hdr_size(),
 480                         words - filler_array_hdr_size(), 0XDEAFBABE);
 481   }
 482 }
 483 #endif // ASSERT
 484 
 485 void
 486 CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
 487 {
 488   assert(words >= filler_array_min_size(), "too small for an array");
 489   assert(words <= filler_array_max_size(), "too big for a single object");
 490 
 491   const size_t payload_size = words - filler_array_hdr_size();
 492   const size_t len = payload_size * HeapWordSize / sizeof(jint);
 493   assert((int)len >= 0, "size too large " SIZE_FORMAT " becomes %d", words, (int)len);
 494 
 495   // Set the length first for concurrent GC.
 496   ((arrayOop)start)->set_length((int)len);
 497   post_allocation_setup_common(Universe::intArrayKlassObj(), start);
 498   DEBUG_ONLY(zap_filler_array(start, words, zap);)
 499 }
 500 
 501 void
 502 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
 503 {
 504   assert(words <= filler_array_max_size(), "too big for a single object");
 505 
 506   if (words >= filler_array_min_size()) {
 507     fill_with_array(start, words, zap);
 508   } else if (words > 0) {
 509     assert(words == min_fill_size(), "unaligned size");
 510     post_allocation_setup_common(SystemDictionary::Object_klass(), start);
 511   }
 512 }
 513 
 514 void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
 515 {
 516   DEBUG_ONLY(fill_args_check(start, words);)
 517   HandleMark hm;  // Free handles before leaving.
 518   fill_with_object_impl(start, words, zap);
 519 }
 520 
 521 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
 522 {
 523   DEBUG_ONLY(fill_args_check(start, words);)
 524   HandleMark hm;  // Free handles before leaving.
 525 
 526   // Multiple objects may be required depending on the filler array maximum size. Fill
 527   // the range up to that with objects that are filler_array_max_size sized. The
 528   // remainder is filled with a single object.
 529   const size_t min = min_fill_size();
 530   const size_t max = filler_array_max_size();
 531   while (words > max) {
 532     const size_t cur = (words - max) >= min ? max : max - min;
 533     fill_with_array(start, cur, zap);
 534     start += cur;
 535     words -= cur;
 536   }
 537 
 538   fill_with_object_impl(start, words, zap);
 539 }
 540 
 541 HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
 542   guarantee(false, "thread-local allocation buffers not supported");
 543   return NULL;
 544 }
 545 
 546 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
 547   // The second disjunct in the assertion below makes a concession
 548   // for the start-up verification done while the VM is being
 549   // created. Callers be careful that you know that mutators
 550   // aren't going to interfere -- for instance, this is permissible
 551   // if we are still single-threaded and have either not yet
 552   // started allocating (nothing much to verify) or we have
 553   // started allocating but are now a full-fledged JavaThread
 554   // (and have thus made our TLAB's) available for filling.
 555   assert(SafepointSynchronize::is_at_safepoint() ||
 556          !is_init_completed(),
 557          "Should only be called at a safepoint or at start-up"
 558          " otherwise concurrent mutator activity may make heap "
 559          " unparsable again");
 560   const bool use_tlab = UseTLAB;
 561   const bool deferred = _defer_initial_card_mark;
 562   // The main thread starts allocating via a TLAB even before it
 563   // has added itself to the threads list at vm boot-up.
 564   JavaThreadIteratorWithHandle jtiwh;
 565   assert(!use_tlab || jtiwh.length() > 0,
 566          "Attempt to fill tlabs before main thread has been added"
 567          " to threads list is doomed to failure!");
 568   for (; JavaThread *thread = jtiwh.next(); ) {
 569      if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
 570 #if COMPILER2_OR_JVMCI
 571      // The deferred store barriers must all have been flushed to the
 572      // card-table (or other remembered set structure) before GC starts
 573      // processing the card-table (or other remembered set).
 574      if (deferred) flush_deferred_store_barrier(thread);
 575 #else
 576      assert(!deferred, "Should be false");
 577      assert(thread->deferred_card_mark().is_empty(), "Should be empty");
 578 #endif
 579   }
 580 }
 581 
 582 void CollectedHeap::accumulate_statistics_all_tlabs() {
 583   if (UseTLAB) {
 584     assert(SafepointSynchronize::is_at_safepoint() ||
 585          !is_init_completed(),
 586          "should only accumulate statistics on tlabs at safepoint");
 587 
 588     ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
 589   }
 590 }
 591 
 592 void CollectedHeap::resize_all_tlabs() {
 593   if (UseTLAB) {
 594     assert(SafepointSynchronize::is_at_safepoint() ||
 595          !is_init_completed(),
 596          "should only resize tlabs at safepoint");
 597 
 598     ThreadLocalAllocBuffer::resize_all_tlabs();
 599   }
 600 }
 601 
 602 void CollectedHeap::full_gc_dump(GCTimer* timer, bool before) {
 603   assert(timer != NULL, "timer is null");
 604   if ((HeapDumpBeforeFullGC && before) || (HeapDumpAfterFullGC && !before)) {
 605     GCTraceTime(Info, gc) tm(before ? "Heap Dump (before full gc)" : "Heap Dump (after full gc)", timer);
 606     HeapDumper::dump_heap();
 607   }
 608 
 609   LogTarget(Trace, gc, classhisto) lt;
 610   if (lt.is_enabled()) {
 611     GCTraceTime(Trace, gc, classhisto) tm(before ? "Class Histogram (before full gc)" : "Class Histogram (after full gc)", timer);
 612     ResourceMark rm;
 613     LogStream ls(lt);
 614     VM_GC_HeapInspection inspector(&ls, false /* ! full gc */);
 615     inspector.doit();
 616   }
 617 }
 618 
 619 void CollectedHeap::pre_full_gc_dump(GCTimer* timer) {
 620   full_gc_dump(timer, true);
 621 }
 622 
 623 void CollectedHeap::post_full_gc_dump(GCTimer* timer) {
 624   full_gc_dump(timer, false);
 625 }
 626 
 627 void CollectedHeap::initialize_reserved_region(HeapWord *start, HeapWord *end) {
 628   // It is important to do this in a way such that concurrent readers can't
 629   // temporarily think something is in the heap.  (Seen this happen in asserts.)
 630   _reserved.set_word_size(0);
 631   _reserved.set_start(start);
 632   _reserved.set_end(end);
 633 }
 634 
 635 void CollectedHeap::post_initialize() {
 636   initialize_serviceability();
 637 }