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