src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp

changeset 7975
79b13c9a93e8
parent 7974
8f8f1d578796
child 7990
1f646daf0d67
equal deleted inserted replaced
7974:8f8f1d578796 7975:79b13c9a93e8
44 #include "gc_implementation/g1/g1MarkSweep.hpp" 44 #include "gc_implementation/g1/g1MarkSweep.hpp"
45 #include "gc_implementation/g1/g1OopClosures.inline.hpp" 45 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
46 #include "gc_implementation/g1/g1ParScanThreadState.inline.hpp" 46 #include "gc_implementation/g1/g1ParScanThreadState.inline.hpp"
47 #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp" 47 #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"
48 #include "gc_implementation/g1/g1RemSet.inline.hpp" 48 #include "gc_implementation/g1/g1RemSet.inline.hpp"
49 #include "gc_implementation/g1/g1RootProcessor.hpp"
49 #include "gc_implementation/g1/g1StringDedup.hpp" 50 #include "gc_implementation/g1/g1StringDedup.hpp"
50 #include "gc_implementation/g1/g1YCTypes.hpp" 51 #include "gc_implementation/g1/g1YCTypes.hpp"
51 #include "gc_implementation/g1/heapRegion.inline.hpp" 52 #include "gc_implementation/g1/heapRegion.inline.hpp"
52 #include "gc_implementation/g1/heapRegionRemSet.hpp" 53 #include "gc_implementation/g1/heapRegionRemSet.hpp"
53 #include "gc_implementation/g1/heapRegionSet.inline.hpp" 54 #include "gc_implementation/g1/heapRegionSet.inline.hpp"
82 // serialized by acquiring the HeapLock. This happens in mem_allocate 83 // serialized by acquiring the HeapLock. This happens in mem_allocate
83 // and allocate_new_tlab, which are the "entry" points to the 84 // and allocate_new_tlab, which are the "entry" points to the
84 // allocation code from the rest of the JVM. (Note that this does not 85 // allocation code from the rest of the JVM. (Note that this does not
85 // apply to TLAB allocation, which is not part of this interface: it 86 // apply to TLAB allocation, which is not part of this interface: it
86 // is done by clients of this interface.) 87 // is done by clients of this interface.)
87
88 // Notes on implementation of parallelism in different tasks.
89 //
90 // G1ParVerifyTask uses heap_region_par_iterate_chunked() for parallelism.
91 // The number of GC workers is passed to heap_region_par_iterate_chunked().
92 // It does use run_task() which sets _n_workers in the task.
93 // G1ParTask executes g1_process_roots() ->
94 // SharedHeap::process_roots() which calls eventually to
95 // CardTableModRefBS::par_non_clean_card_iterate_work() which uses
96 // SequentialSubTasksDone. SharedHeap::process_roots() also
97 // directly uses SubTasksDone (_process_strong_tasks field in SharedHeap).
98 //
99 88
100 // Local to this file. 89 // Local to this file.
101 90
102 class RefineCardTableEntryClosure: public CardTableEntryClosure { 91 class RefineCardTableEntryClosure: public CardTableEntryClosure {
103 bool _concurrent; 92 bool _concurrent;
1853 _into_cset_dirty_card_queue_set(false), 1842 _into_cset_dirty_card_queue_set(false),
1854 _is_alive_closure_cm(this), 1843 _is_alive_closure_cm(this),
1855 _is_alive_closure_stw(this), 1844 _is_alive_closure_stw(this),
1856 _ref_processor_cm(NULL), 1845 _ref_processor_cm(NULL),
1857 _ref_processor_stw(NULL), 1846 _ref_processor_stw(NULL),
1858 _process_strong_tasks(new SubTasksDone(G1H_PS_NumElements)),
1859 _bot_shared(NULL), 1847 _bot_shared(NULL),
1860 _evac_failure_scan_stack(NULL), 1848 _evac_failure_scan_stack(NULL),
1861 _mark_in_progress(false), 1849 _mark_in_progress(false),
1862 _cg1r(NULL), 1850 _cg1r(NULL),
1863 _g1mm(NULL), 1851 _g1mm(NULL),
1886 _gc_timer_cm(new (ResourceObj::C_HEAP, mtGC) ConcurrentGCTimer()), 1874 _gc_timer_cm(new (ResourceObj::C_HEAP, mtGC) ConcurrentGCTimer()),
1887 _gc_tracer_stw(new (ResourceObj::C_HEAP, mtGC) G1NewTracer()), 1875 _gc_tracer_stw(new (ResourceObj::C_HEAP, mtGC) G1NewTracer()),
1888 _gc_tracer_cm(new (ResourceObj::C_HEAP, mtGC) G1OldTracer()) { 1876 _gc_tracer_cm(new (ResourceObj::C_HEAP, mtGC) G1OldTracer()) {
1889 1877
1890 _g1h = this; 1878 _g1h = this;
1891 if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) {
1892 vm_exit_during_initialization("Failed necessary allocation.");
1893 }
1894 1879
1895 _allocator = G1Allocator::create_allocator(_g1h); 1880 _allocator = G1Allocator::create_allocator(_g1h);
1896 _humongous_object_threshold_in_words = HeapRegion::GrainWords / 2; 1881 _humongous_object_threshold_in_words = HeapRegion::GrainWords / 2;
1897 1882
1898 int n_queues = MAX2((int)ParallelGCThreads, 1); 1883 int n_queues = MAX2((int)ParallelGCThreads, 1);
3290 // system dictionary, class loader data graph, the string table 3275 // system dictionary, class loader data graph, the string table
3291 // and the nmethods in the code cache. 3276 // and the nmethods in the code cache.
3292 G1VerifyCodeRootOopClosure codeRootsCl(this, &rootsCl, vo); 3277 G1VerifyCodeRootOopClosure codeRootsCl(this, &rootsCl, vo);
3293 G1VerifyCodeRootBlobClosure blobsCl(&codeRootsCl); 3278 G1VerifyCodeRootBlobClosure blobsCl(&codeRootsCl);
3294 3279
3295 process_all_roots(true, // activate StrongRootsScope 3280 {
3296 SO_AllCodeCache, // roots scanning options 3281 G1RootProcessor root_processor(this);
3297 &rootsCl, 3282 root_processor.process_all_roots(&rootsCl,
3298 &cldCl, 3283 &cldCl,
3299 &blobsCl); 3284 &blobsCl);
3285 }
3300 3286
3301 bool failures = rootsCl.failures() || codeRootsCl.failures(); 3287 bool failures = rootsCl.failures() || codeRootsCl.failures();
3302 3288
3303 if (vo != VerifyOption_G1UseMarkWord) { 3289 if (vo != VerifyOption_G1UseMarkWord) {
3304 // If we're verifying during a full GC then the region sets 3290 // If we're verifying during a full GC then the region sets
4580 } 4566 }
4581 _count++; 4567 _count++;
4582 } 4568 }
4583 }; 4569 };
4584 4570
4585 class G1CodeBlobClosure : public CodeBlobClosure {
4586 class HeapRegionGatheringOopClosure : public OopClosure {
4587 G1CollectedHeap* _g1h;
4588 OopClosure* _work;
4589 nmethod* _nm;
4590
4591 template <typename T>
4592 void do_oop_work(T* p) {
4593 _work->do_oop(p);
4594 T oop_or_narrowoop = oopDesc::load_heap_oop(p);
4595 if (!oopDesc::is_null(oop_or_narrowoop)) {
4596 oop o = oopDesc::decode_heap_oop_not_null(oop_or_narrowoop);
4597 HeapRegion* hr = _g1h->heap_region_containing_raw(o);
4598 assert(!_g1h->obj_in_cs(o) || hr->rem_set()->strong_code_roots_list_contains(_nm), "if o still in CS then evacuation failed and nm must already be in the remset");
4599 hr->add_strong_code_root(_nm);
4600 }
4601 }
4602
4603 public:
4604 HeapRegionGatheringOopClosure(OopClosure* oc) : _g1h(G1CollectedHeap::heap()), _work(oc), _nm(NULL) {}
4605
4606 void do_oop(oop* o) {
4607 do_oop_work(o);
4608 }
4609
4610 void do_oop(narrowOop* o) {
4611 do_oop_work(o);
4612 }
4613
4614 void set_nm(nmethod* nm) {
4615 _nm = nm;
4616 }
4617 };
4618
4619 HeapRegionGatheringOopClosure _oc;
4620 public:
4621 G1CodeBlobClosure(OopClosure* oc) : _oc(oc) {}
4622
4623 void do_code_blob(CodeBlob* cb) {
4624 nmethod* nm = cb->as_nmethod_or_null();
4625 if (nm != NULL) {
4626 if (!nm->test_set_oops_do_mark()) {
4627 _oc.set_nm(nm);
4628 nm->oops_do(&_oc);
4629 nm->fix_oop_relocations();
4630 }
4631 }
4632 }
4633 };
4634
4635 class G1ParTask : public AbstractGangTask { 4571 class G1ParTask : public AbstractGangTask {
4636 protected: 4572 protected:
4637 G1CollectedHeap* _g1h; 4573 G1CollectedHeap* _g1h;
4638 RefToScanQueueSet *_queues; 4574 RefToScanQueueSet *_queues;
4575 G1RootProcessor* _root_processor;
4639 ParallelTaskTerminator _terminator; 4576 ParallelTaskTerminator _terminator;
4640 uint _n_workers; 4577 uint _n_workers;
4641 4578
4642 Mutex _stats_lock; 4579 Mutex _stats_lock;
4643 Mutex* stats_lock() { return &_stats_lock; } 4580 Mutex* stats_lock() { return &_stats_lock; }
4644 4581
4645 public: 4582 public:
4646 G1ParTask(G1CollectedHeap* g1h, RefToScanQueueSet *task_queues) 4583 G1ParTask(G1CollectedHeap* g1h, RefToScanQueueSet *task_queues, G1RootProcessor* root_processor)
4647 : AbstractGangTask("G1 collection"), 4584 : AbstractGangTask("G1 collection"),
4648 _g1h(g1h), 4585 _g1h(g1h),
4649 _queues(task_queues), 4586 _queues(task_queues),
4587 _root_processor(root_processor),
4650 _terminator(0, _queues), 4588 _terminator(0, _queues),
4651 _stats_lock(Mutex::leaf, "parallel G1 stats lock", true) 4589 _stats_lock(Mutex::leaf, "parallel G1 stats lock", true)
4652 {} 4590 {}
4653 4591
4654 RefToScanQueueSet* queues() { return _queues; } 4592 RefToScanQueueSet* queues() { return _queues; }
4658 } 4596 }
4659 4597
4660 ParallelTaskTerminator* terminator() { return &_terminator; } 4598 ParallelTaskTerminator* terminator() { return &_terminator; }
4661 4599
4662 virtual void set_for_termination(int active_workers) { 4600 virtual void set_for_termination(int active_workers) {
4663 // This task calls set_n_termination() in par_non_clean_card_iterate_work() 4601 _root_processor->set_num_workers(active_workers);
4664 // in the young space (_par_seq_tasks) in the G1 heap
4665 // for SequentialSubTasksDone.
4666 // This task also uses SubTasksDone in SharedHeap and G1CollectedHeap
4667 // both of which need setting by set_n_termination().
4668 _g1h->SharedHeap::set_n_termination(active_workers);
4669 _g1h->set_n_termination(active_workers);
4670 terminator()->reset_for_reuse(active_workers); 4602 terminator()->reset_for_reuse(active_workers);
4671 _n_workers = active_workers; 4603 _n_workers = active_workers;
4672 } 4604 }
4673 4605
4674 // Helps out with CLD processing. 4606 // Helps out with CLD processing.
4733 G1ParCopyClosure<G1BarrierNone, G1MarkPromotedFromRoot> scan_mark_weak_root_cl(_g1h, &pss, rp); 4665 G1ParCopyClosure<G1BarrierNone, G1MarkPromotedFromRoot> scan_mark_weak_root_cl(_g1h, &pss, rp);
4734 G1CLDClosure<G1MarkPromotedFromRoot> scan_mark_weak_cld_cl(&scan_mark_weak_root_cl, 4666 G1CLDClosure<G1MarkPromotedFromRoot> scan_mark_weak_cld_cl(&scan_mark_weak_root_cl,
4735 false, // Process all klasses. 4667 false, // Process all klasses.
4736 true); // Need to claim CLDs. 4668 true); // Need to claim CLDs.
4737 4669
4738 G1CodeBlobClosure scan_only_code_cl(&scan_only_root_cl);
4739 G1CodeBlobClosure scan_mark_code_cl(&scan_mark_root_cl);
4740 // IM Weak code roots are handled later.
4741
4742 OopClosure* strong_root_cl; 4670 OopClosure* strong_root_cl;
4743 OopClosure* weak_root_cl; 4671 OopClosure* weak_root_cl;
4744 CLDClosure* strong_cld_cl; 4672 CLDClosure* strong_cld_cl;
4745 CLDClosure* weak_cld_cl; 4673 CLDClosure* weak_cld_cl;
4746 CodeBlobClosure* strong_code_cl; 4674
4675 bool trace_metadata = false;
4747 4676
4748 if (_g1h->g1_policy()->during_initial_mark_pause()) { 4677 if (_g1h->g1_policy()->during_initial_mark_pause()) {
4749 // We also need to mark copied objects. 4678 // We also need to mark copied objects.
4750 strong_root_cl = &scan_mark_root_cl; 4679 strong_root_cl = &scan_mark_root_cl;
4751 strong_cld_cl = &scan_mark_cld_cl; 4680 strong_cld_cl = &scan_mark_cld_cl;
4752 strong_code_cl = &scan_mark_code_cl;
4753 if (ClassUnloadingWithConcurrentMark) { 4681 if (ClassUnloadingWithConcurrentMark) {
4754 weak_root_cl = &scan_mark_weak_root_cl; 4682 weak_root_cl = &scan_mark_weak_root_cl;
4755 weak_cld_cl = &scan_mark_weak_cld_cl; 4683 weak_cld_cl = &scan_mark_weak_cld_cl;
4684 trace_metadata = true;
4756 } else { 4685 } else {
4757 weak_root_cl = &scan_mark_root_cl; 4686 weak_root_cl = &scan_mark_root_cl;
4758 weak_cld_cl = &scan_mark_cld_cl; 4687 weak_cld_cl = &scan_mark_cld_cl;
4759 } 4688 }
4760 } else { 4689 } else {
4761 strong_root_cl = &scan_only_root_cl; 4690 strong_root_cl = &scan_only_root_cl;
4762 weak_root_cl = &scan_only_root_cl; 4691 weak_root_cl = &scan_only_root_cl;
4763 strong_cld_cl = &scan_only_cld_cl; 4692 strong_cld_cl = &scan_only_cld_cl;
4764 weak_cld_cl = &scan_only_cld_cl; 4693 weak_cld_cl = &scan_only_cld_cl;
4765 strong_code_cl = &scan_only_code_cl;
4766 } 4694 }
4767 4695
4768
4769 G1ParPushHeapRSClosure push_heap_rs_cl(_g1h, &pss);
4770
4771 pss.start_strong_roots(); 4696 pss.start_strong_roots();
4772 _g1h->g1_process_roots(strong_root_cl, 4697
4773 weak_root_cl, 4698 _root_processor->evacuate_roots(strong_root_cl,
4774 &push_heap_rs_cl, 4699 weak_root_cl,
4775 strong_cld_cl, 4700 strong_cld_cl,
4776 weak_cld_cl, 4701 weak_cld_cl,
4777 strong_code_cl, 4702 trace_metadata,
4778 worker_id); 4703 worker_id);
4779 4704
4705 G1ParPushHeapRSClosure push_heap_rs_cl(_g1h, &pss);
4706 _root_processor->scan_remembered_sets(&push_heap_rs_cl,
4707 weak_root_cl,
4708 worker_id);
4780 pss.end_strong_roots(); 4709 pss.end_strong_roots();
4781 4710
4782 { 4711 {
4783 double start = os::elapsedTime(); 4712 double start = os::elapsedTime();
4784 G1ParEvacuateFollowersClosure evac(_g1h, &pss, _queues, &_terminator); 4713 G1ParEvacuateFollowersClosure evac(_g1h, &pss, _queues, &_terminator);
4804 // "GC Worker Time". 4733 // "GC Worker Time".
4805 } 4734 }
4806 _g1h->g1_policy()->phase_times()->record_time_secs(G1GCPhaseTimes::GCWorkerEnd, worker_id, os::elapsedTime()); 4735 _g1h->g1_policy()->phase_times()->record_time_secs(G1GCPhaseTimes::GCWorkerEnd, worker_id, os::elapsedTime());
4807 } 4736 }
4808 }; 4737 };
4809
4810 // *** Common G1 Evacuation Stuff
4811
4812 // This method is run in a GC worker.
4813
4814 void
4815 G1CollectedHeap::
4816 g1_process_roots(OopClosure* scan_non_heap_roots,
4817 OopClosure* scan_non_heap_weak_roots,
4818 G1ParPushHeapRSClosure* scan_rs,
4819 CLDClosure* scan_strong_clds,
4820 CLDClosure* scan_weak_clds,
4821 CodeBlobClosure* scan_strong_code,
4822 uint worker_i) {
4823
4824 // First scan the shared roots.
4825 double ext_roots_start = os::elapsedTime();
4826 double closure_app_time_sec = 0.0;
4827
4828 bool during_im = _g1h->g1_policy()->during_initial_mark_pause();
4829 bool trace_metadata = during_im && ClassUnloadingWithConcurrentMark;
4830
4831 BufferingOopClosure buf_scan_non_heap_roots(scan_non_heap_roots);
4832 BufferingOopClosure buf_scan_non_heap_weak_roots(scan_non_heap_weak_roots);
4833
4834 process_roots(false, // no scoping; this is parallel code
4835 SharedHeap::SO_None,
4836 &buf_scan_non_heap_roots,
4837 &buf_scan_non_heap_weak_roots,
4838 scan_strong_clds,
4839 // Unloading Initial Marks handle the weak CLDs separately.
4840 (trace_metadata ? NULL : scan_weak_clds),
4841 scan_strong_code);
4842
4843 // Now the CM ref_processor roots.
4844 if (!_process_strong_tasks->is_task_claimed(G1H_PS_refProcessor_oops_do)) {
4845 // We need to treat the discovered reference lists of the
4846 // concurrent mark ref processor as roots and keep entries
4847 // (which are added by the marking threads) on them live
4848 // until they can be processed at the end of marking.
4849 ref_processor_cm()->weak_oops_do(&buf_scan_non_heap_roots);
4850 }
4851
4852 if (trace_metadata) {
4853 // Barrier to make sure all workers passed
4854 // the strong CLD and strong nmethods phases.
4855 active_strong_roots_scope()->wait_until_all_workers_done_with_threads(n_par_threads());
4856
4857 // Now take the complement of the strong CLDs.
4858 ClassLoaderDataGraph::roots_cld_do(NULL, scan_weak_clds);
4859 }
4860
4861 // Finish up any enqueued closure apps (attributed as object copy time).
4862 buf_scan_non_heap_roots.done();
4863 buf_scan_non_heap_weak_roots.done();
4864
4865 double obj_copy_time_sec = buf_scan_non_heap_roots.closure_app_seconds()
4866 + buf_scan_non_heap_weak_roots.closure_app_seconds();
4867
4868 g1_policy()->phase_times()->record_time_secs(G1GCPhaseTimes::ObjCopy, worker_i, obj_copy_time_sec);
4869
4870 double ext_root_time_sec = os::elapsedTime() - ext_roots_start - obj_copy_time_sec;
4871 g1_policy()->phase_times()->record_time_secs(G1GCPhaseTimes::ExtRootScan, worker_i, ext_root_time_sec);
4872
4873 // During conc marking we have to filter the per-thread SATB buffers
4874 // to make sure we remove any oops into the CSet (which will show up
4875 // as implicitly live).
4876 {
4877 G1GCParPhaseTimesTracker x(g1_policy()->phase_times(), G1GCPhaseTimes::SATBFiltering, worker_i);
4878 if (!_process_strong_tasks->is_task_claimed(G1H_PS_filter_satb_buffers) && mark_in_progress()) {
4879 JavaThread::satb_mark_queue_set().filter_thread_buffers();
4880 }
4881 }
4882
4883 // Now scan the complement of the collection set.
4884 G1CodeBlobClosure scavenge_cs_nmethods(scan_non_heap_weak_roots);
4885
4886 g1_rem_set()->oops_into_collection_set_do(scan_rs, &scavenge_cs_nmethods, worker_i);
4887
4888 _process_strong_tasks->all_tasks_completed();
4889 }
4890 4738
4891 class G1StringSymbolTableUnlinkTask : public AbstractGangTask { 4739 class G1StringSymbolTableUnlinkTask : public AbstractGangTask {
4892 private: 4740 private:
4893 BoolObjectClosure* _is_alive; 4741 BoolObjectClosure* _is_alive;
4894 int _initial_string_table_size; 4742 int _initial_string_table_size;
5873 assert(n_par_threads() == 0, 5721 assert(n_par_threads() == 0,
5874 "Should be the original non-parallel value"); 5722 "Should be the original non-parallel value");
5875 n_workers = 1; 5723 n_workers = 1;
5876 } 5724 }
5877 5725
5878 G1ParTask g1_par_task(this, _task_queues);
5879 5726
5880 init_for_evac_failure(NULL); 5727 init_for_evac_failure(NULL);
5881 5728
5882 rem_set()->prepare_for_younger_refs_iterate(true); 5729 rem_set()->prepare_for_younger_refs_iterate(true);
5883 5730
5884 assert(dirty_card_queue_set().completed_buffers_num() == 0, "Should be empty"); 5731 assert(dirty_card_queue_set().completed_buffers_num() == 0, "Should be empty");
5885 double start_par_time_sec = os::elapsedTime(); 5732 double start_par_time_sec = os::elapsedTime();
5886 double end_par_time_sec; 5733 double end_par_time_sec;
5887 5734
5888 { 5735 {
5889 StrongRootsScope srs(this); 5736 G1RootProcessor root_processor(this);
5737 G1ParTask g1_par_task(this, _task_queues, &root_processor);
5890 // InitialMark needs claim bits to keep track of the marked-through CLDs. 5738 // InitialMark needs claim bits to keep track of the marked-through CLDs.
5891 if (g1_policy()->during_initial_mark_pause()) { 5739 if (g1_policy()->during_initial_mark_pause()) {
5892 ClassLoaderDataGraph::clear_claimed_marks(); 5740 ClassLoaderDataGraph::clear_claimed_marks();
5893 } 5741 }
5894 5742
5905 g1_par_task.work(0); 5753 g1_par_task.work(0);
5906 } 5754 }
5907 end_par_time_sec = os::elapsedTime(); 5755 end_par_time_sec = os::elapsedTime();
5908 5756
5909 // Closing the inner scope will execute the destructor 5757 // Closing the inner scope will execute the destructor
5910 // for the StrongRootsScope object. We record the current 5758 // for the G1RootProcessor object. We record the current
5911 // elapsed time before closing the scope so that time 5759 // elapsed time before closing the scope so that time
5912 // taken for the SRS destructor is NOT included in the 5760 // taken for the destructor is NOT included in the
5913 // reported parallel time. 5761 // reported parallel time.
5914 } 5762 }
5915 5763
5916 G1GCPhaseTimes* phase_times = g1_policy()->phase_times(); 5764 G1GCPhaseTimes* phase_times = g1_policy()->phase_times();
5917 5765

mercurial