g1GCPhaseTimes.cpp 13.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */


#include "precompiled.hpp"
#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
#include "gc_implementation/g1/g1GCPhaseTimes.hpp"
#include "gc_implementation/g1/g1Log.hpp"
P
pliden 已提交
30
#include "gc_implementation/g1/g1StringDedup.hpp"
31 32 33 34 35 36 37 38 39 40 41

// Helper class for avoiding interleaved logging
class LineBuffer: public StackObj {

private:
  static const int BUFFER_LEN = 1024;
  static const int INDENT_CHARS = 3;
  char _buffer[BUFFER_LEN];
  int _indent_level;
  int _cur;

42
  void vappend(const char* format, va_list ap)  ATTRIBUTE_PRINTF(2, 0) {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    int res = vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap);
    if (res != -1) {
      _cur += res;
    } else {
      DEBUG_ONLY(warning("buffer too small in LineBuffer");)
      _buffer[BUFFER_LEN -1] = 0;
      _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again
    }
  }

public:
  explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) {
    for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) {
      _buffer[_cur] = ' ';
    }
  }

#ifndef PRODUCT
  ~LineBuffer() {
    assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?");
  }
#endif

66
  void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3) {
67 68 69 70 71 72
    va_list ap;
    va_start(ap, format);
    vappend(format, ap);
    va_end(ap);
  }

73
  void append_and_print_cr(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3) {
74 75 76 77 78 79 80 81 82
    va_list ap;
    va_start(ap, format);
    vappend(format, ap);
    va_end(ap);
    gclog_or_tty->print_cr("%s", _buffer);
    _cur = _indent_level * INDENT_CHARS;
  }
};

83 84
PRAGMA_DIAG_PUSH
PRAGMA_FORMAT_NONLITERAL_IGNORED
85 86 87 88 89 90 91 92 93
template <class T>
void WorkerDataArray<T>::print(int level, const char* title) {
  if (_length == 1) {
    // No need for min, max, average and sum for only one worker
    LineBuffer buf(level);
    buf.append("[%s:  ", title);
    buf.append(_print_format, _data[0]);
    buf.append_and_print_cr("]");
    return;
94 95
  }

96 97 98
  T min = _data[0];
  T max = _data[0];
  T sum = 0;
99 100

  LineBuffer buf(level);
101 102 103 104 105 106
  buf.append("[%s:", title);
  for (uint i = 0; i < _length; ++i) {
    T val = _data[i];
    min = MIN2(val, min);
    max = MAX2(val, max);
    sum += val;
107
    if (G1Log::finest()) {
108 109
      buf.append("  ");
      buf.append(_print_format, val);
110 111 112 113
    }
  }

  if (G1Log::finest()) {
114
    buf.append_and_print_cr("%s", "");
115
  }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  double avg = (double)sum / (double)_length;
  buf.append(" Min: ");
  buf.append(_print_format, min);
  buf.append(", Avg: ");
  buf.append("%.1lf", avg); // Always print average as a double
  buf.append(", Max: ");
  buf.append(_print_format, max);
  buf.append(", Diff: ");
  buf.append(_print_format, max - min);
  if (_print_sum) {
    // for things like the start and end times the sum is not
    // that relevant
    buf.append(", Sum: ");
    buf.append(_print_format, sum);
131
  }
132
  buf.append_and_print_cr("]");
133
}
134
PRAGMA_DIAG_POP
135

136
#ifndef PRODUCT
137

138 139 140 141
template <> const int WorkerDataArray<int>::_uninitialized = -1;
template <> const double WorkerDataArray<double>::_uninitialized = -1.0;
template <> const size_t WorkerDataArray<size_t>::_uninitialized = (size_t)-1;

142 143 144
template <class T>
void WorkerDataArray<T>::reset() {
  for (uint i = 0; i < _length; i++) {
145
    _data[i] = (T)_uninitialized;
146
  }
147 148
}

149 150 151
template <class T>
void WorkerDataArray<T>::verify() {
  for (uint i = 0; i < _length; i++) {
152 153 154
    assert(_data[i] != _uninitialized,
        err_msg("Invalid data for worker " UINT32_FORMAT ", data: %lf, uninitialized: %lf",
            i, (double)_data[i], (double)_uninitialized));
155
  }
156 157
}

158 159 160 161 162 163 164 165 166 167
#endif

