OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/heap/memory-reducer.h" | 5 #include "src/heap/memory-reducer.h" |
6 | 6 |
7 #include "src/flags.h" | 7 #include "src/flags.h" |
8 #include "src/heap/gc-tracer.h" | 8 #include "src/heap/gc-tracer.h" |
9 #include "src/heap/heap-inl.h" | 9 #include "src/heap/heap-inl.h" |
10 #include "src/utils.h" | 10 #include "src/utils.h" |
11 #include "src/v8.h" | 11 #include "src/v8.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 const int MemoryReducer::kLongDelayMs = 8000; | 16 const int MemoryReducer::kLongDelayMs = 8000; |
17 const int MemoryReducer::kShortDelayMs = 500; | 17 const int MemoryReducer::kShortDelayMs = 500; |
18 const int MemoryReducer::kWatchdogDelayMs = 100000; | 18 const int MemoryReducer::kWatchdogDelayMs = 100000; |
19 const int MemoryReducer::kMaxNumberOfGCs = 3; | 19 const int MemoryReducer::kMaxNumberOfGCs = 3; |
20 const double MemoryReducer::kCommittedMemoryFactor = 1.1; | |
21 const size_t MemoryReducer::kCommittedMemoryDelta = 10 * MB; | |
22 | 20 |
23 MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer) | 21 MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer) |
24 : CancelableTask(memory_reducer->heap()->isolate()), | 22 : CancelableTask(memory_reducer->heap()->isolate()), |
25 memory_reducer_(memory_reducer) {} | 23 memory_reducer_(memory_reducer) {} |
26 | 24 |
27 | 25 |
28 void MemoryReducer::TimerTask::RunInternal() { | 26 void MemoryReducer::TimerTask::RunInternal() { |
29 const double kJsCallsPerMsThreshold = 0.5; | 27 const double kJsCallsPerMsThreshold = 0.5; |
30 Heap* heap = memory_reducer_->heap(); | 28 Heap* heap = memory_reducer_->heap(); |
31 Event event; | 29 Event event; |
(...skipping 11 matching lines...) Expand all Loading... |
43 } | 41 } |
44 event.type = kTimer; | 42 event.type = kTimer; |
45 event.time_ms = time_ms; | 43 event.time_ms = time_ms; |
46 // The memory reducer will start incremental markig if | 44 // The memory reducer will start incremental markig if |
47 // 1) mutator is likely idle: js call rate is low and allocation rate is low. | 45 // 1) mutator is likely idle: js call rate is low and allocation rate is low. |
48 // 2) mutator is in background: optimize for memory flag is set. | 46 // 2) mutator is in background: optimize for memory flag is set. |
49 event.should_start_incremental_gc = is_idle || optimize_for_memory; | 47 event.should_start_incremental_gc = is_idle || optimize_for_memory; |
50 event.can_start_incremental_gc = | 48 event.can_start_incremental_gc = |
51 heap->incremental_marking()->IsStopped() && | 49 heap->incremental_marking()->IsStopped() && |
52 (heap->incremental_marking()->CanBeActivated() || optimize_for_memory); | 50 (heap->incremental_marking()->CanBeActivated() || optimize_for_memory); |
53 event.committed_memory = heap->CommittedOldGenerationMemory(); | |
54 memory_reducer_->NotifyTimer(event); | 51 memory_reducer_->NotifyTimer(event); |
55 } | 52 } |
56 | 53 |
57 | 54 |
58 double MemoryReducer::SampleAndGetJsCallsPerMs(double time_ms) { | 55 double MemoryReducer::SampleAndGetJsCallsPerMs(double time_ms) { |
59 unsigned int counter = heap()->isolate()->js_calls_from_api_counter(); | 56 unsigned int counter = heap()->isolate()->js_calls_from_api_counter(); |
60 unsigned int call_delta = counter - js_calls_counter_; | 57 unsigned int call_delta = counter - js_calls_counter_; |
61 double time_delta_ms = time_ms - js_calls_sample_time_ms_; | 58 double time_delta_ms = time_ms - js_calls_sample_time_ms_; |
62 js_calls_counter_ = counter; | 59 js_calls_counter_ = counter; |
63 js_calls_sample_time_ms_ = time_ms; | 60 js_calls_sample_time_ms_ = time_ms; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { | 131 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { |
135 return state.last_gc_time_ms != 0 && | 132 return state.last_gc_time_ms != 0 && |
136 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; | 133 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; |
137 } | 134 } |
138 | 135 |
139 | 136 |
140 // For specification of this function see the comment for MemoryReducer class. | 137 // For specification of this function see the comment for MemoryReducer class. |
141 MemoryReducer::State MemoryReducer::Step(const State& state, | 138 MemoryReducer::State MemoryReducer::Step(const State& state, |
142 const Event& event) { | 139 const Event& event) { |
143 if (!FLAG_incremental_marking || !FLAG_memory_reducer) { | 140 if (!FLAG_incremental_marking || !FLAG_memory_reducer) { |
144 return State(kDone, 0, 0, state.last_gc_time_ms, 0); | 141 return State(kDone, 0, 0, state.last_gc_time_ms); |
145 } | 142 } |
146 switch (state.action) { | 143 switch (state.action) { |
147 case kDone: | 144 case kDone: |
148 if (event.type == kTimer) { | 145 if (event.type == kTimer) { |
149 return state; | 146 return state; |
150 } else if (event.type == kMarkCompact) { | |
151 if (event.committed_memory < | |
152 Max(static_cast<size_t>(state.committed_memory_at_last_run * | |
153 kCommittedMemoryFactor), | |
154 state.committed_memory_at_last_run + kCommittedMemoryDelta)) { | |
155 return state; | |
156 } else { | |
157 return State(kWait, 0, event.time_ms + kLongDelayMs, | |
158 event.type == kMarkCompact ? event.time_ms | |
159 : state.last_gc_time_ms, | |
160 0); | |
161 } | |
162 } else { | 147 } else { |
163 DCHECK_EQ(kPossibleGarbage, event.type); | 148 DCHECK(event.type == kPossibleGarbage || event.type == kMarkCompact); |
164 return State( | 149 return State( |
165 kWait, 0, event.time_ms + kLongDelayMs, | 150 kWait, 0, event.time_ms + kLongDelayMs, |
166 event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms, | 151 event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms); |
167 0); | |
168 } | 152 } |
169 case kWait: | 153 case kWait: |
170 switch (event.type) { | 154 switch (event.type) { |
171 case kPossibleGarbage: | 155 case kPossibleGarbage: |
172 return state; | 156 return state; |
173 case kTimer: | 157 case kTimer: |
174 if (state.started_gcs >= kMaxNumberOfGCs) { | 158 if (state.started_gcs >= kMaxNumberOfGCs) { |
175 return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms, | 159 return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms); |
176 event.committed_memory); | |
177 } else if (event.can_start_incremental_gc && | 160 } else if (event.can_start_incremental_gc && |
178 (event.should_start_incremental_gc || | 161 (event.should_start_incremental_gc || |
179 WatchdogGC(state, event))) { | 162 WatchdogGC(state, event))) { |
180 if (state.next_gc_start_ms <= event.time_ms) { | 163 if (state.next_gc_start_ms <= event.time_ms) { |
181 return State(kRun, state.started_gcs + 1, 0.0, | 164 return State(kRun, state.started_gcs + 1, 0.0, |
182 state.last_gc_time_ms, 0); | 165 state.last_gc_time_ms); |
183 } else { | 166 } else { |
184 return state; | 167 return state; |
185 } | 168 } |
186 } else { | 169 } else { |
187 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, | 170 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
188 state.last_gc_time_ms, 0); | 171 state.last_gc_time_ms); |
189 } | 172 } |
190 case kMarkCompact: | 173 case kMarkCompact: |
191 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, | 174 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
192 event.time_ms, 0); | 175 event.time_ms); |
193 } | 176 } |
194 case kRun: | 177 case kRun: |
195 if (event.type != kMarkCompact) { | 178 if (event.type != kMarkCompact) { |
196 return state; | 179 return state; |
197 } else { | 180 } else { |
198 if (state.started_gcs < kMaxNumberOfGCs && | 181 if (state.started_gcs < kMaxNumberOfGCs && |
199 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { | 182 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { |
200 return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs, | 183 return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs, |
201 event.time_ms, 0); | 184 event.time_ms); |
202 } else { | 185 } else { |
203 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms, | 186 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms); |
204 event.committed_memory); | |
205 } | 187 } |
206 } | 188 } |
207 } | 189 } |
208 UNREACHABLE(); | 190 UNREACHABLE(); |
209 return State(kDone, 0, 0, 0.0, 0); // Make the compiler happy. | 191 return State(kDone, 0, 0, 0.0); // Make the compiler happy. |
210 } | 192 } |
211 | 193 |
212 | 194 |
213 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { | 195 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { |
214 DCHECK(delay_ms > 0); | 196 DCHECK(delay_ms > 0); |
215 // Record the time and the js call counter. | 197 // Record the time and the js call counter. |
216 SampleAndGetJsCallsPerMs(time_ms); | 198 SampleAndGetJsCallsPerMs(time_ms); |
217 // Leave some room for precision error in task scheduler. | 199 // Leave some room for precision error in task scheduler. |
218 const double kSlackMs = 100; | 200 const double kSlackMs = 100; |
219 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); | 201 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
220 auto timer_task = new MemoryReducer::TimerTask(this); | 202 auto timer_task = new MemoryReducer::TimerTask(this); |
221 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 203 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
222 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); | 204 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); |
223 } | 205 } |
224 | 206 |
225 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0, 0); } | 207 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } |
226 | 208 |
227 } // namespace internal | 209 } // namespace internal |
228 } // namespace v8 | 210 } // namespace v8 |
OLD | NEW |