src/share/vm/gc_implementation/parallelScavenge/psMarkSweep.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/_psMarkSweep.cpp.incl"
duke@435 27
duke@435 28 elapsedTimer PSMarkSweep::_accumulated_time;
duke@435 29 unsigned int PSMarkSweep::_total_invocations = 0;
duke@435 30 jlong PSMarkSweep::_time_of_last_gc = 0;
duke@435 31 CollectorCounters* PSMarkSweep::_counters = NULL;
duke@435 32
duke@435 33 void PSMarkSweep::initialize() {
duke@435 34 MemRegion mr = Universe::heap()->reserved_region();
duke@435 35 _ref_processor = new ReferenceProcessor(mr,
duke@435 36 true, // atomic_discovery
duke@435 37 false); // mt_discovery
duke@435 38 if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {
duke@435 39 _counters = new CollectorCounters("PSMarkSweep", 1);
duke@435 40 }
duke@435 41 }
duke@435 42
duke@435 43 // This method contains all heap specific policy for invoking mark sweep.
duke@435 44 // PSMarkSweep::invoke_no_policy() will only attempt to mark-sweep-compact
duke@435 45 // the heap. It will do nothing further. If we need to bail out for policy
duke@435 46 // reasons, scavenge before full gc, or any other specialized behavior, it
duke@435 47 // needs to be added here.
duke@435 48 //
duke@435 49 // Note that this method should only be called from the vm_thread while
duke@435 50 // at a safepoint!
duke@435 51 void PSMarkSweep::invoke(bool maximum_heap_compaction) {
duke@435 52 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
duke@435 53 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
duke@435 54 assert(!Universe::heap()->is_gc_active(), "not reentrant");
duke@435 55
duke@435 56 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 57 GCCause::Cause gc_cause = heap->gc_cause();
duke@435 58 PSAdaptiveSizePolicy* policy = heap->size_policy();
duke@435 59
duke@435 60 // Before each allocation/collection attempt, find out from the
duke@435 61 // policy object if GCs are, on the whole, taking too long. If so,
duke@435 62 // bail out without attempting a collection. The exceptions are
duke@435 63 // for explicitly requested GC's.
duke@435 64 if (!policy->gc_time_limit_exceeded() ||
duke@435 65 GCCause::is_user_requested_gc(gc_cause) ||
duke@435 66 GCCause::is_serviceability_requested_gc(gc_cause)) {
duke@435 67 IsGCActiveMark mark;
duke@435 68
duke@435 69 if (ScavengeBeforeFullGC) {
duke@435 70 PSScavenge::invoke_no_policy();
duke@435 71 }
duke@435 72
duke@435 73 int count = (maximum_heap_compaction)?1:MarkSweepAlwaysCompactCount;
duke@435 74 IntFlagSetting flag_setting(MarkSweepAlwaysCompactCount, count);
duke@435 75 PSMarkSweep::invoke_no_policy(maximum_heap_compaction);
duke@435 76 }
duke@435 77 }
duke@435 78
duke@435 79 // This method contains no policy. You should probably
duke@435 80 // be calling invoke() instead.
duke@435 81 void PSMarkSweep::invoke_no_policy(bool clear_all_softrefs) {
duke@435 82 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
duke@435 83 assert(ref_processor() != NULL, "Sanity");
duke@435 84
duke@435 85 if (GC_locker::check_active_before_gc()) {
duke@435 86 return;
duke@435 87 }
duke@435 88
duke@435 89 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 90 GCCause::Cause gc_cause = heap->gc_cause();
duke@435 91 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 92 PSAdaptiveSizePolicy* size_policy = heap->size_policy();
duke@435 93
duke@435 94 PSYoungGen* young_gen = heap->young_gen();
duke@435 95 PSOldGen* old_gen = heap->old_gen();
duke@435 96 PSPermGen* perm_gen = heap->perm_gen();
duke@435 97
duke@435 98 // Increment the invocation count
duke@435 99 heap->increment_total_collections(true /* full */);
duke@435 100
duke@435 101 // We need to track unique mark sweep invocations as well.
duke@435 102 _total_invocations++;
duke@435 103
duke@435 104 AdaptiveSizePolicyOutput(size_policy, heap->total_collections());
duke@435 105
duke@435 106 if (PrintHeapAtGC) {
duke@435 107 Universe::print_heap_before_gc();
duke@435 108 }
duke@435 109
duke@435 110 // Fill in TLABs
duke@435 111 heap->accumulate_statistics_all_tlabs();
duke@435 112 heap->ensure_parsability(true); // retire TLABs
duke@435 113
duke@435 114 if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
duke@435 115 HandleMark hm; // Discard invalid handles created during verification
duke@435 116 gclog_or_tty->print(" VerifyBeforeGC:");
duke@435 117 Universe::verify(true);
duke@435 118 }
duke@435 119
duke@435 120 // Verify object start arrays
duke@435 121 if (VerifyObjectStartArray &&
duke@435 122 VerifyBeforeGC) {
duke@435 123 old_gen->verify_object_start_array();
duke@435 124 perm_gen->verify_object_start_array();
duke@435 125 }
duke@435 126
duke@435 127 // Filled in below to track the state of the young gen after the collection.
duke@435 128 bool eden_empty;
duke@435 129 bool survivors_empty;
duke@435 130 bool young_gen_empty;
duke@435 131
duke@435 132 {
duke@435 133 HandleMark hm;
duke@435 134 const bool is_system_gc = gc_cause == GCCause::_java_lang_system_gc;
duke@435 135 // This is useful for debugging but don't change the output the
duke@435 136 // the customer sees.
duke@435 137 const char* gc_cause_str = "Full GC";
duke@435 138 if (is_system_gc && PrintGCDetails) {
duke@435 139 gc_cause_str = "Full GC (System)";
duke@435 140 }
duke@435 141 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
duke@435 142 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
duke@435 143 TraceTime t1(gc_cause_str, PrintGC, !PrintGCDetails, gclog_or_tty);
duke@435 144 TraceCollectorStats tcs(counters());
duke@435 145 TraceMemoryManagerStats tms(true /* Full GC */);
duke@435 146
duke@435 147 if (TraceGen1Time) accumulated_time()->start();
duke@435 148
duke@435 149 // Let the size policy know we're starting
duke@435 150 size_policy->major_collection_begin();
duke@435 151
duke@435 152 // When collecting the permanent generation methodOops may be moving,
duke@435 153 // so we either have to flush all bcp data or convert it into bci.
duke@435 154 CodeCache::gc_prologue();
duke@435 155 Threads::gc_prologue();
duke@435 156 BiasedLocking::preserve_marks();
duke@435 157
duke@435 158 // Capture heap size before collection for printing.
duke@435 159 size_t prev_used = heap->used();
duke@435 160
duke@435 161 // Capture perm gen size before collection for sizing.
duke@435 162 size_t perm_gen_prev_used = perm_gen->used_in_bytes();
duke@435 163
duke@435 164 // For PrintGCDetails
duke@435 165 size_t old_gen_prev_used = old_gen->used_in_bytes();
duke@435 166 size_t young_gen_prev_used = young_gen->used_in_bytes();
duke@435 167
duke@435 168 allocate_stacks();
duke@435 169
duke@435 170 NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
duke@435 171 COMPILER2_PRESENT(DerivedPointerTable::clear());
duke@435 172
duke@435 173 ref_processor()->enable_discovery();
duke@435 174
duke@435 175 mark_sweep_phase1(clear_all_softrefs);
duke@435 176
duke@435 177 mark_sweep_phase2();
duke@435 178
duke@435 179 // Don't add any more derived pointers during phase3
duke@435 180 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
duke@435 181 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
duke@435 182
duke@435 183 mark_sweep_phase3();
duke@435 184
duke@435 185 mark_sweep_phase4();
duke@435 186
duke@435 187 restore_marks();
duke@435 188
duke@435 189 deallocate_stacks();
duke@435 190
duke@435 191 eden_empty = young_gen->eden_space()->is_empty();
duke@435 192 if (!eden_empty) {
duke@435 193 eden_empty = absorb_live_data_from_eden(size_policy, young_gen, old_gen);
duke@435 194 }
duke@435 195
duke@435 196 // Update heap occupancy information which is used as
duke@435 197 // input to soft ref clearing policy at the next gc.
duke@435 198 Universe::update_heap_info_at_gc();
duke@435 199
duke@435 200 survivors_empty = young_gen->from_space()->is_empty() &&
duke@435 201 young_gen->to_space()->is_empty();
duke@435 202 young_gen_empty = eden_empty && survivors_empty;
duke@435 203
duke@435 204 BarrierSet* bs = heap->barrier_set();
duke@435 205 if (bs->is_a(BarrierSet::ModRef)) {
duke@435 206 ModRefBarrierSet* modBS = (ModRefBarrierSet*)bs;
duke@435 207 MemRegion old_mr = heap->old_gen()->reserved();
duke@435 208 MemRegion perm_mr = heap->perm_gen()->reserved();
duke@435 209 assert(perm_mr.end() <= old_mr.start(), "Generations out of order");
duke@435 210
duke@435 211 if (young_gen_empty) {
duke@435 212 modBS->clear(MemRegion(perm_mr.start(), old_mr.end()));
duke@435 213 } else {
duke@435 214 modBS->invalidate(MemRegion(perm_mr.start(), old_mr.end()));
duke@435 215 }
duke@435 216 }
duke@435 217
duke@435 218 BiasedLocking::restore_marks();
duke@435 219 Threads::gc_epilogue();
duke@435 220 CodeCache::gc_epilogue();
duke@435 221
duke@435 222 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
duke@435 223
duke@435 224 ref_processor()->enqueue_discovered_references(NULL);
duke@435 225
duke@435 226 // Update time of last GC
duke@435 227 reset_millis_since_last_gc();
duke@435 228
duke@435 229 // Let the size policy know we're done
duke@435 230 size_policy->major_collection_end(old_gen->used_in_bytes(), gc_cause);
duke@435 231
duke@435 232 if (UseAdaptiveSizePolicy) {
duke@435 233
duke@435 234 if (PrintAdaptiveSizePolicy) {
duke@435 235 gclog_or_tty->print("AdaptiveSizeStart: ");
duke@435 236 gclog_or_tty->stamp();
duke@435 237 gclog_or_tty->print_cr(" collection: %d ",
duke@435 238 heap->total_collections());
duke@435 239 if (Verbose) {
duke@435 240 gclog_or_tty->print("old_gen_capacity: %d young_gen_capacity: %d"
duke@435 241 " perm_gen_capacity: %d ",
duke@435 242 old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes(),
duke@435 243 perm_gen->capacity_in_bytes());
duke@435 244 }
duke@435 245 }
duke@435 246
duke@435 247 // Don't check if the size_policy is ready here. Let
duke@435 248 // the size_policy check that internally.
duke@435 249 if (UseAdaptiveGenerationSizePolicyAtMajorCollection &&
duke@435 250 ((gc_cause != GCCause::_java_lang_system_gc) ||
duke@435 251 UseAdaptiveSizePolicyWithSystemGC)) {
duke@435 252 // Calculate optimal free space amounts
duke@435 253 assert(young_gen->max_size() >
duke@435 254 young_gen->from_space()->capacity_in_bytes() +
duke@435 255 young_gen->to_space()->capacity_in_bytes(),
duke@435 256 "Sizes of space in young gen are out-of-bounds");
duke@435 257 size_t max_eden_size = young_gen->max_size() -
duke@435 258 young_gen->from_space()->capacity_in_bytes() -
duke@435 259 young_gen->to_space()->capacity_in_bytes();
duke@435 260 size_policy->compute_generation_free_space(young_gen->used_in_bytes(),
duke@435 261 young_gen->eden_space()->used_in_bytes(),
duke@435 262 old_gen->used_in_bytes(),
duke@435 263 perm_gen->used_in_bytes(),
duke@435 264 young_gen->eden_space()->capacity_in_bytes(),
duke@435 265 old_gen->max_gen_size(),
duke@435 266 max_eden_size,
duke@435 267 true /* full gc*/,
duke@435 268 gc_cause);
duke@435 269
duke@435 270 heap->resize_old_gen(size_policy->calculated_old_free_size_in_bytes());
duke@435 271
duke@435 272 // Don't resize the young generation at an major collection. A
duke@435 273 // desired young generation size may have been calculated but
duke@435 274 // resizing the young generation complicates the code because the
duke@435 275 // resizing of the old generation may have moved the boundary
duke@435 276 // between the young generation and the old generation. Let the
duke@435 277 // young generation resizing happen at the minor collections.
duke@435 278 }
duke@435 279 if (PrintAdaptiveSizePolicy) {
duke@435 280 gclog_or_tty->print_cr("AdaptiveSizeStop: collection: %d ",
duke@435 281 heap->total_collections());
duke@435 282 }
duke@435 283 }
duke@435 284
duke@435 285 if (UsePerfData) {
duke@435 286 heap->gc_policy_counters()->update_counters();
duke@435 287 heap->gc_policy_counters()->update_old_capacity(
duke@435 288 old_gen->capacity_in_bytes());
duke@435 289 heap->gc_policy_counters()->update_young_capacity(
duke@435 290 young_gen->capacity_in_bytes());
duke@435 291 }
duke@435 292
duke@435 293 heap->resize_all_tlabs();
duke@435 294
duke@435 295 // We collected the perm gen, so we'll resize it here.
duke@435 296 perm_gen->compute_new_size(perm_gen_prev_used);
duke@435 297
duke@435 298 if (TraceGen1Time) accumulated_time()->stop();
duke@435 299
duke@435 300 if (PrintGC) {
duke@435 301 if (PrintGCDetails) {
duke@435 302 // Don't print a GC timestamp here. This is after the GC so
duke@435 303 // would be confusing.
duke@435 304 young_gen->print_used_change(young_gen_prev_used);
duke@435 305 old_gen->print_used_change(old_gen_prev_used);
duke@435 306 }
duke@435 307 heap->print_heap_change(prev_used);
duke@435 308 // Do perm gen after heap becase prev_used does
duke@435 309 // not include the perm gen (done this way in the other
duke@435 310 // collectors).
duke@435 311 if (PrintGCDetails) {
duke@435 312 perm_gen->print_used_change(perm_gen_prev_used);
duke@435 313 }
duke@435 314 }
duke@435 315
duke@435 316 // Track memory usage and detect low memory
duke@435 317 MemoryService::track_memory_usage();
duke@435 318 heap->update_counters();
duke@435 319
duke@435 320 if (PrintGCDetails) {
duke@435 321 if (size_policy->print_gc_time_limit_would_be_exceeded()) {
duke@435 322 if (size_policy->gc_time_limit_exceeded()) {
duke@435 323 gclog_or_tty->print_cr(" GC time is exceeding GCTimeLimit "
duke@435 324 "of %d%%", GCTimeLimit);
duke@435 325 } else {
duke@435 326 gclog_or_tty->print_cr(" GC time would exceed GCTimeLimit "
duke@435 327 "of %d%%", GCTimeLimit);
duke@435 328 }
duke@435 329 }
duke@435 330 size_policy->set_print_gc_time_limit_would_be_exceeded(false);
duke@435 331 }
duke@435 332 }
duke@435 333
duke@435 334 if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
duke@435 335 HandleMark hm; // Discard invalid handles created during verification
duke@435 336 gclog_or_tty->print(" VerifyAfterGC:");
duke@435 337 Universe::verify(false);
duke@435 338 }
duke@435 339
duke@435 340 // Re-verify object start arrays
duke@435 341 if (VerifyObjectStartArray &&
duke@435 342 VerifyAfterGC) {
duke@435 343 old_gen->verify_object_start_array();
duke@435 344 perm_gen->verify_object_start_array();
duke@435 345 }
duke@435 346
duke@435 347 NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
duke@435 348
duke@435 349 if (PrintHeapAtGC) {
duke@435 350 Universe::print_heap_after_gc();
duke@435 351 }
duke@435 352 }
duke@435 353
duke@435 354 bool PSMarkSweep::absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy,
duke@435 355 PSYoungGen* young_gen,
duke@435 356 PSOldGen* old_gen) {
duke@435 357 MutableSpace* const eden_space = young_gen->eden_space();
duke@435 358 assert(!eden_space->is_empty(), "eden must be non-empty");
duke@435 359 assert(young_gen->virtual_space()->alignment() ==
duke@435 360 old_gen->virtual_space()->alignment(), "alignments do not match");
duke@435 361
duke@435 362 if (!(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary)) {
duke@435 363 return false;
duke@435 364 }
duke@435 365
duke@435 366 // Both generations must be completely committed.
duke@435 367 if (young_gen->virtual_space()->uncommitted_size() != 0) {
duke@435 368 return false;
duke@435 369 }
duke@435 370 if (old_gen->virtual_space()->uncommitted_size() != 0) {
duke@435 371 return false;
duke@435 372 }
duke@435 373
duke@435 374 // Figure out how much to take from eden. Include the average amount promoted
duke@435 375 // in the total; otherwise the next young gen GC will simply bail out to a
duke@435 376 // full GC.
duke@435 377 const size_t alignment = old_gen->virtual_space()->alignment();
duke@435 378 const size_t eden_used = eden_space->used_in_bytes();
duke@435 379 const size_t promoted = (size_t)(size_policy->avg_promoted()->padded_average());
duke@435 380 const size_t absorb_size = align_size_up(eden_used + promoted, alignment);
duke@435 381 const size_t eden_capacity = eden_space->capacity_in_bytes();
duke@435 382
duke@435 383 if (absorb_size >= eden_capacity) {
duke@435 384 return false; // Must leave some space in eden.
duke@435 385 }
duke@435 386
duke@435 387 const size_t new_young_size = young_gen->capacity_in_bytes() - absorb_size;
duke@435 388 if (new_young_size < young_gen->min_gen_size()) {
duke@435 389 return false; // Respect young gen minimum size.
duke@435 390 }
duke@435 391
duke@435 392 if (TraceAdaptiveGCBoundary && Verbose) {
duke@435 393 gclog_or_tty->print(" absorbing " SIZE_FORMAT "K: "
duke@435 394 "eden " SIZE_FORMAT "K->" SIZE_FORMAT "K "
duke@435 395 "from " SIZE_FORMAT "K, to " SIZE_FORMAT "K "
duke@435 396 "young_gen " SIZE_FORMAT "K->" SIZE_FORMAT "K ",
duke@435 397 absorb_size / K,
duke@435 398 eden_capacity / K, (eden_capacity - absorb_size) / K,
duke@435 399 young_gen->from_space()->used_in_bytes() / K,
duke@435 400 young_gen->to_space()->used_in_bytes() / K,
duke@435 401 young_gen->capacity_in_bytes() / K, new_young_size / K);
duke@435 402 }
duke@435 403
duke@435 404 // Fill the unused part of the old gen.
duke@435 405 MutableSpace* const old_space = old_gen->object_space();
duke@435 406 MemRegion old_gen_unused(old_space->top(), old_space->end());
duke@435 407
duke@435 408 // If the unused part of the old gen cannot be filled, skip
duke@435 409 // absorbing eden.
duke@435 410 if (old_gen_unused.word_size() < SharedHeap::min_fill_size()) {
duke@435 411 return false;
duke@435 412 }
duke@435 413
duke@435 414 if (!old_gen_unused.is_empty()) {
duke@435 415 SharedHeap::fill_region_with_object(old_gen_unused);
duke@435 416 }
duke@435 417
duke@435 418 // Take the live data from eden and set both top and end in the old gen to
duke@435 419 // eden top. (Need to set end because reset_after_change() mangles the region
duke@435 420 // from end to virtual_space->high() in debug builds).
duke@435 421 HeapWord* const new_top = eden_space->top();
duke@435 422 old_gen->virtual_space()->expand_into(young_gen->virtual_space(),
duke@435 423 absorb_size);
duke@435 424 young_gen->reset_after_change();
duke@435 425 old_space->set_top(new_top);
duke@435 426 old_space->set_end(new_top);
duke@435 427 old_gen->reset_after_change();
duke@435 428
duke@435 429 // Update the object start array for the filler object and the data from eden.
duke@435 430 ObjectStartArray* const start_array = old_gen->start_array();
duke@435 431 HeapWord* const start = old_gen_unused.start();
duke@435 432 for (HeapWord* addr = start; addr < new_top; addr += oop(addr)->size()) {
duke@435 433 start_array->allocate_block(addr);
duke@435 434 }
duke@435 435
duke@435 436 // Could update the promoted average here, but it is not typically updated at
duke@435 437 // full GCs and the value to use is unclear. Something like
duke@435 438 //
duke@435 439 // cur_promoted_avg + absorb_size / number_of_scavenges_since_last_full_gc.
duke@435 440
duke@435 441 size_policy->set_bytes_absorbed_from_eden(absorb_size);
duke@435 442 return true;
duke@435 443 }
duke@435 444
duke@435 445 void PSMarkSweep::allocate_stacks() {
duke@435 446 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 447 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 448
duke@435 449 PSYoungGen* young_gen = heap->young_gen();
duke@435 450
duke@435 451 MutableSpace* to_space = young_gen->to_space();
duke@435 452 _preserved_marks = (PreservedMark*)to_space->top();
duke@435 453 _preserved_count = 0;
duke@435 454
duke@435 455 // We want to calculate the size in bytes first.
duke@435 456 _preserved_count_max = pointer_delta(to_space->end(), to_space->top(), sizeof(jbyte));
duke@435 457 // Now divide by the size of a PreservedMark
duke@435 458 _preserved_count_max /= sizeof(PreservedMark);
duke@435 459
duke@435 460 _preserved_mark_stack = NULL;
duke@435 461 _preserved_oop_stack = NULL;
duke@435 462
duke@435 463 _marking_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true);
duke@435 464
duke@435 465 int size = SystemDictionary::number_of_classes() * 2;
duke@435 466 _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true);
duke@435 467 }
duke@435 468
duke@435 469
duke@435 470 void PSMarkSweep::deallocate_stacks() {
duke@435 471 if (_preserved_oop_stack) {
duke@435 472 delete _preserved_mark_stack;
duke@435 473 _preserved_mark_stack = NULL;
duke@435 474 delete _preserved_oop_stack;
duke@435 475 _preserved_oop_stack = NULL;
duke@435 476 }
duke@435 477
duke@435 478 delete _marking_stack;
duke@435 479 delete _revisit_klass_stack;
duke@435 480 }
duke@435 481
duke@435 482 void PSMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) {
duke@435 483 // Recursively traverse all live objects and mark them
duke@435 484 EventMark m("1 mark object");
duke@435 485 TraceTime tm("phase 1", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 486 trace(" 1");
duke@435 487
duke@435 488 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 489 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 490
duke@435 491 // General strong roots.
duke@435 492 Universe::oops_do(mark_and_push_closure());
duke@435 493 ReferenceProcessor::oops_do(mark_and_push_closure());
duke@435 494 JNIHandles::oops_do(mark_and_push_closure()); // Global (strong) JNI handles
duke@435 495 Threads::oops_do(mark_and_push_closure());
duke@435 496 ObjectSynchronizer::oops_do(mark_and_push_closure());
duke@435 497 FlatProfiler::oops_do(mark_and_push_closure());
duke@435 498 Management::oops_do(mark_and_push_closure());
duke@435 499 JvmtiExport::oops_do(mark_and_push_closure());
duke@435 500 SystemDictionary::always_strong_oops_do(mark_and_push_closure());
duke@435 501 vmSymbols::oops_do(mark_and_push_closure());
duke@435 502
duke@435 503 // Flush marking stack.
duke@435 504 follow_stack();
duke@435 505
duke@435 506 // Process reference objects found during marking
duke@435 507
duke@435 508 // Skipping the reference processing for VerifyParallelOldWithMarkSweep
duke@435 509 // affects the marking (makes it different).
duke@435 510 {
duke@435 511 ReferencePolicy *soft_ref_policy;
duke@435 512 if (clear_all_softrefs) {
duke@435 513 soft_ref_policy = new AlwaysClearPolicy();
duke@435 514 } else {
duke@435 515 #ifdef COMPILER2
duke@435 516 soft_ref_policy = new LRUMaxHeapPolicy();
duke@435 517 #else
duke@435 518 soft_ref_policy = new LRUCurrentHeapPolicy();
duke@435 519 #endif // COMPILER2
duke@435 520 }
duke@435 521 assert(soft_ref_policy != NULL,"No soft reference policy");
duke@435 522 ref_processor()->process_discovered_references(
duke@435 523 soft_ref_policy, is_alive_closure(), mark_and_push_closure(),
duke@435 524 follow_stack_closure(), NULL);
duke@435 525 }
duke@435 526
duke@435 527 // Follow system dictionary roots and unload classes
duke@435 528 bool purged_class = SystemDictionary::do_unloading(is_alive_closure());
duke@435 529
duke@435 530 // Follow code cache roots
duke@435 531 CodeCache::do_unloading(is_alive_closure(), mark_and_push_closure(),
duke@435 532 purged_class);
duke@435 533 follow_stack(); // Flush marking stack
duke@435 534
duke@435 535 // Update subklass/sibling/implementor links of live klasses
duke@435 536 follow_weak_klass_links();
duke@435 537 assert(_marking_stack->is_empty(), "just drained");
duke@435 538
duke@435 539 // Visit symbol and interned string tables and delete unmarked oops
duke@435 540 SymbolTable::unlink(is_alive_closure());
duke@435 541 StringTable::unlink(is_alive_closure());
duke@435 542
duke@435 543 assert(_marking_stack->is_empty(), "stack should be empty by now");
duke@435 544 }
duke@435 545
duke@435 546
duke@435 547 void PSMarkSweep::mark_sweep_phase2() {
duke@435 548 EventMark m("2 compute new addresses");
duke@435 549 TraceTime tm("phase 2", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 550 trace("2");
duke@435 551
duke@435 552 // Now all live objects are marked, compute the new object addresses.
duke@435 553
duke@435 554 // It is imperative that we traverse perm_gen LAST. If dead space is
duke@435 555 // allowed a range of dead object may get overwritten by a dead int
duke@435 556 // array. If perm_gen is not traversed last a klassOop may get
duke@435 557 // overwritten. This is fine since it is dead, but if the class has dead
duke@435 558 // instances we have to skip them, and in order to find their size we
duke@435 559 // need the klassOop!
duke@435 560 //
duke@435 561 // It is not required that we traverse spaces in the same order in
duke@435 562 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
duke@435 563 // tracking expects us to do so. See comment under phase4.
duke@435 564
duke@435 565 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 566 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 567
duke@435 568 PSOldGen* old_gen = heap->old_gen();
duke@435 569 PSPermGen* perm_gen = heap->perm_gen();
duke@435 570
duke@435 571 // Begin compacting into the old gen
duke@435 572 PSMarkSweepDecorator::set_destination_decorator_tenured();
duke@435 573
duke@435 574 // This will also compact the young gen spaces.
duke@435 575 old_gen->precompact();
duke@435 576
duke@435 577 // Compact the perm gen into the perm gen
duke@435 578 PSMarkSweepDecorator::set_destination_decorator_perm_gen();
duke@435 579
duke@435 580 perm_gen->precompact();
duke@435 581 }
duke@435 582
duke@435 583 // This should be moved to the shared markSweep code!
duke@435 584 class PSAlwaysTrueClosure: public BoolObjectClosure {
duke@435 585 public:
duke@435 586 void do_object(oop p) { ShouldNotReachHere(); }
duke@435 587 bool do_object_b(oop p) { return true; }
duke@435 588 };
duke@435 589 static PSAlwaysTrueClosure always_true;
duke@435 590
duke@435 591 void PSMarkSweep::mark_sweep_phase3() {
duke@435 592 // Adjust the pointers to reflect the new locations
duke@435 593 EventMark m("3 adjust pointers");
duke@435 594 TraceTime tm("phase 3", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 595 trace("3");
duke@435 596
duke@435 597 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 598 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 599
duke@435 600 PSYoungGen* young_gen = heap->young_gen();
duke@435 601 PSOldGen* old_gen = heap->old_gen();
duke@435 602 PSPermGen* perm_gen = heap->perm_gen();
duke@435 603
duke@435 604 // General strong roots.
duke@435 605 Universe::oops_do(adjust_root_pointer_closure());
duke@435 606 ReferenceProcessor::oops_do(adjust_root_pointer_closure());
duke@435 607 JNIHandles::oops_do(adjust_root_pointer_closure()); // Global (strong) JNI handles
duke@435 608 Threads::oops_do(adjust_root_pointer_closure());
duke@435 609 ObjectSynchronizer::oops_do(adjust_root_pointer_closure());
duke@435 610 FlatProfiler::oops_do(adjust_root_pointer_closure());
duke@435 611 Management::oops_do(adjust_root_pointer_closure());
duke@435 612 JvmtiExport::oops_do(adjust_root_pointer_closure());
duke@435 613 // SO_AllClasses
duke@435 614 SystemDictionary::oops_do(adjust_root_pointer_closure());
duke@435 615 vmSymbols::oops_do(adjust_root_pointer_closure());
duke@435 616
duke@435 617 // Now adjust pointers in remaining weak roots. (All of which should
duke@435 618 // have been cleared if they pointed to non-surviving objects.)
duke@435 619 // Global (weak) JNI handles
duke@435 620 JNIHandles::weak_oops_do(&always_true, adjust_root_pointer_closure());
duke@435 621
duke@435 622 CodeCache::oops_do(adjust_pointer_closure());
duke@435 623 SymbolTable::oops_do(adjust_root_pointer_closure());
duke@435 624 StringTable::oops_do(adjust_root_pointer_closure());
duke@435 625 ref_processor()->weak_oops_do(adjust_root_pointer_closure());
duke@435 626 PSScavenge::reference_processor()->weak_oops_do(adjust_root_pointer_closure());
duke@435 627
duke@435 628 adjust_marks();
duke@435 629
duke@435 630 young_gen->adjust_pointers();
duke@435 631 old_gen->adjust_pointers();
duke@435 632 perm_gen->adjust_pointers();
duke@435 633 }
duke@435 634
duke@435 635 void PSMarkSweep::mark_sweep_phase4() {
duke@435 636 EventMark m("4 compact heap");
duke@435 637 TraceTime tm("phase 4", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 638 trace("4");
duke@435 639
duke@435 640 // All pointers are now adjusted, move objects accordingly
duke@435 641
duke@435 642 // It is imperative that we traverse perm_gen first in phase4. All
duke@435 643 // classes must be allocated earlier than their instances, and traversing
duke@435 644 // perm_gen first makes sure that all klassOops have moved to their new
duke@435 645 // location before any instance does a dispatch through it's klass!
duke@435 646 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 647 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 648
duke@435 649 PSYoungGen* young_gen = heap->young_gen();
duke@435 650 PSOldGen* old_gen = heap->old_gen();
duke@435 651 PSPermGen* perm_gen = heap->perm_gen();
duke@435 652
duke@435 653 perm_gen->compact();
duke@435 654 old_gen->compact();
duke@435 655 young_gen->compact();
duke@435 656 }
duke@435 657
duke@435 658 jlong PSMarkSweep::millis_since_last_gc() {
duke@435 659 jlong ret_val = os::javaTimeMillis() - _time_of_last_gc;
duke@435 660 // XXX See note in genCollectedHeap::millis_since_last_gc().
duke@435 661 if (ret_val < 0) {
duke@435 662 NOT_PRODUCT(warning("time warp: %d", ret_val);)
duke@435 663 return 0;
duke@435 664 }
duke@435 665 return ret_val;
duke@435 666 }
duke@435 667
duke@435 668 void PSMarkSweep::reset_millis_since_last_gc() {
duke@435 669 _time_of_last_gc = os::javaTimeMillis();
duke@435 670 }

mercurial