Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "src/heap/memory-reducer.h"
6 :
7 : #include "src/flags.h"
8 : #include "src/heap/gc-tracer.h"
9 : #include "src/heap/heap-inl.h"
10 : #include "src/utils.h"
11 : #include "src/v8.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 : const int MemoryReducer::kLongDelayMs = 8000;
17 : const int MemoryReducer::kShortDelayMs = 500;
18 : const int MemoryReducer::kWatchdogDelayMs = 100000;
19 : const int MemoryReducer::kMaxNumberOfGCs = 3;
20 : const double MemoryReducer::kCommittedMemoryFactor = 1.1;
21 : const size_t MemoryReducer::kCommittedMemoryDelta = 10 * MB;
22 :
23 269 : MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer)
24 : : CancelableTask(memory_reducer->heap()->isolate()),
25 269 : memory_reducer_(memory_reducer) {}
26 :
27 :
28 10 : void MemoryReducer::TimerTask::RunInternal() {
29 30 : Heap* heap = memory_reducer_->heap();
30 : Event event;
31 10 : double time_ms = heap->MonotonicallyIncreasingTimeInMs();
32 : heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(),
33 10 : heap->OldGenerationAllocationCounter());
34 10 : bool low_allocation_rate = heap->HasLowAllocationRate();
35 10 : bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage();
36 10 : if (FLAG_trace_gc_verbose) {
37 : heap->isolate()->PrintWithTimestamp(
38 : "Memory reducer: %s, %s\n",
39 : low_allocation_rate ? "low alloc" : "high alloc",
40 0 : optimize_for_memory ? "background" : "foreground");
41 : }
42 10 : event.type = kTimer;
43 10 : event.time_ms = time_ms;
44 : // The memory reducer will start incremental markig if
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 =
48 10 : low_allocation_rate || optimize_for_memory;
49 : event.can_start_incremental_gc =
50 12 : heap->incremental_marking()->IsStopped() &&
51 2 : (heap->incremental_marking()->CanBeActivated() || optimize_for_memory);
52 10 : event.committed_memory = heap->CommittedOldGenerationMemory();
53 10 : memory_reducer_->NotifyTimer(event);
54 10 : }
55 :
56 :
57 20 : void MemoryReducer::NotifyTimer(const Event& event) {
58 : DCHECK_EQ(kTimer, event.type);
59 : DCHECK_EQ(kWait, state_.action);
60 10 : state_ = Step(state_, event);
61 10 : if (state_.action == kRun) {
62 : DCHECK(heap()->incremental_marking()->IsStopped());
63 : DCHECK(FLAG_incremental_marking);
64 0 : if (FLAG_trace_gc_verbose) {
65 : heap()->isolate()->PrintWithTimestamp("Memory reducer: started GC #%d\n",
66 0 : state_.started_gcs);
67 : }
68 : heap()->StartIdleIncrementalMarking(
69 0 : GarbageCollectionReason::kMemoryReducer);
70 10 : } else if (state_.action == kWait) {
71 28 : if (!heap()->incremental_marking()->IsStopped() &&
72 8 : heap()->ShouldOptimizeForMemoryUsage()) {
73 : // Make progress with pending incremental marking if memory usage has
74 : // higher priority than latency. This is important for background tabs
75 : // that do not send idle notifications.
76 : const int kIncrementalMarkingDelayMs = 500;
77 0 : double deadline = heap()->MonotonicallyIncreasingTimeInMs() +
78 0 : kIncrementalMarkingDelayMs;
79 : heap()->incremental_marking()->AdvanceIncrementalMarking(
80 : deadline, IncrementalMarking::NO_GC_VIA_STACK_GUARD,
81 0 : IncrementalMarking::FORCE_COMPLETION, StepOrigin::kTask);
82 : heap()->FinalizeIncrementalMarkingIfComplete(
83 0 : GarbageCollectionReason::kFinalizeMarkingViaTask);
84 : }
85 : // Re-schedule the timer.
86 10 : ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
87 10 : if (FLAG_trace_gc_verbose) {
88 : heap()->isolate()->PrintWithTimestamp(
89 : "Memory reducer: waiting for %.f ms\n",
90 0 : state_.next_gc_start_ms - event.time_ms);
91 : }
92 : }
93 10 : }
94 :
95 :
96 53346 : void MemoryReducer::NotifyMarkCompact(const Event& event) {
97 : DCHECK_EQ(kMarkCompact, event.type);
98 53346 : Action old_action = state_.action;
99 53346 : state_ = Step(state_, event);
100 53346 : if (old_action != kWait && state_.action == kWait) {
101 : // If we are transitioning to the WAIT state, start the timer.
102 233 : ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
103 : }
104 53346 : if (old_action == kRun) {
105 0 : if (FLAG_trace_gc_verbose) {
106 : heap()->isolate()->PrintWithTimestamp(
107 : "Memory reducer: finished GC #%d (%s)\n", state_.started_gcs,
108 0 : state_.action == kWait ? "will do more" : "done");
109 : }
110 : }
111 53346 : }
112 :
113 44 : void MemoryReducer::NotifyPossibleGarbage(const Event& event) {
114 : DCHECK_EQ(kPossibleGarbage, event.type);
115 44 : Action old_action = state_.action;
116 44 : state_ = Step(state_, event);
117 44 : if (old_action != kWait && state_.action == kWait) {
118 : // If we are transitioning to the WAIT state, start the timer.
119 26 : ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
120 : }
121 44 : }
122 :
123 :
124 0 : bool MemoryReducer::WatchdogGC(const State& state, const Event& event) {
125 11 : return state.last_gc_time_ms != 0 &&
126 5 : event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs;
127 : }
128 :
129 :
130 : // For specification of this function see the comment for MemoryReducer class.
131 53430 : MemoryReducer::State MemoryReducer::Step(const State& state,
132 : const Event& event) {
133 53430 : if (!FLAG_incremental_marking || !FLAG_memory_reducer) {
134 422 : return State(kDone, 0, 0, state.last_gc_time_ms, 0);
135 : }
136 53008 : switch (state.action) {
137 : case kDone:
138 52496 : if (event.type == kTimer) {
139 3 : return state;
140 52493 : } else if (event.type == kMarkCompact) {
141 104932 : if (event.committed_memory <
142 52466 : Max(static_cast<size_t>(state.committed_memory_at_last_run *
143 : kCommittedMemoryFactor),
144 104932 : state.committed_memory_at_last_run + kCommittedMemoryDelta)) {
145 52230 : return state;
146 : } else {
147 : return State(kWait, 0, event.time_ms + kLongDelayMs,
148 : event.type == kMarkCompact ? event.time_ms
149 : : state.last_gc_time_ms,
150 236 : 0);
151 : }
152 : } else {
153 : DCHECK_EQ(kPossibleGarbage, event.type);
154 : return State(
155 : kWait, 0, event.time_ms + kLongDelayMs,
156 : event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms,
157 27 : 0);
158 : }
159 : case kWait:
160 504 : switch (event.type) {
161 : case kPossibleGarbage:
162 19 : return state;
163 : case kTimer:
164 20 : if (state.started_gcs >= kMaxNumberOfGCs) {
165 : return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms,
166 3 : event.committed_memory);
167 34 : } else if (event.can_start_incremental_gc &&
168 6 : (event.should_start_incremental_gc ||
169 : WatchdogGC(state, event))) {
170 3 : if (state.next_gc_start_ms <= event.time_ms) {
171 : return State(kRun, state.started_gcs + 1, 0.0,
172 2 : state.last_gc_time_ms, 0);
173 : } else {
174 1 : return state;
175 : }
176 : } else {
177 : return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
178 14 : state.last_gc_time_ms, 0);
179 : }
180 : case kMarkCompact:
181 : return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
182 465 : event.time_ms, 0);
183 : }
184 : case kRun:
185 8 : if (event.type != kMarkCompact) {
186 4 : return state;
187 : } else {
188 4 : if (state.started_gcs < kMaxNumberOfGCs &&
189 2 : (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) {
190 : return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs,
191 2 : event.time_ms, 0);
192 : } else {
193 : return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms,
194 2 : event.committed_memory);
195 : }
196 : }
197 : }
198 0 : UNREACHABLE();
199 : return State(kDone, 0, 0, 0.0, 0); // Make the compiler happy.
200 : }
201 :
202 :
203 269 : void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) {
204 : DCHECK(delay_ms > 0);
205 : // Leave some room for precision error in task scheduler.
206 : const double kSlackMs = 100;
207 : v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate());
208 269 : auto timer_task = new MemoryReducer::TimerTask(this);
209 269 : V8::GetCurrentPlatform()->CallDelayedOnForegroundThread(
210 269 : isolate, timer_task, (delay_ms + kSlackMs) / 1000.0);
211 269 : }
212 :
213 59285 : void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0, 0); }
214 :
215 : } // namespace internal
216 : } // namespace v8
|