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