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