1 /*
2  * Copyright (c) 2013, 2020, 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/g1/g1CollectedHeap.inline.hpp"
27 #include "gc/g1/g1GCParPhaseTimesTracker.hpp"
28 #include "gc/g1/g1GCPhaseTimes.hpp"
29 #include "gc/g1/g1HotCardCache.hpp"
30 #include "gc/g1/g1ParScanThreadState.inline.hpp"
31 #include "gc/g1/g1StringDedup.hpp"
32 #include "gc/shared/gcTimer.hpp"
33 #include "gc/shared/oopStorage.hpp"
34 #include "gc/shared/workerDataArray.inline.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "logging/log.hpp"
37 #include "logging/logStream.hpp"
38 #include "runtime/timer.hpp"
39 #include "runtime/os.hpp"
40 #include "utilities/macros.hpp"
41 
indent(uint level)42 static const char* indent(uint level) {
43   static const char* Indents[] = {"", "  ", "    ", "      ", "        ", "          "};
44   assert(level < ARRAY_SIZE(Indents), "Too high indent level %u", level);
45   return Indents[level];
46 }
47 
G1GCPhaseTimes(STWGCTimer * gc_timer,uint max_gc_threads)48 G1GCPhaseTimes::G1GCPhaseTimes(STWGCTimer* gc_timer, uint max_gc_threads) :
49   _max_gc_threads(max_gc_threads),
50   _gc_start_counter(0),
51   _gc_pause_time_ms(0.0),
52   _ref_phase_times(gc_timer, max_gc_threads),
53   _weak_phase_times(max_gc_threads)
54 {
55   assert(max_gc_threads > 0, "Must have some GC threads");
56 
57   _gc_par_phases[GCWorkerStart] = new WorkerDataArray<double>("GCWorkerStart", "GC Worker Start (ms):", max_gc_threads);
58   _gc_par_phases[ExtRootScan] = new WorkerDataArray<double>("ExtRootScan", "Ext Root Scanning (ms):", max_gc_threads);
59 
60   // Root scanning phases
61   _gc_par_phases[ThreadRoots] = new WorkerDataArray<double>("ThreadRoots", "Thread Roots (ms):", max_gc_threads);
62   _gc_par_phases[CLDGRoots] = new WorkerDataArray<double>("CLDGRoots", "CLDG Roots (ms):", max_gc_threads);
63   AOT_ONLY(_gc_par_phases[AOTCodeRoots] = new WorkerDataArray<double>("AOTCodeRoots", "AOT Root Scan (ms):", max_gc_threads);)
64   _gc_par_phases[CMRefRoots] = new WorkerDataArray<double>("CMRefRoots", "CM RefProcessor Roots (ms):", max_gc_threads);
65 
66   int index = G1GCPhaseTimes::StrongOopStorageSetRoots;
67   for (OopStorageSet::Iterator it = OopStorageSet::strong_iterator(); !it.is_end(); ++it, ++index) {
68     const char* phase_name_postfix = " Roots (ms):";
69     char* oop_storage_phase_name = NEW_C_HEAP_ARRAY(char, strlen(phase_name_postfix) + strlen(it->name()) + 1, mtGC);
70     strcpy(oop_storage_phase_name, it->name());
71     strcat(oop_storage_phase_name, phase_name_postfix);
72     _gc_par_phases[index] = new WorkerDataArray<double>(it->name(), oop_storage_phase_name, max_gc_threads);
73   }
74 
75   _gc_par_phases[MergeER] = new WorkerDataArray<double>("MergeER", "Eager Reclaim (ms):", max_gc_threads);
76 
77   _gc_par_phases[MergeRS] = new WorkerDataArray<double>("MergeRS", "Remembered Sets (ms):", max_gc_threads);
78   _gc_par_phases[MergeRS]->create_thread_work_items("Merged Sparse:", MergeRSMergedSparse);
79   _gc_par_phases[MergeRS]->create_thread_work_items("Merged Fine:", MergeRSMergedFine);
80   _gc_par_phases[MergeRS]->create_thread_work_items("Merged Coarse:", MergeRSMergedCoarse);
81   _gc_par_phases[MergeRS]->create_thread_work_items("Dirty Cards:", MergeRSDirtyCards);
82 
83   _gc_par_phases[OptMergeRS] = new WorkerDataArray<double>("OptMergeRS", "Optional Remembered Sets (ms):", max_gc_threads);
84   _gc_par_phases[OptMergeRS]->create_thread_work_items("Merged Sparse:", MergeRSMergedSparse);
85   _gc_par_phases[OptMergeRS]->create_thread_work_items("Merged Fine:", MergeRSMergedFine);
86   _gc_par_phases[OptMergeRS]->create_thread_work_items("Merged Coarse:", MergeRSMergedCoarse);
87   _gc_par_phases[OptMergeRS]->create_thread_work_items("Dirty Cards:", MergeRSDirtyCards);
88 
89   _gc_par_phases[MergeLB] = new WorkerDataArray<double>("MergeLB", "Log Buffers (ms):", max_gc_threads);
90   if (G1HotCardCache::default_use_cache()) {
91     _gc_par_phases[MergeHCC] = new WorkerDataArray<double>("MergeHCC", "Hot Card Cache (ms):", max_gc_threads);
92     _gc_par_phases[MergeHCC]->create_thread_work_items("Dirty Cards:", MergeHCCDirtyCards);
93     _gc_par_phases[MergeHCC]->create_thread_work_items("Skipped Cards:", MergeHCCSkippedCards);
94   } else {
95     _gc_par_phases[MergeHCC] = NULL;
96   }
97   _gc_par_phases[ScanHR] = new WorkerDataArray<double>("ScanHR", "Scan Heap Roots (ms):", max_gc_threads);
98   _gc_par_phases[OptScanHR] = new WorkerDataArray<double>("OptScanHR", "Optional Scan Heap Roots (ms):", max_gc_threads);
99   _gc_par_phases[CodeRoots] = new WorkerDataArray<double>("CodeRoots", "Code Root Scan (ms):", max_gc_threads);
100   _gc_par_phases[OptCodeRoots] = new WorkerDataArray<double>("OptCodeRoots", "Optional Code Root Scan (ms):", max_gc_threads);
101   _gc_par_phases[ObjCopy] = new WorkerDataArray<double>("ObjCopy", "Object Copy (ms):", max_gc_threads);
102   _gc_par_phases[OptObjCopy] = new WorkerDataArray<double>("OptObjCopy", "Optional Object Copy (ms):", max_gc_threads);
103   _gc_par_phases[Termination] = new WorkerDataArray<double>("Termination", "Termination (ms):", max_gc_threads);
104   _gc_par_phases[OptTermination] = new WorkerDataArray<double>("OptTermination", "Optional Termination (ms):", max_gc_threads);
105   _gc_par_phases[GCWorkerTotal] = new WorkerDataArray<double>("GCWorkerTotal", "GC Worker Total (ms):", max_gc_threads);
106   _gc_par_phases[GCWorkerEnd] = new WorkerDataArray<double>("GCWorkerEnd", "GC Worker End (ms):", max_gc_threads);
107   _gc_par_phases[Other] = new WorkerDataArray<double>("Other", "GC Worker Other (ms):", max_gc_threads);
108 
109   _gc_par_phases[ScanHR]->create_thread_work_items("Scanned Cards:", ScanHRScannedCards);
110   _gc_par_phases[ScanHR]->create_thread_work_items("Scanned Blocks:", ScanHRScannedBlocks);
111   _gc_par_phases[ScanHR]->create_thread_work_items("Claimed Chunks:", ScanHRClaimedChunks);
112 
113   _gc_par_phases[OptScanHR]->create_thread_work_items("Scanned Cards:", ScanHRScannedCards);
114   _gc_par_phases[OptScanHR]->create_thread_work_items("Scanned Blocks:", ScanHRScannedBlocks);
115   _gc_par_phases[OptScanHR]->create_thread_work_items("Claimed Chunks:", ScanHRClaimedChunks);
116   _gc_par_phases[OptScanHR]->create_thread_work_items("Scanned Refs:", ScanHRScannedOptRefs);
117   _gc_par_phases[OptScanHR]->create_thread_work_items("Used Memory:", ScanHRUsedMemory);
118 
119   _gc_par_phases[MergeLB]->create_thread_work_items("Dirty Cards:", MergeLBDirtyCards);
120   _gc_par_phases[MergeLB]->create_thread_work_items("Skipped Cards:", MergeLBSkippedCards);
121 
122   _gc_par_phases[MergePSS] = new WorkerDataArray<double>("MergePSS", "Merge Per-Thread State", 1 /* length */, true /* is_serial */);
123 
124   _gc_par_phases[MergePSS]->create_thread_work_items("Copied Bytes", MergePSSCopiedBytes, max_gc_threads);
125   _gc_par_phases[MergePSS]->create_thread_work_items("LAB Waste", MergePSSLABWasteBytes, max_gc_threads);
126   _gc_par_phases[MergePSS]->create_thread_work_items("LAB Undo Waste", MergePSSLABUndoWasteBytes, max_gc_threads);
127 
128   _gc_par_phases[Termination]->create_thread_work_items("Termination Attempts:");
129 
130   _gc_par_phases[OptTermination]->create_thread_work_items("Optional Termination Attempts:");
131 
132   if (UseStringDeduplication) {
133     _gc_par_phases[StringDedupQueueFixup] = new WorkerDataArray<double>("StringDedupQueueFixup", "Queue Fixup (ms):", max_gc_threads);
134     _gc_par_phases[StringDedupTableFixup] = new WorkerDataArray<double>("StringDedupTableFixup", "Table Fixup (ms):", max_gc_threads);
135   } else {
136     _gc_par_phases[StringDedupQueueFixup] = NULL;
137     _gc_par_phases[StringDedupTableFixup] = NULL;
138   }
139 
140   _gc_par_phases[RedirtyCards] = new WorkerDataArray<double>("RedirtyCards", "Parallel Redirty (ms):", max_gc_threads);
141   _gc_par_phases[RedirtyCards]->create_thread_work_items("Redirtied Cards:");
142 
143   _gc_par_phases[ParFreeCSet] = new WorkerDataArray<double>("ParFreeCSet", "Parallel Free Collection Set (ms):", max_gc_threads);
144   _gc_par_phases[YoungFreeCSet] = new WorkerDataArray<double>("YoungFreeCSet", "Young Free Collection Set (ms):", max_gc_threads);
145   _gc_par_phases[NonYoungFreeCSet] = new WorkerDataArray<double>("NonYoungFreeCSet", "Non-Young Free Collection Set (ms):", max_gc_threads);
146   _gc_par_phases[RebuildFreeList] = new WorkerDataArray<double>("RebuildFreeList", "Parallel Rebuild Free List (ms):", max_gc_threads);
147 
148   reset();
149 }
150 
reset()151 void G1GCPhaseTimes::reset() {
152   _cur_collection_initial_evac_time_ms = 0.0;
153   _cur_optional_evac_time_ms = 0.0;
154   _cur_collection_code_root_fixup_time_ms = 0.0;
155   _cur_strong_code_root_purge_time_ms = 0.0;
156   _cur_merge_heap_roots_time_ms = 0.0;
157   _cur_optional_merge_heap_roots_time_ms = 0.0;
158   _cur_prepare_merge_heap_roots_time_ms = 0.0;
159   _cur_optional_prepare_merge_heap_roots_time_ms = 0.0;
160   _cur_evac_fail_recalc_used = 0.0;
161   _cur_evac_fail_remove_self_forwards = 0.0;
162   _cur_string_deduplication_time_ms = 0.0;
163   _cur_prepare_tlab_time_ms = 0.0;
164   _cur_resize_tlab_time_ms = 0.0;
165   _cur_concatenate_dirty_card_logs_time_ms = 0.0;
166   _cur_derived_pointer_table_update_time_ms = 0.0;
167   _cur_clear_ct_time_ms = 0.0;
168   _cur_expand_heap_time_ms = 0.0;
169   _cur_ref_proc_time_ms = 0.0;
170   _cur_collection_start_sec = 0.0;
171   _root_region_scan_wait_time_ms = 0.0;
172   _external_accounted_time_ms = 0.0;
173   _recorded_prepare_heap_roots_time_ms = 0.0;
174   _recorded_clear_claimed_marks_time_ms = 0.0;
175   _recorded_young_cset_choice_time_ms = 0.0;
176   _recorded_non_young_cset_choice_time_ms = 0.0;
177   _recorded_redirty_logged_cards_time_ms = 0.0;
178   _recorded_preserve_cm_referents_time_ms = 0.0;
179   _recorded_start_new_cset_time_ms = 0.0;
180   _recorded_total_free_cset_time_ms = 0.0;
181   _recorded_serial_free_cset_time_ms = 0.0;
182   _recorded_total_rebuild_freelist_time_ms = 0.0;
183   _recorded_serial_rebuild_freelist_time_ms = 0.0;
184   _cur_fast_reclaim_humongous_time_ms = 0.0;
185   _cur_region_register_time = 0.0;
186   _cur_fast_reclaim_humongous_total = 0;
187   _cur_fast_reclaim_humongous_candidates = 0;
188   _cur_fast_reclaim_humongous_reclaimed = 0;
189   _cur_verify_before_time_ms = 0.0;
190   _cur_verify_after_time_ms = 0.0;
191 
192   for (int i = 0; i < GCParPhasesSentinel; i++) {
193     if (_gc_par_phases[i] != NULL) {
194       _gc_par_phases[i]->reset();
195     }
196   }
197 
198   _ref_phase_times.reset();
199   _weak_phase_times.reset();
200 }
201 
note_gc_start()202 void G1GCPhaseTimes::note_gc_start() {
203   _gc_start_counter = os::elapsed_counter();
204   reset();
205 }
206 
207 #define ASSERT_PHASE_UNINITIALIZED(phase) \
208     assert(_gc_par_phases[phase] == NULL || _gc_par_phases[phase]->get(i) == uninitialized, "Phase " #phase " reported for thread that was not started");
209 
worker_time(GCParPhases phase,uint worker)210 double G1GCPhaseTimes::worker_time(GCParPhases phase, uint worker) {
211   if (_gc_par_phases[phase] == NULL) {
212     return 0.0;
213   }
214   double value = _gc_par_phases[phase]->get(worker);
215   if (value != WorkerDataArray<double>::uninitialized()) {
216     return value;
217   }
218   return 0.0;
219 }
220 
note_gc_end()221 void G1GCPhaseTimes::note_gc_end() {
222   _gc_pause_time_ms = TimeHelper::counter_to_millis(os::elapsed_counter() - _gc_start_counter);
223 
224   double uninitialized = WorkerDataArray<double>::uninitialized();
225 
226   for (uint i = 0; i < _max_gc_threads; i++) {
227     double worker_start = _gc_par_phases[GCWorkerStart]->get(i);
228     if (worker_start != uninitialized) {
229       assert(_gc_par_phases[GCWorkerEnd]->get(i) != uninitialized, "Worker started but not ended.");
230       double total_worker_time = _gc_par_phases[GCWorkerEnd]->get(i) - _gc_par_phases[GCWorkerStart]->get(i);
231       record_time_secs(GCWorkerTotal, i , total_worker_time);
232 
233       double worker_known_time = worker_time(ExtRootScan, i) +
234                                  worker_time(ScanHR, i) +
235                                  worker_time(CodeRoots, i) +
236                                  worker_time(ObjCopy, i) +
237                                  worker_time(Termination, i);
238 
239       record_time_secs(Other, i, total_worker_time - worker_known_time);
240     } else {
241       // Make sure all slots are uninitialized since this thread did not seem to have been started
242       ASSERT_PHASE_UNINITIALIZED(GCWorkerEnd);
243       ASSERT_PHASE_UNINITIALIZED(ExtRootScan);
244       ASSERT_PHASE_UNINITIALIZED(MergeER);
245       ASSERT_PHASE_UNINITIALIZED(MergeRS);
246       ASSERT_PHASE_UNINITIALIZED(OptMergeRS);
247       ASSERT_PHASE_UNINITIALIZED(MergeHCC);
248       ASSERT_PHASE_UNINITIALIZED(MergeLB);
249       ASSERT_PHASE_UNINITIALIZED(ScanHR);
250       ASSERT_PHASE_UNINITIALIZED(CodeRoots);
251       ASSERT_PHASE_UNINITIALIZED(OptCodeRoots);
252       ASSERT_PHASE_UNINITIALIZED(ObjCopy);
253       ASSERT_PHASE_UNINITIALIZED(OptObjCopy);
254       ASSERT_PHASE_UNINITIALIZED(Termination);
255     }
256   }
257 }
258 
259 #undef ASSERT_PHASE_UNINITIALIZED
260 
261 // record the time a phase took in seconds
record_time_secs(GCParPhases phase,uint worker_id,double secs)262 void G1GCPhaseTimes::record_time_secs(GCParPhases phase, uint worker_id, double secs) {
263   _gc_par_phases[phase]->set(worker_id, secs);
264 }
265 
266 // add a number of seconds to a phase
add_time_secs(GCParPhases phase,uint worker_id,double secs)267 void G1GCPhaseTimes::add_time_secs(GCParPhases phase, uint worker_id, double secs) {
268   _gc_par_phases[phase]->add(worker_id, secs);
269 }
270 
record_or_add_time_secs(GCParPhases phase,uint worker_id,double secs)271 void G1GCPhaseTimes::record_or_add_time_secs(GCParPhases phase, uint worker_id, double secs) {
272   if (_gc_par_phases[phase]->get(worker_id) == _gc_par_phases[phase]->uninitialized()) {
273     record_time_secs(phase, worker_id, secs);
274   } else {
275     add_time_secs(phase, worker_id, secs);
276   }
277 }
278 
get_time_secs(GCParPhases phase,uint worker_id)279 double G1GCPhaseTimes::get_time_secs(GCParPhases phase, uint worker_id) {
280   return _gc_par_phases[phase]->get(worker_id);
281 }
282 
record_thread_work_item(GCParPhases phase,uint worker_id,size_t count,uint index)283 void G1GCPhaseTimes::record_thread_work_item(GCParPhases phase, uint worker_id, size_t count, uint index) {
284   _gc_par_phases[phase]->set_thread_work_item(worker_id, count, index);
285 }
286 
record_or_add_thread_work_item(GCParPhases phase,uint worker_id,size_t count,uint index)287 void G1GCPhaseTimes::record_or_add_thread_work_item(GCParPhases phase, uint worker_id, size_t count, uint index) {
288   _gc_par_phases[phase]->set_or_add_thread_work_item(worker_id, count, index);
289 }
290 
get_thread_work_item(GCParPhases phase,uint worker_id,uint index)291 size_t G1GCPhaseTimes::get_thread_work_item(GCParPhases phase, uint worker_id, uint index) {
292   return _gc_par_phases[phase]->get_thread_work_item(worker_id, index);
293 }
294 
295 // return the average time for a phase in milliseconds
average_time_ms(GCParPhases phase)296 double G1GCPhaseTimes::average_time_ms(GCParPhases phase) {
297   if (_gc_par_phases[phase] == NULL) {
298     return 0.0;
299   }
300   return _gc_par_phases[phase]->average() * 1000.0;
301 }
302 
sum_thread_work_items(GCParPhases phase,uint index)303 size_t G1GCPhaseTimes::sum_thread_work_items(GCParPhases phase, uint index) {
304   if (_gc_par_phases[phase] == NULL) {
305     return 0;
306   }
307   assert(_gc_par_phases[phase]->thread_work_items(index) != NULL, "No sub count");
308   return _gc_par_phases[phase]->thread_work_items(index)->sum();
309 }
310 
311 template <class T>
details(T * phase,const char * indent_str) const312 void G1GCPhaseTimes::details(T* phase, const char* indent_str) const {
313   LogTarget(Trace, gc, phases, task) lt;
314   if (lt.is_enabled()) {
315     LogStream ls(lt);
316     ls.print("%s", indent_str);
317     phase->print_details_on(&ls);
318   }
319 }
320 
log_phase(WorkerDataArray<double> * phase,uint indent_level,outputStream * out,bool print_sum) const321 void G1GCPhaseTimes::log_phase(WorkerDataArray<double>* phase, uint indent_level, outputStream* out, bool print_sum) const {
322   out->print("%s", indent(indent_level));
323   phase->print_summary_on(out, print_sum);
324   details(phase, indent(indent_level));
325 
326   for (uint i = 0; i < phase->MaxThreadWorkItems; i++) {
327     WorkerDataArray<size_t>* work_items = phase->thread_work_items(i);
328     if (work_items != NULL) {
329       out->print("%s", indent(indent_level + 1));
330       work_items->print_summary_on(out, true);
331       details(work_items, indent(indent_level + 1));
332     }
333   }
334 }
335 
debug_phase(WorkerDataArray<double> * phase,uint extra_indent) const336 void G1GCPhaseTimes::debug_phase(WorkerDataArray<double>* phase, uint extra_indent) const {
337   LogTarget(Debug, gc, phases) lt;
338   if (lt.is_enabled()) {
339     ResourceMark rm;
340     LogStream ls(lt);
341     log_phase(phase, 2 + extra_indent, &ls, true);
342   }
343 }
344 
trace_phase(WorkerDataArray<double> * phase,bool print_sum,uint extra_indent) const345 void G1GCPhaseTimes::trace_phase(WorkerDataArray<double>* phase, bool print_sum, uint extra_indent) const {
346   LogTarget(Trace, gc, phases) lt;
347   if (lt.is_enabled()) {
348     LogStream ls(lt);
349     log_phase(phase, 3 + extra_indent, &ls, print_sum);
350   }
351 }
352 
353 #define TIME_FORMAT "%.1lfms"
354 
info_time(const char * name,double value) const355 void G1GCPhaseTimes::info_time(const char* name, double value) const {
356   log_info(gc, phases)("%s%s: " TIME_FORMAT, indent(1), name, value);
357 }
358 
debug_time(const char * name,double value) const359 void G1GCPhaseTimes::debug_time(const char* name, double value) const {
360   log_debug(gc, phases)("%s%s: " TIME_FORMAT, indent(2), name, value);
361 }
362 
debug_time_for_reference(const char * name,double value) const363 void G1GCPhaseTimes::debug_time_for_reference(const char* name, double value) const {
364   LogTarget(Debug, gc, phases) lt;
365   LogTarget(Debug, gc, phases, ref) lt2;
366 
367   if (lt.is_enabled()) {
368     LogStream ls(lt);
369     ls.print_cr("%s%s: " TIME_FORMAT, indent(2), name, value);
370   } else if (lt2.is_enabled()) {
371     LogStream ls(lt2);
372     ls.print_cr("%s%s: " TIME_FORMAT, indent(2), name, value);
373   }
374 }
375 
trace_time(const char * name,double value) const376 void G1GCPhaseTimes::trace_time(const char* name, double value) const {
377   log_trace(gc, phases)("%s%s: " TIME_FORMAT, indent(3), name, value);
378 }
379 
trace_count(const char * name,size_t value) const380 void G1GCPhaseTimes::trace_count(const char* name, size_t value) const {
381   log_trace(gc, phases)("%s%s: " SIZE_FORMAT, indent(3), name, value);
382 }
383 
print_pre_evacuate_collection_set() const384 double G1GCPhaseTimes::print_pre_evacuate_collection_set() const {
385   const double sum_ms = _root_region_scan_wait_time_ms +
386                         _cur_prepare_tlab_time_ms +
387                         _cur_concatenate_dirty_card_logs_time_ms +
388                         _recorded_young_cset_choice_time_ms +
389                         _recorded_non_young_cset_choice_time_ms +
390                         _cur_region_register_time +
391                         _recorded_prepare_heap_roots_time_ms +
392                         _recorded_clear_claimed_marks_time_ms;
393 
394   info_time("Pre Evacuate Collection Set", sum_ms);
395 
396   if (_root_region_scan_wait_time_ms > 0.0) {
397     debug_time("Root Region Scan Waiting", _root_region_scan_wait_time_ms);
398   }
399   debug_time("Prepare TLABs", _cur_prepare_tlab_time_ms);
400   debug_time("Concatenate Dirty Card Logs", _cur_concatenate_dirty_card_logs_time_ms);
401   debug_time("Choose Collection Set", (_recorded_young_cset_choice_time_ms + _recorded_non_young_cset_choice_time_ms));
402   debug_time("Region Register", _cur_region_register_time);
403   if (G1EagerReclaimHumongousObjects) {
404     trace_count("Humongous Total", _cur_fast_reclaim_humongous_total);
405     trace_count("Humongous Candidate", _cur_fast_reclaim_humongous_candidates);
406   }
407 
408   debug_time("Prepare Heap Roots", _recorded_prepare_heap_roots_time_ms);
409   if (_recorded_clear_claimed_marks_time_ms > 0.0) {
410     debug_time("Clear Claimed Marks", _recorded_clear_claimed_marks_time_ms);
411   }
412   return sum_ms;
413 }
414 
print_evacuate_optional_collection_set() const415 double G1GCPhaseTimes::print_evacuate_optional_collection_set() const {
416   const double sum_ms = _cur_optional_evac_time_ms + _cur_optional_merge_heap_roots_time_ms;
417   if (sum_ms > 0) {
418     info_time("Merge Optional Heap Roots", _cur_optional_merge_heap_roots_time_ms);
419 
420     debug_time("Prepare Optional Merge Heap Roots", _cur_optional_prepare_merge_heap_roots_time_ms);
421     debug_phase(_gc_par_phases[OptMergeRS]);
422 
423     info_time("Evacuate Optional Collection Set", _cur_optional_evac_time_ms);
424     debug_phase(_gc_par_phases[OptScanHR]);
425     debug_phase(_gc_par_phases[OptObjCopy]);
426     debug_phase(_gc_par_phases[OptCodeRoots]);
427     debug_phase(_gc_par_phases[OptTermination]);
428   }
429   return sum_ms;
430 }
431 
print_evacuate_initial_collection_set() const432 double G1GCPhaseTimes::print_evacuate_initial_collection_set() const {
433   info_time("Merge Heap Roots", _cur_merge_heap_roots_time_ms);
434 
435   debug_time("Prepare Merge Heap Roots", _cur_prepare_merge_heap_roots_time_ms);
436   debug_phase(_gc_par_phases[MergeER]);
437   debug_phase(_gc_par_phases[MergeRS]);
438   if (G1HotCardCache::default_use_cache()) {
439     debug_phase(_gc_par_phases[MergeHCC]);
440   }
441   debug_phase(_gc_par_phases[MergeLB]);
442 
443   info_time("Evacuate Collection Set", _cur_collection_initial_evac_time_ms);
444 
445   trace_phase(_gc_par_phases[GCWorkerStart], false);
446   debug_phase(_gc_par_phases[ExtRootScan]);
447   for (int i = ExtRootScanSubPhasesFirst; i <= ExtRootScanSubPhasesLast; i++) {
448     trace_phase(_gc_par_phases[i]);
449   }
450   debug_phase(_gc_par_phases[ScanHR]);
451   debug_phase(_gc_par_phases[CodeRoots]);
452   debug_phase(_gc_par_phases[ObjCopy]);
453   debug_phase(_gc_par_phases[Termination]);
454   debug_phase(_gc_par_phases[Other]);
455   debug_phase(_gc_par_phases[GCWorkerTotal]);
456   trace_phase(_gc_par_phases[GCWorkerEnd], false);
457 
458   return _cur_collection_initial_evac_time_ms + _cur_merge_heap_roots_time_ms;
459 }
460 
print_post_evacuate_collection_set() const461 double G1GCPhaseTimes::print_post_evacuate_collection_set() const {
462   const double evac_fail_handling = _cur_evac_fail_recalc_used +
463                                     _cur_evac_fail_remove_self_forwards;
464   assert(_gc_par_phases[MergePSS]->get(0) != WorkerDataArray<double>::uninitialized(), "must be set");
465   const double merge_pss = _gc_par_phases[MergePSS]->get(0) * MILLIUNITS;
466   const double sum_ms = evac_fail_handling +
467                         _cur_collection_code_root_fixup_time_ms +
468                         _recorded_preserve_cm_referents_time_ms +
469                         _cur_ref_proc_time_ms +
470                         (_weak_phase_times.total_time_sec() * MILLIUNITS) +
471                         _cur_clear_ct_time_ms +
472                         merge_pss +
473                         _cur_strong_code_root_purge_time_ms +
474                         _recorded_redirty_logged_cards_time_ms +
475                         _recorded_total_free_cset_time_ms +
476                         _recorded_total_rebuild_freelist_time_ms +
477                         _cur_fast_reclaim_humongous_time_ms +
478                         _cur_expand_heap_time_ms +
479                         _cur_string_deduplication_time_ms;
480 
481   info_time("Post Evacuate Collection Set", sum_ms);
482 
483   debug_time("Code Roots Fixup", _cur_collection_code_root_fixup_time_ms);
484 
485   debug_time("Clear Card Table", _cur_clear_ct_time_ms);
486 
487   debug_time_for_reference("Reference Processing", _cur_ref_proc_time_ms);
488   _ref_phase_times.print_all_references(2, false);
489   _weak_phase_times.log_print(2);
490 
491   if (G1StringDedup::is_enabled()) {
492     debug_time("String Deduplication", _cur_string_deduplication_time_ms);
493     debug_phase(_gc_par_phases[StringDedupQueueFixup], 1);
494     debug_phase(_gc_par_phases[StringDedupTableFixup], 1);
495   }
496 
497   if (G1CollectedHeap::heap()->evacuation_failed()) {
498     debug_time("Evacuation Failure", evac_fail_handling);
499     trace_time("Recalculate Used", _cur_evac_fail_recalc_used);
500     trace_time("Remove Self Forwards",_cur_evac_fail_remove_self_forwards);
501   }
502 
503   debug_phase(_gc_par_phases[MergePSS], 0);
504   debug_time("Code Roots Purge", _cur_strong_code_root_purge_time_ms);
505 
506   debug_time("Redirty Cards", _recorded_redirty_logged_cards_time_ms);
507   trace_phase(_gc_par_phases[RedirtyCards]);
508 #if COMPILER2_OR_JVMCI
509   debug_time("DerivedPointerTable Update", _cur_derived_pointer_table_update_time_ms);
510 #endif
511 
512   debug_time("Free Collection Set", _recorded_total_free_cset_time_ms);
513   trace_time("Serial Free Collection Set", _recorded_serial_free_cset_time_ms);
514   trace_phase(_gc_par_phases[ParFreeCSet]);
515   trace_phase(_gc_par_phases[YoungFreeCSet], true, 1);
516   trace_phase(_gc_par_phases[NonYoungFreeCSet], true, 1);
517 
518   debug_time("Rebuild Free List", _recorded_total_rebuild_freelist_time_ms);
519   trace_time("Serial Rebuild Free List ", _recorded_serial_rebuild_freelist_time_ms);
520   trace_phase(_gc_par_phases[RebuildFreeList]);
521 
522   if (G1EagerReclaimHumongousObjects) {
523     debug_time("Humongous Reclaim", _cur_fast_reclaim_humongous_time_ms);
524     trace_count("Humongous Reclaimed", _cur_fast_reclaim_humongous_reclaimed);
525   }
526   debug_time("Start New Collection Set", _recorded_start_new_cset_time_ms);
527   if (UseTLAB && ResizeTLAB) {
528     debug_time("Resize TLABs", _cur_resize_tlab_time_ms);
529   }
530   debug_time("Expand Heap After Collection", _cur_expand_heap_time_ms);
531 
532 
533   return sum_ms;
534 }
535 
print_other(double accounted_ms) const536 void G1GCPhaseTimes::print_other(double accounted_ms) const {
537   info_time("Other", _gc_pause_time_ms - accounted_ms);
538 }
539 
print()540 void G1GCPhaseTimes::print() {
541   note_gc_end();
542 
543   if (_cur_verify_before_time_ms > 0.0) {
544     debug_time("Verify Before", _cur_verify_before_time_ms);
545   }
546 
547   double accounted_ms = 0.0;
548   accounted_ms += print_pre_evacuate_collection_set();
549   accounted_ms += print_evacuate_initial_collection_set();
550   accounted_ms += print_evacuate_optional_collection_set();
551   accounted_ms += print_post_evacuate_collection_set();
552   print_other(accounted_ms);
553 
554   if (_cur_verify_after_time_ms > 0.0) {
555     debug_time("Verify After", _cur_verify_after_time_ms);
556   }
557 }
558 
phase_name(GCParPhases phase)559 const char* G1GCPhaseTimes::phase_name(GCParPhases phase) {
560   G1GCPhaseTimes* phase_times = G1CollectedHeap::heap()->phase_times();
561   return phase_times->_gc_par_phases[phase]->short_name();
562 }
563 
G1EvacPhaseWithTrimTimeTracker(G1ParScanThreadState * pss,Tickspan & total_time,Tickspan & trim_time)564 G1EvacPhaseWithTrimTimeTracker::G1EvacPhaseWithTrimTimeTracker(G1ParScanThreadState* pss, Tickspan& total_time, Tickspan& trim_time) :
565   _pss(pss),
566   _start(Ticks::now()),
567   _total_time(total_time),
568   _trim_time(trim_time),
569   _stopped(false) {
570 
571   assert(_pss->trim_ticks().value() == 0, "Possibly remaining trim ticks left over from previous use");
572 }
573 
~G1EvacPhaseWithTrimTimeTracker()574 G1EvacPhaseWithTrimTimeTracker::~G1EvacPhaseWithTrimTimeTracker() {
575   if (!_stopped) {
576     stop();
577   }
578 }
579 
stop()580 void G1EvacPhaseWithTrimTimeTracker::stop() {
581   assert(!_stopped, "Should only be called once");
582   _total_time += (Ticks::now() - _start) - _pss->trim_ticks();
583   _trim_time += _pss->trim_ticks();
584   _pss->reset_trim_ticks();
585   _stopped = true;
586 }
587 
G1GCParPhaseTimesTracker(G1GCPhaseTimes * phase_times,G1GCPhaseTimes::GCParPhases phase,uint worker_id,bool must_record)588 G1GCParPhaseTimesTracker::G1GCParPhaseTimesTracker(G1GCPhaseTimes* phase_times, G1GCPhaseTimes::GCParPhases phase, uint worker_id, bool must_record) :
589   _start_time(), _phase(phase), _phase_times(phase_times), _worker_id(worker_id), _event(), _must_record(must_record) {
590   if (_phase_times != NULL) {
591     _start_time = Ticks::now();
592   }
593 }
594 
~G1GCParPhaseTimesTracker()595 G1GCParPhaseTimesTracker::~G1GCParPhaseTimesTracker() {
596   if (_phase_times != NULL) {
597     if (_must_record) {
598       _phase_times->record_time_secs(_phase, _worker_id, (Ticks::now() - _start_time).seconds());
599     } else {
600       _phase_times->record_or_add_time_secs(_phase, _worker_id, (Ticks::now() - _start_time).seconds());
601     }
602     _event.commit(GCId::current(), _worker_id, G1GCPhaseTimes::phase_name(_phase));
603   }
604 }
605 
G1EvacPhaseTimesTracker(G1GCPhaseTimes * phase_times,G1ParScanThreadState * pss,G1GCPhaseTimes::GCParPhases phase,uint worker_id)606 G1EvacPhaseTimesTracker::G1EvacPhaseTimesTracker(G1GCPhaseTimes* phase_times,
607                                                  G1ParScanThreadState* pss,
608                                                  G1GCPhaseTimes::GCParPhases phase,
609                                                  uint worker_id) :
610   G1GCParPhaseTimesTracker(phase_times, phase, worker_id),
611   _total_time(),
612   _trim_time(),
613   _trim_tracker(pss, _total_time, _trim_time) {
614 }
615 
~G1EvacPhaseTimesTracker()616 G1EvacPhaseTimesTracker::~G1EvacPhaseTimesTracker() {
617   if (_phase_times != NULL) {
618     // Explicitly stop the trim tracker since it's not yet destructed.
619     _trim_tracker.stop();
620     // Exclude trim time by increasing the start time.
621     _start_time += _trim_time;
622     _phase_times->record_or_add_time_secs(G1GCPhaseTimes::ObjCopy, _worker_id, _trim_time.seconds());
623   }
624 }
625 
626