1 /*
  2  * Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP
 28 
 29 #include "jfr/jfrEvents.hpp"
 30 #include "gc/shenandoah/shenandoahNumberSeq.hpp"
 31 #include "gc/shared/workerDataArray.hpp"
 32 #include "memory/allocation.hpp"
 33 
 34 class ShenandoahCollectorPolicy;
 35 class outputStream;
 36 
 37 #define SHENANDOAH_PAR_PHASE_DO(CNT_PREFIX, DESC_PREFIX, f)                            \
 38   f(CNT_PREFIX ## TotalWork,                DESC_PREFIX "<total>")                     \
 39   f(CNT_PREFIX ## ThreadRoots,              DESC_PREFIX "Thread Roots")                \
 40   f(CNT_PREFIX ## CodeCacheRoots,           DESC_PREFIX "Code Cache Roots")            \
 41   f(CNT_PREFIX ## VMStrongRoots,            DESC_PREFIX "VM Strong Roots")             \
 42   f(CNT_PREFIX ## VMWeakRoots,              DESC_PREFIX "VM Weak Roots")               \
 43   f(CNT_PREFIX ## CLDGRoots,                DESC_PREFIX "CLDG Roots")                  \
 44   f(CNT_PREFIX ## CodeCacheUnload,          DESC_PREFIX "Unload Code Caches")          \
 45   f(CNT_PREFIX ## CLDUnlink,                DESC_PREFIX "Unlink CLDs")                 \
 46   f(CNT_PREFIX ## WeakRefProc,              DESC_PREFIX "Weak References")             \
 47   f(CNT_PREFIX ## ParallelMark,             DESC_PREFIX "Parallel Mark")               \
 48   f(CNT_PREFIX ## ScanClusters,             DESC_PREFIX "Scan Clusters")               \
 49   // end
 50 
 51 #define SHENANDOAH_PHASE_DO(f)                                                         \
 52   f(conc_reset,                                     "Concurrent Reset")                \
 53   f(conc_reset_old,                                 "Concurrent Reset (OLD)")          \
 54   f(init_mark_gross,                                "Pause Init Mark (G)")             \
 55   f(init_mark,                                      "Pause Init Mark (N)")             \
 56   f(init_manage_tlabs,                              "  Manage TLABs")                  \
 57   f(init_swap_rset,                                 "  Swap Remembered Set")           \
 58   f(init_transfer_satb,                             "  Transfer Old From SATB")        \
 59   f(init_update_region_states,                      "  Update Region States")          \
 60                                                                                        \
 61   f(init_scan_rset,                                 "Concurrent Scan Remembered Set")  \
 62   SHENANDOAH_PAR_PHASE_DO(init_scan_rset_,          "  RS: ", f)                       \
 63                                                                                        \
 64   f(conc_mark_roots,                                "Concurrent Mark Roots ")          \
 65   SHENANDOAH_PAR_PHASE_DO(conc_mark_roots,          "  CMR: ", f)                      \
 66   f(conc_mark,                                      "Concurrent Marking")              \
 67   SHENANDOAH_PAR_PHASE_DO(conc_mark,                "  CM: ", f)                       \
 68                                                                                        \
 69   f(final_mark_gross,                               "Pause Final Mark (G)")            \
 70   f(final_mark,                                     "Pause Final Mark (N)")            \
 71   f(finish_mark,                                    "  Finish Mark")                   \
 72   SHENANDOAH_PAR_PHASE_DO(finish_mark_,             "    FM: ", f)                     \
 73   f(purge,                                          "  System Purge")                  \
 74   SHENANDOAH_PAR_PHASE_DO(purge_cu_par_,            "      CU: ", f)                   \
 75   f(purge_weak_par,                                 "    Weak Roots")                  \
 76   SHENANDOAH_PAR_PHASE_DO(purge_weak_par_,          "      WR: ", f)                   \
 77   f(final_update_region_states,                     "  Update Region States")          \
 78   f(final_manage_labs,                              "  Manage GC/TLABs")               \
 79   f(choose_cset,                                    "  Choose Collection Set")         \
 80   f(final_rebuild_freeset,                          "  Rebuild Free Set")              \
 81   f(init_evac,                                      "  Initial Evacuation")            \
 82   SHENANDOAH_PAR_PHASE_DO(evac_,                    "    E: ", f)                      \
 83                                                                                        \
 84   f(conc_thread_roots,                              "Concurrent Thread Roots")         \
 85   SHENANDOAH_PAR_PHASE_DO(conc_thread_roots_,       "  CTR: ", f)                      \
 86   f(conc_weak_refs,                                 "Concurrent Weak References")      \
 87   SHENANDOAH_PAR_PHASE_DO(conc_weak_refs_,          "  CWRF: ", f)                     \
 88   f(conc_weak_roots,                                "Concurrent Weak Roots")           \
 89   f(conc_weak_roots_work,                           "  Roots")                         \
 90   SHENANDOAH_PAR_PHASE_DO(conc_weak_roots_work_,    "    CWR: ", f)                    \
 91   f(conc_weak_roots_rendezvous,                     "  Rendezvous")                    \
 92   f(conc_cleanup_early,                             "Concurrent Cleanup")              \
 93   f(conc_class_unload,                              "Concurrent Class Unloading")      \
 94   f(conc_class_unload_unlink,                       "  Unlink Stale")                  \
 95   f(conc_class_unload_unlink_sd,                    "    System Dictionary")           \
 96   f(conc_class_unload_unlink_weak_klass,            "    Weak Class Links")            \
 97   f(conc_class_unload_unlink_code_roots,            "    Code Roots")                  \
 98   f(conc_class_unload_rendezvous,                   "  Rendezvous")                    \
 99   f(conc_class_unload_purge,                        "  Purge Unlinked")                \
100   f(conc_class_unload_purge_coderoots,              "    Code Roots")                  \
101   f(conc_class_unload_purge_cldg,                   "    CLDG")                        \
102   f(conc_class_unload_purge_ec,                     "    Exception Caches")            \
103   f(conc_strong_roots,                              "Concurrent Strong Roots")         \
104   SHENANDOAH_PAR_PHASE_DO(conc_strong_roots_,       "  CSR: ", f)                      \
105   f(coalesce_and_fill,                              "Coalesce and Fill Old Dead")      \
106   SHENANDOAH_PAR_PHASE_DO(coalesce_and_fill_,       "  CFOD: ", f)                     \
107   f(conc_evac,                                      "Concurrent Evacuation")           \
108                                                                                        \
109   f(final_roots_gross,                              "Pause Final Roots (G)")           \
110   f(final_roots,                                    "Pause Final Roots (N)")           \
111                                                                                        \
112   f(init_update_refs_gross,                         "Pause Init Update Refs (G)")      \
113   f(init_update_refs,                               "Pause Init Update Refs (N)")      \
114   f(init_update_refs_manage_gclabs,                 "  Manage GCLABs")                 \
115                                                                                        \
116   f(conc_update_refs,                               "Concurrent Update Refs")          \
117   f(conc_update_thread_roots,                       "Concurrent Update Thread Roots")  \
118                                                                                        \
119   f(final_update_refs_gross,                        "Pause Final Update Refs (G)")     \
120   f(final_update_refs,                              "Pause Final Update Refs (N)")     \
121   f(final_update_refs_finish_work,                  "  Finish Work")                   \
122   f(final_update_refs_update_region_states,         "  Update Region States")          \
123   f(final_update_refs_trash_cset,                   "  Trash Collection Set")          \
124   f(final_update_refs_rebuild_freeset,              "  Rebuild Free Set")              \
125                                                                                        \
126   f(conc_cleanup_complete,                          "Concurrent Cleanup")              \
127                                                                                        \
128   f(degen_gc_gross,                                 "Pause Degenerated GC (G)")        \
129   f(degen_gc,                                       "Pause Degenerated GC (N)")        \
130   f(degen_gc_stw_mark,                              "  Degen STW Mark")                \
131   SHENANDOAH_PAR_PHASE_DO(degen_gc_stw_mark_,       "    DSM: ", f)                    \
132   f(degen_gc_mark,                                  "  Degen Mark")                    \
133   SHENANDOAH_PAR_PHASE_DO(degen_gc_mark_,           "    DM: ", f)                     \
134   f(degen_gc_purge,                                 "    System Purge")                \
135   f(degen_gc_weakrefs,                              "      Weak References")           \
136   SHENANDOAH_PAR_PHASE_DO(degen_gc_weakrefs_p_,     "        WRP: ", f)                \
137   f(degen_gc_purge_class_unload,                    "      Unload Classes")            \
138   SHENANDOAH_PAR_PHASE_DO(degen_gc_purge_cu_par_,   "        DCU: ", f)                \
139   f(degen_gc_purge_weak_par,                        "      Weak Roots")                \
140   SHENANDOAH_PAR_PHASE_DO(degen_gc_purge_weak_p_,   "        DWR: ", f)                \
141   f(degen_gc_purge_cldg,                            "      CLDG")                      \
142   f(degen_gc_final_update_region_states,            "  Update Region States")          \
143   f(degen_gc_final_manage_labs,                     "  Manage GC/TLABs")               \
144   f(degen_gc_choose_cset,                           "  Choose Collection Set")         \
145   f(degen_gc_final_rebuild_freeset,                 "  Rebuild Free Set")              \
146   f(degen_gc_stw_evac,                              "  Evacuation")                    \
147   f(degen_gc_init_update_refs_manage_gclabs,        "  Manage GCLABs")                 \
148   f(degen_gc_updaterefs,                            "  Update References")             \
149   f(degen_gc_final_update_refs_finish_work,         "  Finish Work")                   \
150   f(degen_gc_final_update_refs_update_region_states,"  Update Region States")          \
151   f(degen_gc_final_update_refs_trash_cset,          "  Trash Collection Set")          \
152   f(degen_gc_final_update_refs_rebuild_freeset,     "  Rebuild Free Set")              \
153   f(degen_gc_update_roots,                          "  Degen Update Roots")            \
154   SHENANDOAH_PAR_PHASE_DO(degen_gc_update_,         "    DU: ", f)                     \
155   f(degen_gc_cleanup_complete,                      "  Cleanup")                       \
156                                                                                        \
157   f(full_gc_gross,                                  "Pause Full GC (G)")               \
158   f(full_gc,                                        "Pause Full GC (N)")               \
159   f(full_gc_heapdump_pre,                           "  Pre Heap Dump")                 \
160   f(full_gc_prepare,                                "  Prepare")                       \
161   f(full_gc_update_roots,                           "    Update Roots")                \
162   SHENANDOAH_PAR_PHASE_DO(full_gc_update_roots_,    "      FU: ", f)                   \
163   f(full_gc_mark,                                   "  Mark")                          \
164   SHENANDOAH_PAR_PHASE_DO(full_gc_mark_,            "    FM: ", f)                     \
165   f(full_gc_purge,                                  "    System Purge")                \
166   f(full_gc_weakrefs,                               "      Weak References")           \
167   SHENANDOAH_PAR_PHASE_DO(full_gc_weakrefs_p_,      "        WRP: ", f)                \
168   f(full_gc_purge_class_unload,                     "      Unload Classes")            \
169   SHENANDOAH_PAR_PHASE_DO(full_gc_purge_cu_par_,    "        CU: ", f)                 \
170   f(full_gc_purge_weak_par,                         "      Weak Roots")                \
171   SHENANDOAH_PAR_PHASE_DO(full_gc_purge_weak_p_,    "        WR: ", f)                 \
172   f(full_gc_purge_cldg,                             "      CLDG")                      \
173   f(full_gc_calculate_addresses,                    "  Calculate Addresses")           \
174   f(full_gc_calculate_addresses_regular,            "    Regular Objects")             \
175   f(full_gc_calculate_addresses_humong,             "    Humongous Objects")           \
176   f(full_gc_adjust_pointers,                        "  Adjust Pointers")               \
177   f(full_gc_adjust_roots,                           "  Adjust Roots")                  \
178   SHENANDOAH_PAR_PHASE_DO(full_gc_adjust_roots_,    "    FA: ", f)                     \
179   f(full_gc_copy_objects,                           "  Copy Objects")                  \
180   f(full_gc_copy_objects_regular,                   "    Regular Objects")             \
181   f(full_gc_copy_objects_humong,                    "    Humongous Objects")           \
182   f(full_gc_recompute_generation_usage,             "    Recompute generation usage")  \
183   f(full_gc_copy_objects_reset_complete,            "    Reset Complete Bitmap")       \
184   f(full_gc_copy_objects_rebuild,                   "    Rebuild Region Sets")         \
185   f(full_gc_reconstruct_remembered_set,             "    Reconstruct Remembered Set")  \
186   f(full_gc_heapdump_post,                          "  Post Heap Dump")                \
187                                                                                        \
188   f(conc_uncommit,                                  "Concurrent Uncommit")             \
189   f(pacing,                                         "Pacing")                          \
190                                                                                        \
191   f(heap_iteration_roots,                           "Heap Iteration")                  \
192   SHENANDOAH_PAR_PHASE_DO(heap_iteration_roots_,    "  HI: ", f)                       \
193   // end
194 
195 typedef WorkerDataArray<double> ShenandoahWorkerData;
196 
197 class ShenandoahPhaseTimings : public CHeapObj<mtGC> {
198   friend class ShenandoahGCPhase;
199   friend class ShenandoahWorkerTimingsTracker;
200 public:
201 #define SHENANDOAH_PHASE_DECLARE_ENUM(type, title)   type,
202 
203   enum Phase {
204     SHENANDOAH_PHASE_DO(SHENANDOAH_PHASE_DECLARE_ENUM)
205     _num_phases,
206     _invalid_phase = _num_phases
207   };
208 
209   enum ParPhase {
210     SHENANDOAH_PAR_PHASE_DO(,, SHENANDOAH_PHASE_DECLARE_ENUM)
211     _num_par_phases
212   };
213 
214 #undef SHENANDOAH_PHASE_DECLARE_ENUM
215 
216 private:
217   uint                _max_workers;
218   double              _cycle_data[_num_phases];
219   HdrSeq              _global_data[_num_phases];
220   static const char*  _phase_names[_num_phases];
221 
222   ShenandoahWorkerData* _worker_data[_num_phases];
223   ShenandoahCollectorPolicy* _policy;
224 
225   static bool is_worker_phase(Phase phase);
226   static bool is_root_work_phase(Phase phase);
227 
228   ShenandoahWorkerData* worker_data(Phase phase, ParPhase par_phase);
229   Phase worker_par_phase(Phase phase, ParPhase par_phase);
230 
231   void set_cycle_data(Phase phase, double time);
232   static double uninitialized() { return -1; }
233 
234 public:
235   ShenandoahPhaseTimings(uint max_workers);
236 
237   void record_phase_time(Phase phase, double time);
238 
239   void record_workers_start(Phase phase);
240   void record_workers_end(Phase phase);
241 
242   void flush_par_workers_to_cycle();
243   void flush_cycle_to_global();
244 
245   static const char* phase_name(Phase phase) {
246     assert(phase >= 0 && phase < _num_phases, "Out of bound");
247     return _phase_names[phase];
248   }
249 
250   void print_cycle_on(outputStream* out) const;
251   void print_global_on(outputStream* out) const;
252 };
253 
254 class ShenandoahWorkerTimingsTracker : public StackObj {
255 private:
256   ShenandoahPhaseTimings*          const _timings;
257   ShenandoahPhaseTimings::Phase    const _phase;
258   ShenandoahPhaseTimings::ParPhase const _par_phase;
259   uint const _worker_id;
260 
261   double _start_time;
262   EventGCPhaseParallel _event;
263 public:
264   ShenandoahWorkerTimingsTracker(ShenandoahPhaseTimings::Phase phase,
265                                  ShenandoahPhaseTimings::ParPhase par_phase,
266                                  uint worker_id,
267                                  bool cumulative = false);
268   ~ShenandoahWorkerTimingsTracker();
269 };
270 
271 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP