1 /*
2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. 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 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
27 #include "gc_implementation/g1/g1GCPhaseTimes.hpp"
28 #include "gc_implementation/g1/g1Log.hpp"
29 #include "gc_implementation/g1/g1StringDedup.hpp"
30 #include "memory/allocation.hpp"
31 #include "runtime/os.hpp"
32 
33 // Helper class for avoiding interleaved logging
34 class LineBuffer: public StackObj {
35 
36 private:
37   static const int BUFFER_LEN = 1024;
38   static const int INDENT_CHARS = 3;
39   char _buffer[BUFFER_LEN];
40   int _indent_level;
41   int _cur;
42 
vappend(const char * format,va_list ap)43   void vappend(const char* format, va_list ap)  ATTRIBUTE_PRINTF(2, 0) {
44     int res = os::vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap);
45     if (res > BUFFER_LEN) {
46       DEBUG_ONLY(warning("buffer too small in LineBuffer");)
47       _buffer[BUFFER_LEN -1] = 0;
48       _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again
49     } else if (res != -1) {
50       _cur += res;
51     }
52   }
53 
54 public:
LineBuffer(int indent_level)55   explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) {
56     for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) {
57       _buffer[_cur] = ' ';
58     }
59   }
60 
61 #ifndef PRODUCT
~LineBuffer()62   ~LineBuffer() {
63     assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?");
64   }
65 #endif
66 
append(const char * format,...)67   void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3) {
68     va_list ap;
69     va_start(ap, format);
70     vappend(format, ap);
71     va_end(ap);
72   }
73 
print_cr()74   void print_cr() {
75     gclog_or_tty->print_cr("%s", _buffer);
76     _cur = _indent_level * INDENT_CHARS;
77   }
78 
append_and_print_cr(const char * format,...)79   void append_and_print_cr(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3) {
80     va_list ap;
81     va_start(ap, format);
82     vappend(format, ap);
83     va_end(ap);
84     print_cr();
85   }
86 };
87 
88 template <class T>
89 class WorkerDataArray  : public CHeapObj<mtGC> {
90   friend class G1GCParPhasePrinter;
91   T*          _data;
92   uint        _length;
93   const char* _title;
94   bool        _print_sum;
95   int         _log_level;
96   uint        _indent_level;
97   bool        _enabled;
98 
99   WorkerDataArray<size_t>* _thread_work_items;
100 
101   NOT_PRODUCT(T uninitialized();)
102 
103   // We are caching the sum and average to only have to calculate them once.
104   // This is not done in an MT-safe way. It is intended to allow single
105   // threaded code to call sum() and average() multiple times in any order
106   // without having to worry about the cost.
107   bool   _has_new_data;
108   T      _sum;
109   T      _min;
110   T      _max;
111   double _average;
112 
113  public:
WorkerDataArray(uint length,const char * title,bool print_sum,int log_level,uint indent_level)114   WorkerDataArray(uint length, const char* title, bool print_sum, int log_level, uint indent_level) :
115     _title(title), _length(0), _print_sum(print_sum), _log_level(log_level), _indent_level(indent_level),
116     _has_new_data(true), _thread_work_items(NULL), _enabled(true) {
117     assert(length > 0, "Must have some workers to store data for");
118     _length = length;
119     _data = NEW_C_HEAP_ARRAY(T, _length, mtGC);
120   }
121 
~WorkerDataArray()122   ~WorkerDataArray() {
123     FREE_C_HEAP_ARRAY(T, _data, mtGC);
124   }
125 
link_thread_work_items(WorkerDataArray<size_t> * thread_work_items)126   void link_thread_work_items(WorkerDataArray<size_t>* thread_work_items) {
127     _thread_work_items = thread_work_items;
128   }
129 
thread_work_items()130   WorkerDataArray<size_t>* thread_work_items() { return _thread_work_items; }
131 
set(uint worker_i,T value)132   void set(uint worker_i, T value) {
133     assert(worker_i < _length, err_msg("Worker %d is greater than max: %d", worker_i, _length));
134     assert(_data[worker_i] == WorkerDataArray<T>::uninitialized(), err_msg("Overwriting data for worker %d in %s", worker_i, _title));
135     _data[worker_i] = value;
136     _has_new_data = true;
137   }
138 
set_thread_work_item(uint worker_i,size_t value)139   void set_thread_work_item(uint worker_i, size_t value) {
140     assert(_thread_work_items != NULL, "No sub count");
141     _thread_work_items->set(worker_i, value);
142   }
143 
get(uint worker_i)144   T get(uint worker_i) {
145     assert(worker_i < _length, err_msg("Worker %d is greater than max: %d", worker_i, _length));
146     assert(_data[worker_i] != WorkerDataArray<T>::uninitialized(), err_msg("No data added for worker %d", worker_i));
147     return _data[worker_i];
148   }
149 
add(uint worker_i,T value)150   void add(uint worker_i, T value) {
151     assert(worker_i < _length, err_msg("Worker %d is greater than max: %d", worker_i, _length));
152     assert(_data[worker_i] != WorkerDataArray<T>::uninitialized(), err_msg("No data to add to for worker %d", worker_i));
153     _data[worker_i] += value;
154     _has_new_data = true;
155   }
156 
average(uint active_threads)157   double average(uint active_threads){
158     calculate_totals(active_threads);
159     return _average;
160   }
161 
sum(uint active_threads)162   T sum(uint active_threads) {
163     calculate_totals(active_threads);
164     return _sum;
165   }
166 
minimum(uint active_threads)167   T minimum(uint active_threads) {
168     calculate_totals(active_threads);
169     return _min;
170   }
171 
maximum(uint active_threads)172   T maximum(uint active_threads) {
173     calculate_totals(active_threads);
174     return _max;
175   }
176 
177   void reset() PRODUCT_RETURN;
178   void verify(uint active_threads) PRODUCT_RETURN;
179 
set_enabled(bool enabled)180   void set_enabled(bool enabled) { _enabled = enabled; }
181 
log_level()182   int log_level() { return _log_level;  }
183 
184  private:
185 
calculate_totals(uint active_threads)186   void calculate_totals(uint active_threads){
187     if (!_has_new_data) {
188       return;
189     }
190 
191     _sum = (T)0;
192     _min = _data[0];
193     _max = _min;
194     assert(active_threads <= _length, "Wrong number of active threads");
195     for (uint i = 0; i < active_threads; ++i) {
196       T val = _data[i];
197       _sum += val;
198       _min = MIN2(_min, val);
199       _max = MAX2(_max, val);
200     }
201     _average = (double)_sum / (double)active_threads;
202     _has_new_data = false;
203   }
204 };
205 
206 
207 #ifndef PRODUCT
208 
209 template <>
uninitialized()210 size_t WorkerDataArray<size_t>::uninitialized() {
211   return (size_t)-1;
212 }
213 
214 template <>
uninitialized()215 double WorkerDataArray<double>::uninitialized() {
216   return -1.0;
217 }
218 
219 template <class T>
reset()220 void WorkerDataArray<T>::reset() {
221   for (uint i = 0; i < _length; i++) {
222     _data[i] = WorkerDataArray<T>::uninitialized();
223   }
224   if (_thread_work_items != NULL) {
225     _thread_work_items->reset();
226   }
227 }
228 
229 template <class T>
verify(uint active_threads)230 void WorkerDataArray<T>::verify(uint active_threads) {
231   if (!_enabled) {
232     return;
233   }
234 
235   assert(active_threads <= _length, "Wrong number of active threads");
236   for (uint i = 0; i < active_threads; i++) {
237     assert(_data[i] != WorkerDataArray<T>::uninitialized(),
238         err_msg("Invalid data for worker %u in '%s'", i, _title));
239   }
240   if (_thread_work_items != NULL) {
241     _thread_work_items->verify(active_threads);
242   }
243 }
244 
245 #endif
246 
G1GCPhaseTimes(uint max_gc_threads)247 G1GCPhaseTimes::G1GCPhaseTimes(uint max_gc_threads) :
248   _max_gc_threads(max_gc_threads)
249 {
250   assert(max_gc_threads > 0, "Must have some GC threads");
251 
252   _gc_par_phases[GCWorkerStart] = new WorkerDataArray<double>(max_gc_threads, "GC Worker Start (ms)", false, G1Log::LevelFiner, 2);
253   _gc_par_phases[ExtRootScan] = new WorkerDataArray<double>(max_gc_threads, "Ext Root Scanning (ms)", true, G1Log::LevelFiner, 2);
254 
255   // Root scanning phases
256   _gc_par_phases[ThreadRoots] = new WorkerDataArray<double>(max_gc_threads, "Thread Roots (ms)", true, G1Log::LevelFinest, 3);
257   _gc_par_phases[StringTableRoots] = new WorkerDataArray<double>(max_gc_threads, "StringTable Roots (ms)", true, G1Log::LevelFinest, 3);
258   _gc_par_phases[UniverseRoots] = new WorkerDataArray<double>(max_gc_threads, "Universe Roots (ms)", true, G1Log::LevelFinest, 3);
259   _gc_par_phases[JNIRoots] = new WorkerDataArray<double>(max_gc_threads, "JNI Handles Roots (ms)", true, G1Log::LevelFinest, 3);
260   _gc_par_phases[ObjectSynchronizerRoots] = new WorkerDataArray<double>(max_gc_threads, "ObjectSynchronizer Roots (ms)", true, G1Log::LevelFinest, 3);
261   _gc_par_phases[FlatProfilerRoots] = new WorkerDataArray<double>(max_gc_threads, "FlatProfiler Roots (ms)", true, G1Log::LevelFinest, 3);
262   _gc_par_phases[ManagementRoots] = new WorkerDataArray<double>(max_gc_threads, "Management Roots (ms)", true, G1Log::LevelFinest, 3);
263   _gc_par_phases[SystemDictionaryRoots] = new WorkerDataArray<double>(max_gc_threads, "SystemDictionary Roots (ms)", true, G1Log::LevelFinest, 3);
264   _gc_par_phases[CLDGRoots] = new WorkerDataArray<double>(max_gc_threads, "CLDG Roots (ms)", true, G1Log::LevelFinest, 3);
265   _gc_par_phases[JVMTIRoots] = new WorkerDataArray<double>(max_gc_threads, "JVMTI Roots (ms)", true, G1Log::LevelFinest, 3);
266   _gc_par_phases[CodeCacheRoots] = new WorkerDataArray<double>(max_gc_threads, "CodeCache Roots (ms)", true, G1Log::LevelFinest, 3);
267   _gc_par_phases[CMRefRoots] = new WorkerDataArray<double>(max_gc_threads, "CM RefProcessor Roots (ms)", true, G1Log::LevelFinest, 3);
268   _gc_par_phases[WaitForStrongCLD] = new WorkerDataArray<double>(max_gc_threads, "Wait For Strong CLD (ms)", true, G1Log::LevelFinest, 3);
269   _gc_par_phases[WeakCLDRoots] = new WorkerDataArray<double>(max_gc_threads, "Weak CLD Roots (ms)", true, G1Log::LevelFinest, 3);
270   _gc_par_phases[SATBFiltering] = new WorkerDataArray<double>(max_gc_threads, "SATB Filtering (ms)", true, G1Log::LevelFinest, 3);
271 
272   _gc_par_phases[UpdateRS] = new WorkerDataArray<double>(max_gc_threads, "Update RS (ms)", true, G1Log::LevelFiner, 2);
273   _gc_par_phases[ScanRS] = new WorkerDataArray<double>(max_gc_threads, "Scan RS (ms)", true, G1Log::LevelFiner, 2);
274   _gc_par_phases[CodeRoots] = new WorkerDataArray<double>(max_gc_threads, "Code Root Scanning (ms)", true, G1Log::LevelFiner, 2);
275   _gc_par_phases[ObjCopy] = new WorkerDataArray<double>(max_gc_threads, "Object Copy (ms)", true, G1Log::LevelFiner, 2);
276   _gc_par_phases[Termination] = new WorkerDataArray<double>(max_gc_threads, "Termination (ms)", true, G1Log::LevelFiner, 2);
277   _gc_par_phases[GCWorkerTotal] = new WorkerDataArray<double>(max_gc_threads, "GC Worker Total (ms)", true, G1Log::LevelFiner, 2);
278   _gc_par_phases[GCWorkerEnd] = new WorkerDataArray<double>(max_gc_threads, "GC Worker End (ms)", false, G1Log::LevelFiner, 2);
279   _gc_par_phases[Other] = new WorkerDataArray<double>(max_gc_threads, "GC Worker Other (ms)", true, G1Log::LevelFiner, 2);
280 
281   _update_rs_processed_buffers = new WorkerDataArray<size_t>(max_gc_threads, "Processed Buffers", true, G1Log::LevelFiner, 3);
282   _gc_par_phases[UpdateRS]->link_thread_work_items(_update_rs_processed_buffers);
283 
284   _termination_attempts = new WorkerDataArray<size_t>(max_gc_threads, "Termination Attempts", true, G1Log::LevelFinest, 3);
285   _gc_par_phases[Termination]->link_thread_work_items(_termination_attempts);
286 
287   _gc_par_phases[StringDedupQueueFixup] = new WorkerDataArray<double>(max_gc_threads, "Queue Fixup (ms)", true, G1Log::LevelFiner, 2);
288   _gc_par_phases[StringDedupTableFixup] = new WorkerDataArray<double>(max_gc_threads, "Table Fixup (ms)", true, G1Log::LevelFiner, 2);
289 
290   _gc_par_phases[RedirtyCards] = new WorkerDataArray<double>(max_gc_threads, "Parallel Redirty", true, G1Log::LevelFinest, 3);
291   _redirtied_cards = new WorkerDataArray<size_t>(max_gc_threads, "Redirtied Cards", true, G1Log::LevelFinest, 3);
292   _gc_par_phases[RedirtyCards]->link_thread_work_items(_redirtied_cards);
293 }
294 
note_gc_start(uint active_gc_threads,bool mark_in_progress)295 void G1GCPhaseTimes::note_gc_start(uint active_gc_threads, bool mark_in_progress) {
296   assert(active_gc_threads > 0, "The number of threads must be > 0");
297   assert(active_gc_threads <= _max_gc_threads, "The number of active threads must be <= the max number of threads");
298   _active_gc_threads = active_gc_threads;
299 
300   for (int i = 0; i < GCParPhasesSentinel; i++) {
301     _gc_par_phases[i]->reset();
302   }
303 
304   _gc_par_phases[StringDedupQueueFixup]->set_enabled(G1StringDedup::is_enabled());
305   _gc_par_phases[StringDedupTableFixup]->set_enabled(G1StringDedup::is_enabled());
306 }
307 
note_gc_end()308 void G1GCPhaseTimes::note_gc_end() {
309   for (uint i = 0; i < _active_gc_threads; i++) {
310     double worker_time = _gc_par_phases[GCWorkerEnd]->get(i) - _gc_par_phases[GCWorkerStart]->get(i);
311     record_time_secs(GCWorkerTotal, i , worker_time);
312 
313     double worker_known_time =
314         _gc_par_phases[ExtRootScan]->get(i) +
315         _gc_par_phases[SATBFiltering]->get(i) +
316         _gc_par_phases[UpdateRS]->get(i) +
317         _gc_par_phases[ScanRS]->get(i) +
318         _gc_par_phases[CodeRoots]->get(i) +
319         _gc_par_phases[ObjCopy]->get(i) +
320         _gc_par_phases[Termination]->get(i);
321 
322     record_time_secs(Other, i, worker_time - worker_known_time);
323   }
324 
325   for (int i = 0; i < GCParPhasesSentinel; i++) {
326     _gc_par_phases[i]->verify(_active_gc_threads);
327   }
328 }
329 
print_stats(int level,const char * str,double value)330 void G1GCPhaseTimes::print_stats(int level, const char* str, double value) {
331   LineBuffer(level).append_and_print_cr("[%s: %.1lf ms]", str, value);
332 }
333 
print_stats(int level,const char * str,size_t value)334 void G1GCPhaseTimes::print_stats(int level, const char* str, size_t value) {
335   LineBuffer(level).append_and_print_cr("[%s: " SIZE_FORMAT "]", str, value);
336 }
337 
print_stats(int level,const char * str,double value,uint workers)338 void G1GCPhaseTimes::print_stats(int level, const char* str, double value, uint workers) {
339   LineBuffer(level).append_and_print_cr("[%s: %.1lf ms, GC Workers: %u]", str, value, workers);
340 }
341 
accounted_time_ms()342 double G1GCPhaseTimes::accounted_time_ms() {
343     // Subtract the root region scanning wait time. It's initialized to
344     // zero at the start of the pause.
345     double misc_time_ms = _root_region_scan_wait_time_ms;
346 
347     misc_time_ms += _cur_collection_par_time_ms;
348 
349     // Now subtract the time taken to fix up roots in generated code
350     misc_time_ms += _cur_collection_code_root_fixup_time_ms;
351 
352     // Strong code root purge time
353     misc_time_ms += _cur_strong_code_root_purge_time_ms;
354 
355     if (G1StringDedup::is_enabled()) {
356       // String dedup fixup time
357       misc_time_ms += _cur_string_dedup_fixup_time_ms;
358     }
359 
360     // Subtract the time taken to clean the card table from the
361     // current value of "other time"
362     misc_time_ms += _cur_clear_ct_time_ms;
363 
364     return misc_time_ms;
365 }
366 
367 // record the time a phase took in seconds
record_time_secs(GCParPhases phase,uint worker_i,double secs)368 void G1GCPhaseTimes::record_time_secs(GCParPhases phase, uint worker_i, double secs) {
369   _gc_par_phases[phase]->set(worker_i, secs);
370 }
371 
372 // add a number of seconds to a phase
add_time_secs(GCParPhases phase,uint worker_i,double secs)373 void G1GCPhaseTimes::add_time_secs(GCParPhases phase, uint worker_i, double secs) {
374   _gc_par_phases[phase]->add(worker_i, secs);
375 }
376 
record_thread_work_item(GCParPhases phase,uint worker_i,size_t count)377 void G1GCPhaseTimes::record_thread_work_item(GCParPhases phase, uint worker_i, size_t count) {
378   _gc_par_phases[phase]->set_thread_work_item(worker_i, count);
379 }
380 
381 // return the average time for a phase in milliseconds
average_time_ms(GCParPhases phase)382 double G1GCPhaseTimes::average_time_ms(GCParPhases phase) {
383   return _gc_par_phases[phase]->average(_active_gc_threads) * 1000.0;
384 }
385 
get_time_ms(GCParPhases phase,uint worker_i)386 double G1GCPhaseTimes::get_time_ms(GCParPhases phase, uint worker_i) {
387   return _gc_par_phases[phase]->get(worker_i) * 1000.0;
388 }
389 
sum_time_ms(GCParPhases phase)390 double G1GCPhaseTimes::sum_time_ms(GCParPhases phase) {
391   return _gc_par_phases[phase]->sum(_active_gc_threads) * 1000.0;
392 }
393 
min_time_ms(GCParPhases phase)394 double G1GCPhaseTimes::min_time_ms(GCParPhases phase) {
395   return _gc_par_phases[phase]->minimum(_active_gc_threads) * 1000.0;
396 }
397 
max_time_ms(GCParPhases phase)398 double G1GCPhaseTimes::max_time_ms(GCParPhases phase) {
399   return _gc_par_phases[phase]->maximum(_active_gc_threads) * 1000.0;
400 }
401 
get_thread_work_item(GCParPhases phase,uint worker_i)402 size_t G1GCPhaseTimes::get_thread_work_item(GCParPhases phase, uint worker_i) {
403   assert(_gc_par_phases[phase]->thread_work_items() != NULL, "No sub count");
404   return _gc_par_phases[phase]->thread_work_items()->get(worker_i);
405 }
406 
sum_thread_work_items(GCParPhases phase)407 size_t G1GCPhaseTimes::sum_thread_work_items(GCParPhases phase) {
408   assert(_gc_par_phases[phase]->thread_work_items() != NULL, "No sub count");
409   return _gc_par_phases[phase]->thread_work_items()->sum(_active_gc_threads);
410 }
411 
average_thread_work_items(GCParPhases phase)412 double G1GCPhaseTimes::average_thread_work_items(GCParPhases phase) {
413   assert(_gc_par_phases[phase]->thread_work_items() != NULL, "No sub count");
414   return _gc_par_phases[phase]->thread_work_items()->average(_active_gc_threads);
415 }
416 
min_thread_work_items(GCParPhases phase)417 size_t G1GCPhaseTimes::min_thread_work_items(GCParPhases phase) {
418   assert(_gc_par_phases[phase]->thread_work_items() != NULL, "No sub count");
419   return _gc_par_phases[phase]->thread_work_items()->minimum(_active_gc_threads);
420 }
421 
max_thread_work_items(GCParPhases phase)422 size_t G1GCPhaseTimes::max_thread_work_items(GCParPhases phase) {
423   assert(_gc_par_phases[phase]->thread_work_items() != NULL, "No sub count");
424   return _gc_par_phases[phase]->thread_work_items()->maximum(_active_gc_threads);
425 }
426 
427 class G1GCParPhasePrinter : public StackObj {
428   G1GCPhaseTimes* _phase_times;
429  public:
G1GCParPhasePrinter(G1GCPhaseTimes * phase_times)430   G1GCParPhasePrinter(G1GCPhaseTimes* phase_times) : _phase_times(phase_times) {}
431 
print(G1GCPhaseTimes::GCParPhases phase_id)432   void print(G1GCPhaseTimes::GCParPhases phase_id) {
433     WorkerDataArray<double>* phase = _phase_times->_gc_par_phases[phase_id];
434 
435     if (phase->_log_level > G1Log::level() || !phase->_enabled) {
436       return;
437     }
438 
439     if (phase->_length == 1) {
440       print_single_length(phase_id, phase);
441     } else {
442       print_multi_length(phase_id, phase);
443     }
444   }
445 
446  private:
447 
print_single_length(G1GCPhaseTimes::GCParPhases phase_id,WorkerDataArray<double> * phase)448   void print_single_length(G1GCPhaseTimes::GCParPhases phase_id, WorkerDataArray<double>* phase) {
449     // No need for min, max, average and sum for only one worker
450     LineBuffer buf(phase->_indent_level);
451     buf.append_and_print_cr("[%s:  %.1lf]", phase->_title, _phase_times->get_time_ms(phase_id, 0));
452 
453     if (phase->_thread_work_items != NULL) {
454       LineBuffer buf2(phase->_thread_work_items->_indent_level);
455       buf2.append_and_print_cr("[%s:  " SIZE_FORMAT "]", phase->_thread_work_items->_title, _phase_times->sum_thread_work_items(phase_id));
456     }
457   }
458 
print_time_values(LineBuffer & buf,G1GCPhaseTimes::GCParPhases phase_id,WorkerDataArray<double> * phase)459   void print_time_values(LineBuffer& buf, G1GCPhaseTimes::GCParPhases phase_id, WorkerDataArray<double>* phase) {
460     uint active_length = _phase_times->_active_gc_threads;
461     for (uint i = 0; i < active_length; ++i) {
462       buf.append("  %.1lf", _phase_times->get_time_ms(phase_id, i));
463     }
464     buf.print_cr();
465   }
466 
print_count_values(LineBuffer & buf,G1GCPhaseTimes::GCParPhases phase_id,WorkerDataArray<size_t> * thread_work_items)467   void print_count_values(LineBuffer& buf, G1GCPhaseTimes::GCParPhases phase_id, WorkerDataArray<size_t>* thread_work_items) {
468     uint active_length = _phase_times->_active_gc_threads;
469     for (uint i = 0; i < active_length; ++i) {
470       buf.append("  " SIZE_FORMAT, _phase_times->get_thread_work_item(phase_id, i));
471     }
472     buf.print_cr();
473   }
474 
print_thread_work_items(G1GCPhaseTimes::GCParPhases phase_id,WorkerDataArray<size_t> * thread_work_items)475   void print_thread_work_items(G1GCPhaseTimes::GCParPhases phase_id, WorkerDataArray<size_t>* thread_work_items) {
476     LineBuffer buf(thread_work_items->_indent_level);
477     buf.append("[%s:", thread_work_items->_title);
478 
479     if (G1Log::finest()) {
480       print_count_values(buf, phase_id, thread_work_items);
481     }
482 
483     assert(thread_work_items->_print_sum, err_msg("%s does not have print sum true even though it is a count", thread_work_items->_title));
484 
485     buf.append_and_print_cr(" Min: " SIZE_FORMAT ", Avg: %.1lf, Max: " SIZE_FORMAT ", Diff: " SIZE_FORMAT ", Sum: " SIZE_FORMAT "]",
486         _phase_times->min_thread_work_items(phase_id), _phase_times->average_thread_work_items(phase_id), _phase_times->max_thread_work_items(phase_id),
487         _phase_times->max_thread_work_items(phase_id) - _phase_times->min_thread_work_items(phase_id), _phase_times->sum_thread_work_items(phase_id));
488   }
489 
print_multi_length(G1GCPhaseTimes::GCParPhases phase_id,WorkerDataArray<double> * phase)490   void print_multi_length(G1GCPhaseTimes::GCParPhases phase_id, WorkerDataArray<double>* phase) {
491     LineBuffer buf(phase->_indent_level);
492     buf.append("[%s:", phase->_title);
493 
494     if (G1Log::finest()) {
495       print_time_values(buf, phase_id, phase);
496     }
497 
498     buf.append(" Min: %.1lf, Avg: %.1lf, Max: %.1lf, Diff: %.1lf",
499         _phase_times->min_time_ms(phase_id), _phase_times->average_time_ms(phase_id), _phase_times->max_time_ms(phase_id),
500         _phase_times->max_time_ms(phase_id) - _phase_times->min_time_ms(phase_id));
501 
502     if (phase->_print_sum) {
503       // for things like the start and end times the sum is not
504       // that relevant
505       buf.append(", Sum: %.1lf", _phase_times->sum_time_ms(phase_id));
506     }
507 
508     buf.append_and_print_cr("]");
509 
510     if (phase->_thread_work_items != NULL) {
511       print_thread_work_items(phase_id, phase->_thread_work_items);
512     }
513   }
514 };
515 
print(double pause_time_sec)516 void G1GCPhaseTimes::print(double pause_time_sec) {
517   G1GCParPhasePrinter par_phase_printer(this);
518 
519   if (_root_region_scan_wait_time_ms > 0.0) {
520     print_stats(1, "Root Region Scan Waiting", _root_region_scan_wait_time_ms);
521   }
522 
523   print_stats(1, "Parallel Time", _cur_collection_par_time_ms, _active_gc_threads);
524   for (int i = 0; i <= GCMainParPhasesLast; i++) {
525     par_phase_printer.print((GCParPhases) i);
526   }
527 
528   print_stats(1, "Code Root Fixup", _cur_collection_code_root_fixup_time_ms);
529   print_stats(1, "Code Root Purge", _cur_strong_code_root_purge_time_ms);
530   if (G1StringDedup::is_enabled()) {
531     print_stats(1, "String Dedup Fixup", _cur_string_dedup_fixup_time_ms, _active_gc_threads);
532     for (int i = StringDedupPhasesFirst; i <= StringDedupPhasesLast; i++) {
533       par_phase_printer.print((GCParPhases) i);
534     }
535   }
536   print_stats(1, "Clear CT", _cur_clear_ct_time_ms);
537   double misc_time_ms = pause_time_sec * MILLIUNITS - accounted_time_ms();
538   print_stats(1, "Other", misc_time_ms);
539   if (_cur_verify_before_time_ms > 0.0) {
540     print_stats(2, "Verify Before", _cur_verify_before_time_ms);
541   }
542   if (G1CollectedHeap::heap()->evacuation_failed()) {
543     double evac_fail_handling = _cur_evac_fail_recalc_used + _cur_evac_fail_remove_self_forwards +
544       _cur_evac_fail_restore_remsets;
545     print_stats(2, "Evacuation Failure", evac_fail_handling);
546     if (G1Log::finest()) {
547       print_stats(3, "Recalculate Used", _cur_evac_fail_recalc_used);
548       print_stats(3, "Remove Self Forwards", _cur_evac_fail_remove_self_forwards);
549       print_stats(3, "Restore RemSet", _cur_evac_fail_restore_remsets);
550     }
551   }
552   print_stats(2, "Choose CSet",
553     (_recorded_young_cset_choice_time_ms +
554     _recorded_non_young_cset_choice_time_ms));
555   print_stats(2, "Ref Proc", _cur_ref_proc_time_ms);
556   print_stats(2, "Ref Enq", _cur_ref_enq_time_ms);
557   print_stats(2, "Redirty Cards", _recorded_redirty_logged_cards_time_ms);
558   par_phase_printer.print(RedirtyCards);
559 
560   if (G1EagerReclaimHumongousObjects) {
561     print_stats(2, "Humongous Register", _cur_fast_reclaim_humongous_register_time_ms);
562     if (G1Log::finest()) {
563       print_stats(3, "Humongous Total", _cur_fast_reclaim_humongous_total);
564       print_stats(3, "Humongous Candidate", _cur_fast_reclaim_humongous_candidates);
565     }
566     print_stats(2, "Humongous Reclaim", _cur_fast_reclaim_humongous_time_ms);
567     if (G1Log::finest()) {
568       print_stats(3, "Humongous Reclaimed", _cur_fast_reclaim_humongous_reclaimed);
569     }
570   }
571   print_stats(2, "Free CSet",
572     (_recorded_young_free_cset_time_ms +
573     _recorded_non_young_free_cset_time_ms));
574   if (G1Log::finest()) {
575     print_stats(3, "Young Free CSet", _recorded_young_free_cset_time_ms);
576     print_stats(3, "Non-Young Free CSet", _recorded_non_young_free_cset_time_ms);
577   }
578   if (_cur_verify_after_time_ms > 0.0) {
579     print_stats(2, "Verify After", _cur_verify_after_time_ms);
580   }
581 }
582 
G1GCParPhaseTimesTracker(G1GCPhaseTimes * phase_times,G1GCPhaseTimes::GCParPhases phase,uint worker_id)583 G1GCParPhaseTimesTracker::G1GCParPhaseTimesTracker(G1GCPhaseTimes* phase_times, G1GCPhaseTimes::GCParPhases phase, uint worker_id) :
584     _phase_times(phase_times), _phase(phase), _worker_id(worker_id) {
585   if (_phase_times != NULL) {
586     _start_time = Ticks::now();
587   }
588 }
589 
~G1GCParPhaseTimesTracker()590 G1GCParPhaseTimesTracker::~G1GCParPhaseTimesTracker() {
591   if (_phase_times != NULL) {
592     _phase_times->record_time_secs(_phase, _worker_id, (Ticks::now() - _start_time).seconds());
593   }
594 }
595