hotspot/src/share/vm/gc_implementation/g1/concurrentMark.cpp
changeset 25492 d27050bdfb04
parent 25491 70fb742e40aa
child 25728 7f8a76e3baa8
equal deleted inserted replaced
25491:70fb742e40aa 25492:d27050bdfb04
    22  *
    22  *
    23  */
    23  */
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "classfile/symbolTable.hpp"
    26 #include "classfile/symbolTable.hpp"
       
    27 #include "code/codeCache.hpp"
    27 #include "gc_implementation/g1/concurrentMark.inline.hpp"
    28 #include "gc_implementation/g1/concurrentMark.inline.hpp"
    28 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
    29 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
    29 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
    30 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
    30 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
    31 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
    31 #include "gc_implementation/g1/g1ErgoVerbose.hpp"
    32 #include "gc_implementation/g1/g1ErgoVerbose.hpp"
    37 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
    38 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
    38 #include "gc_implementation/shared/vmGCOperations.hpp"
    39 #include "gc_implementation/shared/vmGCOperations.hpp"
    39 #include "gc_implementation/shared/gcTimer.hpp"
    40 #include "gc_implementation/shared/gcTimer.hpp"
    40 #include "gc_implementation/shared/gcTrace.hpp"
    41 #include "gc_implementation/shared/gcTrace.hpp"
    41 #include "gc_implementation/shared/gcTraceTime.hpp"
    42 #include "gc_implementation/shared/gcTraceTime.hpp"
       
    43 #include "memory/allocation.hpp"
    42 #include "memory/genOopClosures.inline.hpp"
    44 #include "memory/genOopClosures.inline.hpp"
    43 #include "memory/referencePolicy.hpp"
    45 #include "memory/referencePolicy.hpp"
    44 #include "memory/resourceArea.hpp"
    46 #include "memory/resourceArea.hpp"
    45 #include "oops/oop.inline.hpp"
    47 #include "oops/oop.inline.hpp"
    46 #include "runtime/handles.inline.hpp"
    48 #include "runtime/handles.inline.hpp"
    56   _shifter(shifter) {
    58   _shifter(shifter) {
    57   _bmStartWord = 0;
    59   _bmStartWord = 0;
    58   _bmWordSize = 0;
    60   _bmWordSize = 0;
    59 }
    61 }
    60 
    62 
    61 HeapWord* CMBitMapRO::getNextMarkedWordAddress(HeapWord* addr,
    63 HeapWord* CMBitMapRO::getNextMarkedWordAddress(const HeapWord* addr,
    62                                                HeapWord* limit) const {
    64                                                const HeapWord* limit) const {
    63   // First we must round addr *up* to a possible object boundary.
    65   // First we must round addr *up* to a possible object boundary.
    64   addr = (HeapWord*)align_size_up((intptr_t)addr,
    66   addr = (HeapWord*)align_size_up((intptr_t)addr,
    65                                   HeapWordSize << _shifter);
    67                                   HeapWordSize << _shifter);
    66   size_t addrOffset = heapWordToOffset(addr);
    68   size_t addrOffset = heapWordToOffset(addr);
    67   if (limit == NULL) {
    69   if (limit == NULL) {
    74   assert(nextAddr == limit || isMarked(nextAddr),
    76   assert(nextAddr == limit || isMarked(nextAddr),
    75          "get_next_one postcondition");
    77          "get_next_one postcondition");
    76   return nextAddr;
    78   return nextAddr;
    77 }
    79 }
    78 
    80 
    79 HeapWord* CMBitMapRO::getNextUnmarkedWordAddress(HeapWord* addr,
    81 HeapWord* CMBitMapRO::getNextUnmarkedWordAddress(const HeapWord* addr,
    80                                                  HeapWord* limit) const {
    82                                                  const HeapWord* limit) const {
    81   size_t addrOffset = heapWordToOffset(addr);
    83   size_t addrOffset = heapWordToOffset(addr);
    82   if (limit == NULL) {
    84   if (limit == NULL) {
    83     limit = _bmStartWord + _bmWordSize;
    85     limit = _bmStartWord + _bmWordSize;
    84   }
    86   }
    85   size_t limitOffset = heapWordToOffset(limit);
    87   size_t limitOffset = heapWordToOffset(limit);
  1221     }
  1223     }
  1222   }
  1224   }
  1223 };
  1225 };
  1224 
  1226 
  1225 void ConcurrentMark::scanRootRegions() {
  1227 void ConcurrentMark::scanRootRegions() {
       
  1228   // Start of concurrent marking.
       
  1229   ClassLoaderDataGraph::clear_claimed_marks();
       
  1230 
  1226   // scan_in_progress() will have been set to true only if there was
  1231   // scan_in_progress() will have been set to true only if there was
  1227   // at least one root region to scan. So, if it's false, we
  1232   // at least one root region to scan. So, if it's false, we
  1228   // should not attempt to do any further work.
  1233   // should not attempt to do any further work.
  1229   if (root_regions()->scan_in_progress()) {
  1234   if (root_regions()->scan_in_progress()) {
  1230     _parallel_marking_threads = calc_parallel_marking_threads();
  1235     _parallel_marking_threads = calc_parallel_marking_threads();
  1269   set_concurrency_and_phase(active_workers, true /* concurrent */);
  1274   set_concurrency_and_phase(active_workers, true /* concurrent */);
  1270 
  1275 
  1271   CMConcurrentMarkingTask markingTask(this, cmThread());
  1276   CMConcurrentMarkingTask markingTask(this, cmThread());
  1272   if (use_parallel_marking_threads()) {
  1277   if (use_parallel_marking_threads()) {
  1273     _parallel_workers->set_active_workers((int)active_workers);
  1278     _parallel_workers->set_active_workers((int)active_workers);
  1274     // Don't set _n_par_threads because it affects MT in process_strong_roots()
  1279     // Don't set _n_par_threads because it affects MT in process_roots()
  1275     // and the decisions on that MT processing is made elsewhere.
  1280     // and the decisions on that MT processing is made elsewhere.
  1276     assert(_parallel_workers->active_workers() > 0, "Should have been set");
  1281     assert(_parallel_workers->active_workers() > 0, "Should have been set");
  1277     _parallel_workers->run_task(&markingTask);
  1282     _parallel_workers->run_task(&markingTask);
  1278   } else {
  1283   } else {
  1279     markingTask.work(0);
  1284     markingTask.work(0);
  2140 
  2145 
  2141   // Clean up will have freed any regions completely full of garbage.
  2146   // Clean up will have freed any regions completely full of garbage.
  2142   // Update the soft reference policy with the new heap occupancy.
  2147   // Update the soft reference policy with the new heap occupancy.
  2143   Universe::update_heap_info_at_gc();
  2148   Universe::update_heap_info_at_gc();
  2144 
  2149 
  2145   // We need to make this be a "collection" so any collection pause that
       
  2146   // races with it goes around and waits for completeCleanup to finish.
       
  2147   g1h->increment_total_collections();
       
  2148 
       
  2149   // We reclaimed old regions so we should calculate the sizes to make
       
  2150   // sure we update the old gen/space data.
       
  2151   g1h->g1mm()->update_sizes();
       
  2152 
       
  2153   if (VerifyDuringGC) {
  2150   if (VerifyDuringGC) {
  2154     HandleMark hm;  // handle scope
  2151     HandleMark hm;  // handle scope
  2155     Universe::heap()->prepare_for_verify();
  2152     Universe::heap()->prepare_for_verify();
  2156     Universe::verify(VerifyOption_G1UsePrevMarking,
  2153     Universe::verify(VerifyOption_G1UsePrevMarking,
  2157                      " VerifyDuringGC:(after)");
  2154                      " VerifyDuringGC:(after)");
  2158   }
  2155   }
       
  2156 
  2159   g1h->check_bitmaps("Cleanup End");
  2157   g1h->check_bitmaps("Cleanup End");
  2160 
  2158 
  2161   g1h->verify_region_sets_optional();
  2159   g1h->verify_region_sets_optional();
       
  2160 
       
  2161   // We need to make this be a "collection" so any collection pause that
       
  2162   // races with it goes around and waits for completeCleanup to finish.
       
  2163   g1h->increment_total_collections();
       
  2164 
       
  2165   // Clean out dead classes and update Metaspace sizes.
       
  2166   ClassLoaderDataGraph::purge();
       
  2167   MetaspaceGC::compute_new_size();
       
  2168 
       
  2169   // We reclaimed old regions so we should calculate the sizes to make
       
  2170   // sure we update the old gen/space data.
       
  2171   g1h->g1mm()->update_sizes();
       
  2172 
  2162   g1h->trace_heap_after_concurrent_cycle();
  2173   g1h->trace_heap_after_concurrent_cycle();
  2163 }
  2174 }
  2164 
  2175 
  2165 void ConcurrentMark::completeCleanup() {
  2176 void ConcurrentMark::completeCleanup() {
  2166   if (has_aborted()) return;
  2177   if (has_aborted()) return;
  2443   _g1h->set_par_threads(_active_workers);
  2454   _g1h->set_par_threads(_active_workers);
  2444   _workers->run_task(&enq_task_proxy);
  2455   _workers->run_task(&enq_task_proxy);
  2445   _g1h->set_par_threads(0);
  2456   _g1h->set_par_threads(0);
  2446 }
  2457 }
  2447 
  2458 
       
  2459 void ConcurrentMark::weakRefsWorkParallelPart(BoolObjectClosure* is_alive, bool purged_classes) {
       
  2460   G1CollectedHeap::heap()->parallel_cleaning(is_alive, true, true, purged_classes);
       
  2461 }
       
  2462 
       
  2463 // Helper class to get rid of some boilerplate code.
       
  2464 class G1RemarkGCTraceTime : public GCTraceTime {
       
  2465   static bool doit_and_prepend(bool doit) {
       
  2466     if (doit) {
       
  2467       gclog_or_tty->put(' ');
       
  2468     }
       
  2469     return doit;
       
  2470   }
       
  2471 
       
  2472  public:
       
  2473   G1RemarkGCTraceTime(const char* title, bool doit)
       
  2474     : GCTraceTime(title, doit_and_prepend(doit), false, G1CollectedHeap::heap()->gc_timer_cm(),
       
  2475         G1CollectedHeap::heap()->concurrent_mark()->concurrent_gc_id()) {
       
  2476   }
       
  2477 };
       
  2478 
  2448 void ConcurrentMark::weakRefsWork(bool clear_all_soft_refs) {
  2479 void ConcurrentMark::weakRefsWork(bool clear_all_soft_refs) {
  2449   if (has_overflown()) {
  2480   if (has_overflown()) {
  2450     // Skip processing the discovered references if we have
  2481     // Skip processing the discovered references if we have
  2451     // overflown the global marking stack. Reference objects
  2482     // overflown the global marking stack. Reference objects
  2452     // only get discovered once so it is OK to not
  2483     // only get discovered once so it is OK to not
  2555   if (has_overflown()) {
  2586   if (has_overflown()) {
  2556     // We can not trust g1_is_alive if the marking stack overflowed
  2587     // We can not trust g1_is_alive if the marking stack overflowed
  2557     return;
  2588     return;
  2558   }
  2589   }
  2559 
  2590 
  2560   g1h->unlink_string_and_symbol_table(&g1_is_alive,
  2591   assert(_markStack.isEmpty(), "Marking should have completed");
  2561                                       /* process_strings */ false, // currently strings are always roots
  2592 
  2562                                       /* process_symbols */ true);
  2593   // Unload Klasses, String, Symbols, Code Cache, etc.
       
  2594 
       
  2595   G1RemarkGCTraceTime trace("Unloading", G1Log::finer());
       
  2596 
       
  2597   bool purged_classes;
       
  2598 
       
  2599   {
       
  2600     G1RemarkGCTraceTime trace("System Dictionary Unloading", G1Log::finest());
       
  2601     purged_classes = SystemDictionary::do_unloading(&g1_is_alive);
       
  2602   }
       
  2603 
       
  2604   {
       
  2605     G1RemarkGCTraceTime trace("Parallel Unloading", G1Log::finest());
       
  2606     weakRefsWorkParallelPart(&g1_is_alive, purged_classes);
       
  2607   }
       
  2608 
       
  2609   if (G1StringDedup::is_enabled()) {
       
  2610     G1RemarkGCTraceTime trace("String Deduplication Unlink", G1Log::finest());
       
  2611     G1StringDedup::unlink(&g1_is_alive);
       
  2612   }
  2563 }
  2613 }
  2564 
  2614 
  2565 void ConcurrentMark::swapMarkBitMaps() {
  2615 void ConcurrentMark::swapMarkBitMaps() {
  2566   CMBitMapRO* temp = _prevMarkBitMap;
  2616   CMBitMapRO* temp = _prevMarkBitMap;
  2567   _prevMarkBitMap  = (CMBitMapRO*)_nextMarkBitMap;
  2617   _prevMarkBitMap  = (CMBitMapRO*)_nextMarkBitMap;
  2568   _nextMarkBitMap  = (CMBitMap*)  temp;
  2618   _nextMarkBitMap  = (CMBitMap*)  temp;
  2569 }
  2619 }
       
  2620 
       
  2621 class CMObjectClosure;
       
  2622 
       
  2623 // Closure for iterating over objects, currently only used for
       
  2624 // processing SATB buffers.
       
  2625 class CMObjectClosure : public ObjectClosure {
       
  2626 private:
       
  2627   CMTask* _task;
       
  2628 
       
  2629 public:
       
  2630   void do_object(oop obj) {
       
  2631     _task->deal_with_reference(obj);
       
  2632   }
       
  2633 
       
  2634   CMObjectClosure(CMTask* task) : _task(task) { }
       
  2635 };
       
  2636 
       
  2637 class G1RemarkThreadsClosure : public ThreadClosure {
       
  2638   CMObjectClosure _cm_obj;
       
  2639   G1CMOopClosure _cm_cl;
       
  2640   MarkingCodeBlobClosure _code_cl;
       
  2641   int _thread_parity;
       
  2642   bool _is_par;
       
  2643 
       
  2644  public:
       
  2645   G1RemarkThreadsClosure(G1CollectedHeap* g1h, CMTask* task, bool is_par) :
       
  2646     _cm_obj(task), _cm_cl(g1h, g1h->concurrent_mark(), task), _code_cl(&_cm_cl, !CodeBlobToOopClosure::FixRelocations),
       
  2647     _thread_parity(SharedHeap::heap()->strong_roots_parity()), _is_par(is_par) {}
       
  2648 
       
  2649   void do_thread(Thread* thread) {
       
  2650     if (thread->is_Java_thread()) {
       
  2651       if (thread->claim_oops_do(_is_par, _thread_parity)) {
       
  2652         JavaThread* jt = (JavaThread*)thread;
       
  2653 
       
  2654         // In theory it should not be neccessary to explicitly walk the nmethods to find roots for concurrent marking
       
  2655         // however the liveness of oops reachable from nmethods have very complex lifecycles:
       
  2656         // * Alive if on the stack of an executing method
       
  2657         // * Weakly reachable otherwise
       
  2658         // Some objects reachable from nmethods, such as the class loader (or klass_holder) of the receiver should be
       
  2659         // live by the SATB invariant but other oops recorded in nmethods may behave differently.
       
  2660         jt->nmethods_do(&_code_cl);
       
  2661 
       
  2662         jt->satb_mark_queue().apply_closure_and_empty(&_cm_obj);
       
  2663       }
       
  2664     } else if (thread->is_VM_thread()) {
       
  2665       if (thread->claim_oops_do(_is_par, _thread_parity)) {
       
  2666         JavaThread::satb_mark_queue_set().shared_satb_queue()->apply_closure_and_empty(&_cm_obj);
       
  2667       }
       
  2668     }
       
  2669   }
       
  2670 };
  2570 
  2671 
  2571 class CMRemarkTask: public AbstractGangTask {
  2672 class CMRemarkTask: public AbstractGangTask {
  2572 private:
  2673 private:
  2573   ConcurrentMark* _cm;
  2674   ConcurrentMark* _cm;
  2574   bool            _is_serial;
  2675   bool            _is_serial;
  2577     // Since all available tasks are actually started, we should
  2678     // Since all available tasks are actually started, we should
  2578     // only proceed if we're supposed to be active.
  2679     // only proceed if we're supposed to be active.
  2579     if (worker_id < _cm->active_tasks()) {
  2680     if (worker_id < _cm->active_tasks()) {
  2580       CMTask* task = _cm->task(worker_id);
  2681       CMTask* task = _cm->task(worker_id);
  2581       task->record_start_time();
  2682       task->record_start_time();
       
  2683       {
       
  2684         ResourceMark rm;
       
  2685         HandleMark hm;
       
  2686 
       
  2687         G1RemarkThreadsClosure threads_f(G1CollectedHeap::heap(), task, !_is_serial);
       
  2688         Threads::threads_do(&threads_f);
       
  2689       }
       
  2690 
  2582       do {
  2691       do {
  2583         task->do_marking_step(1000000000.0 /* something very large */,
  2692         task->do_marking_step(1000000000.0 /* something very large */,
  2584                               true         /* do_termination       */,
  2693                               true         /* do_termination       */,
  2585                               _is_serial);
  2694                               _is_serial);
  2586       } while (task->has_aborted() && !_cm->has_overflown());
  2695       } while (task->has_aborted() && !_cm->has_overflown());
  2598 
  2707 
  2599 void ConcurrentMark::checkpointRootsFinalWork() {
  2708 void ConcurrentMark::checkpointRootsFinalWork() {
  2600   ResourceMark rm;
  2709   ResourceMark rm;
  2601   HandleMark   hm;
  2710   HandleMark   hm;
  2602   G1CollectedHeap* g1h = G1CollectedHeap::heap();
  2711   G1CollectedHeap* g1h = G1CollectedHeap::heap();
       
  2712 
       
  2713   G1RemarkGCTraceTime trace("Finalize Marking", G1Log::finer());
  2603 
  2714 
  2604   g1h->ensure_parsability(false);
  2715   g1h->ensure_parsability(false);
  2605 
  2716 
  2606   if (G1CollectedHeap::use_parallel_gc_threads()) {
  2717   if (G1CollectedHeap::use_parallel_gc_threads()) {
  2607     G1CollectedHeap::StrongRootsScope srs(g1h);
  2718     G1CollectedHeap::StrongRootsScope srs(g1h);
  3428     // the iteration
  3539     // the iteration
  3429     return !_task->has_aborted();
  3540     return !_task->has_aborted();
  3430   }
  3541   }
  3431 };
  3542 };
  3432 
  3543 
  3433 // Closure for iterating over objects, currently only used for
       
  3434 // processing SATB buffers.
       
  3435 class CMObjectClosure : public ObjectClosure {
       
  3436 private:
       
  3437   CMTask* _task;
       
  3438 
       
  3439 public:
       
  3440   void do_object(oop obj) {
       
  3441     _task->deal_with_reference(obj);
       
  3442   }
       
  3443 
       
  3444   CMObjectClosure(CMTask* task) : _task(task) { }
       
  3445 };
       
  3446 
       
  3447 G1CMOopClosure::G1CMOopClosure(G1CollectedHeap* g1h,
  3544 G1CMOopClosure::G1CMOopClosure(G1CollectedHeap* g1h,
  3448                                ConcurrentMark* cm,
  3545                                ConcurrentMark* cm,
  3449                                CMTask* task)
  3546                                CMTask* task)
  3450   : _g1h(g1h), _cm(cm), _task(task) {
  3547   : _g1h(g1h), _cm(cm), _task(task) {
  3451   assert(_ref_processor == NULL, "should be initialized to NULL");
  3548   assert(_ref_processor == NULL, "should be initialized to NULL");
  3903       if (_cm->verbose_medium()) {
  4000       if (_cm->verbose_medium()) {
  3904         gclog_or_tty->print_cr("[%u] processed an SATB buffer", _worker_id);
  4001         gclog_or_tty->print_cr("[%u] processed an SATB buffer", _worker_id);
  3905       }
  4002       }
  3906       statsOnly( ++_satb_buffers_processed );
  4003       statsOnly( ++_satb_buffers_processed );
  3907       regular_clock_call();
  4004       regular_clock_call();
  3908     }
       
  3909   }
       
  3910 
       
  3911   if (!concurrent() && !has_aborted()) {
       
  3912     // We should only do this during remark.
       
  3913     if (G1CollectedHeap::use_parallel_gc_threads()) {
       
  3914       satb_mq_set.par_iterate_closure_all_threads(_worker_id);
       
  3915     } else {
       
  3916       satb_mq_set.iterate_closure_all_threads();
       
  3917     }
  4005     }
  3918   }
  4006   }
  3919 
  4007 
  3920   _draining_satb_buffers = false;
  4008   _draining_satb_buffers = false;
  3921 
  4009