annotate src/share/vm/memory/genCollectedHeap.cpp @ 5649:46d7652b223c

8026853: Prepare GC code for collector policy regression fix Summary: Cleanup related to the NewSize and MaxNewSize bugs Reviewed-by: tschatzl, jcoomes, ehelin
author jwilhelm
date Mon, 21 Oct 2013 18:56:20 +0200
parents 4c84d351cca9
children 8f07aa079343
rev   line source
duke@0 1 /*
sla@4802 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/symbolTable.hpp"
stefank@1879 27 #include "classfile/systemDictionary.hpp"
stefank@1879 28 #include "classfile/vmSymbols.hpp"
stefank@1879 29 #include "code/icBuffer.hpp"
stefank@1879 30 #include "gc_implementation/shared/collectorCounters.hpp"
sla@4802 31 #include "gc_implementation/shared/gcTraceTime.hpp"
stefank@1879 32 #include "gc_implementation/shared/vmGCOperations.hpp"
stefank@1879 33 #include "gc_interface/collectedHeap.inline.hpp"
stefank@1879 34 #include "memory/filemap.hpp"
stefank@1879 35 #include "memory/gcLocker.inline.hpp"
stefank@1879 36 #include "memory/genCollectedHeap.hpp"
stefank@1879 37 #include "memory/genOopClosures.inline.hpp"
stefank@1879 38 #include "memory/generation.inline.hpp"
stefank@1879 39 #include "memory/generationSpec.hpp"
stefank@1879 40 #include "memory/resourceArea.hpp"
stefank@1879 41 #include "memory/sharedHeap.hpp"
stefank@1879 42 #include "memory/space.hpp"
stefank@1879 43 #include "oops/oop.inline.hpp"
stefank@1879 44 #include "oops/oop.inline2.hpp"
stefank@1879 45 #include "runtime/biasedLocking.hpp"
stefank@1879 46 #include "runtime/fprofiler.hpp"
stefank@1879 47 #include "runtime/handles.hpp"
stefank@1879 48 #include "runtime/handles.inline.hpp"
stefank@1879 49 #include "runtime/java.hpp"
stefank@1879 50 #include "runtime/vmThread.hpp"
stefank@1879 51 #include "services/memoryService.hpp"
stefank@1879 52 #include "utilities/vmError.hpp"
stefank@1879 53 #include "utilities/workgroup.hpp"
jprovino@4107 54 #include "utilities/macros.hpp"
jprovino@4107 55 #if INCLUDE_ALL_GCS
stefank@1879 56 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
stefank@1879 57 #include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"
jprovino@4107 58 #endif // INCLUDE_ALL_GCS
duke@0 59
duke@0 60 GenCollectedHeap* GenCollectedHeap::_gch;
duke@0 61 NOT_PRODUCT(size_t GenCollectedHeap::_skip_header_HeapWords = 0;)
duke@0 62
duke@0 63 // The set of potentially parallel tasks in strong root scanning.
duke@0 64 enum GCH_process_strong_roots_tasks {
duke@0 65 // We probably want to parallelize both of these internally, but for now...
duke@0 66 GCH_PS_younger_gens,
duke@0 67 // Leave this one last.
duke@0 68 GCH_PS_NumElements
duke@0 69 };
duke@0 70
duke@0 71 GenCollectedHeap::GenCollectedHeap(GenCollectorPolicy *policy) :
duke@0 72 SharedHeap(policy),
duke@0 73 _gen_policy(policy),
duke@0 74 _gen_process_strong_tasks(new SubTasksDone(GCH_PS_NumElements)),
duke@0 75 _full_collections_completed(0)
duke@0 76 {
duke@0 77 if (_gen_process_strong_tasks == NULL ||
duke@0 78 !_gen_process_strong_tasks->valid()) {
duke@0 79 vm_exit_during_initialization("Failed necessary allocation.");
duke@0 80 }
duke@0 81 assert(policy != NULL, "Sanity check");
duke@0 82 }
duke@0 83
duke@0 84 jint GenCollectedHeap::initialize() {
ysr@1166 85 CollectedHeap::pre_initialize();
ysr@1166 86
duke@0 87 int i;
duke@0 88 _n_gens = gen_policy()->number_of_generations();
duke@0 89
duke@0 90 // While there are no constraints in the GC code that HeapWordSize
duke@0 91 // be any particular value, there are multiple other areas in the
duke@0 92 // system which believe this to be true (e.g. oop->object_size in some
duke@0 93 // cases incorrectly returns the size in wordSize units rather than
duke@0 94 // HeapWordSize).
duke@0 95 guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
duke@0 96
duke@0 97 // The heap must be at least as aligned as generations.
stefank@5143 98 size_t gen_alignment = Generation::GenGrain;
duke@0 99
duke@0 100 _gen_specs = gen_policy()->generations();
duke@0 101
duke@0 102 // Make sure the sizes are all aligned.
duke@0 103 for (i = 0; i < _n_gens; i++) {
stefank@5143 104 _gen_specs[i]->align(gen_alignment);
duke@0 105 }
duke@0 106
duke@0 107 // Allocate space for the heap.
duke@0 108
duke@0 109 char* heap_address;
duke@0 110 size_t total_reserved = 0;
duke@0 111 int n_covered_regions = 0;
stefank@5143 112 ReservedSpace heap_rs;
duke@0 113
stefank@5143 114 size_t heap_alignment = collector_policy()->max_alignment();
stefank@5143 115
stefank@5143 116 heap_address = allocate(heap_alignment, &total_reserved,
duke@0 117 &n_covered_regions, &heap_rs);
duke@0 118
duke@0 119 if (!heap_rs.is_reserved()) {
duke@0 120 vm_shutdown_during_initialization(
duke@0 121 "Could not reserve enough space for object heap");
duke@0 122 return JNI_ENOMEM;
duke@0 123 }
duke@0 124
duke@0 125 _reserved = MemRegion((HeapWord*)heap_rs.base(),
duke@0 126 (HeapWord*)(heap_rs.base() + heap_rs.size()));
duke@0 127
duke@0 128 // It is important to do this in a way such that concurrent readers can't
duke@0 129 // temporarily think somethings in the heap. (Seen this happen in asserts.)
duke@0 130 _reserved.set_word_size(0);
duke@0 131 _reserved.set_start((HeapWord*)heap_rs.base());
coleenp@3602 132 size_t actual_heap_size = heap_rs.size();
duke@0 133 _reserved.set_end((HeapWord*)(heap_rs.base() + actual_heap_size));
duke@0 134
duke@0 135 _rem_set = collector_policy()->create_rem_set(_reserved, n_covered_regions);
duke@0 136 set_barrier_set(rem_set()->bs());
ysr@1166 137
duke@0 138 _gch = this;
duke@0 139
duke@0 140 for (i = 0; i < _n_gens; i++) {
coleenp@3602 141 ReservedSpace this_rs = heap_rs.first_part(_gen_specs[i]->max_size(), false, false);
duke@0 142 _gens[i] = _gen_specs[i]->init(this_rs, i, rem_set());
duke@0 143 heap_rs = heap_rs.last_part(_gen_specs[i]->max_size());
duke@0 144 }
ysr@1808 145 clear_incremental_collection_failed();
duke@0 146
jprovino@4107 147 #if INCLUDE_ALL_GCS
duke@0 148 // If we are running CMS, create the collector responsible
duke@0 149 // for collecting the CMS generations.
duke@0 150 if (collector_policy()->is_concurrent_mark_sweep_policy()) {
duke@0 151 bool success = create_cms_collector();
duke@0 152 if (!success) return JNI_ENOMEM;
duke@0 153 }
jprovino@4107 154 #endif // INCLUDE_ALL_GCS
duke@0 155
duke@0 156 return JNI_OK;
duke@0 157 }
duke@0 158
duke@0 159
duke@0 160 char* GenCollectedHeap::allocate(size_t alignment,
duke@0 161 size_t* _total_reserved,
duke@0 162 int* _n_covered_regions,
duke@0 163 ReservedSpace* heap_rs){
duke@0 164 const char overflow_msg[] = "The size of the object heap + VM data exceeds "
duke@0 165 "the maximum representable size";
duke@0 166
duke@0 167 // Now figure out the total size.
duke@0 168 size_t total_reserved = 0;
duke@0 169 int n_covered_regions = 0;
duke@0 170 const size_t pageSize = UseLargePages ?
duke@0 171 os::large_page_size() : os::vm_page_size();
duke@0 172
stefank@5143 173 assert(alignment % pageSize == 0, "Must be");
stefank@5143 174
duke@0 175 for (int i = 0; i < _n_gens; i++) {
duke@0 176 total_reserved += _gen_specs[i]->max_size();
duke@0 177 if (total_reserved < _gen_specs[i]->max_size()) {
duke@0 178 vm_exit_during_initialization(overflow_msg);
duke@0 179 }
duke@0 180 n_covered_regions += _gen_specs[i]->n_covered_regions();
duke@0 181 }
stefank@5143 182 assert(total_reserved % alignment == 0,
stefank@5143 183 err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment="
stefank@5143 184 SIZE_FORMAT, total_reserved, alignment));
duke@0 185
coleenp@3602 186 // Needed until the cardtable is fixed to have the right number
coleenp@3602 187 // of covered regions.
coleenp@3602 188 n_covered_regions += 2;
duke@0 189
stefank@5143 190 *_total_reserved = total_reserved;
stefank@5143 191 *_n_covered_regions = n_covered_regions;
duke@0 192
coleenp@3602 193 *heap_rs = Universe::reserve_heap(total_reserved, alignment);
coleenp@3602 194 return heap_rs->base();
duke@0 195 }
duke@0 196
duke@0 197
duke@0 198 void GenCollectedHeap::post_initialize() {
duke@0 199 SharedHeap::post_initialize();
duke@0 200 TwoGenerationCollectorPolicy *policy =
duke@0 201 (TwoGenerationCollectorPolicy *)collector_policy();
duke@0 202 guarantee(policy->is_two_generation_policy(), "Illegal policy type");
duke@0 203 DefNewGeneration* def_new_gen = (DefNewGeneration*) get_gen(0);
duke@0 204 assert(def_new_gen->kind() == Generation::DefNew ||
duke@0 205 def_new_gen->kind() == Generation::ParNew ||
duke@0 206 def_new_gen->kind() == Generation::ASParNew,
duke@0 207 "Wrong generation kind");
duke@0 208
duke@0 209 Generation* old_gen = get_gen(1);
duke@0 210 assert(old_gen->kind() == Generation::ConcurrentMarkSweep ||
duke@0 211 old_gen->kind() == Generation::ASConcurrentMarkSweep ||
duke@0 212 old_gen->kind() == Generation::MarkSweepCompact,
duke@0 213 "Wrong generation kind");
duke@0 214
duke@0 215 policy->initialize_size_policy(def_new_gen->eden()->capacity(),
duke@0 216 old_gen->capacity(),
duke@0 217 def_new_gen->from()->capacity());
duke@0 218 policy->initialize_gc_policy_counters();
duke@0 219 }
duke@0 220
duke@0 221 void GenCollectedHeap::ref_processing_init() {
duke@0 222 SharedHeap::ref_processing_init();
duke@0 223 for (int i = 0; i < _n_gens; i++) {
duke@0 224 _gens[i]->ref_processor_init();
duke@0 225 }
duke@0 226 }
duke@0 227
duke@0 228 size_t GenCollectedHeap::capacity() const {
duke@0 229 size_t res = 0;
duke@0 230 for (int i = 0; i < _n_gens; i++) {
duke@0 231 res += _gens[i]->capacity();
duke@0 232 }
duke@0 233 return res;
duke@0 234 }
duke@0 235
duke@0 236 size_t GenCollectedHeap::used() const {
duke@0 237 size_t res = 0;
duke@0 238 for (int i = 0; i < _n_gens; i++) {
duke@0 239 res += _gens[i]->used();
duke@0 240 }
duke@0 241 return res;
duke@0 242 }
duke@0 243
coleenp@3602 244 // Save the "used_region" for generations level and lower.
coleenp@3602 245 void GenCollectedHeap::save_used_regions(int level) {
duke@0 246 assert(level < _n_gens, "Illegal level parameter");
duke@0 247 for (int i = level; i >= 0; i--) {
duke@0 248 _gens[i]->save_used_region();
duke@0 249 }
duke@0 250 }
duke@0 251
duke@0 252 size_t GenCollectedHeap::max_capacity() const {
duke@0 253 size_t res = 0;
duke@0 254 for (int i = 0; i < _n_gens; i++) {
duke@0 255 res += _gens[i]->max_capacity();
duke@0 256 }
duke@0 257 return res;
duke@0 258 }
duke@0 259
duke@0 260 // Update the _full_collections_completed counter
duke@0 261 // at the end of a stop-world full GC.
duke@0 262 unsigned int GenCollectedHeap::update_full_collections_completed() {
duke@0 263 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
duke@0 264 assert(_full_collections_completed <= _total_full_collections,
duke@0 265 "Can't complete more collections than were started");
duke@0 266 _full_collections_completed = _total_full_collections;
duke@0 267 ml.notify_all();
duke@0 268 return _full_collections_completed;
duke@0 269 }
duke@0 270
duke@0 271 // Update the _full_collections_completed counter, as appropriate,
duke@0 272 // at the end of a concurrent GC cycle. Note the conditional update
duke@0 273 // below to allow this method to be called by a concurrent collector
duke@0 274 // without synchronizing in any manner with the VM thread (which
duke@0 275 // may already have initiated a STW full collection "concurrently").
duke@0 276 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) {
duke@0 277 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
duke@0 278 assert((_full_collections_completed <= _total_full_collections) &&
duke@0 279 (count <= _total_full_collections),
duke@0 280 "Can't complete more collections than were started");
duke@0 281 if (count > _full_collections_completed) {
duke@0 282 _full_collections_completed = count;
duke@0 283 ml.notify_all();
duke@0 284 }
duke@0 285 return _full_collections_completed;
duke@0 286 }
duke@0 287
duke@0 288
duke@0 289 #ifndef PRODUCT
duke@0 290 // Override of memory state checking method in CollectedHeap:
duke@0 291 // Some collectors (CMS for example) can't have badHeapWordVal written
duke@0 292 // in the first two words of an object. (For instance , in the case of
duke@0 293 // CMS these words hold state used to synchronize between certain
duke@0 294 // (concurrent) GC steps and direct allocating mutators.)
duke@0 295 // The skip_header_HeapWords() method below, allows us to skip
duke@0 296 // over the requisite number of HeapWord's. Note that (for
duke@0 297 // generational collectors) this means that those many words are
duke@0 298 // skipped in each object, irrespective of the generation in which
duke@0 299 // that object lives. The resultant loss of precision seems to be
duke@0 300 // harmless and the pain of avoiding that imprecision appears somewhat
duke@0 301 // higher than we are prepared to pay for such rudimentary debugging
duke@0 302 // support.
duke@0 303 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr,
duke@0 304 size_t size) {
duke@0 305 if (CheckMemoryInitialization && ZapUnusedHeapArea) {
duke@0 306 // We are asked to check a size in HeapWords,
duke@0 307 // but the memory is mangled in juint words.
duke@0 308 juint* start = (juint*) (addr + skip_header_HeapWords());
duke@0 309 juint* end = (juint*) (addr + size);
duke@0 310 for (juint* slot = start; slot < end; slot += 1) {
duke@0 311 assert(*slot == badHeapWordVal,
duke@0 312 "Found non badHeapWordValue in pre-allocation check");
duke@0 313 }
duke@0 314 }
duke@0 315 }
duke@0 316 #endif
duke@0 317
duke@0 318 HeapWord* GenCollectedHeap::attempt_allocation(size_t size,
duke@0 319 bool is_tlab,
duke@0 320 bool first_only) {
duke@0 321 HeapWord* res;
duke@0 322 for (int i = 0; i < _n_gens; i++) {
duke@0 323 if (_gens[i]->should_allocate(size, is_tlab)) {
duke@0 324 res = _gens[i]->allocate(size, is_tlab);
duke@0 325 if (res != NULL) return res;
duke@0 326 else if (first_only) break;
duke@0 327 }
duke@0 328 }
duke@0 329 // Otherwise...
duke@0 330 return NULL;
duke@0 331 }
duke@0 332
duke@0 333 HeapWord* GenCollectedHeap::mem_allocate(size_t size,
duke@0 334 bool* gc_overhead_limit_was_exceeded) {
duke@0 335 return collector_policy()->mem_allocate_work(size,
tonyp@2536 336 false /* is_tlab */,
duke@0 337 gc_overhead_limit_was_exceeded);
duke@0 338 }
duke@0 339
duke@0 340 bool GenCollectedHeap::must_clear_all_soft_refs() {
duke@0 341 return _gc_cause == GCCause::_last_ditch_collection;
duke@0 342 }
duke@0 343
duke@0 344 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
ysr@1440 345 return UseConcMarkSweepGC &&
ysr@1440 346 ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
ysr@1440 347 (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent));
duke@0 348 }
duke@0 349
duke@0 350 void GenCollectedHeap::do_collection(bool full,
duke@0 351 bool clear_all_soft_refs,
duke@0 352 size_t size,
duke@0 353 bool is_tlab,
duke@0 354 int max_level) {
duke@0 355 bool prepared_for_verification = false;
duke@0 356 ResourceMark rm;
duke@0 357 DEBUG_ONLY(Thread* my_thread = Thread::current();)
duke@0 358
duke@0 359 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
duke@0 360 assert(my_thread->is_VM_thread() ||
duke@0 361 my_thread->is_ConcurrentGC_thread(),
duke@0 362 "incorrect thread type capability");
jmasa@1387 363 assert(Heap_lock->is_locked(),
jmasa@1387 364 "the requesting thread should have the Heap_lock");
duke@0 365 guarantee(!is_gc_active(), "collection is not reentrant");
duke@0 366 assert(max_level < n_gens(), "sanity check");
duke@0 367
duke@0 368 if (GC_locker::check_active_before_gc()) {
duke@0 369 return; // GC is disabled (e.g. JNI GetXXXCritical operation)
duke@0 370 }
duke@0 371
jmasa@1387 372 const bool do_clear_all_soft_refs = clear_all_soft_refs ||
jmasa@1387 373 collector_policy()->should_clear_all_soft_refs();
jmasa@1387 374
jmasa@1387 375 ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy());
jmasa@1387 376
jmasa@4580 377 const size_t metadata_prev_used = MetaspaceAux::allocated_used_bytes();
duke@0 378
never@3064 379 print_heap_before_gc();
duke@0 380
duke@0 381 {
duke@0 382 FlagSetting fl(_is_gc_active, true);
duke@0 383
duke@0 384 bool complete = full && (max_level == (n_gens()-1));
brutisso@3332 385 const char* gc_cause_prefix = complete ? "Full GC" : "GC";
duke@0 386 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
duke@0 387 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
sla@4802 388 GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL);
duke@0 389
duke@0 390 gc_prologue(complete);
duke@0 391 increment_total_collections(complete);
duke@0 392
duke@0 393 size_t gch_prev_used = used();
duke@0 394
duke@0 395 int starting_level = 0;
duke@0 396 if (full) {
duke@0 397 // Search for the oldest generation which will collect all younger
duke@0 398 // generations, and start collection loop there.
duke@0 399 for (int i = max_level; i >= 0; i--) {
duke@0 400 if (_gens[i]->full_collects_younger_generations()) {
duke@0 401 starting_level = i;
duke@0 402 break;
duke@0 403 }
duke@0 404 }
duke@0 405 }
duke@0 406
duke@0 407 bool must_restore_marks_for_biased_locking = false;
duke@0 408
duke@0 409 int max_level_collected = starting_level;
duke@0 410 for (int i = starting_level; i <= max_level; i++) {
duke@0 411 if (_gens[i]->should_collect(full, size, is_tlab)) {
dcubed@880 412 if (i == n_gens() - 1) { // a major collection is to happen
dcubed@880 413 if (!complete) {
dcubed@880 414 // The full_collections increment was missed above.
dcubed@880 415 increment_total_full_collections();
dcubed@880 416 }
sla@4802 417 pre_full_gc_dump(NULL); // do any pre full gc dumps
dcubed@880 418 }
duke@0 419 // Timer for individual generations. Last argument is false: no CR
sla@4802 420 // FIXME: We should try to start the timing earlier to cover more of the GC pause
sla@4802 421 GCTraceTime t1(_gens[i]->short_name(), PrintGCDetails, false, NULL);
duke@0 422 TraceCollectorStats tcs(_gens[i]->counters());
fparain@2453 423 TraceMemoryManagerStats tmms(_gens[i]->kind(),gc_cause());
duke@0 424
duke@0 425 size_t prev_used = _gens[i]->used();
duke@0 426 _gens[i]->stat_record()->invocations++;
duke@0 427 _gens[i]->stat_record()->accumulated_time.start();
duke@0 428
jmasa@263 429 // Must be done anew before each collection because
jmasa@263 430 // a previous collection will do mangling and will
jmasa@263 431 // change top of some spaces.
jmasa@263 432 record_gen_tops_before_GC();
jmasa@263 433
duke@0 434 if (PrintGC && Verbose) {
duke@0 435 gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT,
duke@0 436 i,
duke@0 437 _gens[i]->stat_record()->invocations,
duke@0 438 size*HeapWordSize);
duke@0 439 }
duke@0 440
duke@0 441 if (VerifyBeforeGC && i >= VerifyGCLevel &&
duke@0 442 total_collections() >= VerifyGCStartAt) {
duke@0 443 HandleMark hm; // Discard invalid handles created during verification
duke@0 444 if (!prepared_for_verification) {
duke@0 445 prepare_for_verify();
duke@0 446 prepared_for_verification = true;
duke@0 447 }
stefank@4583 448 Universe::verify(" VerifyBeforeGC:");
duke@0 449 }
duke@0 450 COMPILER2_PRESENT(DerivedPointerTable::clear());
duke@0 451
duke@0 452 if (!must_restore_marks_for_biased_locking &&
duke@0 453 _gens[i]->performs_in_place_marking()) {
duke@0 454 // We perform this mark word preservation work lazily
duke@0 455 // because it's only at this point that we know whether we
duke@0 456 // absolutely have to do it; we want to avoid doing it for
duke@0 457 // scavenge-only collections where it's unnecessary
duke@0 458 must_restore_marks_for_biased_locking = true;
duke@0 459 BiasedLocking::preserve_marks();
duke@0 460 }
duke@0 461
duke@0 462 // Do collection work
duke@0 463 {
duke@0 464 // Note on ref discovery: For what appear to be historical reasons,
duke@0 465 // GCH enables and disabled (by enqueing) refs discovery.
duke@0 466 // In the future this should be moved into the generation's
duke@0 467 // collect method so that ref discovery and enqueueing concerns
duke@0 468 // are local to a generation. The collect method could return
duke@0 469 // an appropriate indication in the case that notification on
duke@0 470 // the ref lock was needed. This will make the treatment of
duke@0 471 // weak refs more uniform (and indeed remove such concerns
duke@0 472 // from GCH). XXX
duke@0 473
duke@0 474 HandleMark hm; // Discard invalid handles created during gc
duke@0 475 save_marks(); // save marks for all gens
duke@0 476 // We want to discover references, but not process them yet.
duke@0 477 // This mode is disabled in process_discovered_references if the
duke@0 478 // generation does some collection work, or in
duke@0 479 // enqueue_discovered_references if the generation returns
duke@0 480 // without doing any work.
duke@0 481 ReferenceProcessor* rp = _gens[i]->ref_processor();
duke@0 482 // If the discovery of ("weak") refs in this generation is
duke@0 483 // atomic wrt other collectors in this configuration, we
duke@0 484 // are guaranteed to have empty discovered ref lists.
duke@0 485 if (rp->discovery_is_atomic()) {
johnc@2740 486 rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);
jmasa@1387 487 rp->setup_policy(do_clear_all_soft_refs);
duke@0 488 } else {
ysr@453 489 // collect() below will enable discovery as appropriate
duke@0 490 }
jmasa@1387 491 _gens[i]->collect(full, do_clear_all_soft_refs, size, is_tlab);
duke@0 492 if (!rp->enqueuing_is_done()) {
duke@0 493 rp->enqueue_discovered_references();
duke@0 494 } else {
duke@0 495 rp->set_enqueuing_is_done(false);
duke@0 496 }
duke@0 497 rp->verify_no_references_recorded();
duke@0 498 }
duke@0 499 max_level_collected = i;
duke@0 500
duke@0 501 // Determine if allocation request was met.
duke@0 502 if (size > 0) {
duke@0 503 if (!is_tlab || _gens[i]->supports_tlab_allocation()) {
duke@0 504 if (size*HeapWordSize <= _gens[i]->unsafe_max_alloc_nogc()) {
duke@0 505 size = 0;
duke@0 506 }
duke@0 507 }
duke@0 508 }
duke@0 509
duke@0 510 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
duke@0 511
duke@0 512 _gens[i]->stat_record()->accumulated_time.stop();
duke@0 513
duke@0 514 update_gc_stats(i, full);
duke@0 515
duke@0 516 if (VerifyAfterGC && i >= VerifyGCLevel &&
duke@0 517 total_collections() >= VerifyGCStartAt) {
duke@0 518 HandleMark hm; // Discard invalid handles created during verification
stefank@4583 519 Universe::verify(" VerifyAfterGC:");
duke@0 520 }
duke@0 521
duke@0 522 if (PrintGCDetails) {
duke@0 523 gclog_or_tty->print(":");
duke@0 524 _gens[i]->print_heap_change(prev_used);
duke@0 525 }
duke@0 526 }
duke@0 527 }
duke@0 528
duke@0 529 // Update "complete" boolean wrt what actually transpired --
duke@0 530 // for instance, a promotion failure could have led to
duke@0 531 // a whole heap collection.
duke@0 532 complete = complete || (max_level_collected == n_gens() - 1);
duke@0 533
ysr@615 534 if (complete) { // We did a "major" collection
sla@4802 535 // FIXME: See comment at pre_full_gc_dump call
sla@4802 536 post_full_gc_dump(NULL); // do any post full gc dumps
ysr@615 537 }
ysr@615 538
duke@0 539 if (PrintGCDetails) {
duke@0 540 print_heap_change(gch_prev_used);
duke@0 541
coleenp@3602 542 // Print metaspace info for full GC with PrintGCDetails flag.
duke@0 543 if (complete) {
coleenp@3602 544 MetaspaceAux::print_metaspace_change(metadata_prev_used);
duke@0 545 }
duke@0 546 }
duke@0 547
duke@0 548 for (int j = max_level_collected; j >= 0; j -= 1) {
duke@0 549 // Adjust generation sizes.
duke@0 550 _gens[j]->compute_new_size();
duke@0 551 }
duke@0 552
duke@0 553 if (complete) {
mgerdin@4349 554 // Delete metaspaces for unloaded class loaders and clean up loader_data graph
mgerdin@4349 555 ClassLoaderDataGraph::purge();
jmasa@4580 556 MetaspaceAux::verify_metrics();
coleenp@3602 557 // Resize the metaspace capacity after full collections
coleenp@3602 558 MetaspaceGC::compute_new_size();
duke@0 559 update_full_collections_completed();
duke@0 560 }
duke@0 561
duke@0 562 // Track memory usage and detect low memory after GC finishes
duke@0 563 MemoryService::track_memory_usage();
duke@0 564
duke@0 565 gc_epilogue(complete);
duke@0 566
duke@0 567 if (must_restore_marks_for_biased_locking) {
duke@0 568 BiasedLocking::restore_marks();
duke@0 569 }
duke@0 570 }
duke@0 571
duke@0 572 AdaptiveSizePolicy* sp = gen_policy()->size_policy();
duke@0 573 AdaptiveSizePolicyOutput(sp, total_collections());
duke@0 574
never@3064 575 print_heap_after_gc();
duke@0 576
jmasa@546 577 #ifdef TRACESPINNING
jmasa@546 578 ParallelTaskTerminator::print_termination_counts();
jmasa@546 579 #endif
duke@0 580 }
duke@0 581
duke@0 582 HeapWord* GenCollectedHeap::satisfy_failed_allocation(size_t size, bool is_tlab) {
duke@0 583 return collector_policy()->satisfy_failed_allocation(size, is_tlab);
duke@0 584 }
duke@0 585
jmasa@2922 586 void GenCollectedHeap::set_par_threads(uint t) {
duke@0 587 SharedHeap::set_par_threads(t);
jmasa@1753 588 _gen_process_strong_tasks->set_n_threads(t);
duke@0 589 }
duke@0 590
duke@0 591 void GenCollectedHeap::
duke@0 592 gen_process_strong_roots(int level,
duke@0 593 bool younger_gens_as_roots,
jrose@989 594 bool activate_scope,
coleenp@3602 595 bool is_scavenging,
duke@0 596 SharedHeap::ScanningOption so,
jrose@989 597 OopsInGenClosure* not_older_gens,
jrose@989 598 bool do_code_roots,
coleenp@3602 599 OopsInGenClosure* older_gens,
coleenp@3602 600 KlassClosure* klass_closure) {
duke@0 601 // General strong roots.
jrose@989 602
jrose@989 603 if (!do_code_roots) {
coleenp@3602 604 SharedHeap::process_strong_roots(activate_scope, is_scavenging, so,
coleenp@3602 605 not_older_gens, NULL, klass_closure);
jrose@989 606 } else {
jrose@989 607 bool do_code_marking = (activate_scope || nmethod::oops_do_marking_is_active());
jrose@989 608 CodeBlobToOopClosure code_roots(not_older_gens, /*do_marking=*/ do_code_marking);
coleenp@3602 609 SharedHeap::process_strong_roots(activate_scope, is_scavenging, so,
coleenp@3602 610 not_older_gens, &code_roots, klass_closure);
jrose@989 611 }
duke@0 612
duke@0 613 if (younger_gens_as_roots) {
duke@0 614 if (!_gen_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) {
duke@0 615 for (int i = 0; i < level; i++) {
duke@0 616 not_older_gens->set_generation(_gens[i]);
duke@0 617 _gens[i]->oop_iterate(not_older_gens);
duke@0 618 }
duke@0 619 not_older_gens->reset_generation();
duke@0 620 }
duke@0 621 }
duke@0 622 // When collection is parallel, all threads get to cooperate to do
duke@0 623 // older-gen scanning.
duke@0 624 for (int i = level+1; i < _n_gens; i++) {
duke@0 625 older_gens->set_generation(_gens[i]);
duke@0 626 rem_set()->younger_refs_iterate(_gens[i], older_gens);
duke@0 627 older_gens->reset_generation();
duke@0 628 }
duke@0 629
duke@0 630 _gen_process_strong_tasks->all_tasks_completed();
duke@0 631 }
duke@0 632
duke@0 633 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure,
stefank@4576 634 CodeBlobClosure* code_roots) {
stefank@4576 635 SharedHeap::process_weak_roots(root_closure, code_roots);
duke@0 636 // "Local" "weak" refs
duke@0 637 for (int i = 0; i < _n_gens; i++) {
duke@0 638 _gens[i]->ref_processor()->weak_oops_do(root_closure);
duke@0 639 }
duke@0 640 }
duke@0 641
duke@0 642 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \
duke@0 643 void GenCollectedHeap:: \
duke@0 644 oop_since_save_marks_iterate(int level, \
duke@0 645 OopClosureType* cur, \
duke@0 646 OopClosureType* older) { \
duke@0 647 _gens[level]->oop_since_save_marks_iterate##nv_suffix(cur); \
duke@0 648 for (int i = level+1; i < n_gens(); i++) { \
duke@0 649 _gens[i]->oop_since_save_marks_iterate##nv_suffix(older); \
duke@0 650 } \
duke@0 651 }
duke@0 652
duke@0 653 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN)
duke@0 654
duke@0 655 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN
duke@0 656
duke@0 657 bool GenCollectedHeap::no_allocs_since_save_marks(int level) {
duke@0 658 for (int i = level; i < _n_gens; i++) {
duke@0 659 if (!_gens[i]->no_allocs_since_save_marks()) return false;
duke@0 660 }
coleenp@3602 661 return true;
duke@0 662 }
duke@0 663
duke@0 664 bool GenCollectedHeap::supports_inline_contig_alloc() const {
duke@0 665 return _gens[0]->supports_inline_contig_alloc();
duke@0 666 }
duke@0 667
duke@0 668 HeapWord** GenCollectedHeap::top_addr() const {
duke@0 669 return _gens[0]->top_addr();
duke@0 670 }
duke@0 671
duke@0 672 HeapWord** GenCollectedHeap::end_addr() const {
duke@0 673 return _gens[0]->end_addr();
duke@0 674 }
duke@0 675
duke@0 676 size_t GenCollectedHeap::unsafe_max_alloc() {
duke@0 677 return _gens[0]->unsafe_max_alloc_nogc();
duke@0 678 }
duke@0 679
duke@0 680 // public collection interfaces
duke@0 681
duke@0 682 void GenCollectedHeap::collect(GCCause::Cause cause) {
duke@0 683 if (should_do_concurrent_full_gc(cause)) {
jprovino@4107 684 #if INCLUDE_ALL_GCS
duke@0 685 // mostly concurrent full collection
duke@0 686 collect_mostly_concurrent(cause);
jprovino@4107 687 #else // INCLUDE_ALL_GCS
duke@0 688 ShouldNotReachHere();
jprovino@4107 689 #endif // INCLUDE_ALL_GCS
duke@0 690 } else {
duke@0 691 #ifdef ASSERT
duke@0 692 if (cause == GCCause::_scavenge_alot) {
duke@0 693 // minor collection only
duke@0 694 collect(cause, 0);
duke@0 695 } else {
duke@0 696 // Stop-the-world full collection
duke@0 697 collect(cause, n_gens() - 1);
duke@0 698 }
duke@0 699 #else
duke@0 700 // Stop-the-world full collection
duke@0 701 collect(cause, n_gens() - 1);
duke@0 702 #endif
duke@0 703 }
duke@0 704 }
duke@0 705
duke@0 706 void GenCollectedHeap::collect(GCCause::Cause cause, int max_level) {
duke@0 707 // The caller doesn't have the Heap_lock
duke@0 708 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
duke@0 709 MutexLocker ml(Heap_lock);
duke@0 710 collect_locked(cause, max_level);
duke@0 711 }
duke@0 712
duke@0 713 void GenCollectedHeap::collect_locked(GCCause::Cause cause) {
duke@0 714 // The caller has the Heap_lock
duke@0 715 assert(Heap_lock->owned_by_self(), "this thread should own the Heap_lock");
duke@0 716 collect_locked(cause, n_gens() - 1);
duke@0 717 }
duke@0 718
duke@0 719 // this is the private collection interface
duke@0 720 // The Heap_lock is expected to be held on entry.
duke@0 721
duke@0 722 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) {
duke@0 723 // Read the GC count while holding the Heap_lock
duke@0 724 unsigned int gc_count_before = total_collections();
duke@0 725 unsigned int full_gc_count_before = total_full_collections();
duke@0 726 {
duke@0 727 MutexUnlocker mu(Heap_lock); // give up heap lock, execute gets it back
duke@0 728 VM_GenCollectFull op(gc_count_before, full_gc_count_before,
duke@0 729 cause, max_level);
duke@0 730 VMThread::execute(&op);
duke@0 731 }
duke@0 732 }
duke@0 733
jprovino@4107 734 #if INCLUDE_ALL_GCS
duke@0 735 bool GenCollectedHeap::create_cms_collector() {
duke@0 736
duke@0 737 assert(((_gens[1]->kind() == Generation::ConcurrentMarkSweep) ||
coleenp@3602 738 (_gens[1]->kind() == Generation::ASConcurrentMarkSweep)),
duke@0 739 "Unexpected generation kinds");
duke@0 740 // Skip two header words in the block content verification
duke@0 741 NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
duke@0 742 CMSCollector* collector = new CMSCollector(
duke@0 743 (ConcurrentMarkSweepGeneration*)_gens[1],
duke@0 744 _rem_set->as_CardTableRS(),
duke@0 745 (ConcurrentMarkSweepPolicy*) collector_policy());
duke@0 746
duke@0 747 if (collector == NULL || !collector->completed_initialization()) {
duke@0 748 if (collector) {
duke@0 749 delete collector; // Be nice in embedded situation
duke@0 750 }
duke@0 751 vm_shutdown_during_initialization("Could not create CMS collector");
duke@0 752 return false;
duke@0 753 }
duke@0 754 return true; // success
duke@0 755 }
duke@0 756
duke@0 757 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) {
duke@0 758 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
duke@0 759
duke@0 760 MutexLocker ml(Heap_lock);
duke@0 761 // Read the GC counts while holding the Heap_lock
duke@0 762 unsigned int full_gc_count_before = total_full_collections();
duke@0 763 unsigned int gc_count_before = total_collections();
duke@0 764 {
duke@0 765 MutexUnlocker mu(Heap_lock);
duke@0 766 VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
duke@0 767 VMThread::execute(&op);
duke@0 768 }
duke@0 769 }
jprovino@4107 770 #endif // INCLUDE_ALL_GCS
duke@0 771
coleenp@3602 772 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
coleenp@3602 773 do_full_collection(clear_all_soft_refs, _n_gens - 1);
coleenp@3602 774 }
duke@0 775
duke@0 776 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs,
duke@0 777 int max_level) {
duke@0 778 int local_max_level;
ysr@1901 779 if (!incremental_collection_will_fail(false /* don't consult_young */) &&
duke@0 780 gc_cause() == GCCause::_gc_locker) {
duke@0 781 local_max_level = 0;
duke@0 782 } else {
duke@0 783 local_max_level = max_level;
duke@0 784 }
duke@0 785
duke@0 786 do_collection(true /* full */,
duke@0 787 clear_all_soft_refs /* clear_all_soft_refs */,
duke@0 788 0 /* size */,
duke@0 789 false /* is_tlab */,
duke@0 790 local_max_level /* max_level */);
duke@0 791 // Hack XXX FIX ME !!!
duke@0 792 // A scavenge may not have been attempted, or may have
duke@0 793 // been attempted and failed, because the old gen was too full
duke@0 794 if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker &&
ysr@1901 795 incremental_collection_will_fail(false /* don't consult_young */)) {
duke@0 796 if (PrintGCDetails) {
duke@0 797 gclog_or_tty->print_cr("GC locker: Trying a full collection "
duke@0 798 "because scavenge failed");
duke@0 799 }
duke@0 800 // This time allow the old gen to be collected as well
duke@0 801 do_collection(true /* full */,
duke@0 802 clear_all_soft_refs /* clear_all_soft_refs */,
duke@0 803 0 /* size */,
duke@0 804 false /* is_tlab */,
duke@0 805 n_gens() - 1 /* max_level */);
duke@0 806 }
duke@0 807 }
duke@0 808
jmasa@2474 809 bool GenCollectedHeap::is_in_young(oop p) {
jmasa@2474 810 bool result = ((HeapWord*)p) < _gens[_n_gens - 1]->reserved().start();
jmasa@2474 811 assert(result == _gens[0]->is_in_reserved(p),
jmasa@2474 812 err_msg("incorrect test - result=%d, p=" PTR_FORMAT, result, (void*)p));
jmasa@2474 813 return result;
jmasa@2474 814 }
jmasa@2474 815
stefank@2900 816 // Returns "TRUE" iff "p" points into the committed areas of the heap.
duke@0 817 bool GenCollectedHeap::is_in(const void* p) const {
duke@0 818 #ifndef ASSERT
johnc@4464 819 guarantee(VerifyBeforeGC ||
johnc@4464 820 VerifyDuringGC ||
johnc@4464 821 VerifyBeforeExit ||
johnc@4464 822 VerifyDuringStartup ||
johnc@4464 823 PrintAssembly ||
johnc@4464 824 tty->count() != 0 || // already printing
johnc@4464 825 VerifyAfterGC ||
bobv@1601 826 VMError::fatal_error_in_progress(), "too expensive");
bobv@1601 827
duke@0 828 #endif
duke@0 829 // This might be sped up with a cache of the last generation that
duke@0 830 // answered yes.
duke@0 831 for (int i = 0; i < _n_gens; i++) {
duke@0 832 if (_gens[i]->is_in(p)) return true;
duke@0 833 }
duke@0 834 // Otherwise...
duke@0 835 return false;
duke@0 836 }
duke@0 837
jmasa@2474 838 #ifdef ASSERT
jmasa@2474 839 // Don't implement this by using is_in_young(). This method is used
jmasa@2474 840 // in some cases to check that is_in_young() is correct.
jmasa@2474 841 bool GenCollectedHeap::is_in_partial_collection(const void* p) {
jmasa@2474 842 assert(is_in_reserved(p) || p == NULL,
jmasa@2474 843 "Does not work if address is non-null and outside of the heap");
jmasa@2474 844 return p < _gens[_n_gens - 2]->reserved().end() && p != NULL;
duke@0 845 }
jmasa@2474 846 #endif
duke@0 847
coleenp@3602 848 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) {
duke@0 849 for (int i = 0; i < _n_gens; i++) {
duke@0 850 _gens[i]->oop_iterate(cl);
duke@0 851 }
duke@0 852 }
duke@0 853
coleenp@3602 854 void GenCollectedHeap::oop_iterate(MemRegion mr, ExtendedOopClosure* cl) {
duke@0 855 for (int i = 0; i < _n_gens; i++) {
duke@0 856 _gens[i]->oop_iterate(mr, cl);
duke@0 857 }
duke@0 858 }
duke@0 859
duke@0 860 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
duke@0 861 for (int i = 0; i < _n_gens; i++) {
duke@0 862 _gens[i]->object_iterate(cl);
duke@0 863 }
duke@0 864 }
duke@0 865
jmasa@517 866 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
jmasa@517 867 for (int i = 0; i < _n_gens; i++) {
jmasa@517 868 _gens[i]->safe_object_iterate(cl);
jmasa@517 869 }
jmasa@517 870 }
jmasa@517 871
duke@0 872 Space* GenCollectedHeap::space_containing(const void* addr) const {
duke@0 873 for (int i = 0; i < _n_gens; i++) {
duke@0 874 Space* res = _gens[i]->space_containing(addr);
duke@0 875 if (res != NULL) return res;
duke@0 876 }
duke@0 877 // Otherwise...
duke@0 878 assert(false, "Could not find containing space");
duke@0 879 return NULL;
duke@0 880 }
duke@0 881
duke@0 882
duke@0 883 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
duke@0 884 assert(is_in_reserved(addr), "block_start of address outside of heap");
duke@0 885 for (int i = 0; i < _n_gens; i++) {
duke@0 886 if (_gens[i]->is_in_reserved(addr)) {
duke@0 887 assert(_gens[i]->is_in(addr),
duke@0 888 "addr should be in allocated part of generation");
duke@0 889 return _gens[i]->block_start(addr);
duke@0 890 }
duke@0 891 }
duke@0 892 assert(false, "Some generation should contain the address");
duke@0 893 return NULL;
duke@0 894 }
duke@0 895
duke@0 896 size_t GenCollectedHeap::block_size(const HeapWord* addr) const {
duke@0 897 assert(is_in_reserved(addr), "block_size of address outside of heap");
duke@0 898 for (int i = 0; i < _n_gens; i++) {
duke@0 899 if (_gens[i]->is_in_reserved(addr)) {
duke@0 900 assert(_gens[i]->is_in(addr),
duke@0 901 "addr should be in allocated part of generation");
duke@0 902 return _gens[i]->block_size(addr);
duke@0 903 }
duke@0 904 }
duke@0 905 assert(false, "Some generation should contain the address");
duke@0 906 return 0;
duke@0 907 }
duke@0 908
duke@0 909 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
duke@0 910 assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
duke@0 911 assert(block_start(addr) == addr, "addr must be a block start");
duke@0 912 for (int i = 0; i < _n_gens; i++) {
duke@0 913 if (_gens[i]->is_in_reserved(addr)) {
duke@0 914 return _gens[i]->block_is_obj(addr);
duke@0 915 }
duke@0 916 }
duke@0 917 assert(false, "Some generation should contain the address");
duke@0 918 return false;
duke@0 919 }
duke@0 920
duke@0 921 bool GenCollectedHeap::supports_tlab_allocation() const {
duke@0 922 for (int i = 0; i < _n_gens; i += 1) {
duke@0 923 if (_gens[i]->supports_tlab_allocation()) {
duke@0 924 return true;
duke@0 925 }
duke@0 926 }
duke@0 927 return false;
duke@0 928 }
duke@0 929
duke@0 930 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
duke@0 931 size_t result = 0;
duke@0 932 for (int i = 0; i < _n_gens; i += 1) {
duke@0 933 if (_gens[i]->supports_tlab_allocation()) {
duke@0 934 result += _gens[i]->tlab_capacity();
duke@0 935 }
duke@0 936 }
duke@0 937 return result;
duke@0 938 }
duke@0 939
duke@0 940 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
duke@0 941 size_t result = 0;
duke@0 942 for (int i = 0; i < _n_gens; i += 1) {
duke@0 943 if (_gens[i]->supports_tlab_allocation()) {
duke@0 944 result += _gens[i]->unsafe_max_tlab_alloc();
duke@0 945 }
duke@0 946 }
duke@0 947 return result;
duke@0 948 }
duke@0 949
duke@0 950 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) {
duke@0 951 bool gc_overhead_limit_was_exceeded;
tonyp@2536 952 return collector_policy()->mem_allocate_work(size /* size */,
tonyp@2536 953 true /* is_tlab */,
tonyp@2536 954 &gc_overhead_limit_was_exceeded);
duke@0 955 }
duke@0 956
duke@0 957 // Requires "*prev_ptr" to be non-NULL. Deletes and a block of minimal size
duke@0 958 // from the list headed by "*prev_ptr".
duke@0 959 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
duke@0 960 bool first = true;
duke@0 961 size_t min_size = 0; // "first" makes this conceptually infinite.
duke@0 962 ScratchBlock **smallest_ptr, *smallest;
duke@0 963 ScratchBlock *cur = *prev_ptr;
duke@0 964 while (cur) {
duke@0 965 assert(*prev_ptr == cur, "just checking");
duke@0 966 if (first || cur->num_words < min_size) {
duke@0 967 smallest_ptr = prev_ptr;
duke@0 968 smallest = cur;
duke@0 969 min_size = smallest->num_words;
duke@0 970 first = false;
duke@0 971 }
duke@0 972 prev_ptr = &cur->next;
duke@0 973 cur = cur->next;
duke@0 974 }
duke@0 975 smallest = *smallest_ptr;
duke@0 976 *smallest_ptr = smallest->next;
duke@0 977 return smallest;
duke@0 978 }
duke@0 979
duke@0 980 // Sort the scratch block list headed by res into decreasing size order,
duke@0 981 // and set "res" to the result.
duke@0 982 static void sort_scratch_list(ScratchBlock*& list) {
duke@0 983 ScratchBlock* sorted = NULL;
duke@0 984 ScratchBlock* unsorted = list;
duke@0 985 while (unsorted) {
duke@0 986 ScratchBlock *smallest = removeSmallestScratch(&unsorted);
duke@0 987 smallest->next = sorted;
duke@0 988 sorted = smallest;
duke@0 989 }
duke@0 990 list = sorted;
duke@0 991 }
duke@0 992
duke@0 993 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
duke@0 994 size_t max_alloc_words) {
duke@0 995 ScratchBlock* res = NULL;
duke@0 996 for (int i = 0; i < _n_gens; i++) {
duke@0 997 _gens[i]->contribute_scratch(res, requestor, max_alloc_words);
duke@0 998 }
duke@0 999 sort_scratch_list(res);
duke@0 1000 return res;
duke@0 1001 }
duke@0 1002
jmasa@263 1003 void GenCollectedHeap::release_scratch() {
jmasa@263 1004 for (int i = 0; i < _n_gens; i++) {
jmasa@263 1005 _gens[i]->reset_scratch();
jmasa@263 1006 }
jmasa@263 1007 }
jmasa@263 1008
duke@0 1009 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure {
duke@0 1010 void do_generation(Generation* gen) {
duke@0 1011 gen->prepare_for_verify();
duke@0 1012 }
duke@0 1013 };
duke@0 1014
duke@0 1015 void GenCollectedHeap::prepare_for_verify() {
duke@0 1016 ensure_parsability(false); // no need to retire TLABs
duke@0 1017 GenPrepareForVerifyClosure blk;
duke@0 1018 generation_iterate(&blk, false);
duke@0 1019 }
duke@0 1020
duke@0 1021
duke@0 1022 void GenCollectedHeap::generation_iterate(GenClosure* cl,
duke@0 1023 bool old_to_young) {
duke@0 1024 if (old_to_young) {
duke@0 1025 for (int i = _n_gens-1; i >= 0; i--) {
duke@0 1026 cl->do_generation(_gens[i]);
duke@0 1027 }
duke@0 1028 } else {
duke@0 1029 for (int i = 0; i < _n_gens; i++) {
duke@0 1030 cl->do_generation(_gens[i]);
duke@0 1031 }
duke@0 1032 }
duke@0 1033 }
duke@0 1034
duke@0 1035 void GenCollectedHeap::space_iterate(SpaceClosure* cl) {
duke@0 1036 for (int i = 0; i < _n_gens; i++) {
duke@0 1037 _gens[i]->space_iterate(cl, true);
duke@0 1038 }
duke@0 1039 }
duke@0 1040
duke@0 1041 bool GenCollectedHeap::is_maximal_no_gc() const {
coleenp@3602 1042 for (int i = 0; i < _n_gens; i++) {
duke@0 1043 if (!_gens[i]->is_maximal_no_gc()) {
duke@0 1044 return false;
duke@0 1045 }
duke@0 1046 }
duke@0 1047 return true;
duke@0 1048 }
duke@0 1049
duke@0 1050 void GenCollectedHeap::save_marks() {
duke@0 1051 for (int i = 0; i < _n_gens; i++) {
duke@0 1052 _gens[i]->save_marks();
duke@0 1053 }
duke@0 1054 }
duke@0 1055
duke@0 1056 GenCollectedHeap* GenCollectedHeap::heap() {
duke@0 1057 assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()");
duke@0 1058 assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap");
duke@0 1059 return _gch;
duke@0 1060 }
duke@0 1061
duke@0 1062
duke@0 1063 void GenCollectedHeap::prepare_for_compaction() {
brutisso@5081 1064 guarantee(_n_gens = 2, "Wrong number of generations");
brutisso@5081 1065 Generation* old_gen = _gens[1];
duke@0 1066 // Start by compacting into same gen.
brutisso@5081 1067 CompactPoint cp(old_gen, NULL, NULL);
brutisso@5081 1068 old_gen->prepare_for_compaction(&cp);
brutisso@5081 1069 Generation* young_gen = _gens[0];
brutisso@5081 1070 young_gen->prepare_for_compaction(&cp);
duke@0 1071 }
duke@0 1072
duke@0 1073 GCStats* GenCollectedHeap::gc_stats(int level) const {
duke@0 1074 return _gens[level]->gc_stats();
duke@0 1075 }
duke@0 1076
brutisso@3276 1077 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) {
duke@0 1078 for (int i = _n_gens-1; i >= 0; i--) {
duke@0 1079 Generation* g = _gens[i];
duke@0 1080 if (!silent) {
duke@0 1081 gclog_or_tty->print(g->name());
duke@0 1082 gclog_or_tty->print(" ");
duke@0 1083 }
brutisso@3276 1084 g->verify();
duke@0 1085 }
duke@0 1086 if (!silent) {
duke@0 1087 gclog_or_tty->print("remset ");
duke@0 1088 }
duke@0 1089 rem_set()->verify();
duke@0 1090 }
duke@0 1091
duke@0 1092 void GenCollectedHeap::print_on(outputStream* st) const {
duke@0 1093 for (int i = 0; i < _n_gens; i++) {
duke@0 1094 _gens[i]->print_on(st);
duke@0 1095 }
coleenp@3602 1096 MetaspaceAux::print_on(st);
duke@0 1097 }
duke@0 1098
duke@0 1099 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
duke@0 1100 if (workers() != NULL) {
duke@0 1101 workers()->threads_do(tc);
duke@0 1102 }
jprovino@4107 1103 #if INCLUDE_ALL_GCS
duke@0 1104 if (UseConcMarkSweepGC) {
duke@0 1105 ConcurrentMarkSweepThread::threads_do(tc);
duke@0 1106 }
jprovino@4107 1107 #endif // INCLUDE_ALL_GCS
duke@0 1108 }
duke@0 1109
duke@0 1110 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const {
jprovino@4107 1111 #if INCLUDE_ALL_GCS
duke@0 1112 if (UseParNewGC) {
duke@0 1113 workers()->print_worker_threads_on(st);
duke@0 1114 }
duke@0 1115 if (UseConcMarkSweepGC) {
duke@0 1116 ConcurrentMarkSweepThread::print_all_on(st);
duke@0 1117 }
jprovino@4107 1118 #endif // INCLUDE_ALL_GCS
duke@0 1119 }
duke@0 1120
stefank@4469 1121 void GenCollectedHeap::print_on_error(outputStream* st) const {
stefank@4469 1122 this->CollectedHeap::print_on_error(st);
stefank@4469 1123
stefank@4469 1124 #if INCLUDE_ALL_GCS
stefank@4469 1125 if (UseConcMarkSweepGC) {
stefank@4469 1126 st->cr();
stefank@4469 1127 CMSCollector::print_on_error(st);
stefank@4469 1128 }
stefank@4469 1129 #endif // INCLUDE_ALL_GCS
stefank@4469 1130 }
stefank@4469 1131
duke@0 1132 void GenCollectedHeap::print_tracing_info() const {
duke@0 1133 if (TraceGen0Time) {
duke@0 1134 get_gen(0)->print_summary_info();
duke@0 1135 }
duke@0 1136 if (TraceGen1Time) {
duke@0 1137 get_gen(1)->print_summary_info();
duke@0 1138 }
duke@0 1139 }
duke@0 1140
duke@0 1141 void GenCollectedHeap::print_heap_change(size_t prev_used) const {
duke@0 1142 if (PrintGCDetails && Verbose) {
duke@0 1143 gclog_or_tty->print(" " SIZE_FORMAT
duke@0 1144 "->" SIZE_FORMAT
duke@0 1145 "(" SIZE_FORMAT ")",
duke@0 1146 prev_used, used(), capacity());
duke@0 1147 } else {
duke@0 1148 gclog_or_tty->print(" " SIZE_FORMAT "K"
duke@0 1149 "->" SIZE_FORMAT "K"
duke@0 1150 "(" SIZE_FORMAT "K)",
duke@0 1151 prev_used / K, used() / K, capacity() / K);
duke@0 1152 }
duke@0 1153 }
duke@0 1154
duke@0 1155 class GenGCPrologueClosure: public GenCollectedHeap::GenClosure {
duke@0 1156 private:
duke@0 1157 bool _full;
duke@0 1158 public:
duke@0 1159 void do_generation(Generation* gen) {
duke@0 1160 gen->gc_prologue(_full);
duke@0 1161 }
duke@0 1162 GenGCPrologueClosure(bool full) : _full(full) {};
duke@0 1163 };
duke@0 1164
duke@0 1165 void GenCollectedHeap::gc_prologue(bool full) {
duke@0 1166 assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
duke@0 1167
duke@0 1168 always_do_update_barrier = false;
duke@0 1169 // Fill TLAB's and such
duke@0 1170 CollectedHeap::accumulate_statistics_all_tlabs();
duke@0 1171 ensure_parsability(true); // retire TLABs
duke@0 1172
duke@0 1173 // Walk generations
duke@0 1174 GenGCPrologueClosure blk(full);
duke@0 1175 generation_iterate(&blk, false); // not old-to-young.
duke@0 1176 };
duke@0 1177
duke@0 1178 class GenGCEpilogueClosure: public GenCollectedHeap::GenClosure {
duke@0 1179 private:
duke@0 1180 bool _full;
duke@0 1181 public:
duke@0 1182 void do_generation(Generation* gen) {
duke@0 1183 gen->gc_epilogue(_full);
duke@0 1184 }
duke@0 1185 GenGCEpilogueClosure(bool full) : _full(full) {};
duke@0 1186 };
duke@0 1187
duke@0 1188 void GenCollectedHeap::gc_epilogue(bool full) {
duke@0 1189 #ifdef COMPILER2
duke@0 1190 assert(DerivedPointerTable::is_empty(), "derived pointer present");
duke@0 1191 size_t actual_gap = pointer_delta((HeapWord*) (max_uintx-3), *(end_addr()));
duke@0 1192 guarantee(actual_gap > (size_t)FastAllocateSizeLimit, "inline allocation wraps");
duke@0 1193 #endif /* COMPILER2 */
duke@0 1194
duke@0 1195 resize_all_tlabs();
duke@0 1196
duke@0 1197 GenGCEpilogueClosure blk(full);
duke@0 1198 generation_iterate(&blk, false); // not old-to-young.
duke@0 1199
jcoomes@2561 1200 if (!CleanChunkPoolAsync) {
jcoomes@2561 1201 Chunk::clean_chunk_pool();
jcoomes@2561 1202 }
jcoomes@2561 1203
coleenp@3602 1204 MetaspaceCounters::update_performance_counters();
ehelin@5096 1205 CompressedClassSpaceCounters::update_performance_counters();
coleenp@3602 1206
duke@0 1207 always_do_update_barrier = UseConcMarkSweepGC;
duke@0 1208 };
duke@0 1209
jmasa@263 1210 #ifndef PRODUCT
jmasa@263 1211 class GenGCSaveTopsBeforeGCClosure: public GenCollectedHeap::GenClosure {
jmasa@263 1212 private:
jmasa@263 1213 public:
jmasa@263 1214 void do_generation(Generation* gen) {
jmasa@263 1215 gen->record_spaces_top();
jmasa@263 1216 }
jmasa@263 1217 };
jmasa@263 1218
jmasa@263 1219 void GenCollectedHeap::record_gen_tops_before_GC() {
jmasa@263 1220 if (ZapUnusedHeapArea) {
jmasa@263 1221 GenGCSaveTopsBeforeGCClosure blk;
jmasa@263 1222 generation_iterate(&blk, false); // not old-to-young.
jmasa@263 1223 }
jmasa@263 1224 }
jmasa@263 1225 #endif // not PRODUCT
jmasa@263 1226
duke@0 1227 class GenEnsureParsabilityClosure: public GenCollectedHeap::GenClosure {
duke@0 1228 public:
duke@0 1229 void do_generation(Generation* gen) {
duke@0 1230 gen->ensure_parsability();
duke@0 1231 }
duke@0 1232 };
duke@0 1233
duke@0 1234 void GenCollectedHeap::ensure_parsability(bool retire_tlabs) {
duke@0 1235 CollectedHeap::ensure_parsability(retire_tlabs);
duke@0 1236 GenEnsureParsabilityClosure ep_cl;
duke@0 1237 generation_iterate(&ep_cl, false);
duke@0 1238 }
duke@0 1239
brutisso@5081 1240 oop GenCollectedHeap::handle_failed_promotion(Generation* old_gen,
duke@0 1241 oop obj,
coleenp@113 1242 size_t obj_size) {
brutisso@5081 1243 guarantee(old_gen->level() == 1, "We only get here with an old generation");
duke@0 1244 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
duke@0 1245 HeapWord* result = NULL;
duke@0 1246
brutisso@5081 1247 result = old_gen->expand_and_allocate(obj_size, false);
duke@0 1248
duke@0 1249 if (result != NULL) {
duke@0 1250 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
duke@0 1251 }
duke@0 1252 return oop(result);
duke@0 1253 }
duke@0 1254
duke@0 1255 class GenTimeOfLastGCClosure: public GenCollectedHeap::GenClosure {
duke@0 1256 jlong _time; // in ms
duke@0 1257 jlong _now; // in ms
duke@0 1258
duke@0 1259 public:
duke@0 1260 GenTimeOfLastGCClosure(jlong now) : _time(now), _now(now) { }
duke@0 1261
duke@0 1262 jlong time() { return _time; }
duke@0 1263
duke@0 1264 void do_generation(Generation* gen) {
duke@0 1265 _time = MIN2(_time, gen->time_of_last_gc(_now));
duke@0 1266 }
duke@0 1267 };
duke@0 1268
duke@0 1269 jlong GenCollectedHeap::millis_since_last_gc() {
johnc@2904 1270 // We need a monotonically non-deccreasing time in ms but
johnc@2904 1271 // os::javaTimeMillis() does not guarantee monotonicity.
johnc@2904 1272 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
duke@0 1273 GenTimeOfLastGCClosure tolgc_cl(now);
duke@0 1274 // iterate over generations getting the oldest
duke@0 1275 // time that a generation was collected
duke@0 1276 generation_iterate(&tolgc_cl, false);
johnc@2904 1277
johnc@2904 1278 // javaTimeNanos() is guaranteed to be monotonically non-decreasing
johnc@2904 1279 // provided the underlying platform provides such a time source
johnc@2904 1280 // (and it is bug free). So we still have to guard against getting
johnc@2904 1281 // back a time later than 'now'.
duke@0 1282 jlong retVal = now - tolgc_cl.time();
duke@0 1283 if (retVal < 0) {
johnc@2904 1284 NOT_PRODUCT(warning("time warp: "INT64_FORMAT, retVal);)
duke@0 1285 return 0;
duke@0 1286 }
duke@0 1287 return retVal;
duke@0 1288 }