121 vm_shutdown_during_initialization( 122 "Could not reserve enough space for object heap"); 123 return JNI_ENOMEM; 124 } 125 126 _reserved = MemRegion((HeapWord*)heap_rs.base(), 127 (HeapWord*)(heap_rs.base() + heap_rs.size())); 128 129 // It is important to do this in a way such that concurrent readers can't 130 // temporarily think something is in the heap. (Seen this happen in asserts.) 131 _reserved.set_word_size(0); 132 _reserved.set_start((HeapWord*)heap_rs.base()); 133 size_t actual_heap_size = heap_rs.size(); 134 _reserved.set_end((HeapWord*)(heap_rs.base() + actual_heap_size)); 135 136 _rem_set = collector_policy()->create_rem_set(_reserved, n_covered_regions); 137 set_barrier_set(rem_set()->bs()); 138 139 _gch = this; 140 141 for (i = 0; i < _n_gens; i++) { 142 ReservedSpace this_rs = heap_rs.first_part(_gen_specs[i]->max_size(), false, false); 143 _gens[i] = _gen_specs[i]->init(this_rs, i, rem_set()); 144 heap_rs = heap_rs.last_part(_gen_specs[i]->max_size()); 145 } 146 clear_incremental_collection_failed(); 147 148 #if INCLUDE_ALL_GCS 149 // If we are running CMS, create the collector responsible 150 // for collecting the CMS generations. 151 if (collector_policy()->is_concurrent_mark_sweep_policy()) { 152 bool success = create_cms_collector(); 153 if (!success) return JNI_ENOMEM; 154 } 155 #endif // INCLUDE_ALL_GCS 156 157 return JNI_OK; 158 } 159 160 161 char* GenCollectedHeap::allocate(size_t alignment, 162 size_t* _total_reserved, 163 int* _n_covered_regions, 164 ReservedSpace* heap_rs){ 165 const char overflow_msg[] = "The size of the object heap + VM data exceeds " 166 "the maximum representable size"; 167 168 // Now figure out the total size. 169 size_t total_reserved = 0; 170 int n_covered_regions = 0; 171 const size_t pageSize = UseLargePages ? 172 os::large_page_size() : os::vm_page_size(); 173 174 assert(alignment % pageSize == 0, "Must be"); 175 176 for (int i = 0; i < _n_gens; i++) { 177 total_reserved += _gen_specs[i]->max_size(); 178 if (total_reserved < _gen_specs[i]->max_size()) { 179 vm_exit_during_initialization(overflow_msg); 180 } 181 n_covered_regions += _gen_specs[i]->n_covered_regions(); 182 } 183 assert(total_reserved % alignment == 0, 184 err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment=" 185 SIZE_FORMAT, total_reserved, alignment)); 186 187 // Needed until the cardtable is fixed to have the right number 188 // of covered regions. 189 n_covered_regions += 2; 190 191 *_total_reserved = total_reserved; 192 *_n_covered_regions = n_covered_regions; 193 194 *heap_rs = Universe::reserve_heap(total_reserved, alignment); 195 return heap_rs->base(); 196 } 197 198 199 void GenCollectedHeap::post_initialize() { 200 SharedHeap::post_initialize(); 201 GenCollectorPolicy *policy = (GenCollectorPolicy *)collector_policy(); 202 guarantee(policy->is_generation_policy(), "Illegal policy type"); 203 DefNewGeneration* def_new_gen = (DefNewGeneration*) get_gen(0); 204 assert(def_new_gen->kind() == Generation::DefNew || 205 def_new_gen->kind() == Generation::ParNew, 206 "Wrong generation kind"); 207 208 Generation* old_gen = get_gen(1); 209 assert(old_gen->kind() == Generation::ConcurrentMarkSweep || 210 old_gen->kind() == Generation::MarkSweepCompact, 211 "Wrong generation kind"); 212 213 policy->initialize_size_policy(def_new_gen->eden()->capacity(), 214 old_gen->capacity(), 215 def_new_gen->from()->capacity()); 216 policy->initialize_gc_policy_counters(); 217 } 218 219 void GenCollectedHeap::ref_processing_init() { 220 SharedHeap::ref_processing_init(); 221 for (int i = 0; i < _n_gens; i++) { 222 _gens[i]->ref_processor_init(); 223 } 224 } 225 226 size_t GenCollectedHeap::capacity() const { 227 size_t res = 0; 228 for (int i = 0; i < _n_gens; i++) { 229 res += _gens[i]->capacity(); 230 } 231 return res; 232 } 233 234 size_t GenCollectedHeap::used() const { 235 size_t res = 0; 236 for (int i = 0; i < _n_gens; i++) { 237 res += _gens[i]->used(); 238 } 239 return res; 240 } 241 242 // Save the "used_region" for generations level and lower. 243 void GenCollectedHeap::save_used_regions(int level) { 244 assert(level < _n_gens, "Illegal level parameter"); 245 for (int i = level; i >= 0; i--) { 246 _gens[i]->save_used_region(); 247 } 248 } 249 250 size_t GenCollectedHeap::max_capacity() const { 251 size_t res = 0; 252 for (int i = 0; i < _n_gens; i++) { 253 res += _gens[i]->max_capacity(); 254 } 255 return res; 256 } 257 258 // Update the _full_collections_completed counter 259 // at the end of a stop-world full GC. 260 unsigned int GenCollectedHeap::update_full_collections_completed() { 261 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag); 262 assert(_full_collections_completed <= _total_full_collections, 263 "Can't complete more collections than were started"); 264 _full_collections_completed = _total_full_collections; 265 ml.notify_all(); 266 return _full_collections_completed; 267 } 268 269 // Update the _full_collections_completed counter, as appropriate, 270 // at the end of a concurrent GC cycle. Note the conditional update 271 // below to allow this method to be called by a concurrent collector 272 // without synchronizing in any manner with the VM thread (which 273 // may already have initiated a STW full collection "concurrently"). 274 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) { 275 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag); 299 // higher than we are prepared to pay for such rudimentary debugging 300 // support. 301 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, 302 size_t size) { 303 if (CheckMemoryInitialization && ZapUnusedHeapArea) { 304 // We are asked to check a size in HeapWords, 305 // but the memory is mangled in juint words. 306 juint* start = (juint*) (addr + skip_header_HeapWords()); 307 juint* end = (juint*) (addr + size); 308 for (juint* slot = start; slot < end; slot += 1) { 309 assert(*slot == badHeapWordVal, 310 "Found non badHeapWordValue in pre-allocation check"); 311 } 312 } 313 } 314 #endif 315 316 HeapWord* GenCollectedHeap::attempt_allocation(size_t size, 317 bool is_tlab, 318 bool first_only) { 319 HeapWord* res; 320 for (int i = 0; i < _n_gens; i++) { 321 if (_gens[i]->should_allocate(size, is_tlab)) { 322 res = _gens[i]->allocate(size, is_tlab); 323 if (res != NULL) return res; 324 else if (first_only) break; 325 } 326 } 327 // Otherwise... 328 return NULL; 329 } 330 331 HeapWord* GenCollectedHeap::mem_allocate(size_t size, 332 bool* gc_overhead_limit_was_exceeded) { 333 return collector_policy()->mem_allocate_work(size, 334 false /* is_tlab */, 335 gc_overhead_limit_was_exceeded); 336 } 337 338 bool GenCollectedHeap::must_clear_all_soft_refs() { 339 return _gc_cause == GCCause::_last_ditch_collection; 340 } 341 342 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) { 343 return UseConcMarkSweepGC && 344 ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) || 345 (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent)); 346 } 347 348 void GenCollectedHeap::do_collection(bool full, 349 bool clear_all_soft_refs, 350 size_t size, 351 bool is_tlab, 352 int max_level) { 353 bool prepared_for_verification = false; 354 ResourceMark rm; 355 DEBUG_ONLY(Thread* my_thread = Thread::current();) 356 357 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint"); 358 assert(my_thread->is_VM_thread() || 359 my_thread->is_ConcurrentGC_thread(), 360 "incorrect thread type capability"); 361 assert(Heap_lock->is_locked(), 362 "the requesting thread should have the Heap_lock"); 363 guarantee(!is_gc_active(), "collection is not reentrant"); 364 assert(max_level < n_gens(), "sanity check"); 365 366 if (GC_locker::check_active_before_gc()) { 367 return; // GC is disabled (e.g. JNI GetXXXCritical operation) 368 } 369 370 const bool do_clear_all_soft_refs = clear_all_soft_refs || 371 collector_policy()->should_clear_all_soft_refs(); 372 373 ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy()); 374 375 const size_t metadata_prev_used = MetaspaceAux::used_bytes(); 376 377 print_heap_before_gc(); 378 379 { 380 FlagSetting fl(_is_gc_active, true); 381 382 bool complete = full && (max_level == (n_gens()-1)); 383 const char* gc_cause_prefix = complete ? "Full GC" : "GC"; 384 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps); 385 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty); 386 // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later 387 // so we can assume here that the next GC id is what we want. 388 GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL, GCId::peek()); 389 390 gc_prologue(complete); 391 increment_total_collections(complete); 392 393 size_t gch_prev_used = used(); 394 395 int starting_level = 0; 396 if (full) { 397 // Search for the oldest generation which will collect all younger 398 // generations, and start collection loop there. 399 for (int i = max_level; i >= 0; i--) { 400 if (_gens[i]->full_collects_younger_generations()) { 401 starting_level = i; 402 break; 403 } 404 } 405 } 406 407 bool must_restore_marks_for_biased_locking = false; 408 409 int max_level_collected = starting_level; 410 for (int i = starting_level; i <= max_level; i++) { 411 if (_gens[i]->should_collect(full, size, is_tlab)) { 412 if (i == n_gens() - 1) { // a major collection is to happen 413 if (!complete) { 414 // The full_collections increment was missed above. 415 increment_total_full_collections(); 416 } 417 pre_full_gc_dump(NULL); // do any pre full gc dumps 418 } 419 // Timer for individual generations. Last argument is false: no CR 420 // FIXME: We should try to start the timing earlier to cover more of the GC pause 421 // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later 422 // so we can assume here that the next GC id is what we want. 423 GCTraceTime t1(_gens[i]->short_name(), PrintGCDetails, false, NULL, GCId::peek()); 424 TraceCollectorStats tcs(_gens[i]->counters()); 425 TraceMemoryManagerStats tmms(_gens[i]->kind(),gc_cause()); 426 427 size_t prev_used = _gens[i]->used(); 428 _gens[i]->stat_record()->invocations++; 429 _gens[i]->stat_record()->accumulated_time.start(); 430 431 // Must be done anew before each collection because 432 // a previous collection will do mangling and will 433 // change top of some spaces. 434 record_gen_tops_before_GC(); 435 436 if (PrintGC && Verbose) { 437 gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT, 438 i, 439 _gens[i]->stat_record()->invocations, 440 size*HeapWordSize); 441 } 442 443 if (VerifyBeforeGC && i >= VerifyGCLevel && 444 total_collections() >= VerifyGCStartAt) { 445 HandleMark hm; // Discard invalid handles created during verification 446 if (!prepared_for_verification) { 447 prepare_for_verify(); 448 prepared_for_verification = true; 449 } 450 Universe::verify(" VerifyBeforeGC:"); 451 } 452 COMPILER2_PRESENT(DerivedPointerTable::clear()); 453 454 if (!must_restore_marks_for_biased_locking && 455 _gens[i]->performs_in_place_marking()) { 456 // We perform this mark word preservation work lazily 457 // because it's only at this point that we know whether we 458 // absolutely have to do it; we want to avoid doing it for 459 // scavenge-only collections where it's unnecessary 460 must_restore_marks_for_biased_locking = true; 461 BiasedLocking::preserve_marks(); 462 } 463 464 // Do collection work 465 { 466 // Note on ref discovery: For what appear to be historical reasons, 467 // GCH enables and disabled (by enqueing) refs discovery. 468 // In the future this should be moved into the generation's 469 // collect method so that ref discovery and enqueueing concerns 470 // are local to a generation. The collect method could return 471 // an appropriate indication in the case that notification on 472 // the ref lock was needed. This will make the treatment of 473 // weak refs more uniform (and indeed remove such concerns 474 // from GCH). XXX 475 476 HandleMark hm; // Discard invalid handles created during gc 477 save_marks(); // save marks for all gens 478 // We want to discover references, but not process them yet. 479 // This mode is disabled in process_discovered_references if the 480 // generation does some collection work, or in 481 // enqueue_discovered_references if the generation returns 482 // without doing any work. 483 ReferenceProcessor* rp = _gens[i]->ref_processor(); 484 // If the discovery of ("weak") refs in this generation is 485 // atomic wrt other collectors in this configuration, we 486 // are guaranteed to have empty discovered ref lists. 487 if (rp->discovery_is_atomic()) { 488 rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/); 489 rp->setup_policy(do_clear_all_soft_refs); 490 } else { 491 // collect() below will enable discovery as appropriate 492 } 493 _gens[i]->collect(full, do_clear_all_soft_refs, size, is_tlab); 494 if (!rp->enqueuing_is_done()) { 495 rp->enqueue_discovered_references(); 496 } else { 497 rp->set_enqueuing_is_done(false); 498 } 499 rp->verify_no_references_recorded(); 500 } 501 max_level_collected = i; 502 503 // Determine if allocation request was met. 504 if (size > 0) { 505 if (!is_tlab || _gens[i]->supports_tlab_allocation()) { 506 if (size*HeapWordSize <= _gens[i]->unsafe_max_alloc_nogc()) { 507 size = 0; 508 } 509 } 510 } 511 512 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 513 514 _gens[i]->stat_record()->accumulated_time.stop(); 515 516 update_gc_stats(i, full); 517 518 if (VerifyAfterGC && i >= VerifyGCLevel && 519 total_collections() >= VerifyGCStartAt) { 520 HandleMark hm; // Discard invalid handles created during verification 521 Universe::verify(" VerifyAfterGC:"); 522 } 523 524 if (PrintGCDetails) { 525 gclog_or_tty->print(":"); 526 _gens[i]->print_heap_change(prev_used); 527 } 528 } 529 } 530 531 // Update "complete" boolean wrt what actually transpired -- 532 // for instance, a promotion failure could have led to 533 // a whole heap collection. 534 complete = complete || (max_level_collected == n_gens() - 1); 535 536 if (complete) { // We did a "major" collection 537 // FIXME: See comment at pre_full_gc_dump call 538 post_full_gc_dump(NULL); // do any post full gc dumps 539 } 540 541 if (PrintGCDetails) { 542 print_heap_change(gch_prev_used); 543 544 // Print metaspace info for full GC with PrintGCDetails flag. 545 if (complete) { 546 MetaspaceAux::print_metaspace_change(metadata_prev_used); 547 } 548 } 549 550 for (int j = max_level_collected; j >= 0; j -= 1) { 551 // Adjust generation sizes. 552 _gens[j]->compute_new_size(); 553 } 554 555 if (complete) { 556 // Delete metaspaces for unloaded class loaders and clean up loader_data graph 557 ClassLoaderDataGraph::purge(); 558 MetaspaceAux::verify_metrics(); 559 // Resize the metaspace capacity after full collections 560 MetaspaceGC::compute_new_size(); 561 update_full_collections_completed(); 562 } 563 564 // Track memory usage and detect low memory after GC finishes 565 MemoryService::track_memory_usage(); 566 567 gc_epilogue(complete); 568 569 if (must_restore_marks_for_biased_locking) { 570 BiasedLocking::restore_marks(); 571 } 572 } 573 590 void GenCollectedHeap:: 591 gen_process_roots(int level, 592 bool younger_gens_as_roots, 593 bool activate_scope, 594 SharedHeap::ScanningOption so, 595 OopsInGenClosure* not_older_gens, 596 OopsInGenClosure* weak_roots, 597 OopsInGenClosure* older_gens, 598 CLDClosure* cld_closure, 599 CLDClosure* weak_cld_closure, 600 CodeBlobClosure* code_closure) { 601 602 // General roots. 603 SharedHeap::process_roots(activate_scope, so, 604 not_older_gens, weak_roots, 605 cld_closure, weak_cld_closure, 606 code_closure); 607 608 if (younger_gens_as_roots) { 609 if (!_gen_process_roots_tasks->is_task_claimed(GCH_PS_younger_gens)) { 610 for (int i = 0; i < level; i++) { 611 not_older_gens->set_generation(_gens[i]); 612 _gens[i]->oop_iterate(not_older_gens); 613 } 614 not_older_gens->reset_generation(); 615 } 616 } 617 // When collection is parallel, all threads get to cooperate to do 618 // older-gen scanning. 619 for (int i = level+1; i < _n_gens; i++) { 620 older_gens->set_generation(_gens[i]); 621 rem_set()->younger_refs_iterate(_gens[i], older_gens); 622 older_gens->reset_generation(); 623 } 624 625 _gen_process_roots_tasks->all_tasks_completed(); 626 } 627 628 void GenCollectedHeap:: 629 gen_process_roots(int level, 630 bool younger_gens_as_roots, 631 bool activate_scope, 632 SharedHeap::ScanningOption so, 633 bool only_strong_roots, 634 OopsInGenClosure* not_older_gens, 635 OopsInGenClosure* older_gens, 636 CLDClosure* cld_closure) { 637 638 const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots; 639 640 bool is_moving_collection = false; 641 if (level == 0 || is_adjust_phase) { 642 // young collections are always moving 643 is_moving_collection = true; 644 } 645 646 MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection); 647 CodeBlobClosure* code_closure = &mark_code_closure; 648 649 gen_process_roots(level, 650 younger_gens_as_roots, 651 activate_scope, so, 652 not_older_gens, only_strong_roots ? NULL : not_older_gens, 653 older_gens, 654 cld_closure, only_strong_roots ? NULL : cld_closure, 655 code_closure); 656 657 } 658 659 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) { 660 SharedHeap::process_weak_roots(root_closure); 661 // "Local" "weak" refs 662 for (int i = 0; i < _n_gens; i++) { 663 _gens[i]->ref_processor()->weak_oops_do(root_closure); 664 } 665 } 666 667 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \ 668 void GenCollectedHeap:: \ 669 oop_since_save_marks_iterate(int level, \ 670 OopClosureType* cur, \ 671 OopClosureType* older) { \ 672 _gens[level]->oop_since_save_marks_iterate##nv_suffix(cur); \ 673 for (int i = level+1; i < n_gens(); i++) { \ 674 _gens[i]->oop_since_save_marks_iterate##nv_suffix(older); \ 675 } \ 676 } 677 678 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN) 679 680 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN 681 682 bool GenCollectedHeap::no_allocs_since_save_marks(int level) { 683 for (int i = level; i < _n_gens; i++) { 684 if (!_gens[i]->no_allocs_since_save_marks()) return false; 685 } 686 return true; 687 } 688 689 bool GenCollectedHeap::supports_inline_contig_alloc() const { 690 return _gens[0]->supports_inline_contig_alloc(); 691 } 692 693 HeapWord** GenCollectedHeap::top_addr() const { 694 return _gens[0]->top_addr(); 695 } 696 697 HeapWord** GenCollectedHeap::end_addr() const { 698 return _gens[0]->end_addr(); 699 } 700 701 // public collection interfaces 702 703 void GenCollectedHeap::collect(GCCause::Cause cause) { 704 if (should_do_concurrent_full_gc(cause)) { 705 #if INCLUDE_ALL_GCS 706 // mostly concurrent full collection 707 collect_mostly_concurrent(cause); 708 #else // INCLUDE_ALL_GCS 709 ShouldNotReachHere(); 710 #endif // INCLUDE_ALL_GCS 711 } else { 712 #ifdef ASSERT 713 if (cause == GCCause::_scavenge_alot) { 714 // minor collection only 715 collect(cause, 0); 716 } else { 717 // Stop-the-world full collection 718 collect(cause, n_gens() - 1); 738 } 739 740 // this is the private collection interface 741 // The Heap_lock is expected to be held on entry. 742 743 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) { 744 // Read the GC count while holding the Heap_lock 745 unsigned int gc_count_before = total_collections(); 746 unsigned int full_gc_count_before = total_full_collections(); 747 { 748 MutexUnlocker mu(Heap_lock); // give up heap lock, execute gets it back 749 VM_GenCollectFull op(gc_count_before, full_gc_count_before, 750 cause, max_level); 751 VMThread::execute(&op); 752 } 753 } 754 755 #if INCLUDE_ALL_GCS 756 bool GenCollectedHeap::create_cms_collector() { 757 758 assert(_gens[1]->kind() == Generation::ConcurrentMarkSweep, 759 "Unexpected generation kinds"); 760 // Skip two header words in the block content verification 761 NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();) 762 CMSCollector* collector = new CMSCollector( 763 (ConcurrentMarkSweepGeneration*)_gens[1], 764 _rem_set->as_CardTableRS(), 765 (ConcurrentMarkSweepPolicy*) collector_policy()); 766 767 if (collector == NULL || !collector->completed_initialization()) { 768 if (collector) { 769 delete collector; // Be nice in embedded situation 770 } 771 vm_shutdown_during_initialization("Could not create CMS collector"); 772 return false; 773 } 774 return true; // success 775 } 776 777 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) { 778 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock"); 779 780 MutexLocker ml(Heap_lock); 781 // Read the GC counts while holding the Heap_lock 782 unsigned int full_gc_count_before = total_full_collections(); 783 unsigned int gc_count_before = total_collections(); 810 local_max_level /* max_level */); 811 // Hack XXX FIX ME !!! 812 // A scavenge may not have been attempted, or may have 813 // been attempted and failed, because the old gen was too full 814 if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker && 815 incremental_collection_will_fail(false /* don't consult_young */)) { 816 if (PrintGCDetails) { 817 gclog_or_tty->print_cr("GC locker: Trying a full collection " 818 "because scavenge failed"); 819 } 820 // This time allow the old gen to be collected as well 821 do_collection(true /* full */, 822 clear_all_soft_refs /* clear_all_soft_refs */, 823 0 /* size */, 824 false /* is_tlab */, 825 n_gens() - 1 /* max_level */); 826 } 827 } 828 829 bool GenCollectedHeap::is_in_young(oop p) { 830 bool result = ((HeapWord*)p) < _gens[_n_gens - 1]->reserved().start(); 831 assert(result == _gens[0]->is_in_reserved(p), 832 err_msg("incorrect test - result=%d, p=" INTPTR_FORMAT, result, p2i((void*)p))); 833 return result; 834 } 835 836 // Returns "TRUE" iff "p" points into the committed areas of the heap. 837 bool GenCollectedHeap::is_in(const void* p) const { 838 #ifndef ASSERT 839 guarantee(VerifyBeforeGC || 840 VerifyDuringGC || 841 VerifyBeforeExit || 842 VerifyDuringStartup || 843 PrintAssembly || 844 tty->count() != 0 || // already printing 845 VerifyAfterGC || 846 VMError::fatal_error_in_progress(), "too expensive"); 847 848 #endif 849 // This might be sped up with a cache of the last generation that 850 // answered yes. 851 for (int i = 0; i < _n_gens; i++) { 852 if (_gens[i]->is_in(p)) return true; 853 } 854 // Otherwise... 855 return false; 856 } 857 858 #ifdef ASSERT 859 // Don't implement this by using is_in_young(). This method is used 860 // in some cases to check that is_in_young() is correct. 861 bool GenCollectedHeap::is_in_partial_collection(const void* p) { 862 assert(is_in_reserved(p) || p == NULL, 863 "Does not work if address is non-null and outside of the heap"); 864 return p < _gens[_n_gens - 2]->reserved().end() && p != NULL; 865 } 866 #endif 867 868 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) { 869 for (int i = 0; i < _n_gens; i++) { 870 _gens[i]->oop_iterate(cl); 871 } 872 } 873 874 void GenCollectedHeap::object_iterate(ObjectClosure* cl) { 875 for (int i = 0; i < _n_gens; i++) { 876 _gens[i]->object_iterate(cl); 877 } 878 } 879 880 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) { 881 for (int i = 0; i < _n_gens; i++) { 882 _gens[i]->safe_object_iterate(cl); 883 } 884 } 885 886 Space* GenCollectedHeap::space_containing(const void* addr) const { 887 for (int i = 0; i < _n_gens; i++) { 888 Space* res = _gens[i]->space_containing(addr); 889 if (res != NULL) return res; 890 } 891 // Otherwise... 892 assert(false, "Could not find containing space"); 893 return NULL; 894 } 895 896 897 HeapWord* GenCollectedHeap::block_start(const void* addr) const { 898 assert(is_in_reserved(addr), "block_start of address outside of heap"); 899 for (int i = 0; i < _n_gens; i++) { 900 if (_gens[i]->is_in_reserved(addr)) { 901 assert(_gens[i]->is_in(addr), 902 "addr should be in allocated part of generation"); 903 return _gens[i]->block_start(addr); 904 } 905 } 906 assert(false, "Some generation should contain the address"); 907 return NULL; 908 } 909 910 size_t GenCollectedHeap::block_size(const HeapWord* addr) const { 911 assert(is_in_reserved(addr), "block_size of address outside of heap"); 912 for (int i = 0; i < _n_gens; i++) { 913 if (_gens[i]->is_in_reserved(addr)) { 914 assert(_gens[i]->is_in(addr), 915 "addr should be in allocated part of generation"); 916 return _gens[i]->block_size(addr); 917 } 918 } 919 assert(false, "Some generation should contain the address"); 920 return 0; 921 } 922 923 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const { 924 assert(is_in_reserved(addr), "block_is_obj of address outside of heap"); 925 assert(block_start(addr) == addr, "addr must be a block start"); 926 for (int i = 0; i < _n_gens; i++) { 927 if (_gens[i]->is_in_reserved(addr)) { 928 return _gens[i]->block_is_obj(addr); 929 } 930 } 931 assert(false, "Some generation should contain the address"); 932 return false; 933 } 934 935 bool GenCollectedHeap::supports_tlab_allocation() const { 936 for (int i = 0; i < _n_gens; i += 1) { 937 if (_gens[i]->supports_tlab_allocation()) { 938 return true; 939 } 940 } 941 return false; 942 } 943 944 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const { 945 size_t result = 0; 946 for (int i = 0; i < _n_gens; i += 1) { 947 if (_gens[i]->supports_tlab_allocation()) { 948 result += _gens[i]->tlab_capacity(); 949 } 950 } 951 return result; 952 } 953 954 size_t GenCollectedHeap::tlab_used(Thread* thr) const { 955 size_t result = 0; 956 for (int i = 0; i < _n_gens; i += 1) { 957 if (_gens[i]->supports_tlab_allocation()) { 958 result += _gens[i]->tlab_used(); 959 } 960 } 961 return result; 962 } 963 964 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const { 965 size_t result = 0; 966 for (int i = 0; i < _n_gens; i += 1) { 967 if (_gens[i]->supports_tlab_allocation()) { 968 result += _gens[i]->unsafe_max_tlab_alloc(); 969 } 970 } 971 return result; 972 } 973 974 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) { 975 bool gc_overhead_limit_was_exceeded; 976 return collector_policy()->mem_allocate_work(size /* size */, 977 true /* is_tlab */, 978 &gc_overhead_limit_was_exceeded); 979 } 980 981 // Requires "*prev_ptr" to be non-NULL. Deletes and a block of minimal size 982 // from the list headed by "*prev_ptr". 983 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) { 984 bool first = true; 985 size_t min_size = 0; // "first" makes this conceptually infinite. 986 ScratchBlock **smallest_ptr, *smallest; 987 ScratchBlock *cur = *prev_ptr; 988 while (cur) { 989 assert(*prev_ptr == cur, "just checking"); 990 if (first || cur->num_words < min_size) { 991 smallest_ptr = prev_ptr; 1000 *smallest_ptr = smallest->next; 1001 return smallest; 1002 } 1003 1004 // Sort the scratch block list headed by res into decreasing size order, 1005 // and set "res" to the result. 1006 static void sort_scratch_list(ScratchBlock*& list) { 1007 ScratchBlock* sorted = NULL; 1008 ScratchBlock* unsorted = list; 1009 while (unsorted) { 1010 ScratchBlock *smallest = removeSmallestScratch(&unsorted); 1011 smallest->next = sorted; 1012 sorted = smallest; 1013 } 1014 list = sorted; 1015 } 1016 1017 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor, 1018 size_t max_alloc_words) { 1019 ScratchBlock* res = NULL; 1020 for (int i = 0; i < _n_gens; i++) { 1021 _gens[i]->contribute_scratch(res, requestor, max_alloc_words); 1022 } 1023 sort_scratch_list(res); 1024 return res; 1025 } 1026 1027 void GenCollectedHeap::release_scratch() { 1028 for (int i = 0; i < _n_gens; i++) { 1029 _gens[i]->reset_scratch(); 1030 } 1031 } 1032 1033 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure { 1034 void do_generation(Generation* gen) { 1035 gen->prepare_for_verify(); 1036 } 1037 }; 1038 1039 void GenCollectedHeap::prepare_for_verify() { 1040 ensure_parsability(false); // no need to retire TLABs 1041 GenPrepareForVerifyClosure blk; 1042 generation_iterate(&blk, false); 1043 } 1044 1045 1046 void GenCollectedHeap::generation_iterate(GenClosure* cl, 1047 bool old_to_young) { 1048 if (old_to_young) { 1049 for (int i = _n_gens-1; i >= 0; i--) { 1050 cl->do_generation(_gens[i]); 1051 } 1052 } else { 1053 for (int i = 0; i < _n_gens; i++) { 1054 cl->do_generation(_gens[i]); 1055 } 1056 } 1057 } 1058 1059 void GenCollectedHeap::space_iterate(SpaceClosure* cl) { 1060 for (int i = 0; i < _n_gens; i++) { 1061 _gens[i]->space_iterate(cl, true); 1062 } 1063 } 1064 1065 bool GenCollectedHeap::is_maximal_no_gc() const { 1066 for (int i = 0; i < _n_gens; i++) { 1067 if (!_gens[i]->is_maximal_no_gc()) { 1068 return false; 1069 } 1070 } 1071 return true; 1072 } 1073 1074 void GenCollectedHeap::save_marks() { 1075 for (int i = 0; i < _n_gens; i++) { 1076 _gens[i]->save_marks(); 1077 } 1078 } 1079 1080 GenCollectedHeap* GenCollectedHeap::heap() { 1081 assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()"); 1082 assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap"); 1083 return _gch; 1084 } 1085 1086 1087 void GenCollectedHeap::prepare_for_compaction() { 1088 guarantee(_n_gens = 2, "Wrong number of generations"); 1089 Generation* old_gen = _gens[1]; 1090 // Start by compacting into same gen. 1091 CompactPoint cp(old_gen); 1092 old_gen->prepare_for_compaction(&cp); 1093 Generation* young_gen = _gens[0]; 1094 young_gen->prepare_for_compaction(&cp); 1095 } 1096 1097 GCStats* GenCollectedHeap::gc_stats(int level) const { 1098 return _gens[level]->gc_stats(); 1099 } 1100 1101 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) { 1102 for (int i = _n_gens-1; i >= 0; i--) { 1103 Generation* g = _gens[i]; 1104 if (!silent) { 1105 gclog_or_tty->print("%s", g->name()); 1106 gclog_or_tty->print(" "); 1107 } 1108 g->verify(); 1109 } 1110 if (!silent) { 1111 gclog_or_tty->print("remset "); 1112 } 1113 rem_set()->verify(); 1114 } 1115 1116 void GenCollectedHeap::print_on(outputStream* st) const { 1117 for (int i = 0; i < _n_gens; i++) { 1118 _gens[i]->print_on(st); 1119 } 1120 MetaspaceAux::print_on(st); 1121 } 1122 1123 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const { 1124 if (workers() != NULL) { 1125 workers()->threads_do(tc); 1126 } 1127 #if INCLUDE_ALL_GCS 1128 if (UseConcMarkSweepGC) { 1129 ConcurrentMarkSweepThread::threads_do(tc); 1130 } 1131 #endif // INCLUDE_ALL_GCS 1132 } 1133 1134 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const { 1135 #if INCLUDE_ALL_GCS 1136 if (UseParNewGC) { 1137 workers()->print_worker_threads_on(st); 1138 } 1139 if (UseConcMarkSweepGC) { | 121 vm_shutdown_during_initialization( 122 "Could not reserve enough space for object heap"); 123 return JNI_ENOMEM; 124 } 125 126 _reserved = MemRegion((HeapWord*)heap_rs.base(), 127 (HeapWord*)(heap_rs.base() + heap_rs.size())); 128 129 // It is important to do this in a way such that concurrent readers can't 130 // temporarily think something is in the heap. (Seen this happen in asserts.) 131 _reserved.set_word_size(0); 132 _reserved.set_start((HeapWord*)heap_rs.base()); 133 size_t actual_heap_size = heap_rs.size(); 134 _reserved.set_end((HeapWord*)(heap_rs.base() + actual_heap_size)); 135 136 _rem_set = collector_policy()->create_rem_set(_reserved, n_covered_regions); 137 set_barrier_set(rem_set()->bs()); 138 139 _gch = this; 140 141 ReservedSpace young_rs = heap_rs.first_part(_gen_specs[0]->max_size(), false, false); 142 _young_gen = _gen_specs[0]->init(young_rs, 0, rem_set()); 143 heap_rs = heap_rs.last_part(_gen_specs[0]->max_size()); 144 145 ReservedSpace old_rs = heap_rs.first_part(_gen_specs[1]->max_size(), false, false); 146 _old_gen = _gen_specs[1]->init(old_rs, 1, rem_set()); 147 heap_rs = heap_rs.last_part(_gen_specs[1]->max_size()); 148 clear_incremental_collection_failed(); 149 150 #if INCLUDE_ALL_GCS 151 // If we are running CMS, create the collector responsible 152 // for collecting the CMS generations. 153 if (collector_policy()->is_concurrent_mark_sweep_policy()) { 154 bool success = create_cms_collector(); 155 if (!success) return JNI_ENOMEM; 156 } 157 #endif // INCLUDE_ALL_GCS 158 159 return JNI_OK; 160 } 161 162 char* GenCollectedHeap::allocate(size_t alignment, 163 size_t* _total_reserved, 164 int* _n_covered_regions, 165 ReservedSpace* heap_rs){ 166 const char overflow_msg[] = "The size of the object heap + VM data exceeds " 167 "the maximum representable size"; 168 169 // Now figure out the total size. 170 size_t total_reserved = 0; 171 int n_covered_regions = 0; 172 const size_t pageSize = UseLargePages ? 173 os::large_page_size() : os::vm_page_size(); 174 175 assert(alignment % pageSize == 0, "Must be"); 176 177 for (int i = 0; i < _n_gens; i++) { 178 total_reserved += _gen_specs[i]->max_size(); 179 if (total_reserved < _gen_specs[i]->max_size()) { 180 vm_exit_during_initialization(overflow_msg); 181 } 182 n_covered_regions += _gen_specs[i]->n_covered_regions(); 183 } 184 assert(total_reserved % alignment == 0, 185 err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment=" 186 SIZE_FORMAT, total_reserved, alignment)); 187 188 // Needed until the cardtable is fixed to have the right number 189 // of covered regions. 190 n_covered_regions += 2; 191 192 *_total_reserved = total_reserved; 193 *_n_covered_regions = n_covered_regions; 194 195 *heap_rs = Universe::reserve_heap(total_reserved, alignment); 196 return heap_rs->base(); 197 } 198 199 void GenCollectedHeap::post_initialize() { 200 SharedHeap::post_initialize(); 201 GenCollectorPolicy *policy = (GenCollectorPolicy *)collector_policy(); 202 guarantee(policy->is_generation_policy(), "Illegal policy type"); 203 DefNewGeneration* def_new_gen = (DefNewGeneration*) get_gen(0); 204 assert(def_new_gen->kind() == Generation::DefNew || 205 def_new_gen->kind() == Generation::ParNew, 206 "Wrong generation kind"); 207 208 Generation* old_gen = get_gen(1); 209 assert(old_gen->kind() == Generation::ConcurrentMarkSweep || 210 old_gen->kind() == Generation::MarkSweepCompact, 211 "Wrong generation kind"); 212 213 policy->initialize_size_policy(def_new_gen->eden()->capacity(), 214 old_gen->capacity(), 215 def_new_gen->from()->capacity()); 216 policy->initialize_gc_policy_counters(); 217 } 218 219 void GenCollectedHeap::ref_processing_init() { 220 SharedHeap::ref_processing_init(); 221 _young_gen->ref_processor_init(); 222 _old_gen->ref_processor_init(); 223 } 224 225 size_t GenCollectedHeap::capacity() const { 226 return _young_gen->capacity() + _old_gen->capacity(); 227 } 228 229 size_t GenCollectedHeap::used() const { 230 return _young_gen->used() + _old_gen->used(); 231 } 232 233 // Save the "used_region" for generations level and lower. 234 void GenCollectedHeap::save_used_regions(int level) { 235 assert(level < _n_gens, "Illegal level parameter"); 236 if (level == 1) { 237 _old_gen->save_used_region(); 238 } 239 _young_gen->save_used_region(); 240 } 241 242 size_t GenCollectedHeap::max_capacity() const { 243 return _young_gen->max_capacity() + _old_gen->max_capacity(); 244 } 245 246 // Update the _full_collections_completed counter 247 // at the end of a stop-world full GC. 248 unsigned int GenCollectedHeap::update_full_collections_completed() { 249 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag); 250 assert(_full_collections_completed <= _total_full_collections, 251 "Can't complete more collections than were started"); 252 _full_collections_completed = _total_full_collections; 253 ml.notify_all(); 254 return _full_collections_completed; 255 } 256 257 // Update the _full_collections_completed counter, as appropriate, 258 // at the end of a concurrent GC cycle. Note the conditional update 259 // below to allow this method to be called by a concurrent collector 260 // without synchronizing in any manner with the VM thread (which 261 // may already have initiated a STW full collection "concurrently"). 262 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) { 263 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag); 287 // higher than we are prepared to pay for such rudimentary debugging 288 // support. 289 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, 290 size_t size) { 291 if (CheckMemoryInitialization && ZapUnusedHeapArea) { 292 // We are asked to check a size in HeapWords, 293 // but the memory is mangled in juint words. 294 juint* start = (juint*) (addr + skip_header_HeapWords()); 295 juint* end = (juint*) (addr + size); 296 for (juint* slot = start; slot < end; slot += 1) { 297 assert(*slot == badHeapWordVal, 298 "Found non badHeapWordValue in pre-allocation check"); 299 } 300 } 301 } 302 #endif 303 304 HeapWord* GenCollectedHeap::attempt_allocation(size_t size, 305 bool is_tlab, 306 bool first_only) { 307 HeapWord* res = NULL; 308 309 if (_young_gen->should_allocate(size, is_tlab)) { 310 res = _young_gen->allocate(size, is_tlab); 311 if (res != NULL || first_only) { 312 return res; 313 } 314 } 315 316 if (_old_gen->should_allocate(size, is_tlab)) { 317 res = _old_gen->allocate(size, is_tlab); 318 } 319 320 return res; 321 } 322 323 HeapWord* GenCollectedHeap::mem_allocate(size_t size, 324 bool* gc_overhead_limit_was_exceeded) { 325 return collector_policy()->mem_allocate_work(size, 326 false /* is_tlab */, 327 gc_overhead_limit_was_exceeded); 328 } 329 330 bool GenCollectedHeap::must_clear_all_soft_refs() { 331 return _gc_cause == GCCause::_last_ditch_collection; 332 } 333 334 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) { 335 return UseConcMarkSweepGC && 336 ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) || 337 (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent)); 338 } 339 340 void GenCollectedHeap::collect_generation(Generation* gen, bool full, size_t size, 341 bool is_tlab, bool run_verification, bool clear_soft_refs) { 342 // Timer for individual generations. Last argument is false: no CR 343 // FIXME: We should try to start the timing earlier to cover more of the GC pause 344 // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later 345 // so we can assume here that the next GC id is what we want. 346 GCTraceTime t1(gen->short_name(), PrintGCDetails, false, NULL, GCId::peek()); 347 TraceCollectorStats tcs(gen->counters()); 348 TraceMemoryManagerStats tmms(gen->kind(),gc_cause()); 349 350 size_t prev_used = gen->used(); 351 gen->stat_record()->invocations++; 352 gen->stat_record()->accumulated_time.start(); 353 354 // Must be done anew before each collection because 355 // a previous collection will do mangling and will 356 // change top of some spaces. 357 record_gen_tops_before_GC(); 358 359 if (PrintGC && Verbose) { 360 gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT, 361 gen->level(), 362 gen->stat_record()->invocations, 363 size * HeapWordSize); 364 } 365 366 if (run_verification && VerifyBeforeGC) { 367 HandleMark hm; // Discard invalid handles created during verification 368 Universe::verify(" VerifyBeforeGC:"); 369 } 370 COMPILER2_PRESENT(DerivedPointerTable::clear()); 371 372 // Do collection work 373 { 374 // Note on ref discovery: For what appear to be historical reasons, 375 // GCH enables and disabled (by enqueing) refs discovery. 376 // In the future this should be moved into the generation's 377 // collect method so that ref discovery and enqueueing concerns 378 // are local to a generation. The collect method could return 379 // an appropriate indication in the case that notification on 380 // the ref lock was needed. This will make the treatment of 381 // weak refs more uniform (and indeed remove such concerns 382 // from GCH). XXX 383 384 HandleMark hm; // Discard invalid handles created during gc 385 save_marks(); // save marks for all gens 386 // We want to discover references, but not process them yet. 387 // This mode is disabled in process_discovered_references if the 388 // generation does some collection work, or in 389 // enqueue_discovered_references if the generation returns 390 // without doing any work. 391 ReferenceProcessor* rp = gen->ref_processor(); 392 // If the discovery of ("weak") refs in this generation is 393 // atomic wrt other collectors in this configuration, we 394 // are guaranteed to have empty discovered ref lists. 395 if (rp->discovery_is_atomic()) { 396 rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/); 397 rp->setup_policy(clear_soft_refs); 398 } else { 399 // collect() below will enable discovery as appropriate 400 } 401 gen->collect(full, clear_soft_refs, size, is_tlab); 402 if (!rp->enqueuing_is_done()) { 403 rp->enqueue_discovered_references(); 404 } else { 405 rp->set_enqueuing_is_done(false); 406 } 407 rp->verify_no_references_recorded(); 408 } 409 410 // Determine if allocation request was met. 411 if (size > 0) { 412 if (!is_tlab || gen->supports_tlab_allocation()) { 413 if (size * HeapWordSize <= gen->unsafe_max_alloc_nogc()) { 414 size = 0; 415 } 416 } 417 } 418 419 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 420 421 gen->stat_record()->accumulated_time.stop(); 422 423 update_gc_stats(gen->level(), full); 424 425 if (run_verification && VerifyAfterGC) { 426 HandleMark hm; // Discard invalid handles created during verification 427 Universe::verify(" VerifyAfterGC:"); 428 } 429 430 if (PrintGCDetails) { 431 gclog_or_tty->print(":"); 432 gen->print_heap_change(prev_used); 433 } 434 } 435 436 void GenCollectedHeap::do_collection(bool full, 437 bool clear_all_soft_refs, 438 size_t size, 439 bool is_tlab, 440 int max_level) { 441 ResourceMark rm; 442 DEBUG_ONLY(Thread* my_thread = Thread::current();) 443 444 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint"); 445 assert(my_thread->is_VM_thread() || 446 my_thread->is_ConcurrentGC_thread(), 447 "incorrect thread type capability"); 448 assert(Heap_lock->is_locked(), 449 "the requesting thread should have the Heap_lock"); 450 guarantee(!is_gc_active(), "collection is not reentrant"); 451 assert(max_level < n_gens(), "sanity check"); 452 453 if (GC_locker::check_active_before_gc()) { 454 return; // GC is disabled (e.g. JNI GetXXXCritical operation) 455 } 456 457 const bool do_clear_all_soft_refs = clear_all_soft_refs || 458 collector_policy()->should_clear_all_soft_refs(); 459 460 ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy()); 461 462 const size_t metadata_prev_used = MetaspaceAux::used_bytes(); 463 464 print_heap_before_gc(); 465 466 { 467 FlagSetting fl(_is_gc_active, true); 468 469 bool complete = full && (max_level == (n_gens()-1)); 470 const char* gc_cause_prefix = complete ? "Full GC" : "GC"; 471 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps); 472 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty); 473 // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later 474 // so we can assume here that the next GC id is what we want. 475 GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL, GCId::peek()); 476 477 gc_prologue(complete); 478 increment_total_collections(complete); 479 480 size_t gch_prev_used = used(); 481 bool must_restore_marks_for_biased_locking = false; 482 bool run_verification = total_collections() >= VerifyGCStartAt; 483 484 if (_young_gen->performs_in_place_marking() || 485 _old_gen->performs_in_place_marking()) { 486 // We want to avoid doing this for 487 // scavenge-only collections where it's unnecessary. 488 must_restore_marks_for_biased_locking = true; 489 BiasedLocking::preserve_marks(); 490 } 491 492 bool prepared_for_verification = false; 493 int max_level_collected = 0; 494 if (!(full && _old_gen->full_collects_younger_generations()) && 495 _young_gen->should_collect(full, size, is_tlab)) { 496 if (run_verification && VerifyGCLevel <= 0 && VerifyBeforeGC) { 497 prepare_for_verify(); 498 prepared_for_verification = true; 499 } 500 collect_generation(_young_gen, full, size, is_tlab, run_verification && VerifyGCLevel <= 0, do_clear_all_soft_refs); 501 } 502 if (max_level == 1 && _old_gen->should_collect(full, size, is_tlab)) { 503 if (!complete) { 504 // The full_collections increment was missed above. 505 increment_total_full_collections(); 506 } 507 pre_full_gc_dump(NULL); // do any pre full gc dumps 508 if (run_verification && VerifyGCLevel <= 1 && VerifyBeforeGC) { 509 if (!prepared_for_verification) { 510 prepare_for_verify(); 511 } 512 } 513 collect_generation(_old_gen, full, size, is_tlab, run_verification && VerifyGCLevel <= 1, do_clear_all_soft_refs); 514 max_level_collected = 1; 515 } 516 517 // Update "complete" boolean wrt what actually transpired -- 518 // for instance, a promotion failure could have led to 519 // a whole heap collection. 520 complete = complete || (max_level_collected == n_gens() - 1); 521 522 if (complete) { // We did a "major" collection 523 // FIXME: See comment at pre_full_gc_dump call 524 post_full_gc_dump(NULL); // do any post full gc dumps 525 } 526 527 if (PrintGCDetails) { 528 print_heap_change(gch_prev_used); 529 530 // Print metaspace info for full GC with PrintGCDetails flag. 531 if (complete) { 532 MetaspaceAux::print_metaspace_change(metadata_prev_used); 533 } 534 } 535 536 // Adjust generation sizes. 537 if (max_level_collected == 1) { 538 _old_gen->compute_new_size(); 539 } 540 _young_gen->compute_new_size(); 541 542 if (complete) { 543 // Delete metaspaces for unloaded class loaders and clean up loader_data graph 544 ClassLoaderDataGraph::purge(); 545 MetaspaceAux::verify_metrics(); 546 // Resize the metaspace capacity after full collections 547 MetaspaceGC::compute_new_size(); 548 update_full_collections_completed(); 549 } 550 551 // Track memory usage and detect low memory after GC finishes 552 MemoryService::track_memory_usage(); 553 554 gc_epilogue(complete); 555 556 if (must_restore_marks_for_biased_locking) { 557 BiasedLocking::restore_marks(); 558 } 559 } 560 577 void GenCollectedHeap:: 578 gen_process_roots(int level, 579 bool younger_gens_as_roots, 580 bool activate_scope, 581 SharedHeap::ScanningOption so, 582 OopsInGenClosure* not_older_gens, 583 OopsInGenClosure* weak_roots, 584 OopsInGenClosure* older_gens, 585 CLDClosure* cld_closure, 586 CLDClosure* weak_cld_closure, 587 CodeBlobClosure* code_closure) { 588 589 // General roots. 590 SharedHeap::process_roots(activate_scope, so, 591 not_older_gens, weak_roots, 592 cld_closure, weak_cld_closure, 593 code_closure); 594 595 if (younger_gens_as_roots) { 596 if (!_gen_process_roots_tasks->is_task_claimed(GCH_PS_younger_gens)) { 597 if (level == 1) { 598 not_older_gens->set_generation(_young_gen); 599 _young_gen->oop_iterate(not_older_gens); 600 } 601 not_older_gens->reset_generation(); 602 } 603 } 604 // When collection is parallel, all threads get to cooperate to do 605 // older-gen scanning. 606 if (level == 0) { 607 older_gens->set_generation(_old_gen); 608 rem_set()->younger_refs_iterate(_old_gen, older_gens); 609 older_gens->reset_generation(); 610 } 611 612 _gen_process_roots_tasks->all_tasks_completed(); 613 } 614 615 void GenCollectedHeap:: 616 gen_process_roots(int level, 617 bool younger_gens_as_roots, 618 bool activate_scope, 619 SharedHeap::ScanningOption so, 620 bool only_strong_roots, 621 OopsInGenClosure* not_older_gens, 622 OopsInGenClosure* older_gens, 623 CLDClosure* cld_closure) { 624 625 const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots; 626 627 bool is_moving_collection = false; 628 if (level == 0 || is_adjust_phase) { 629 // young collections are always moving 630 is_moving_collection = true; 631 } 632 633 MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection); 634 CodeBlobClosure* code_closure = &mark_code_closure; 635 636 gen_process_roots(level, 637 younger_gens_as_roots, 638 activate_scope, so, 639 not_older_gens, only_strong_roots ? NULL : not_older_gens, 640 older_gens, 641 cld_closure, only_strong_roots ? NULL : cld_closure, 642 code_closure); 643 644 } 645 646 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) { 647 SharedHeap::process_weak_roots(root_closure); 648 // "Local" "weak" refs 649 _young_gen->ref_processor()->weak_oops_do(root_closure); 650 _old_gen->ref_processor()->weak_oops_do(root_closure); 651 } 652 653 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \ 654 void GenCollectedHeap:: \ 655 oop_since_save_marks_iterate(int level, \ 656 OopClosureType* cur, \ 657 OopClosureType* older) { \ 658 if (level == 0) { \ 659 _young_gen->oop_since_save_marks_iterate##nv_suffix(cur); \ 660 _old_gen->oop_since_save_marks_iterate##nv_suffix(older); \ 661 } else { \ 662 _old_gen->oop_since_save_marks_iterate##nv_suffix(cur); \ 663 } \ 664 } 665 666 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN) 667 668 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN 669 670 bool GenCollectedHeap::no_allocs_since_save_marks(int level) { 671 if (level == 0) { 672 if (!_young_gen->no_allocs_since_save_marks()) return false; 673 } 674 if (!_old_gen->no_allocs_since_save_marks()) return false; 675 return true; 676 } 677 678 bool GenCollectedHeap::supports_inline_contig_alloc() const { 679 return _young_gen->supports_inline_contig_alloc(); 680 } 681 682 HeapWord** GenCollectedHeap::top_addr() const { 683 return _young_gen->top_addr(); 684 } 685 686 HeapWord** GenCollectedHeap::end_addr() const { 687 return _young_gen->end_addr(); 688 } 689 690 // public collection interfaces 691 692 void GenCollectedHeap::collect(GCCause::Cause cause) { 693 if (should_do_concurrent_full_gc(cause)) { 694 #if INCLUDE_ALL_GCS 695 // mostly concurrent full collection 696 collect_mostly_concurrent(cause); 697 #else // INCLUDE_ALL_GCS 698 ShouldNotReachHere(); 699 #endif // INCLUDE_ALL_GCS 700 } else { 701 #ifdef ASSERT 702 if (cause == GCCause::_scavenge_alot) { 703 // minor collection only 704 collect(cause, 0); 705 } else { 706 // Stop-the-world full collection 707 collect(cause, n_gens() - 1); 727 } 728 729 // this is the private collection interface 730 // The Heap_lock is expected to be held on entry. 731 732 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) { 733 // Read the GC count while holding the Heap_lock 734 unsigned int gc_count_before = total_collections(); 735 unsigned int full_gc_count_before = total_full_collections(); 736 { 737 MutexUnlocker mu(Heap_lock); // give up heap lock, execute gets it back 738 VM_GenCollectFull op(gc_count_before, full_gc_count_before, 739 cause, max_level); 740 VMThread::execute(&op); 741 } 742 } 743 744 #if INCLUDE_ALL_GCS 745 bool GenCollectedHeap::create_cms_collector() { 746 747 assert(_old_gen->kind() == Generation::ConcurrentMarkSweep, 748 "Unexpected generation kinds"); 749 // Skip two header words in the block content verification 750 NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();) 751 CMSCollector* collector = new CMSCollector( 752 (ConcurrentMarkSweepGeneration*)_old_gen, 753 _rem_set->as_CardTableRS(), 754 (ConcurrentMarkSweepPolicy*) collector_policy()); 755 756 if (collector == NULL || !collector->completed_initialization()) { 757 if (collector) { 758 delete collector; // Be nice in embedded situation 759 } 760 vm_shutdown_during_initialization("Could not create CMS collector"); 761 return false; 762 } 763 return true; // success 764 } 765 766 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) { 767 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock"); 768 769 MutexLocker ml(Heap_lock); 770 // Read the GC counts while holding the Heap_lock 771 unsigned int full_gc_count_before = total_full_collections(); 772 unsigned int gc_count_before = total_collections(); 799 local_max_level /* max_level */); 800 // Hack XXX FIX ME !!! 801 // A scavenge may not have been attempted, or may have 802 // been attempted and failed, because the old gen was too full 803 if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker && 804 incremental_collection_will_fail(false /* don't consult_young */)) { 805 if (PrintGCDetails) { 806 gclog_or_tty->print_cr("GC locker: Trying a full collection " 807 "because scavenge failed"); 808 } 809 // This time allow the old gen to be collected as well 810 do_collection(true /* full */, 811 clear_all_soft_refs /* clear_all_soft_refs */, 812 0 /* size */, 813 false /* is_tlab */, 814 n_gens() - 1 /* max_level */); 815 } 816 } 817 818 bool GenCollectedHeap::is_in_young(oop p) { 819 bool result = ((HeapWord*)p) < _old_gen->reserved().start(); 820 assert(result == _young_gen->is_in_reserved(p), 821 err_msg("incorrect test - result=%d, p=" INTPTR_FORMAT, result, p2i((void*)p))); 822 return result; 823 } 824 825 // Returns "TRUE" iff "p" points into the committed areas of the heap. 826 bool GenCollectedHeap::is_in(const void* p) const { 827 #ifndef ASSERT 828 guarantee(VerifyBeforeGC || 829 VerifyDuringGC || 830 VerifyBeforeExit || 831 VerifyDuringStartup || 832 PrintAssembly || 833 tty->count() != 0 || // already printing 834 VerifyAfterGC || 835 VMError::fatal_error_in_progress(), "too expensive"); 836 837 #endif 838 // This might be sped up with a cache of the last generation that 839 // answered yes. 840 if (_young_gen->is_in(p) || _old_gen->is_in(p)) { 841 return true; 842 } 843 // Otherwise... 844 return false; 845 } 846 847 #ifdef ASSERT 848 // Don't implement this by using is_in_young(). This method is used 849 // in some cases to check that is_in_young() is correct. 850 bool GenCollectedHeap::is_in_partial_collection(const void* p) { 851 assert(is_in_reserved(p) || p == NULL, 852 "Does not work if address is non-null and outside of the heap"); 853 return p < _young_gen->reserved().end() && p != NULL; 854 } 855 #endif 856 857 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) { 858 _young_gen->oop_iterate(cl); 859 _old_gen->oop_iterate(cl); 860 } 861 862 void GenCollectedHeap::object_iterate(ObjectClosure* cl) { 863 _young_gen->object_iterate(cl); 864 _old_gen->object_iterate(cl); 865 } 866 867 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) { 868 _young_gen->safe_object_iterate(cl); 869 _old_gen->safe_object_iterate(cl); 870 } 871 872 Space* GenCollectedHeap::space_containing(const void* addr) const { 873 Space* res = _young_gen->space_containing(addr); 874 if (res != NULL) { 875 return res; 876 } 877 res = _old_gen->space_containing(addr); 878 assert(res != NULL, "Could not find containing space"); 879 return res; 880 } 881 882 HeapWord* GenCollectedHeap::block_start(const void* addr) const { 883 assert(is_in_reserved(addr), "block_start of address outside of heap"); 884 if (_young_gen->is_in_reserved(addr)) { 885 assert(_young_gen->is_in(addr), "addr should be in allocated part of generation"); 886 return _young_gen->block_start(addr); 887 } 888 889 assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address"); 890 assert(_old_gen->is_in(addr), "addr should be in allocated part of generation"); 891 return _old_gen->block_start(addr); 892 } 893 894 size_t GenCollectedHeap::block_size(const HeapWord* addr) const { 895 assert(is_in_reserved(addr), "block_size of address outside of heap"); 896 if (_young_gen->is_in_reserved(addr)) { 897 assert(_young_gen->is_in(addr), "addr should be in allocated part of generation"); 898 return _young_gen->block_size(addr); 899 } 900 901 assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address"); 902 assert(_old_gen->is_in(addr), "addr should be in allocated part of generation"); 903 return _old_gen->block_size(addr); 904 } 905 906 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const { 907 assert(is_in_reserved(addr), "block_is_obj of address outside of heap"); 908 assert(block_start(addr) == addr, "addr must be a block start"); 909 if (_young_gen->is_in_reserved(addr)) { 910 return _young_gen->block_is_obj(addr); 911 } 912 913 assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address"); 914 return _old_gen->block_is_obj(addr); 915 } 916 917 bool GenCollectedHeap::supports_tlab_allocation() const { 918 assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!"); 919 return _young_gen->supports_tlab_allocation(); 920 } 921 922 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const { 923 assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!"); 924 if (_young_gen->supports_tlab_allocation()) { 925 return _young_gen->tlab_capacity(); 926 } 927 return 0; 928 } 929 930 size_t GenCollectedHeap::tlab_used(Thread* thr) const { 931 assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!"); 932 if (_young_gen->supports_tlab_allocation()) { 933 return _young_gen->tlab_used(); 934 } 935 return 0; 936 } 937 938 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const { 939 assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!"); 940 if (_young_gen->supports_tlab_allocation()) { 941 return _young_gen->unsafe_max_tlab_alloc(); 942 } 943 return 0; 944 } 945 946 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) { 947 bool gc_overhead_limit_was_exceeded; 948 return collector_policy()->mem_allocate_work(size /* size */, 949 true /* is_tlab */, 950 &gc_overhead_limit_was_exceeded); 951 } 952 953 // Requires "*prev_ptr" to be non-NULL. Deletes and a block of minimal size 954 // from the list headed by "*prev_ptr". 955 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) { 956 bool first = true; 957 size_t min_size = 0; // "first" makes this conceptually infinite. 958 ScratchBlock **smallest_ptr, *smallest; 959 ScratchBlock *cur = *prev_ptr; 960 while (cur) { 961 assert(*prev_ptr == cur, "just checking"); 962 if (first || cur->num_words < min_size) { 963 smallest_ptr = prev_ptr; 972 *smallest_ptr = smallest->next; 973 return smallest; 974 } 975 976 // Sort the scratch block list headed by res into decreasing size order, 977 // and set "res" to the result. 978 static void sort_scratch_list(ScratchBlock*& list) { 979 ScratchBlock* sorted = NULL; 980 ScratchBlock* unsorted = list; 981 while (unsorted) { 982 ScratchBlock *smallest = removeSmallestScratch(&unsorted); 983 smallest->next = sorted; 984 sorted = smallest; 985 } 986 list = sorted; 987 } 988 989 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor, 990 size_t max_alloc_words) { 991 ScratchBlock* res = NULL; 992 _young_gen->contribute_scratch(res, requestor, max_alloc_words); 993 _old_gen->contribute_scratch(res, requestor, max_alloc_words); 994 sort_scratch_list(res); 995 return res; 996 } 997 998 void GenCollectedHeap::release_scratch() { 999 _young_gen->reset_scratch(); 1000 _old_gen->reset_scratch(); 1001 } 1002 1003 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure { 1004 void do_generation(Generation* gen) { 1005 gen->prepare_for_verify(); 1006 } 1007 }; 1008 1009 void GenCollectedHeap::prepare_for_verify() { 1010 ensure_parsability(false); // no need to retire TLABs 1011 GenPrepareForVerifyClosure blk; 1012 generation_iterate(&blk, false); 1013 } 1014 1015 void GenCollectedHeap::generation_iterate(GenClosure* cl, 1016 bool old_to_young) { 1017 if (old_to_young) { 1018 cl->do_generation(_old_gen); 1019 cl->do_generation(_young_gen); 1020 } else { 1021 cl->do_generation(_young_gen); 1022 cl->do_generation(_old_gen); 1023 } 1024 } 1025 1026 void GenCollectedHeap::space_iterate(SpaceClosure* cl) { 1027 _young_gen->space_iterate(cl, true); 1028 _old_gen->space_iterate(cl, true); 1029 } 1030 1031 bool GenCollectedHeap::is_maximal_no_gc() const { 1032 return _young_gen->is_maximal_no_gc() && _old_gen->is_maximal_no_gc(); 1033 } 1034 1035 void GenCollectedHeap::save_marks() { 1036 _young_gen->save_marks(); 1037 _old_gen->save_marks(); 1038 } 1039 1040 GenCollectedHeap* GenCollectedHeap::heap() { 1041 assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()"); 1042 assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap"); 1043 return _gch; 1044 } 1045 1046 1047 void GenCollectedHeap::prepare_for_compaction() { 1048 guarantee(_n_gens = 2, "Wrong number of generations"); 1049 Generation* old_gen = _old_gen; 1050 // Start by compacting into same gen. 1051 CompactPoint cp(old_gen); 1052 old_gen->prepare_for_compaction(&cp); 1053 Generation* young_gen = _young_gen; 1054 young_gen->prepare_for_compaction(&cp); 1055 } 1056 1057 GCStats* GenCollectedHeap::gc_stats(int level) const { 1058 if (level == 0) { 1059 return _young_gen->gc_stats(); 1060 } else { 1061 return _old_gen->gc_stats(); 1062 } 1063 } 1064 1065 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) { 1066 if (!silent) { 1067 gclog_or_tty->print("%s", _old_gen->name()); 1068 gclog_or_tty->print(" "); 1069 } 1070 _old_gen->verify(); 1071 1072 if (!silent) { 1073 gclog_or_tty->print("%s", _young_gen->name()); 1074 gclog_or_tty->print(" "); 1075 } 1076 _young_gen->verify(); 1077 1078 if (!silent) { 1079 gclog_or_tty->print("remset "); 1080 } 1081 rem_set()->verify(); 1082 } 1083 1084 void GenCollectedHeap::print_on(outputStream* st) const { 1085 _young_gen->print_on(st); 1086 _old_gen->print_on(st); 1087 MetaspaceAux::print_on(st); 1088 } 1089 1090 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const { 1091 if (workers() != NULL) { 1092 workers()->threads_do(tc); 1093 } 1094 #if INCLUDE_ALL_GCS 1095 if (UseConcMarkSweepGC) { 1096 ConcurrentMarkSweepThread::threads_do(tc); 1097 } 1098 #endif // INCLUDE_ALL_GCS 1099 } 1100 1101 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const { 1102 #if INCLUDE_ALL_GCS 1103 if (UseParNewGC) { 1104 workers()->print_worker_threads_on(st); 1105 } 1106 if (UseConcMarkSweepGC) { |