src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.cpp

Print this page




1943     size_policy()->msc_collection_begin();
1944   }
1945 
1946   // Temporarily widen the span of the weak reference processing to
1947   // the entire heap.
1948   MemRegion new_span(GenCollectedHeap::heap()->reserved_region());
1949   ReferenceProcessorSpanMutator x(ref_processor(), new_span);
1950 
1951   // Temporarily, clear the "is_alive_non_header" field of the
1952   // reference processor.
1953   ReferenceProcessorIsAliveMutator y(ref_processor(), NULL);
1954 
1955   // Temporarily make reference _processing_ single threaded (non-MT).
1956   ReferenceProcessorMTProcMutator z(ref_processor(), false);
1957 
1958   // Temporarily make refs discovery atomic
1959   ReferenceProcessorAtomicMutator w(ref_processor(), true);
1960 
1961   ref_processor()->set_enqueuing_is_done(false);
1962   ref_processor()->enable_discovery();

1963   // If an asynchronous collection finishes, the _modUnionTable is
1964   // all clear.  If we are assuming the collection from an asynchronous
1965   // collection, clear the _modUnionTable.
1966   assert(_collectorState != Idling || _modUnionTable.isAllClear(),
1967     "_modUnionTable should be clear if the baton was not passed");
1968   _modUnionTable.clear_all();
1969 
1970   // We must adjust the allocation statistics being maintained
1971   // in the free list space. We do so by reading and clearing
1972   // the sweep timer and updating the block flux rate estimates below.
1973   assert(_sweep_timer.is_active(), "We should never see the timer inactive");
1974   _sweep_timer.stop();
1975   // Note that we do not use this sample to update the _sweep_estimate.
1976   _cmsGen->cmsSpace()->beginSweepFLCensus((float)(_sweep_timer.seconds()),
1977                                           _sweep_estimate.padded_average());
1978 
1979   GenMarkSweep::invoke_at_safepoint(_cmsGen->level(),
1980     ref_processor(), clear_all_soft_refs);
1981   #ifdef ASSERT
1982     CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();


2366          "Foreground collector should be waiting, not executing");
2367   assert(Thread::current()->is_VM_thread(), "A foreground collection"
2368     "may only be done by the VM Thread with the world stopped");
2369   assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),
2370          "VM thread should have CMS token");
2371 
2372   NOT_PRODUCT(TraceTime t("CMS:MS (foreground) ", PrintGCDetails && Verbose,
2373     true, gclog_or_tty);)
2374   if (UseAdaptiveSizePolicy) {
2375     size_policy()->ms_collection_begin();
2376   }
2377   COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact);
2378 
2379   HandleMark hm;  // Discard invalid handles created during verification
2380 
2381   if (VerifyBeforeGC &&
2382       GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
2383     Universe::verify(true);
2384   }
2385 



2386   bool init_mark_was_synchronous = false; // until proven otherwise
2387   while (_collectorState != Idling) {
2388     if (TraceCMSState) {
2389       gclog_or_tty->print_cr("Thread " INTPTR_FORMAT " in CMS state %d",
2390         Thread::current(), _collectorState);
2391     }
2392     switch (_collectorState) {
2393       case InitialMarking:
2394         init_mark_was_synchronous = true;  // fact to be exploited in re-mark
2395         checkpointRootsInitial(false);
2396         assert(_collectorState == Marking, "Collector state should have changed"
2397           " within checkpointRootsInitial()");
2398         break;
2399       case Marking:
2400         // initial marking in checkpointRootsInitialWork has been completed
2401         if (VerifyDuringGC &&
2402             GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
2403           gclog_or_tty->print("Verify before initial mark: ");
2404           Universe::verify(true);
2405         }


4573       // simultaneously clearing it.
4574       dirtyRegion =
4575         _modUnionTable.getAndClearMarkedRegion(nextAddr, endAddr);
4576       assert(dirtyRegion.start() >= nextAddr,
4577              "returned region inconsistent?");
4578     }
4579     // Remember where the next search should begin.
4580     // The returned region (if non-empty) is a right open interval,
4581     // so lastOffset is obtained from the right end of that
4582     // interval.
4583     lastAddr = dirtyRegion.end();
4584     // Should do something more transparent and less hacky XXX
4585     numDirtyCards =
4586       _modUnionTable.heapWordDiffToOffsetDiff(dirtyRegion.word_size());
4587 
4588     // We'll scan the cards in the dirty region (with periodic
4589     // yields for foreground GC as needed).
4590     if (!dirtyRegion.is_empty()) {
4591       assert(numDirtyCards > 0, "consistency check");
4592       HeapWord* stop_point = NULL;
4593       {
4594         stopTimer();
4595         CMSTokenSyncWithLocks ts(true, gen->freelistLock(),
4596                                  bitMapLock());
4597         startTimer();

4598         verify_work_stacks_empty();
4599         verify_overflow_empty();
4600         sample_eden();
4601         stop_point =
4602           gen->cmsSpace()->object_iterate_careful_m(dirtyRegion, cl);
4603       }
4604       if (stop_point != NULL) {
4605         // The careful iteration stopped early either because it found an
4606         // uninitialized object, or because we were in the midst of an
4607         // "abortable preclean", which should now be aborted. Redirty
4608         // the bits corresponding to the partially-scanned or unscanned
4609         // cards. We'll either restart at the next block boundary or
4610         // abort the preclean.
4611         assert((CMSPermGenPrecleaningEnabled && (gen == _permGen)) ||
4612                (_collectorState == AbortablePreclean && should_abort_preclean()),
4613                "Unparsable objects should only be in perm gen.");
4614 
4615         stopTimer();
4616         CMSTokenSyncWithLocks ts(true, bitMapLock());
4617         startTimer();
4618         _modUnionTable.mark_range(MemRegion(stop_point, dirtyRegion.end()));
4619         if (should_abort_preclean()) {
4620           break; // out of preclean loop
4621         } else {
4622           // Compute the next address at which preclean should pick up;
4623           // might need bitMapLock in order to read P-bits.
4624           lastAddr = next_card_start_after_block(stop_point);
4625         }
4626       }
4627     } else {
4628       assert(lastAddr == endAddr, "consistency check");
4629       assert(numDirtyCards == 0, "consistency check");
4630       break;
4631     }
4632   }
4633   verify_work_stacks_empty();
4634   verify_overflow_empty();
4635   return cumNumDirtyCards;
4636 }
4637 


