| 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 | 20 |
| 21 MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer) | 21 MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer) |
| 22 : CancelableTask(memory_reducer->heap()->isolate()), | 22 : CancelableTask(memory_reducer->heap()->isolate()), |
| 23 memory_reducer_(memory_reducer) {} | 23 memory_reducer_(memory_reducer) {} |
| 24 | 24 |
| 25 | 25 |
| 26 void MemoryReducer::TimerTask::RunInternal() { | 26 void MemoryReducer::TimerTask::RunInternal() { |
| 27 const double kJsCallsPerMsThreshold = 0.5; | |
| 28 Heap* heap = memory_reducer_->heap(); | 27 Heap* heap = memory_reducer_->heap(); |
| 29 Event event; | 28 Event event; |
| 30 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); | 29 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
| 31 heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), | 30 heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), |
| 32 heap->OldGenerationAllocationCounter()); | 31 heap->OldGenerationAllocationCounter()); |
| 33 double js_call_rate = memory_reducer_->SampleAndGetJsCallsPerMs(time_ms); | |
| 34 bool low_allocation_rate = heap->HasLowAllocationRate(); | 32 bool low_allocation_rate = heap->HasLowAllocationRate(); |
| 35 bool is_idle = js_call_rate < kJsCallsPerMsThreshold && low_allocation_rate; | |
| 36 bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage(); | 33 bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage(); |
| 37 if (FLAG_trace_gc_verbose) { | 34 if (FLAG_trace_gc_verbose) { |
| 38 heap->isolate()->PrintWithTimestamp( | 35 heap->isolate()->PrintWithTimestamp( |
| 39 "Memory reducer: call rate %.3lf, %s, %s\n", js_call_rate, | 36 "Memory reducer: %s, %s\n", |
| 40 low_allocation_rate ? "low alloc" : "high alloc", | 37 low_allocation_rate ? "low alloc" : "high alloc", |
| 41 optimize_for_memory ? "background" : "foreground"); | 38 optimize_for_memory ? "background" : "foreground"); |
| 42 } | 39 } |
| 43 event.type = kTimer; | 40 event.type = kTimer; |
| 44 event.time_ms = time_ms; | 41 event.time_ms = time_ms; |
| 45 // The memory reducer will start incremental markig if | 42 // The memory reducer will start incremental markig if |
| 46 // 1) mutator is likely idle: js call rate is low and allocation rate is low. | 43 // 1) mutator is likely idle: js call rate is low and allocation rate is low. |
| 47 // 2) mutator is in background: optimize for memory flag is set. | 44 // 2) mutator is in background: optimize for memory flag is set. |
| 48 event.should_start_incremental_gc = is_idle || optimize_for_memory; | 45 event.should_start_incremental_gc = |
| 46 low_allocation_rate || optimize_for_memory; |
| 49 event.can_start_incremental_gc = | 47 event.can_start_incremental_gc = |
| 50 heap->incremental_marking()->IsStopped() && | 48 heap->incremental_marking()->IsStopped() && |
| 51 (heap->incremental_marking()->CanBeActivated() || optimize_for_memory); | 49 (heap->incremental_marking()->CanBeActivated() || optimize_for_memory); |
| 52 memory_reducer_->NotifyTimer(event); | 50 memory_reducer_->NotifyTimer(event); |
| 53 } | 51 } |
| 54 | 52 |
| 55 | 53 |
| 56 double MemoryReducer::SampleAndGetJsCallsPerMs(double time_ms) { | |
| 57 unsigned int counter = heap()->isolate()->js_calls_from_api_counter(); | |
| 58 unsigned int call_delta = counter - js_calls_counter_; | |
| 59 double time_delta_ms = time_ms - js_calls_sample_time_ms_; | |
| 60 js_calls_counter_ = counter; | |
| 61 js_calls_sample_time_ms_ = time_ms; | |
| 62 return time_delta_ms > 0 ? call_delta / time_delta_ms : 0; | |
| 63 } | |
| 64 | |
| 65 | |
| 66 void MemoryReducer::NotifyTimer(const Event& event) { | 54 void MemoryReducer::NotifyTimer(const Event& event) { |
| 67 DCHECK_EQ(kTimer, event.type); | 55 DCHECK_EQ(kTimer, event.type); |
| 68 DCHECK_EQ(kWait, state_.action); | 56 DCHECK_EQ(kWait, state_.action); |
| 69 state_ = Step(state_, event); | 57 state_ = Step(state_, event); |
| 70 if (state_.action == kRun) { | 58 if (state_.action == kRun) { |
| 71 DCHECK(heap()->incremental_marking()->IsStopped()); | 59 DCHECK(heap()->incremental_marking()->IsStopped()); |
| 72 DCHECK(FLAG_incremental_marking); | 60 DCHECK(FLAG_incremental_marking); |
| 73 if (FLAG_trace_gc_verbose) { | 61 if (FLAG_trace_gc_verbose) { |
| 74 heap()->isolate()->PrintWithTimestamp("Memory reducer: started GC #%d\n", | 62 heap()->isolate()->PrintWithTimestamp("Memory reducer: started GC #%d\n", |
| 75 state_.started_gcs); | 63 state_.started_gcs); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 177 } |
| 190 } | 178 } |
| 191 } | 179 } |
| 192 UNREACHABLE(); | 180 UNREACHABLE(); |
| 193 return State(kDone, 0, 0, 0.0); // Make the compiler happy. | 181 return State(kDone, 0, 0, 0.0); // Make the compiler happy. |
| 194 } | 182 } |
| 195 | 183 |
| 196 | 184 |
| 197 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { | 185 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { |
| 198 DCHECK(delay_ms > 0); | 186 DCHECK(delay_ms > 0); |
| 199 // Record the time and the js call counter. | |
| 200 SampleAndGetJsCallsPerMs(time_ms); | |
| 201 // Leave some room for precision error in task scheduler. | 187 // Leave some room for precision error in task scheduler. |
| 202 const double kSlackMs = 100; | 188 const double kSlackMs = 100; |
| 203 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); | 189 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
| 204 auto timer_task = new MemoryReducer::TimerTask(this); | 190 auto timer_task = new MemoryReducer::TimerTask(this); |
| 205 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 191 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
| 206 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); | 192 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); |
| 207 } | 193 } |
| 208 | 194 |
| 209 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } | 195 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } |
| 210 | 196 |
| 211 } // namespace internal | 197 } // namespace internal |
| 212 } // namespace v8 | 198 } // namespace v8 |
| OLD | NEW |