1 /*
2 * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "precompiled.hpp"
27
28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
29 #include "gc/shenandoah/shenandoahGC.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "runtime/os.hpp"
32
33 ShenandoahCollectorPolicy::ShenandoahCollectorPolicy() :
34 _success_concurrent_gcs(0),
35 _abbreviated_concurrent_gcs(0),
36 _success_degenerated_gcs(0),
37 _abbreviated_degenerated_gcs(0),
38 _success_full_gcs(0),
39 _consecutive_degenerated_gcs(0),
40 _consecutive_degenerated_gcs_without_progress(0),
41 _consecutive_young_gcs(0),
42 _mixed_gcs(0),
43 _success_old_gcs(0),
44 _interrupted_old_gcs(0),
45 _alloc_failure_degenerated(0),
46 _alloc_failure_degenerated_upgrade_to_full(0),
47 _alloc_failure_full(0) {
48
49 Copy::zero_to_bytes(_degen_point_counts, sizeof(size_t) * ShenandoahGC::_DEGENERATED_LIMIT);
50 Copy::zero_to_bytes(_collection_cause_counts, sizeof(size_t) * GCCause::_last_gc_cause);
51
52 _tracer = new ShenandoahTracer();
53 }
54
55 void ShenandoahCollectorPolicy::record_collection_cause(GCCause::Cause cause) {
56 assert(cause < GCCause::_last_gc_cause, "Invalid GCCause");
57 _collection_cause_counts[cause]++;
58 }
59
60 void ShenandoahCollectorPolicy::record_alloc_failure_to_full() {
61 _alloc_failure_full++;
62 }
63
64 void ShenandoahCollectorPolicy::record_alloc_failure_to_degenerated(ShenandoahGC::ShenandoahDegenPoint point) {
65 assert(point < ShenandoahGC::_DEGENERATED_LIMIT, "sanity");
66 _alloc_failure_degenerated++;
67 _degen_point_counts[point]++;
68 }
69
70 void ShenandoahCollectorPolicy::record_degenerated_upgrade_to_full() {
71 reset_consecutive_degenerated_gcs();
72 _alloc_failure_degenerated_upgrade_to_full++;
73 }
74
75 void ShenandoahCollectorPolicy::record_success_concurrent(bool is_young, bool is_abbreviated) {
76 update_young(is_young);
77
78 reset_consecutive_degenerated_gcs();
79 _success_concurrent_gcs++;
80 if (is_abbreviated) {
81 _abbreviated_concurrent_gcs++;
82 }
83 }
84
85 void ShenandoahCollectorPolicy::record_mixed_cycle() {
86 _mixed_gcs++;
87 }
88
89 void ShenandoahCollectorPolicy::record_success_old() {
90 _consecutive_young_gcs = 0;
91 _success_old_gcs++;
92 }
93
94 void ShenandoahCollectorPolicy::record_interrupted_old() {
95 _consecutive_young_gcs = 0;
96 _interrupted_old_gcs++;
97 }
98
99 void ShenandoahCollectorPolicy::record_degenerated(bool is_young, bool is_abbreviated, bool progress) {
100 update_young(is_young);
101
102 _success_degenerated_gcs++;
103 _consecutive_degenerated_gcs++;
104
105 if (progress) {
106 _consecutive_degenerated_gcs_without_progress = 0;
107 } else {
108 _consecutive_degenerated_gcs_without_progress++;
109 }
110
111 if (is_abbreviated) {
112 _abbreviated_degenerated_gcs++;
113 }
114 }
115
116 void ShenandoahCollectorPolicy::update_young(bool is_young) {
117 if (is_young) {
118 _consecutive_young_gcs++;
119 } else {
120 _consecutive_young_gcs = 0;
121 }
122 }
123
124 void ShenandoahCollectorPolicy::record_success_full() {
125 reset_consecutive_degenerated_gcs();
126 _consecutive_young_gcs = 0;
127 _success_full_gcs++;
128 }
129
130 void ShenandoahCollectorPolicy::record_shutdown() {
131 _in_shutdown.set();
132 }
133
134 bool ShenandoahCollectorPolicy::is_at_shutdown() const {
135 return _in_shutdown.is_set();
136 }
137
138 bool ShenandoahCollectorPolicy::is_explicit_gc(GCCause::Cause cause) {
139 return GCCause::is_user_requested_gc(cause)
140 || GCCause::is_serviceability_requested_gc(cause)
141 || cause == GCCause::_wb_full_gc
142 || cause == GCCause::_wb_young_gc;
143 }
144
145 bool is_implicit_gc(GCCause::Cause cause) {
146 return cause != GCCause::_no_gc
147 && cause != GCCause::_shenandoah_concurrent_gc
148 && cause != GCCause::_allocation_failure
149 && !ShenandoahCollectorPolicy::is_explicit_gc(cause);
150 }
151
152 #ifdef ASSERT
153 bool is_valid_request(GCCause::Cause cause) {
154 return ShenandoahCollectorPolicy::is_explicit_gc(cause)
155 || ShenandoahCollectorPolicy::is_shenandoah_gc(cause)
156 || cause == GCCause::_metadata_GC_clear_soft_refs
157 || cause == GCCause::_codecache_GC_aggressive
158 || cause == GCCause::_codecache_GC_threshold
159 || cause == GCCause::_full_gc_alot
160 || cause == GCCause::_wb_young_gc
161 || cause == GCCause::_wb_full_gc
162 || cause == GCCause::_wb_breakpoint
163 || cause == GCCause::_scavenge_alot;
164 }
165 #endif
166
167 bool ShenandoahCollectorPolicy::is_shenandoah_gc(GCCause::Cause cause) {
168 return cause == GCCause::_allocation_failure
169 || cause == GCCause::_shenandoah_stop_vm
170 || cause == GCCause::_shenandoah_allocation_failure_evac
171 || cause == GCCause::_shenandoah_humongous_allocation_failure
172 || cause == GCCause::_shenandoah_concurrent_gc
173 || cause == GCCause::_shenandoah_upgrade_to_full_gc;
174 }
175
176
177 bool ShenandoahCollectorPolicy::is_allocation_failure(GCCause::Cause cause) {
178 return cause == GCCause::_allocation_failure
179 || cause == GCCause::_shenandoah_allocation_failure_evac
180 || cause == GCCause::_shenandoah_humongous_allocation_failure;
181 }
182
183 bool ShenandoahCollectorPolicy::is_requested_gc(GCCause::Cause cause) {
184 return is_explicit_gc(cause) || is_implicit_gc(cause);
185 }
186
187 bool ShenandoahCollectorPolicy::should_run_full_gc(GCCause::Cause cause) {
188 return is_explicit_gc(cause) ? !ExplicitGCInvokesConcurrent : !ShenandoahImplicitGCInvokesConcurrent;
189 }
190
191 bool ShenandoahCollectorPolicy::should_handle_requested_gc(GCCause::Cause cause) {
192 assert(is_valid_request(cause), "only requested GCs here: %s", GCCause::to_string(cause));
193
194 if (DisableExplicitGC) {
195 return !is_explicit_gc(cause);
196 }
197 return true;
198 }
199
200 void ShenandoahCollectorPolicy::print_gc_stats(outputStream* out) const {
201 out->print_cr("Under allocation pressure, concurrent cycles may cancel, and either continue cycle");
202 out->print_cr("under stop-the-world pause or result in stop-the-world Full GC. Increase heap size,");
203 out->print_cr("tune GC heuristics, set more aggressive pacing delay, or lower allocation rate");
204 out->print_cr("to avoid Degenerated and Full GC cycles. Abbreviated cycles are those which found");
205 out->print_cr("enough regions with no live objects to skip evacuation.");
206 out->cr();
207
208 size_t completed_gcs = _success_full_gcs + _success_degenerated_gcs + _success_concurrent_gcs + _success_old_gcs;
209 out->print_cr(SIZE_FORMAT_W(5) " Completed GCs", completed_gcs);
210
211 size_t explicit_requests = 0;
212 size_t implicit_requests = 0;
213 for (int c = 0; c < GCCause::_last_gc_cause; c++) {
214 size_t cause_count = _collection_cause_counts[c];
215 if (cause_count > 0) {
216 auto cause = (GCCause::Cause) c;
217 if (is_explicit_gc(cause)) {
218 explicit_requests += cause_count;
219 } else if (is_implicit_gc(cause)) {
220 implicit_requests += cause_count;
221 }
222 const char* desc = GCCause::to_string(cause);
223 out->print_cr(" " SIZE_FORMAT_W(5) " caused by %s (%.2f%%)", cause_count, desc, percent_of(cause_count, completed_gcs));
224 }
225 }
226
227 out->cr();
228 out->print_cr(SIZE_FORMAT_W(5) " Successful Concurrent GCs (%.2f%%)", _success_concurrent_gcs, percent_of(_success_concurrent_gcs, completed_gcs));
229 if (ExplicitGCInvokesConcurrent) {
230 out->print_cr(" " SIZE_FORMAT_W(5) " invoked explicitly (%.2f%%)", explicit_requests, percent_of(explicit_requests, _success_concurrent_gcs));
231 }
232 if (ShenandoahImplicitGCInvokesConcurrent) {
233 out->print_cr(" " SIZE_FORMAT_W(5) " invoked implicitly (%.2f%%)", implicit_requests, percent_of(implicit_requests, _success_concurrent_gcs));
234 }
235 out->print_cr(" " SIZE_FORMAT_W(5) " abbreviated (%.2f%%)", _abbreviated_concurrent_gcs, percent_of(_abbreviated_concurrent_gcs, _success_concurrent_gcs));
236 out->cr();
237
238 if (ShenandoahHeap::heap()->mode()->is_generational()) {
239 out->print_cr(SIZE_FORMAT_W(5) " Completed Old GCs (%.2f%%)", _success_old_gcs, percent_of(_success_old_gcs, completed_gcs));
240 out->print_cr(" " SIZE_FORMAT_W(5) " mixed", _mixed_gcs);
241 out->print_cr(" " SIZE_FORMAT_W(5) " interruptions", _interrupted_old_gcs);
242 out->cr();
243 }
244
245 size_t degenerated_gcs = _alloc_failure_degenerated_upgrade_to_full + _success_degenerated_gcs;
246 out->print_cr(SIZE_FORMAT_W(5) " Degenerated GCs (%.2f%%)", degenerated_gcs, percent_of(degenerated_gcs, completed_gcs));
247 out->print_cr(" " SIZE_FORMAT_W(5) " upgraded to Full GC (%.2f%%)", _alloc_failure_degenerated_upgrade_to_full, percent_of(_alloc_failure_degenerated_upgrade_to_full, degenerated_gcs));
248 out->print_cr(" " SIZE_FORMAT_W(5) " caused by allocation failure (%.2f%%)", _alloc_failure_degenerated, percent_of(_alloc_failure_degenerated, degenerated_gcs));
249 out->print_cr(" " SIZE_FORMAT_W(5) " abbreviated (%.2f%%)", _abbreviated_degenerated_gcs, percent_of(_abbreviated_degenerated_gcs, degenerated_gcs));
250 for (int c = 0; c < ShenandoahGC::_DEGENERATED_LIMIT; c++) {
251 if (_degen_point_counts[c] > 0) {
252 const char* desc = ShenandoahGC::degen_point_to_string((ShenandoahGC::ShenandoahDegenPoint)c);
253 out->print_cr(" " SIZE_FORMAT_W(5) " happened at %s", _degen_point_counts[c], desc);
254 }
255 }
256 out->cr();
257
258 out->print_cr(SIZE_FORMAT_W(5) " Full GCs (%.2f%%)", _success_full_gcs, percent_of(_success_full_gcs, completed_gcs));
259 if (!ExplicitGCInvokesConcurrent) {
260 out->print_cr(" " SIZE_FORMAT_W(5) " invoked explicitly (%.2f%%)", explicit_requests, percent_of(explicit_requests, _success_concurrent_gcs));
261 }
262 if (!ShenandoahImplicitGCInvokesConcurrent) {
263 out->print_cr(" " SIZE_FORMAT_W(5) " invoked implicitly (%.2f%%)", implicit_requests, percent_of(implicit_requests, _success_concurrent_gcs));
264 }
265 out->print_cr(" " SIZE_FORMAT_W(5) " caused by allocation failure (%.2f%%)", _alloc_failure_full, percent_of(_alloc_failure_full, _success_full_gcs));
266 out->print_cr(" " SIZE_FORMAT_W(5) " upgraded from Degenerated GC (%.2f%%)", _alloc_failure_degenerated_upgrade_to_full, percent_of(_alloc_failure_degenerated_upgrade_to_full, _success_full_gcs));
267 }