5658                               _collector.ref_processor()->span(),
5659                               _collector.markBitMap(),
5660                               n_workers, _collector.task_queues());
5661   workers->run_task(&rp_task);
5662 }
5663 
5664 void CMSRefProcTaskExecutor::execute(EnqueueTask& task)
5665 {
5666 
5667   GenCollectedHeap* gch = GenCollectedHeap::heap();
5668   WorkGang* workers = gch->workers();
5669   assert(workers != NULL, "Need parallel worker threads.");
5670   CMSRefEnqueueTaskProxy enq_task(task);
5671   workers->run_task(&enq_task);
5672 }
5673 
5674 void CMSCollector::refProcessingWork(bool asynch, bool clear_all_soft_refs) {
5675 
5676   ResourceMark rm;
5677   HandleMark   hm;
5678   ReferencePolicy* soft_ref_policy;
5679 
5680   assert(!ref_processor()->enqueuing_is_done(), "Enqueuing should not be complete");


5681   // Process weak references.
5682   if (clear_all_soft_refs) {
5683     soft_ref_policy = new AlwaysClearPolicy();
5684   } else {
5685 #ifdef COMPILER2
5686     soft_ref_policy = new LRUMaxHeapPolicy();
5687 #else
5688     soft_ref_policy = new LRUCurrentHeapPolicy();
5689 #endif // COMPILER2
5690   }
5691   verify_work_stacks_empty();
5692 
5693   ReferenceProcessor* rp = ref_processor();
5694   assert(rp->span().equals(_span), "Spans should be equal");
5695   CMSKeepAliveClosure cmsKeepAliveClosure(this, _span, &_markBitMap,
5696                                           &_markStack);
5697   CMSDrainMarkingStackClosure cmsDrainMarkingStackClosure(this,
5698                                 _span, &_markBitMap, &_markStack,
5699                                 &cmsKeepAliveClosure);
5700   {
5701     TraceTime t("weak refs processing", PrintGCDetails, false, gclog_or_tty);
5702     if (rp->processing_is_mt()) {
5703       CMSRefProcTaskExecutor task_executor(*this);
5704       rp->process_discovered_references(soft_ref_policy,
5705                                         &_is_alive_closure,
5706                                         &cmsKeepAliveClosure,
5707                                         &cmsDrainMarkingStackClosure,
5708                                         &task_executor);
5709     } else {
5710       rp->process_discovered_references(soft_ref_policy,
5711                                         &_is_alive_closure,
5712                                         &cmsKeepAliveClosure,
5713                                         &cmsDrainMarkingStackClosure,
5714                                         NULL);
5715     }
5716     verify_work_stacks_empty();
5717   }
5718 
5719   if (should_unload_classes()) {
5720     {
5721       TraceTime t("class unloading", PrintGCDetails, false, gclog_or_tty);
5722 
5723       // Follow SystemDictionary roots and unload classes
5724       bool purged_class = SystemDictionary::do_unloading(&_is_alive_closure);
5725 
5726       // Follow CodeCache roots and unload any methods marked for unloading
5727       CodeCache::do_unloading(&_is_alive_closure,
5728                               &cmsKeepAliveClosure,
5729                               purged_class);
5730 
5731       cmsDrainMarkingStackClosure.do_void();




1943     size_policy()->msc_collection_begin();
1944   }
1945 
1946   // Temporarily widen the span of the weak reference processing to
1947   // the entire heap.
1948   MemRegion new_span(GenCollectedHeap::heap()->reserved_region());
1949   ReferenceProcessorSpanMutator x(ref_processor(), new_span);
1950 
1951   // Temporarily, clear the "is_alive_non_header" field of the
1952   // reference processor.
1953   ReferenceProcessorIsAliveMutator y(ref_processor(), NULL);
1954 
1955   // Temporarily make reference _processing_ single threaded (non-MT).
1956   ReferenceProcessorMTProcMutator z(ref_processor(), false);
1957 
1958   // Temporarily make refs discovery atomic
1959   ReferenceProcessorAtomicMutator w(ref_processor(), true);
1960 
1961   ref_processor()->set_enqueuing_is_done(false);
1962   ref_processor()->enable_discovery();
1963   ref_processor()->snap_policy(clear_all_soft_refs);
1964   // If an asynchronous collection finishes, the _modUnionTable is
1965   // all clear.  If we are assuming the collection from an asynchronous
1966   // collection, clear the _modUnionTable.
1967   assert(_collectorState != Idling || _modUnionTable.isAllClear(),
1968     "_modUnionTable should be clear if the baton was not passed");
1969   _modUnionTable.clear_all();
1970 
1971   // We must adjust the allocation statistics being maintained
1972   // in the free list space. We do so by reading and clearing
1973   // the sweep timer and updating the block flux rate estimates below.
1974   assert(_sweep_timer.is_active(), "We should never see the timer inactive");
1975   _sweep_timer.stop();
1976   // Note that we do not use this sample to update the _sweep_estimate.
1977   _cmsGen->cmsSpace()->beginSweepFLCensus((float)(_sweep_timer.seconds()),
1978                                           _sweep_estimate.padded_average());
1979 
1980   GenMarkSweep::invoke_at_safepoint(_cmsGen->level(),
1981     ref_processor(), clear_all_soft_refs);
1982   #ifdef ASSERT
1983     CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();


2367          "Foreground collector should be waiting, not executing");
2368   assert(Thread::current()->is_VM_thread(), "A foreground collection"
2369     "may only be done by the VM Thread with the world stopped");
2370   assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),
2371          "VM thread should have CMS token");
2372 
2373   NOT_PRODUCT(TraceTime t("CMS:MS (foreground) ", PrintGCDetails && Verbose,
2374     true, gclog_or_tty);)
2375   if (UseAdaptiveSizePolicy) {
2376     size_policy()->ms_collection_begin();
2377   }
2378   COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact);
2379 
2380   HandleMark hm;  // Discard invalid handles created during verification
2381 
2382   if (VerifyBeforeGC &&
2383       GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
2384     Universe::verify(true);
2385   }
2386 
2387   // Snapshot the soft reference policy to be used in this collection cycle.
2388   ref_processor()->snap_policy(clear_all_soft_refs);
2389 
2390   bool init_mark_was_synchronous = false; // until proven otherwise
2391   while (_collectorState != Idling) {
2392     if (TraceCMSState) {
2393       gclog_or_tty->print_cr("Thread " INTPTR_FORMAT " in CMS state %d",
2394         Thread::current(), _collectorState);
2395     }
2396     switch (_collectorState) {
2397       case InitialMarking:
2398         init_mark_was_synchronous = true;  // fact to be exploited in re-mark
2399         checkpointRootsInitial(false);
2400         assert(_collectorState == Marking, "Collector state should have changed"
2401           " within checkpointRootsInitial()");
2402         break;
2403       case Marking:
2404         // initial marking in checkpointRootsInitialWork has been completed
2405         if (VerifyDuringGC &&
2406             GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
2407           gclog_or_tty->print("Verify before initial mark: ");
2408           Universe::verify(true);
2409         }


4577       // simultaneously clearing it.
4578       dirtyRegion =
4579         _modUnionTable.getAndClearMarkedRegion(nextAddr, endAddr);
4580       assert(dirtyRegion.start() >= nextAddr,
4581              "returned region inconsistent?");
4582     }
4583     // Remember where the next search should begin.
4584     // The returned region (if non-empty) is a right open interval,
4585     // so lastOffset is obtained from the right end of that
4586     // interval.
4587     lastAddr = dirtyRegion.end();
4588     // Should do something more transparent and less hacky XXX
4589     numDirtyCards =
4590       _modUnionTable.heapWordDiffToOffsetDiff(dirtyRegion.word_size());
4591 
4592     // We'll scan the cards in the dirty region (with periodic
4593     // yields for foreground GC as needed).
4594     if (!dirtyRegion.is_empty()) {
4595       assert(numDirtyCards > 0, "consistency check");
4596       HeapWord* stop_point = NULL;

4597       stopTimer();
4598       CMSTokenSyncWithLocks ts(true, gen->freelistLock(),
4599                                bitMapLock());
4600       startTimer();
4601       {
4602         verify_work_stacks_empty();
4603         verify_overflow_empty();
4604         sample_eden();
4605         stop_point =
4606           gen->cmsSpace()->object_iterate_careful_m(dirtyRegion, cl);
4607       }
4608       if (stop_point != NULL) {
4609         // The careful iteration stopped early either because it found an
4610         // uninitialized object, or because we were in the midst of an
4611         // "abortable preclean", which should now be aborted. Redirty
4612         // the bits corresponding to the partially-scanned or unscanned
4613         // cards. We'll either restart at the next block boundary or
4614         // abort the preclean.
4615         assert((CMSPermGenPrecleaningEnabled && (gen == _permGen)) ||
4616                (_collectorState == AbortablePreclean && should_abort_preclean()),
4617                "Unparsable objects should only be in perm gen.");




4618         _modUnionTable.mark_range(MemRegion(stop_point, dirtyRegion.end()));
4619         if (should_abort_preclean()) {
4620           break; // out of preclean loop
4621         } else {
4622           // Compute the next address at which preclean should pick up;
4623           // might need bitMapLock in order to read P-bits.
4624           lastAddr = next_card_start_after_block(stop_point);
4625         }
4626       }
4627     } else {
4628       assert(lastAddr == endAddr, "consistency check");
4629       assert(numDirtyCards == 0, "consistency check");
4630       break;
4631     }
4632   }
4633   verify_work_stacks_empty();
4634   verify_overflow_empty();
4635   return cumNumDirtyCards;
4636 }
4637 


