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

Sat, 19 Jul 2008 17:38:22 -0400

author
coleenp
date
Sat, 19 Jul 2008 17:38:22 -0400
changeset 672
1fdb98a17101
parent 435
a61af66fc99e
child 698
12eea04c8b06
permissions
-rw-r--r--

6716785: implicit null checks not triggering with CompressedOops
Summary: allocate alignment-sized page(s) below java heap so that memory accesses at heap_base+1page give signal and cause an implicit null check
Reviewed-by: kvn, jmasa, phh, jcoomes

duke@435 1 /*
duke@435 2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_psOldGen.cpp.incl"
duke@435 27
duke@435 28 inline const char* PSOldGen::select_name() {
duke@435 29 return UseParallelOldGC ? "ParOldGen" : "PSOldGen";
duke@435 30 }
duke@435 31
duke@435 32 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment,
duke@435 33 size_t initial_size, size_t min_size, size_t max_size,
duke@435 34 const char* perf_data_name, int level):
duke@435 35 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
duke@435 36 _max_gen_size(max_size)
duke@435 37 {
duke@435 38 initialize(rs, alignment, perf_data_name, level);
duke@435 39 }
duke@435 40
duke@435 41 PSOldGen::PSOldGen(size_t initial_size,
duke@435 42 size_t min_size, size_t max_size,
duke@435 43 const char* perf_data_name, int level):
duke@435 44 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
duke@435 45 _max_gen_size(max_size)
duke@435 46 {}
duke@435 47
duke@435 48 void PSOldGen::initialize(ReservedSpace rs, size_t alignment,
duke@435 49 const char* perf_data_name, int level) {
duke@435 50 initialize_virtual_space(rs, alignment);
duke@435 51 initialize_work(perf_data_name, level);
duke@435 52 // The old gen can grow to gen_size_limit(). _reserve reflects only
duke@435 53 // the current maximum that can be committed.
duke@435 54 assert(_reserved.byte_size() <= gen_size_limit(), "Consistency check");
duke@435 55 }
duke@435 56
duke@435 57 void PSOldGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) {
duke@435 58
duke@435 59 _virtual_space = new PSVirtualSpace(rs, alignment);
duke@435 60 if (!_virtual_space->expand_by(_init_gen_size)) {
duke@435 61 vm_exit_during_initialization("Could not reserve enough space for "
duke@435 62 "object heap");
duke@435 63 }
duke@435 64 }
duke@435 65
duke@435 66 void PSOldGen::initialize_work(const char* perf_data_name, int level) {
duke@435 67 //
duke@435 68 // Basic memory initialization
duke@435 69 //
duke@435 70
duke@435 71 MemRegion limit_reserved((HeapWord*)virtual_space()->low_boundary(),
duke@435 72 heap_word_size(_max_gen_size));
duke@435 73 assert(limit_reserved.byte_size() == _max_gen_size,
duke@435 74 "word vs bytes confusion");
duke@435 75 //
duke@435 76 // Object start stuff
duke@435 77 //
duke@435 78
duke@435 79 start_array()->initialize(limit_reserved);
duke@435 80
duke@435 81 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
duke@435 82 (HeapWord*)virtual_space()->high_boundary());
duke@435 83
duke@435 84 //
duke@435 85 // Card table stuff
duke@435 86 //
duke@435 87
duke@435 88 MemRegion cmr((HeapWord*)virtual_space()->low(),
duke@435 89 (HeapWord*)virtual_space()->high());
duke@435 90 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@435 91
duke@435 92 CardTableModRefBS* _ct = (CardTableModRefBS*)Universe::heap()->barrier_set();
duke@435 93 assert (_ct->kind() == BarrierSet::CardTableModRef, "Sanity");
duke@435 94
duke@435 95 // Verify that the start and end of this generation is the start of a card.
duke@435 96 // If this wasn't true, a single card could span more than one generation,
duke@435 97 // which would cause problems when we commit/uncommit memory, and when we
duke@435 98 // clear and dirty cards.
duke@435 99 guarantee(_ct->is_card_aligned(_reserved.start()), "generation must be card aligned");
duke@435 100 if (_reserved.end() != Universe::heap()->reserved_region().end()) {
duke@435 101 // Don't check at the very end of the heap as we'll assert that we're probing off
duke@435 102 // the end if we try.
duke@435 103 guarantee(_ct->is_card_aligned(_reserved.end()), "generation must be card aligned");
duke@435 104 }
duke@435 105
duke@435 106 //
duke@435 107 // ObjectSpace stuff
duke@435 108 //
duke@435 109
duke@435 110 _object_space = new MutableSpace();
duke@435 111
duke@435 112 if (_object_space == NULL)
duke@435 113 vm_exit_during_initialization("Could not allocate an old gen space");
duke@435 114
duke@435 115 object_space()->initialize(cmr, true);
duke@435 116
duke@435 117 _object_mark_sweep = new PSMarkSweepDecorator(_object_space, start_array(), MarkSweepDeadRatio);
duke@435 118
duke@435 119 if (_object_mark_sweep == NULL)
duke@435 120 vm_exit_during_initialization("Could not complete allocation of old generation");
duke@435 121
duke@435 122 // Update the start_array
duke@435 123 start_array()->set_covered_region(cmr);
duke@435 124
duke@435 125 // Generation Counters, generation 'level', 1 subspace
duke@435 126 _gen_counters = new PSGenerationCounters(perf_data_name, level, 1,
duke@435 127 virtual_space());
duke@435 128 _space_counters = new SpaceCounters(perf_data_name, 0,
duke@435 129 virtual_space()->reserved_size(),
duke@435 130 _object_space, _gen_counters);
duke@435 131 }
duke@435 132
duke@435 133 // Assume that the generation has been allocated if its
duke@435 134 // reserved size is not 0.
duke@435 135 bool PSOldGen::is_allocated() {
duke@435 136 return virtual_space()->reserved_size() != 0;
duke@435 137 }
duke@435 138
duke@435 139 void PSOldGen::precompact() {
duke@435 140 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 141 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 142
duke@435 143 // Reset start array first.
duke@435 144 debug_only(if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {)
duke@435 145 start_array()->reset();
duke@435 146 debug_only(})
duke@435 147
duke@435 148 object_mark_sweep()->precompact();
duke@435 149
duke@435 150 // Now compact the young gen
duke@435 151 heap->young_gen()->precompact();
duke@435 152 }
duke@435 153
duke@435 154 void PSOldGen::adjust_pointers() {
duke@435 155 object_mark_sweep()->adjust_pointers();
duke@435 156 }
duke@435 157
duke@435 158 void PSOldGen::compact() {
duke@435 159 object_mark_sweep()->compact(ZapUnusedHeapArea);
duke@435 160 }
duke@435 161
duke@435 162 void PSOldGen::move_and_update(ParCompactionManager* cm) {
duke@435 163 PSParallelCompact::move_and_update(cm, PSParallelCompact::old_space_id);
duke@435 164 }
duke@435 165
duke@435 166 size_t PSOldGen::contiguous_available() const {
duke@435 167 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size();
duke@435 168 }
duke@435 169
duke@435 170 // Allocation. We report all successful allocations to the size policy
duke@435 171 // Note that the perm gen does not use this method, and should not!
duke@435 172 HeapWord* PSOldGen::allocate(size_t word_size, bool is_tlab) {
duke@435 173 assert_locked_or_safepoint(Heap_lock);
duke@435 174 HeapWord* res = allocate_noexpand(word_size, is_tlab);
duke@435 175
duke@435 176 if (res == NULL) {
duke@435 177 res = expand_and_allocate(word_size, is_tlab);
duke@435 178 }
duke@435 179
duke@435 180 // Allocations in the old generation need to be reported
duke@435 181 if (res != NULL) {
duke@435 182 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 183 heap->size_policy()->tenured_allocation(word_size);
duke@435 184 }
duke@435 185
duke@435 186 return res;
duke@435 187 }
duke@435 188
duke@435 189 HeapWord* PSOldGen::expand_and_allocate(size_t word_size, bool is_tlab) {
duke@435 190 assert(!is_tlab, "TLAB's are not supported in PSOldGen");
duke@435 191 expand(word_size*HeapWordSize);
duke@435 192 if (GCExpandToAllocateDelayMillis > 0) {
duke@435 193 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
duke@435 194 }
duke@435 195 return allocate_noexpand(word_size, is_tlab);
duke@435 196 }
duke@435 197
duke@435 198 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) {
duke@435 199 expand(word_size*HeapWordSize);
duke@435 200 if (GCExpandToAllocateDelayMillis > 0) {
duke@435 201 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
duke@435 202 }
duke@435 203 return cas_allocate_noexpand(word_size);
duke@435 204 }
duke@435 205
duke@435 206 void PSOldGen::expand(size_t bytes) {
duke@435 207 MutexLocker x(ExpandHeap_lock);
duke@435 208 const size_t alignment = virtual_space()->alignment();
duke@435 209 size_t aligned_bytes = align_size_up(bytes, alignment);
duke@435 210 size_t aligned_expand_bytes = align_size_up(MinHeapDeltaBytes, alignment);
duke@435 211
duke@435 212 bool success = false;
duke@435 213 if (aligned_expand_bytes > aligned_bytes) {
duke@435 214 success = expand_by(aligned_expand_bytes);
duke@435 215 }
duke@435 216 if (!success) {
duke@435 217 success = expand_by(aligned_bytes);
duke@435 218 }
duke@435 219 if (!success) {
duke@435 220 success = expand_to_reserved();
duke@435 221 }
duke@435 222
duke@435 223 if (GC_locker::is_active()) {
duke@435 224 if (PrintGC && Verbose) {
duke@435 225 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
duke@435 226 }
duke@435 227 }
duke@435 228 }
duke@435 229
duke@435 230 bool PSOldGen::expand_by(size_t bytes) {
duke@435 231 assert_lock_strong(ExpandHeap_lock);
duke@435 232 assert_locked_or_safepoint(Heap_lock);
duke@435 233 bool result = virtual_space()->expand_by(bytes);
duke@435 234 if (result) {
duke@435 235 post_resize();
duke@435 236 if (UsePerfData) {
duke@435 237 _space_counters->update_capacity();
duke@435 238 _gen_counters->update_all();
duke@435 239 }
duke@435 240 }
duke@435 241
duke@435 242 if (result && Verbose && PrintGC) {
duke@435 243 size_t new_mem_size = virtual_space()->committed_size();
duke@435 244 size_t old_mem_size = new_mem_size - bytes;
duke@435 245 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
duke@435 246 SIZE_FORMAT "K to "
duke@435 247 SIZE_FORMAT "K",
duke@435 248 name(), old_mem_size/K, bytes/K, new_mem_size/K);
duke@435 249 }
duke@435 250
duke@435 251 return result;
duke@435 252 }
duke@435 253
duke@435 254 bool PSOldGen::expand_to_reserved() {
duke@435 255 assert_lock_strong(ExpandHeap_lock);
duke@435 256 assert_locked_or_safepoint(Heap_lock);
duke@435 257
duke@435 258 bool result = true;
duke@435 259 const size_t remaining_bytes = virtual_space()->uncommitted_size();
duke@435 260 if (remaining_bytes > 0) {
duke@435 261 result = expand_by(remaining_bytes);
duke@435 262 DEBUG_ONLY(if (!result) warning("grow to reserve failed"));
duke@435 263 }
duke@435 264 return result;
duke@435 265 }
duke@435 266
duke@435 267 void PSOldGen::shrink(size_t bytes) {
duke@435 268 assert_lock_strong(ExpandHeap_lock);
duke@435 269 assert_locked_or_safepoint(Heap_lock);
duke@435 270
duke@435 271 size_t size = align_size_down(bytes, virtual_space()->alignment());
duke@435 272 if (size > 0) {
duke@435 273 assert_lock_strong(ExpandHeap_lock);
duke@435 274 virtual_space()->shrink_by(bytes);
duke@435 275 post_resize();
duke@435 276
duke@435 277 if (Verbose && PrintGC) {
duke@435 278 size_t new_mem_size = virtual_space()->committed_size();
duke@435 279 size_t old_mem_size = new_mem_size + bytes;
duke@435 280 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by "
duke@435 281 SIZE_FORMAT "K to "
duke@435 282 SIZE_FORMAT "K",
duke@435 283 name(), old_mem_size/K, bytes/K, new_mem_size/K);
duke@435 284 }
duke@435 285 }
duke@435 286 }
duke@435 287
duke@435 288 void PSOldGen::resize(size_t desired_free_space) {
duke@435 289 const size_t alignment = virtual_space()->alignment();
duke@435 290 const size_t size_before = virtual_space()->committed_size();
duke@435 291 size_t new_size = used_in_bytes() + desired_free_space;
duke@435 292 if (new_size < used_in_bytes()) {
duke@435 293 // Overflowed the addition.
duke@435 294 new_size = gen_size_limit();
duke@435 295 }
duke@435 296 // Adjust according to our min and max
duke@435 297 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size());
duke@435 298
duke@435 299 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?");
duke@435 300 new_size = align_size_up(new_size, alignment);
duke@435 301
duke@435 302 const size_t current_size = capacity_in_bytes();
duke@435 303
duke@435 304 if (PrintAdaptiveSizePolicy && Verbose) {
duke@435 305 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
duke@435 306 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT
duke@435 307 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT
duke@435 308 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
duke@435 309 desired_free_space, used_in_bytes(), new_size, current_size,
duke@435 310 gen_size_limit(), min_gen_size());
duke@435 311 }
duke@435 312
duke@435 313 if (new_size == current_size) {
duke@435 314 // No change requested
duke@435 315 return;
duke@435 316 }
duke@435 317 if (new_size > current_size) {
duke@435 318 size_t change_bytes = new_size - current_size;
duke@435 319 expand(change_bytes);
duke@435 320 } else {
duke@435 321 size_t change_bytes = current_size - new_size;
duke@435 322 // shrink doesn't grab this lock, expand does. Is that right?
duke@435 323 MutexLocker x(ExpandHeap_lock);
duke@435 324 shrink(change_bytes);
duke@435 325 }
duke@435 326
duke@435 327 if (PrintAdaptiveSizePolicy) {
duke@435 328 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 329 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 330 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
duke@435 331 "collection: %d "
duke@435 332 "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ",
duke@435 333 heap->total_collections(),
duke@435 334 size_before, virtual_space()->committed_size());
duke@435 335 }
duke@435 336 }
duke@435 337
duke@435 338 // NOTE! We need to be careful about resizing. During a GC, multiple
duke@435 339 // allocators may be active during heap expansion. If we allow the
duke@435 340 // heap resizing to become visible before we have correctly resized
duke@435 341 // all heap related data structures, we may cause program failures.
duke@435 342 void PSOldGen::post_resize() {
duke@435 343 // First construct a memregion representing the new size
duke@435 344 MemRegion new_memregion((HeapWord*)virtual_space()->low(),
duke@435 345 (HeapWord*)virtual_space()->high());
duke@435 346 size_t new_word_size = new_memregion.word_size();
duke@435 347
duke@435 348 start_array()->set_covered_region(new_memregion);
duke@435 349 Universe::heap()->barrier_set()->resize_covered_region(new_memregion);
duke@435 350
duke@435 351 // Did we expand?
duke@435 352 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high();
duke@435 353 if (object_space()->end() < virtual_space_high) {
duke@435 354 // We need to mangle the newly expanded area. The memregion spans
duke@435 355 // end -> new_end, we assume that top -> end is already mangled.
duke@435 356 // This cannot be safely tested for, as allocation may be taking
duke@435 357 // place.
duke@435 358 MemRegion mangle_region(object_space()->end(), virtual_space_high);
duke@435 359 object_space()->mangle_region(mangle_region);
duke@435 360 }
duke@435 361
duke@435 362 // ALWAYS do this last!!
duke@435 363 object_space()->set_end(virtual_space_high);
duke@435 364
duke@435 365 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()),
duke@435 366 "Sanity");
duke@435 367 }
duke@435 368
duke@435 369 size_t PSOldGen::gen_size_limit() {
duke@435 370 return _max_gen_size;
duke@435 371 }
duke@435 372
duke@435 373 void PSOldGen::reset_after_change() {
duke@435 374 ShouldNotReachHere();
duke@435 375 return;
duke@435 376 }
duke@435 377
duke@435 378 size_t PSOldGen::available_for_expansion() {
duke@435 379 ShouldNotReachHere();
duke@435 380 return 0;
duke@435 381 }
duke@435 382
duke@435 383 size_t PSOldGen::available_for_contraction() {
duke@435 384 ShouldNotReachHere();
duke@435 385 return 0;
duke@435 386 }
duke@435 387
duke@435 388 void PSOldGen::print() const { print_on(tty);}
duke@435 389 void PSOldGen::print_on(outputStream* st) const {
duke@435 390 st->print(" %-15s", name());
duke@435 391 if (PrintGCDetails && Verbose) {
duke@435 392 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
duke@435 393 capacity_in_bytes(), used_in_bytes());
duke@435 394 } else {
duke@435 395 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
duke@435 396 capacity_in_bytes()/K, used_in_bytes()/K);
duke@435 397 }
duke@435 398 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
duke@435 399 virtual_space()->low_boundary(),
duke@435 400 virtual_space()->high(),
duke@435 401 virtual_space()->high_boundary());
duke@435 402
duke@435 403 st->print(" object"); object_space()->print_on(st);
duke@435 404 }
duke@435 405
duke@435 406 void PSOldGen::print_used_change(size_t prev_used) const {
duke@435 407 gclog_or_tty->print(" [%s:", name());
duke@435 408 gclog_or_tty->print(" " SIZE_FORMAT "K"
duke@435 409 "->" SIZE_FORMAT "K"
duke@435 410 "(" SIZE_FORMAT "K)",
duke@435 411 prev_used / K, used_in_bytes() / K,
duke@435 412 capacity_in_bytes() / K);
duke@435 413 gclog_or_tty->print("]");
duke@435 414 }
duke@435 415
duke@435 416 void PSOldGen::update_counters() {
duke@435 417 if (UsePerfData) {
duke@435 418 _space_counters->update_all();
duke@435 419 _gen_counters->update_all();
duke@435 420 }
duke@435 421 }
duke@435 422
duke@435 423 #ifndef PRODUCT
duke@435 424
duke@435 425 void PSOldGen::space_invariants() {
duke@435 426 assert(object_space()->end() == (HeapWord*) virtual_space()->high(),
duke@435 427 "Space invariant");
duke@435 428 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(),
duke@435 429 "Space invariant");
duke@435 430 assert(virtual_space()->low_boundary() <= virtual_space()->low(),
duke@435 431 "Space invariant");
duke@435 432 assert(virtual_space()->high_boundary() >= virtual_space()->high(),
duke@435 433 "Space invariant");
duke@435 434 assert(virtual_space()->low_boundary() == (char*) _reserved.start(),
duke@435 435 "Space invariant");
duke@435 436 assert(virtual_space()->high_boundary() == (char*) _reserved.end(),
duke@435 437 "Space invariant");
duke@435 438 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(),
duke@435 439 "Space invariant");
duke@435 440 }
duke@435 441 #endif
duke@435 442
duke@435 443 void PSOldGen::verify(bool allow_dirty) {
duke@435 444 object_space()->verify(allow_dirty);
duke@435 445 }
duke@435 446 class VerifyObjectStartArrayClosure : public ObjectClosure {
duke@435 447 PSOldGen* _gen;
duke@435 448 ObjectStartArray* _start_array;
duke@435 449
duke@435 450 public:
duke@435 451 VerifyObjectStartArrayClosure(PSOldGen* gen, ObjectStartArray* start_array) :
duke@435 452 _gen(gen), _start_array(start_array) { }
duke@435 453
duke@435 454 virtual void do_object(oop obj) {
duke@435 455 HeapWord* test_addr = (HeapWord*)obj + 1;
duke@435 456 guarantee(_start_array->object_start(test_addr) == (HeapWord*)obj, "ObjectStartArray cannot find start of object");
duke@435 457 guarantee(_start_array->is_block_allocated((HeapWord*)obj), "ObjectStartArray missing block allocation");
duke@435 458 }
duke@435 459 };
duke@435 460
duke@435 461 void PSOldGen::verify_object_start_array() {
duke@435 462 VerifyObjectStartArrayClosure check( this, &_start_array );
duke@435 463 object_iterate(&check);
duke@435 464 }

mercurial