1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_HEAP_GC_IDLE_TIME_HANDLER_H_
#define V8_HEAP_GC_IDLE_TIME_HANDLER_H_
#include "src/globals.h"
namespace v8 {
namespace internal {
enum GCIdleTimeActionType {
DONE,
DO_NOTHING,
DO_INCREMENTAL_MARKING,
DO_SCAVENGE,
DO_FULL_GC,
DO_FINALIZE_SWEEPING
};
class GCIdleTimeAction {
public:
static GCIdleTimeAction Done() {
GCIdleTimeAction result;
result.type = DONE;
result.parameter = 0;
result.additional_work = false;
result.reduce_memory = false;
return result;
}
static GCIdleTimeAction Nothing() {
GCIdleTimeAction result;
result.type = DO_NOTHING;
result.parameter = 0;
result.additional_work = false;
result.reduce_memory = false;
return result;
}
static GCIdleTimeAction IncrementalMarking(intptr_t step_size,
bool reduce_memory) {
GCIdleTimeAction result;
result.type = DO_INCREMENTAL_MARKING;
result.parameter = step_size;
result.additional_work = false;
result.reduce_memory = reduce_memory;
return result;
}
static GCIdleTimeAction Scavenge() {
GCIdleTimeAction result;
result.type = DO_SCAVENGE;
result.parameter = 0;
result.additional_work = false;
// TODO(ulan): add reduce_memory argument and shrink new space size if
// reduce_memory = true.
result.reduce_memory = false;
return result;
}
static GCIdleTimeAction FullGC(bool reduce_memory) {
GCIdleTimeAction result;
result.type = DO_FULL_GC;
result.parameter = 0;
result.additional_work = false;
result.reduce_memory = reduce_memory;
return result;
}
static GCIdleTimeAction FinalizeSweeping() {
GCIdleTimeAction result;
result.type = DO_FINALIZE_SWEEPING;
result.parameter = 0;
result.additional_work = false;
result.reduce_memory = false;
return result;
}
void Print();
GCIdleTimeActionType type;
intptr_t parameter;
bool additional_work;
bool reduce_memory;
};
class GCTracer;
// The idle time handler makes decisions about which garbage collection
// operations are executing during IdleNotification.
class GCIdleTimeHandler {
public:
// If we haven't recorded any incremental marking events yet, we carefully
// mark with a conservative lower bound for the marking speed.
static const size_t kInitialConservativeMarkingSpeed = 100 * KB;
// Maximum marking step size returned by EstimateMarkingStepSize.
static const size_t kMaximumMarkingStepSize = 700 * MB;
// We have to make sure that we finish the IdleNotification before
// idle_time_in_ms. Hence, we conservatively prune our workload estimate.
static const double kConservativeTimeRatio;
// If we haven't recorded any mark-compact events yet, we use
// conservative lower bound for the mark-compact speed.
static const size_t kInitialConservativeMarkCompactSpeed = 2 * MB;
// If we haven't recorded any final incremental mark-compact events yet, we
// use conservative lower bound for the mark-compact speed.
static const size_t kInitialConservativeFinalIncrementalMarkCompactSpeed =
2 * MB;
// Maximum mark-compact time returned by EstimateMarkCompactTime.
static const size_t kMaxMarkCompactTimeInMs;
// Maximum final incremental mark-compact time returned by
// EstimateFinalIncrementalMarkCompactTime.
static const size_t kMaxFinalIncrementalMarkCompactTimeInMs;
// This is the maximum scheduled idle time. Note that it can be more than
// 16.66 ms when there is currently no rendering going on.
static const size_t kMaxScheduledIdleTime = 50;
// The maximum idle time when frames are rendered is 16.66ms.
static const size_t kMaxFrameRenderingIdleTime = 17;
// We conservatively assume that in the next kTimeUntilNextIdleEvent ms
// no idle notification happens.
static const size_t kTimeUntilNextIdleEvent = 100;
// If we haven't recorded any scavenger events yet, we use a conservative
// lower bound for the scavenger speed.
static const size_t kInitialConservativeScavengeSpeed = 100 * KB;
// If contexts are disposed at a higher rate a full gc is triggered.
static const double kHighContextDisposalRate;
// Incremental marking step time.
static const size_t kIncrementalMarkingStepTimeInMs = 1;
static const size_t kMinTimeForOverApproximatingWeakClosureInMs;
// The number of idle MarkCompact GCs to perform before transitioning to
// the kDone mode.
static const int kMaxIdleMarkCompacts = 3;
// The number of mutator MarkCompact GCs before transitioning to the
// kReduceLatency mode.
static const int kMarkCompactsBeforeMutatorIsActive = 1;
// Mutator is considered idle if
// 1) there are N idle notification with time >= kMinBackgroundIdleTime,
// 2) or there are M idle notifications with time >= kMinLongIdleTime
// without any mutator GC in between.
// Where N = kBackgroundIdleNotificationsBeforeMutatorIsIdle,
// M = kLongIdleNotificationsBeforeMutatorIsIdle
static const int kMinLongIdleTime = kMaxFrameRenderingIdleTime + 1;
static const int kMinBackgroundIdleTime = 900;
static const int kBackgroundIdleNotificationsBeforeMutatorIsIdle = 2;
static const int kLongIdleNotificationsBeforeMutatorIsIdle = 50;
// Number of times we will return a Nothing action in the current mode
// despite having idle time available before we returning a Done action to
// ensure we don't keep scheduling idle tasks and making no progress.
static const int kMaxNoProgressIdleTimesPerMode = 10;
class HeapState {
public:
void Print();
int contexts_disposed;
double contexts_disposal_rate;
size_t size_of_objects;
bool incremental_marking_stopped;
bool can_start_incremental_marking;
bool sweeping_in_progress;
bool sweeping_completed;
size_t mark_compact_speed_in_bytes_per_ms;
size_t incremental_marking_speed_in_bytes_per_ms;
size_t final_incremental_mark_compact_speed_in_bytes_per_ms;
size_t scavenge_speed_in_bytes_per_ms;
size_t used_new_space_size;
size_t new_space_capacity;
size_t new_space_allocation_throughput_in_bytes_per_ms;
};
GCIdleTimeHandler()
: idle_mark_compacts_(0),
mark_compacts_(0),
scavenges_(0),
long_idle_notifications_(0),
background_idle_notifications_(0),
idle_times_which_made_no_progress_per_mode_(0),
mode_(kReduceLatency) {}
GCIdleTimeAction Compute(double idle_time_in_ms, HeapState heap_state);
void NotifyIdleMarkCompact() { ++idle_mark_compacts_; }
void NotifyMarkCompact() { ++mark_compacts_; }
void NotifyScavenge() { ++scavenges_; }
static size_t EstimateMarkingStepSize(size_t idle_time_in_ms,
size_t marking_speed_in_bytes_per_ms);
static size_t EstimateMarkCompactTime(
size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms);
static size_t EstimateFinalIncrementalMarkCompactTime(
size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms);
static bool ShouldDoMarkCompact(size_t idle_time_in_ms,
size_t size_of_objects,
size_t mark_compact_speed_in_bytes_per_ms);
static bool ShouldDoContextDisposalMarkCompact(int context_disposed,
double contexts_disposal_rate);
static bool ShouldDoFinalIncrementalMarkCompact(
size_t idle_time_in_ms, size_t size_of_objects,
size_t final_incremental_mark_compact_speed_in_bytes_per_ms);
static bool ShouldDoOverApproximateWeakClosure(size_t idle_time_in_ms);
static bool ShouldDoScavenge(
size_t idle_time_in_ms, size_t new_space_size, size_t used_new_space_size,
size_t scavenger_speed_in_bytes_per_ms,
size_t new_space_allocation_throughput_in_bytes_per_ms);
enum Mode { kReduceLatency, kReduceMemory, kDone };
Mode mode() { return mode_; }
private:
bool IsMutatorActive(int contexts_disposed, int gcs);
bool IsMutatorIdle(int long_idle_notifications,
int background_idle_notifications, int gcs);
void UpdateCounters(double idle_time_in_ms);
void ResetCounters();
Mode NextMode(const HeapState& heap_state);
GCIdleTimeAction Action(double idle_time_in_ms, const HeapState& heap_state,
bool reduce_memory);
GCIdleTimeAction NothingOrDone();
int idle_mark_compacts_;
int mark_compacts_;
int scavenges_;
// The number of long idle notifications with no GC happening
// between the notifications.
int long_idle_notifications_;
// The number of background idle notifications with no GC happening
// between the notifications.
int background_idle_notifications_;
// Idle notifications with no progress in the current mode.
int idle_times_which_made_no_progress_per_mode_;
Mode mode_;
DISALLOW_COPY_AND_ASSIGN(GCIdleTimeHandler);
};
} // namespace internal
} // namespace v8
#endif // V8_HEAP_GC_IDLE_TIME_HANDLER_H_
|