5658                               _collector.ref_processor()->span(),
5659                               _collector.markBitMap(),
5660                               n_workers, _collector.task_queues());
5661   workers->run_task(&rp_task);
5662 }
5663 
5664 void CMSRefProcTaskExecutor::execute(EnqueueTask& task)
5665 {
5666 
5667   GenCollectedHeap* gch = GenCollectedHeap::heap();
5668   WorkGang* workers = gch->workers();
5669   assert(workers != NULL, "Need parallel worker threads.");
5670   CMSRefEnqueueTaskProxy enq_task(task);
5671   workers->run_task(&enq_task);
5672 }
5673 
5674 void CMSCollector::refProcessingWork(bool asynch, bool clear_all_soft_refs) {
5675 
5676   ResourceMark rm;
5677   HandleMark   hm;

5678 
5679   ReferenceProcessor* rp = ref_processor();
5680   assert(rp->span().equals(_span), "Spans should be equal");
5681   assert(!rp->enqueuing_is_done(), "Enqueuing should not be complete");
5682   // Process weak references.
5683   rp->snap_policy(clear_all_soft_refs);








5684   verify_work_stacks_empty();
5685 


5686   CMSKeepAliveClosure cmsKeepAliveClosure(this, _span, &_markBitMap,
5687                                           &_markStack);
5688   CMSDrainMarkingStackClosure cmsDrainMarkingStackClosure(this,
5689                                 _span, &_markBitMap, &_markStack,
5690                                 &cmsKeepAliveClosure);
5691   {
5692     TraceTime t("weak refs processing", PrintGCDetails, false, gclog_or_tty);
5693     if (rp->processing_is_mt()) {
5694       CMSRefProcTaskExecutor task_executor(*this);
5695       rp->process_discovered_references(&_is_alive_closure,

5696                                         &cmsKeepAliveClosure,
5697                                         &cmsDrainMarkingStackClosure,
5698                                         &task_executor);
5699     } else {
5700       rp->process_discovered_references(&_is_alive_closure,

5701                                         &cmsKeepAliveClosure,
5702                                         &cmsDrainMarkingStackClosure,
5703                                         NULL);
5704     }
5705     verify_work_stacks_empty();
5706   }
5707 
5708   if (should_unload_classes()) {
5709     {
5710       TraceTime t("class unloading", PrintGCDetails, false, gclog_or_tty);
5711 
5712       // Follow SystemDictionary roots and unload classes
5713       bool purged_class = SystemDictionary::do_unloading(&_is_alive_closure);
5714 
5715       // Follow CodeCache roots and unload any methods marked for unloading
5716       CodeCache::do_unloading(&_is_alive_closure,
5717                               &cmsKeepAliveClosure,
5718                               purged_class);
5719 
5720       cmsDrainMarkingStackClosure.do_void();