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

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/symbolTable.hpp"
aoqi@0 27 #include "classfile/systemDictionary.hpp"
aoqi@0 28 #include "code/codeCache.hpp"
aoqi@0 29 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
aoqi@0 30 #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp"
aoqi@0 31 #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
aoqi@0 32 #include "gc_implementation/parallelScavenge/psMarkSweepDecorator.hpp"
aoqi@0 33 #include "gc_implementation/parallelScavenge/psOldGen.hpp"
aoqi@0 34 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
aoqi@0 35 #include "gc_implementation/parallelScavenge/psYoungGen.hpp"
aoqi@0 36 #include "gc_implementation/shared/gcHeapSummary.hpp"
aoqi@0 37 #include "gc_implementation/shared/gcTimer.hpp"
aoqi@0 38 #include "gc_implementation/shared/gcTrace.hpp"
aoqi@0 39 #include "gc_implementation/shared/gcTraceTime.hpp"
aoqi@0 40 #include "gc_implementation/shared/isGCActiveMark.hpp"
aoqi@0 41 #include "gc_implementation/shared/markSweep.hpp"
aoqi@0 42 #include "gc_implementation/shared/spaceDecorator.hpp"
aoqi@0 43 #include "gc_interface/gcCause.hpp"
aoqi@0 44 #include "memory/gcLocker.inline.hpp"
aoqi@0 45 #include "memory/referencePolicy.hpp"
aoqi@0 46 #include "memory/referenceProcessor.hpp"
aoqi@0 47 #include "oops/oop.inline.hpp"
aoqi@0 48 #include "runtime/biasedLocking.hpp"
aoqi@0 49 #include "runtime/fprofiler.hpp"
aoqi@0 50 #include "runtime/safepoint.hpp"
aoqi@0 51 #include "runtime/vmThread.hpp"
aoqi@0 52 #include "services/management.hpp"
aoqi@0 53 #include "services/memoryService.hpp"
aoqi@0 54 #include "utilities/events.hpp"
aoqi@0 55 #include "utilities/stack.inline.hpp"
aoqi@0 56
aoqi@0 57 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 58
aoqi@0 59 elapsedTimer PSMarkSweep::_accumulated_time;
aoqi@0 60 jlong PSMarkSweep::_time_of_last_gc = 0;
aoqi@0 61 CollectorCounters* PSMarkSweep::_counters = NULL;
aoqi@0 62
aoqi@0 63 void PSMarkSweep::initialize() {
aoqi@0 64 MemRegion mr = Universe::heap()->reserved_region();
aoqi@0 65 _ref_processor = new ReferenceProcessor(mr); // a vanilla ref proc
aoqi@0 66 _counters = new CollectorCounters("PSMarkSweep", 1);
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 // This method contains all heap specific policy for invoking mark sweep.
aoqi@0 70 // PSMarkSweep::invoke_no_policy() will only attempt to mark-sweep-compact
aoqi@0 71 // the heap. It will do nothing further. If we need to bail out for policy
aoqi@0 72 // reasons, scavenge before full gc, or any other specialized behavior, it
aoqi@0 73 // needs to be added here.
aoqi@0 74 //
aoqi@0 75 // Note that this method should only be called from the vm_thread while
aoqi@0 76 // at a safepoint!
aoqi@0 77 //
aoqi@0 78 // Note that the all_soft_refs_clear flag in the collector policy
aoqi@0 79 // may be true because this method can be called without intervening
aoqi@0 80 // activity. For example when the heap space is tight and full measure
aoqi@0 81 // are being taken to free space.
aoqi@0 82
aoqi@0 83 void PSMarkSweep::invoke(bool maximum_heap_compaction) {
aoqi@0 84 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
aoqi@0 85 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
aoqi@0 86 assert(!Universe::heap()->is_gc_active(), "not reentrant");
aoqi@0 87
aoqi@0 88 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 89 GCCause::Cause gc_cause = heap->gc_cause();
aoqi@0 90 PSAdaptiveSizePolicy* policy = heap->size_policy();
aoqi@0 91 IsGCActiveMark mark;
aoqi@0 92
aoqi@0 93 if (ScavengeBeforeFullGC) {
aoqi@0 94 PSScavenge::invoke_no_policy();
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 const bool clear_all_soft_refs =
aoqi@0 98 heap->collector_policy()->should_clear_all_soft_refs();
aoqi@0 99
aoqi@0 100 uint count = maximum_heap_compaction ? 1 : MarkSweepAlwaysCompactCount;
aoqi@0 101 UIntFlagSetting flag_setting(MarkSweepAlwaysCompactCount, count);
aoqi@0 102 PSMarkSweep::invoke_no_policy(clear_all_soft_refs || maximum_heap_compaction);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 // This method contains no policy. You should probably
aoqi@0 106 // be calling invoke() instead.
aoqi@0 107 bool PSMarkSweep::invoke_no_policy(bool clear_all_softrefs) {
aoqi@0 108 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
aoqi@0 109 assert(ref_processor() != NULL, "Sanity");
aoqi@0 110
aoqi@0 111 if (GC_locker::check_active_before_gc()) {
aoqi@0 112 return false;
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 116 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 117 GCCause::Cause gc_cause = heap->gc_cause();
aoqi@0 118
aoqi@0 119 _gc_timer->register_gc_start();
aoqi@0 120 _gc_tracer->report_gc_start(gc_cause, _gc_timer->gc_start());
aoqi@0 121
aoqi@0 122 PSAdaptiveSizePolicy* size_policy = heap->size_policy();
aoqi@0 123
aoqi@0 124 // The scope of casr should end after code that can change
aoqi@0 125 // CollectorPolicy::_should_clear_all_soft_refs.
aoqi@0 126 ClearedAllSoftRefs casr(clear_all_softrefs, heap->collector_policy());
aoqi@0 127
aoqi@0 128 PSYoungGen* young_gen = heap->young_gen();
aoqi@0 129 PSOldGen* old_gen = heap->old_gen();
aoqi@0 130
aoqi@0 131 // Increment the invocation count
aoqi@0 132 heap->increment_total_collections(true /* full */);
aoqi@0 133
aoqi@0 134 // Save information needed to minimize mangling
aoqi@0 135 heap->record_gen_tops_before_GC();
aoqi@0 136
aoqi@0 137 // We need to track unique mark sweep invocations as well.
aoqi@0 138 _total_invocations++;
aoqi@0 139
aoqi@0 140 AdaptiveSizePolicyOutput(size_policy, heap->total_collections());
aoqi@0 141
aoqi@0 142 heap->print_heap_before_gc();
aoqi@0 143 heap->trace_heap_before_gc(_gc_tracer);
aoqi@0 144
aoqi@0 145 // Fill in TLABs
aoqi@0 146 heap->accumulate_statistics_all_tlabs();
aoqi@0 147 heap->ensure_parsability(true); // retire TLABs
aoqi@0 148
aoqi@0 149 if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
aoqi@0 150 HandleMark hm; // Discard invalid handles created during verification
aoqi@0 151 Universe::verify(" VerifyBeforeGC:");
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 // Verify object start arrays
aoqi@0 155 if (VerifyObjectStartArray &&
aoqi@0 156 VerifyBeforeGC) {
aoqi@0 157 old_gen->verify_object_start_array();
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 heap->pre_full_gc_dump(_gc_timer);
aoqi@0 161
aoqi@0 162 // Filled in below to track the state of the young gen after the collection.
aoqi@0 163 bool eden_empty;
aoqi@0 164 bool survivors_empty;
aoqi@0 165 bool young_gen_empty;
aoqi@0 166
aoqi@0 167 {
aoqi@0 168 HandleMark hm;
aoqi@0 169
aoqi@0 170 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
aoqi@0 171 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
aoqi@0 172 GCTraceTime t1(GCCauseString("Full GC", gc_cause), PrintGC, !PrintGCDetails, NULL);
aoqi@0 173 TraceCollectorStats tcs(counters());
aoqi@0 174 TraceMemoryManagerStats tms(true /* Full GC */,gc_cause);
aoqi@0 175
aoqi@0 176 if (TraceGen1Time) accumulated_time()->start();
aoqi@0 177
aoqi@0 178 // Let the size policy know we're starting
aoqi@0 179 size_policy->major_collection_begin();
aoqi@0 180
aoqi@0 181 CodeCache::gc_prologue();
aoqi@0 182 Threads::gc_prologue();
aoqi@0 183 BiasedLocking::preserve_marks();
aoqi@0 184
aoqi@0 185 // Capture heap size before collection for printing.
aoqi@0 186 size_t prev_used = heap->used();
aoqi@0 187
aoqi@0 188 // Capture metadata size before collection for sizing.
aoqi@0 189 size_t metadata_prev_used = MetaspaceAux::used_bytes();
aoqi@0 190
aoqi@0 191 // For PrintGCDetails
aoqi@0 192 size_t old_gen_prev_used = old_gen->used_in_bytes();
aoqi@0 193 size_t young_gen_prev_used = young_gen->used_in_bytes();
aoqi@0 194
aoqi@0 195 allocate_stacks();
aoqi@0 196
aoqi@0 197 COMPILER2_PRESENT(DerivedPointerTable::clear());
aoqi@0 198
aoqi@0 199 ref_processor()->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);
aoqi@0 200 ref_processor()->setup_policy(clear_all_softrefs);
aoqi@0 201
aoqi@0 202 mark_sweep_phase1(clear_all_softrefs);
aoqi@0 203
aoqi@0 204 mark_sweep_phase2();
aoqi@0 205
aoqi@0 206 // Don't add any more derived pointers during phase3
aoqi@0 207 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
aoqi@0 208 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
aoqi@0 209
aoqi@0 210 mark_sweep_phase3();
aoqi@0 211
aoqi@0 212 mark_sweep_phase4();
aoqi@0 213
aoqi@0 214 restore_marks();
aoqi@0 215
aoqi@0 216 deallocate_stacks();
aoqi@0 217
aoqi@0 218 if (ZapUnusedHeapArea) {
aoqi@0 219 // Do a complete mangle (top to end) because the usage for
aoqi@0 220 // scratch does not maintain a top pointer.
aoqi@0 221 young_gen->to_space()->mangle_unused_area_complete();
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 eden_empty = young_gen->eden_space()->is_empty();
aoqi@0 225 if (!eden_empty) {
aoqi@0 226 eden_empty = absorb_live_data_from_eden(size_policy, young_gen, old_gen);
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 // Update heap occupancy information which is used as
aoqi@0 230 // input to soft ref clearing policy at the next gc.
aoqi@0 231 Universe::update_heap_info_at_gc();
aoqi@0 232
aoqi@0 233 survivors_empty = young_gen->from_space()->is_empty() &&
aoqi@0 234 young_gen->to_space()->is_empty();
aoqi@0 235 young_gen_empty = eden_empty && survivors_empty;
aoqi@0 236
aoqi@0 237 BarrierSet* bs = heap->barrier_set();
aoqi@0 238 if (bs->is_a(BarrierSet::ModRef)) {
aoqi@0 239 ModRefBarrierSet* modBS = (ModRefBarrierSet*)bs;
aoqi@0 240 MemRegion old_mr = heap->old_gen()->reserved();
aoqi@0 241 if (young_gen_empty) {
aoqi@0 242 modBS->clear(MemRegion(old_mr.start(), old_mr.end()));
aoqi@0 243 } else {
aoqi@0 244 modBS->invalidate(MemRegion(old_mr.start(), old_mr.end()));
aoqi@0 245 }
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 // Delete metaspaces for unloaded class loaders and clean up loader_data graph
aoqi@0 249 ClassLoaderDataGraph::purge();
aoqi@0 250 MetaspaceAux::verify_metrics();
aoqi@0 251
aoqi@0 252 BiasedLocking::restore_marks();
aoqi@0 253 Threads::gc_epilogue();
aoqi@0 254 CodeCache::gc_epilogue();
aoqi@0 255 JvmtiExport::gc_epilogue();
aoqi@0 256
aoqi@0 257 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
aoqi@0 258
aoqi@0 259 ref_processor()->enqueue_discovered_references(NULL);
aoqi@0 260
aoqi@0 261 // Update time of last GC
aoqi@0 262 reset_millis_since_last_gc();
aoqi@0 263
aoqi@0 264 // Let the size policy know we're done
aoqi@0 265 size_policy->major_collection_end(old_gen->used_in_bytes(), gc_cause);
aoqi@0 266
aoqi@0 267 if (UseAdaptiveSizePolicy) {
aoqi@0 268
aoqi@0 269 if (PrintAdaptiveSizePolicy) {
aoqi@0 270 gclog_or_tty->print("AdaptiveSizeStart: ");
aoqi@0 271 gclog_or_tty->stamp();
aoqi@0 272 gclog_or_tty->print_cr(" collection: %d ",
aoqi@0 273 heap->total_collections());
aoqi@0 274 if (Verbose) {
aoqi@0 275 gclog_or_tty->print("old_gen_capacity: %d young_gen_capacity: %d",
aoqi@0 276 old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes());
aoqi@0 277 }
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 // Don't check if the size_policy is ready here. Let
aoqi@0 281 // the size_policy check that internally.
aoqi@0 282 if (UseAdaptiveGenerationSizePolicyAtMajorCollection &&
aoqi@0 283 ((gc_cause != GCCause::_java_lang_system_gc) ||
aoqi@0 284 UseAdaptiveSizePolicyWithSystemGC)) {
aoqi@0 285 // Calculate optimal free space amounts
aoqi@0 286 assert(young_gen->max_size() >
aoqi@0 287 young_gen->from_space()->capacity_in_bytes() +
aoqi@0 288 young_gen->to_space()->capacity_in_bytes(),
aoqi@0 289 "Sizes of space in young gen are out-of-bounds");
aoqi@0 290
aoqi@0 291 size_t young_live = young_gen->used_in_bytes();
aoqi@0 292 size_t eden_live = young_gen->eden_space()->used_in_bytes();
aoqi@0 293 size_t old_live = old_gen->used_in_bytes();
aoqi@0 294 size_t cur_eden = young_gen->eden_space()->capacity_in_bytes();
aoqi@0 295 size_t max_old_gen_size = old_gen->max_gen_size();
aoqi@0 296 size_t max_eden_size = young_gen->max_size() -
aoqi@0 297 young_gen->from_space()->capacity_in_bytes() -
aoqi@0 298 young_gen->to_space()->capacity_in_bytes();
aoqi@0 299
aoqi@0 300 // Used for diagnostics
aoqi@0 301 size_policy->clear_generation_free_space_flags();
aoqi@0 302
aoqi@0 303 size_policy->compute_generations_free_space(young_live,
aoqi@0 304 eden_live,
aoqi@0 305 old_live,
aoqi@0 306 cur_eden,
aoqi@0 307 max_old_gen_size,
aoqi@0 308 max_eden_size,
aoqi@0 309 true /* full gc*/);
aoqi@0 310
aoqi@0 311 size_policy->check_gc_overhead_limit(young_live,
aoqi@0 312 eden_live,
aoqi@0 313 max_old_gen_size,
aoqi@0 314 max_eden_size,
aoqi@0 315 true /* full gc*/,
aoqi@0 316 gc_cause,
aoqi@0 317 heap->collector_policy());
aoqi@0 318
aoqi@0 319 size_policy->decay_supplemental_growth(true /* full gc*/);
aoqi@0 320
aoqi@0 321 heap->resize_old_gen(size_policy->calculated_old_free_size_in_bytes());
aoqi@0 322
aoqi@0 323 // Don't resize the young generation at an major collection. A
aoqi@0 324 // desired young generation size may have been calculated but
aoqi@0 325 // resizing the young generation complicates the code because the
aoqi@0 326 // resizing of the old generation may have moved the boundary
aoqi@0 327 // between the young generation and the old generation. Let the
aoqi@0 328 // young generation resizing happen at the minor collections.
aoqi@0 329 }
aoqi@0 330 if (PrintAdaptiveSizePolicy) {
aoqi@0 331 gclog_or_tty->print_cr("AdaptiveSizeStop: collection: %d ",
aoqi@0 332 heap->total_collections());
aoqi@0 333 }
aoqi@0 334 }
aoqi@0 335
aoqi@0 336 if (UsePerfData) {
aoqi@0 337 heap->gc_policy_counters()->update_counters();
aoqi@0 338 heap->gc_policy_counters()->update_old_capacity(
aoqi@0 339 old_gen->capacity_in_bytes());
aoqi@0 340 heap->gc_policy_counters()->update_young_capacity(
aoqi@0 341 young_gen->capacity_in_bytes());
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 heap->resize_all_tlabs();
aoqi@0 345
aoqi@0 346 // We collected the heap, recalculate the metaspace capacity
aoqi@0 347 MetaspaceGC::compute_new_size();
aoqi@0 348
aoqi@0 349 if (TraceGen1Time) accumulated_time()->stop();
aoqi@0 350
aoqi@0 351 if (PrintGC) {
aoqi@0 352 if (PrintGCDetails) {
aoqi@0 353 // Don't print a GC timestamp here. This is after the GC so
aoqi@0 354 // would be confusing.
aoqi@0 355 young_gen->print_used_change(young_gen_prev_used);
aoqi@0 356 old_gen->print_used_change(old_gen_prev_used);
aoqi@0 357 }
aoqi@0 358 heap->print_heap_change(prev_used);
aoqi@0 359 if (PrintGCDetails) {
aoqi@0 360 MetaspaceAux::print_metaspace_change(metadata_prev_used);
aoqi@0 361 }
aoqi@0 362 }
aoqi@0 363
aoqi@0 364 // Track memory usage and detect low memory
aoqi@0 365 MemoryService::track_memory_usage();
aoqi@0 366 heap->update_counters();
aoqi@0 367 }
aoqi@0 368
aoqi@0 369 if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
aoqi@0 370 HandleMark hm; // Discard invalid handles created during verification
aoqi@0 371 Universe::verify(" VerifyAfterGC:");
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 // Re-verify object start arrays
aoqi@0 375 if (VerifyObjectStartArray &&
aoqi@0 376 VerifyAfterGC) {
aoqi@0 377 old_gen->verify_object_start_array();
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 if (ZapUnusedHeapArea) {
aoqi@0 381 old_gen->object_space()->check_mangled_unused_area_complete();
aoqi@0 382 }
aoqi@0 383
aoqi@0 384 NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
aoqi@0 385
aoqi@0 386 heap->print_heap_after_gc();
aoqi@0 387 heap->trace_heap_after_gc(_gc_tracer);
aoqi@0 388
aoqi@0 389 heap->post_full_gc_dump(_gc_timer);
aoqi@0 390
aoqi@0 391 #ifdef TRACESPINNING
aoqi@0 392 ParallelTaskTerminator::print_termination_counts();
aoqi@0 393 #endif
aoqi@0 394
aoqi@0 395 _gc_timer->register_gc_end();
aoqi@0 396
aoqi@0 397 _gc_tracer->report_gc_end(_gc_timer->gc_end(), _gc_timer->time_partitions());
aoqi@0 398
aoqi@0 399 return true;
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 bool PSMarkSweep::absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy,
aoqi@0 403 PSYoungGen* young_gen,
aoqi@0 404 PSOldGen* old_gen) {
aoqi@0 405 MutableSpace* const eden_space = young_gen->eden_space();
aoqi@0 406 assert(!eden_space->is_empty(), "eden must be non-empty");
aoqi@0 407 assert(young_gen->virtual_space()->alignment() ==
aoqi@0 408 old_gen->virtual_space()->alignment(), "alignments do not match");
aoqi@0 409
aoqi@0 410 if (!(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary)) {
aoqi@0 411 return false;
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 // Both generations must be completely committed.
aoqi@0 415 if (young_gen->virtual_space()->uncommitted_size() != 0) {
aoqi@0 416 return false;
aoqi@0 417 }
aoqi@0 418 if (old_gen->virtual_space()->uncommitted_size() != 0) {
aoqi@0 419 return false;
aoqi@0 420 }
aoqi@0 421
aoqi@0 422 // Figure out how much to take from eden. Include the average amount promoted
aoqi@0 423 // in the total; otherwise the next young gen GC will simply bail out to a
aoqi@0 424 // full GC.
aoqi@0 425 const size_t alignment = old_gen->virtual_space()->alignment();
aoqi@0 426 const size_t eden_used = eden_space->used_in_bytes();
aoqi@0 427 const size_t promoted = (size_t)size_policy->avg_promoted()->padded_average();
aoqi@0 428 const size_t absorb_size = align_size_up(eden_used + promoted, alignment);
aoqi@0 429 const size_t eden_capacity = eden_space->capacity_in_bytes();
aoqi@0 430
aoqi@0 431 if (absorb_size >= eden_capacity) {
aoqi@0 432 return false; // Must leave some space in eden.
aoqi@0 433 }
aoqi@0 434
aoqi@0 435 const size_t new_young_size = young_gen->capacity_in_bytes() - absorb_size;
aoqi@0 436 if (new_young_size < young_gen->min_gen_size()) {
aoqi@0 437 return false; // Respect young gen minimum size.
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 if (TraceAdaptiveGCBoundary && Verbose) {
aoqi@0 441 gclog_or_tty->print(" absorbing " SIZE_FORMAT "K: "
aoqi@0 442 "eden " SIZE_FORMAT "K->" SIZE_FORMAT "K "
aoqi@0 443 "from " SIZE_FORMAT "K, to " SIZE_FORMAT "K "
aoqi@0 444 "young_gen " SIZE_FORMAT "K->" SIZE_FORMAT "K ",
aoqi@0 445 absorb_size / K,
aoqi@0 446 eden_capacity / K, (eden_capacity - absorb_size) / K,
aoqi@0 447 young_gen->from_space()->used_in_bytes() / K,
aoqi@0 448 young_gen->to_space()->used_in_bytes() / K,
aoqi@0 449 young_gen->capacity_in_bytes() / K, new_young_size / K);
aoqi@0 450 }
aoqi@0 451
aoqi@0 452 // Fill the unused part of the old gen.
aoqi@0 453 MutableSpace* const old_space = old_gen->object_space();
aoqi@0 454 HeapWord* const unused_start = old_space->top();
aoqi@0 455 size_t const unused_words = pointer_delta(old_space->end(), unused_start);
aoqi@0 456
aoqi@0 457 if (unused_words > 0) {
aoqi@0 458 if (unused_words < CollectedHeap::min_fill_size()) {
aoqi@0 459 return false; // If the old gen cannot be filled, must give up.
aoqi@0 460 }
aoqi@0 461 CollectedHeap::fill_with_objects(unused_start, unused_words);
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 // Take the live data from eden and set both top and end in the old gen to
aoqi@0 465 // eden top. (Need to set end because reset_after_change() mangles the region
aoqi@0 466 // from end to virtual_space->high() in debug builds).
aoqi@0 467 HeapWord* const new_top = eden_space->top();
aoqi@0 468 old_gen->virtual_space()->expand_into(young_gen->virtual_space(),
aoqi@0 469 absorb_size);
aoqi@0 470 young_gen->reset_after_change();
aoqi@0 471 old_space->set_top(new_top);
aoqi@0 472 old_space->set_end(new_top);
aoqi@0 473 old_gen->reset_after_change();
aoqi@0 474
aoqi@0 475 // Update the object start array for the filler object and the data from eden.
aoqi@0 476 ObjectStartArray* const start_array = old_gen->start_array();
aoqi@0 477 for (HeapWord* p = unused_start; p < new_top; p += oop(p)->size()) {
aoqi@0 478 start_array->allocate_block(p);
aoqi@0 479 }
aoqi@0 480
aoqi@0 481 // Could update the promoted average here, but it is not typically updated at
aoqi@0 482 // full GCs and the value to use is unclear. Something like
aoqi@0 483 //
aoqi@0 484 // cur_promoted_avg + absorb_size / number_of_scavenges_since_last_full_gc.
aoqi@0 485
aoqi@0 486 size_policy->set_bytes_absorbed_from_eden(absorb_size);
aoqi@0 487 return true;
aoqi@0 488 }
aoqi@0 489
aoqi@0 490 void PSMarkSweep::allocate_stacks() {
aoqi@0 491 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 492 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 493
aoqi@0 494 PSYoungGen* young_gen = heap->young_gen();
aoqi@0 495
aoqi@0 496 MutableSpace* to_space = young_gen->to_space();
aoqi@0 497 _preserved_marks = (PreservedMark*)to_space->top();
aoqi@0 498 _preserved_count = 0;
aoqi@0 499
aoqi@0 500 // We want to calculate the size in bytes first.
aoqi@0 501 _preserved_count_max = pointer_delta(to_space->end(), to_space->top(), sizeof(jbyte));
aoqi@0 502 // Now divide by the size of a PreservedMark
aoqi@0 503 _preserved_count_max /= sizeof(PreservedMark);
aoqi@0 504 }
aoqi@0 505
aoqi@0 506
aoqi@0 507 void PSMarkSweep::deallocate_stacks() {
aoqi@0 508 _preserved_mark_stack.clear(true);
aoqi@0 509 _preserved_oop_stack.clear(true);
aoqi@0 510 _marking_stack.clear();
aoqi@0 511 _objarray_stack.clear(true);
aoqi@0 512 }
aoqi@0 513
aoqi@0 514 void PSMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) {
aoqi@0 515 // Recursively traverse all live objects and mark them
aoqi@0 516 GCTraceTime tm("phase 1", PrintGCDetails && Verbose, true, _gc_timer);
aoqi@0 517 trace(" 1");
aoqi@0 518
aoqi@0 519 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 520 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 521
aoqi@0 522 // Need to clear claim bits before the tracing starts.
aoqi@0 523 ClassLoaderDataGraph::clear_claimed_marks();
aoqi@0 524
aoqi@0 525 // General strong roots.
aoqi@0 526 {
aoqi@0 527 ParallelScavengeHeap::ParStrongRootsScope psrs;
aoqi@0 528 Universe::oops_do(mark_and_push_closure());
aoqi@0 529 JNIHandles::oops_do(mark_and_push_closure()); // Global (strong) JNI handles
aoqi@0 530 CLDToOopClosure mark_and_push_from_cld(mark_and_push_closure());
aoqi@0 531 CodeBlobToOopClosure each_active_code_blob(mark_and_push_closure(), /*do_marking=*/ true);
aoqi@0 532 Threads::oops_do(mark_and_push_closure(), &mark_and_push_from_cld, &each_active_code_blob);
aoqi@0 533 ObjectSynchronizer::oops_do(mark_and_push_closure());
aoqi@0 534 FlatProfiler::oops_do(mark_and_push_closure());
aoqi@0 535 Management::oops_do(mark_and_push_closure());
aoqi@0 536 JvmtiExport::oops_do(mark_and_push_closure());
aoqi@0 537 SystemDictionary::always_strong_oops_do(mark_and_push_closure());
aoqi@0 538 ClassLoaderDataGraph::always_strong_oops_do(mark_and_push_closure(), follow_klass_closure(), true);
aoqi@0 539 // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
aoqi@0 540 //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(mark_and_push_closure()));
aoqi@0 541 }
aoqi@0 542
aoqi@0 543 // Flush marking stack.
aoqi@0 544 follow_stack();
aoqi@0 545
aoqi@0 546 // Process reference objects found during marking
aoqi@0 547 {
aoqi@0 548 ref_processor()->setup_policy(clear_all_softrefs);
aoqi@0 549 const ReferenceProcessorStats& stats =
aoqi@0 550 ref_processor()->process_discovered_references(
aoqi@0 551 is_alive_closure(), mark_and_push_closure(), follow_stack_closure(), NULL, _gc_timer);
aoqi@0 552 gc_tracer()->report_gc_reference_stats(stats);
aoqi@0 553 }
aoqi@0 554
aoqi@0 555 // This is the point where the entire marking should have completed.
aoqi@0 556 assert(_marking_stack.is_empty(), "Marking should have completed");
aoqi@0 557
aoqi@0 558 // Unload classes and purge the SystemDictionary.
aoqi@0 559 bool purged_class = SystemDictionary::do_unloading(is_alive_closure());
aoqi@0 560
aoqi@0 561 // Unload nmethods.
aoqi@0 562 CodeCache::do_unloading(is_alive_closure(), purged_class);
aoqi@0 563
aoqi@0 564 // Prune dead klasses from subklass/sibling/implementor lists.
aoqi@0 565 Klass::clean_weak_klass_links(is_alive_closure());
aoqi@0 566
aoqi@0 567 // Delete entries for dead interned strings.
aoqi@0 568 StringTable::unlink(is_alive_closure());
aoqi@0 569
aoqi@0 570 // Clean up unreferenced symbols in symbol table.
aoqi@0 571 SymbolTable::unlink();
aoqi@0 572 _gc_tracer->report_object_count_after_gc(is_alive_closure());
aoqi@0 573 }
aoqi@0 574
aoqi@0 575
aoqi@0 576 void PSMarkSweep::mark_sweep_phase2() {
aoqi@0 577 GCTraceTime tm("phase 2", PrintGCDetails && Verbose, true, _gc_timer);
aoqi@0 578 trace("2");
aoqi@0 579
aoqi@0 580 // Now all live objects are marked, compute the new object addresses.
aoqi@0 581
aoqi@0 582 // It is not required that we traverse spaces in the same order in
aoqi@0 583 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
aoqi@0 584 // tracking expects us to do so. See comment under phase4.
aoqi@0 585
aoqi@0 586 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 587 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 588
aoqi@0 589 PSOldGen* old_gen = heap->old_gen();
aoqi@0 590
aoqi@0 591 // Begin compacting into the old gen
aoqi@0 592 PSMarkSweepDecorator::set_destination_decorator_tenured();
aoqi@0 593
aoqi@0 594 // This will also compact the young gen spaces.
aoqi@0 595 old_gen->precompact();
aoqi@0 596 }
aoqi@0 597
aoqi@0 598 // This should be moved to the shared markSweep code!
aoqi@0 599 class PSAlwaysTrueClosure: public BoolObjectClosure {
aoqi@0 600 public:
aoqi@0 601 bool do_object_b(oop p) { return true; }
aoqi@0 602 };
aoqi@0 603 static PSAlwaysTrueClosure always_true;
aoqi@0 604
aoqi@0 605 void PSMarkSweep::mark_sweep_phase3() {
aoqi@0 606 // Adjust the pointers to reflect the new locations
aoqi@0 607 GCTraceTime tm("phase 3", PrintGCDetails && Verbose, true, _gc_timer);
aoqi@0 608 trace("3");
aoqi@0 609
aoqi@0 610 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 611 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 612
aoqi@0 613 PSYoungGen* young_gen = heap->young_gen();
aoqi@0 614 PSOldGen* old_gen = heap->old_gen();
aoqi@0 615
aoqi@0 616 // Need to clear claim bits before the tracing starts.
aoqi@0 617 ClassLoaderDataGraph::clear_claimed_marks();
aoqi@0 618
aoqi@0 619 // General strong roots.
aoqi@0 620 Universe::oops_do(adjust_pointer_closure());
aoqi@0 621 JNIHandles::oops_do(adjust_pointer_closure()); // Global (strong) JNI handles
aoqi@0 622 CLDToOopClosure adjust_from_cld(adjust_pointer_closure());
aoqi@0 623 Threads::oops_do(adjust_pointer_closure(), &adjust_from_cld, NULL);
aoqi@0 624 ObjectSynchronizer::oops_do(adjust_pointer_closure());
aoqi@0 625 FlatProfiler::oops_do(adjust_pointer_closure());
aoqi@0 626 Management::oops_do(adjust_pointer_closure());
aoqi@0 627 JvmtiExport::oops_do(adjust_pointer_closure());
aoqi@0 628 // SO_AllClasses
aoqi@0 629 SystemDictionary::oops_do(adjust_pointer_closure());
aoqi@0 630 ClassLoaderDataGraph::oops_do(adjust_pointer_closure(), adjust_klass_closure(), true);
aoqi@0 631
aoqi@0 632 // Now adjust pointers in remaining weak roots. (All of which should
aoqi@0 633 // have been cleared if they pointed to non-surviving objects.)
aoqi@0 634 // Global (weak) JNI handles
aoqi@0 635 JNIHandles::weak_oops_do(&always_true, adjust_pointer_closure());
aoqi@0 636
aoqi@0 637 CodeCache::oops_do(adjust_pointer_closure());
aoqi@0 638 StringTable::oops_do(adjust_pointer_closure());
aoqi@0 639 ref_processor()->weak_oops_do(adjust_pointer_closure());
aoqi@0 640 PSScavenge::reference_processor()->weak_oops_do(adjust_pointer_closure());
aoqi@0 641
aoqi@0 642 adjust_marks();
aoqi@0 643
aoqi@0 644 young_gen->adjust_pointers();
aoqi@0 645 old_gen->adjust_pointers();
aoqi@0 646 }
aoqi@0 647
aoqi@0 648 void PSMarkSweep::mark_sweep_phase4() {
aoqi@0 649 EventMark m("4 compact heap");
aoqi@0 650 GCTraceTime tm("phase 4", PrintGCDetails && Verbose, true, _gc_timer);
aoqi@0 651 trace("4");
aoqi@0 652
aoqi@0 653 // All pointers are now adjusted, move objects accordingly
aoqi@0 654
aoqi@0 655 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 656 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 657
aoqi@0 658 PSYoungGen* young_gen = heap->young_gen();
aoqi@0 659 PSOldGen* old_gen = heap->old_gen();
aoqi@0 660
aoqi@0 661 old_gen->compact();
aoqi@0 662 young_gen->compact();
aoqi@0 663 }
aoqi@0 664
aoqi@0 665 jlong PSMarkSweep::millis_since_last_gc() {
aoqi@0 666 // We need a monotonically non-deccreasing time in ms but
aoqi@0 667 // os::javaTimeMillis() does not guarantee monotonicity.
aoqi@0 668 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
aoqi@0 669 jlong ret_val = now - _time_of_last_gc;
aoqi@0 670 // XXX See note in genCollectedHeap::millis_since_last_gc().
aoqi@0 671 if (ret_val < 0) {
aoqi@0 672 NOT_PRODUCT(warning("time warp: "INT64_FORMAT, ret_val);)
aoqi@0 673 return 0;
aoqi@0 674 }
aoqi@0 675 return ret_val;
aoqi@0 676 }
aoqi@0 677
aoqi@0 678 void PSMarkSweep::reset_millis_since_last_gc() {
aoqi@0 679 // We need a monotonically non-deccreasing time in ms but
aoqi@0 680 // os::javaTimeMillis() does not guarantee monotonicity.
aoqi@0 681 _time_of_last_gc = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
aoqi@0 682 }

mercurial