1 /*
  2  * Copyright (c) 2017, 2021, Red Hat, Inc. 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_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP
 27 
 28 #include "jfr/jfrEvents.hpp"
 29 #include "gc/shenandoah/shenandoahNumberSeq.hpp"
 30 #include "gc/shared/workerDataArray.hpp"
 31 #include "memory/allocation.hpp"
 32 
 33 class ShenandoahCollectorPolicy;
 34 class outputStream;
 35 
 36 #define SHENANDOAH_PAR_PHASE_DO(CNT_PREFIX, DESC_PREFIX, f)                            \
 37   f(CNT_PREFIX ## TotalWork,                DESC_PREFIX "<total>")                     \
 38   f(CNT_PREFIX ## ThreadRoots,              DESC_PREFIX "Thread Roots")                \
 39   f(CNT_PREFIX ## CodeCacheRoots,           DESC_PREFIX "Code Cache Roots")            \
 40   f(CNT_PREFIX ## VMStrongRoots,            DESC_PREFIX "VM Strong Roots")             \
 41   f(CNT_PREFIX ## VMWeakRoots,              DESC_PREFIX "VM Weak Roots")               \
 42   f(CNT_PREFIX ## CLDGRoots,                DESC_PREFIX "CLDG Roots")                  \
 43   f(CNT_PREFIX ## CodeCacheUnload,          DESC_PREFIX "Unload Code Caches")          \
 44   f(CNT_PREFIX ## CLDUnlink,                DESC_PREFIX "Unlink CLDs")                 \
 45   f(CNT_PREFIX ## WeakRefProc,              DESC_PREFIX "Weak References")             \
 46   f(CNT_PREFIX ## ParallelMark,             DESC_PREFIX "Parallel Mark")               \
 47   f(CNT_PREFIX ## ScanClusters,             DESC_PREFIX "Scan Clusters")
 48   // end
 49 
 50 #define SHENANDOAH_PHASE_DO(f)                                                         \
 51   f(conc_reset,                                     "Concurrent Reset")                \
 52                                                                                        \
 53   f(init_mark_gross,                                "Pause Init Mark (G)")             \
 54   f(init_mark,                                      "Pause Init Mark (N)")             \
 55   f(init_manage_tlabs,                              "  Manage TLABs")                  \
 56   f(init_update_region_states,                      "  Update Region States")          \
 57                                                                                        \
 58   f(init_scan_rset,                                 "Concurrent Scan Remembered Set")  \
 59   SHENANDOAH_PAR_PHASE_DO(init_scan_rset_,          "  RS: ", f)                       \
 60                                                                                        \
 61   f(conc_mark_roots,                                "Concurrent Mark Roots ")          \
 62   SHENANDOAH_PAR_PHASE_DO(conc_mark_roots,          "  CMR: ", f)                      \
 63   f(conc_mark,                                      "Concurrent Marking")              \
 64                                                                                        \
 65   f(final_mark_gross,                               "Pause Final Mark (G)")            \
 66   f(final_mark,                                     "Pause Final Mark (N)")            \
 67   f(finish_mark,                                    "  Finish Mark")                   \
 68   SHENANDOAH_PAR_PHASE_DO(finish_mark_,             "    FM: ", f)                     \
 69   f(coalesce_and_fill,                              "Coalesce and Fill Old Dead")      \
 70   SHENANDOAH_PAR_PHASE_DO(coalesce_and_fill_,       "    CFOD: ", f)                   \
 71   f(purge,                                          "  System Purge")                  \
 72   SHENANDOAH_PAR_PHASE_DO(purge_cu_par_,            "      CU: ", f)                   \
 73   f(purge_weak_par,                                 "    Weak Roots")                  \
 74   SHENANDOAH_PAR_PHASE_DO(purge_weak_par_,          "      WR: ", f)                   \
 75   f(final_update_region_states,                     "  Update Region States")          \
 76   f(final_manage_labs,                              "  Manage GC/TLABs")               \
 77   f(choose_cset,                                    "  Choose Collection Set")         \
 78   f(final_rebuild_freeset,                          "  Rebuild Free Set")              \
 79   f(init_evac,                                      "  Initial Evacuation")            \
 80   SHENANDOAH_PAR_PHASE_DO(evac_,                    "    E: ", f)                      \
 81                                                                                        \
 82   f(conc_thread_roots,                              "Concurrent Thread Roots")         \
 83   SHENANDOAH_PAR_PHASE_DO(conc_thread_roots_,       "  CTR: ", f)                      \
 84   f(conc_weak_refs,                                 "Concurrent Weak References")      \
 85   SHENANDOAH_PAR_PHASE_DO(conc_weak_refs_,          "  CWRF: ", f)                     \
 86   f(conc_weak_roots,                                "Concurrent Weak Roots")           \
 87   f(conc_weak_roots_work,                           "  Roots")                         \
 88   SHENANDOAH_PAR_PHASE_DO(conc_weak_roots_work_,    "    CWR: ", f)                    \
 89   f(conc_weak_roots_rendezvous,                     "  Rendezvous")                    \
 90   f(conc_cleanup_early,                             "Concurrent Cleanup")              \
 91   f(conc_class_unload,                              "Concurrent Class Unloading")      \
 92   f(conc_class_unload_unlink,                       "  Unlink Stale")                  \
 93   f(conc_class_unload_unlink_sd,                    "    System Dictionary")           \
 94   f(conc_class_unload_unlink_weak_klass,            "    Weak Class Links")            \
 95   f(conc_class_unload_unlink_code_roots,            "    Code Roots")                  \
 96   f(conc_class_unload_rendezvous,                   "  Rendezvous")                    \
 97   f(conc_class_unload_purge,                        "  Purge Unlinked")                \
 98   f(conc_class_unload_purge_coderoots,              "    Code Roots")                  \
 99   f(conc_class_unload_purge_cldg,                   "    CLDG")                        \
100   f(conc_class_unload_purge_ec,                     "    Exception Caches")            \
101   f(conc_strong_roots,                              "Concurrent Strong Roots")         \
102   SHENANDOAH_PAR_PHASE_DO(conc_strong_roots_,       "  CSR: ", f)                      \
103   f(conc_evac,                                      "Concurrent Evacuation")           \
104                                                                                        \
105   f(final_roots_gross,                              "Pause Final Roots (G)")           \
106   f(final_roots,                                    "Pause Final Roots (N)")           \
107                                                                                        \
108   f(init_update_refs_gross,                         "Pause Init Update Refs (G)")      \
109   f(init_update_refs,                               "Pause Init Update Refs (N)")      \
110   f(init_update_refs_manage_gclabs,                 "  Manage GCLABs")                 \
111                                                                                        \
112   f(conc_update_refs,                               "Concurrent Update Refs")          \
113   f(conc_update_thread_roots,                       "Concurrent Update Thread Roots")  \
114                                                                                        \
115   f(final_update_refs_gross,                        "Pause Final Update Refs (G)")     \
116   f(final_update_refs,                              "Pause Final Update Refs (N)")     \
117   f(final_update_refs_finish_work,                  "  Finish Work")                   \
118   f(final_update_refs_update_region_states,         "  Update Region States")          \
119   f(final_update_refs_trash_cset,                   "  Trash Collection Set")          \
120   f(final_update_refs_rebuild_freeset,              "  Rebuild Free Set")              \
121                                                                                        \
122   f(conc_cleanup_complete,                          "Concurrent Cleanup")              \
123                                                                                        \
124   f(degen_gc_gross,                                 "Pause Degenerated GC (G)")        \
125   f(degen_gc,                                       "Pause Degenerated GC (N)")        \
126   f(degen_gc_stw_mark,                              "  Degen STW Mark")                \
127   SHENANDOAH_PAR_PHASE_DO(degen_gc_stw_mark_,       "    DSM: ", f)                    \
128   f(degen_gc_mark,                                  "  Degen Mark")                    \
129   SHENANDOAH_PAR_PHASE_DO(degen_gc_mark_,           "    DM: ", f)                     \
130   f(degen_gc_purge,                                 "    System Purge")                \
131   f(degen_gc_weakrefs,                              "      Weak References")           \
132   SHENANDOAH_PAR_PHASE_DO(degen_gc_weakrefs_p_,     "        WRP: ", f)                \
133   f(degen_gc_purge_class_unload,                    "      Unload Classes")            \
134   SHENANDOAH_PAR_PHASE_DO(degen_gc_purge_cu_par_,   "        DCU: ", f)                \
135   f(degen_gc_purge_weak_par,                        "      Weak Roots")                \
136   SHENANDOAH_PAR_PHASE_DO(degen_gc_purge_weak_p_,   "        DWR: ", f)                \
137   f(degen_gc_purge_cldg,                            "      CLDG")                      \
138   f(degen_gc_final_update_region_states,            "  Update Region States")          \
139   f(degen_gc_final_manage_labs,                     "  Manage GC/TLABs")               \
140   f(degen_gc_choose_cset,                           "  Choose Collection Set")         \
141   f(degen_gc_final_rebuild_freeset,                 "  Rebuild Free Set")              \
142   f(degen_gc_stw_evac,                              "  Evacuation")                    \
143   f(degen_gc_init_update_refs_manage_gclabs,        "  Manage GCLABs")                 \
144   f(degen_gc_updaterefs,                            "  Update References")             \
145   f(degen_gc_final_update_refs_finish_work,         "  Finish Work")                   \
146   f(degen_gc_final_update_refs_update_region_states,"  Update Region States")          \
147   f(degen_gc_final_update_refs_trash_cset,          "  Trash Collection Set")          \
148   f(degen_gc_final_update_refs_rebuild_freeset,     "  Rebuild Free Set")              \
149   f(degen_gc_update_roots,                          "  Degen Update Roots")            \
150   SHENANDOAH_PAR_PHASE_DO(degen_gc_update_,         "    DU: ", f)                     \
151   f(degen_gc_cleanup_complete,                      "  Cleanup")                       \
152                                                                                        \
153   f(full_gc_gross,                                  "Pause Full GC (G)")               \
154   f(full_gc,                                        "Pause Full GC (N)")               \
155   f(full_gc_heapdump_pre,                           "  Pre Heap Dump")                 \
156   f(full_gc_prepare,                                "  Prepare")                       \
157   f(full_gc_update_roots,                           "    Update Roots")                \
158   SHENANDOAH_PAR_PHASE_DO(full_gc_update_roots_,    "      FU: ", f)                   \
159   f(full_gc_mark,                                   "  Mark")                          \
160   SHENANDOAH_PAR_PHASE_DO(full_gc_mark_,            "    FM: ", f)                     \
161   f(full_gc_purge,                                  "    System Purge")                \
162   f(full_gc_weakrefs,                               "      Weak References")           \
163   SHENANDOAH_PAR_PHASE_DO(full_gc_weakrefs_p_,      "        WRP: ", f)                \
164   f(full_gc_purge_class_unload,                     "      Unload Classes")            \
165   SHENANDOAH_PAR_PHASE_DO(full_gc_purge_cu_par_,    "        CU: ", f)                 \
166   f(full_gc_purge_weak_par,                         "      Weak Roots")                \
167   SHENANDOAH_PAR_PHASE_DO(full_gc_purge_weak_p_,    "        WR: ", f)                 \
168   f(full_gc_purge_cldg,                             "      CLDG")                      \
169   f(full_gc_calculate_addresses,                    "  Calculate Addresses")           \
170   f(full_gc_calculate_addresses_regular,            "    Regular Objects")             \
171   f(full_gc_calculate_addresses_humong,             "    Humongous Objects")           \
172   f(full_gc_adjust_pointers,                        "  Adjust Pointers")               \
173   f(full_gc_adjust_roots,                           "  Adjust Roots")                  \
174   SHENANDOAH_PAR_PHASE_DO(full_gc_adjust_roots_,    "    FA: ", f)                     \
175   f(full_gc_copy_objects,                           "  Copy Objects")                  \
176   f(full_gc_copy_objects_regular,                   "    Regular Objects")             \
177   f(full_gc_copy_objects_humong,                    "    Humongous Objects")           \
178   f(full_gc_copy_objects_reset_complete,            "    Reset Complete Bitmap")       \
179   f(full_gc_copy_objects_rebuild,                   "    Rebuild Region Sets")         \
180   f(full_gc_reconstruct_remembered_set,             "    Reconstruct Remembered Set")  \
181   f(full_gc_heapdump_post,                          "  Post Heap Dump")                \
182                                                                                        \
183   f(conc_uncommit,                                  "Concurrent Uncommit")             \
184   f(pacing,                                         "Pacing")                          \
185                                                                                        \
186   f(heap_iteration_roots,                           "Heap Iteration")                  \
187   SHENANDOAH_PAR_PHASE_DO(heap_iteration_roots_,    "  HI: ", f)                       \
188   // end
189 
190 typedef WorkerDataArray<double> ShenandoahWorkerData;
191 
192 class ShenandoahPhaseTimings : public CHeapObj<mtGC> {
193   friend class ShenandoahGCPhase;
194   friend class ShenandoahWorkerTimingsTracker;
195 public:
196 #define SHENANDOAH_PHASE_DECLARE_ENUM(type, title)   type,
197 
198   enum Phase {
199     SHENANDOAH_PHASE_DO(SHENANDOAH_PHASE_DECLARE_ENUM)
200     _num_phases,
201     _invalid_phase = _num_phases
202   };
203 
204   enum ParPhase {
205     SHENANDOAH_PAR_PHASE_DO(,, SHENANDOAH_PHASE_DECLARE_ENUM)
206     _num_par_phases
207   };
208 
209 #undef SHENANDOAH_PHASE_DECLARE_ENUM
210 
211 private:
212   uint                _max_workers;
213   double              _cycle_data[_num_phases];
214   HdrSeq              _global_data[_num_phases];
215   static const char*  _phase_names[_num_phases];
216 
217   ShenandoahWorkerData* _worker_data[_num_phases];
218   ShenandoahCollectorPolicy* _policy;
219 
220   static bool is_worker_phase(Phase phase);
221   static bool is_root_work_phase(Phase phase);
222 
223   ShenandoahWorkerData* worker_data(Phase phase, ParPhase par_phase);
224   Phase worker_par_phase(Phase phase, ParPhase par_phase);
225 
226   void set_cycle_data(Phase phase, double time);
227   static double uninitialized() { return -1; }
228 
229 public:
230   ShenandoahPhaseTimings(uint max_workers);
231 
232   void record_phase_time(Phase phase, double time);
233 
234   void record_workers_start(Phase phase);
235   void record_workers_end(Phase phase);
236 
237   void flush_par_workers_to_cycle();
238   void flush_cycle_to_global();
239 
240   static const char* phase_name(Phase phase) {
241     assert(phase >= 0 && phase < _num_phases, "Out of bound");
242     return _phase_names[phase];
243   }
244 
245   void print_cycle_on(outputStream* out) const;
246   void print_global_on(outputStream* out) const;
247 };
248 
249 class ShenandoahWorkerTimingsTracker : public StackObj {
250 private:
251   ShenandoahPhaseTimings*          const _timings;
252   ShenandoahPhaseTimings::Phase    const _phase;
253   ShenandoahPhaseTimings::ParPhase const _par_phase;
254   uint const _worker_id;
255 
256   double _start_time;
257   EventGCPhaseParallel _event;
258 public:
259   ShenandoahWorkerTimingsTracker(ShenandoahPhaseTimings::Phase phase, ShenandoahPhaseTimings::ParPhase par_phase, uint worker_id);
260   ~ShenandoahWorkerTimingsTracker();
261 };
262 
263 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP