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 | 19 |
20 void MemoryReducer::TimerTask::Run() { | 20 void MemoryReducer::TimerTask::Run() { |
| 21 if (heap_is_torn_down_) return; |
21 Heap* heap = memory_reducer_->heap(); | 22 Heap* heap = memory_reducer_->heap(); |
22 Event event; | 23 Event event; |
23 event.type = kTimer; | 24 event.type = kTimer; |
24 event.time_ms = heap->MonotonicallyIncreasingTimeInMs(); | 25 event.time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
25 event.low_allocation_rate = heap->HasLowAllocationRate(); | 26 event.low_allocation_rate = heap->HasLowAllocationRate(); |
26 event.can_start_incremental_gc = | 27 event.can_start_incremental_gc = |
27 heap->incremental_marking()->IsStopped() && | 28 heap->incremental_marking()->IsStopped() && |
28 heap->incremental_marking()->CanBeActivated(); | 29 heap->incremental_marking()->CanBeActivated(); |
29 memory_reducer_->NotifyTimer(event); | 30 memory_reducer_->NotifyTimer(event); |
30 } | 31 } |
31 | 32 |
32 | 33 |
33 void MemoryReducer::NotifyTimer(const Event& event) { | 34 void MemoryReducer::NotifyTimer(const Event& event) { |
| 35 DCHECK(nullptr != pending_task_); |
34 DCHECK_EQ(kTimer, event.type); | 36 DCHECK_EQ(kTimer, event.type); |
35 DCHECK_EQ(kWait, state_.action); | 37 DCHECK_EQ(kWait, state_.action); |
| 38 pending_task_ = nullptr; |
36 state_ = Step(state_, event); | 39 state_ = Step(state_, event); |
37 if (state_.action == kRun) { | 40 if (state_.action == kRun) { |
38 DCHECK(heap()->incremental_marking()->IsStopped()); | 41 DCHECK(heap()->incremental_marking()->IsStopped()); |
39 DCHECK(FLAG_incremental_marking); | 42 DCHECK(FLAG_incremental_marking); |
40 heap()->StartIdleIncrementalMarking(); | 43 heap()->StartIdleIncrementalMarking(); |
41 if (FLAG_trace_gc_verbose) { | 44 if (FLAG_trace_gc_verbose) { |
42 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", | 45 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", |
43 state_.started_gcs); | 46 state_.started_gcs); |
44 } | 47 } |
45 } else if (state_.action == kWait) { | 48 } else if (state_.action == kWait) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 UNREACHABLE(); | 163 UNREACHABLE(); |
161 return State(kDone, 0, 0); // Make the compiler happy. | 164 return State(kDone, 0, 0); // Make the compiler happy. |
162 } | 165 } |
163 | 166 |
164 | 167 |
165 void MemoryReducer::ScheduleTimer(double delay_ms) { | 168 void MemoryReducer::ScheduleTimer(double delay_ms) { |
166 DCHECK(delay_ms > 0); | 169 DCHECK(delay_ms > 0); |
167 // Leave some room for precision error in task scheduler. | 170 // Leave some room for precision error in task scheduler. |
168 const double kSlackMs = 100; | 171 const double kSlackMs = 100; |
169 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); | 172 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
| 173 DCHECK(nullptr == pending_task_); |
| 174 pending_task_ = new MemoryReducer::TimerTask(this); |
170 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 175 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
171 isolate, new MemoryReducer::TimerTask(this), | 176 isolate, pending_task_, (delay_ms + kSlackMs) / 1000.0); |
172 (delay_ms + kSlackMs) / 1000.0); | 177 } |
| 178 |
| 179 |
| 180 void MemoryReducer::TearDown() { |
| 181 if (pending_task_ != nullptr) { |
| 182 pending_task_->NotifyHeapTearDown(); |
| 183 pending_task_ = nullptr; |
| 184 } |
| 185 state_ = State(kDone, 0, 0); |
173 } | 186 } |
174 | 187 |
175 } // internal | 188 } // internal |
176 } // v8 | 189 } // v8 |
OLD | NEW |