1 /*
   2  * Copyright (c) 2013, 2014 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 #include "gc_implementation/g1/g1StringDedup.hpp"
  31 #include "runtime/atomic.inline.hpp"
  32 #include "g1CollectedHeap.hpp"
  33 
  34 // Helper class for avoiding interleaved logging
  35 class LineBuffer: public StackObj {
  36 
  37 private:
  38   static const int BUFFER_LEN = 1024;
  39   static const int INDENT_CHARS = 3;
  40   char _buffer[BUFFER_LEN];
  41   int _indent_level;
  42   int _cur;
  43 
  44   void vappend(const char* format, va_list ap)  ATTRIBUTE_PRINTF(2, 0) {
  45     int res = vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap);
  46     if (res != -1) {
  47       _cur += res;
  48     } else {
  49       DEBUG_ONLY(warning("buffer too small in LineBuffer");)
  50       _buffer[BUFFER_LEN -1] = 0;
  51       _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again
  52     }
  53   }
  54 
  55 public:
  56   explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) {
  57     for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) {
  58       _buffer[_cur] = ' ';
  59     }
  60   }
  61 
  62 #ifndef PRODUCT
  63   ~LineBuffer() {
  64     assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?");
  65   }
  66 #endif
  67 
  68   void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3) {
  69     va_list ap;
  70     va_start(ap, format);
  71     vappend(format, ap);
  72     va_end(ap);
  73   }
  74 
  75   void append_and_print_cr(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3) {
  76     va_list ap;
  77     va_start(ap, format);
  78     vappend(format, ap);
  79     va_end(ap);
  80     gclog_or_tty->print_cr("%s", _buffer);
  81     _cur = _indent_level * INDENT_CHARS;
  82   }
  83 };
  84 
  85 PRAGMA_DIAG_PUSH
  86 PRAGMA_FORMAT_NONLITERAL_IGNORED
  87 template <class T>
  88 void WorkerDataArray<T>::print(int level, const char* title) {
  89   if (_length == 1) {
  90     // No need for min, max, average and sum for only one worker
  91     LineBuffer buf(level);
  92     buf.append("[%s:  ", title);
  93     buf.append(_print_format, _data[0]);
  94     buf.append_and_print_cr("]");
  95     return;
  96   }
  97 
  98   T min = _data[0];
  99   T max = _data[0];
 100   T sum = 0;
 101 
 102   LineBuffer buf(level);
 103   buf.append("[%s:", title);
 104   for (uint i = 0; i < _length; ++i) {
 105     T val = _data[i];
 106     min = MIN2(val, min);
 107     max = MAX2(val, max);
 108     sum += val;
 109     if (G1Log::finest()) {
 110       buf.append("  ");
 111       buf.append(_print_format, val);
 112     }
 113   }
 114 
 115   if (G1Log::finest()) {
 116     buf.append_and_print_cr("%s", "");
 117   }
 118 
 119   double avg = (double)sum / (double)_length;
 120   buf.append(" Min: ");
 121   buf.append(_print_format, min);
 122   buf.append(", Avg: ");
 123   buf.append("%.1lf", avg); // Always print average as a double
 124   buf.append(", Max: ");
 125   buf.append(_print_format, max);
 126   buf.append(", Diff: ");
 127   buf.append(_print_format, max - min);
 128   if (_print_sum) {
 129     // for things like the start and end times the sum is not
 130     // that relevant
 131     buf.append(", Sum: ");
 132     buf.append(_print_format, sum);
 133   }
 134   buf.append_and_print_cr("]");
 135 }
 136 PRAGMA_DIAG_POP
 137 
 138 #ifndef PRODUCT
 139 
 140 template <> const int WorkerDataArray<int>::_uninitialized = -1;
 141 template <> const double WorkerDataArray<double>::_uninitialized = -1.0;
 142 template <> const size_t WorkerDataArray<size_t>::_uninitialized = (size_t)-1;
 143 
 144 template <class T>
 145 void WorkerDataArray<T>::reset() {
 146   for (uint i = 0; i < _length; i++) {
 147     _data[i] = (T)_uninitialized;
 148   }
 149   _has_new_data = true;
 150 }
 151 
 152 template <class T>
 153 void WorkerDataArray<T>::verify() {
 154   for (uint i = 0; i < _length; i++) {
 155     assert(_data[i] != _uninitialized,
 156         err_msg("Invalid data for worker %u, data: %lf, uninitialized: %lf",
 157             i, (double)_data[i], (double)_uninitialized));
 158   }
 159 }
 160 
 161 #endif
 162 
 163 G1GCPhaseTimes::G1GCPhaseTimes(uint max_gc_threads, uint num_ext_root_scan_phases) :
 164   _max_gc_threads(max_gc_threads),
 165   _num_ext_root_scan_phases(num_ext_root_scan_phases),
 166   _last_gc_worker_start_times_ms(_max_gc_threads, "%.1lf", false),
 167   _last_ext_root_scan_times_ms(_max_gc_threads, "%.1lf"),
 168   _last_ext_root_scan_phase_times_ms(NULL),
 169   _last_satb_filtering_times_ms(_max_gc_threads, "%.1lf"),
 170   _last_update_rs_times_ms(_max_gc_threads, "%.1lf"),
 171   _last_update_rs_processed_buffers(_max_gc_threads, "%d"),
 172   _last_scan_rs_times_ms(_max_gc_threads, "%.1lf"),
 173   _last_strong_code_root_scan_times_ms(_max_gc_threads, "%.1lf"),
 174   _last_obj_copy_times_ms(_max_gc_threads, "%.1lf"),
 175   _last_termination_times_ms(_max_gc_threads, "%.1lf"),
 176   _last_termination_attempts(_max_gc_threads, SIZE_FORMAT),
 177   _last_gc_worker_end_times_ms(_max_gc_threads, "%.1lf", false),
 178   _last_gc_worker_times_ms(_max_gc_threads, "%.1lf"),
 179   _last_gc_worker_other_times_ms(_max_gc_threads, "%.1lf"),
 180   _last_redirty_logged_cards_time_ms(_max_gc_threads, "%.1lf"),
 181   _last_redirty_logged_cards_processed_cards(_max_gc_threads, SIZE_FORMAT),
 182   _cur_string_dedup_queue_fixup_worker_times_ms(_max_gc_threads, "%.1lf"),
 183   _cur_string_dedup_table_fixup_worker_times_ms(_max_gc_threads, "%.1lf")
 184 {
 185   assert(max_gc_threads > 0, "Must have some GC threads");
 186   if (track_ext_root_scan_phases()) {
 187     _last_ext_root_scan_phase_times_ms = NEW_C_HEAP_ARRAY(WorkerDataArray<double>*, num_ext_root_scan_phases, mtGC);
 188     for (uint i = 0; i < num_ext_root_scan_phases; i++) {
 189       _last_ext_root_scan_phase_times_ms[i] = new WorkerDataArray<double>(_max_gc_threads, "%.1lf");
 190     }
 191   }
 192 }
 193 
 194 G1GCPhaseTimes::~G1GCPhaseTimes() {
 195   if (track_ext_root_scan_phases()) {
 196     for (uint i = 0; i < _num_ext_root_scan_phases; i++) {
 197       delete _last_ext_root_scan_phase_times_ms[i];
 198     }
 199     FREE_C_HEAP_ARRAY(WorkerDataArray<double>*, _last_ext_root_scan_phase_times_ms);
 200   }
 201 }
 202 
 203 void G1GCPhaseTimes::note_gc_start(uint active_gc_threads) {
 204   assert(active_gc_threads > 0, "The number of threads must be > 0");
 205   assert(active_gc_threads <= _max_gc_threads, "The number of active threads must be <= the max nubmer of threads");
 206   _active_gc_threads = active_gc_threads;
 207 
 208   _last_gc_worker_start_times_ms.reset();
 209   _last_ext_root_scan_times_ms.reset();
 210   _last_satb_filtering_times_ms.reset();
 211   _last_update_rs_times_ms.reset();
 212   _last_update_rs_processed_buffers.reset();
 213   _last_scan_rs_times_ms.reset();
 214   _last_strong_code_root_scan_times_ms.reset();
 215   _last_obj_copy_times_ms.reset();
 216   _last_termination_times_ms.reset();
 217   _last_termination_attempts.reset();
 218   _last_gc_worker_end_times_ms.reset();
 219   _last_gc_worker_times_ms.reset();
 220   _last_gc_worker_other_times_ms.reset();
 221 
 222   _last_redirty_logged_cards_time_ms.reset();
 223   _last_redirty_logged_cards_processed_cards.reset();
 224 
 225   for (uint i = 0; i < _num_ext_root_scan_phases; i++) {
 226     _last_ext_root_scan_phase_times_ms[i]->reset();
 227   }
 228 }
 229 
 230 void G1GCPhaseTimes::note_gc_end() {
 231   _last_gc_worker_start_times_ms.verify();
 232   _last_ext_root_scan_times_ms.verify();
 233   _last_satb_filtering_times_ms.verify();
 234   _last_update_rs_times_ms.verify();
 235   _last_update_rs_processed_buffers.verify();
 236   _last_scan_rs_times_ms.verify();
 237   _last_strong_code_root_scan_times_ms.verify();
 238   _last_obj_copy_times_ms.verify();
 239   _last_termination_times_ms.verify();
 240   _last_termination_attempts.verify();
 241   _last_gc_worker_end_times_ms.verify();
 242 
 243   for (uint i = 0; i < _active_gc_threads; i++) {
 244     double worker_time = _last_gc_worker_end_times_ms.get(i) - _last_gc_worker_start_times_ms.get(i);
 245     _last_gc_worker_times_ms.set(i, worker_time);
 246 
 247     double worker_known_time = _last_ext_root_scan_times_ms.get(i) +
 248                                _last_satb_filtering_times_ms.get(i) +
 249                                _last_update_rs_times_ms.get(i) +
 250                                _last_scan_rs_times_ms.get(i) +
 251                                _last_strong_code_root_scan_times_ms.get(i) +
 252                                _last_obj_copy_times_ms.get(i) +
 253                                _last_termination_times_ms.get(i);
 254 
 255     double worker_other_time = worker_time - worker_known_time;
 256     _last_gc_worker_other_times_ms.set(i, worker_other_time);
 257   }
 258 
 259   _last_gc_worker_times_ms.verify();
 260   _last_gc_worker_other_times_ms.verify();
 261 
 262   _last_redirty_logged_cards_time_ms.verify();
 263   _last_redirty_logged_cards_processed_cards.verify();
 264 }
 265 
 266 void G1GCPhaseTimes::note_string_dedup_fixup_start() {
 267   _cur_string_dedup_queue_fixup_worker_times_ms.reset();
 268   _cur_string_dedup_table_fixup_worker_times_ms.reset();
 269 }
 270 
 271 void G1GCPhaseTimes::note_string_dedup_fixup_end() {
 272   _cur_string_dedup_queue_fixup_worker_times_ms.verify();
 273   _cur_string_dedup_table_fixup_worker_times_ms.verify();
 274 }
 275 
 276 void G1GCPhaseTimes::print_stats(int level, const char* str, double value) {
 277   LineBuffer(level).append_and_print_cr("[%s: %.1lf ms]", str, value);
 278 }
 279 
 280 void G1GCPhaseTimes::print_stats(int level, const char* str, size_t value) {
 281   LineBuffer(level).append_and_print_cr("[%s: "SIZE_FORMAT"]", str, value);
 282 }
 283 
 284 void G1GCPhaseTimes::print_stats(int level, const char* str, double value, uint workers) {
 285   LineBuffer(level).append_and_print_cr("[%s: %.1lf ms, GC Workers: %u]", str, value, workers);
 286 }
 287 
 288 double G1GCPhaseTimes::accounted_time_ms() {
 289     // Subtract the root region scanning wait time. It's initialized to
 290     // zero at the start of the pause.
 291     double misc_time_ms = _root_region_scan_wait_time_ms;
 292 
 293     misc_time_ms += _cur_collection_par_time_ms;
 294 
 295     // Now subtract the time taken to fix up roots in generated code
 296     misc_time_ms += _cur_collection_code_root_fixup_time_ms;
 297 
 298     // Strong code root purge time
 299     misc_time_ms += _cur_strong_code_root_purge_time_ms;
 300 
 301     if (G1StringDedup::is_enabled()) {
 302       // String dedup fixup time
 303       misc_time_ms += _cur_string_dedup_fixup_time_ms;
 304     }
 305 
 306     // Subtract the time taken to clean the card table from the
 307     // current value of "other time"
 308     misc_time_ms += _cur_clear_ct_time_ms;
 309 
 310     return misc_time_ms;
 311 }
 312 
 313 void G1GCPhaseTimes::print(double pause_time_sec) {
 314   if (_root_region_scan_wait_time_ms > 0.0) {
 315     print_stats(1, "Root Region Scan Waiting", _root_region_scan_wait_time_ms);
 316   }
 317   print_stats(1, "Parallel Time", _cur_collection_par_time_ms, _active_gc_threads);
 318   _last_gc_worker_start_times_ms.print(2, "GC Worker Start (ms)");
 319   _last_ext_root_scan_times_ms.print(2, "Ext Root Scanning (ms)");
 320   if (track_ext_root_scan_phases()) {
 321     for (uint i = 0; i < _num_ext_root_scan_phases; i++) {
 322       WorkerDataArray<double>* data = _last_ext_root_scan_phase_times_ms[i];
 323       data->print(3, G1CollectedHeap::ext_roots_task_string(i));
 324     }
 325   }
 326   if (_last_satb_filtering_times_ms.sum() > 0.0) {
 327     _last_satb_filtering_times_ms.print(2, "SATB Filtering (ms)");
 328   }
 329   _last_update_rs_times_ms.print(2, "Update RS (ms)");
 330     _last_update_rs_processed_buffers.print(3, "Processed Buffers");
 331   _last_scan_rs_times_ms.print(2, "Scan RS (ms)");
 332   _last_strong_code_root_scan_times_ms.print(2, "Code Root Scanning (ms)");
 333   _last_obj_copy_times_ms.print(2, "Object Copy (ms)");
 334   _last_termination_times_ms.print(2, "Termination (ms)");
 335   if (G1Log::finest()) {
 336     _last_termination_attempts.print(3, "Termination Attempts");
 337   }
 338   _last_gc_worker_other_times_ms.print(2, "GC Worker Other (ms)");
 339   _last_gc_worker_times_ms.print(2, "GC Worker Total (ms)");
 340   _last_gc_worker_end_times_ms.print(2, "GC Worker End (ms)");
 341 
 342   print_stats(1, "Code Root Fixup", _cur_collection_code_root_fixup_time_ms);
 343   print_stats(1, "Code Root Purge", _cur_strong_code_root_purge_time_ms);
 344   if (G1StringDedup::is_enabled()) {
 345     print_stats(1, "String Dedup Fixup", _cur_string_dedup_fixup_time_ms, _active_gc_threads);
 346     _cur_string_dedup_queue_fixup_worker_times_ms.print(2, "Queue Fixup (ms)");
 347     _cur_string_dedup_table_fixup_worker_times_ms.print(2, "Table Fixup (ms)");
 348   }
 349   print_stats(1, "Clear CT", _cur_clear_ct_time_ms);
 350   double misc_time_ms = pause_time_sec * MILLIUNITS - accounted_time_ms();
 351   print_stats(1, "Other", misc_time_ms);
 352   if (_cur_verify_before_time_ms > 0.0) {
 353     print_stats(2, "Verify Before", _cur_verify_before_time_ms);
 354   }
 355   if (G1CollectedHeap::heap()->evacuation_failed()) {
 356     double evac_fail_handling = _cur_evac_fail_recalc_used + _cur_evac_fail_remove_self_forwards +
 357       _cur_evac_fail_restore_remsets;
 358     print_stats(2, "Evacuation Failure", evac_fail_handling);
 359     if (G1Log::finest()) {
 360       print_stats(3, "Recalculate Used", _cur_evac_fail_recalc_used);
 361       print_stats(3, "Remove Self Forwards", _cur_evac_fail_remove_self_forwards);
 362       print_stats(3, "Restore RemSet", _cur_evac_fail_restore_remsets);
 363     }
 364   }
 365   print_stats(2, "Choose CSet",
 366     (_recorded_young_cset_choice_time_ms +
 367     _recorded_non_young_cset_choice_time_ms));
 368   print_stats(2, "Ref Proc", _cur_ref_proc_time_ms);
 369   print_stats(2, "Ref Enq", _cur_ref_enq_time_ms);
 370   print_stats(2, "Redirty Cards", _recorded_redirty_logged_cards_time_ms);
 371   if (G1Log::finest()) {
 372     _last_redirty_logged_cards_time_ms.print(3, "Parallel Redirty");
 373     _last_redirty_logged_cards_processed_cards.print(3, "Redirtied Cards");
 374   }
 375   if (G1EagerReclaimHumongousObjects) {
 376     print_stats(2, "Humongous Register", _cur_fast_reclaim_humongous_register_time_ms);
 377     if (G1Log::finest()) {
 378       print_stats(3, "Humongous Total", _cur_fast_reclaim_humongous_total);
 379       print_stats(3, "Humongous Candidate", _cur_fast_reclaim_humongous_candidates);
 380     }
 381     print_stats(2, "Humongous Reclaim", _cur_fast_reclaim_humongous_time_ms);
 382     if (G1Log::finest()) {
 383       print_stats(3, "Humongous Reclaimed", _cur_fast_reclaim_humongous_reclaimed);
 384     }
 385   }
 386   print_stats(2, "Free CSet",
 387     (_recorded_young_free_cset_time_ms +
 388     _recorded_non_young_free_cset_time_ms));
 389   if (G1Log::finest()) {
 390     print_stats(3, "Young Free CSet", _recorded_young_free_cset_time_ms);
 391     print_stats(3, "Non-Young Free CSet", _recorded_non_young_free_cset_time_ms);
 392   }
 393   if (_cur_verify_after_time_ms > 0.0) {
 394     print_stats(2, "Verify After", _cur_verify_after_time_ms);
 395   }
 396 }