28 #include "gc/cms/parNewGeneration.inline.hpp"
29 #include "gc/cms/parOopClosures.inline.hpp"
30 #include "gc/serial/defNewGeneration.inline.hpp"
31 #include "gc/shared/adaptiveSizePolicy.hpp"
32 #include "gc/shared/ageTable.hpp"
33 #include "gc/shared/copyFailedInfo.hpp"
34 #include "gc/shared/gcHeapSummary.hpp"
35 #include "gc/shared/gcTimer.hpp"
36 #include "gc/shared/gcTrace.hpp"
37 #include "gc/shared/gcTraceTime.hpp"
38 #include "gc/shared/genCollectedHeap.hpp"
39 #include "gc/shared/genOopClosures.inline.hpp"
40 #include "gc/shared/generation.hpp"
41 #include "gc/shared/plab.inline.hpp"
42 #include "gc/shared/referencePolicy.hpp"
43 #include "gc/shared/space.hpp"
44 #include "gc/shared/spaceDecorator.hpp"
45 #include "gc/shared/strongRootsScope.hpp"
46 #include "gc/shared/taskqueue.inline.hpp"
47 #include "gc/shared/workgroup.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "oops/objArrayOop.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "runtime/atomic.inline.hpp"
52 #include "runtime/handles.hpp"
53 #include "runtime/handles.inline.hpp"
54 #include "runtime/java.hpp"
55 #include "runtime/thread.inline.hpp"
56 #include "utilities/copy.hpp"
57 #include "utilities/globalDefinitions.hpp"
58 #include "utilities/stack.inline.hpp"
59
60 ParScanThreadState::ParScanThreadState(Space* to_space_,
61 ParNewGeneration* young_gen_,
62 Generation* old_gen_,
63 int thread_num_,
64 ObjToScanQueueSet* work_queue_set_,
65 Stack<oop, mtGC>* overflow_stacks_,
66 size_t desired_plab_sz_,
67 ParallelTaskTerminator& term_) :
281 // Initializes states for the specified number of threads;
282 ParScanThreadStateSet(int num_threads,
283 Space& to_space,
284 ParNewGeneration& young_gen,
285 Generation& old_gen,
286 ObjToScanQueueSet& queue_set,
287 Stack<oop, mtGC>* overflow_stacks_,
288 size_t desired_plab_sz,
289 ParallelTaskTerminator& term);
290
291 ~ParScanThreadStateSet() { TASKQUEUE_STATS_ONLY(reset_stats()); }
292
293 inline ParScanThreadState& thread_state(int i);
294
295 void trace_promotion_failed(const YoungGCTracer* gc_tracer);
296 void reset(uint active_workers, bool promotion_failed);
297 void flush();
298
299 #if TASKQUEUE_STATS
300 static void
301 print_termination_stats_hdr(outputStream* const st = gclog_or_tty);
302 void print_termination_stats(outputStream* const st = gclog_or_tty);
303 static void
304 print_taskqueue_stats_hdr(outputStream* const st = gclog_or_tty);
305 void print_taskqueue_stats(outputStream* const st = gclog_or_tty);
306 void reset_stats();
307 #endif // TASKQUEUE_STATS
308
309 private:
310 ParallelTaskTerminator& _term;
311 ParNewGeneration& _young_gen;
312 Generation& _old_gen;
313 public:
314 bool is_valid(int id) const { return id < length(); }
315 ParallelTaskTerminator* terminator() { return &_term; }
316 };
317
318 ParScanThreadStateSet::ParScanThreadStateSet(int num_threads,
319 Space& to_space,
320 ParNewGeneration& young_gen,
321 Generation& old_gen,
322 ObjToScanQueueSet& queue_set,
323 Stack<oop, mtGC>* overflow_stacks,
324 size_t desired_plab_sz,
325 ParallelTaskTerminator& term)
366 void ParScanThreadState::reset_stats() {
367 taskqueue_stats().reset();
368 _term_attempts = 0;
369 _overflow_refills = 0;
370 _overflow_refill_objs = 0;
371 }
372
373 void ParScanThreadStateSet::reset_stats() {
374 for (int i = 0; i < length(); ++i) {
375 thread_state(i).reset_stats();
376 }
377 }
378
379 void ParScanThreadStateSet::print_termination_stats_hdr(outputStream* const st) {
380 st->print_raw_cr("GC Termination Stats");
381 st->print_raw_cr(" elapsed --strong roots-- -------termination-------");
382 st->print_raw_cr("thr ms ms % ms % attempts");
383 st->print_raw_cr("--- --------- --------- ------ --------- ------ --------");
384 }
385
386 void ParScanThreadStateSet::print_termination_stats(outputStream* const st) {
387 print_termination_stats_hdr(st);
388
389 for (int i = 0; i < length(); ++i) {
390 const ParScanThreadState & pss = thread_state(i);
391 const double elapsed_ms = pss.elapsed_time() * 1000.0;
392 const double s_roots_ms = pss.strong_roots_time() * 1000.0;
393 const double term_ms = pss.term_time() * 1000.0;
394 st->print_cr("%3d %9.2f %9.2f %6.2f %9.2f %6.2f " SIZE_FORMAT_W(8),
395 i, elapsed_ms, s_roots_ms, s_roots_ms * 100 / elapsed_ms,
396 term_ms, term_ms * 100 / elapsed_ms, pss.term_attempts());
397 }
398 }
399
400 // Print stats related to work queue activity.
401 void ParScanThreadStateSet::print_taskqueue_stats_hdr(outputStream* const st) {
402 st->print_raw_cr("GC Task Stats");
403 st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr();
404 st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr();
405 }
406
407 void ParScanThreadStateSet::print_taskqueue_stats(outputStream* const st) {
408 print_taskqueue_stats_hdr(st);
409
410 TaskQueueStats totals;
411 for (int i = 0; i < length(); ++i) {
412 const ParScanThreadState & pss = thread_state(i);
413 const TaskQueueStats & stats = pss.taskqueue_stats();
414 st->print("%3d ", i); stats.print(st); st->cr();
415 totals += stats;
416
417 if (pss.overflow_refills() > 0) {
418 st->print_cr(" " SIZE_FORMAT_W(10) " overflow refills "
419 SIZE_FORMAT_W(10) " overflow objects",
420 pss.overflow_refills(), pss.overflow_refill_objs());
421 }
422 }
423 st->print("tot "); totals.print(st); st->cr();
424
425 DEBUG_ONLY(totals.verify());
426 }
427 #endif // TASKQUEUE_STATS
865
866 // If the next generation is too full to accommodate worst-case promotion
867 // from this generation, pass on collection; let the next generation
868 // do it.
869 if (!collection_attempt_is_safe()) {
870 gch->set_incremental_collection_failed(); // slight lie, in that we did not even attempt one
871 return;
872 }
873 assert(to()->is_empty(), "Else not collection_attempt_is_safe");
874
875 _gc_tracer.report_gc_start(gch->gc_cause(), _gc_timer->gc_start());
876 gch->trace_heap_before_gc(gc_tracer());
877
878 init_assuming_no_promotion_failure();
879
880 if (UseAdaptiveSizePolicy) {
881 set_survivor_overflow(false);
882 size_policy->minor_collection_begin();
883 }
884
885 GCTraceTime t1(GCCauseString("GC", gch->gc_cause()), PrintGC && !PrintGCDetails, true, NULL);
886 // Capture heap used before collection (for printing).
887 size_t gch_prev_used = gch->used();
888
889 age_table()->clear();
890 to()->clear(SpaceDecorator::Mangle);
891
892 gch->save_marks();
893
894 // Set the correct parallelism (number of queues) in the reference processor
895 ref_processor()->set_active_mt_degree(active_workers);
896
897 // Always set the terminator for the active number of workers
898 // because only those workers go through the termination protocol.
899 ParallelTaskTerminator _term(active_workers, task_queues());
900 ParScanThreadStateSet thread_state_set(active_workers,
901 *to(), *this, *_old_gen, *task_queues(),
902 _overflow_stacks, desired_plab_sz(), _term);
903
904 thread_state_set.reset(active_workers, promotion_failed());
905
906 {
907 StrongRootsScope srs(active_workers);
973 swap_spaces();
974
975 // A successful scavenge should restart the GC time limit count which is
976 // for full GC's.
977 size_policy->reset_gc_overhead_limit_count();
978
979 assert(to()->is_empty(), "to space should be empty now");
980
981 adjust_desired_tenuring_threshold();
982 } else {
983 handle_promotion_failed(gch, thread_state_set);
984 }
985 // set new iteration safe limit for the survivor spaces
986 from()->set_concurrent_iteration_safe_limit(from()->top());
987 to()->set_concurrent_iteration_safe_limit(to()->top());
988
989 if (ResizePLAB) {
990 plab_stats()->adjust_desired_plab_sz();
991 }
992
993 if (PrintGC && !PrintGCDetails) {
994 gch->print_heap_change(gch_prev_used);
995 }
996
997 TASKQUEUE_STATS_ONLY(if (PrintTerminationStats) thread_state_set.print_termination_stats());
998 TASKQUEUE_STATS_ONLY(if (PrintTaskqueue) thread_state_set.print_taskqueue_stats());
999
1000 if (UseAdaptiveSizePolicy) {
1001 size_policy->minor_collection_end(gch->gc_cause());
1002 size_policy->avg_survived()->sample(from()->used());
1003 }
1004
1005 // We need to use a monotonically non-decreasing time in ms
1006 // or we will see time-warp warnings and os::javaTimeMillis()
1007 // does not guarantee monotonicity.
1008 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
1009 update_time_of_last_gc(now);
1010
1011 rp->set_enqueuing_is_done(true);
1012 if (rp->processing_is_mt()) {
1013 ParNewRefProcTaskExecutor task_executor(*this, *_old_gen, thread_state_set);
1014 rp->enqueue_discovered_references(&task_executor);
1015 } else {
1016 rp->enqueue_discovered_references(NULL);
1017 }
1018 rp->verify_no_references_recorded();
1131
1132 preserve_mark_if_necessary(old, m);
1133 par_scan_state->register_promotion_failure(sz);
1134 }
1135
1136 old->forward_to(new_obj);
1137 forward_ptr = NULL;
1138 } else {
1139 // Is in to-space; do copying ourselves.
1140 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)new_obj, sz);
1141 assert(GenCollectedHeap::heap()->is_in_reserved(new_obj), "illegal forwarding pointer value.");
1142 forward_ptr = old->forward_to_atomic(new_obj);
1143 // Restore the mark word copied above.
1144 new_obj->set_mark(m);
1145 // Increment age if obj still in new generation
1146 new_obj->incr_age();
1147 par_scan_state->age_table()->add(new_obj, sz);
1148 }
1149 assert(new_obj != NULL, "just checking");
1150
1151 #ifndef PRODUCT
1152 // This code must come after the CAS test, or it will print incorrect
1153 // information.
1154 if (TraceScavenge) {
1155 gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",
1156 is_in_reserved(new_obj) ? "copying" : "tenuring",
1157 new_obj->klass()->internal_name(), p2i(old), p2i(new_obj), new_obj->size());
1158 }
1159 #endif
1160
1161 if (forward_ptr == NULL) {
1162 oop obj_to_push = new_obj;
1163 if (par_scan_state->should_be_partially_scanned(obj_to_push, old)) {
1164 // Length field used as index of next element to be scanned.
1165 // Real length can be obtained from real_forwardee()
1166 arrayOop(old)->set_length(0);
1167 obj_to_push = old;
1168 assert(obj_to_push->is_forwarded() && obj_to_push->forwardee() != obj_to_push,
1169 "push forwarded object");
1170 }
1171 // Push it on one of the queues of to-be-scanned objects.
1172 bool simulate_overflow = false;
1173 NOT_PRODUCT(
1174 if (ParGCWorkQueueOverflowALot && should_simulate_overflow()) {
1175 // simulate a stack overflow
1176 simulate_overflow = true;
1177 }
1178 )
1179 if (simulate_overflow || !par_scan_state->work_queue()->push(obj_to_push)) {
|
28 #include "gc/cms/parNewGeneration.inline.hpp"
29 #include "gc/cms/parOopClosures.inline.hpp"
30 #include "gc/serial/defNewGeneration.inline.hpp"
31 #include "gc/shared/adaptiveSizePolicy.hpp"
32 #include "gc/shared/ageTable.hpp"
33 #include "gc/shared/copyFailedInfo.hpp"
34 #include "gc/shared/gcHeapSummary.hpp"
35 #include "gc/shared/gcTimer.hpp"
36 #include "gc/shared/gcTrace.hpp"
37 #include "gc/shared/gcTraceTime.hpp"
38 #include "gc/shared/genCollectedHeap.hpp"
39 #include "gc/shared/genOopClosures.inline.hpp"
40 #include "gc/shared/generation.hpp"
41 #include "gc/shared/plab.inline.hpp"
42 #include "gc/shared/referencePolicy.hpp"
43 #include "gc/shared/space.hpp"
44 #include "gc/shared/spaceDecorator.hpp"
45 #include "gc/shared/strongRootsScope.hpp"
46 #include "gc/shared/taskqueue.inline.hpp"
47 #include "gc/shared/workgroup.hpp"
48 #include "logging/log.hpp"
49 #include "memory/resourceArea.hpp"
50 #include "oops/objArrayOop.hpp"
51 #include "oops/oop.inline.hpp"
52 #include "runtime/atomic.inline.hpp"
53 #include "runtime/handles.hpp"
54 #include "runtime/handles.inline.hpp"
55 #include "runtime/java.hpp"
56 #include "runtime/thread.inline.hpp"
57 #include "utilities/copy.hpp"
58 #include "utilities/globalDefinitions.hpp"
59 #include "utilities/stack.inline.hpp"
60
61 ParScanThreadState::ParScanThreadState(Space* to_space_,
62 ParNewGeneration* young_gen_,
63 Generation* old_gen_,
64 int thread_num_,
65 ObjToScanQueueSet* work_queue_set_,
66 Stack<oop, mtGC>* overflow_stacks_,
67 size_t desired_plab_sz_,
68 ParallelTaskTerminator& term_) :
282 // Initializes states for the specified number of threads;
283 ParScanThreadStateSet(int num_threads,
284 Space& to_space,
285 ParNewGeneration& young_gen,
286 Generation& old_gen,
287 ObjToScanQueueSet& queue_set,
288 Stack<oop, mtGC>* overflow_stacks_,
289 size_t desired_plab_sz,
290 ParallelTaskTerminator& term);
291
292 ~ParScanThreadStateSet() { TASKQUEUE_STATS_ONLY(reset_stats()); }
293
294 inline ParScanThreadState& thread_state(int i);
295
296 void trace_promotion_failed(const YoungGCTracer* gc_tracer);
297 void reset(uint active_workers, bool promotion_failed);
298 void flush();
299
300 #if TASKQUEUE_STATS
301 static void
302 print_termination_stats_hdr(outputStream* const st);
303 void print_termination_stats();
304 static void
305 print_taskqueue_stats_hdr(outputStream* const st);
306 void print_taskqueue_stats();
307 void reset_stats();
308 #endif // TASKQUEUE_STATS
309
310 private:
311 ParallelTaskTerminator& _term;
312 ParNewGeneration& _young_gen;
313 Generation& _old_gen;
314 public:
315 bool is_valid(int id) const { return id < length(); }
316 ParallelTaskTerminator* terminator() { return &_term; }
317 };
318
319 ParScanThreadStateSet::ParScanThreadStateSet(int num_threads,
320 Space& to_space,
321 ParNewGeneration& young_gen,
322 Generation& old_gen,
323 ObjToScanQueueSet& queue_set,
324 Stack<oop, mtGC>* overflow_stacks,
325 size_t desired_plab_sz,
326 ParallelTaskTerminator& term)
367 void ParScanThreadState::reset_stats() {
368 taskqueue_stats().reset();
369 _term_attempts = 0;
370 _overflow_refills = 0;
371 _overflow_refill_objs = 0;
372 }
373
374 void ParScanThreadStateSet::reset_stats() {
375 for (int i = 0; i < length(); ++i) {
376 thread_state(i).reset_stats();
377 }
378 }
379
380 void ParScanThreadStateSet::print_termination_stats_hdr(outputStream* const st) {
381 st->print_raw_cr("GC Termination Stats");
382 st->print_raw_cr(" elapsed --strong roots-- -------termination-------");
383 st->print_raw_cr("thr ms ms % ms % attempts");
384 st->print_raw_cr("--- --------- --------- ------ --------- ------ --------");
385 }
386
387 void ParScanThreadStateSet::print_termination_stats() {
388 LogHandle(gc, task, stats) log;
389 if (!log.is_debug()) {
390 return;
391 }
392
393 ResourceMark rm;
394 outputStream* st = log.debug_stream();
395
396 print_termination_stats_hdr(st);
397
398 for (int i = 0; i < length(); ++i) {
399 const ParScanThreadState & pss = thread_state(i);
400 const double elapsed_ms = pss.elapsed_time() * 1000.0;
401 const double s_roots_ms = pss.strong_roots_time() * 1000.0;
402 const double term_ms = pss.term_time() * 1000.0;
403 st->print_cr("%3d %9.2f %9.2f %6.2f %9.2f %6.2f " SIZE_FORMAT_W(8),
404 i, elapsed_ms, s_roots_ms, s_roots_ms * 100 / elapsed_ms,
405 term_ms, term_ms * 100 / elapsed_ms, pss.term_attempts());
406 }
407 }
408
409 // Print stats related to work queue activity.
410 void ParScanThreadStateSet::print_taskqueue_stats_hdr(outputStream* const st) {
411 st->print_raw_cr("GC Task Stats");
412 st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr();
413 st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr();
414 }
415
416 void ParScanThreadStateSet::print_taskqueue_stats() {
417 LogHandle(gc, task, stats) log;
418 if (!log.is_develop()) {
419 return;
420 }
421 ResourceMark rm;
422 outputStream* st = log.develop_stream();
423 print_taskqueue_stats_hdr(st);
424
425 TaskQueueStats totals;
426 for (int i = 0; i < length(); ++i) {
427 const ParScanThreadState & pss = thread_state(i);
428 const TaskQueueStats & stats = pss.taskqueue_stats();
429 st->print("%3d ", i); stats.print(st); st->cr();
430 totals += stats;
431
432 if (pss.overflow_refills() > 0) {
433 st->print_cr(" " SIZE_FORMAT_W(10) " overflow refills "
434 SIZE_FORMAT_W(10) " overflow objects",
435 pss.overflow_refills(), pss.overflow_refill_objs());
436 }
437 }
438 st->print("tot "); totals.print(st); st->cr();
439
440 DEBUG_ONLY(totals.verify());
441 }
442 #endif // TASKQUEUE_STATS
880
881 // If the next generation is too full to accommodate worst-case promotion
882 // from this generation, pass on collection; let the next generation
883 // do it.
884 if (!collection_attempt_is_safe()) {
885 gch->set_incremental_collection_failed(); // slight lie, in that we did not even attempt one
886 return;
887 }
888 assert(to()->is_empty(), "Else not collection_attempt_is_safe");
889
890 _gc_tracer.report_gc_start(gch->gc_cause(), _gc_timer->gc_start());
891 gch->trace_heap_before_gc(gc_tracer());
892
893 init_assuming_no_promotion_failure();
894
895 if (UseAdaptiveSizePolicy) {
896 set_survivor_overflow(false);
897 size_policy->minor_collection_begin();
898 }
899
900 GCTraceTime(Trace, gc) t1("ParNew", NULL, gch->gc_cause());
901
902 age_table()->clear();
903 to()->clear(SpaceDecorator::Mangle);
904
905 gch->save_marks();
906
907 // Set the correct parallelism (number of queues) in the reference processor
908 ref_processor()->set_active_mt_degree(active_workers);
909
910 // Always set the terminator for the active number of workers
911 // because only those workers go through the termination protocol.
912 ParallelTaskTerminator _term(active_workers, task_queues());
913 ParScanThreadStateSet thread_state_set(active_workers,
914 *to(), *this, *_old_gen, *task_queues(),
915 _overflow_stacks, desired_plab_sz(), _term);
916
917 thread_state_set.reset(active_workers, promotion_failed());
918
919 {
920 StrongRootsScope srs(active_workers);
986 swap_spaces();
987
988 // A successful scavenge should restart the GC time limit count which is
989 // for full GC's.
990 size_policy->reset_gc_overhead_limit_count();
991
992 assert(to()->is_empty(), "to space should be empty now");
993
994 adjust_desired_tenuring_threshold();
995 } else {
996 handle_promotion_failed(gch, thread_state_set);
997 }
998 // set new iteration safe limit for the survivor spaces
999 from()->set_concurrent_iteration_safe_limit(from()->top());
1000 to()->set_concurrent_iteration_safe_limit(to()->top());
1001
1002 if (ResizePLAB) {
1003 plab_stats()->adjust_desired_plab_sz();
1004 }
1005
1006 TASKQUEUE_STATS_ONLY(thread_state_set.print_termination_stats());
1007 TASKQUEUE_STATS_ONLY(thread_state_set.print_taskqueue_stats());
1008
1009 if (UseAdaptiveSizePolicy) {
1010 size_policy->minor_collection_end(gch->gc_cause());
1011 size_policy->avg_survived()->sample(from()->used());
1012 }
1013
1014 // We need to use a monotonically non-decreasing time in ms
1015 // or we will see time-warp warnings and os::javaTimeMillis()
1016 // does not guarantee monotonicity.
1017 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
1018 update_time_of_last_gc(now);
1019
1020 rp->set_enqueuing_is_done(true);
1021 if (rp->processing_is_mt()) {
1022 ParNewRefProcTaskExecutor task_executor(*this, *_old_gen, thread_state_set);
1023 rp->enqueue_discovered_references(&task_executor);
1024 } else {
1025 rp->enqueue_discovered_references(NULL);
1026 }
1027 rp->verify_no_references_recorded();
1140
1141 preserve_mark_if_necessary(old, m);
1142 par_scan_state->register_promotion_failure(sz);
1143 }
1144
1145 old->forward_to(new_obj);
1146 forward_ptr = NULL;
1147 } else {
1148 // Is in to-space; do copying ourselves.
1149 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)new_obj, sz);
1150 assert(GenCollectedHeap::heap()->is_in_reserved(new_obj), "illegal forwarding pointer value.");
1151 forward_ptr = old->forward_to_atomic(new_obj);
1152 // Restore the mark word copied above.
1153 new_obj->set_mark(m);
1154 // Increment age if obj still in new generation
1155 new_obj->incr_age();
1156 par_scan_state->age_table()->add(new_obj, sz);
1157 }
1158 assert(new_obj != NULL, "just checking");
1159
1160 // This code must come after the CAS test, or it will print incorrect
1161 // information.
1162 log_develop(gc, scavenge)("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",
1163 is_in_reserved(new_obj) ? "copying" : "tenuring",
1164 new_obj->klass()->internal_name(), p2i(old), p2i(new_obj), new_obj->size());
1165
1166 if (forward_ptr == NULL) {
1167 oop obj_to_push = new_obj;
1168 if (par_scan_state->should_be_partially_scanned(obj_to_push, old)) {
1169 // Length field used as index of next element to be scanned.
1170 // Real length can be obtained from real_forwardee()
1171 arrayOop(old)->set_length(0);
1172 obj_to_push = old;
1173 assert(obj_to_push->is_forwarded() && obj_to_push->forwardee() != obj_to_push,
1174 "push forwarded object");
1175 }
1176 // Push it on one of the queues of to-be-scanned objects.
1177 bool simulate_overflow = false;
1178 NOT_PRODUCT(
1179 if (ParGCWorkQueueOverflowALot && should_simulate_overflow()) {
1180 // simulate a stack overflow
1181 simulate_overflow = true;
1182 }
1183 )
1184 if (simulate_overflow || !par_scan_state->work_queue()->push(obj_to_push)) {
|