1 /* 2 * Copyright (c) 2012, 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 26 #include "precompiled.hpp" 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 28 #include "gc_implementation/g1/g1GCPhaseTimes.hpp" 29 #include "gc_implementation/g1/g1Log.hpp" 30 31 // Helper class for avoiding interleaved logging 32 class LineBuffer: public StackObj { 33 34 private: 35 static const int BUFFER_LEN = 1024; 36 static const int INDENT_CHARS = 3; 37 char _buffer[BUFFER_LEN]; 38 int _indent_level; 39 int _cur; 40 41 void vappend(const char* format, va_list ap) { 42 int res = vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap); 43 if (res != -1) { 44 _cur += res; 45 } else { 46 DEBUG_ONLY(warning("buffer too small in LineBuffer");) 47 _buffer[BUFFER_LEN -1] = 0; 48 _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again 49 } 50 } 51 52 public: 53 explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) { 54 for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) { 55 _buffer[_cur] = ' '; 56 } 57 } 58 59 #ifndef PRODUCT 60 ~LineBuffer() { 61 assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?"); 62 } 63 #endif 64 65 void append(const char* format, ...) { 66 va_list ap; 67 va_start(ap, format); 68 vappend(format, ap); 69 va_end(ap); 70 } 71 72 void append_and_print_cr(const char* format, ...) { 73 va_list ap; 74 va_start(ap, format); 75 vappend(format, ap); 76 va_end(ap); 77 gclog_or_tty->print_cr("%s", _buffer); 78 _cur = _indent_level * INDENT_CHARS; 79 } 80 }; 81 82 G1GCPhaseTimes::G1GCPhaseTimes(uint max_gc_threads) : 83 _max_gc_threads(max_gc_threads), 84 _min_clear_cc_time_ms(-1.0), 85 _max_clear_cc_time_ms(-1.0), 86 _cur_clear_cc_time_ms(0.0), 87 _cum_clear_cc_time_ms(0.0), 88 _num_cc_clears(0L) 89 { 90 assert(max_gc_threads > 0, "Must have some GC threads"); 91 _par_last_gc_worker_start_times_ms = new double[_max_gc_threads]; 92 _par_last_ext_root_scan_times_ms = new double[_max_gc_threads]; 93 _par_last_satb_filtering_times_ms = new double[_max_gc_threads]; 94 _par_last_update_rs_times_ms = new double[_max_gc_threads]; 95 _par_last_update_rs_processed_buffers = new double[_max_gc_threads]; 96 _par_last_scan_rs_times_ms = new double[_max_gc_threads]; 97 _par_last_obj_copy_times_ms = new double[_max_gc_threads]; 98 _par_last_termination_times_ms = new double[_max_gc_threads]; 99 _par_last_termination_attempts = new double[_max_gc_threads]; 100 _par_last_gc_worker_end_times_ms = new double[_max_gc_threads]; 101 _par_last_gc_worker_times_ms = new double[_max_gc_threads]; 102 _par_last_gc_worker_other_times_ms = new double[_max_gc_threads]; 103 } 104 105 void G1GCPhaseTimes::note_gc_start(double pause_start_time_sec, uint active_gc_threads, 106 bool is_young_gc, bool is_initial_mark_gc, GCCause::Cause gc_cause) { 107 assert(active_gc_threads > 0, "The number of threads must be > 0"); 108 assert(active_gc_threads <= _max_gc_threads, "The number of active threads must be <= the max nubmer of threads"); 109 _active_gc_threads = active_gc_threads; 110 _pause_start_time_sec = pause_start_time_sec; 111 _is_young_gc = is_young_gc; 112 _is_initial_mark_gc = is_initial_mark_gc; 113 _gc_cause = gc_cause; 114 115 #ifdef ASSERT 116 // initialise the timing data to something well known so that we can spot 117 // if something is not set properly 118 119 for (uint i = 0; i < _max_gc_threads; ++i) { 120 _par_last_gc_worker_start_times_ms[i] = -1234.0; 121 _par_last_ext_root_scan_times_ms[i] = -1234.0; 122 _par_last_satb_filtering_times_ms[i] = -1234.0; 123 _par_last_update_rs_times_ms[i] = -1234.0; 124 _par_last_update_rs_processed_buffers[i] = -1234.0; 125 _par_last_scan_rs_times_ms[i] = -1234.0; 126 _par_last_obj_copy_times_ms[i] = -1234.0; 127 _par_last_termination_times_ms[i] = -1234.0; 128 _par_last_termination_attempts[i] = -1234.0; 129 _par_last_gc_worker_end_times_ms[i] = -1234.0; 130 _par_last_gc_worker_times_ms[i] = -1234.0; 131 _par_last_gc_worker_other_times_ms[i] = -1234.0; 132 } 133 #endif 134 } 135 136 void G1GCPhaseTimes::note_gc_end(double pause_end_time_sec) { 137 if (G1Log::fine()) { 138 double pause_time_ms = (pause_end_time_sec - _pause_start_time_sec) * MILLIUNITS; 139 140 for (uint i = 0; i < _active_gc_threads; i++) { 141 _par_last_gc_worker_times_ms[i] = _par_last_gc_worker_end_times_ms[i] - 142 _par_last_gc_worker_start_times_ms[i]; 143 144 double worker_known_time = _par_last_ext_root_scan_times_ms[i] + 145 _par_last_satb_filtering_times_ms[i] + 146 _par_last_update_rs_times_ms[i] + 147 _par_last_scan_rs_times_ms[i] + 148 _par_last_obj_copy_times_ms[i] + 149 _par_last_termination_times_ms[i]; 150 151 _par_last_gc_worker_other_times_ms[i] = _par_last_gc_worker_times_ms[i] - 152 worker_known_time; 153 } 154 155 print(pause_time_ms); 156 } 157 158 } 159 160 void G1GCPhaseTimes::print_par_stats(int level, 161 const char* str, 162 double* data, 163 bool showDecimals) { 164 double min = data[0], max = data[0]; 165 double total = 0.0; 166 LineBuffer buf(level); 167 buf.append("[%s (ms):", str); 168 for (uint i = 0; i < _active_gc_threads; ++i) { 169 double val = data[i]; 170 if (val < min) 171 min = val; 172 if (val > max) 173 max = val; 174 total += val; 175 if (G1Log::finest()) { 176 if (showDecimals) { 177 buf.append(" %.1lf", val); 178 } else { 179 buf.append(" %d", (int)val); 180 } 181 } 182 } 183 184 if (G1Log::finest()) { 185 buf.append_and_print_cr(""); 186 } 187 double avg = total / (double) _active_gc_threads; 188 if (showDecimals) { 189 buf.append_and_print_cr(" Min: %.1lf, Avg: %.1lf, Max: %.1lf, Diff: %.1lf, Sum: %.1lf]", 190 min, avg, max, max - min, total); 191 } else { 192 buf.append_and_print_cr(" Min: %d, Avg: %d, Max: %d, Diff: %d, Sum: %d]", 193 (int)min, (int)avg, (int)max, (int)max - (int)min, (int)total); 194 } 195 } 196 197 void G1GCPhaseTimes::print_stats(int level, const char* str, double value) { 198 LineBuffer(level).append_and_print_cr("[%s: %.1lf ms]", str, value); 199 } 200 201 void G1GCPhaseTimes::print_stats(int level, const char* str, double value, int workers) { 202 LineBuffer(level).append_and_print_cr("[%s: %.1lf ms, GC Workers: %d]", str, value, workers); 203 } 204 205 void G1GCPhaseTimes::print_stats(int level, const char* str, int value) { 206 LineBuffer(level).append_and_print_cr("[%s: %d]", str, value); 207 } 208 209 double G1GCPhaseTimes::avg_value(double* data) { 210 if (G1CollectedHeap::use_parallel_gc_threads()) { 211 double ret = 0.0; 212 for (uint i = 0; i < _active_gc_threads; ++i) { 213 ret += data[i]; 214 } 215 return ret / (double) _active_gc_threads; 216 } else { 217 return data[0]; 218 } 219 } 220 221 double G1GCPhaseTimes::max_value(double* data) { 222 if (G1CollectedHeap::use_parallel_gc_threads()) { 223 double ret = data[0]; 224 for (uint i = 1; i < _active_gc_threads; ++i) { 225 if (data[i] > ret) { 226 ret = data[i]; 227 } 228 } 229 return ret; 230 } else { 231 return data[0]; 232 } 233 } 234 235 double G1GCPhaseTimes::sum_of_values(double* data) { 236 if (G1CollectedHeap::use_parallel_gc_threads()) { 237 double sum = 0.0; 238 for (uint i = 0; i < _active_gc_threads; i++) { 239 sum += data[i]; 240 } 241 return sum; 242 } else { 243 return data[0]; 244 } 245 } 246 247 double G1GCPhaseTimes::max_sum(double* data1, double* data2) { 248 double ret = data1[0] + data2[0]; 249 250 if (G1CollectedHeap::use_parallel_gc_threads()) { 251 for (uint i = 1; i < _active_gc_threads; ++i) { 252 double data = data1[i] + data2[i]; 253 if (data > ret) { 254 ret = data; 255 } 256 } 257 } 258 return ret; 259 } 260 261 void G1GCPhaseTimes::collapse_par_times() { 262 _ext_root_scan_time = avg_value(_par_last_ext_root_scan_times_ms); 263 _satb_filtering_time = avg_value(_par_last_satb_filtering_times_ms); 264 _update_rs_time = avg_value(_par_last_update_rs_times_ms); 265 _update_rs_processed_buffers = 266 sum_of_values(_par_last_update_rs_processed_buffers); 267 _scan_rs_time = avg_value(_par_last_scan_rs_times_ms); 268 _obj_copy_time = avg_value(_par_last_obj_copy_times_ms); 269 _termination_time = avg_value(_par_last_termination_times_ms); 270 } 271 272 double G1GCPhaseTimes::accounted_time_ms() { 273 // Subtract the root region scanning wait time. It's initialized to 274 // zero at the start of the pause. 275 double misc_time_ms = _root_region_scan_wait_time_ms; 276 277 misc_time_ms += _cur_collection_par_time_ms; 278 279 // Now subtract the time taken to fix up roots in generated code 280 misc_time_ms += _cur_collection_code_root_fixup_time_ms; 281 282 // Subtract the time taken to clean the card table from the 283 // current value of "other time" 284 misc_time_ms += _cur_clear_ct_time_ms; 285 286 return misc_time_ms; 287 } 288 289 void G1GCPhaseTimes::print(double pause_time_ms) { 290 291 if (PrintGCTimeStamps) { 292 gclog_or_tty->stamp(); 293 gclog_or_tty->print(": "); 294 } 295 296 GCCauseString gc_cause_str = GCCauseString("GC pause", _gc_cause) 297 .append(_is_young_gc ? " (young)" : " (mixed)") 298 .append(_is_initial_mark_gc ? " (initial-mark)" : ""); 299 gclog_or_tty->print_cr("[%s, %3.7f secs]", (const char*)gc_cause_str, pause_time_ms / 1000.0); 300 301 if (!G1Log::finer()) { 302 return; 303 } 304 305 if (_root_region_scan_wait_time_ms > 0.0) { 306 print_stats(1, "Root Region Scan Waiting", _root_region_scan_wait_time_ms); 307 } 308 if (G1CollectedHeap::use_parallel_gc_threads()) { 309 print_stats(1, "Parallel Time", _cur_collection_par_time_ms, _active_gc_threads); 310 print_par_stats(2, "GC Worker Start", _par_last_gc_worker_start_times_ms); 311 print_par_stats(2, "Ext Root Scanning", _par_last_ext_root_scan_times_ms); 312 if (_satb_filtering_time > 0.0) { 313 print_par_stats(2, "SATB Filtering", _par_last_satb_filtering_times_ms); 314 } 315 print_par_stats(2, "Update RS", _par_last_update_rs_times_ms); 316 if (G1Log::finest()) { 317 print_par_stats(3, "Processed Buffers", _par_last_update_rs_processed_buffers, 318 false /* showDecimals */); 319 } 320 print_par_stats(2, "Scan RS", _par_last_scan_rs_times_ms); 321 print_par_stats(2, "Object Copy", _par_last_obj_copy_times_ms); 322 print_par_stats(2, "Termination", _par_last_termination_times_ms); 323 if (G1Log::finest()) { 324 print_par_stats(3, "Termination Attempts", _par_last_termination_attempts, 325 false /* showDecimals */); 326 } 327 print_par_stats(2, "GC Worker Other", _par_last_gc_worker_other_times_ms); 328 print_par_stats(2, "GC Worker Total", _par_last_gc_worker_times_ms); 329 print_par_stats(2, "GC Worker End", _par_last_gc_worker_end_times_ms); 330 } else { 331 print_stats(1, "Ext Root Scanning", _ext_root_scan_time); 332 if (_satb_filtering_time > 0.0) { 333 print_stats(1, "SATB Filtering", _satb_filtering_time); 334 } 335 print_stats(1, "Update RS", _update_rs_time); 336 if (G1Log::finest()) { 337 print_stats(2, "Processed Buffers", (int)_update_rs_processed_buffers); 338 } 339 print_stats(1, "Scan RS", _scan_rs_time); 340 print_stats(1, "Object Copying", _obj_copy_time); 341 } 342 print_stats(1, "Code Root Fixup", _cur_collection_code_root_fixup_time_ms); 343 print_stats(1, "Clear CT", _cur_clear_ct_time_ms); 344 if (Verbose && G1Log::finest()) { 345 print_stats(1, "Cur Clear CC", _cur_clear_cc_time_ms); 346 print_stats(1, "Cum Clear CC", _cum_clear_cc_time_ms); 347 print_stats(1, "Min Clear CC", _min_clear_cc_time_ms); 348 print_stats(1, "Max Clear CC", _max_clear_cc_time_ms); 349 if (_num_cc_clears > 0) { 350 print_stats(1, "Avg Clear CC", _cum_clear_cc_time_ms / ((double)_num_cc_clears)); 351 } 352 } 353 double misc_time_ms = pause_time_ms - accounted_time_ms(); 354 print_stats(1, "Other", misc_time_ms); 355 print_stats(2, "Choose CSet", 356 (_recorded_young_cset_choice_time_ms + 357 _recorded_non_young_cset_choice_time_ms)); 358 print_stats(2, "Ref Proc", _cur_ref_proc_time_ms); 359 print_stats(2, "Ref Enq", _cur_ref_enq_time_ms); 360 print_stats(2, "Free CSet", 361 (_recorded_young_free_cset_time_ms + 362 _recorded_non_young_free_cset_time_ms)); 363 } 364 365 void G1GCPhaseTimes::record_cc_clear_time_ms(double ms) { 366 if (!(Verbose && G1Log::finest())) { 367 return; 368 } 369 370 if (_min_clear_cc_time_ms < 0.0 || ms <= _min_clear_cc_time_ms) { 371 _min_clear_cc_time_ms = ms; 372 } 373 if (_max_clear_cc_time_ms < 0.0 || ms >= _max_clear_cc_time_ms) { 374 _max_clear_cc_time_ms = ms; 375 } 376 _cur_clear_cc_time_ms = ms; 377 _cum_clear_cc_time_ms += ms; 378 _num_cc_clears++; 379 }