G1GCPhaseTimes::G1GCPhaseTimes(uint max_gc_threads) :
  _max_gc_threads(max_gc_threads),
  _last_gc_worker_start_times_ms(_max_gc_threads, "%.1lf", false),
  _last_ext_root_scan_times_ms(_max_gc_threads, "%.1lf"),
  _last_satb_filtering_times_ms(_max_gc_threads, "%.1lf"),
  _last_update_rs_times_ms(_max_gc_threads, "%.1lf"),
  _last_update_rs_processed_buffers(_max_gc_threads, "%d"),
  _last_scan_rs_times_ms(_max_gc_threads, "%.1lf"),
J
johnc 已提交
168
  _last_strong_code_root_scan_times_ms(_max_gc_threads, "%.1lf"),
169 170 171 172 173
  _last_obj_copy_times_ms(_max_gc_threads, "%.1lf"),
  _last_termination_times_ms(_max_gc_threads, "%.1lf"),
  _last_termination_attempts(_max_gc_threads, SIZE_FORMAT),
  _last_gc_worker_end_times_ms(_max_gc_threads, "%.1lf", false),
  _last_gc_worker_times_ms(_max_gc_threads, "%.1lf"),
P
pliden 已提交
174
  _last_gc_worker_other_times_ms(_max_gc_threads, "%.1lf"),
175 176
  _last_redirty_logged_cards_time_ms(_max_gc_threads, "%.1lf"),
  _last_redirty_logged_cards_processed_cards(_max_gc_threads, SIZE_FORMAT),
P
pliden 已提交
177 178
  _cur_string_dedup_queue_fixup_worker_times_ms(_max_gc_threads, "%.1lf"),
  _cur_string_dedup_table_fixup_worker_times_ms(_max_gc_threads, "%.1lf")
179 180
{
  assert(max_gc_threads > 0, "Must have some GC threads");
181 182
}

183 184 185 186 187 188 189 190 191 192 193
void G1GCPhaseTimes::note_gc_start(uint active_gc_threads) {
  assert(active_gc_threads > 0, "The number of threads must be > 0");
  assert(active_gc_threads <= _max_gc_threads, "The number of active threads must be <= the max nubmer of threads");
  _active_gc_threads = active_gc_threads;

  _last_gc_worker_start_times_ms.reset();
  _last_ext_root_scan_times_ms.reset();
  _last_satb_filtering_times_ms.reset();
  _last_update_rs_times_ms.reset();
  _last_update_rs_processed_buffers.reset();
  _last_scan_rs_times_ms.reset();
J
johnc 已提交
194
  _last_strong_code_root_scan_times_ms.reset();
195 196 197 198 199 200
  _last_obj_copy_times_ms.reset();
  _last_termination_times_ms.reset();
  _last_termination_attempts.reset();
  _last_gc_worker_end_times_ms.reset();
  _last_gc_worker_times_ms.reset();
  _last_gc_worker_other_times_ms.reset();
201 202 203 204

  _last_redirty_logged_cards_time_ms.reset();
  _last_redirty_logged_cards_processed_cards.reset();

205 206
}

207 208 209 210 211 212 213
void G1GCPhaseTimes::note_gc_end() {
  _last_gc_worker_start_times_ms.verify();
  _last_ext_root_scan_times_ms.verify();
  _last_satb_filtering_times_ms.verify();
  _last_update_rs_times_ms.verify();
  _last_update_rs_processed_buffers.verify();
  _last_scan_rs_times_ms.verify();
J
johnc 已提交
214
  _last_strong_code_root_scan_times_ms.verify();
215 216 217 218 219
  _last_obj_copy_times_ms.verify();
  _last_termination_times_ms.verify();
  _last_termination_attempts.verify();
  _last_gc_worker_end_times_ms.verify();

220 221 222
  for (uint i = 0; i < _active_gc_threads; i++) {
    double worker_time = _last_gc_worker_end_times_ms.get(i) - _last_gc_worker_start_times_ms.get(i);
    _last_gc_worker_times_ms.set(i, worker_time);
223

224
    double worker_known_time = _last_ext_root_scan_times_ms.get(i) +
225 226 227
                               _last_satb_filtering_times_ms.get(i) +
                               _last_update_rs_times_ms.get(i) +
                               _last_scan_rs_times_ms.get(i) +
J
johnc 已提交
228
                               _last_strong_code_root_scan_times_ms.get(i) +
229 230
                               _last_obj_copy_times_ms.get(i) +
                               _last_termination_times_ms.get(i);
231

232 233 234
    double worker_other_time = worker_time - worker_known_time;
    _last_gc_worker_other_times_ms.set(i, worker_other_time);
  }
235

236 237
  _last_gc_worker_times_ms.verify();
  _last_gc_worker_other_times_ms.verify();
238

239 240
  _last_redirty_logged_cards_time_ms.verify();
  _last_redirty_logged_cards_processed_cards.verify();
241
}
242

