Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1115)

Side by Side Diff: src/heap/memory-reducer.cc

Issue 1414483004: Add the rate of js invocations from the api as a signal of idleness (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Address comments from Hannes Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/heap/memory-reducer.h ('k') | src/isolate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 = 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.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 low_allocation_rate = heap->HasLowAllocationRate();
35 bool is_idle = js_call_rate < kJsCallsPerMsThreshold && low_allocation_rate;
36 bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage();
37 if (FLAG_trace_gc_verbose) {
38 PrintIsolate(heap->isolate(), "Memory reducer: call rate %.3lf, %s, %s\n",
39 js_call_rate, low_allocation_rate ? "low alloc" : "high alloc",
40 optimize_for_memory ? "background" : "foreground");
41 }
32 event.type = kTimer; 42 event.type = kTimer;
33 event.time_ms = time_ms; 43 event.time_ms = time_ms;
34 event.should_start_incremental_gc = 44 // The memory reducer will start incremental markig if
35 heap->HasLowAllocationRate() || heap->ShouldOptimizeForMemoryUsage(); 45 // 1) mutator is likely idle: js call rate is low and allocation rate is low.
46 // 2) mutator is in background: optimize for memory flag is set.
47 event.should_start_incremental_gc = is_idle || optimize_for_memory;
36 event.can_start_incremental_gc = 48 event.can_start_incremental_gc =
37 heap->incremental_marking()->IsStopped() && 49 heap->incremental_marking()->IsStopped() &&
38 heap->incremental_marking()->CanBeActivated(); 50 heap->incremental_marking()->CanBeActivated();
39 memory_reducer_->NotifyTimer(event); 51 memory_reducer_->NotifyTimer(event);
40 } 52 }
41 53
42 54
55 double MemoryReducer::SampleAndGetJsCallsPerMs(double time_ms) {
56 unsigned int counter = heap()->isolate()->js_calls_from_api_counter();
57 unsigned int call_delta = counter - js_calls_counter_;
58 double time_delta_ms = time_ms - js_calls_sample_time_ms_;
59 js_calls_counter_ = counter;
60 js_calls_sample_time_ms_ = time_ms;
61 return time_delta_ms > 0 ? call_delta / time_delta_ms : 0;
62 }
63
64
43 void MemoryReducer::NotifyTimer(const Event& event) { 65 void MemoryReducer::NotifyTimer(const Event& event) {
44 DCHECK_EQ(kTimer, event.type); 66 DCHECK_EQ(kTimer, event.type);
45 DCHECK_EQ(kWait, state_.action); 67 DCHECK_EQ(kWait, state_.action);
46 state_ = Step(state_, event); 68 state_ = Step(state_, event);
47 if (state_.action == kRun) { 69 if (state_.action == kRun) {
48 DCHECK(heap()->incremental_marking()->IsStopped()); 70 DCHECK(heap()->incremental_marking()->IsStopped());
49 DCHECK(FLAG_incremental_marking); 71 DCHECK(FLAG_incremental_marking);
50 if (FLAG_trace_gc_verbose) { 72 if (FLAG_trace_gc_verbose) {
51 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", 73 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n",
52 state_.started_gcs); 74 state_.started_gcs);
(...skipping 10 matching lines...) Expand all
63 kIncrementalMarkingDelayMs; 85 kIncrementalMarkingDelayMs;
64 heap()->incremental_marking()->AdvanceIncrementalMarking( 86 heap()->incremental_marking()->AdvanceIncrementalMarking(
65 0, deadline, i::IncrementalMarking::StepActions( 87 0, deadline, i::IncrementalMarking::StepActions(
66 i::IncrementalMarking::NO_GC_VIA_STACK_GUARD, 88 i::IncrementalMarking::NO_GC_VIA_STACK_GUARD,
67 i::IncrementalMarking::FORCE_MARKING, 89 i::IncrementalMarking::FORCE_MARKING,
68 i::IncrementalMarking::FORCE_COMPLETION)); 90 i::IncrementalMarking::FORCE_COMPLETION));
69 heap()->FinalizeIncrementalMarkingIfComplete( 91 heap()->FinalizeIncrementalMarkingIfComplete(
70 "Memory reducer: finalize incremental marking"); 92 "Memory reducer: finalize incremental marking");
71 } 93 }
72 // Re-schedule the timer. 94 // Re-schedule the timer.
73 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); 95 ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
74 if (FLAG_trace_gc_verbose) { 96 if (FLAG_trace_gc_verbose) {
75 PrintIsolate(heap()->isolate(), "Memory reducer: waiting for %.f ms\n", 97 PrintIsolate(heap()->isolate(), "Memory reducer: waiting for %.f ms\n",
76 state_.next_gc_start_ms - event.time_ms); 98 state_.next_gc_start_ms - event.time_ms);
77 } 99 }
78 } 100 }
79 } 101 }
80 102
81 103
82 void MemoryReducer::NotifyMarkCompact(const Event& event) { 104 void MemoryReducer::NotifyMarkCompact(const Event& event) {
83 DCHECK_EQ(kMarkCompact, event.type); 105 DCHECK_EQ(kMarkCompact, event.type);
84 Action old_action = state_.action; 106 Action old_action = state_.action;
85 state_ = Step(state_, event); 107 state_ = Step(state_, event);
86 if (old_action != kWait && state_.action == kWait) { 108 if (old_action != kWait && state_.action == kWait) {
87 // If we are transitioning to the WAIT state, start the timer. 109 // If we are transitioning to the WAIT state, start the timer.
88 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); 110 ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
89 } 111 }
90 if (old_action == kRun) { 112 if (old_action == kRun) {
91 if (FLAG_trace_gc_verbose) { 113 if (FLAG_trace_gc_verbose) {
92 PrintIsolate(heap()->isolate(), "Memory reducer: finished GC #%d (%s)\n", 114 PrintIsolate(heap()->isolate(), "Memory reducer: finished GC #%d (%s)\n",
93 state_.started_gcs, 115 state_.started_gcs,
94 state_.action == kWait ? "will do more" : "done"); 116 state_.action == kWait ? "will do more" : "done");
95 } 117 }
96 } 118 }
97 } 119 }
98 120
99 121
100 void MemoryReducer::NotifyContextDisposed(const Event& event) { 122 void MemoryReducer::NotifyContextDisposed(const Event& event) {
101 DCHECK_EQ(kContextDisposed, event.type); 123 DCHECK_EQ(kContextDisposed, event.type);
102 Action old_action = state_.action; 124 Action old_action = state_.action;
103 state_ = Step(state_, event); 125 state_ = Step(state_, event);
104 if (old_action != kWait && state_.action == kWait) { 126 if (old_action != kWait && state_.action == kWait) {
105 // If we are transitioning to the WAIT state, start the timer. 127 // If we are transitioning to the WAIT state, start the timer.
106 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); 128 ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
107 } 129 }
108 } 130 }
109 131
110 132
111 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { 133 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) {
112 return state.last_gc_time_ms != 0 && 134 return state.last_gc_time_ms != 0 &&
113 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; 135 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs;
114 } 136 }
115 137
116 138
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 } else { 187 } else {
166 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms); 188 return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms);
167 } 189 }
168 } 190 }
169 } 191 }
170 UNREACHABLE(); 192 UNREACHABLE();
171 return State(kDone, 0, 0, 0.0); // Make the compiler happy. 193 return State(kDone, 0, 0, 0.0); // Make the compiler happy.
172 } 194 }
173 195
174 196
175 void MemoryReducer::ScheduleTimer(double delay_ms) { 197 void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) {
176 DCHECK(delay_ms > 0); 198 DCHECK(delay_ms > 0);
199 // Record the time and the js call counter.
200 SampleAndGetJsCallsPerMs(time_ms);
177 // Leave some room for precision error in task scheduler. 201 // Leave some room for precision error in task scheduler.
178 const double kSlackMs = 100; 202 const double kSlackMs = 100;
179 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); 203 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate());
180 auto timer_task = new MemoryReducer::TimerTask(this); 204 auto timer_task = new MemoryReducer::TimerTask(this);
181 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( 205 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread(
182 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); 206 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0);
183 } 207 }
184 208
185 209
186 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } 210 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); }
187 211
188 } // namespace internal 212 } // namespace internal
189 } // namespace v8 213 } // namespace v8
OLDNEW
« no previous file with comments | « src/heap/memory-reducer.h ('k') | src/isolate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698