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 #include "precompiled.hpp"
 26 
 27 #include "gc/shared/workerDataArray.inline.hpp"
 28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 29 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahUtils.hpp"
 32 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 33 #include "runtime/orderAccess.hpp"
 34 #include "utilities/ostream.hpp"
 35 
 36 #define SHENANDOAH_PHASE_NAME_FORMAT "%-30s"
 37 #define SHENANDOAH_S_TIME_FORMAT "%8.3lf"
 38 #define SHENANDOAH_US_TIME_FORMAT "%8.0lf"
 39 #define SHENANDOAH_US_WORKER_TIME_FORMAT "%3.0lf"
 40 #define SHENANDOAH_US_WORKER_NOTIME_FORMAT "%3s"
 41 #define SHENANDOAH_PARALLELISM_FORMAT "%4.2lf"
 42 
 43 #define SHENANDOAH_PHASE_DECLARE_NAME(type, title) \
 44   title,
 45 
 46 const char* ShenandoahPhaseTimings::_phase_names[] = {
 47   SHENANDOAH_PHASE_DO(SHENANDOAH_PHASE_DECLARE_NAME)
 48 };
 49 
 50 #undef SHENANDOAH_PHASE_DECLARE_NAME
 51 
 52 ShenandoahPhaseTimings::ShenandoahPhaseTimings(uint max_workers) :
 53   _max_workers(max_workers) {
 54   assert(_max_workers > 0, "Must have some GC threads");
 55 
 56   // Initialize everything to sane defaults
 57   for (uint i = 0; i < _num_phases; i++) {
 58 #define SHENANDOAH_WORKER_DATA_NULL(type, title) \
 59     _worker_data[i] = nullptr;
 60     SHENANDOAH_PAR_PHASE_DO(,, SHENANDOAH_WORKER_DATA_NULL)
 61 #undef SHENANDOAH_WORKER_DATA_NULL
 62     _cycle_data[i] = uninitialized();
 63   }
 64 
 65   // Then punch in the worker-related data.
 66   // Every worker phase get a bunch of internal objects, except
 67   // the very first slot, which is "<total>" and is not populated.
 68   for (uint i = 0; i < _num_phases; i++) {
 69     if (is_worker_phase(Phase(i))) {
 70       int c = 0;
 71 #define SHENANDOAH_WORKER_DATA_INIT(type, title) \
 72       if (c++ != 0) _worker_data[i + c] = new ShenandoahWorkerData(nullptr, title, _max_workers);
 73       SHENANDOAH_PAR_PHASE_DO(,, SHENANDOAH_WORKER_DATA_INIT)
 74 #undef SHENANDOAH_WORKER_DATA_INIT
 75     }
 76   }
 77 
 78   _policy = ShenandoahHeap::heap()->shenandoah_policy();
 79   assert(_policy != nullptr, "Can not be null");
 80 }
 81 
 82 ShenandoahPhaseTimings::Phase ShenandoahPhaseTimings::worker_par_phase(Phase phase, ParPhase par_phase) {
 83   assert(is_worker_phase(phase), "Phase should accept worker phase times: %s", phase_name(phase));
 84   Phase p = Phase(phase + 1 + par_phase);
 85   assert(p >= 0 && p < _num_phases, "Out of bound for: %s", phase_name(phase));
 86   return p;
 87 }
 88 
 89 ShenandoahWorkerData* ShenandoahPhaseTimings::worker_data(Phase phase, ParPhase par_phase) {
 90   Phase p = worker_par_phase(phase, par_phase);
 91   ShenandoahWorkerData* wd = _worker_data[p];
 92   assert(wd != nullptr, "Counter initialized: %s", phase_name(p));
 93   return wd;
 94 }
 95 
 96 bool ShenandoahPhaseTimings::is_worker_phase(Phase phase) {
 97   assert(phase >= 0 && phase < _num_phases, "Out of bounds");
 98   switch (phase) {
 99     case init_evac:
100     case finish_mark:
101     case purge_weak_par:
102     case full_gc_mark:
103     case full_gc_update_roots:
104     case full_gc_adjust_roots:
105     case degen_gc_stw_mark:
106     case degen_gc_mark:
107     case degen_gc_update_roots:
108     case full_gc_weakrefs:
109     case full_gc_purge_class_unload:
110     case full_gc_purge_weak_par:
111     case degen_gc_weakrefs:
112     case degen_gc_purge_class_unload:
113     case degen_gc_purge_weak_par:
114     case heap_iteration_roots:
115     case conc_mark_roots:
116     case conc_thread_roots:
117     case conc_weak_roots_work:
118     case conc_weak_refs:
119     case conc_strong_roots:
120       return true;
121     default:
122       return false;
123   }
124 }
125 
126 bool ShenandoahPhaseTimings::is_root_work_phase(Phase phase) {
127   switch (phase) {
128     case finish_mark:
129     case init_evac:
130     case degen_gc_update_roots:
131     case full_gc_mark:
132     case full_gc_update_roots:
133     case full_gc_adjust_roots:
134       return true;
135     default:
136       return false;
137   }
138 }
139 
140 void ShenandoahPhaseTimings::set_cycle_data(Phase phase, double time, bool should_aggregate) {
141   const double cycle_data = _cycle_data[phase];
142   if (should_aggregate) {
143     _cycle_data[phase] = (cycle_data == uninitialized()) ? time :  (cycle_data + time);
144   } else {
145 #ifdef ASSERT
146     assert(cycle_data == uninitialized(), "Should not be set yet: %s, current value: %lf", phase_name(phase), cycle_data);
147 #endif
148     _cycle_data[phase] = time;
149   }
150 }
151 
152 void ShenandoahPhaseTimings::record_phase_time(Phase phase, double time, bool should_aggregate) {
153   if (!_policy->is_at_shutdown()) {
154     set_cycle_data(phase, time, should_aggregate);
155   }
156 }
157 
158 void ShenandoahPhaseTimings::record_workers_start(Phase phase) {
159   assert(is_worker_phase(phase), "Phase should accept worker phase times: %s", phase_name(phase));
160 
161   // Special case: these phases can enter multiple times, need to reset
162   // their worker data every time.
163   if (phase == heap_iteration_roots) {
164     for (uint i = 1; i < _num_par_phases; i++) {
165       worker_data(phase, ParPhase(i))->reset();
166     }
167   }
168 
169 #ifdef ASSERT
170   for (uint i = 1; i < _num_par_phases; i++) {
171     ShenandoahWorkerData* wd = worker_data(phase, ParPhase(i));
172     for (uint c = 0; c < _max_workers; c++) {
173       assert(wd->get(c) == ShenandoahWorkerData::uninitialized(),
174              "Should not be set: %s", phase_name(worker_par_phase(phase, ParPhase(i))));
175     }
176   }
177 #endif
178 }
179 
180 void ShenandoahPhaseTimings::record_workers_end(Phase phase) {
181   assert(is_worker_phase(phase), "Phase should accept worker phase times: %s", phase_name(phase));
182 }
183 
184 void ShenandoahPhaseTimings::flush_par_workers_to_cycle() {
185   for (uint pi = 0; pi < _num_phases; pi++) {
186     Phase phase = Phase(pi);
187     if (is_worker_phase(phase)) {
188       double s = uninitialized();
189       for (uint i = 1; i < _num_par_phases; i++) {
190         ShenandoahWorkerData* wd = worker_data(phase, ParPhase(i));
191         double ws = uninitialized();
192         for (uint c = 0; c < _max_workers; c++) {
193           double v = wd->get(c);
194           if (v != ShenandoahWorkerData::uninitialized()) {
195             if (ws == uninitialized()) {
196               ws = v;
197             } else {
198               ws += v;
199             }
200           }
201         }
202         if (ws != uninitialized()) {
203           // add to each line in phase
204           set_cycle_data(Phase(phase + i + 1), ws);
205           if (s == uninitialized()) {
206             s = ws;
207           } else {
208             s += ws;
209           }
210         }
211       }
212       if (s != uninitialized()) {
213         // add to total for phase
214         set_cycle_data(Phase(phase + 1), s);
215       }
216     }
217   }
218 }
219 
220 void ShenandoahPhaseTimings::flush_cycle_to_global() {
221   for (uint i = 0; i < _num_phases; i++) {
222     if (_cycle_data[i] != uninitialized()) {
223       _global_data[i].add(_cycle_data[i]);
224       _cycle_data[i] = uninitialized();
225     }
226     if (_worker_data[i] != nullptr) {
227       _worker_data[i]->reset();
228     }
229   }
230   OrderAccess::fence();
231 }
232 
233 void ShenandoahPhaseTimings::print_cycle_on(outputStream* out) const {
234   out->cr();
235   out->print_cr("All times are wall-clock times, except per-root-class counters, that are sum over");
236   out->print_cr("all workers. Dividing the <total> over the root stage time estimates parallelism.");
237   out->cr();
238   for (uint i = 0; i < _num_phases; i++) {
239     double v = _cycle_data[i] * 1000000.0;
240     if (v > 0) {
241       out->print(SHENANDOAH_PHASE_NAME_FORMAT " " SHENANDOAH_US_TIME_FORMAT " us", _phase_names[i], v);
242 
243       if (is_worker_phase(Phase(i))) {
244         double total = _cycle_data[i + 1] * 1000000.0;
245         if (total > 0) {
246           out->print(", parallelism: " SHENANDOAH_PARALLELISM_FORMAT "x", total / v);
247         }
248       }
249 
250       if (_worker_data[i] != nullptr) {
251         out->print(", workers (us): ");
252         for (uint c = 0; c < _max_workers; c++) {
253           double tv = _worker_data[i]->get(c);
254           if (tv != ShenandoahWorkerData::uninitialized()) {
255             out->print(SHENANDOAH_US_WORKER_TIME_FORMAT ", ", tv * 1000000.0);
256           } else {
257             out->print(SHENANDOAH_US_WORKER_NOTIME_FORMAT ", ", "---");
258           }
259         }
260       }
261       out->cr();
262     }
263   }
264 }
265 
266 void ShenandoahPhaseTimings::print_global_on(outputStream* out) const {
267   out->cr();
268   out->print_cr("GC STATISTICS:");
269   out->print_cr("  \"(G)\" (gross) pauses include VM time: time to notify and block threads, do the pre-");
270   out->print_cr("        and post-safepoint housekeeping. Use -Xlog:safepoint+stats to dissect.");
271   out->print_cr("  \"(N)\" (net) pauses are the times spent in the actual GC code.");
272   out->print_cr("  \"a\" is average time for each phase, look at levels to see if average makes sense.");
273   out->print_cr("  \"lvls\" are quantiles: 0%% (minimum), 25%%, 50%% (median), 75%%, 100%% (maximum).");
274   out->cr();
275   out->print_cr("  All times are wall-clock times, except per-root-class counters, that are sum over");
276   out->print_cr("  all workers. Dividing the <total> over the root stage time estimates parallelism.");
277   out->cr();
278 
279   out->print_cr("  Pacing delays are measured from entering the pacing code till exiting it. Therefore,");
280   out->print_cr("  observed pacing delays may be higher than the threshold when paced thread spent more");
281   out->print_cr("  time in the pacing code. It usually happens when thread is de-scheduled while paced,");
282   out->print_cr("  OS takes longer to unblock the thread, or JVM experiences an STW pause.");
283   out->cr();
284   out->print_cr("  Higher delay would prevent application outpacing the GC, but it will hide the GC latencies");
285   out->print_cr("  from the STW pause times. Pacing affects the individual threads, and so it would also be");
286   out->print_cr("  invisible to the usual profiling tools, but would add up to end-to-end application latency.");
287   out->print_cr("  Raise max pacing delay with care.");
288   out->cr();
289 
290   for (uint i = 0; i < _num_phases; i++) {
291     if (_global_data[i].maximum() != 0) {
292       out->print_cr(SHENANDOAH_PHASE_NAME_FORMAT " = " SHENANDOAH_S_TIME_FORMAT " s "
293                     "(a = " SHENANDOAH_US_TIME_FORMAT " us) "
294                     "(n = " INT32_FORMAT_W(5) ") (lvls, us = "
295                     SHENANDOAH_US_TIME_FORMAT ", "
296                     SHENANDOAH_US_TIME_FORMAT ", "
297                     SHENANDOAH_US_TIME_FORMAT ", "
298                     SHENANDOAH_US_TIME_FORMAT ", "
299                     SHENANDOAH_US_TIME_FORMAT ")",
300                     _phase_names[i],
301                     _global_data[i].sum(),
302                     _global_data[i].avg() * 1000000.0,
303                     _global_data[i].num(),
304                     _global_data[i].percentile(0) * 1000000.0,
305                     _global_data[i].percentile(25) * 1000000.0,
306                     _global_data[i].percentile(50) * 1000000.0,
307                     _global_data[i].percentile(75) * 1000000.0,
308                     _global_data[i].maximum() * 1000000.0
309       );
310     }
311   }
312 }
313 
314 ShenandoahWorkerTimingsTracker::ShenandoahWorkerTimingsTracker(ShenandoahPhaseTimings::Phase phase,
315         ShenandoahPhaseTimings::ParPhase par_phase, uint worker_id) :
316         _timings(ShenandoahHeap::heap()->phase_timings()),
317         _phase(phase), _par_phase(par_phase), _worker_id(worker_id) {
318 
319   assert(_timings->worker_data(_phase, _par_phase)->get(_worker_id) == ShenandoahWorkerData::uninitialized(),
320          "Should not be set yet: %s", ShenandoahPhaseTimings::phase_name(_timings->worker_par_phase(_phase, _par_phase)));
321   _start_time = os::elapsedTime();
322 }
323 
324 ShenandoahWorkerTimingsTracker::~ShenandoahWorkerTimingsTracker() {
325   _timings->worker_data(_phase, _par_phase)->set(_worker_id, os::elapsedTime() - _start_time);
326 
327   if (ShenandoahPhaseTimings::is_root_work_phase(_phase)) {
328     ShenandoahPhaseTimings::Phase root_phase = _phase;
329     ShenandoahPhaseTimings::Phase cur_phase = _timings->worker_par_phase(root_phase, _par_phase);
330     _event.commit(GCId::current(), _worker_id, ShenandoahPhaseTimings::phase_name(cur_phase));
331   }
332 }