P
pliden 已提交
243 244 245 246 247 248 249 250 251 252
void G1GCPhaseTimes::note_string_dedup_fixup_start() {
  _cur_string_dedup_queue_fixup_worker_times_ms.reset();
  _cur_string_dedup_table_fixup_worker_times_ms.reset();
}

void G1GCPhaseTimes::note_string_dedup_fixup_end() {
  _cur_string_dedup_queue_fixup_worker_times_ms.verify();
  _cur_string_dedup_table_fixup_worker_times_ms.verify();
}

253 254
void G1GCPhaseTimes::print_stats(int level, const char* str, double value) {
  LineBuffer(level).append_and_print_cr("[%s: %.1lf ms]", str, value);
255 256
}

257 258 259 260
void G1GCPhaseTimes::print_stats(int level, const char* str, size_t value) {
  LineBuffer(level).append_and_print_cr("[%s: "SIZE_FORMAT"]", str, value);
}

261 262
void G1GCPhaseTimes::print_stats(int level, const char* str, double value, uint workers) {
  LineBuffer(level).append_and_print_cr("[%s: %.1lf ms, GC Workers: " UINT32_FORMAT "]", str, value, workers);
263 264 265 266 267 268 269 270 271 272 273 274
}

double G1GCPhaseTimes::accounted_time_ms() {
    // Subtract the root region scanning wait time. It's initialized to
    // zero at the start of the pause.
    double misc_time_ms = _root_region_scan_wait_time_ms;

    misc_time_ms += _cur_collection_par_time_ms;

    // Now subtract the time taken to fix up roots in generated code
    misc_time_ms += _cur_collection_code_root_fixup_time_ms;

275 276 277
    // Strong code root purge time
    misc_time_ms += _cur_strong_code_root_purge_time_ms;

P
pliden 已提交
278 279 280 281 282
    if (G1StringDedup::is_enabled()) {
      // String dedup fixup time
      misc_time_ms += _cur_string_dedup_fixup_time_ms;
    }

283 284 285 286 287 288 289
    // Subtract the time taken to clean the card table from the
    // current value of "other time"
    misc_time_ms += _cur_clear_ct_time_ms;

    return misc_time_ms;
}

