Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/gc/z/zDirector.cpp
40957 views
1
/*
2
* Copyright (c) 2015, 2021, 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
#include "precompiled.hpp"
25
#include "gc/shared/gc_globals.hpp"
26
#include "gc/z/zCollectedHeap.hpp"
27
#include "gc/z/zDirector.hpp"
28
#include "gc/z/zHeap.inline.hpp"
29
#include "gc/z/zHeuristics.hpp"
30
#include "gc/z/zStat.hpp"
31
#include "logging/log.hpp"
32
33
const double ZDirector::one_in_1000 = 3.290527;
34
35
ZDirector::ZDirector() :
36
_relocation_headroom(ZHeuristics::relocation_headroom()),
37
_metronome(ZStatAllocRate::sample_hz) {
38
set_name("ZDirector");
39
create_and_start();
40
}
41
42
void ZDirector::sample_allocation_rate() const {
43
// Sample allocation rate. This is needed by rule_allocation_rate()
44
// below to estimate the time we have until we run out of memory.
45
const double bytes_per_second = ZStatAllocRate::sample_and_reset();
46
47
log_debug(gc, alloc)("Allocation Rate: %.3fMB/s, Avg: %.3f(+/-%.3f)MB/s",
48
bytes_per_second / M,
49
ZStatAllocRate::avg() / M,
50
ZStatAllocRate::avg_sd() / M);
51
}
52
53
bool ZDirector::rule_timer() const {
54
if (ZCollectionInterval <= 0) {
55
// Rule disabled
56
return false;
57
}
58
59
// Perform GC if timer has expired.
60
const double time_since_last_gc = ZStatCycle::time_since_last();
61
const double time_until_gc = ZCollectionInterval - time_since_last_gc;
62
63
log_debug(gc, director)("Rule: Timer, Interval: %.3fs, TimeUntilGC: %.3fs",
64
ZCollectionInterval, time_until_gc);
65
66
return time_until_gc <= 0;
67
}
68
69
bool ZDirector::rule_warmup() const {
70
if (ZStatCycle::is_warm()) {
71
// Rule disabled
72
return false;
73
}
74
75
// Perform GC if heap usage passes 10/20/30% and no other GC has been
76
// performed yet. This allows us to get some early samples of the GC
77
// duration, which is needed by the other rules.
78
const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();
79
const size_t used = ZHeap::heap()->used();
80
const double used_threshold_percent = (ZStatCycle::nwarmup_cycles() + 1) * 0.1;
81
const size_t used_threshold = soft_max_capacity * used_threshold_percent;
82
83
log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB",
84
used_threshold_percent * 100, used / M, used_threshold / M);
85
86
return used >= used_threshold;
87
}
88
89
bool ZDirector::rule_allocation_rate() const {
90
if (!ZStatCycle::is_normalized_duration_trustable()) {
91
// Rule disabled
92
return false;
93
}
94
95
// Perform GC if the estimated max allocation rate indicates that we
96
// will run out of memory. The estimated max allocation rate is based
97
// on the moving average of the sampled allocation rate plus a safety
98
// margin based on variations in the allocation rate and unforeseen
99
// allocation spikes.
100
101
// Calculate amount of free memory available. Note that we take the
102
// relocation headroom into account to avoid in-place relocation.
103
const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();
104
const size_t used = ZHeap::heap()->used();
105
const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);
106
const size_t free = free_including_headroom - MIN2(free_including_headroom, _relocation_headroom);
107
108
// Calculate time until OOM given the max allocation rate and the amount
109
// of free memory. The allocation rate is a moving average and we multiply
110
// that with an allocation spike tolerance factor to guard against unforeseen
111
// phase changes in the allocate rate. We then add ~3.3 sigma to account for
112
// the allocation rate variance, which means the probability is 1 in 1000
113
// that a sample is outside of the confidence interval.
114
const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * one_in_1000);
115
const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero
116
117
// Calculate max duration of a GC cycle. The duration of GC is a moving
118
// average, we add ~3.3 sigma to account for the GC duration variance.
119
const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration();
120
const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000);
121
122
// Calculate time until GC given the time until OOM and max duration of GC.
123
// We also deduct the sample interval, so that we don't overshoot the target
124
// time and end up starting the GC too late in the next interval.
125
const double sample_interval = 1.0 / ZStatAllocRate::sample_hz;
126
const double time_until_gc = time_until_oom - max_duration_of_gc - sample_interval;
127
128
log_debug(gc, director)("Rule: Allocation Rate, MaxAllocRate: %.3fMB/s, Free: " SIZE_FORMAT "MB, MaxDurationOfGC: %.3fs, TimeUntilGC: %.3fs",
129
max_alloc_rate / M, free / M, max_duration_of_gc, time_until_gc);
130
131
return time_until_gc <= 0;
132
}
133
134
bool ZDirector::rule_proactive() const {
135
if (!ZProactive || !ZStatCycle::is_warm()) {
136
// Rule disabled
137
return false;
138
}
139
140
// Perform GC if the impact of doing so, in terms of application throughput
141
// reduction, is considered acceptable. This rule allows us to keep the heap
142
// size down and allow reference processing to happen even when we have a lot
143
// of free space on the heap.
144
145
// Only consider doing a proactive GC if the heap usage has grown by at least
146
// 10% of the max capacity since the previous GC, or more than 5 minutes has
147
// passed since the previous GC. This helps avoid superfluous GCs when running
148
// applications with very low allocation rate.
149
const size_t used_after_last_gc = ZStatHeap::used_at_relocate_end();
150
const size_t used_increase_threshold = ZHeap::heap()->soft_max_capacity() * 0.10; // 10%
151
const size_t used_threshold = used_after_last_gc + used_increase_threshold;
152
const size_t used = ZHeap::heap()->used();
153
const double time_since_last_gc = ZStatCycle::time_since_last();
154
const double time_since_last_gc_threshold = 5 * 60; // 5 minutes
155
if (used < used_threshold && time_since_last_gc < time_since_last_gc_threshold) {
156
// Don't even consider doing a proactive GC
157
log_debug(gc, director)("Rule: Proactive, UsedUntilEnabled: " SIZE_FORMAT "MB, TimeUntilEnabled: %.3fs",
158
(used_threshold - used) / M,
159
time_since_last_gc_threshold - time_since_last_gc);
160
return false;
161
}
162
163
const double assumed_throughput_drop_during_gc = 0.50; // 50%
164
const double acceptable_throughput_drop = 0.01; // 1%
165
const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration();
166
const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000);
167
const double acceptable_gc_interval = max_duration_of_gc * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0);
168
const double time_until_gc = acceptable_gc_interval - time_since_last_gc;
169
170
log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3fs, TimeSinceLastGC: %.3fs, TimeUntilGC: %.3fs",
171
acceptable_gc_interval, time_since_last_gc, time_until_gc);
172
173
return time_until_gc <= 0;
174
}
175
176
bool ZDirector::rule_high_usage() const {
177
// Perform GC if the amount of free memory is 5% or less. This is a preventive
178
// meassure in the case where the application has a very low allocation rate,
179
// such that the allocation rate rule doesn't trigger, but the amount of free
180
// memory is still slowly but surely heading towards zero. In this situation,
181
// we start a GC cycle to avoid a potential allocation stall later.
182
183
// Calculate amount of free memory available. Note that we take the
184
// relocation headroom into account to avoid in-place relocation.
185
const size_t soft_max_capacity = ZHeap::heap()->soft_max_capacity();
186
const size_t used = ZHeap::heap()->used();
187
const size_t free_including_headroom = soft_max_capacity - MIN2(soft_max_capacity, used);
188
const size_t free = free_including_headroom - MIN2(free_including_headroom, _relocation_headroom);
189
const double free_percent = percent_of(free, soft_max_capacity);
190
191
log_debug(gc, director)("Rule: High Usage, Free: " SIZE_FORMAT "MB(%.1f%%)",
192
free / M, free_percent);
193
194
return free_percent <= 5.0;
195
}
196
197
GCCause::Cause ZDirector::make_gc_decision() const {
198
// Rule 0: Timer
199
if (rule_timer()) {
200
return GCCause::_z_timer;
201
}
202
203
// Rule 1: Warmup
204
if (rule_warmup()) {
205
return GCCause::_z_warmup;
206
}
207
208
// Rule 2: Allocation rate
209
if (rule_allocation_rate()) {
210
return GCCause::_z_allocation_rate;
211
}
212
213
// Rule 3: Proactive
214
if (rule_proactive()) {
215
return GCCause::_z_proactive;
216
}
217
218
// Rule 4: High usage
219
if (rule_high_usage()) {
220
return GCCause::_z_high_usage;
221
}
222
223
// No GC
224
return GCCause::_no_gc;
225
}
226
227
void ZDirector::run_service() {
228
// Main loop
229
while (_metronome.wait_for_tick()) {
230
sample_allocation_rate();
231
const GCCause::Cause cause = make_gc_decision();
232
if (cause != GCCause::_no_gc) {
233
ZCollectedHeap::heap()->collect(cause);
234
}
235
}
236
}
237
238
void ZDirector::stop_service() {
239
_metronome.stop();
240
}
241
242