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/heap.h" | 8 #include "src/heap/heap.h" |
9 #include "src/utils.h" | 9 #include "src/utils.h" |
10 #include "src/v8.h" | 10 #include "src/v8.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 const int MemoryReducer::kLongDelayMs = 5000; | 15 const int MemoryReducer::kLongDelayMs = 5000; |
16 const int MemoryReducer::kShortDelayMs = 500; | 16 const int MemoryReducer::kShortDelayMs = 500; |
17 const int MemoryReducer::kMaxNumberOfGCs = 3; | 17 const int MemoryReducer::kMaxNumberOfGCs = 3; |
18 | 18 |
| 19 MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer) |
| 20 : CancelableTask(memory_reducer->heap()->isolate()), |
| 21 memory_reducer_(memory_reducer) {} |
19 | 22 |
20 void MemoryReducer::TimerTask::Run() { | 23 |
21 if (heap_is_torn_down_) return; | 24 void MemoryReducer::TimerTask::RunInternal() { |
22 Heap* heap = memory_reducer_->heap(); | 25 Heap* heap = memory_reducer_->heap(); |
23 Event event; | 26 Event event; |
24 event.type = kTimer; | 27 event.type = kTimer; |
25 event.time_ms = heap->MonotonicallyIncreasingTimeInMs(); | 28 event.time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
26 event.low_allocation_rate = heap->HasLowAllocationRate(); | 29 event.low_allocation_rate = heap->HasLowAllocationRate(); |
27 event.can_start_incremental_gc = | 30 event.can_start_incremental_gc = |
28 heap->incremental_marking()->IsStopped() && | 31 heap->incremental_marking()->IsStopped() && |
29 heap->incremental_marking()->CanBeActivated(); | 32 heap->incremental_marking()->CanBeActivated(); |
30 memory_reducer_->NotifyTimer(event); | 33 memory_reducer_->NotifyTimer(event); |
31 } | 34 } |
32 | 35 |
33 | 36 |
34 void MemoryReducer::NotifyTimer(const Event& event) { | 37 void MemoryReducer::NotifyTimer(const Event& event) { |
35 DCHECK(nullptr != pending_task_); | 38 DCHECK(nullptr != pending_task_); |
36 DCHECK_EQ(kTimer, event.type); | 39 DCHECK_EQ(kTimer, event.type); |
37 DCHECK_EQ(kWait, state_.action); | 40 DCHECK_EQ(kWait, state_.action); |
38 pending_task_ = nullptr; | |
39 state_ = Step(state_, event); | 41 state_ = Step(state_, event); |
40 if (state_.action == kRun) { | 42 if (state_.action == kRun) { |
41 DCHECK(heap()->incremental_marking()->IsStopped()); | 43 DCHECK(heap()->incremental_marking()->IsStopped()); |
42 DCHECK(FLAG_incremental_marking); | 44 DCHECK(FLAG_incremental_marking); |
43 heap()->StartIdleIncrementalMarking(); | 45 heap()->StartIdleIncrementalMarking(); |
44 if (FLAG_trace_gc_verbose) { | 46 if (FLAG_trace_gc_verbose) { |
45 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", | 47 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", |
46 state_.started_gcs); | 48 state_.started_gcs); |
47 } | 49 } |
48 } else if (state_.action == kWait) { | 50 } else if (state_.action == kWait) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 return State(kDone, 0, 0); // Make the compiler happy. | 166 return State(kDone, 0, 0); // Make the compiler happy. |
165 } | 167 } |
166 | 168 |
167 | 169 |
168 void MemoryReducer::ScheduleTimer(double delay_ms) { | 170 void MemoryReducer::ScheduleTimer(double delay_ms) { |
169 DCHECK(delay_ms > 0); | 171 DCHECK(delay_ms > 0); |
170 // Leave some room for precision error in task scheduler. | 172 // Leave some room for precision error in task scheduler. |
171 const double kSlackMs = 100; | 173 const double kSlackMs = 100; |
172 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); | 174 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
173 DCHECK(nullptr == pending_task_); | 175 DCHECK(nullptr == pending_task_); |
174 pending_task_ = new MemoryReducer::TimerTask(this); | 176 auto timer_task = new MemoryReducer::TimerTask(this); |
175 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 177 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
176 isolate, pending_task_, (delay_ms + kSlackMs) / 1000.0); | 178 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); |
177 } | |
178 | |
179 | |
180 void MemoryReducer::ClearTask(v8::Task* task) { | |
181 if (pending_task_ == task) { | |
182 pending_task_ = nullptr; | |
183 } | |
184 } | 179 } |
185 | 180 |
186 | 181 |
187 void MemoryReducer::TearDown() { | 182 void MemoryReducer::TearDown() { |
188 if (pending_task_ != nullptr) { | |
189 pending_task_->NotifyHeapTearDown(); | |
190 pending_task_ = nullptr; | |
191 } | |
192 state_ = State(kDone, 0, 0); | 183 state_ = State(kDone, 0, 0); |
193 } | 184 } |
194 | 185 |
195 } // internal | 186 } // internal |
196 } // v8 | 187 } // v8 |
OLD | NEW |