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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1GCPHASETIMESLOG_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1GCPHASETIMESLOG_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "gc_interface/gcCause.hpp"
  30 
  31 template <class T>
  32 class WorkerDataArray  : public CHeapObj<mtGC> {
  33   T*          _data;
  34   uint        _length;
  35   const char* _print_format;
  36   bool        _print_sum;
  37 
  38   NOT_PRODUCT(static const T _uninitialized;)
  39 
  40   // We are caching the sum and average to only have to calculate them once.
  41   // This is not done in an MT-safe way. It is intended to allow single
  42   // threaded code to call sum() and average() multiple times in any order
  43   // without having to worry about the cost.
  44   bool   _has_new_data;
  45   T      _sum;
  46   double _average;
  47 
  48  public:
  49   WorkerDataArray(uint length, const char* print_format, bool print_sum = true) :
  50   _length(length), _print_format(print_format), _print_sum(print_sum), _has_new_data(true) {
  51     assert(length > 0, "Must have some workers to store data for");
  52     _data = NEW_C_HEAP_ARRAY(T, _length, mtGC);
  53   }
  54 
  55   ~WorkerDataArray() {
  56     FREE_C_HEAP_ARRAY(T, _data);
  57   }
  58 
  59   void set(uint worker_i, T value) {
  60     assert(worker_i < _length, err_msg("Worker %d is greater than max: %d", worker_i, _length));
  61     assert(_data[worker_i] == (T)-1, err_msg("Overwriting data for worker %d", worker_i));
  62     _data[worker_i] = value;
  63     _has_new_data = true;
  64   }
  65 
  66   T get(uint worker_i) {
  67     assert(worker_i < _length, err_msg("Worker %d is greater than max: %d", worker_i, _length));
  68     assert(_data[worker_i] != (T)-1, err_msg("No data to add to for worker %d", worker_i));
  69     return _data[worker_i];
  70   }
  71 
  72   void add(uint worker_i, T value) {
  73     assert(worker_i < _length, err_msg("Worker %d is greater than max: %d", worker_i, _length));
  74     assert(_data[worker_i] != (T)-1, err_msg("No data to add to for worker %d", worker_i));
  75     _data[worker_i] += value;
  76     _has_new_data = true;
  77   }
  78 
  79   double average(){
  80     if (_has_new_data) {
  81       calculate_totals();
  82     }
  83     return _average;
  84   }
  85 
  86   T sum() {
  87     if (_has_new_data) {
  88       calculate_totals();
  89     }
  90     return _sum;
  91   }
  92 
  93   void print(int level, const char* title);
  94 
  95   void reset() PRODUCT_RETURN;
  96   void verify() PRODUCT_RETURN;
  97 
  98  private:
  99 
 100   void calculate_totals(){
 101     _sum = (T)0;
 102     for (uint i = 0; i < _length; ++i) {
 103       _sum += _data[i];
 104     }
 105     _average = (double)_sum / (double)_length;
 106     _has_new_data = false;
 107   }
 108 };
 109 
 110 class GCPhaseTimeTracker VALUE_OBJ_CLASS_SPEC {
 111 private:
 112   friend class G1GCPhaseTimes;
 113 
 114   WorkerDataArray<double>** _data;
 115   uint _num_phases;
 116   uint _worker_id;
 117 public:
 118   GCPhaseTimeTracker(WorkerDataArray<double>** data, uint num_phases, uint worker_id) :
 119     _data(data), _num_phases(num_phases), _worker_id(worker_id) { }
 120 
 121   bool active() const { return _num_phases > 0; }
 122 
 123   void set_value(uint phase, double value) {
 124     assert(_data != NULL, "just checking");
 125     _data[phase]->set(_worker_id, value);
 126   }
 127 };
 128 
 129 class G1GCPhaseTimes : public CHeapObj<mtGC> {
 130 
 131  private:
 132   uint _active_gc_threads;
 133   uint _max_gc_threads;
 134   uint _num_ext_root_scan_phases;
 135 
 136   bool track_ext_root_scan_phases() const { return _num_ext_root_scan_phases > 0; }
 137 
 138   WorkerDataArray<double> _last_gc_worker_start_times_ms;
 139   WorkerDataArray<double> _last_ext_root_scan_times_ms;
 140   WorkerDataArray<double>** _last_ext_root_scan_phase_times_ms;
 141   WorkerDataArray<double> _last_satb_filtering_times_ms;
 142   WorkerDataArray<double> _last_update_rs_times_ms;
 143   WorkerDataArray<int>    _last_update_rs_processed_buffers;
 144   WorkerDataArray<double> _last_scan_rs_times_ms;
 145   WorkerDataArray<double> _last_strong_code_root_scan_times_ms;
 146   WorkerDataArray<double> _last_obj_copy_times_ms;
 147   WorkerDataArray<double> _last_termination_times_ms;
 148   WorkerDataArray<size_t> _last_termination_attempts;
 149   WorkerDataArray<double> _last_gc_worker_end_times_ms;
 150   WorkerDataArray<double> _last_gc_worker_times_ms;
 151   WorkerDataArray<double> _last_gc_worker_other_times_ms;
 152 
 153   double _cur_collection_par_time_ms;
 154   double _cur_collection_code_root_fixup_time_ms;
 155   double _cur_strong_code_root_purge_time_ms;
 156 
 157   double _cur_evac_fail_recalc_used;
 158   double _cur_evac_fail_restore_remsets;
 159   double _cur_evac_fail_remove_self_forwards;
 160 
 161   double                  _cur_string_dedup_fixup_time_ms;
 162   WorkerDataArray<double> _cur_string_dedup_queue_fixup_worker_times_ms;
 163   WorkerDataArray<double> _cur_string_dedup_table_fixup_worker_times_ms;
 164 
 165   double _cur_clear_ct_time_ms;
 166   double _cur_ref_proc_time_ms;
 167   double _cur_ref_enq_time_ms;
 168 
 169   double _cur_collection_start_sec;
 170   double _root_region_scan_wait_time_ms;
 171 
 172   double _recorded_young_cset_choice_time_ms;
 173   double _recorded_non_young_cset_choice_time_ms;
 174 
 175   WorkerDataArray<double> _last_redirty_logged_cards_time_ms;
 176   WorkerDataArray<size_t> _last_redirty_logged_cards_processed_cards;
 177   double _recorded_redirty_logged_cards_time_ms;
 178 
 179   double _recorded_young_free_cset_time_ms;
 180   double _recorded_non_young_free_cset_time_ms;
 181 
 182   double _cur_fast_reclaim_humongous_time_ms;
 183   double _cur_fast_reclaim_humongous_register_time_ms;
 184   size_t _cur_fast_reclaim_humongous_total;
 185   size_t _cur_fast_reclaim_humongous_candidates;
 186   size_t _cur_fast_reclaim_humongous_reclaimed;
 187 
 188   double _cur_verify_before_time_ms;
 189   double _cur_verify_after_time_ms;
 190 
 191   // Helper methods for detailed logging
 192   void print_stats(int level, const char* str, double value);
 193   void print_stats(int level, const char* str, size_t value);
 194   void print_stats(int level, const char* str, double value, uint workers);
 195 
 196  public:
 197   G1GCPhaseTimes(uint max_gc_threads, uint num_ext_root_scan_phases);
 198   virtual ~G1GCPhaseTimes();
 199 
 200   WorkerDataArray<double>** get_ext_root_scan_phase_times() const { return _last_ext_root_scan_phase_times_ms; }
 201   uint num_ext_root_scan_phases() const { return _num_ext_root_scan_phases; }
 202 
 203   void note_gc_start(uint active_gc_threads);
 204   void note_gc_end();
 205   void print(double pause_time_sec);
 206 
 207   void record_gc_worker_start_time(uint worker_i, double ms) {
 208     _last_gc_worker_start_times_ms.set(worker_i, ms);
 209   }
 210 
 211   void record_ext_root_scan_time(uint worker_i, double ms) {
 212     _last_ext_root_scan_times_ms.set(worker_i, ms);
 213   }
 214 
 215   void record_satb_filtering_time(uint worker_i, double ms) {
 216     _last_satb_filtering_times_ms.set(worker_i, ms);
 217   }
 218 
 219   void record_update_rs_time(uint worker_i, double ms) {
 220     _last_update_rs_times_ms.set(worker_i, ms);
 221   }
 222 
 223   void record_update_rs_processed_buffers(uint worker_i, int processed_buffers) {
 224     _last_update_rs_processed_buffers.set(worker_i, processed_buffers);
 225   }
 226 
 227   void record_scan_rs_time(uint worker_i, double ms) {
 228     _last_scan_rs_times_ms.set(worker_i, ms);
 229   }
 230 
 231   void record_strong_code_root_scan_time(uint worker_i, double ms) {
 232     _last_strong_code_root_scan_times_ms.set(worker_i, ms);
 233   }
 234 
 235   void record_obj_copy_time(uint worker_i, double ms) {
 236     _last_obj_copy_times_ms.set(worker_i, ms);
 237   }
 238 
 239   void add_obj_copy_time(uint worker_i, double ms) {
 240     _last_obj_copy_times_ms.add(worker_i, ms);
 241   }
 242 
 243   void record_termination(uint worker_i, double ms, size_t attempts) {
 244     _last_termination_times_ms.set(worker_i, ms);
 245     _last_termination_attempts.set(worker_i, attempts);
 246   }
 247 
 248   void record_gc_worker_end_time(uint worker_i, double ms) {
 249     _last_gc_worker_end_times_ms.set(worker_i, ms);
 250   }
 251 
 252   void record_clear_ct_time(double ms) {
 253     _cur_clear_ct_time_ms = ms;
 254   }
 255 
 256   void record_par_time(double ms) {
 257     _cur_collection_par_time_ms = ms;
 258   }
 259 
 260   void record_code_root_fixup_time(double ms) {
 261     _cur_collection_code_root_fixup_time_ms = ms;
 262   }
 263 
 264   void record_strong_code_root_purge_time(double ms) {
 265     _cur_strong_code_root_purge_time_ms = ms;
 266   }
 267 
 268   void record_evac_fail_recalc_used_time(double ms) {
 269     _cur_evac_fail_recalc_used = ms;
 270   }
 271 
 272   void record_evac_fail_restore_remsets(double ms) {
 273     _cur_evac_fail_restore_remsets = ms;
 274   }
 275 
 276   void record_evac_fail_remove_self_forwards(double ms) {
 277     _cur_evac_fail_remove_self_forwards = ms;
 278   }
 279 
 280   void note_string_dedup_fixup_start();
 281   void note_string_dedup_fixup_end();
 282 
 283   void record_string_dedup_fixup_time(double ms) {
 284     _cur_string_dedup_fixup_time_ms = ms;
 285   }
 286 
 287   void record_string_dedup_queue_fixup_worker_time(uint worker_id, double ms) {
 288     _cur_string_dedup_queue_fixup_worker_times_ms.set(worker_id, ms);
 289   }
 290 
 291   void record_string_dedup_table_fixup_worker_time(uint worker_id, double ms) {
 292     _cur_string_dedup_table_fixup_worker_times_ms.set(worker_id, ms);
 293   }
 294 
 295   void record_ref_proc_time(double ms) {
 296     _cur_ref_proc_time_ms = ms;
 297   }
 298 
 299   void record_ref_enq_time(double ms) {
 300     _cur_ref_enq_time_ms = ms;
 301   }
 302 
 303   void record_root_region_scan_wait_time(double time_ms) {
 304     _root_region_scan_wait_time_ms = time_ms;
 305   }
 306 
 307   void record_young_free_cset_time_ms(double time_ms) {
 308     _recorded_young_free_cset_time_ms = time_ms;
 309   }
 310 
 311   void record_non_young_free_cset_time_ms(double time_ms) {
 312     _recorded_non_young_free_cset_time_ms = time_ms;
 313   }
 314 
 315   void record_fast_reclaim_humongous_stats(double time_ms, size_t total, size_t candidates) {
 316     _cur_fast_reclaim_humongous_register_time_ms = time_ms;
 317     _cur_fast_reclaim_humongous_total = total;
 318     _cur_fast_reclaim_humongous_candidates = candidates;
 319   }
 320 
 321   void record_fast_reclaim_humongous_time_ms(double value, size_t reclaimed) {
 322     _cur_fast_reclaim_humongous_time_ms = value;
 323     _cur_fast_reclaim_humongous_reclaimed = reclaimed;
 324   }
 325 
 326   void record_young_cset_choice_time_ms(double time_ms) {
 327     _recorded_young_cset_choice_time_ms = time_ms;
 328   }
 329 
 330   void record_non_young_cset_choice_time_ms(double time_ms) {
 331     _recorded_non_young_cset_choice_time_ms = time_ms;
 332   }
 333 
 334   void record_redirty_logged_cards_time_ms(uint worker_i, double time_ms) {
 335     _last_redirty_logged_cards_time_ms.set(worker_i, time_ms);
 336   }
 337 
 338   void record_redirty_logged_cards_processed_cards(uint worker_i, size_t processed_buffers) {
 339     _last_redirty_logged_cards_processed_cards.set(worker_i, processed_buffers);
 340   }
 341 
 342   void record_redirty_logged_cards_time_ms(double time_ms) {
 343     _recorded_redirty_logged_cards_time_ms = time_ms;
 344   }
 345 
 346   void record_cur_collection_start_sec(double time_ms) {
 347     _cur_collection_start_sec = time_ms;
 348   }
 349 
 350   void record_verify_before_time_ms(double time_ms) {
 351     _cur_verify_before_time_ms = time_ms;
 352   }
 353 
 354   void record_verify_after_time_ms(double time_ms) {
 355     _cur_verify_after_time_ms = time_ms;
 356   }
 357 
 358   double accounted_time_ms();
 359 
 360   double cur_collection_start_sec() {
 361     return _cur_collection_start_sec;
 362   }
 363 
 364   double cur_collection_par_time_ms() {
 365     return _cur_collection_par_time_ms;
 366   }
 367 
 368   double cur_clear_ct_time_ms() {
 369     return _cur_clear_ct_time_ms;
 370   }
 371 
 372   double root_region_scan_wait_time_ms() {
 373     return _root_region_scan_wait_time_ms;
 374   }
 375 
 376   double young_cset_choice_time_ms() {
 377     return _recorded_young_cset_choice_time_ms;
 378   }
 379 
 380   double young_free_cset_time_ms() {
 381     return _recorded_young_free_cset_time_ms;
 382   }
 383 
 384   double non_young_cset_choice_time_ms() {
 385     return _recorded_non_young_cset_choice_time_ms;
 386   }
 387 
 388   double non_young_free_cset_time_ms() {
 389     return _recorded_non_young_free_cset_time_ms;
 390   }
 391 
 392   double fast_reclaim_humongous_time_ms() {
 393     return _cur_fast_reclaim_humongous_time_ms;
 394   }
 395 
 396   double average_last_update_rs_time() {
 397     return _last_update_rs_times_ms.average();
 398   }
 399 
 400   int sum_last_update_rs_processed_buffers() {
 401     return _last_update_rs_processed_buffers.sum();
 402   }
 403 
 404   double average_last_scan_rs_time(){
 405     return _last_scan_rs_times_ms.average();
 406   }
 407 
 408   double average_last_strong_code_root_scan_time(){
 409     return _last_strong_code_root_scan_times_ms.average();
 410   }
 411 
 412   double average_last_obj_copy_time() {
 413     return _last_obj_copy_times_ms.average();
 414   }
 415 
 416   double average_last_termination_time() {
 417     return _last_termination_times_ms.average();
 418   }
 419 
 420   double average_last_ext_root_scan_time() {
 421     return _last_ext_root_scan_times_ms.average();
 422   }
 423 
 424   double average_last_satb_filtering_times_ms() {
 425     return _last_satb_filtering_times_ms.average();
 426   }
 427 };
 428 
 429 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1GCPHASETIMESLOG_HPP