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