12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.hpp"
27 #include "classfile/stringTable.hpp"
28 #include "classfile/symbolTable.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "code/codeCache.hpp"
31 #include "gc/cms/cmsCollectorPolicy.hpp"
32 #include "gc/cms/cmsOopClosures.inline.hpp"
33 #include "gc/cms/compactibleFreeListSpace.hpp"
34 #include "gc/cms/concurrentMarkSweepGeneration.inline.hpp"
35 #include "gc/cms/concurrentMarkSweepThread.hpp"
36 #include "gc/cms/parNewGeneration.hpp"
37 #include "gc/cms/vmCMSOperations.hpp"
38 #include "gc/serial/genMarkSweep.hpp"
39 #include "gc/serial/tenuredGeneration.hpp"
40 #include "gc/shared/adaptiveSizePolicy.hpp"
41 #include "gc/shared/cardGeneration.inline.hpp"
42 #include "gc/shared/cardTableRS.hpp"
43 #include "gc/shared/collectedHeap.inline.hpp"
44 #include "gc/shared/collectorCounters.hpp"
45 #include "gc/shared/collectorPolicy.hpp"
46 #include "gc/shared/gcLocker.inline.hpp"
47 #include "gc/shared/gcPolicyCounters.hpp"
48 #include "gc/shared/gcTimer.hpp"
49 #include "gc/shared/gcTrace.hpp"
50 #include "gc/shared/gcTraceTime.inline.hpp"
51 #include "gc/shared/genCollectedHeap.hpp"
2309 // In this first variant of verification, we complete
2310 // all marking, then check if the new marks-vector is
2311 // a subset of the CMS marks-vector.
2312 verify_after_remark_work_1();
2313 } else {
2314 guarantee(CMSRemarkVerifyVariant == 2, "Range checking for CMSRemarkVerifyVariant should guarantee 1 or 2");
2315 // In this second variant of verification, we flag an error
2316 // (i.e. an object reachable in the new marks-vector not reachable
2317 // in the CMS marks-vector) immediately, also indicating the
2318 // identify of an object (A) that references the unmarked object (B) --
2319 // presumably, a mutation to A failed to be picked up by preclean/remark?
2320 verify_after_remark_work_2();
2321 }
2322
2323 return true;
2324 }
2325
2326 void CMSCollector::verify_after_remark_work_1() {
2327 ResourceMark rm;
2328 HandleMark hm;
2329 GenCollectedHeap* gch = GenCollectedHeap::heap();
2330
2331 // Get a clear set of claim bits for the roots processing to work with.
2332 ClassLoaderDataGraph::clear_claimed_marks();
2333
2334 // Mark from roots one level into CMS
2335 MarkRefsIntoClosure notOlder(_span, verification_mark_bm());
2336 gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2337
2338 {
2339 StrongRootsScope srs(1);
2340
2341 gch->cms_process_roots(&srs,
2342 true, // young gen as roots
2343 GenCollectedHeap::ScanningOption(roots_scanning_options()),
2344 should_unload_classes(),
2345 ¬Older,
2346 NULL);
2347 }
2348
2349 // Now mark from the roots
2378 }
2379
2380 class VerifyKlassOopsKlassClosure : public KlassClosure {
2381 class VerifyKlassOopsClosure : public OopClosure {
2382 CMSBitMap* _bitmap;
2383 public:
2384 VerifyKlassOopsClosure(CMSBitMap* bitmap) : _bitmap(bitmap) { }
2385 void do_oop(oop* p) { guarantee(*p == NULL || _bitmap->isMarked((HeapWord*) *p), "Should be marked"); }
2386 void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2387 } _oop_closure;
2388 public:
2389 VerifyKlassOopsKlassClosure(CMSBitMap* bitmap) : _oop_closure(bitmap) {}
2390 void do_klass(Klass* k) {
2391 k->oops_do(&_oop_closure);
2392 }
2393 };
2394
2395 void CMSCollector::verify_after_remark_work_2() {
2396 ResourceMark rm;
2397 HandleMark hm;
2398 GenCollectedHeap* gch = GenCollectedHeap::heap();
2399
2400 // Get a clear set of claim bits for the roots processing to work with.
2401 ClassLoaderDataGraph::clear_claimed_marks();
2402
2403 // Mark from roots one level into CMS
2404 MarkRefsIntoVerifyClosure notOlder(_span, verification_mark_bm(),
2405 markBitMap());
2406 CLDToOopClosure cld_closure(¬Older, true);
2407
2408 gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2409
2410 {
2411 StrongRootsScope srs(1);
2412
2413 gch->cms_process_roots(&srs,
2414 true, // young gen as roots
2415 GenCollectedHeap::ScanningOption(roots_scanning_options()),
2416 should_unload_classes(),
2417 ¬Older,
2418 &cld_closure);
2823
2824 // Already have locks.
2825 assert_lock_strong(bitMapLock());
2826 assert(_markBitMap.isAllClear(), "was reset at end of previous cycle");
2827
2828 // Setup the verification and class unloading state for this
2829 // CMS collection cycle.
2830 setup_cms_unloading_and_verification_state();
2831
2832 GCTraceTime(Trace, gc, phases) ts("checkpointRootsInitialWork", _gc_timer_cm);
2833
2834 // Reset all the PLAB chunk arrays if necessary.
2835 if (_survivor_plab_array != NULL && !CMSPLABRecordAlways) {
2836 reset_survivor_plab_arrays();
2837 }
2838
2839 ResourceMark rm;
2840 HandleMark hm;
2841
2842 MarkRefsIntoClosure notOlder(_span, &_markBitMap);
2843 GenCollectedHeap* gch = GenCollectedHeap::heap();
2844
2845 verify_work_stacks_empty();
2846 verify_overflow_empty();
2847
2848 gch->ensure_parsability(false); // fill TLABs, but no need to retire them
2849 // Update the saved marks which may affect the root scans.
2850 gch->save_marks();
2851
2852 // weak reference processing has not started yet.
2853 ref_processor()->set_enqueuing_is_done(false);
2854
2855 // Need to remember all newly created CLDs,
2856 // so that we can guarantee that the remark finds them.
2857 ClassLoaderDataGraph::remember_new_clds(true);
2858
2859 // Whenever a CLD is found, it will be claimed before proceeding to mark
2860 // the klasses. The claimed marks need to be cleared before marking starts.
2861 ClassLoaderDataGraph::clear_claimed_marks();
2862
2863 print_eden_and_survivor_chunk_arrays();
4241 }
4242
4243 _gc_tracer_cm->report_object_count_after_gc(&_is_alive_closure);
4244
4245 // Change under the freelistLocks.
4246 _collectorState = Sweeping;
4247 // Call isAllClear() under bitMapLock
4248 assert(_modUnionTable.isAllClear(),
4249 "Should be clear by end of the final marking");
4250 assert(_ct->klass_rem_set()->mod_union_is_clear(),
4251 "Should be clear by end of the final marking");
4252 }
4253
4254 void CMSParInitialMarkTask::work(uint worker_id) {
4255 elapsedTimer _timer;
4256 ResourceMark rm;
4257 HandleMark hm;
4258
4259 // ---------- scan from roots --------------
4260 _timer.start();
4261 GenCollectedHeap* gch = GenCollectedHeap::heap();
4262 ParMarkRefsIntoClosure par_mri_cl(_collector->_span, &(_collector->_markBitMap));
4263
4264 // ---------- young gen roots --------------
4265 {
4266 work_on_young_gen_roots(&par_mri_cl);
4267 _timer.stop();
4268 log_trace(gc, task)("Finished young gen initial mark scan work in %dth thread: %3.3f sec", worker_id, _timer.seconds());
4269 }
4270
4271 // ---------- remaining roots --------------
4272 _timer.reset();
4273 _timer.start();
4274
4275 CLDToOopClosure cld_closure(&par_mri_cl, true);
4276
4277 gch->cms_process_roots(_strong_roots_scope,
4278 false, // yg was scanned above
4279 GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),
4280 _collector->should_unload_classes(),
4281 &par_mri_cl,
4366 assert(ect <= _collector->_eden_chunk_capacity, "out of bounds");
4367 assert(sct <= _collector->_survivor_chunk_capacity, "out of bounds");
4368
4369 do_young_space_rescan(cl, to_space, NULL, 0);
4370 do_young_space_rescan(cl, from_space, sca, sct);
4371 do_young_space_rescan(cl, eden_space, eca, ect);
4372 }
4373
4374 // work_queue(i) is passed to the closure
4375 // ParMarkRefsIntoAndScanClosure. The "i" parameter
4376 // also is passed to do_dirty_card_rescan_tasks() and to
4377 // do_work_steal() to select the i-th task_queue.
4378
4379 void CMSParRemarkTask::work(uint worker_id) {
4380 elapsedTimer _timer;
4381 ResourceMark rm;
4382 HandleMark hm;
4383
4384 // ---------- rescan from roots --------------
4385 _timer.start();
4386 GenCollectedHeap* gch = GenCollectedHeap::heap();
4387 ParMarkRefsIntoAndScanClosure par_mrias_cl(_collector,
4388 _collector->_span, _collector->ref_processor(),
4389 &(_collector->_markBitMap),
4390 work_queue(worker_id));
4391
4392 // Rescan young gen roots first since these are likely
4393 // coarsely partitioned and may, on that account, constitute
4394 // the critical path; thus, it's best to start off that
4395 // work first.
4396 // ---------- young gen roots --------------
4397 {
4398 work_on_young_gen_roots(&par_mrias_cl);
4399 _timer.stop();
4400 log_trace(gc, task)("Finished young gen rescan work in %dth thread: %3.3f sec", worker_id, _timer.seconds());
4401 }
4402
4403 // ---------- remaining roots --------------
4404 _timer.reset();
4405 _timer.start();
4406 gch->cms_process_roots(_strong_roots_scope,
4818 pst->set_n_tasks(1);
4819 assert(pst->valid(), "Error");
4820 }
4821
4822 // From space
4823 {
4824 SequentialSubTasksDone* pst = _young_gen->from()->par_seq_tasks();
4825 assert(!pst->valid(), "Clobbering existing data?");
4826 size_t n_tasks = _survivor_chunk_index + 1;
4827 assert(n_tasks == 1 || _survivor_chunk_array != NULL, "Error");
4828 // Sets the condition for completion of the subtask (how many threads
4829 // need to finish in order to be done).
4830 pst->set_n_threads(n_threads);
4831 pst->set_n_tasks((int)n_tasks);
4832 assert(pst->valid(), "Error");
4833 }
4834 }
4835
4836 // Parallel version of remark
4837 void CMSCollector::do_remark_parallel() {
4838 GenCollectedHeap* gch = GenCollectedHeap::heap();
4839 WorkGang* workers = gch->workers();
4840 assert(workers != NULL, "Need parallel worker threads.");
4841 // Choose to use the number of GC workers most recently set
4842 // into "active_workers".
4843 uint n_workers = workers->active_workers();
4844
4845 CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();
4846
4847 StrongRootsScope srs(n_workers);
4848
4849 CMSParRemarkTask tsk(this, cms_space, n_workers, workers, task_queues(), &srs);
4850
4851 // We won't be iterating over the cards in the card table updating
4852 // the younger_gen cards, so we shouldn't call the following else
4853 // the verification code as well as subsequent younger_refs_iterate
4854 // code would get confused. XXX
4855 // gch->rem_set()->prepare_for_younger_refs_iterate(true); // parallel
4856
4857 // The young gen rescan work will not be done as part of
4858 // process_roots (which currently doesn't know how to
4877 if (n_workers > 1) {
4878 // Make refs discovery MT-safe, if it isn't already: it may not
4879 // necessarily be so, since it's possible that we are doing
4880 // ST marking.
4881 ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), true);
4882 workers->run_task(&tsk);
4883 } else {
4884 ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), false);
4885 tsk.work(0);
4886 }
4887
4888 // restore, single-threaded for now, any preserved marks
4889 // as a result of work_q overflow
4890 restore_preserved_marks_if_any();
4891 }
4892
4893 // Non-parallel version of remark
4894 void CMSCollector::do_remark_non_parallel() {
4895 ResourceMark rm;
4896 HandleMark hm;
4897 GenCollectedHeap* gch = GenCollectedHeap::heap();
4898 ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), false);
4899
4900 MarkRefsIntoAndScanClosure
4901 mrias_cl(_span, ref_processor(), &_markBitMap, NULL /* not precleaning */,
4902 &_markStack, this,
4903 false /* should_yield */, false /* not precleaning */);
4904 MarkFromDirtyCardsClosure
4905 markFromDirtyCardsClosure(this, _span,
4906 NULL, // space is set further below
4907 &_markBitMap, &_markStack, &mrias_cl);
4908 {
4909 GCTraceTime(Trace, gc, phases) t("Grey Object Rescan", _gc_timer_cm);
4910 // Iterate over the dirty cards, setting the corresponding bits in the
4911 // mod union table.
4912 {
4913 ModUnionClosure modUnionClosure(&_modUnionTable);
4914 _ct->ct_bs()->dirty_card_iterate(
4915 _cmsGen->used_region(),
4916 &modUnionClosure);
4917 }
5129 }
5130 // Verify that we have no work before we resort to stealing
5131 assert(work_q->size() == 0, "Have work, shouldn't steal");
5132 // Try to steal from other queues that have work
5133 if (task_queues()->steal(i, seed, /* reference */ obj_to_scan)) {
5134 NOT_PRODUCT(num_steals++;)
5135 assert(obj_to_scan->is_oop(), "Oops, not an oop!");
5136 assert(_mark_bit_map->isMarked((HeapWord*)obj_to_scan), "Stole an unmarked oop?");
5137 // Do scanning work
5138 obj_to_scan->oop_iterate(keep_alive);
5139 // Loop around, finish this work, and try to steal some more
5140 } else if (terminator()->offer_termination()) {
5141 break; // nirvana from the infinite cycle
5142 }
5143 }
5144 log_develop_trace(gc, task)("\t(%d: stole %d oops)", i, num_steals);
5145 }
5146
5147 void CMSRefProcTaskExecutor::execute(ProcessTask& task)
5148 {
5149 GenCollectedHeap* gch = GenCollectedHeap::heap();
5150 WorkGang* workers = gch->workers();
5151 assert(workers != NULL, "Need parallel worker threads.");
5152 CMSRefProcTaskProxy rp_task(task, &_collector,
5153 _collector.ref_processor()->span(),
5154 _collector.markBitMap(),
5155 workers, _collector.task_queues());
5156 workers->run_task(&rp_task);
5157 }
5158
5159 void CMSRefProcTaskExecutor::execute(EnqueueTask& task)
5160 {
5161
5162 GenCollectedHeap* gch = GenCollectedHeap::heap();
5163 WorkGang* workers = gch->workers();
5164 assert(workers != NULL, "Need parallel worker threads.");
5165 CMSRefEnqueueTaskProxy enq_task(task);
5166 workers->run_task(&enq_task);
5167 }
5168
5169 void CMSCollector::refProcessingWork() {
5170 ResourceMark rm;
5171 HandleMark hm;
5172
5173 ReferenceProcessor* rp = ref_processor();
5174 assert(rp->span().equals(_span), "Spans should be equal");
5175 assert(!rp->enqueuing_is_done(), "Enqueuing should not be complete");
5176 // Process weak references.
5177 rp->setup_policy(false);
5178 verify_work_stacks_empty();
5179
5180 CMSKeepAliveClosure cmsKeepAliveClosure(this, _span, &_markBitMap,
5181 &_markStack, false /* !preclean */);
5182 CMSDrainMarkingStackClosure cmsDrainMarkingStackClosure(this,
5183 _span, &_markBitMap, &_markStack,
5184 &cmsKeepAliveClosure, false /* !preclean */);
5185 {
5186 GCTraceTime(Debug, gc, phases) t("Reference Processing", _gc_timer_cm);
5187
5188 ReferenceProcessorStats stats;
5189 if (rp->processing_is_mt()) {
5190 // Set the degree of MT here. If the discovery is done MT, there
5191 // may have been a different number of threads doing the discovery
5192 // and a different number of discovered lists may have Ref objects.
5193 // That is OK as long as the Reference lists are balanced (see
5194 // balance_all_queues() and balance_queues()).
5195 GenCollectedHeap* gch = GenCollectedHeap::heap();
5196 uint active_workers = ParallelGCThreads;
5197 WorkGang* workers = gch->workers();
5198 if (workers != NULL) {
5199 active_workers = workers->active_workers();
5200 // The expectation is that active_workers will have already
5201 // been set to a reasonable value. If it has not been set,
5202 // investigate.
5203 assert(active_workers > 0, "Should have been set during scavenge");
5204 }
5205 rp->set_active_mt_degree(active_workers);
5206 CMSRefProcTaskExecutor task_executor(*this);
5207 stats = rp->process_discovered_references(&_is_alive_closure,
5208 &cmsKeepAliveClosure,
5209 &cmsDrainMarkingStackClosure,
5210 &task_executor,
5211 _gc_timer_cm);
5212 } else {
5213 stats = rp->process_discovered_references(&_is_alive_closure,
5214 &cmsKeepAliveClosure,
5215 &cmsDrainMarkingStackClosure,
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.hpp"
27 #include "classfile/stringTable.hpp"
28 #include "classfile/symbolTable.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "code/codeCache.hpp"
31 #include "gc/cms/cmsCollectorPolicy.hpp"
32 #include "gc/cms/cmsHeap.hpp"
33 #include "gc/cms/cmsOopClosures.inline.hpp"
34 #include "gc/cms/compactibleFreeListSpace.hpp"
35 #include "gc/cms/concurrentMarkSweepGeneration.inline.hpp"
36 #include "gc/cms/concurrentMarkSweepThread.hpp"
37 #include "gc/cms/parNewGeneration.hpp"
38 #include "gc/cms/vmCMSOperations.hpp"
39 #include "gc/serial/genMarkSweep.hpp"
40 #include "gc/serial/tenuredGeneration.hpp"
41 #include "gc/shared/adaptiveSizePolicy.hpp"
42 #include "gc/shared/cardGeneration.inline.hpp"
43 #include "gc/shared/cardTableRS.hpp"
44 #include "gc/shared/collectedHeap.inline.hpp"
45 #include "gc/shared/collectorCounters.hpp"
46 #include "gc/shared/collectorPolicy.hpp"
47 #include "gc/shared/gcLocker.inline.hpp"
48 #include "gc/shared/gcPolicyCounters.hpp"
49 #include "gc/shared/gcTimer.hpp"
50 #include "gc/shared/gcTrace.hpp"
51 #include "gc/shared/gcTraceTime.inline.hpp"
52 #include "gc/shared/genCollectedHeap.hpp"
2310 // In this first variant of verification, we complete
2311 // all marking, then check if the new marks-vector is
2312 // a subset of the CMS marks-vector.
2313 verify_after_remark_work_1();
2314 } else {
2315 guarantee(CMSRemarkVerifyVariant == 2, "Range checking for CMSRemarkVerifyVariant should guarantee 1 or 2");
2316 // In this second variant of verification, we flag an error
2317 // (i.e. an object reachable in the new marks-vector not reachable
2318 // in the CMS marks-vector) immediately, also indicating the
2319 // identify of an object (A) that references the unmarked object (B) --
2320 // presumably, a mutation to A failed to be picked up by preclean/remark?
2321 verify_after_remark_work_2();
2322 }
2323
2324 return true;
2325 }
2326
2327 void CMSCollector::verify_after_remark_work_1() {
2328 ResourceMark rm;
2329 HandleMark hm;
2330 CMSHeap* gch = CMSHeap::heap();
2331
2332 // Get a clear set of claim bits for the roots processing to work with.
2333 ClassLoaderDataGraph::clear_claimed_marks();
2334
2335 // Mark from roots one level into CMS
2336 MarkRefsIntoClosure notOlder(_span, verification_mark_bm());
2337 gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2338
2339 {
2340 StrongRootsScope srs(1);
2341
2342 gch->cms_process_roots(&srs,
2343 true, // young gen as roots
2344 GenCollectedHeap::ScanningOption(roots_scanning_options()),
2345 should_unload_classes(),
2346 ¬Older,
2347 NULL);
2348 }
2349
2350 // Now mark from the roots
2379 }
2380
2381 class VerifyKlassOopsKlassClosure : public KlassClosure {
2382 class VerifyKlassOopsClosure : public OopClosure {
2383 CMSBitMap* _bitmap;
2384 public:
2385 VerifyKlassOopsClosure(CMSBitMap* bitmap) : _bitmap(bitmap) { }
2386 void do_oop(oop* p) { guarantee(*p == NULL || _bitmap->isMarked((HeapWord*) *p), "Should be marked"); }
2387 void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2388 } _oop_closure;
2389 public:
2390 VerifyKlassOopsKlassClosure(CMSBitMap* bitmap) : _oop_closure(bitmap) {}
2391 void do_klass(Klass* k) {
2392 k->oops_do(&_oop_closure);
2393 }
2394 };
2395
2396 void CMSCollector::verify_after_remark_work_2() {
2397 ResourceMark rm;
2398 HandleMark hm;
2399 CMSHeap* gch = CMSHeap::heap();
2400
2401 // Get a clear set of claim bits for the roots processing to work with.
2402 ClassLoaderDataGraph::clear_claimed_marks();
2403
2404 // Mark from roots one level into CMS
2405 MarkRefsIntoVerifyClosure notOlder(_span, verification_mark_bm(),
2406 markBitMap());
2407 CLDToOopClosure cld_closure(¬Older, true);
2408
2409 gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.
2410
2411 {
2412 StrongRootsScope srs(1);
2413
2414 gch->cms_process_roots(&srs,
2415 true, // young gen as roots
2416 GenCollectedHeap::ScanningOption(roots_scanning_options()),
2417 should_unload_classes(),
2418 ¬Older,
2419 &cld_closure);
2824
2825 // Already have locks.
2826 assert_lock_strong(bitMapLock());
2827 assert(_markBitMap.isAllClear(), "was reset at end of previous cycle");
2828
2829 // Setup the verification and class unloading state for this
2830 // CMS collection cycle.
2831 setup_cms_unloading_and_verification_state();
2832
2833 GCTraceTime(Trace, gc, phases) ts("checkpointRootsInitialWork", _gc_timer_cm);
2834
2835 // Reset all the PLAB chunk arrays if necessary.
2836 if (_survivor_plab_array != NULL && !CMSPLABRecordAlways) {
2837 reset_survivor_plab_arrays();
2838 }
2839
2840 ResourceMark rm;
2841 HandleMark hm;
2842
2843 MarkRefsIntoClosure notOlder(_span, &_markBitMap);
2844 CMSHeap* gch = CMSHeap::heap();
2845
2846 verify_work_stacks_empty();
2847 verify_overflow_empty();
2848
2849 gch->ensure_parsability(false); // fill TLABs, but no need to retire them
2850 // Update the saved marks which may affect the root scans.
2851 gch->save_marks();
2852
2853 // weak reference processing has not started yet.
2854 ref_processor()->set_enqueuing_is_done(false);
2855
2856 // Need to remember all newly created CLDs,
2857 // so that we can guarantee that the remark finds them.
2858 ClassLoaderDataGraph::remember_new_clds(true);
2859
2860 // Whenever a CLD is found, it will be claimed before proceeding to mark
2861 // the klasses. The claimed marks need to be cleared before marking starts.
2862 ClassLoaderDataGraph::clear_claimed_marks();
2863
2864 print_eden_and_survivor_chunk_arrays();
4242 }
4243
4244 _gc_tracer_cm->report_object_count_after_gc(&_is_alive_closure);
4245
4246 // Change under the freelistLocks.
4247 _collectorState = Sweeping;
4248 // Call isAllClear() under bitMapLock
4249 assert(_modUnionTable.isAllClear(),
4250 "Should be clear by end of the final marking");
4251 assert(_ct->klass_rem_set()->mod_union_is_clear(),
4252 "Should be clear by end of the final marking");
4253 }
4254
4255 void CMSParInitialMarkTask::work(uint worker_id) {
4256 elapsedTimer _timer;
4257 ResourceMark rm;
4258 HandleMark hm;
4259
4260 // ---------- scan from roots --------------
4261 _timer.start();
4262 CMSHeap* gch = CMSHeap::heap();
4263 ParMarkRefsIntoClosure par_mri_cl(_collector->_span, &(_collector->_markBitMap));
4264
4265 // ---------- young gen roots --------------
4266 {
4267 work_on_young_gen_roots(&par_mri_cl);
4268 _timer.stop();
4269 log_trace(gc, task)("Finished young gen initial mark scan work in %dth thread: %3.3f sec", worker_id, _timer.seconds());
4270 }
4271
4272 // ---------- remaining roots --------------
4273 _timer.reset();
4274 _timer.start();
4275
4276 CLDToOopClosure cld_closure(&par_mri_cl, true);
4277
4278 gch->cms_process_roots(_strong_roots_scope,
4279 false, // yg was scanned above
4280 GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),
4281 _collector->should_unload_classes(),
4282 &par_mri_cl,
4367 assert(ect <= _collector->_eden_chunk_capacity, "out of bounds");
4368 assert(sct <= _collector->_survivor_chunk_capacity, "out of bounds");
4369
4370 do_young_space_rescan(cl, to_space, NULL, 0);
4371 do_young_space_rescan(cl, from_space, sca, sct);
4372 do_young_space_rescan(cl, eden_space, eca, ect);
4373 }
4374
4375 // work_queue(i) is passed to the closure
4376 // ParMarkRefsIntoAndScanClosure. The "i" parameter
4377 // also is passed to do_dirty_card_rescan_tasks() and to
4378 // do_work_steal() to select the i-th task_queue.
4379
4380 void CMSParRemarkTask::work(uint worker_id) {
4381 elapsedTimer _timer;
4382 ResourceMark rm;
4383 HandleMark hm;
4384
4385 // ---------- rescan from roots --------------
4386 _timer.start();
4387 CMSHeap* gch = CMSHeap::heap();
4388 ParMarkRefsIntoAndScanClosure par_mrias_cl(_collector,
4389 _collector->_span, _collector->ref_processor(),
4390 &(_collector->_markBitMap),
4391 work_queue(worker_id));
4392
4393 // Rescan young gen roots first since these are likely
4394 // coarsely partitioned and may, on that account, constitute
4395 // the critical path; thus, it's best to start off that
4396 // work first.
4397 // ---------- young gen roots --------------
4398 {
4399 work_on_young_gen_roots(&par_mrias_cl);
4400 _timer.stop();
4401 log_trace(gc, task)("Finished young gen rescan work in %dth thread: %3.3f sec", worker_id, _timer.seconds());
4402 }
4403
4404 // ---------- remaining roots --------------
4405 _timer.reset();
4406 _timer.start();
4407 gch->cms_process_roots(_strong_roots_scope,
4819 pst->set_n_tasks(1);
4820 assert(pst->valid(), "Error");
4821 }
4822
4823 // From space
4824 {
4825 SequentialSubTasksDone* pst = _young_gen->from()->par_seq_tasks();
4826 assert(!pst->valid(), "Clobbering existing data?");
4827 size_t n_tasks = _survivor_chunk_index + 1;
4828 assert(n_tasks == 1 || _survivor_chunk_array != NULL, "Error");
4829 // Sets the condition for completion of the subtask (how many threads
4830 // need to finish in order to be done).
4831 pst->set_n_threads(n_threads);
4832 pst->set_n_tasks((int)n_tasks);
4833 assert(pst->valid(), "Error");
4834 }
4835 }
4836
4837 // Parallel version of remark
4838 void CMSCollector::do_remark_parallel() {
4839 CMSHeap* gch = CMSHeap::heap();
4840 WorkGang* workers = gch->workers();
4841 assert(workers != NULL, "Need parallel worker threads.");
4842 // Choose to use the number of GC workers most recently set
4843 // into "active_workers".
4844 uint n_workers = workers->active_workers();
4845
4846 CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();
4847
4848 StrongRootsScope srs(n_workers);
4849
4850 CMSParRemarkTask tsk(this, cms_space, n_workers, workers, task_queues(), &srs);
4851
4852 // We won't be iterating over the cards in the card table updating
4853 // the younger_gen cards, so we shouldn't call the following else
4854 // the verification code as well as subsequent younger_refs_iterate
4855 // code would get confused. XXX
4856 // gch->rem_set()->prepare_for_younger_refs_iterate(true); // parallel
4857
4858 // The young gen rescan work will not be done as part of
4859 // process_roots (which currently doesn't know how to
4878 if (n_workers > 1) {
4879 // Make refs discovery MT-safe, if it isn't already: it may not
4880 // necessarily be so, since it's possible that we are doing
4881 // ST marking.
4882 ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), true);
4883 workers->run_task(&tsk);
4884 } else {
4885 ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), false);
4886 tsk.work(0);
4887 }
4888
4889 // restore, single-threaded for now, any preserved marks
4890 // as a result of work_q overflow
4891 restore_preserved_marks_if_any();
4892 }
4893
4894 // Non-parallel version of remark
4895 void CMSCollector::do_remark_non_parallel() {
4896 ResourceMark rm;
4897 HandleMark hm;
4898 CMSHeap* gch = CMSHeap::heap();
4899 ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), false);
4900
4901 MarkRefsIntoAndScanClosure
4902 mrias_cl(_span, ref_processor(), &_markBitMap, NULL /* not precleaning */,
4903 &_markStack, this,
4904 false /* should_yield */, false /* not precleaning */);
4905 MarkFromDirtyCardsClosure
4906 markFromDirtyCardsClosure(this, _span,
4907 NULL, // space is set further below
4908 &_markBitMap, &_markStack, &mrias_cl);
4909 {
4910 GCTraceTime(Trace, gc, phases) t("Grey Object Rescan", _gc_timer_cm);
4911 // Iterate over the dirty cards, setting the corresponding bits in the
4912 // mod union table.
4913 {
4914 ModUnionClosure modUnionClosure(&_modUnionTable);
4915 _ct->ct_bs()->dirty_card_iterate(
4916 _cmsGen->used_region(),
4917 &modUnionClosure);
4918 }
5130 }
5131 // Verify that we have no work before we resort to stealing
5132 assert(work_q->size() == 0, "Have work, shouldn't steal");
5133 // Try to steal from other queues that have work
5134 if (task_queues()->steal(i, seed, /* reference */ obj_to_scan)) {
5135 NOT_PRODUCT(num_steals++;)
5136 assert(obj_to_scan->is_oop(), "Oops, not an oop!");
5137 assert(_mark_bit_map->isMarked((HeapWord*)obj_to_scan), "Stole an unmarked oop?");
5138 // Do scanning work
5139 obj_to_scan->oop_iterate(keep_alive);
5140 // Loop around, finish this work, and try to steal some more
5141 } else if (terminator()->offer_termination()) {
5142 break; // nirvana from the infinite cycle
5143 }
5144 }
5145 log_develop_trace(gc, task)("\t(%d: stole %d oops)", i, num_steals);
5146 }
5147
5148 void CMSRefProcTaskExecutor::execute(ProcessTask& task)
5149 {
5150 CMSHeap* gch = CMSHeap::heap();
5151 WorkGang* workers = gch->workers();
5152 assert(workers != NULL, "Need parallel worker threads.");
5153 CMSRefProcTaskProxy rp_task(task, &_collector,
5154 _collector.ref_processor()->span(),
5155 _collector.markBitMap(),
5156 workers, _collector.task_queues());
5157 workers->run_task(&rp_task);
5158 }
5159
5160 void CMSRefProcTaskExecutor::execute(EnqueueTask& task)
5161 {
5162
5163 CMSHeap* gch = CMSHeap::heap();
5164 WorkGang* workers = gch->workers();
5165 assert(workers != NULL, "Need parallel worker threads.");
5166 CMSRefEnqueueTaskProxy enq_task(task);
5167 workers->run_task(&enq_task);
5168 }
5169
5170 void CMSCollector::refProcessingWork() {
5171 ResourceMark rm;
5172 HandleMark hm;
5173
5174 ReferenceProcessor* rp = ref_processor();
5175 assert(rp->span().equals(_span), "Spans should be equal");
5176 assert(!rp->enqueuing_is_done(), "Enqueuing should not be complete");
5177 // Process weak references.
5178 rp->setup_policy(false);
5179 verify_work_stacks_empty();
5180
5181 CMSKeepAliveClosure cmsKeepAliveClosure(this, _span, &_markBitMap,
5182 &_markStack, false /* !preclean */);
5183 CMSDrainMarkingStackClosure cmsDrainMarkingStackClosure(this,
5184 _span, &_markBitMap, &_markStack,
5185 &cmsKeepAliveClosure, false /* !preclean */);
5186 {
5187 GCTraceTime(Debug, gc, phases) t("Reference Processing", _gc_timer_cm);
5188
5189 ReferenceProcessorStats stats;
5190 if (rp->processing_is_mt()) {
5191 // Set the degree of MT here. If the discovery is done MT, there
5192 // may have been a different number of threads doing the discovery
5193 // and a different number of discovered lists may have Ref objects.
5194 // That is OK as long as the Reference lists are balanced (see
5195 // balance_all_queues() and balance_queues()).
5196 CMSHeap* gch = CMSHeap::heap();
5197 uint active_workers = ParallelGCThreads;
5198 WorkGang* workers = gch->workers();
5199 if (workers != NULL) {
5200 active_workers = workers->active_workers();
5201 // The expectation is that active_workers will have already
5202 // been set to a reasonable value. If it has not been set,
5203 // investigate.
5204 assert(active_workers > 0, "Should have been set during scavenge");
5205 }
5206 rp->set_active_mt_degree(active_workers);
5207 CMSRefProcTaskExecutor task_executor(*this);
5208 stats = rp->process_discovered_references(&_is_alive_closure,
5209 &cmsKeepAliveClosure,
5210 &cmsDrainMarkingStackClosure,
5211 &task_executor,
5212 _gc_timer_cm);
5213 } else {
5214 stats = rp->process_discovered_references(&_is_alive_closure,
5215 &cmsKeepAliveClosure,
5216 &cmsDrainMarkingStackClosure,
|