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 = 20000; | 16 const int MemoryReducer::kLongDelayMs = 20000; |
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() { |
Hannes Payer (out of office)
2015/10/28 14:58:08
Can we leave a comment somewhere mentioning under
ulan
2015/10/29 12:39:24
Done.
| |
27 const double kJsCallsPerMsThreshold = 0.25; | |
27 Heap* heap = memory_reducer_->heap(); | 28 Heap* heap = memory_reducer_->heap(); |
28 Event event; | 29 Event event; |
29 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); | 30 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
30 heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), | 31 heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), |
31 heap->OldGenerationAllocationCounter()); | 32 heap->OldGenerationAllocationCounter()); |
33 double js_call_rate = memory_reducer_->SampleAndGetJsCallsPerMs(time_ms); | |
34 bool is_idle = | |
35 js_call_rate < kJsCallsPerMsThreshold && heap->HasLowAllocationRate(); | |
32 event.type = kTimer; | 36 event.type = kTimer; |
33 event.time_ms = time_ms; | 37 event.time_ms = time_ms; |
34 event.should_start_incremental_gc = | 38 event.should_start_incremental_gc = |
35 heap->HasLowAllocationRate() || heap->ShouldOptimizeForMemoryUsage(); | 39 is_idle || heap->ShouldOptimizeForMemoryUsage(); |
36 event.can_start_incremental_gc = | 40 event.can_start_incremental_gc = |
37 heap->incremental_marking()->IsStopped() && | 41 heap->incremental_marking()->IsStopped() && |
38 heap->incremental_marking()->CanBeActivated(); | 42 heap->incremental_marking()->CanBeActivated(); |
39 memory_reducer_->NotifyTimer(event); | 43 memory_reducer_->NotifyTimer(event); |
40 } | 44 } |
41 | 45 |
42 | 46 |
47 double MemoryReducer::SampleAndGetJsCallsPerMs(double time_ms) { | |
48 unsigned int counter = heap()->isolate()->js_calls_from_api_counter(); | |
49 unsigned int call_delta = counter - js_calls_counter_; | |
50 double time_delta_ms = time_ms - js_calls_sample_time_ms_; | |
51 js_calls_counter_ = counter; | |
52 js_calls_sample_time_ms_ = time_ms; | |
53 return time_delta_ms > 0 ? call_delta / time_delta_ms : 0; | |
54 } | |
55 | |
56 | |
43 void MemoryReducer::NotifyTimer(const Event& event) { | 57 void MemoryReducer::NotifyTimer(const Event& event) { |
44 DCHECK_EQ(kTimer, event.type); | 58 DCHECK_EQ(kTimer, event.type); |
45 DCHECK_EQ(kWait, state_.action); | 59 DCHECK_EQ(kWait, state_.action); |
46 state_ = Step(state_, event); | 60 state_ = Step(state_, event); |
47 if (state_.action == kRun) { | 61 if (state_.action == kRun) { |
48 DCHECK(heap()->incremental_marking()->IsStopped()); | 62 DCHECK(heap()->incremental_marking()->IsStopped()); |
49 DCHECK(FLAG_incremental_marking); | 63 DCHECK(FLAG_incremental_marking); |
50 if (FLAG_trace_gc_verbose) { | 64 if (FLAG_trace_gc_verbose) { |
51 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", | 65 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", |
52 state_.started_gcs); | 66 state_.started_gcs); |
(...skipping 10 matching lines...) Expand all Loading... | |
63 kIncrementalMarkingDelayMs; | 77 kIncrementalMarkingDelayMs; |
64 heap()->incremental_marking()->AdvanceIncrementalMarking( | 78 heap()->incremental_marking()->AdvanceIncrementalMarking( |
65 0, deadline, i::IncrementalMarking::StepActions( | 79 0, deadline, i::IncrementalMarking::StepActions( |
66 i::IncrementalMarking::NO_GC_VIA_STACK_GUARD, | 80 i::IncrementalMarking::NO_GC_VIA_STACK_GUARD, |
67 i::IncrementalMarking::FORCE_MARKING, | 81 i::IncrementalMarking::FORCE_MARKING, |
68 i::IncrementalMarking::FORCE_COMPLETION)); | 82 i::IncrementalMarking::FORCE_COMPLETION)); |
69 heap()->FinalizeIncrementalMarkingIfComplete( | 83 heap()->FinalizeIncrementalMarkingIfComplete( |
70 "Memory reducer: finalize incremental marking"); | 84 "Memory reducer: finalize incremental marking"); |
71 } | 85 } |
72 // Re-schedule the timer. | 86 // Re-schedule the timer. |
73 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); | 87 ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms); |
74 if (FLAG_trace_gc_verbose) { | 88 if (FLAG_trace_gc_verbose) { |
75 PrintIsolate(heap()->isolate(), "Memory reducer: waiting for %.f ms\n", | 89 PrintIsolate(heap()->isolate(), "Memory reducer: waiting for %.f ms\n", |
76 state_.next_gc_start_ms - event.time_ms); | 90 state_.next_gc_start_ms - event.time_ms); |
77 } | 91 } |
78 } | 92 } |
79 } | 93 } |
80 | 94 |
81 | 95 |
82 void MemoryReducer::NotifyMarkCompact(const Event& event) { | 96 void MemoryReducer::NotifyMarkCompact(const Event& event) { |
83 DCHECK_EQ(kMarkCompact, event.type); | 97 DCHECK_EQ(kMarkCompact, event.type); |
84 Action old_action = state_.action; | 98 Action old_action = state_.action; |
85 state_ = Step(state_, event); | 99 state_ = Step(state_, event); |
86 if (old_action != kWait && state_.action == kWait) { | 100 if (old_action != kWait && state_.action == kWait) { |
87 // If we are transitioning to the WAIT state, start the timer. | 101 // If we are transitioning to the WAIT state, start the timer. |
88 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); | 102 ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms); |
89 } | 103 } |
90 if (old_action == kRun) { | 104 if (old_action == kRun) { |
91 if (FLAG_trace_gc_verbose) { | 105 if (FLAG_trace_gc_verbose) { |
92 PrintIsolate(heap()->isolate(), "Memory reducer: finished GC #%d (%s)\n", | 106 PrintIsolate(heap()->isolate(), "Memory reducer: finished GC #%d (%s)\n", |
93 state_.started_gcs, | 107 state_.started_gcs, |
94 state_.action == kWait ? "will do more" : "done"); | 108 state_.action == kWait ? "will do more" : "done"); |
95 } | 109 } |
96 } | 110 } |
97 } | 111 } |
98 | 112 |
99 | 113 |
100 void MemoryReducer::NotifyContextDisposed(const Event& event) { | 114 void MemoryReducer::NotifyContextDisposed(const Event& event) { |
101 DCHECK_EQ(kContextDisposed, event.type); | 115 DCHECK_EQ(kContextDisposed, event.type); |
102 Action old_action = state_.action; | 116 Action old_action = state_.action; |
103 state_ = Step(state_, event); | 117 state_ = Step(state_, event); |
104 if (old_action != kWait && state_.action == kWait) { | 118 if (old_action != kWait && state_.action == kWait) { |
105 // If we are transitioning to the WAIT state, start the timer. | 119 // If we are transitioning to the WAIT state, start the timer. |
106 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); | 120 ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms); |
107 } | 121 } |
108 } | 122 } |
109 | 123 |
110 | 124 |
111 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { | 125 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { |
112 return state.last_gc_time_ms != 0 && | 126 return state.last_gc_time_ms != 0 && |
113 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; | 127 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; |
114 } | 128 } |
115 | 129 |
116 | 130 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 } else { | 179 } else { |
166 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms); | 180 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms); |
167 } | 181 } |
168 } | 182 } |
169 } | 183 } |
170 UNREACHABLE(); | 184 UNREACHABLE(); |
171 return State(kDone, 0, 0, 0.0); // Make the compiler happy. | 185 return State(kDone, 0, 0, 0.0); // Make the compiler happy. |
172 } | 186 } |
173 | 187 |
174 | 188 |
175 void MemoryReducer::ScheduleTimer(double delay_ms) { | 189 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) { |
176 DCHECK(delay_ms > 0); | 190 DCHECK(delay_ms > 0); |
191 // Record the time and the js call counter. | |
192 SampleAndGetJsCallsPerMs(time_ms); | |
177 // Leave some room for precision error in task scheduler. | 193 // Leave some room for precision error in task scheduler. |
178 const double kSlackMs = 100; | 194 const double kSlackMs = 100; |
179 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); | 195 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
180 auto timer_task = new MemoryReducer::TimerTask(this); | 196 auto timer_task = new MemoryReducer::TimerTask(this); |
181 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 197 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
182 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); | 198 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); |
183 } | 199 } |
184 | 200 |
185 | 201 |
186 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } | 202 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } |
187 | 203 |
188 } // namespace internal | 204 } // namespace internal |
189 } // namespace v8 | 205 } // namespace v8 |
OLD | NEW |