1 /*
  2  * Copyright (c) 2017, 2020, 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 ## UniverseRoots,            DESC_PREFIX "Universe 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 ## ObjectSynchronizerRoots,  DESC_PREFIX "Synchronizer Roots")          \
 44   f(CNT_PREFIX ## ManagementRoots,          DESC_PREFIX "Management Roots")            \
 45   f(CNT_PREFIX ## CLDGRoots,                DESC_PREFIX "CLDG Roots")                  \
 46   f(CNT_PREFIX ## JVMTIRoots,               DESC_PREFIX "JVMTI Roots")                 \
 47   f(CNT_PREFIX ## JVMTIWeakRoots,           DESC_PREFIX "JVMTI Weak Roots")            \
 48   f(CNT_PREFIX ## JFRWeakRoots,             DESC_PREFIX "JFR Weak Roots")              \
 49   f(CNT_PREFIX ## StringDedupTableRoots,    DESC_PREFIX "Dedup Table Roots")           \
 50   f(CNT_PREFIX ## StringDedupQueueRoots,    DESC_PREFIX "Dedup Queue Roots")           \
 51   f(CNT_PREFIX ## FinishQueues,             DESC_PREFIX "Finish Queues")               \
 52   // end
 53 
 54 #define SHENANDOAH_PHASE_DO(f)                                                         \
 55   f(conc_reset,                                     "Concurrent Reset")                \
 56                                                                                        \
 57   f(init_mark_gross,                                "Pause Init Mark (G)")             \
 58   f(init_mark,                                      "Pause Init Mark (N)")             \
 59   f(make_parsable,                                  "  Make Parsable")                 \
 60   f(init_update_region_states,                      "  Update Region States")          \
 61   f(scan_roots,                                     "  Scan Roots")                    \
 62   SHENANDOAH_PAR_PHASE_DO(scan_,                    "    S: ", f)                      \
 63   f(resize_tlabs,                                   "  Resize TLABs")                  \
 64                                                                                        \
 65   f(conc_mark,                                      "Concurrent Marking")              \
 66   f(conc_mark_roots,                                "  Roots ")                        \
 67   SHENANDOAH_PAR_PHASE_DO(conc_mark_roots,          "    CM: ", f)                     \
 68                                                                                        \
 69   f(conc_preclean,                                  "Concurrent Precleaning")          \
 70                                                                                        \
 71   f(final_mark_gross,                               "Pause Final Mark (G)")            \
 72   f(final_mark,                                     "Pause Final Mark (N)")            \
 73   f(update_roots,                                   "  Update Roots")                  \
 74   SHENANDOAH_PAR_PHASE_DO(update_,                  "    U: ", f)                      \
 75   f(finish_queues,                                  "  Finish Queues")                 \
 76   f(weakrefs,                                       "  Weak References")               \
 77   f(weakrefs_process,                               "    Process")                     \
 78   f(purge,                                          "  System Purge")                  \
 79   f(purge_class_unload,                             "    Unload Classes")              \
 80   SHENANDOAH_PAR_PHASE_DO(purge_cu_par_,            "      CU: ", f)                   \
 81   f(purge_weak_par,                                 "    Weak Roots")                  \
 82   SHENANDOAH_PAR_PHASE_DO(purge_weak_par_,          "      WR: ", f)                   \
 83   f(purge_cldg,                                     "    CLDG")                        \
 84   f(final_update_region_states,                     "  Update Region States")          \
 85   f(retire_tlabs,                                   "  Retire TLABs")                  \
 86   f(choose_cset,                                    "  Choose Collection Set")         \
 87   f(final_rebuild_freeset,                          "  Rebuild Free Set")              \
 88   f(init_evac,                                      "  Initial Evacuation")            \
 89   SHENANDOAH_PAR_PHASE_DO(evac_,                    "    E: ", f)                      \
 90                                                                                        \
 91   f(conc_weak_roots,                                "Concurrent Weak Roots")           \
 92   f(conc_weak_roots_work,                           "  Roots")                         \
 93   SHENANDOAH_PAR_PHASE_DO(conc_weak_roots_work_,    "    CWR: ", f)                    \
 94   f(conc_weak_roots_rendezvous,                     "  Rendezvous")                    \
 95   f(conc_cleanup_early,                             "Concurrent Cleanup")              \
 96   f(conc_class_unload,                              "Concurrent Class Unloading")      \
 97   f(conc_class_unload_unlink,                       "  Unlink Stale")                  \
 98   f(conc_class_unload_unlink_sd,                    "    System Dictionary")           \
 99   f(conc_class_unload_unlink_weak_klass,            "    Weak Class Links")            \
100   f(conc_class_unload_unlink_code_roots,            "    Code Roots")                  \
101   f(conc_class_unload_rendezvous,                   "  Rendezvous")                    \
102   f(conc_class_unload_purge,                        "  Purge Unlinked")                \
103   f(conc_class_unload_purge_coderoots,              "    Code Roots")                  \
104   f(conc_class_unload_purge_cldg,                   "    CLDG")                        \
105   f(conc_class_unload_purge_ec,                     "    Exception Caches")            \
106   f(conc_strong_roots,                              "Concurrent Strong Roots")         \
107   SHENANDOAH_PAR_PHASE_DO(conc_strong_roots_,       "  CSR: ", f)                      \
108   f(conc_evac,                                      "Concurrent Evacuation")           \
109                                                                                        \
110   f(init_update_refs_gross,                         "Pause Init  Update Refs (G)")     \
111   f(init_update_refs,                               "Pause Init  Update Refs (N)")     \
112   f(init_update_refs_retire_gclabs,                 "  Retire GCLABs")                 \
113                                                                                        \
114   f(conc_update_refs,                               "Concurrent Update Refs")          \
115                                                                                        \
116   f(final_update_refs_gross,                        "Pause Final Update Refs (G)")     \
117   f(final_update_refs,                              "Pause Final Update Refs (N)")     \
118   f(final_update_refs_finish_work,                  "  Finish Work")                   \
119   f(final_update_refs_roots,                        "  Update Roots")                  \
120   SHENANDOAH_PAR_PHASE_DO(final_update_,            "    UR: ", f)                     \
121   f(final_update_refs_update_region_states,         "  Update Region States")          \
122   f(final_update_refs_trash_cset,                   "  Trash Collection Set")          \
123   f(final_update_refs_rebuild_freeset,              "  Rebuild Free Set")              \
124                                                                                        \
125   f(conc_cleanup_complete,                          "Concurrent Cleanup")              \
126                                                                                        \
127   f(degen_gc_gross,                                 "Pause Degenerated GC (G)")        \
128   f(degen_gc,                                       "Pause Degenerated GC (N)")        \
129   f(degen_gc_scan_conc_roots,                       "  Degen Mark Roots")              \
130   SHENANDOAH_PAR_PHASE_DO(degen_gc_conc_mark_,      "    DM: ", f)                     \
131   f(degen_gc_update_roots,                          "  Degen Update Roots")            \
132   SHENANDOAH_PAR_PHASE_DO(degen_gc_update_,         "    DU: ", f)                     \
133                                                                                        \
134   f(full_gc_gross,                                  "Pause Full GC (G)")               \
135   f(full_gc,                                        "Pause Full GC (N)")               \
136   f(full_gc_heapdump_pre,                           "  Pre Heap Dump")                 \
137   f(full_gc_prepare,                                "  Prepare")                       \
138   f(full_gc_update_roots,                           "    Update Roots")                \
139   SHENANDOAH_PAR_PHASE_DO(full_gc_update_roots_,    "      FU: ", f)                   \
140   f(full_gc_scan_roots,                             "  Scan Roots")                    \
141   SHENANDOAH_PAR_PHASE_DO(full_gc_scan_roots_,      "    FS: ", f)                     \
142   f(full_gc_scan_conc_roots,                        "  Scan Concurrent Roots")         \
143   SHENANDOAH_PAR_PHASE_DO(full_gc_scan_conc_roots,  "    FCS: ", f)                    \
144   f(full_gc_mark,                                   "  Mark")                          \
145   f(full_gc_mark_finish_queues,                     "    Finish Queues")               \
146   f(full_gc_weakrefs,                               "    Weak References")             \
147   f(full_gc_weakrefs_process,                       "      Process")                   \
148   f(full_gc_purge,                                  "    System Purge")                \
149   f(full_gc_purge_class_unload,                     "      Unload Classes")            \
150   SHENANDOAH_PAR_PHASE_DO(full_gc_purge_cu_par_,    "        CU: ", f)                 \
151   f(full_gc_purge_weak_par,                         "      Weak Roots")                \
152   SHENANDOAH_PAR_PHASE_DO(full_gc_purge_weak_p_,    "        WR: ", f)                 \
153   f(full_gc_purge_cldg,                             "      CLDG")                      \
154   f(full_gc_calculate_addresses,                    "  Calculate Addresses")           \
155   f(full_gc_calculate_addresses_regular,            "    Regular Objects")             \
156   f(full_gc_calculate_addresses_humong,             "    Humongous Objects")           \
157   f(full_gc_adjust_pointers,                        "  Adjust Pointers")               \
158   f(full_gc_adjust_roots,                           "  Adjust Roots")                  \
159   SHENANDOAH_PAR_PHASE_DO(full_gc_adjust_roots_,    "    FA: ", f)                     \
160   f(full_gc_copy_objects,                           "  Copy Objects")                  \
161   f(full_gc_copy_objects_regular,                   "    Regular Objects")             \
162   f(full_gc_copy_objects_humong,                    "    Humongous Objects")           \
163   f(full_gc_copy_objects_reset_complete,            "    Reset Complete Bitmap")       \
164   f(full_gc_copy_objects_rebuild,                   "    Rebuild Region Sets")         \
165   f(full_gc_resize_tlabs,                           "  Resize TLABs")                  \
166   f(full_gc_heapdump_post,                          "  Post Heap Dump")                \
167                                                                                        \
168   f(conc_uncommit,                                  "Concurrent Uncommit")             \
169                                                                                        \
170   f(heap_iteration_roots,                           "Heap Iteration")                  \
171   SHENANDOAH_PAR_PHASE_DO(heap_iteration_roots_,    "  HI: ", f)                       \
172   // end
173 
174 typedef WorkerDataArray<double> ShenandoahWorkerData;
175 
176 class ShenandoahPhaseTimings : public CHeapObj<mtGC> {
177   friend class ShenandoahGCPhase;
178   friend class ShenandoahWorkerTimingsTracker;
179 public:
180 #define SHENANDOAH_PHASE_DECLARE_ENUM(type, title)   type,
181 
182   enum Phase {
183     SHENANDOAH_PHASE_DO(SHENANDOAH_PHASE_DECLARE_ENUM)
184     _num_phases,
185     _invalid_phase = _num_phases
186   };
187 
188   enum ParPhase {
189     SHENANDOAH_PAR_PHASE_DO(,, SHENANDOAH_PHASE_DECLARE_ENUM)
190     _num_par_phases
191   };
192 
193 #undef SHENANDOAH_PHASE_DECLARE_ENUM
194 
195 private:
196   uint                _max_workers;
197   double              _cycle_data[_num_phases];
198   HdrSeq              _global_data[_num_phases];
199   static const char*  _phase_names[_num_phases];
200 
201   ShenandoahWorkerData* _worker_data[_num_phases];
202   ShenandoahCollectorPolicy* _policy;
203 
204   static bool is_worker_phase(Phase phase);
205   static bool is_root_work_phase(Phase phase);
206 
207   ShenandoahWorkerData* worker_data(Phase phase, ParPhase par_phase);
208   Phase worker_par_phase(Phase phase, ParPhase par_phase);
209 
210   void set_cycle_data(Phase phase, double time);
211   static double uninitialized() { return -1; }
212 
213 public:
214   ShenandoahPhaseTimings(uint max_workers);
215 
216   void record_phase_time(Phase phase, double time);
217 
218   void record_workers_start(Phase phase);
219   void record_workers_end(Phase phase);
220 
221   void flush_par_workers_to_cycle();
222   void flush_cycle_to_global();
223 
224   static const char* phase_name(Phase phase) {
225     assert(phase >= 0 && phase < _num_phases, "Out of bound");
226     return _phase_names[phase];
227   }
228 
229   void print_cycle_on(outputStream* out) const;
230   void print_global_on(outputStream* out) const;
231 };
232 
233 class ShenandoahWorkerTimingsTracker : public StackObj {
234 private:
235   ShenandoahPhaseTimings*          const _timings;
236   ShenandoahPhaseTimings::Phase    const _phase;
237   ShenandoahPhaseTimings::ParPhase const _par_phase;
238   uint const _worker_id;
239 
240   double _start_time;
241   EventGCPhaseParallel _event;
242 public:
243   ShenandoahWorkerTimingsTracker(ShenandoahPhaseTimings::Phase phase, ShenandoahPhaseTimings::ParPhase par_phase, uint worker_id);
244   ~ShenandoahWorkerTimingsTracker();
245 };
246 
247 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHPHASETIMINGS_HPP