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 Heap* heap = memory_reducer_->heap(); | 29 Heap* heap = memory_reducer_->heap(); |
28 Event event; | 30 Event event; |
29 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); | 31 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
(...skipping 10 matching lines...) Expand all Loading... |
40 event.type = kTimer; | 42 event.type = kTimer; |
41 event.time_ms = time_ms; | 43 event.time_ms = time_ms; |
42 // The memory reducer will start incremental markig if | 44 // The memory reducer will start incremental markig if |
43 // 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. |
44 // 2) mutator is in background: optimize for memory flag is set. | 46 // 2) mutator is in background: optimize for memory flag is set. |
45 event.should_start_incremental_gc = | 47 event.should_start_incremental_gc = |
46 low_allocation_rate || optimize_for_memory; | 48 low_allocation_rate || optimize_for_memory; |
47 event.can_start_incremental_gc = | 49 event.can_start_incremental_gc = |
48 heap->incremental_marking()->IsStopped() && | 50 heap->incremental_marking()->IsStopped() && |
49 (heap->incremental_marking()->CanBeActivated() || optimize_for_memory); | 51 (heap->incremental_marking()->CanBeActivated() || optimize_for_memory); |
| 52 event.committed_memory = heap->CommittedOldGenerationMemory(); |
50 memory_reducer_->NotifyTimer(event); | 53 memory_reducer_->NotifyTimer(event); |
51 } | 54 } |
52 | 55 |
53 | 56 |
54 void MemoryReducer::NotifyTimer(const Event& event) { | 57 void MemoryReducer::NotifyTimer(const Event& event) { |
55 DCHECK_EQ(kTimer, event.type); | 58 DCHECK_EQ(kTimer, event.type); |
56 DCHECK_EQ(kWait, state_.action); | 59 DCHECK_EQ(kWait, state_.action); |
57 state_ = Step(state_, event); | 60 state_ = Step(state_, event); |
58 if (state_.action == kRun) { | 61 if (state_.action == kRun) { |
59 DCHECK(heap()->incremental_marking()->IsStopped()); | 62 DCHECK(heap()->incremental_marking()->IsStopped()); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { | 124 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { |
122 return state.last_gc_time_ms != 0 && | 125 return state.last_gc_time_ms != 0 && |
123 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; | 126 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; |
124 } | 127 } |
125 | 128 |
126 | 129 |
127 // For specification of this function see the comment for MemoryReducer class. | 130 // For specification of this function see the comment for MemoryReducer class. |
128 MemoryReducer::State MemoryReducer::Step(const State& state, | 131 MemoryReducer::State MemoryReducer::Step(const State& state, |
129 const Event& event) { | 132 const Event& event) { |
130 if (!FLAG_incremental_marking || !FLAG_memory_reducer) { | 133 if (!FLAG_incremental_marking || !FLAG_memory_reducer) { |
131 return State(kDone, 0, 0, state.last_gc_time_ms); | 134 return State(kDone, 0, 0, state.last_gc_time_ms, 0); |
132 } | 135 } |
133 switch (state.action) { | 136 switch (state.action) { |
134 case kDone: | 137 case kDone: |
135 if (event.type == kTimer) { | 138 if (event.type == kTimer) { |
136 return state; | 139 return state; |
| 140 } else if (event.type == kMarkCompact) { |
| 141 if (event.committed_memory < |
| 142 Max(static_cast<size_t>(state.committed_memory_at_last_run * |
| 143 kCommittedMemoryFactor), |
| 144 state.committed_memory_at_last_run + kCommittedMemoryDelta)) { |
| 145 return state; |
| 146 } else { |
| 147 return State(kWait, 0, event.time_ms + kLongDelayMs, |
| 148 event.type == kMarkCompact ? event.time_ms |
| 149 : state.last_gc_time_ms, |
| 150 0); |
| 151 } |
137 } else { | 152 } else { |
138 DCHECK(event.type == kPossibleGarbage || event.type == kMarkCompact); | 153 DCHECK_EQ(kPossibleGarbage, event.type); |
139 return State( | 154 return State( |
140 kWait, 0, event.time_ms + kLongDelayMs, | 155 kWait, 0, event.time_ms + kLongDelayMs, |
141 event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms); | 156 event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms, |
| 157 0); |
142 } | 158 } |
143 case kWait: | 159 case kWait: |
144 switch (event.type) { | 160 switch (event.type) { |
145 case kPossibleGarbage: | 161 case kPossibleGarbage: |
146 return state; | 162 return state; |
147 case kTimer: | 163 case kTimer: |
148 if (state.started_gcs >= kMaxNumberOfGCs) { | 164 if (state.started_gcs >= kMaxNumberOfGCs) { |
149 return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms); | 165 return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms, |
| 166 event.committed_memory); |
150 } else if (event.can_start_incremental_gc && | 167 } else if (event.can_start_incremental_gc && |
151 (event.should_start_incremental_gc || | 168 (event.should_start_incremental_gc || |
152 WatchdogGC(state, event))) { | 169 WatchdogGC(state, event))) { |
153 if (state.next_gc_start_ms <= event.time_ms) { | 170 if (state.next_gc_start_ms <= event.time_ms) { |
154 return State(kRun, state.started_gcs + 1, 0.0, | 171 return State(kRun, state.started_gcs + 1, 0.0, |
155 state.last_gc_time_ms); | 172 state.last_gc_time_ms, 0); |
156 } else { | 173 } else { |
157 return state; | 174 return state; |
158 } | 175 } |
159 } else { | 176 } else { |
160 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, | 177 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
161 state.last_gc_time_ms); | 178 state.last_gc_time_ms, 0); |
162 } | 179 } |
163 case kMarkCompact: | 180 case kMarkCompact: |
164 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, | 181 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
165 event.time_ms); | 182 event.time_ms, 0); |
166 } | 183 } |
167 case kRun: | 184 case kRun: |
168 if (event.type != kMarkCompact) { | 185 if (event.type != kMarkCompact) { |
169 return state; | 186 return state; |
170 } else { | 187 } else { |
171 if (state.started_gcs < kMaxNumberOfGCs && | 188 if (state.started_gcs < kMaxNumberOfGCs && |
172 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { | 189 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { |
173 return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs, | 190 return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs, |
174 event.time_ms); | 191 event.time_ms, 0); |
175 } else { | 192 } else { |
176 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms); | 193 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms, |
| 194 event.committed_memory); |
177 } | 195 } |
178 } | 196 } |
179 } | 197 } |
180 UNREACHABLE(); | 198 UNREACHABLE(); |
181 return State(kDone, 0, 0, 0.0); // Make the compiler happy. | 199 return State(kDone, 0, 0, 0.0, 0); // Make the compiler happy. |
182 } | 200 } |
183 | 201 |
184 | 202 |
185 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { | 203 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { |
186 DCHECK(delay_ms > 0); | 204 DCHECK(delay_ms > 0); |
187 // Leave some room for precision error in task scheduler. | 205 // Leave some room for precision error in task scheduler. |
188 const double kSlackMs = 100; | 206 const double kSlackMs = 100; |
189 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); | 207 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
190 auto timer_task = new MemoryReducer::TimerTask(this); | 208 auto timer_task = new MemoryReducer::TimerTask(this); |
191 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 209 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
192 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); | 210 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); |
193 } | 211 } |
194 | 212 |
195 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } | 213 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0, 0); } |
196 | 214 |
197 } // namespace internal | 215 } // namespace internal |
198 } // namespace v8 | 216 } // namespace v8 |
OLD | NEW |