annotate src/share/vm/gc_implementation/parallelScavenge/psMarkSweep.cpp @ 481:7d7a7c599c17

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