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