1 /*
   2  * Copyright (c) 2013, 2018, 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_G1_G1GCPHASETIMES_HPP
  26 #define SHARE_VM_GC_G1_G1GCPHASETIMES_HPP
  27 
  28 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
  29 #include "gc/shared/weakProcessorPhaseTimes.hpp"
  30 #include "jfr/jfrEvents.hpp"
  31 #include "logging/logLevel.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 class LineBuffer;
  36 class G1ParScanThreadState;
  37 class STWGCTimer;
  38 
  39 template <class T> class WorkerDataArray;
  40 
  41 class G1GCPhaseTimes : public CHeapObj<mtGC> {
  42   uint _max_gc_threads;
  43   jlong _gc_start_counter;
  44   double _gc_pause_time_ms;
  45 
  46  public:
  47   enum GCParPhases {
  48     GCWorkerStart,
  49     ExtRootScan,
  50     ThreadRoots,
  51     StringTableRoots,
  52     UniverseRoots,
  53     JNIRoots,
  54     ObjectSynchronizerRoots,
  55     ManagementRoots,
  56     SystemDictionaryRoots,
  57     CLDGRoots,
  58     JVMTIRoots,
  59     CMRefRoots,
  60     WaitForStrongCLD,
  61     WeakCLDRoots,
  62     SATBFiltering,
  63     UpdateRS,
  64     ScanHCC,
  65     ScanRS,
  66     OptScanRS,
  67     CodeRoots,
  68 #if INCLUDE_AOT
  69     AOTCodeRoots,
  70 #endif
  71     ObjCopy,
  72     OptObjCopy,
  73     Termination,
  74     Other,
  75     GCWorkerTotal,
  76     GCWorkerEnd,
  77     StringDedupQueueFixup,
  78     StringDedupTableFixup,
  79     RedirtyCards,
  80     YoungFreeCSet,
  81     NonYoungFreeCSet,
  82     GCParPhasesSentinel
  83   };
  84 
  85   enum GCScanRSWorkItems {
  86     ScanRSScannedCards,
  87     ScanRSClaimedCards,
  88     ScanRSSkippedCards
  89   };
  90 
  91   enum GCUpdateRSWorkItems {
  92     UpdateRSProcessedBuffers,
  93     UpdateRSScannedCards,
  94     UpdateRSSkippedCards
  95   };
  96 
  97   enum GCOptCSetWorkItems {
  98       OptCSetScannedCards,
  99       OptCSetClaimedCards,
 100       OptCSetSkippedCards,
 101       OptCSetUsedMemory
 102   };
 103 
 104  private:
 105   // Markers for grouping the phases in the GCPhases enum above
 106   static const int GCMainParPhasesLast = GCWorkerEnd;
 107   static const int StringDedupPhasesFirst = StringDedupQueueFixup;
 108   static const int StringDedupPhasesLast = StringDedupTableFixup;
 109 
 110   WorkerDataArray<double>* _gc_par_phases[GCParPhasesSentinel];
 111 
 112   WorkerDataArray<size_t>* _update_rs_processed_buffers;
 113   WorkerDataArray<size_t>* _update_rs_scanned_cards;
 114   WorkerDataArray<size_t>* _update_rs_skipped_cards;
 115 
 116   WorkerDataArray<size_t>* _scan_rs_scanned_cards;
 117   WorkerDataArray<size_t>* _scan_rs_claimed_cards;
 118   WorkerDataArray<size_t>* _scan_rs_skipped_cards;
 119 
 120   WorkerDataArray<size_t>* _opt_cset_scanned_cards;
 121   WorkerDataArray<size_t>* _opt_cset_claimed_cards;
 122   WorkerDataArray<size_t>* _opt_cset_skipped_cards;
 123   WorkerDataArray<size_t>* _opt_cset_used_memory;
 124 
 125   WorkerDataArray<size_t>* _termination_attempts;
 126 
 127   WorkerDataArray<size_t>* _redirtied_cards;
 128 
 129   double _cur_collection_par_time_ms;
 130   double _cur_optional_evac_ms;
 131   double _cur_collection_code_root_fixup_time_ms;
 132   double _cur_strong_code_root_purge_time_ms;
 133 
 134   double _cur_evac_fail_recalc_used;
 135   double _cur_evac_fail_remove_self_forwards;
 136 
 137   double _cur_string_dedup_fixup_time_ms;
 138 
 139   double _cur_prepare_tlab_time_ms;
 140   double _cur_resize_tlab_time_ms;
 141 
 142   double _cur_derived_pointer_table_update_time_ms;
 143 
 144   double _cur_clear_ct_time_ms;
 145   double _cur_expand_heap_time_ms;
 146   double _cur_ref_proc_time_ms;
 147 
 148   double _cur_collection_start_sec;
 149   double _root_region_scan_wait_time_ms;
 150 
 151   double _external_accounted_time_ms;
 152 
 153   double _recorded_clear_claimed_marks_time_ms;
 154 
 155   double _recorded_young_cset_choice_time_ms;
 156   double _recorded_non_young_cset_choice_time_ms;
 157 
 158   double _recorded_redirty_logged_cards_time_ms;
 159 
 160   double _recorded_preserve_cm_referents_time_ms;
 161 
 162   double _recorded_merge_pss_time_ms;
 163 
 164   double _recorded_start_new_cset_time_ms;
 165 
 166   double _recorded_total_free_cset_time_ms;
 167 
 168   double _recorded_serial_free_cset_time_ms;
 169 
 170   double _cur_fast_reclaim_humongous_time_ms;
 171   double _cur_fast_reclaim_humongous_register_time_ms;
 172   size_t _cur_fast_reclaim_humongous_total;
 173   size_t _cur_fast_reclaim_humongous_candidates;
 174   size_t _cur_fast_reclaim_humongous_reclaimed;
 175 
 176   double _cur_verify_before_time_ms;
 177   double _cur_verify_after_time_ms;
 178 
 179   ReferenceProcessorPhaseTimes _ref_phase_times;
 180   WeakProcessorPhaseTimes _weak_phase_times;
 181 
 182   double worker_time(GCParPhases phase, uint worker);
 183   void note_gc_end();
 184   void reset();
 185 
 186   template <class T>
 187   void details(T* phase, const char* indent) const;
 188 
 189   void log_phase(WorkerDataArray<double>* phase, uint indent, outputStream* out, bool print_sum) const;
 190   void debug_phase(WorkerDataArray<double>* phase) const;
 191   void trace_phase(WorkerDataArray<double>* phase, bool print_sum = true) const;
 192 
 193   void info_time(const char* name, double value) const;
 194   void debug_time(const char* name, double value) const;
 195   // This will print logs for both 'gc+phases' and 'gc+phases+ref'.
 196   void debug_time_for_reference(const char* name, double value) const;
 197   void trace_time(const char* name, double value) const;
 198   void trace_count(const char* name, size_t value) const;
 199 
 200   double print_pre_evacuate_collection_set() const;
 201   double print_evacuate_collection_set() const;
 202   double print_evacuate_optional_collection_set() const;
 203   double print_post_evacuate_collection_set() const;
 204   void print_other(double accounted_ms) const;
 205 
 206  public:
 207   G1GCPhaseTimes(STWGCTimer* gc_timer, uint max_gc_threads);
 208   void note_gc_start();
 209   void print();
 210   static const char* phase_name(GCParPhases phase);
 211 
 212   // record the time a phase took in seconds
 213   void record_time_secs(GCParPhases phase, uint worker_i, double secs);
 214 
 215   // add a number of seconds to a phase
 216   void add_time_secs(GCParPhases phase, uint worker_i, double secs);
 217 
 218   void record_or_add_time_secs(GCParPhases phase, uint worker_i, double secs);
 219 
 220   void record_thread_work_item(GCParPhases phase, uint worker_i, size_t count, uint index = 0);
 221 
 222   // return the average time for a phase in milliseconds
 223   double average_time_ms(GCParPhases phase);
 224 
 225   size_t sum_thread_work_items(GCParPhases phase, uint index = 0);
 226 
 227  public:
 228 
 229   void record_prepare_tlab_time_ms(double ms) {
 230     _cur_prepare_tlab_time_ms = ms;
 231   }
 232 
 233   void record_resize_tlab_time_ms(double ms) {
 234     _cur_resize_tlab_time_ms = ms;
 235   }
 236 
 237   void record_derived_pointer_table_update_time(double ms) {
 238     _cur_derived_pointer_table_update_time_ms = ms;
 239   }
 240 
 241   void record_clear_ct_time(double ms) {
 242     _cur_clear_ct_time_ms = ms;
 243   }
 244 
 245   void record_expand_heap_time(double ms) {
 246     _cur_expand_heap_time_ms = ms;
 247   }
 248 
 249   void record_par_time(double ms) {
 250     _cur_collection_par_time_ms = ms;
 251   }
 252 
 253   void record_optional_evacuation(double ms) {
 254     _cur_optional_evac_ms = ms;
 255   }
 256 
 257   void record_code_root_fixup_time(double ms) {
 258     _cur_collection_code_root_fixup_time_ms = ms;
 259   }
 260 
 261   void record_strong_code_root_purge_time(double ms) {
 262     _cur_strong_code_root_purge_time_ms = ms;
 263   }
 264 
 265   void record_evac_fail_recalc_used_time(double ms) {
 266     _cur_evac_fail_recalc_used = ms;
 267   }
 268 
 269   void record_evac_fail_remove_self_forwards(double ms) {
 270     _cur_evac_fail_remove_self_forwards = ms;
 271   }
 272 
 273   void record_string_dedup_fixup_time(double ms) {
 274     _cur_string_dedup_fixup_time_ms = ms;
 275   }
 276 
 277   void record_ref_proc_time(double ms) {
 278     _cur_ref_proc_time_ms = ms;
 279   }
 280 
 281   void record_root_region_scan_wait_time(double time_ms) {
 282     _root_region_scan_wait_time_ms = time_ms;
 283   }
 284 
 285   void record_total_free_cset_time_ms(double time_ms) {
 286     _recorded_total_free_cset_time_ms = time_ms;
 287   }
 288 
 289   void record_serial_free_cset_time_ms(double time_ms) {
 290     _recorded_serial_free_cset_time_ms = time_ms;
 291   }
 292 
 293   void record_fast_reclaim_humongous_stats(double time_ms, size_t total, size_t candidates) {
 294     _cur_fast_reclaim_humongous_register_time_ms = time_ms;
 295     _cur_fast_reclaim_humongous_total = total;
 296     _cur_fast_reclaim_humongous_candidates = candidates;
 297   }
 298 
 299   void record_fast_reclaim_humongous_time_ms(double value, size_t reclaimed) {
 300     _cur_fast_reclaim_humongous_time_ms = value;
 301     _cur_fast_reclaim_humongous_reclaimed = reclaimed;
 302   }
 303 
 304   void record_young_cset_choice_time_ms(double time_ms) {
 305     _recorded_young_cset_choice_time_ms = time_ms;
 306   }
 307 
 308   void record_non_young_cset_choice_time_ms(double time_ms) {
 309     _recorded_non_young_cset_choice_time_ms = time_ms;
 310   }
 311 
 312   void record_redirty_logged_cards_time_ms(double time_ms) {
 313     _recorded_redirty_logged_cards_time_ms = time_ms;
 314   }
 315 
 316   void record_preserve_cm_referents_time_ms(double time_ms) {
 317     _recorded_preserve_cm_referents_time_ms = time_ms;
 318   }
 319 
 320   void record_merge_pss_time_ms(double time_ms) {
 321     _recorded_merge_pss_time_ms = time_ms;
 322   }
 323 
 324   void record_start_new_cset_time_ms(double time_ms) {
 325     _recorded_start_new_cset_time_ms = time_ms;
 326   }
 327 
 328   void record_cur_collection_start_sec(double time_ms) {
 329     _cur_collection_start_sec = time_ms;
 330   }
 331 
 332   void record_verify_before_time_ms(double time_ms) {
 333     _cur_verify_before_time_ms = time_ms;
 334   }
 335 
 336   void record_verify_after_time_ms(double time_ms) {
 337     _cur_verify_after_time_ms = time_ms;
 338   }
 339 
 340   void inc_external_accounted_time_ms(double time_ms) {
 341     _external_accounted_time_ms += time_ms;
 342   }
 343 
 344   void record_clear_claimed_marks_time_ms(double recorded_clear_claimed_marks_time_ms) {
 345     _recorded_clear_claimed_marks_time_ms = recorded_clear_claimed_marks_time_ms;
 346   }
 347 
 348   double cur_collection_start_sec() {
 349     return _cur_collection_start_sec;
 350   }
 351 
 352   double cur_collection_par_time_ms() {
 353     return _cur_collection_par_time_ms;
 354   }
 355 
 356   double cur_clear_ct_time_ms() {
 357     return _cur_clear_ct_time_ms;
 358   }
 359 
 360   double cur_expand_heap_time_ms() {
 361     return _cur_expand_heap_time_ms;
 362   }
 363 
 364   double root_region_scan_wait_time_ms() {
 365     return _root_region_scan_wait_time_ms;
 366   }
 367 
 368   double young_cset_choice_time_ms() {
 369     return _recorded_young_cset_choice_time_ms;
 370   }
 371 
 372   double total_free_cset_time_ms() {
 373     return _recorded_total_free_cset_time_ms;
 374   }
 375 
 376   double non_young_cset_choice_time_ms() {
 377     return _recorded_non_young_cset_choice_time_ms;
 378   }
 379 
 380   double fast_reclaim_humongous_time_ms() {
 381     return _cur_fast_reclaim_humongous_time_ms;
 382   }
 383 
 384   ReferenceProcessorPhaseTimes* ref_phase_times() { return &_ref_phase_times; }
 385 
 386   WeakProcessorPhaseTimes* weak_phase_times() { return &_weak_phase_times; }
 387 };
 388 
 389 class G1EvacPhaseWithTrimTimeTracker : public StackObj {
 390   G1ParScanThreadState* _pss;
 391   Ticks _start;
 392 
 393   Tickspan& _total_time;
 394   Tickspan& _trim_time;
 395 
 396   bool _stopped;
 397 public:
 398   G1EvacPhaseWithTrimTimeTracker(G1ParScanThreadState* pss, Tickspan& total_time, Tickspan& trim_time);
 399   ~G1EvacPhaseWithTrimTimeTracker();
 400 
 401   void stop();
 402 };
 403 
 404 class G1GCParPhaseTimesTracker : public CHeapObj<mtGC> {
 405 protected:
 406   Ticks _start_time;
 407   G1GCPhaseTimes::GCParPhases _phase;
 408   G1GCPhaseTimes* _phase_times;
 409   uint _worker_id;
 410   EventGCPhaseParallel _event;
 411 public:
 412   G1GCParPhaseTimesTracker(G1GCPhaseTimes* phase_times, G1GCPhaseTimes::GCParPhases phase, uint worker_id);
 413   virtual ~G1GCParPhaseTimesTracker();
 414 };
 415 
 416 class G1EvacPhaseTimesTracker : public G1GCParPhaseTimesTracker {
 417   Tickspan _total_time;
 418   Tickspan _trim_time;
 419 
 420   G1EvacPhaseWithTrimTimeTracker _trim_tracker;
 421 public:
 422   G1EvacPhaseTimesTracker(G1GCPhaseTimes* phase_times, G1ParScanThreadState* pss, G1GCPhaseTimes::GCParPhases phase, uint worker_id);
 423   virtual ~G1EvacPhaseTimesTracker();
 424 };
 425 
 426 #endif // SHARE_VM_GC_G1_G1GCPHASETIMES_HPP