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

Fri, 31 May 2013 14:32:44 +0200

author
stefank
date
Fri, 31 May 2013 14:32:44 +0200
changeset 5515
9766f73e770d
parent 5237
f2110083203d
child 5531
1a8fb39bdbc4
permissions
-rw-r--r--

8022880: False sharing between PSPromotionManager instances
Summary: Pad the PSPromotionManager instances in the manager array.
Reviewed-by: brutisso, jmasa

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

mercurial