1 /*
   2  * Copyright (c) 2001, 2013, 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 "gc_implementation/parallelScavenge/adjoiningGenerations.hpp"
  27 #include "gc_implementation/parallelScavenge/adjoiningVirtualSpaces.hpp"
  28 #include "gc_implementation/parallelScavenge/cardTableExtension.hpp"
  29 #include "gc_implementation/parallelScavenge/gcTaskManager.hpp"
  30 #include "gc_implementation/parallelScavenge/generationSizer.hpp"
  31 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp"
  32 #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp"
  33 #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
  34 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
  35 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
  36 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
  37 #include "gc_implementation/parallelScavenge/vmPSOperations.hpp"
  38 #include "memory/gcLocker.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/java.hpp"
  42 #include "runtime/vmThread.hpp"
  43 #include "services/memTracker.hpp"
  44 #include "utilities/vmError.hpp"
  45 
  46 PSYoungGen*  ParallelScavengeHeap::_young_gen = NULL;
  47 PSOldGen*    ParallelScavengeHeap::_old_gen = NULL;
  48 PSAdaptiveSizePolicy* ParallelScavengeHeap::_size_policy = NULL;
  49 PSGCAdaptivePolicyCounters* ParallelScavengeHeap::_gc_policy_counters = NULL;
  50 ParallelScavengeHeap* ParallelScavengeHeap::_psh = NULL;
  51 GCTaskManager* ParallelScavengeHeap::_gc_task_manager = NULL;
  52 
  53 static void trace_gen_sizes(const char* const str,
  54                             size_t og_min, size_t og_max,
  55                             size_t yg_min, size_t yg_max)
  56 {
  57   if (TracePageSizes) {
  58     tty->print_cr("%s:  " SIZE_FORMAT "," SIZE_FORMAT " "
  59                   SIZE_FORMAT "," SIZE_FORMAT " "
  60                   SIZE_FORMAT,
  61                   str,
  62                   og_min / K, og_max / K,
  63                   yg_min / K, yg_max / K,
  64                   (og_max + yg_max) / K);
  65   }
  66 }
  67 
  68 jint ParallelScavengeHeap::initialize() {
  69   CollectedHeap::pre_initialize();
  70 
  71   // Cannot be initialized until after the flags are parsed
  72   // GenerationSizer flag_parser;
  73   _collector_policy = new GenerationSizer();
  74 
  75   size_t yg_min_size = _collector_policy->min_young_gen_size();
  76   size_t yg_max_size = _collector_policy->max_young_gen_size();
  77   size_t og_min_size = _collector_policy->min_old_gen_size();
  78   size_t og_max_size = _collector_policy->max_old_gen_size();
  79 
  80   trace_gen_sizes("ps heap raw",
  81                   og_min_size, og_max_size,
  82                   yg_min_size, yg_max_size);
  83 
  84   const size_t og_page_sz = os::page_size_for_region(yg_min_size + og_min_size,
  85                                                      yg_max_size + og_max_size,
  86                                                      8);
  87 
  88   const size_t og_align = set_alignment(_old_gen_alignment,   og_page_sz);
  89   const size_t yg_align = set_alignment(_young_gen_alignment, og_page_sz);
  90 
  91   // Update sizes to reflect the selected page size(s).
  92   //
  93   // NEEDS_CLEANUP.  The default TwoGenerationCollectorPolicy uses NewRatio; it
  94   // should check UseAdaptiveSizePolicy.  Changes from generationSizer could
  95   // move to the common code.
  96   yg_min_size = align_size_up(yg_min_size, yg_align);
  97   yg_max_size = align_size_up(yg_max_size, yg_align);
  98   size_t yg_cur_size =
  99     align_size_up(_collector_policy->young_gen_size(), yg_align);
 100   yg_cur_size = MAX2(yg_cur_size, yg_min_size);
 101 
 102   og_min_size = align_size_up(og_min_size, og_align);
 103   // Align old gen size down to preserve specified heap size.
 104   assert(og_align == yg_align, "sanity");
 105   og_max_size = align_size_down(og_max_size, og_align);
 106   og_max_size = MAX2(og_max_size, og_min_size);
 107   size_t og_cur_size =
 108     align_size_down(_collector_policy->old_gen_size(), og_align);
 109   og_cur_size = MAX2(og_cur_size, og_min_size);
 110 
 111   trace_gen_sizes("ps heap rnd",
 112                   og_min_size, og_max_size,
 113                   yg_min_size, yg_max_size);
 114 
 115   const size_t heap_size = og_max_size + yg_max_size;
 116 
 117   ReservedSpace heap_rs = Universe::reserve_heap(heap_size, og_align);
 118 
 119   NMTTrackOp op(NMTTrackOp::TypeOp);
 120   op.execute_op((address)heap_rs.base(), 0, mtJavaHeap);
 121 
 122   os::trace_page_sizes("ps main", og_min_size + yg_min_size,
 123                        og_max_size + yg_max_size, og_page_sz,
 124                        heap_rs.base(),
 125                        heap_rs.size());
 126   if (!heap_rs.is_reserved()) {
 127     vm_shutdown_during_initialization(
 128       "Could not reserve enough space for object heap");
 129     return JNI_ENOMEM;
 130   }
 131 
 132   _reserved = MemRegion((HeapWord*)heap_rs.base(),
 133                         (HeapWord*)(heap_rs.base() + heap_rs.size()));
 134 
 135   CardTableExtension* const barrier_set = new CardTableExtension(_reserved, 3);
 136   _barrier_set = barrier_set;
 137   oopDesc::set_bs(_barrier_set);
 138   if (_barrier_set == NULL) {
 139     vm_shutdown_during_initialization(
 140       "Could not reserve enough space for barrier set");
 141     return JNI_ENOMEM;
 142   }
 143 
 144   // Initial young gen size is 4 Mb
 145   //
 146   // XXX - what about flag_parser.young_gen_size()?
 147   const size_t init_young_size = align_size_up(4 * M, yg_align);
 148   yg_cur_size = MAX2(MIN2(init_young_size, yg_max_size), yg_cur_size);
 149 
 150   // Make up the generations
 151   // Calculate the maximum size that a generation can grow.  This
 152   // includes growth into the other generation.  Note that the
 153   // parameter _max_gen_size is kept as the maximum
 154   // size of the generation as the boundaries currently stand.
 155   // _max_gen_size is still used as that value.
 156   double max_gc_pause_sec = ((double) MaxGCPauseMillis)/1000.0;
 157   double max_gc_minor_pause_sec = ((double) MaxGCMinorPauseMillis)/1000.0;
 158 
 159   _gens = new AdjoiningGenerations(heap_rs,
 160                                    og_cur_size,
 161                                    og_min_size,
 162                                    og_max_size,
 163                                    yg_cur_size,
 164                                    yg_min_size,
 165                                    yg_max_size,
 166                                    yg_align);
 167 
 168   _old_gen = _gens->old_gen();
 169   _young_gen = _gens->young_gen();
 170 
 171   const size_t eden_capacity = _young_gen->eden_space()->capacity_in_bytes();
 172   const size_t old_capacity = _old_gen->capacity_in_bytes();
 173   const size_t initial_promo_size = MIN2(eden_capacity, old_capacity);
 174   _size_policy =
 175     new PSAdaptiveSizePolicy(eden_capacity,
 176                              initial_promo_size,
 177                              young_gen()->to_space()->capacity_in_bytes(),
 178                              intra_heap_alignment(),
 179                              max_gc_pause_sec,
 180                              max_gc_minor_pause_sec,
 181                              GCTimeRatio
 182                              );
 183 
 184   assert(!UseAdaptiveGCBoundary ||
 185     (old_gen()->virtual_space()->high_boundary() ==
 186      young_gen()->virtual_space()->low_boundary()),
 187     "Boundaries must meet");
 188   // initialize the policy counters - 2 collectors, 3 generations
 189   _gc_policy_counters =
 190     new PSGCAdaptivePolicyCounters("ParScav:MSC", 2, 3, _size_policy);
 191   _psh = this;
 192 
 193   // Set up the GCTaskManager
 194   _gc_task_manager = GCTaskManager::create(ParallelGCThreads);
 195 
 196   if (UseParallelOldGC && !PSParallelCompact::initialize()) {
 197     return JNI_ENOMEM;
 198   }
 199 
 200   return JNI_OK;
 201 }
 202 
 203 void ParallelScavengeHeap::post_initialize() {
 204   // Need to init the tenuring threshold
 205   PSScavenge::initialize();
 206   if (UseParallelOldGC) {
 207     PSParallelCompact::post_initialize();
 208   } else {
 209     PSMarkSweep::initialize();
 210   }
 211   PSPromotionManager::initialize();
 212 }
 213 
 214 void ParallelScavengeHeap::update_counters() {
 215   young_gen()->update_counters();
 216   old_gen()->update_counters();
 217   MetaspaceCounters::update_performance_counters();
 218 }
 219 
 220 size_t ParallelScavengeHeap::capacity() const {
 221   size_t value = young_gen()->capacity_in_bytes() + old_gen()->capacity_in_bytes();
 222   return value;
 223 }
 224 
 225 size_t ParallelScavengeHeap::used() const {
 226   size_t value = young_gen()->used_in_bytes() + old_gen()->used_in_bytes();
 227   return value;
 228 }
 229 
 230 bool ParallelScavengeHeap::is_maximal_no_gc() const {
 231   return old_gen()->is_maximal_no_gc() && young_gen()->is_maximal_no_gc();
 232 }
 233 
 234 
 235 size_t ParallelScavengeHeap::max_capacity() const {
 236   size_t estimated = reserved_region().byte_size();
 237   if (UseAdaptiveSizePolicy) {
 238     estimated -= _size_policy->max_survivor_size(young_gen()->max_size());
 239   } else {
 240     estimated -= young_gen()->to_space()->capacity_in_bytes();
 241   }
 242   return MAX2(estimated, capacity());
 243 }
 244 
 245 bool ParallelScavengeHeap::is_in(const void* p) const {
 246   if (young_gen()->is_in(p)) {
 247     return true;
 248   }
 249 
 250   if (old_gen()->is_in(p)) {
 251     return true;
 252   }
 253 
 254   return false;
 255 }
 256 
 257 bool ParallelScavengeHeap::is_in_reserved(const void* p) const {
 258   if (young_gen()->is_in_reserved(p)) {
 259     return true;
 260   }
 261 
 262   if (old_gen()->is_in_reserved(p)) {
 263     return true;
 264   }
 265 
 266   return false;
 267 }
 268 
 269 bool ParallelScavengeHeap::is_scavengable(const void* addr) {
 270   return is_in_young((oop)addr);
 271 }
 272 
 273 #ifdef ASSERT
 274 // Don't implement this by using is_in_young().  This method is used
 275 // in some cases to check that is_in_young() is correct.
 276 bool ParallelScavengeHeap::is_in_partial_collection(const void *p) {
 277   assert(is_in_reserved(p) || p == NULL,
 278     "Does not work if address is non-null and outside of the heap");
 279   // The order of the generations is old (low addr), young (high addr)
 280   return p >= old_gen()->reserved().end();
 281 }
 282 #endif
 283 
 284 // There are two levels of allocation policy here.
 285 //
 286 // When an allocation request fails, the requesting thread must invoke a VM
 287 // operation, transfer control to the VM thread, and await the results of a
 288 // garbage collection. That is quite expensive, and we should avoid doing it
 289 // multiple times if possible.
 290 //
 291 // To accomplish this, we have a basic allocation policy, and also a
 292 // failed allocation policy.
 293 //
 294 // The basic allocation policy controls how you allocate memory without
 295 // attempting garbage collection. It is okay to grab locks and
 296 // expand the heap, if that can be done without coming to a safepoint.
 297 // It is likely that the basic allocation policy will not be very
 298 // aggressive.
 299 //
 300 // The failed allocation policy is invoked from the VM thread after
 301 // the basic allocation policy is unable to satisfy a mem_allocate
 302 // request. This policy needs to cover the entire range of collection,
 303 // heap expansion, and out-of-memory conditions. It should make every
 304 // attempt to allocate the requested memory.
 305 
 306 // Basic allocation policy. Should never be called at a safepoint, or
 307 // from the VM thread.
 308 //
 309 // This method must handle cases where many mem_allocate requests fail
 310 // simultaneously. When that happens, only one VM operation will succeed,
 311 // and the rest will not be executed. For that reason, this method loops
 312 // during failed allocation attempts. If the java heap becomes exhausted,
 313 // we rely on the size_policy object to force a bail out.
 314 HeapWord* ParallelScavengeHeap::mem_allocate(
 315                                      size_t size,
 316                                      bool* gc_overhead_limit_was_exceeded) {
 317   assert(!SafepointSynchronize::is_at_safepoint(), "should not be at safepoint");
 318   assert(Thread::current() != (Thread*)VMThread::vm_thread(), "should not be in vm thread");
 319   assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
 320 
 321   // In general gc_overhead_limit_was_exceeded should be false so
 322   // set it so here and reset it to true only if the gc time
 323   // limit is being exceeded as checked below.
 324   *gc_overhead_limit_was_exceeded = false;
 325 
 326   HeapWord* result = young_gen()->allocate(size);
 327 
 328   uint loop_count = 0;
 329   uint gc_count = 0;
 330   int gclocker_stalled_count = 0;
 331 
 332   while (result == NULL) {
 333     // We don't want to have multiple collections for a single filled generation.
 334     // To prevent this, each thread tracks the total_collections() value, and if
 335     // the count has changed, does not do a new collection.
 336     //
 337     // The collection count must be read only while holding the heap lock. VM
 338     // operations also hold the heap lock during collections. There is a lock
 339     // contention case where thread A blocks waiting on the Heap_lock, while
 340     // thread B is holding it doing a collection. When thread A gets the lock,
 341     // the collection count has already changed. To prevent duplicate collections,
 342     // The policy MUST attempt allocations during the same period it reads the
 343     // total_collections() value!
 344     {
 345       MutexLocker ml(Heap_lock);
 346       gc_count = Universe::heap()->total_collections();
 347 
 348       result = young_gen()->allocate(size);
 349       if (result != NULL) {
 350         return result;
 351       }
 352 
 353       // If certain conditions hold, try allocating from the old gen.
 354       result = mem_allocate_old_gen(size);
 355       if (result != NULL) {
 356         return result;
 357       }
 358 
 359       if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
 360         return NULL;
 361       }
 362 
 363       // Failed to allocate without a gc.
 364       if (GC_locker::is_active_and_needs_gc()) {
 365         // If this thread is not in a jni critical section, we stall
 366         // the requestor until the critical section has cleared and
 367         // GC allowed. When the critical section clears, a GC is
 368         // initiated by the last thread exiting the critical section; so
 369         // we retry the allocation sequence from the beginning of the loop,
 370         // rather than causing more, now probably unnecessary, GC attempts.
 371         JavaThread* jthr = JavaThread::current();
 372         if (!jthr->in_critical()) {
 373           MutexUnlocker mul(Heap_lock);
 374           GC_locker::stall_until_clear();
 375           gclocker_stalled_count += 1;
 376           continue;
 377         } else {
 378           if (CheckJNICalls) {
 379             fatal("Possible deadlock due to allocating while"
 380                   " in jni critical section");
 381           }
 382           return NULL;
 383         }
 384       }
 385     }
 386 
 387     if (result == NULL) {
 388       // Generate a VM operation
 389       VM_ParallelGCFailedAllocation op(size, gc_count);
 390       VMThread::execute(&op);
 391 
 392       // Did the VM operation execute? If so, return the result directly.
 393       // This prevents us from looping until time out on requests that can
 394       // not be satisfied.
 395       if (op.prologue_succeeded()) {
 396         assert(Universe::heap()->is_in_or_null(op.result()),
 397           "result not in heap");
 398 
 399         // If GC was locked out during VM operation then retry allocation
 400         // and/or stall as necessary.
 401         if (op.gc_locked()) {
 402           assert(op.result() == NULL, "must be NULL if gc_locked() is true");
 403           continue;  // retry and/or stall as necessary
 404         }
 405 
 406         // Exit the loop if the gc time limit has been exceeded.
 407         // The allocation must have failed above ("result" guarding
 408         // this path is NULL) and the most recent collection has exceeded the
 409         // gc overhead limit (although enough may have been collected to
 410         // satisfy the allocation).  Exit the loop so that an out-of-memory
 411         // will be thrown (return a NULL ignoring the contents of
 412         // op.result()),
 413         // but clear gc_overhead_limit_exceeded so that the next collection
 414         // starts with a clean slate (i.e., forgets about previous overhead
 415         // excesses).  Fill op.result() with a filler object so that the
 416         // heap remains parsable.
 417         const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
 418         const bool softrefs_clear = collector_policy()->all_soft_refs_clear();
 419 
 420         if (limit_exceeded && softrefs_clear) {
 421           *gc_overhead_limit_was_exceeded = true;
 422           size_policy()->set_gc_overhead_limit_exceeded(false);
 423           if (PrintGCDetails && Verbose) {
 424             gclog_or_tty->print_cr("ParallelScavengeHeap::mem_allocate: "
 425               "return NULL because gc_overhead_limit_exceeded is set");
 426           }
 427           if (op.result() != NULL) {
 428             CollectedHeap::fill_with_object(op.result(), size);
 429           }
 430           return NULL;
 431         }
 432 
 433         return op.result();
 434       }
 435     }
 436 
 437     // The policy object will prevent us from looping forever. If the
 438     // time spent in gc crosses a threshold, we will bail out.
 439     loop_count++;
 440     if ((result == NULL) && (QueuedAllocationWarningCount > 0) &&
 441         (loop_count % QueuedAllocationWarningCount == 0)) {
 442       warning("ParallelScavengeHeap::mem_allocate retries %d times \n\t"
 443               " size=%d", loop_count, size);
 444     }
 445   }
 446 
 447   return result;
 448 }
 449 
 450 // A "death march" is a series of ultra-slow allocations in which a full gc is
 451 // done before each allocation, and after the full gc the allocation still
 452 // cannot be satisfied from the young gen.  This routine detects that condition;
 453 // it should be called after a full gc has been done and the allocation
 454 // attempted from the young gen. The parameter 'addr' should be the result of
 455 // that young gen allocation attempt.
 456 void
 457 ParallelScavengeHeap::death_march_check(HeapWord* const addr, size_t size) {
 458   if (addr != NULL) {
 459     _death_march_count = 0;  // death march has ended
 460   } else if (_death_march_count == 0) {
 461     if (should_alloc_in_eden(size)) {
 462       _death_march_count = 1;    // death march has started
 463     }
 464   }
 465 }
 466 
 467 HeapWord* ParallelScavengeHeap::mem_allocate_old_gen(size_t size) {
 468   if (!should_alloc_in_eden(size) || GC_locker::is_active_and_needs_gc()) {
 469     // Size is too big for eden, or gc is locked out.
 470     return old_gen()->allocate(size);
 471   }
 472 
 473   // If a "death march" is in progress, allocate from the old gen a limited
 474   // number of times before doing a GC.
 475   if (_death_march_count > 0) {
 476     if (_death_march_count < 64) {
 477       ++_death_march_count;
 478       return old_gen()->allocate(size);
 479     } else {
 480       _death_march_count = 0;
 481     }
 482   }
 483   return NULL;
 484 }
 485 
 486 void ParallelScavengeHeap::do_full_collection(bool clear_all_soft_refs) {
 487   if (UseParallelOldGC) {
 488     // The do_full_collection() parameter clear_all_soft_refs
 489     // is interpreted here as maximum_compaction which will
 490     // cause SoftRefs to be cleared.
 491     bool maximum_compaction = clear_all_soft_refs;
 492     PSParallelCompact::invoke(maximum_compaction);
 493   } else {
 494     PSMarkSweep::invoke(clear_all_soft_refs);
 495   }
 496 }
 497 
 498 // Failed allocation policy. Must be called from the VM thread, and
 499 // only at a safepoint! Note that this method has policy for allocation
 500 // flow, and NOT collection policy. So we do not check for gc collection
 501 // time over limit here, that is the responsibility of the heap specific
 502 // collection methods. This method decides where to attempt allocations,
 503 // and when to attempt collections, but no collection specific policy.
 504 HeapWord* ParallelScavengeHeap::failed_mem_allocate(size_t size) {
 505   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 506   assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 507   assert(!Universe::heap()->is_gc_active(), "not reentrant");
 508   assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
 509 
 510   // We assume that allocation in eden will fail unless we collect.
 511 
 512   // First level allocation failure, scavenge and allocate in young gen.
 513   GCCauseSetter gccs(this, GCCause::_allocation_failure);
 514   const bool invoked_full_gc = PSScavenge::invoke();
 515   HeapWord* result = young_gen()->allocate(size);
 516 
 517   // Second level allocation failure.
 518   //   Mark sweep and allocate in young generation.
 519   if (result == NULL && !invoked_full_gc) {
 520     do_full_collection(false);
 521     result = young_gen()->allocate(size);
 522   }
 523 
 524   death_march_check(result, size);
 525 
 526   // Third level allocation failure.
 527   //   After mark sweep and young generation allocation failure,
 528   //   allocate in old generation.
 529   if (result == NULL) {
 530     result = old_gen()->allocate(size);
 531   }
 532 
 533   // Fourth level allocation failure. We're running out of memory.
 534   //   More complete mark sweep and allocate in young generation.
 535   if (result == NULL) {
 536     do_full_collection(true);
 537     result = young_gen()->allocate(size);
 538   }
 539 
 540   // Fifth level allocation failure.
 541   //   After more complete mark sweep, allocate in old generation.
 542   if (result == NULL) {
 543     result = old_gen()->allocate(size);
 544   }
 545 
 546   return result;
 547 }
 548 
 549 void ParallelScavengeHeap::ensure_parsability(bool retire_tlabs) {
 550   CollectedHeap::ensure_parsability(retire_tlabs);
 551   young_gen()->eden_space()->ensure_parsability();
 552 }
 553 
 554 size_t ParallelScavengeHeap::unsafe_max_alloc() {
 555   return young_gen()->eden_space()->free_in_bytes();
 556 }
 557 
 558 size_t ParallelScavengeHeap::tlab_capacity(Thread* thr) const {
 559   return young_gen()->eden_space()->tlab_capacity(thr);
 560 }
 561 
 562 size_t ParallelScavengeHeap::unsafe_max_tlab_alloc(Thread* thr) const {
 563   return young_gen()->eden_space()->unsafe_max_tlab_alloc(thr);
 564 }
 565 
 566 HeapWord* ParallelScavengeHeap::allocate_new_tlab(size_t size) {
 567   return young_gen()->allocate(size);
 568 }
 569 
 570 void ParallelScavengeHeap::accumulate_statistics_all_tlabs() {
 571   CollectedHeap::accumulate_statistics_all_tlabs();
 572 }
 573 
 574 void ParallelScavengeHeap::resize_all_tlabs() {
 575   CollectedHeap::resize_all_tlabs();
 576 }
 577 
 578 bool ParallelScavengeHeap::can_elide_initializing_store_barrier(oop new_obj) {
 579   // We don't need barriers for stores to objects in the
 580   // young gen and, a fortiori, for initializing stores to
 581   // objects therein.
 582   return is_in_young(new_obj);
 583 }
 584 
 585 // This method is used by System.gc() and JVMTI.
 586 void ParallelScavengeHeap::collect(GCCause::Cause cause) {
 587   assert(!Heap_lock->owned_by_self(),
 588     "this thread should not own the Heap_lock");
 589 
 590   unsigned int gc_count      = 0;
 591   unsigned int full_gc_count = 0;
 592   {
 593     MutexLocker ml(Heap_lock);
 594     // This value is guarded by the Heap_lock
 595     gc_count      = Universe::heap()->total_collections();
 596     full_gc_count = Universe::heap()->total_full_collections();
 597   }
 598 
 599   VM_ParallelGCSystemGC op(gc_count, full_gc_count, cause);
 600   VMThread::execute(&op);
 601 }
 602 
 603 void ParallelScavengeHeap::oop_iterate(ExtendedOopClosure* cl) {
 604   Unimplemented();
 605 }
 606 
 607 void ParallelScavengeHeap::object_iterate(ObjectClosure* cl) {
 608   young_gen()->object_iterate(cl);
 609   old_gen()->object_iterate(cl);
 610 }
 611 
 612 
 613 HeapWord* ParallelScavengeHeap::block_start(const void* addr) const {
 614   if (young_gen()->is_in_reserved(addr)) {
 615     assert(young_gen()->is_in(addr),
 616            "addr should be in allocated part of young gen");
 617     // called from os::print_location by find or VMError
 618     if (Debugging || VMError::fatal_error_in_progress())  return NULL;
 619     Unimplemented();
 620   } else if (old_gen()->is_in_reserved(addr)) {
 621     assert(old_gen()->is_in(addr),
 622            "addr should be in allocated part of old gen");
 623     return old_gen()->start_array()->object_start((HeapWord*)addr);
 624   }
 625   return 0;
 626 }
 627 
 628 size_t ParallelScavengeHeap::block_size(const HeapWord* addr) const {
 629   return oop(addr)->size();
 630 }
 631 
 632 bool ParallelScavengeHeap::block_is_obj(const HeapWord* addr) const {
 633   return block_start(addr) == addr;
 634 }
 635 
 636 jlong ParallelScavengeHeap::millis_since_last_gc() {
 637   return UseParallelOldGC ?
 638     PSParallelCompact::millis_since_last_gc() :
 639     PSMarkSweep::millis_since_last_gc();
 640 }
 641 
 642 void ParallelScavengeHeap::prepare_for_verify() {
 643   ensure_parsability(false);  // no need to retire TLABs for verification
 644 }
 645 
 646 void ParallelScavengeHeap::print_on(outputStream* st) const {
 647   young_gen()->print_on(st);
 648   old_gen()->print_on(st);
 649   MetaspaceAux::print_on(st);
 650 }
 651 
 652 void ParallelScavengeHeap::print_on_error(outputStream* st) const {
 653   this->CollectedHeap::print_on_error(st);
 654 
 655   if (UseParallelOldGC) {
 656     st->cr();
 657     PSParallelCompact::print_on_error(st);
 658   }
 659 }
 660 
 661 void ParallelScavengeHeap::gc_threads_do(ThreadClosure* tc) const {
 662   PSScavenge::gc_task_manager()->threads_do(tc);
 663 }
 664 
 665 void ParallelScavengeHeap::print_gc_threads_on(outputStream* st) const {
 666   PSScavenge::gc_task_manager()->print_threads_on(st);
 667 }
 668 
 669 void ParallelScavengeHeap::print_tracing_info() const {
 670   if (TraceGen0Time) {
 671     double time = PSScavenge::accumulated_time()->seconds();
 672     tty->print_cr("[Accumulated GC generation 0 time %3.7f secs]", time);
 673   }
 674   if (TraceGen1Time) {
 675     double time = UseParallelOldGC ? PSParallelCompact::accumulated_time()->seconds() : PSMarkSweep::accumulated_time()->seconds();
 676     tty->print_cr("[Accumulated GC generation 1 time %3.7f secs]", time);
 677   }
 678 }
 679 
 680 
 681 void ParallelScavengeHeap::verify(bool silent, VerifyOption option /* ignored */) {
 682   // Why do we need the total_collections()-filter below?
 683   if (total_collections() > 0) {
 684     if (!silent) {
 685       gclog_or_tty->print("tenured ");
 686     }
 687     old_gen()->verify();
 688 
 689     if (!silent) {
 690       gclog_or_tty->print("eden ");
 691     }
 692     young_gen()->verify();
 693   }
 694 }
 695 
 696 void ParallelScavengeHeap::print_heap_change(size_t prev_used) {
 697   if (PrintGCDetails && Verbose) {
 698     gclog_or_tty->print(" "  SIZE_FORMAT
 699                         "->" SIZE_FORMAT
 700                         "("  SIZE_FORMAT ")",
 701                         prev_used, used(), capacity());
 702   } else {
 703     gclog_or_tty->print(" "  SIZE_FORMAT "K"
 704                         "->" SIZE_FORMAT "K"
 705                         "("  SIZE_FORMAT "K)",
 706                         prev_used / K, used() / K, capacity() / K);
 707   }
 708 }
 709 
 710 ParallelScavengeHeap* ParallelScavengeHeap::heap() {
 711   assert(_psh != NULL, "Uninitialized access to ParallelScavengeHeap::heap()");
 712   assert(_psh->kind() == CollectedHeap::ParallelScavengeHeap, "not a parallel scavenge heap");
 713   return _psh;
 714 }
 715 
 716 // Before delegating the resize to the young generation,
 717 // the reserved space for the young and old generations
 718 // may be changed to accomodate the desired resize.
 719 void ParallelScavengeHeap::resize_young_gen(size_t eden_size,
 720     size_t survivor_size) {
 721   if (UseAdaptiveGCBoundary) {
 722     if (size_policy()->bytes_absorbed_from_eden() != 0) {
 723       size_policy()->reset_bytes_absorbed_from_eden();
 724       return;  // The generation changed size already.
 725     }
 726     gens()->adjust_boundary_for_young_gen_needs(eden_size, survivor_size);
 727   }
 728 
 729   // Delegate the resize to the generation.
 730   _young_gen->resize(eden_size, survivor_size);
 731 }
 732 
 733 // Before delegating the resize to the old generation,
 734 // the reserved space for the young and old generations
 735 // may be changed to accomodate the desired resize.
 736 void ParallelScavengeHeap::resize_old_gen(size_t desired_free_space) {
 737   if (UseAdaptiveGCBoundary) {
 738     if (size_policy()->bytes_absorbed_from_eden() != 0) {
 739       size_policy()->reset_bytes_absorbed_from_eden();
 740       return;  // The generation changed size already.
 741     }
 742     gens()->adjust_boundary_for_old_gen_needs(desired_free_space);
 743   }
 744 
 745   // Delegate the resize to the generation.
 746   _old_gen->resize(desired_free_space);
 747 }
 748 
 749 ParallelScavengeHeap::ParStrongRootsScope::ParStrongRootsScope() {
 750   // nothing particular
 751 }
 752 
 753 ParallelScavengeHeap::ParStrongRootsScope::~ParStrongRootsScope() {
 754   // nothing particular
 755 }
 756 
 757 #ifndef PRODUCT
 758 void ParallelScavengeHeap::record_gen_tops_before_GC() {
 759   if (ZapUnusedHeapArea) {
 760     young_gen()->record_spaces_top();
 761     old_gen()->record_spaces_top();
 762   }
 763 }
 764 
 765 void ParallelScavengeHeap::gen_mangle_unused_area() {
 766   if (ZapUnusedHeapArea) {
 767     young_gen()->eden_space()->mangle_unused_area();
 768     young_gen()->to_space()->mangle_unused_area();
 769     young_gen()->from_space()->mangle_unused_area();
 770     old_gen()->object_space()->mangle_unused_area();
 771   }
 772 }
 773 #endif