1 /* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 "gc/g1/g1CollectedHeap.inline.hpp" 27 #include "gc/g1/g1GCPhaseTimes.hpp" 28 #include "gc/g1/g1HotCardCache.hpp" 29 #include "gc/g1/g1StringDedup.hpp" 30 #include "gc/g1/workerDataArray.inline.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "logging/log.hpp" 33 #include "logging/logStream.hpp" 34 #include "runtime/timer.hpp" 35 #include "runtime/os.hpp" 36 37 static const char* Indents[5] = {"", " ", " ", " ", " "}; 38 39 G1GCPhaseTimes::G1GCPhaseTimes(uint max_gc_threads) : 40 _max_gc_threads(max_gc_threads), 41 _gc_start_counter(0), 42 _gc_pause_time_ms(0.0) 43 { 44 assert(max_gc_threads > 0, "Must have some GC threads"); 45 46 _gc_par_phases[GCWorkerStart] = new WorkerDataArray<double>(max_gc_threads, "GC Worker Start (ms):"); 47 _gc_par_phases[ExtRootScan] = new WorkerDataArray<double>(max_gc_threads, "Ext Root Scanning (ms):"); 48 49 // Root scanning phases 50 _gc_par_phases[ThreadRoots] = new WorkerDataArray<double>(max_gc_threads, "Thread Roots (ms):"); 51 _gc_par_phases[StringTableRoots] = new WorkerDataArray<double>(max_gc_threads, "StringTable Roots (ms):"); 52 _gc_par_phases[UniverseRoots] = new WorkerDataArray<double>(max_gc_threads, "Universe Roots (ms):"); 53 _gc_par_phases[JNIRoots] = new WorkerDataArray<double>(max_gc_threads, "JNI Handles Roots (ms):"); 54 _gc_par_phases[ObjectSynchronizerRoots] = new WorkerDataArray<double>(max_gc_threads, "ObjectSynchronizer Roots (ms):"); 55 _gc_par_phases[FlatProfilerRoots] = new WorkerDataArray<double>(max_gc_threads, "FlatProfiler Roots (ms):"); 56 _gc_par_phases[ManagementRoots] = new WorkerDataArray<double>(max_gc_threads, "Management Roots (ms):"); 57 _gc_par_phases[SystemDictionaryRoots] = new WorkerDataArray<double>(max_gc_threads, "SystemDictionary Roots (ms):"); 58 _gc_par_phases[CLDGRoots] = new WorkerDataArray<double>(max_gc_threads, "CLDG Roots (ms):"); 59 _gc_par_phases[JVMTIRoots] = new WorkerDataArray<double>(max_gc_threads, "JVMTI Roots (ms):"); 60 _gc_par_phases[CMRefRoots] = new WorkerDataArray<double>(max_gc_threads, "CM RefProcessor Roots (ms):"); 61 _gc_par_phases[WaitForStrongCLD] = new WorkerDataArray<double>(max_gc_threads, "Wait For Strong CLD (ms):"); 62 _gc_par_phases[WeakCLDRoots] = new WorkerDataArray<double>(max_gc_threads, "Weak CLD Roots (ms):"); 63 _gc_par_phases[SATBFiltering] = new WorkerDataArray<double>(max_gc_threads, "SATB Filtering (ms):"); 64 65 _gc_par_phases[UpdateRS] = new WorkerDataArray<double>(max_gc_threads, "Update RS (ms):"); 66 if (G1HotCardCache::default_use_cache()) { 67 _gc_par_phases[ScanHCC] = new WorkerDataArray<double>(max_gc_threads, "Scan HCC (ms):"); 68 } else { 69 _gc_par_phases[ScanHCC] = NULL; 70 } 71 _gc_par_phases[ScanRS] = new WorkerDataArray<double>(max_gc_threads, "Scan RS (ms):"); 72 _gc_par_phases[CodeRoots] = new WorkerDataArray<double>(max_gc_threads, "Code Root Scanning (ms):"); 73 #if INCLUDE_AOT 74 _gc_par_phases[AOTCodeRoots] = new WorkerDataArray<double>(max_gc_threads, "AOT Root Scanning (ms):"); 75 #endif 76 _gc_par_phases[ObjCopy] = new WorkerDataArray<double>(max_gc_threads, "Object Copy (ms):"); 77 _gc_par_phases[Termination] = new WorkerDataArray<double>(max_gc_threads, "Termination (ms):"); 78 _gc_par_phases[GCWorkerTotal] = new WorkerDataArray<double>(max_gc_threads, "GC Worker Total (ms):"); 79 _gc_par_phases[GCWorkerEnd] = new WorkerDataArray<double>(max_gc_threads, "GC Worker End (ms):"); 80 _gc_par_phases[Other] = new WorkerDataArray<double>(max_gc_threads, "GC Worker Other (ms):"); 81 82 _update_rs_processed_buffers = new WorkerDataArray<size_t>(max_gc_threads, "Processed Buffers:"); 83 _gc_par_phases[UpdateRS]->link_thread_work_items(_update_rs_processed_buffers); 84 85 _termination_attempts = new WorkerDataArray<size_t>(max_gc_threads, "Termination Attempts:"); 86 _gc_par_phases[Termination]->link_thread_work_items(_termination_attempts); 87 88 if (UseStringDeduplication) { 89 _gc_par_phases[StringDedupQueueFixup] = new WorkerDataArray<double>(max_gc_threads, "Queue Fixup (ms):"); 90 _gc_par_phases[StringDedupTableFixup] = new WorkerDataArray<double>(max_gc_threads, "Table Fixup (ms):"); 91 } else { 92 _gc_par_phases[StringDedupQueueFixup] = NULL; 93 _gc_par_phases[StringDedupTableFixup] = NULL; 94 } 95 96 _gc_par_phases[RedirtyCards] = new WorkerDataArray<double>(max_gc_threads, "Parallel Redirty (ms):"); 97 _redirtied_cards = new WorkerDataArray<size_t>(max_gc_threads, "Redirtied Cards:"); 98 _gc_par_phases[RedirtyCards]->link_thread_work_items(_redirtied_cards); 99 100 _gc_par_phases[YoungFreeCSet] = new WorkerDataArray<double>(max_gc_threads, "Young Free Collection Set (ms):"); 101 _gc_par_phases[NonYoungFreeCSet] = new WorkerDataArray<double>(max_gc_threads, "Non-Young Free Collection Set (ms):"); 102 103 _gc_par_phases[PreserveCMReferents] = new WorkerDataArray<double>(max_gc_threads, "Parallel Preserve CM Refs (ms):"); 104 105 reset(); 106 } 107 108 void G1GCPhaseTimes::reset() { 109 _cur_collection_par_time_ms = 0.0; 110 _cur_collection_code_root_fixup_time_ms = 0.0; 111 _cur_strong_code_root_purge_time_ms = 0.0; 112 _cur_evac_fail_recalc_used = 0.0; 113 _cur_evac_fail_restore_remsets = 0.0; 114 _cur_evac_fail_remove_self_forwards = 0.0; 115 _cur_string_dedup_fixup_time_ms = 0.0; 116 _cur_prepare_tlab_time_ms = 0.0; 117 _cur_resize_tlab_time_ms = 0.0; 118 _cur_derived_pointer_table_update_time_ms = 0.0; 119 _cur_clear_ct_time_ms = 0.0; 120 _cur_expand_heap_time_ms = 0.0; 121 _cur_ref_proc_time_ms = 0.0; 122 _cur_ref_enq_time_ms = 0.0; 123 _cur_collection_start_sec = 0.0; 124 _root_region_scan_wait_time_ms = 0.0; 125 _external_accounted_time_ms = 0.0; 126 _recorded_clear_claimed_marks_time_ms = 0.0; 127 _recorded_young_cset_choice_time_ms = 0.0; 128 _recorded_non_young_cset_choice_time_ms = 0.0; 129 _recorded_redirty_logged_cards_time_ms = 0.0; 130 _recorded_preserve_cm_referents_time_ms = 0.0; 131 _recorded_merge_pss_time_ms = 0.0; 132 _recorded_start_new_cset_time_ms = 0.0; 133 _recorded_total_free_cset_time_ms = 0.0; 134 _recorded_serial_free_cset_time_ms = 0.0; 135 _cur_fast_reclaim_humongous_time_ms = 0.0; 136 _cur_fast_reclaim_humongous_register_time_ms = 0.0; 137 _cur_fast_reclaim_humongous_total = 0; 138 _cur_fast_reclaim_humongous_candidates = 0; 139 _cur_fast_reclaim_humongous_reclaimed = 0; 140 _cur_verify_before_time_ms = 0.0; 141 _cur_verify_after_time_ms = 0.0; 142 143 for (int i = 0; i < GCParPhasesSentinel; i++) { 144 if (_gc_par_phases[i] != NULL) { 145 _gc_par_phases[i]->reset(); 146 } 147 } 148 } 149 150 void G1GCPhaseTimes::note_gc_start() { 151 _gc_start_counter = os::elapsed_counter(); 152 reset(); 153 } 154 155 #define ASSERT_PHASE_UNINITIALIZED(phase) \ 156 assert(_gc_par_phases[phase]->get(i) == uninitialized, "Phase " #phase " reported for thread that was not started"); 157 158 double G1GCPhaseTimes::worker_time(GCParPhases phase, uint worker) { 159 double value = _gc_par_phases[phase]->get(worker); 160 if (value != WorkerDataArray<double>::uninitialized()) { 161 return value; 162 } 163 return 0.0; 164 } 165 166 void G1GCPhaseTimes::note_gc_end() { 167 _gc_pause_time_ms = TimeHelper::counter_to_millis(os::elapsed_counter() - _gc_start_counter); 168 169 double uninitialized = WorkerDataArray<double>::uninitialized(); 170 171 for (uint i = 0; i < _max_gc_threads; i++) { 172 double worker_start = _gc_par_phases[GCWorkerStart]->get(i); 173 if (worker_start != uninitialized) { 174 assert(_gc_par_phases[GCWorkerEnd]->get(i) != uninitialized, "Worker started but not ended."); 175 double total_worker_time = _gc_par_phases[GCWorkerEnd]->get(i) - _gc_par_phases[GCWorkerStart]->get(i); 176 record_time_secs(GCWorkerTotal, i , total_worker_time); 177 178 double worker_known_time = 179 worker_time(ExtRootScan, i) 180 + worker_time(SATBFiltering, i) 181 + worker_time(UpdateRS, i) 182 + worker_time(ScanRS, i) 183 + worker_time(CodeRoots, i) 184 + worker_time(ObjCopy, i) 185 + worker_time(Termination, i); 186 187 record_time_secs(Other, i, total_worker_time - worker_known_time); 188 } else { 189 // Make sure all slots are uninitialized since this thread did not seem to have been started 190 ASSERT_PHASE_UNINITIALIZED(GCWorkerEnd); 191 ASSERT_PHASE_UNINITIALIZED(ExtRootScan); 192 ASSERT_PHASE_UNINITIALIZED(SATBFiltering); 193 ASSERT_PHASE_UNINITIALIZED(UpdateRS); 194 ASSERT_PHASE_UNINITIALIZED(ScanRS); 195 ASSERT_PHASE_UNINITIALIZED(CodeRoots); 196 ASSERT_PHASE_UNINITIALIZED(ObjCopy); 197 ASSERT_PHASE_UNINITIALIZED(Termination); 198 } 199 } 200 } 201 202 #undef ASSERT_PHASE_UNINITIALIZED 203 204 // record the time a phase took in seconds 205 void G1GCPhaseTimes::record_time_secs(GCParPhases phase, uint worker_i, double secs) { 206 _gc_par_phases[phase]->set(worker_i, secs); 207 } 208 209 // add a number of seconds to a phase 210 void G1GCPhaseTimes::add_time_secs(GCParPhases phase, uint worker_i, double secs) { 211 _gc_par_phases[phase]->add(worker_i, secs); 212 } 213 214 void G1GCPhaseTimes::record_thread_work_item(GCParPhases phase, uint worker_i, size_t count) { 215 _gc_par_phases[phase]->set_thread_work_item(worker_i, count); 216 } 217 218 // return the average time for a phase in milliseconds 219 double G1GCPhaseTimes::average_time_ms(GCParPhases phase) { 220 return _gc_par_phases[phase]->average() * 1000.0; 221 } 222 223 size_t G1GCPhaseTimes::sum_thread_work_items(GCParPhases phase) { 224 assert(_gc_par_phases[phase]->thread_work_items() != NULL, "No sub count"); 225 return _gc_par_phases[phase]->thread_work_items()->sum(); 226 } 227 228 template <class T> 229 void G1GCPhaseTimes::details(T* phase, const char* indent) const { 230 LogTarget(Trace, gc, phases, task) lt; 231 if (lt.is_enabled()) { 232 LogStream ls(lt); 233 ls.print("%s", indent); 234 phase->print_details_on(&ls); 235 } 236 } 237 238 void G1GCPhaseTimes::log_phase(WorkerDataArray<double>* phase, uint indent, outputStream* out, bool print_sum) const { 239 out->print("%s", Indents[indent]); 240 phase->print_summary_on(out, print_sum); 241 details(phase, Indents[indent]); 242 243 WorkerDataArray<size_t>* work_items = phase->thread_work_items(); 244 if (work_items != NULL) { 245 out->print("%s", Indents[indent + 1]); 246 work_items->print_summary_on(out, true); 247 details(work_items, Indents[indent + 1]); 248 } 249 } 250 251 void G1GCPhaseTimes::debug_phase(WorkerDataArray<double>* phase) const { 252 Log(gc, phases) log; 253 if (log.is_level(LogLevel::Debug)) { 254 ResourceMark rm; 255 LogStream ls(log.debug()); 256 log_phase(phase, 2, &ls, true); 257 } 258 } 259 260 void G1GCPhaseTimes::trace_phase(WorkerDataArray<double>* phase, bool print_sum) const { 261 LogTarget(Trace, gc, phases) lt; 262 if (lt.is_enabled()) { 263 LogStream ls(lt); 264 log_phase(phase, 3, &ls, print_sum); 265 } 266 } 267 268 #define TIME_FORMAT "%.1lfms" 269 270 void G1GCPhaseTimes::info_time(const char* name, double value) const { 271 log_info(gc, phases)("%s%s: " TIME_FORMAT, Indents[1], name, value); 272 } 273 274 void G1GCPhaseTimes::debug_time(const char* name, double value) const { 275 log_debug(gc, phases)("%s%s: " TIME_FORMAT, Indents[2], name, value); 276 } 277 278 void G1GCPhaseTimes::trace_time(const char* name, double value) const { 279 log_trace(gc, phases)("%s%s: " TIME_FORMAT, Indents[3], name, value); 280 } 281 282 void G1GCPhaseTimes::trace_count(const char* name, size_t value) const { 283 log_trace(gc, phases)("%s%s: " SIZE_FORMAT, Indents[3], name, value); 284 } 285 286 double G1GCPhaseTimes::print_pre_evacuate_collection_set() const { 287 const double sum_ms = _root_region_scan_wait_time_ms + 288 _recorded_young_cset_choice_time_ms + 289 _recorded_non_young_cset_choice_time_ms + 290 _cur_fast_reclaim_humongous_register_time_ms + 291 _recorded_clear_claimed_marks_time_ms; 292 293 info_time("Pre Evacuate Collection Set", sum_ms); 294 295 if (_root_region_scan_wait_time_ms > 0.0) { 296 debug_time("Root Region Scan Waiting", _root_region_scan_wait_time_ms); 297 } 298 debug_time("Prepare TLABs", _cur_prepare_tlab_time_ms); 299 debug_time("Choose Collection Set", (_recorded_young_cset_choice_time_ms + _recorded_non_young_cset_choice_time_ms)); 300 if (G1EagerReclaimHumongousObjects) { 301 debug_time("Humongous Register", _cur_fast_reclaim_humongous_register_time_ms); 302 trace_count("Humongous Total", _cur_fast_reclaim_humongous_total); 303 trace_count("Humongous Candidate", _cur_fast_reclaim_humongous_candidates); 304 } 305 306 if (_recorded_clear_claimed_marks_time_ms > 0.0) { 307 debug_time("Clear Claimed Marks", _recorded_clear_claimed_marks_time_ms); 308 } 309 return sum_ms; 310 } 311 312 double G1GCPhaseTimes::print_evacuate_collection_set() const { 313 const double sum_ms = _cur_collection_par_time_ms; 314 315 info_time("Evacuate Collection Set", sum_ms); 316 317 trace_phase(_gc_par_phases[GCWorkerStart], false); 318 debug_phase(_gc_par_phases[ExtRootScan]); 319 for (int i = ThreadRoots; i <= SATBFiltering; i++) { 320 trace_phase(_gc_par_phases[i]); 321 } 322 debug_phase(_gc_par_phases[UpdateRS]); 323 if (G1HotCardCache::default_use_cache()) { 324 trace_phase(_gc_par_phases[ScanHCC]); 325 } 326 debug_phase(_gc_par_phases[ScanRS]); 327 debug_phase(_gc_par_phases[CodeRoots]); 328 #if INCLUDE_AOT 329 debug_phase(_gc_par_phases[AOTCodeRoots]); 330 #endif 331 debug_phase(_gc_par_phases[ObjCopy]); 332 debug_phase(_gc_par_phases[Termination]); 333 debug_phase(_gc_par_phases[Other]); 334 debug_phase(_gc_par_phases[GCWorkerTotal]); 335 trace_phase(_gc_par_phases[GCWorkerEnd], false); 336 337 return sum_ms; 338 } 339 340 double G1GCPhaseTimes::print_post_evacuate_collection_set() const { 341 const double evac_fail_handling = _cur_evac_fail_recalc_used + 342 _cur_evac_fail_remove_self_forwards + 343 _cur_evac_fail_restore_remsets; 344 const double sum_ms = evac_fail_handling + 345 _cur_collection_code_root_fixup_time_ms + 346 _recorded_preserve_cm_referents_time_ms + 347 _cur_ref_proc_time_ms + 348 _cur_ref_enq_time_ms + 349 _cur_clear_ct_time_ms + 350 _recorded_merge_pss_time_ms + 351 _cur_strong_code_root_purge_time_ms + 352 _recorded_redirty_logged_cards_time_ms + 353 _recorded_total_free_cset_time_ms + 354 _cur_fast_reclaim_humongous_time_ms + 355 _cur_expand_heap_time_ms + 356 _cur_string_dedup_fixup_time_ms; 357 358 info_time("Post Evacuate Collection Set", sum_ms); 359 360 debug_time("Code Roots Fixup", _cur_collection_code_root_fixup_time_ms); 361 362 debug_time("Preserve CM Refs", _recorded_preserve_cm_referents_time_ms); 363 trace_phase(_gc_par_phases[PreserveCMReferents]); 364 365 debug_time("Reference Processing", _cur_ref_proc_time_ms); 366 367 if (G1StringDedup::is_enabled()) { 368 debug_time("String Dedup Fixup", _cur_string_dedup_fixup_time_ms); 369 debug_phase(_gc_par_phases[StringDedupQueueFixup]); 370 debug_phase(_gc_par_phases[StringDedupTableFixup]); 371 } 372 373 debug_time("Clear Card Table", _cur_clear_ct_time_ms); 374 375 if (G1CollectedHeap::heap()->evacuation_failed()) { 376 debug_time("Evacuation Failure", evac_fail_handling); 377 trace_time("Recalculate Used", _cur_evac_fail_recalc_used); 378 trace_time("Remove Self Forwards",_cur_evac_fail_remove_self_forwards); 379 trace_time("Restore RemSet", _cur_evac_fail_restore_remsets); 380 } 381 382 debug_time("Reference Enqueuing", _cur_ref_enq_time_ms); 383 384 debug_time("Merge Per-Thread State", _recorded_merge_pss_time_ms); 385 debug_time("Code Roots Purge", _cur_strong_code_root_purge_time_ms); 386 387 debug_time("Redirty Cards", _recorded_redirty_logged_cards_time_ms); 388 trace_phase(_gc_par_phases[RedirtyCards]); 389 #if defined(COMPILER2) || INCLUDE_JVMCI 390 debug_time("DerivedPointerTable Update", _cur_derived_pointer_table_update_time_ms); 391 #endif 392 393 debug_time("Free Collection Set", _recorded_total_free_cset_time_ms); 394 trace_time("Free Collection Set Serial", _recorded_serial_free_cset_time_ms); 395 trace_phase(_gc_par_phases[YoungFreeCSet]); 396 trace_phase(_gc_par_phases[NonYoungFreeCSet]); 397 398 if (G1EagerReclaimHumongousObjects) { 399 debug_time("Humongous Reclaim", _cur_fast_reclaim_humongous_time_ms); 400 trace_count("Humongous Reclaimed", _cur_fast_reclaim_humongous_reclaimed); 401 } 402 debug_time("Start New Collection Set", _recorded_start_new_cset_time_ms); 403 if (UseTLAB && ResizeTLAB) { 404 debug_time("Resize TLABs", _cur_resize_tlab_time_ms); 405 } 406 debug_time("Expand Heap After Collection", _cur_expand_heap_time_ms); 407 408 409 return sum_ms; 410 } 411 412 void G1GCPhaseTimes::print_other(double accounted_ms) const { 413 info_time("Other", _gc_pause_time_ms - accounted_ms); 414 } 415 416 void G1GCPhaseTimes::print() { 417 note_gc_end(); 418 419 if (_cur_verify_before_time_ms > 0.0) { 420 debug_time("Verify Before", _cur_verify_before_time_ms); 421 } 422 423 double accounted_ms = 0.0; 424 accounted_ms += print_pre_evacuate_collection_set(); 425 accounted_ms += print_evacuate_collection_set(); 426 accounted_ms += print_post_evacuate_collection_set(); 427 print_other(accounted_ms); 428 429 if (_cur_verify_after_time_ms > 0.0) { 430 debug_time("Verify After", _cur_verify_after_time_ms); 431 } 432 } 433 434 G1GCParPhaseTimesTracker::G1GCParPhaseTimesTracker(G1GCPhaseTimes* phase_times, G1GCPhaseTimes::GCParPhases phase, uint worker_id) : 435 _phase_times(phase_times), _phase(phase), _worker_id(worker_id) { 436 if (_phase_times != NULL) { 437 _start_time = os::elapsedTime(); 438 } 439 } 440 441 G1GCParPhaseTimesTracker::~G1GCParPhaseTimesTracker() { 442 if (_phase_times != NULL) { 443 _phase_times->record_time_secs(_phase, _worker_id, os::elapsedTime() - _start_time); 444 } 445 } 446