< prev index next >

src/share/vm/gc/cms/concurrentMarkSweepGeneration.cpp

Print this page




 173 //////////////////////////////////////////////////////////////////
 174 //  Concurrent Mark-Sweep Generation /////////////////////////////
 175 //////////////////////////////////////////////////////////////////
 176 
 177 NOT_PRODUCT(CompactibleFreeListSpace* debug_cms_space;)
 178 
 179 // This struct contains per-thread things necessary to support parallel
 180 // young-gen collection.
 181 class CMSParGCThreadState: public CHeapObj<mtGC> {
 182  public:
 183   CFLS_LAB lab;
 184   PromotionInfo promo;
 185 
 186   // Constructor.
 187   CMSParGCThreadState(CompactibleFreeListSpace* cfls) : lab(cfls) {
 188     promo.setSpace(cfls);
 189   }
 190 };
 191 
 192 ConcurrentMarkSweepGeneration::ConcurrentMarkSweepGeneration(
 193      ReservedSpace rs, size_t initial_byte_size, int level,
 194      CardTableRS* ct, bool use_adaptive_freelists,
 195      FreeBlockDictionary<FreeChunk>::DictionaryChoice dictionaryChoice) :
 196   CardGeneration(rs, initial_byte_size, level, ct),
 197   _dilatation_factor(((double)MinChunkSize)/((double)(CollectedHeap::min_fill_size()))),
 198   _did_compact(false)
 199 {
 200   HeapWord* bottom = (HeapWord*) _virtual_space.low();
 201   HeapWord* end    = (HeapWord*) _virtual_space.high();
 202 
 203   _direct_allocated_words = 0;
 204   NOT_PRODUCT(
 205     _numObjectsPromoted = 0;
 206     _numWordsPromoted = 0;
 207     _numObjectsAllocated = 0;
 208     _numWordsAllocated = 0;
 209   )
 210 
 211   _cmsSpace = new CompactibleFreeListSpace(_bts, MemRegion(bottom, end),
 212                                            use_adaptive_freelists,
 213                                            dictionaryChoice);
 214   NOT_PRODUCT(debug_cms_space = _cmsSpace;)
 215   _cmsSpace->_gen = this;
 216 


 665     _space_counters->update_used(used);
 666     _space_counters->update_capacity();
 667     _gen_counters->update_all();
 668   }
 669 }
 670 
 671 void ConcurrentMarkSweepGeneration::print() const {
 672   Generation::print();
 673   cmsSpace()->print();
 674 }
 675 
 676 #ifndef PRODUCT
 677 void ConcurrentMarkSweepGeneration::print_statistics() {
 678   cmsSpace()->printFLCensus(0);
 679 }
 680 #endif
 681 
 682 void ConcurrentMarkSweepGeneration::printOccupancy(const char *s) {
 683   GenCollectedHeap* gch = GenCollectedHeap::heap();
 684   if (PrintGCDetails) {





 685     if (Verbose) {
 686       gclog_or_tty->print("[%d %s-%s: "SIZE_FORMAT"("SIZE_FORMAT")]",
 687         level(), short_name(), s, used(), capacity());
 688     } else {
 689       gclog_or_tty->print("[%d %s-%s: "SIZE_FORMAT"K("SIZE_FORMAT"K)]",
 690         level(), short_name(), s, used() / K, capacity() / K);
 691     }
 692   }
 693   if (Verbose) {
 694     gclog_or_tty->print(" "SIZE_FORMAT"("SIZE_FORMAT")",
 695               gch->used(), gch->capacity());
 696   } else {
 697     gclog_or_tty->print(" "SIZE_FORMAT"K("SIZE_FORMAT"K)",
 698               gch->used() / K, gch->capacity() / K);
 699   }
 700 }
 701 
 702 size_t
 703 ConcurrentMarkSweepGeneration::contiguous_available() const {
 704   // dld proposes an improvement in precision here. If the committed
 705   // part of the space ends in a free block we should add that to
 706   // uncommitted size in the calculation below. Will make this
 707   // change later, staying with the approximation below for the
 708   // time being. -- ysr.
 709   return MAX2(_virtual_space.uncommitted_size(), unsafe_max_alloc_nogc());
 710 }


 780   if (incremental_collection_failed()) {
 781     clear_incremental_collection_failed();
 782     grow_to_reserved();
 783     return;
 784   }
 785 
 786   double free_percentage = ((double) free()) / capacity();
 787   double desired_free_percentage = (double) MinHeapFreeRatio / 100;
 788   double maximum_free_percentage = (double) MaxHeapFreeRatio / 100;
 789 
 790   // compute expansion delta needed for reaching desired free percentage
 791   if (free_percentage < desired_free_percentage) {
 792     size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));
 793     assert(desired_capacity >= capacity(), "invalid expansion size");
 794     size_t expand_bytes = MAX2(desired_capacity - capacity(), MinHeapDeltaBytes);
 795     if (PrintGCDetails && Verbose) {
 796       size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));
 797       gclog_or_tty->print_cr("\nFrom compute_new_size: ");
 798       gclog_or_tty->print_cr("  Free fraction %f", free_percentage);
 799       gclog_or_tty->print_cr("  Desired free fraction %f",
 800         desired_free_percentage);
 801       gclog_or_tty->print_cr("  Maximum free fraction %f",
 802         maximum_free_percentage);
 803       gclog_or_tty->print_cr("  Capacity "SIZE_FORMAT, capacity()/1000);
 804       gclog_or_tty->print_cr("  Desired capacity "SIZE_FORMAT,
 805         desired_capacity/1000);
 806       int prev_level = level() - 1;
 807       if (prev_level >= 0) {
 808         size_t prev_size = 0;
 809         GenCollectedHeap* gch = GenCollectedHeap::heap();
 810         Generation* prev_gen = gch->young_gen();
 811         prev_size = prev_gen->capacity();
 812           gclog_or_tty->print_cr("  Younger gen size "SIZE_FORMAT,
 813                                  prev_size/1000);
 814       }
 815       gclog_or_tty->print_cr("  unsafe_max_alloc_nogc "SIZE_FORMAT,
 816         unsafe_max_alloc_nogc()/1000);
 817       gclog_or_tty->print_cr("  contiguous available "SIZE_FORMAT,
 818         contiguous_available()/1000);
 819       gclog_or_tty->print_cr("  Expand by "SIZE_FORMAT" (bytes)",
 820         expand_bytes);
 821     }
 822     // safe if expansion fails
 823     expand_for_gc_cause(expand_bytes, 0, CMSExpansionCause::_satisfy_free_ratio);
 824     if (PrintGCDetails && Verbose) {
 825       gclog_or_tty->print_cr("  Expanded free fraction %f",
 826         ((double) free()) / capacity());
 827     }
 828   } else {
 829     size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));
 830     assert(desired_capacity <= capacity(), "invalid expansion size");
 831     size_t shrink_bytes = capacity() - desired_capacity;
 832     // Don't shrink unless the delta is greater than the minimum shrink we want
 833     if (shrink_bytes >= MinHeapDeltaBytes) {
 834       shrink_free_list_by(shrink_bytes);
 835     }
 836   }
 837 }
 838 
 839 Mutex* ConcurrentMarkSweepGeneration::freelistLock() const {
 840   return cmsSpace()->freelistLock();


1633   // collection, clear the _modUnionTable.
1634   assert(_collectorState != Idling || _modUnionTable.isAllClear(),
1635     "_modUnionTable should be clear if the baton was not passed");
1636   _modUnionTable.clear_all();
1637   assert(_collectorState != Idling || _ct->klass_rem_set()->mod_union_is_clear(),
1638     "mod union for klasses should be clear if the baton was passed");
1639   _ct->klass_rem_set()->clear_mod_union();
1640 
1641   // We must adjust the allocation statistics being maintained
1642   // in the free list space. We do so by reading and clearing
1643   // the sweep timer and updating the block flux rate estimates below.
1644   assert(!_intra_sweep_timer.is_active(), "_intra_sweep_timer should be inactive");
1645   if (_inter_sweep_timer.is_active()) {
1646     _inter_sweep_timer.stop();
1647     // Note that we do not use this sample to update the _inter_sweep_estimate.
1648     _cmsGen->cmsSpace()->beginSweepFLCensus((float)(_inter_sweep_timer.seconds()),
1649                                             _inter_sweep_estimate.padded_average(),
1650                                             _intra_sweep_estimate.padded_average());
1651   }
1652 
1653   GenMarkSweep::invoke_at_safepoint(_cmsGen->level(),
1654     ref_processor(), clear_all_soft_refs);
1655   #ifdef ASSERT
1656     CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();
1657     size_t free_size = cms_space->free();
1658     assert(free_size ==
1659            pointer_delta(cms_space->end(), cms_space->compaction_top())
1660            * HeapWordSize,
1661       "All the free space should be compacted into one chunk at top");
1662     assert(cms_space->dictionary()->total_chunk_size(
1663                                       debug_only(cms_space->freelistLock())) == 0 ||
1664            cms_space->totalSizeInIndexedFreeLists() == 0,
1665       "All the free space should be in a single chunk");
1666     size_t num = cms_space->totalCount();
1667     assert((free_size == 0 && num == 0) ||
1668            (free_size > 0  && (num == 1 || num == 2)),
1669          "There should be at most 2 free chunks after compaction");
1670   #endif // ASSERT
1671   _collectorState = Resetting;
1672   assert(_restart_addr == NULL,
1673          "Should have been NULL'd before baton was passed");
1674   reset(false /* == !concurrent */);


2415   if (!silent) gclog_or_tty->print(" done] ");
2416   return true;
2417 }
2418 
2419 void CMSCollector::verify_after_remark_work_1() {
2420   ResourceMark rm;
2421   HandleMark  hm;
2422   GenCollectedHeap* gch = GenCollectedHeap::heap();
2423 
2424   // Get a clear set of claim bits for the roots processing to work with.
2425   ClassLoaderDataGraph::clear_claimed_marks();
2426 
2427   // Mark from roots one level into CMS
2428   MarkRefsIntoClosure notOlder(_span, verification_mark_bm());
2429   gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2430 
2431   {
2432     StrongRootsScope srs(1);
2433 
2434     gch->gen_process_roots(&srs,
2435                            _cmsGen->level(),
2436                            true,   // younger gens are roots
2437                            GenCollectedHeap::ScanningOption(roots_scanning_options()),
2438                            should_unload_classes(),
2439                            &notOlder,
2440                            NULL,
2441                            NULL);
2442   }
2443 
2444   // Now mark from the roots
2445   MarkFromRootsClosure markFromRootsClosure(this, _span,
2446     verification_mark_bm(), verification_mark_stack(),
2447     false /* don't yield */, true /* verifying */);
2448   assert(_restart_addr == NULL, "Expected pre-condition");
2449   verification_mark_bm()->iterate(&markFromRootsClosure);
2450   while (_restart_addr != NULL) {
2451     // Deal with stack overflow: by restarting at the indicated
2452     // address.
2453     HeapWord* ra = _restart_addr;
2454     markFromRootsClosure.reset(ra);
2455     _restart_addr = NULL;


2487 
2488 void CMSCollector::verify_after_remark_work_2() {
2489   ResourceMark rm;
2490   HandleMark  hm;
2491   GenCollectedHeap* gch = GenCollectedHeap::heap();
2492 
2493   // Get a clear set of claim bits for the roots processing to work with.
2494   ClassLoaderDataGraph::clear_claimed_marks();
2495 
2496   // Mark from roots one level into CMS
2497   MarkRefsIntoVerifyClosure notOlder(_span, verification_mark_bm(),
2498                                      markBitMap());
2499   CLDToOopClosure cld_closure(&notOlder, true);
2500 
2501   gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2502 
2503   {
2504     StrongRootsScope srs(1);
2505 
2506     gch->gen_process_roots(&srs,
2507                            _cmsGen->level(),
2508                            true,   // younger gens are roots
2509                            GenCollectedHeap::ScanningOption(roots_scanning_options()),
2510                            should_unload_classes(),
2511                            &notOlder,
2512                            NULL,
2513                            &cld_closure);
2514   }
2515 
2516   // Now mark from the roots
2517   MarkFromRootsVerifyClosure markFromRootsClosure(this, _span,
2518     verification_mark_bm(), markBitMap(), verification_mark_stack());
2519   assert(_restart_addr == NULL, "Expected pre-condition");
2520   verification_mark_bm()->iterate(&markFromRootsClosure);
2521   while (_restart_addr != NULL) {
2522     // Deal with stack overflow: by restarting at the indicated
2523     // address.
2524     HeapWord* ra = _restart_addr;
2525     markFromRootsClosure.reset(ra);
2526     _restart_addr = NULL;
2527     verification_mark_bm()->iterate(&markFromRootsClosure, ra, _span.end());


3014       assert(workers != NULL, "Need parallel worker threads.");
3015       uint n_workers = workers->active_workers();
3016 
3017       StrongRootsScope srs(n_workers);
3018 
3019       CMSParInitialMarkTask tsk(this, &srs, n_workers);
3020       initialize_sequential_subtasks_for_young_gen_rescan(n_workers);
3021       if (n_workers > 1) {
3022         workers->run_task(&tsk);
3023       } else {
3024         tsk.work(0);
3025       }
3026     } else {
3027       // The serial version.
3028       CLDToOopClosure cld_closure(&notOlder, true);
3029       gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
3030 
3031       StrongRootsScope srs(1);
3032 
3033       gch->gen_process_roots(&srs,
3034                              _cmsGen->level(),
3035                              true,   // younger gens are roots
3036                              GenCollectedHeap::ScanningOption(roots_scanning_options()),
3037                              should_unload_classes(),
3038                              &notOlder,
3039                              NULL,
3040                              &cld_closure);
3041     }
3042   }
3043 
3044   // Clear mod-union table; it will be dirtied in the prologue of
3045   // CMS generation per each younger generation collection.
3046 
3047   assert(_modUnionTable.isAllClear(),
3048        "Was cleared in most recent final checkpoint phase"
3049        " or no bits are set in the gc_prologue before the start of the next "
3050        "subsequent marking phase.");
3051 
3052   assert(_ct->klass_rem_set()->mod_union_is_clear(), "Must be");
3053 
3054   // Save the end of the used_region of the constituent generations


4265   assert(SafepointSynchronize::is_at_safepoint(),
4266          "world should be stopped");
4267   TraceCMSMemoryManagerStats tms(_collectorState,GenCollectedHeap::heap()->gc_cause());
4268 
4269   verify_work_stacks_empty();
4270   verify_overflow_empty();
4271 
4272   if (PrintGCDetails) {
4273     gclog_or_tty->print("[YG occupancy: "SIZE_FORMAT" K ("SIZE_FORMAT" K)]",
4274                         _young_gen->used() / K,
4275                         _young_gen->capacity() / K);
4276   }
4277   {
4278     if (CMSScavengeBeforeRemark) {
4279       GenCollectedHeap* gch = GenCollectedHeap::heap();
4280       // Temporarily set flag to false, GCH->do_collection will
4281       // expect it to be false and set to true
4282       FlagSetting fl(gch->_is_gc_active, false);
4283       NOT_PRODUCT(GCTraceTime t("Scavenge-Before-Remark",
4284         PrintGCDetails && Verbose, true, _gc_timer_cm, _gc_tracer_cm->gc_id());)
4285       int level = _cmsGen->level() - 1;
4286       if (level >= 0) {
4287         gch->do_collection(true,        // full (i.e. force, see below)
4288                            false,       // !clear_all_soft_refs
4289                            0,           // size
4290                            false,       // is_tlab
4291                            level        // max_level
4292                           );
4293       }
4294     }
4295     FreelistLocker x(this);
4296     MutexLockerEx y(bitMapLock(),
4297                     Mutex::_no_safepoint_check_flag);
4298     checkpointRootsFinalWork();
4299   }
4300   verify_work_stacks_empty();
4301   verify_overflow_empty();
4302 }
4303 
4304 void CMSCollector::checkpointRootsFinalWork() {
4305   NOT_PRODUCT(GCTraceTime tr("checkpointRootsFinalWork", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());)
4306 
4307   assert(haveFreelistLocks(), "must have free list locks");
4308   assert_lock_strong(bitMapLock());
4309 
4310   ResourceMark rm;
4311   HandleMark   hm;
4312 
4313   GenCollectedHeap* gch = GenCollectedHeap::heap();


4447   Par_MarkRefsIntoClosure par_mri_cl(_collector->_span, &(_collector->_markBitMap));
4448 
4449   // ---------- young gen roots --------------
4450   {
4451     work_on_young_gen_roots(worker_id, &par_mri_cl);
4452     _timer.stop();
4453     if (PrintCMSStatistics != 0) {
4454       gclog_or_tty->print_cr(
4455         "Finished young gen initial mark scan work in %dth thread: %3.3f sec",
4456         worker_id, _timer.seconds());
4457     }
4458   }
4459 
4460   // ---------- remaining roots --------------
4461   _timer.reset();
4462   _timer.start();
4463 
4464   CLDToOopClosure cld_closure(&par_mri_cl, true);
4465 
4466   gch->gen_process_roots(_strong_roots_scope,
4467                          _collector->_cmsGen->level(),
4468                          false,     // yg was scanned above
4469                          GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),
4470                          _collector->should_unload_classes(),
4471                          &par_mri_cl,
4472                          NULL,
4473                          &cld_closure);
4474   assert(_collector->should_unload_classes()
4475          || (_collector->CMSCollector::roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),
4476          "if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");
4477   _timer.stop();
4478   if (PrintCMSStatistics != 0) {
4479     gclog_or_tty->print_cr(
4480       "Finished remaining root initial mark scan work in %dth thread: %3.3f sec",
4481       worker_id, _timer.seconds());
4482   }
4483 }
4484 
4485 // Parallel remark task
4486 class CMSParRemarkTask: public CMSParMarkTask {
4487   CompactibleFreeListSpace* _cms_space;


4586 
4587   // Rescan young gen roots first since these are likely
4588   // coarsely partitioned and may, on that account, constitute
4589   // the critical path; thus, it's best to start off that
4590   // work first.
4591   // ---------- young gen roots --------------
4592   {
4593     work_on_young_gen_roots(worker_id, &par_mrias_cl);
4594     _timer.stop();
4595     if (PrintCMSStatistics != 0) {
4596       gclog_or_tty->print_cr(
4597         "Finished young gen rescan work in %dth thread: %3.3f sec",
4598         worker_id, _timer.seconds());
4599     }
4600   }
4601 
4602   // ---------- remaining roots --------------
4603   _timer.reset();
4604   _timer.start();
4605   gch->gen_process_roots(_strong_roots_scope,
4606                          _collector->_cmsGen->level(),
4607                          false,     // yg was scanned above
4608                          GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),
4609                          _collector->should_unload_classes(),
4610                          &par_mrias_cl,
4611                          NULL,
4612                          NULL);     // The dirty klasses will be handled below
4613 
4614   assert(_collector->should_unload_classes()
4615          || (_collector->CMSCollector::roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),
4616          "if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");
4617   _timer.stop();
4618   if (PrintCMSStatistics != 0) {
4619     gclog_or_tty->print_cr(
4620       "Finished remaining root rescan work in %dth thread: %3.3f sec",
4621       worker_id, _timer.seconds());
4622   }
4623 
4624   // ---------- unhandled CLD scanning ----------
4625   if (worker_id == 0) { // Single threaded at the moment.
4626     _timer.reset();


5167       if (PrintCMSStatistics != 0) {
5168         gclog_or_tty->print(" (re-scanned "SIZE_FORMAT" dirty cards in cms gen) ",
5169           markFromDirtyCardsClosure.num_dirty_cards());
5170       }
5171     }
5172   }
5173   if (VerifyDuringGC &&
5174       GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
5175     HandleMark hm;  // Discard invalid handles created during verification
5176     Universe::verify();
5177   }
5178   {
5179     GCTraceTime t("root rescan", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());
5180 
5181     verify_work_stacks_empty();
5182 
5183     gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
5184     StrongRootsScope srs(1);
5185 
5186     gch->gen_process_roots(&srs,
5187                            _cmsGen->level(),
5188                            true,  // younger gens as roots
5189                            GenCollectedHeap::ScanningOption(roots_scanning_options()),
5190                            should_unload_classes(),
5191                            &mrias_cl,
5192                            NULL,
5193                            NULL); // The dirty klasses will be handled below
5194 
5195     assert(should_unload_classes()
5196            || (roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),
5197            "if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");
5198   }
5199 
5200   {
5201     GCTraceTime t("visit unhandled CLDs", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());
5202 
5203     verify_work_stacks_empty();
5204 
5205     // Scan all class loader data objects that might have been introduced
5206     // during concurrent marking.
5207     ResourceMark rm;


5631   size_t nearLargestOffset =
5632     (size_t)((double)largestOffset * nearLargestPercent) - MinChunkSize;
5633   if (PrintFLSStatistics != 0) {
5634     gclog_or_tty->print_cr(
5635       "CMS: Large Block: " PTR_FORMAT ";"
5636       " Proximity: " PTR_FORMAT " -> " PTR_FORMAT,
5637       p2i(largestAddr),
5638       p2i(_cmsSpace->nearLargestChunk()), p2i(minAddr + nearLargestOffset));
5639   }
5640   _cmsSpace->set_nearLargestChunk(minAddr + nearLargestOffset);
5641 }
5642 
5643 bool ConcurrentMarkSweepGeneration::isNearLargestChunk(HeapWord* addr) {
5644   return addr >= _cmsSpace->nearLargestChunk();
5645 }
5646 
5647 FreeChunk* ConcurrentMarkSweepGeneration::find_chunk_at_end() {
5648   return _cmsSpace->find_chunk_at_end();
5649 }
5650 
5651 void ConcurrentMarkSweepGeneration::update_gc_stats(int current_level,
5652                                                     bool full) {
5653   // The next lower level has been collected.  Gather any statistics
5654   // that are of interest at this point.
5655   if (!full && (current_level + 1) == level()) {

5656     // Gather statistics on the young generation collection.
5657     collector()->stats().record_gc0_end(used());
5658   }
5659 }
5660 
5661 void CMSCollector::sweepWork(ConcurrentMarkSweepGeneration* gen) {
5662   // We iterate over the space(s) underlying this generation,
5663   // checking the mark bit map to see if the bits corresponding
5664   // to specific blocks are marked or not. Blocks that are
5665   // marked are live and are not swept up. All remaining blocks
5666   // are swept up, with coalescing on-the-fly as we sweep up
5667   // contiguous free and/or garbage blocks:
5668   // We need to ensure that the sweeper synchronizes with allocators
5669   // and stop-the-world collectors. In particular, the following
5670   // locks are used:
5671   // . CMS token: if this is held, a stop the world collection cannot occur
5672   // . freelistLock: if this is held no allocation can occur from this
5673   //                 generation by another thread
5674   // . bitMapLock: if this is held, no other thread can access or update
5675   //




 173 //////////////////////////////////////////////////////////////////
 174 //  Concurrent Mark-Sweep Generation /////////////////////////////
 175 //////////////////////////////////////////////////////////////////
 176 
 177 NOT_PRODUCT(CompactibleFreeListSpace* debug_cms_space;)
 178 
 179 // This struct contains per-thread things necessary to support parallel
 180 // young-gen collection.
 181 class CMSParGCThreadState: public CHeapObj<mtGC> {
 182  public:
 183   CFLS_LAB lab;
 184   PromotionInfo promo;
 185 
 186   // Constructor.
 187   CMSParGCThreadState(CompactibleFreeListSpace* cfls) : lab(cfls) {
 188     promo.setSpace(cfls);
 189   }
 190 };
 191 
 192 ConcurrentMarkSweepGeneration::ConcurrentMarkSweepGeneration(
 193      ReservedSpace rs, size_t initial_byte_size,
 194      CardTableRS* ct, bool use_adaptive_freelists,
 195      FreeBlockDictionary<FreeChunk>::DictionaryChoice dictionaryChoice) :
 196   CardGeneration(rs, initial_byte_size, ct),
 197   _dilatation_factor(((double)MinChunkSize)/((double)(CollectedHeap::min_fill_size()))),
 198   _did_compact(false)
 199 {
 200   HeapWord* bottom = (HeapWord*) _virtual_space.low();
 201   HeapWord* end    = (HeapWord*) _virtual_space.high();
 202 
 203   _direct_allocated_words = 0;
 204   NOT_PRODUCT(
 205     _numObjectsPromoted = 0;
 206     _numWordsPromoted = 0;
 207     _numObjectsAllocated = 0;
 208     _numWordsAllocated = 0;
 209   )
 210 
 211   _cmsSpace = new CompactibleFreeListSpace(_bts, MemRegion(bottom, end),
 212                                            use_adaptive_freelists,
 213                                            dictionaryChoice);
 214   NOT_PRODUCT(debug_cms_space = _cmsSpace;)
 215   _cmsSpace->_gen = this;
 216 


 665     _space_counters->update_used(used);
 666     _space_counters->update_capacity();
 667     _gen_counters->update_all();
 668   }
 669 }
 670 
 671 void ConcurrentMarkSweepGeneration::print() const {
 672   Generation::print();
 673   cmsSpace()->print();
 674 }
 675 
 676 #ifndef PRODUCT
 677 void ConcurrentMarkSweepGeneration::print_statistics() {
 678   cmsSpace()->printFLCensus(0);
 679 }
 680 #endif
 681 
 682 void ConcurrentMarkSweepGeneration::printOccupancy(const char *s) {
 683   GenCollectedHeap* gch = GenCollectedHeap::heap();
 684   if (PrintGCDetails) {
 685     // I didn't want to change the logging when removing the level concept,
 686     // but I guess this logging could say "old" or something instead of "1".
 687     assert(gch->is_old_gen(this),
 688            "The CMS generation should be the old generation");
 689     uint level = 1;
 690     if (Verbose) {
 691       gclog_or_tty->print("[%u %s-%s: "SIZE_FORMAT"("SIZE_FORMAT")]",
 692         level, short_name(), s, used(), capacity());
 693     } else {
 694       gclog_or_tty->print("[%u %s-%s: "SIZE_FORMAT"K("SIZE_FORMAT"K)]",
 695         level, short_name(), s, used() / K, capacity() / K);
 696     }
 697   }
 698   if (Verbose) {
 699     gclog_or_tty->print(" "SIZE_FORMAT"("SIZE_FORMAT")",
 700               gch->used(), gch->capacity());
 701   } else {
 702     gclog_or_tty->print(" "SIZE_FORMAT"K("SIZE_FORMAT"K)",
 703               gch->used() / K, gch->capacity() / K);
 704   }
 705 }
 706 
 707 size_t
 708 ConcurrentMarkSweepGeneration::contiguous_available() const {
 709   // dld proposes an improvement in precision here. If the committed
 710   // part of the space ends in a free block we should add that to
 711   // uncommitted size in the calculation below. Will make this
 712   // change later, staying with the approximation below for the
 713   // time being. -- ysr.
 714   return MAX2(_virtual_space.uncommitted_size(), unsafe_max_alloc_nogc());
 715 }


 785   if (incremental_collection_failed()) {
 786     clear_incremental_collection_failed();
 787     grow_to_reserved();
 788     return;
 789   }
 790 
 791   double free_percentage = ((double) free()) / capacity();
 792   double desired_free_percentage = (double) MinHeapFreeRatio / 100;
 793   double maximum_free_percentage = (double) MaxHeapFreeRatio / 100;
 794 
 795   // compute expansion delta needed for reaching desired free percentage
 796   if (free_percentage < desired_free_percentage) {
 797     size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));
 798     assert(desired_capacity >= capacity(), "invalid expansion size");
 799     size_t expand_bytes = MAX2(desired_capacity - capacity(), MinHeapDeltaBytes);
 800     if (PrintGCDetails && Verbose) {
 801       size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));
 802       gclog_or_tty->print_cr("\nFrom compute_new_size: ");
 803       gclog_or_tty->print_cr("  Free fraction %f", free_percentage);
 804       gclog_or_tty->print_cr("  Desired free fraction %f",
 805               desired_free_percentage);
 806       gclog_or_tty->print_cr("  Maximum free fraction %f",
 807               maximum_free_percentage);
 808       gclog_or_tty->print_cr("  Capacity "SIZE_FORMAT, capacity()/1000);
 809       gclog_or_tty->print_cr("  Desired capacity "SIZE_FORMAT,
 810               desired_capacity/1000);
 811       GenCollectedHeap* gch = GenCollectedHeap::heap();
 812       assert(gch->is_old_gen(this), "The CMS generation should always be the old generation");
 813       size_t young_size = gch->young_gen()->capacity();
 814       gclog_or_tty->print_cr("  Young gen size " SIZE_FORMAT, young_size / 1000);





 815       gclog_or_tty->print_cr("  unsafe_max_alloc_nogc "SIZE_FORMAT,
 816               unsafe_max_alloc_nogc()/1000);
 817       gclog_or_tty->print_cr("  contiguous available "SIZE_FORMAT,
 818               contiguous_available()/1000);
 819       gclog_or_tty->print_cr("  Expand by "SIZE_FORMAT" (bytes)",
 820               expand_bytes);
 821     }
 822     // safe if expansion fails
 823     expand_for_gc_cause(expand_bytes, 0, CMSExpansionCause::_satisfy_free_ratio);
 824     if (PrintGCDetails && Verbose) {
 825       gclog_or_tty->print_cr("  Expanded free fraction %f",
 826         ((double) free()) / capacity());
 827     }
 828   } else {
 829     size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));
 830     assert(desired_capacity <= capacity(), "invalid expansion size");
 831     size_t shrink_bytes = capacity() - desired_capacity;
 832     // Don't shrink unless the delta is greater than the minimum shrink we want
 833     if (shrink_bytes >= MinHeapDeltaBytes) {
 834       shrink_free_list_by(shrink_bytes);
 835     }
 836   }
 837 }
 838 
 839 Mutex* ConcurrentMarkSweepGeneration::freelistLock() const {
 840   return cmsSpace()->freelistLock();


1633   // collection, clear the _modUnionTable.
1634   assert(_collectorState != Idling || _modUnionTable.isAllClear(),
1635     "_modUnionTable should be clear if the baton was not passed");
1636   _modUnionTable.clear_all();
1637   assert(_collectorState != Idling || _ct->klass_rem_set()->mod_union_is_clear(),
1638     "mod union for klasses should be clear if the baton was passed");
1639   _ct->klass_rem_set()->clear_mod_union();
1640 
1641   // We must adjust the allocation statistics being maintained
1642   // in the free list space. We do so by reading and clearing
1643   // the sweep timer and updating the block flux rate estimates below.
1644   assert(!_intra_sweep_timer.is_active(), "_intra_sweep_timer should be inactive");
1645   if (_inter_sweep_timer.is_active()) {
1646     _inter_sweep_timer.stop();
1647     // Note that we do not use this sample to update the _inter_sweep_estimate.
1648     _cmsGen->cmsSpace()->beginSweepFLCensus((float)(_inter_sweep_timer.seconds()),
1649                                             _inter_sweep_estimate.padded_average(),
1650                                             _intra_sweep_estimate.padded_average());
1651   }
1652 
1653   GenMarkSweep::invoke_at_safepoint(ref_processor(), clear_all_soft_refs);

1654   #ifdef ASSERT
1655     CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();
1656     size_t free_size = cms_space->free();
1657     assert(free_size ==
1658            pointer_delta(cms_space->end(), cms_space->compaction_top())
1659            * HeapWordSize,
1660       "All the free space should be compacted into one chunk at top");
1661     assert(cms_space->dictionary()->total_chunk_size(
1662                                       debug_only(cms_space->freelistLock())) == 0 ||
1663            cms_space->totalSizeInIndexedFreeLists() == 0,
1664       "All the free space should be in a single chunk");
1665     size_t num = cms_space->totalCount();
1666     assert((free_size == 0 && num == 0) ||
1667            (free_size > 0  && (num == 1 || num == 2)),
1668          "There should be at most 2 free chunks after compaction");
1669   #endif // ASSERT
1670   _collectorState = Resetting;
1671   assert(_restart_addr == NULL,
1672          "Should have been NULL'd before baton was passed");
1673   reset(false /* == !concurrent */);


2414   if (!silent) gclog_or_tty->print(" done] ");
2415   return true;
2416 }
2417 
2418 void CMSCollector::verify_after_remark_work_1() {
2419   ResourceMark rm;
2420   HandleMark  hm;
2421   GenCollectedHeap* gch = GenCollectedHeap::heap();
2422 
2423   // Get a clear set of claim bits for the roots processing to work with.
2424   ClassLoaderDataGraph::clear_claimed_marks();
2425 
2426   // Mark from roots one level into CMS
2427   MarkRefsIntoClosure notOlder(_span, verification_mark_bm());
2428   gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2429 
2430   {
2431     StrongRootsScope srs(1);
2432 
2433     gch->gen_process_roots(&srs,
2434                            Generation::Old,
2435                            true,   // younger gens are roots
2436                            GenCollectedHeap::ScanningOption(roots_scanning_options()),
2437                            should_unload_classes(),
2438                            &notOlder,
2439                            NULL,
2440                            NULL);
2441   }
2442 
2443   // Now mark from the roots
2444   MarkFromRootsClosure markFromRootsClosure(this, _span,
2445     verification_mark_bm(), verification_mark_stack(),
2446     false /* don't yield */, true /* verifying */);
2447   assert(_restart_addr == NULL, "Expected pre-condition");
2448   verification_mark_bm()->iterate(&markFromRootsClosure);
2449   while (_restart_addr != NULL) {
2450     // Deal with stack overflow: by restarting at the indicated
2451     // address.
2452     HeapWord* ra = _restart_addr;
2453     markFromRootsClosure.reset(ra);
2454     _restart_addr = NULL;


2486 
2487 void CMSCollector::verify_after_remark_work_2() {
2488   ResourceMark rm;
2489   HandleMark  hm;
2490   GenCollectedHeap* gch = GenCollectedHeap::heap();
2491 
2492   // Get a clear set of claim bits for the roots processing to work with.
2493   ClassLoaderDataGraph::clear_claimed_marks();
2494 
2495   // Mark from roots one level into CMS
2496   MarkRefsIntoVerifyClosure notOlder(_span, verification_mark_bm(),
2497                                      markBitMap());
2498   CLDToOopClosure cld_closure(&notOlder, true);
2499 
2500   gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2501 
2502   {
2503     StrongRootsScope srs(1);
2504 
2505     gch->gen_process_roots(&srs,
2506                            Generation::Old,
2507                            true,   // younger gens are roots
2508                            GenCollectedHeap::ScanningOption(roots_scanning_options()),
2509                            should_unload_classes(),
2510                            &notOlder,
2511                            NULL,
2512                            &cld_closure);
2513   }
2514 
2515   // Now mark from the roots
2516   MarkFromRootsVerifyClosure markFromRootsClosure(this, _span,
2517     verification_mark_bm(), markBitMap(), verification_mark_stack());
2518   assert(_restart_addr == NULL, "Expected pre-condition");
2519   verification_mark_bm()->iterate(&markFromRootsClosure);
2520   while (_restart_addr != NULL) {
2521     // Deal with stack overflow: by restarting at the indicated
2522     // address.
2523     HeapWord* ra = _restart_addr;
2524     markFromRootsClosure.reset(ra);
2525     _restart_addr = NULL;
2526     verification_mark_bm()->iterate(&markFromRootsClosure, ra, _span.end());


3013       assert(workers != NULL, "Need parallel worker threads.");
3014       uint n_workers = workers->active_workers();
3015 
3016       StrongRootsScope srs(n_workers);
3017 
3018       CMSParInitialMarkTask tsk(this, &srs, n_workers);
3019       initialize_sequential_subtasks_for_young_gen_rescan(n_workers);
3020       if (n_workers > 1) {
3021         workers->run_task(&tsk);
3022       } else {
3023         tsk.work(0);
3024       }
3025     } else {
3026       // The serial version.
3027       CLDToOopClosure cld_closure(&notOlder, true);
3028       gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
3029 
3030       StrongRootsScope srs(1);
3031 
3032       gch->gen_process_roots(&srs,
3033                              Generation::Old,
3034                              true,   // younger gens are roots
3035                              GenCollectedHeap::ScanningOption(roots_scanning_options()),
3036                              should_unload_classes(),
3037                              &notOlder,
3038                              NULL,
3039                              &cld_closure);
3040     }
3041   }
3042 
3043   // Clear mod-union table; it will be dirtied in the prologue of
3044   // CMS generation per each younger generation collection.
3045 
3046   assert(_modUnionTable.isAllClear(),
3047        "Was cleared in most recent final checkpoint phase"
3048        " or no bits are set in the gc_prologue before the start of the next "
3049        "subsequent marking phase.");
3050 
3051   assert(_ct->klass_rem_set()->mod_union_is_clear(), "Must be");
3052 
3053   // Save the end of the used_region of the constituent generations


4264   assert(SafepointSynchronize::is_at_safepoint(),
4265          "world should be stopped");
4266   TraceCMSMemoryManagerStats tms(_collectorState,GenCollectedHeap::heap()->gc_cause());
4267 
4268   verify_work_stacks_empty();
4269   verify_overflow_empty();
4270 
4271   if (PrintGCDetails) {
4272     gclog_or_tty->print("[YG occupancy: "SIZE_FORMAT" K ("SIZE_FORMAT" K)]",
4273                         _young_gen->used() / K,
4274                         _young_gen->capacity() / K);
4275   }
4276   {
4277     if (CMSScavengeBeforeRemark) {
4278       GenCollectedHeap* gch = GenCollectedHeap::heap();
4279       // Temporarily set flag to false, GCH->do_collection will
4280       // expect it to be false and set to true
4281       FlagSetting fl(gch->_is_gc_active, false);
4282       NOT_PRODUCT(GCTraceTime t("Scavenge-Before-Remark",
4283         PrintGCDetails && Verbose, true, _gc_timer_cm, _gc_tracer_cm->gc_id());)
4284       gch->do_collection(true,             // full (i.e. force, see below)
4285                          false,            // !clear_all_soft_refs
4286                          0,                // size
4287                          false,            // is_tlab
4288                          Generation::Young // type
4289         );



4290     }
4291     FreelistLocker x(this);
4292     MutexLockerEx y(bitMapLock(),
4293                     Mutex::_no_safepoint_check_flag);
4294     checkpointRootsFinalWork();
4295   }
4296   verify_work_stacks_empty();
4297   verify_overflow_empty();
4298 }
4299 
4300 void CMSCollector::checkpointRootsFinalWork() {
4301   NOT_PRODUCT(GCTraceTime tr("checkpointRootsFinalWork", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());)
4302 
4303   assert(haveFreelistLocks(), "must have free list locks");
4304   assert_lock_strong(bitMapLock());
4305 
4306   ResourceMark rm;
4307   HandleMark   hm;
4308 
4309   GenCollectedHeap* gch = GenCollectedHeap::heap();


4443   Par_MarkRefsIntoClosure par_mri_cl(_collector->_span, &(_collector->_markBitMap));
4444 
4445   // ---------- young gen roots --------------
4446   {
4447     work_on_young_gen_roots(worker_id, &par_mri_cl);
4448     _timer.stop();
4449     if (PrintCMSStatistics != 0) {
4450       gclog_or_tty->print_cr(
4451         "Finished young gen initial mark scan work in %dth thread: %3.3f sec",
4452         worker_id, _timer.seconds());
4453     }
4454   }
4455 
4456   // ---------- remaining roots --------------
4457   _timer.reset();
4458   _timer.start();
4459 
4460   CLDToOopClosure cld_closure(&par_mri_cl, true);
4461 
4462   gch->gen_process_roots(_strong_roots_scope,
4463                          Generation::Old,
4464                          false,     // yg was scanned above
4465                          GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),
4466                          _collector->should_unload_classes(),
4467                          &par_mri_cl,
4468                          NULL,
4469                          &cld_closure);
4470   assert(_collector->should_unload_classes()
4471          || (_collector->CMSCollector::roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),
4472          "if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");
4473   _timer.stop();
4474   if (PrintCMSStatistics != 0) {
4475     gclog_or_tty->print_cr(
4476       "Finished remaining root initial mark scan work in %dth thread: %3.3f sec",
4477       worker_id, _timer.seconds());
4478   }
4479 }
4480 
4481 // Parallel remark task
4482 class CMSParRemarkTask: public CMSParMarkTask {
4483   CompactibleFreeListSpace* _cms_space;


4582 
4583   // Rescan young gen roots first since these are likely
4584   // coarsely partitioned and may, on that account, constitute
4585   // the critical path; thus, it's best to start off that
4586   // work first.
4587   // ---------- young gen roots --------------
4588   {
4589     work_on_young_gen_roots(worker_id, &par_mrias_cl);
4590     _timer.stop();
4591     if (PrintCMSStatistics != 0) {
4592       gclog_or_tty->print_cr(
4593         "Finished young gen rescan work in %dth thread: %3.3f sec",
4594         worker_id, _timer.seconds());
4595     }
4596   }
4597 
4598   // ---------- remaining roots --------------
4599   _timer.reset();
4600   _timer.start();
4601   gch->gen_process_roots(_strong_roots_scope,
4602                          Generation::Old,
4603                          false,     // yg was scanned above
4604                          GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),
4605                          _collector->should_unload_classes(),
4606                          &par_mrias_cl,
4607                          NULL,
4608                          NULL);     // The dirty klasses will be handled below
4609 
4610   assert(_collector->should_unload_classes()
4611          || (_collector->CMSCollector::roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),
4612          "if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");
4613   _timer.stop();
4614   if (PrintCMSStatistics != 0) {
4615     gclog_or_tty->print_cr(
4616       "Finished remaining root rescan work in %dth thread: %3.3f sec",
4617       worker_id, _timer.seconds());
4618   }
4619 
4620   // ---------- unhandled CLD scanning ----------
4621   if (worker_id == 0) { // Single threaded at the moment.
4622     _timer.reset();


5163       if (PrintCMSStatistics != 0) {
5164         gclog_or_tty->print(" (re-scanned "SIZE_FORMAT" dirty cards in cms gen) ",
5165           markFromDirtyCardsClosure.num_dirty_cards());
5166       }
5167     }
5168   }
5169   if (VerifyDuringGC &&
5170       GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
5171     HandleMark hm;  // Discard invalid handles created during verification
5172     Universe::verify();
5173   }
5174   {
5175     GCTraceTime t("root rescan", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());
5176 
5177     verify_work_stacks_empty();
5178 
5179     gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
5180     StrongRootsScope srs(1);
5181 
5182     gch->gen_process_roots(&srs,
5183                            Generation::Old,
5184                            true,  // younger gens as roots
5185                            GenCollectedHeap::ScanningOption(roots_scanning_options()),
5186                            should_unload_classes(),
5187                            &mrias_cl,
5188                            NULL,
5189                            NULL); // The dirty klasses will be handled below
5190 
5191     assert(should_unload_classes()
5192            || (roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),
5193            "if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");
5194   }
5195 
5196   {
5197     GCTraceTime t("visit unhandled CLDs", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());
5198 
5199     verify_work_stacks_empty();
5200 
5201     // Scan all class loader data objects that might have been introduced
5202     // during concurrent marking.
5203     ResourceMark rm;


5627   size_t nearLargestOffset =
5628     (size_t)((double)largestOffset * nearLargestPercent) - MinChunkSize;
5629   if (PrintFLSStatistics != 0) {
5630     gclog_or_tty->print_cr(
5631       "CMS: Large Block: " PTR_FORMAT ";"
5632       " Proximity: " PTR_FORMAT " -> " PTR_FORMAT,
5633       p2i(largestAddr),
5634       p2i(_cmsSpace->nearLargestChunk()), p2i(minAddr + nearLargestOffset));
5635   }
5636   _cmsSpace->set_nearLargestChunk(minAddr + nearLargestOffset);
5637 }
5638 
5639 bool ConcurrentMarkSweepGeneration::isNearLargestChunk(HeapWord* addr) {
5640   return addr >= _cmsSpace->nearLargestChunk();
5641 }
5642 
5643 FreeChunk* ConcurrentMarkSweepGeneration::find_chunk_at_end() {
5644   return _cmsSpace->find_chunk_at_end();
5645 }
5646 
5647 void ConcurrentMarkSweepGeneration::update_gc_stats(Generation* current_generation,
5648                                                     bool full) {
5649   // If the young generation has been collected, gather any statistics
5650   // that are of interest at this point.
5651   bool current_is_young = GenCollectedHeap::heap()->is_young_gen(current_generation);
5652   if (!full && current_is_young) {
5653     // Gather statistics on the young generation collection.
5654     collector()->stats().record_gc0_end(used());
5655   }
5656 }
5657 
5658 void CMSCollector::sweepWork(ConcurrentMarkSweepGeneration* gen) {
5659   // We iterate over the space(s) underlying this generation,
5660   // checking the mark bit map to see if the bits corresponding
5661   // to specific blocks are marked or not. Blocks that are
5662   // marked are live and are not swept up. All remaining blocks
5663   // are swept up, with coalescing on-the-fly as we sweep up
5664   // contiguous free and/or garbage blocks:
5665   // We need to ensure that the sweeper synchronizes with allocators
5666   // and stop-the-world collectors. In particular, the following
5667   // locks are used:
5668   // . CMS token: if this is held, a stop the world collection cannot occur
5669   // . freelistLock: if this is held no allocation can occur from this
5670   //                 generation by another thread
5671   // . bitMapLock: if this is held, no other thread can access or update
5672   //


< prev index next >