290
void G1GCPhaseTimes::print(double pause_time_sec) {
291 292 293 294 295
  if (_root_region_scan_wait_time_ms > 0.0) {
    print_stats(1, "Root Region Scan Waiting", _root_region_scan_wait_time_ms);
  }
  if (G1CollectedHeap::use_parallel_gc_threads()) {
    print_stats(1, "Parallel Time", _cur_collection_par_time_ms, _active_gc_threads);
296 297 298 299
    _last_gc_worker_start_times_ms.print(2, "GC Worker Start (ms)");
    _last_ext_root_scan_times_ms.print(2, "Ext Root Scanning (ms)");
    if (_last_satb_filtering_times_ms.sum() > 0.0) {
      _last_satb_filtering_times_ms.print(2, "SATB Filtering (ms)");
300
    }
301 302 303
    _last_update_rs_times_ms.print(2, "Update RS (ms)");
      _last_update_rs_processed_buffers.print(3, "Processed Buffers");
    _last_scan_rs_times_ms.print(2, "Scan RS (ms)");
J
johnc 已提交
304
    _last_strong_code_root_scan_times_ms.print(2, "Code Root Scanning (ms)");
305 306
    _last_obj_copy_times_ms.print(2, "Object Copy (ms)");
    _last_termination_times_ms.print(2, "Termination (ms)");
307
    if (G1Log::finest()) {
308
      _last_termination_attempts.print(3, "Termination Attempts");
309
    }
310 311 312
    _last_gc_worker_other_times_ms.print(2, "GC Worker Other (ms)");
    _last_gc_worker_times_ms.print(2, "GC Worker Total (ms)");
    _last_gc_worker_end_times_ms.print(2, "GC Worker End (ms)");
313
  } else {
314 315 316
    _last_ext_root_scan_times_ms.print(1, "Ext Root Scanning (ms)");
    if (_last_satb_filtering_times_ms.sum() > 0.0) {
      _last_satb_filtering_times_ms.print(1, "SATB Filtering (ms)");
317
    }
318 319 320
    _last_update_rs_times_ms.print(1, "Update RS (ms)");
      _last_update_rs_processed_buffers.print(2, "Processed Buffers");
    _last_scan_rs_times_ms.print(1, "Scan RS (ms)");
J
johnc 已提交
321
    _last_strong_code_root_scan_times_ms.print(1, "Code Root Scanning (ms)");
322
    _last_obj_copy_times_ms.print(1, "Object Copy (ms)");
323 324
  }
  print_stats(1, "Code Root Fixup", _cur_collection_code_root_fixup_time_ms);
325
  print_stats(1, "Code Root Purge", _cur_strong_code_root_purge_time_ms);
P
pliden 已提交
326 327 328 329 330
  if (G1StringDedup::is_enabled()) {
    print_stats(1, "String Dedup Fixup", _cur_string_dedup_fixup_time_ms, _active_gc_threads);
    _cur_string_dedup_queue_fixup_worker_times_ms.print(2, "Queue Fixup (ms)");
    _cur_string_dedup_table_fixup_worker_times_ms.print(2, "Table Fixup (ms)");
  }
331
  print_stats(1, "Clear CT", _cur_clear_ct_time_ms);
332
  double misc_time_ms = pause_time_sec * MILLIUNITS - accounted_time_ms();
333
  print_stats(1, "Other", misc_time_ms);
334 335 336
  if (_cur_verify_before_time_ms > 0.0) {
    print_stats(2, "Verify Before", _cur_verify_before_time_ms);
  }
337 338 339 340 341 342 343 344 345 346
  if (G1CollectedHeap::heap()->evacuation_failed()) {
    double evac_fail_handling = _cur_evac_fail_recalc_used + _cur_evac_fail_remove_self_forwards +
      _cur_evac_fail_restore_remsets;
    print_stats(2, "Evacuation Failure", evac_fail_handling);
    if (G1Log::finest()) {
      print_stats(3, "Recalculate Used", _cur_evac_fail_recalc_used);
      print_stats(3, "Remove Self Forwards", _cur_evac_fail_remove_self_forwards);
      print_stats(3, "Restore RemSet", _cur_evac_fail_restore_remsets);
    }
  }
347 348 349 350 351
  print_stats(2, "Choose CSet",
    (_recorded_young_cset_choice_time_ms +
    _recorded_non_young_cset_choice_time_ms));
  print_stats(2, "Ref Proc", _cur_ref_proc_time_ms);
  print_stats(2, "Ref Enq", _cur_ref_enq_time_ms);
352 353 354 355
  print_stats(2, "Redirty Cards", _recorded_redirty_logged_cards_time_ms);
  if (G1Log::finest()) {
    _last_redirty_logged_cards_time_ms.print(3, "Parallel Redirty");
    _last_redirty_logged_cards_processed_cards.print(3, "Redirtied Cards");
356
  }
357 358 359 360 361 362 363 364
  if (G1ReclaimDeadHumongousObjectsAtYoungGC) {
    print_stats(2, "Humongous Reclaim", _cur_fast_reclaim_humongous_time_ms);
    if (G1Log::finest()) {
      print_stats(3, "Humongous Total", _cur_fast_reclaim_humongous_total);
      print_stats(3, "Humongous Candidate", _cur_fast_reclaim_humongous_candidates);
      print_stats(3, "Humongous Reclaimed", _cur_fast_reclaim_humongous_reclaimed);
    }
  }
365 366 367
  print_stats(2, "Free CSet",
    (_recorded_young_free_cset_time_ms +
    _recorded_non_young_free_cset_time_ms));
368 369 370 371
  if (G1Log::finest()) {
    print_stats(3, "Young Free CSet", _recorded_young_free_cset_time_ms);
    print_stats(3, "Non-Young Free CSet", _recorded_non_young_free_cset_time_ms);
  }
372 373 374
  if (_cur_verify_after_time_ms > 0.0) {
    print_stats(2, "Verify After", _cur_verify_after_time_ms);
  }
375
}