src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.cpp

Tue, 04 Feb 2020 18:13:14 +0800

author
aoqi
date
Tue, 04 Feb 2020 18:13:14 +0800
changeset 9806
758c07667682
parent 7994
04ff2f6cd0eb
parent 9787
9f28a4cac6d9
permissions
-rw-r--r--

Merge

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

mercurial