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 61049 : MemoryReducer::MemoryReducer(Heap* heap)
25 : : heap_(heap),
26 61049 : taskrunner_(V8::GetCurrentPlatform()->GetForegroundTaskRunner(
27 61049 : reinterpret_cast<v8::Isolate*>(heap->isolate()))),
28 : state_(kDone, 0, 0.0, 0.0, 0),
29 : js_calls_counter_(0),
30 183147 : js_calls_sample_time_ms_(0.0) {}
31 :
32 2570 : MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer)
33 : : CancelableTask(memory_reducer->heap()->isolate()),
34 2570 : memory_reducer_(memory_reducer) {}
35 :
36 :
37 37 : void MemoryReducer::TimerTask::RunInternal() {
38 37 : Heap* heap = memory_reducer_->heap();
39 : Event event;
40 37 : double time_ms = heap->MonotonicallyIncreasingTimeInMs();
41 : heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(),
42 37 : heap->OldGenerationAllocationCounter());
43 37 : bool low_allocation_rate = heap->HasLowAllocationRate();
44 37 : bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage();
45 37 : if (FLAG_trace_gc_verbose) {
46 : 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 37 : event.type = kTimer;
52 37 : 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 37 : low_allocation_rate || optimize_for_memory;
58 : event.can_start_incremental_gc =
59 65 : heap->incremental_marking()->IsStopped() &&
60 28 : (heap->incremental_marking()->CanBeActivated() || optimize_for_memory);
61 37 : event.committed_memory = heap->CommittedOldGenerationMemory();
62 37 : memory_reducer_->NotifyTimer(event);
63 37 : }
64 :
65 :
66 74 : void MemoryReducer::NotifyTimer(const Event& event) {
67 : DCHECK_EQ(kTimer, event.type);
68 : DCHECK_EQ(kWait, state_.action);
69 37 : state_ = Step(state_, event);
70 37 : if (state_.action == kRun) {
71 : DCHECK(heap()->incremental_marking()->IsStopped());
72 : DCHECK(FLAG_incremental_marking);
73 8 : if (FLAG_trace_gc_verbose) {
74 : heap()->isolate()->PrintWithTimestamp("Memory reducer: started GC #%d\n",
75 0 : state_.started_gcs);
76 : }
77 : heap()->StartIdleIncrementalMarking(
78 : GarbageCollectionReason::kMemoryReducer,
79 8 : kGCCallbackFlagCollectAllExternalMemory);
80 29 : } else if (state_.action == kWait) {
81 38 : if (!heap()->incremental_marking()->IsStopped() &&
82 9 : 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 29 : ScheduleTimer(state_.next_gc_start_ms - event.time_ms);
97 29 : if (FLAG_trace_gc_verbose) {
98 : heap()->isolate()->PrintWithTimestamp(
99 : "Memory reducer: waiting for %.f ms\n",
100 0 : state_.next_gc_start_ms - event.time_ms);
101 : }
102 : }
103 37 : }
104 :
105 :
106 74510 : void MemoryReducer::NotifyMarkCompact(const Event& event) {
107 : DCHECK_EQ(kMarkCompact, event.type);
108 74510 : Action old_action = state_.action;
109 74510 : state_ = Step(state_, event);
110 74510 : if (old_action != kWait && state_.action == kWait) {
111 : // If we are transitioning to the WAIT state, start the timer.
112 89 : ScheduleTimer(state_.next_gc_start_ms - event.time_ms);
113 : }
114 74510 : if (old_action == kRun) {
115 6 : if (FLAG_trace_gc_verbose) {
116 : 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 74510 : }
122 :
123 1748954 : void MemoryReducer::NotifyPossibleGarbage(const Event& event) {
124 : DCHECK_EQ(kPossibleGarbage, event.type);
125 1748954 : Action old_action = state_.action;
126 1748954 : state_ = Step(state_, event);
127 1748954 : if (old_action != kWait && state_.action == kWait) {
128 : // If we are transitioning to the WAIT state, start the timer.
129 2452 : ScheduleTimer(state_.next_gc_start_ms - event.time_ms);
130 : }
131 1748954 : }
132 :
133 :
134 0 : bool MemoryReducer::WatchdogGC(const State& state, const Event& event) {
135 45 : return state.last_gc_time_ms != 0 &&
136 22 : 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 1823531 : MemoryReducer::State MemoryReducer::Step(const State& state,
142 : const Event& event) {
143 1823531 : if (!FLAG_incremental_marking || !FLAG_memory_reducer) {
144 613 : return State(kDone, 0, 0, state.last_gc_time_ms, 0);
145 : }
146 1822918 : switch (state.action) {
147 : case kDone:
148 63343 : if (event.type == kTimer) {
149 3 : return state;
150 63340 : } else if (event.type == kMarkCompact) {
151 121774 : if (event.committed_memory <
152 60887 : Max(static_cast<size_t>(state.committed_memory_at_last_run *
153 : kCommittedMemoryFactor),
154 121774 : state.committed_memory_at_last_run + kCommittedMemoryDelta)) {
155 60800 : return state;
156 : } else {
157 : return State(kWait, 0, event.time_ms + kLongDelayMs,
158 : event.type == kMarkCompact ? event.time_ms
159 : : state.last_gc_time_ms,
160 87 : 0);
161 : }
162 : } else {
163 : DCHECK_EQ(kPossibleGarbage, event.type);
164 : return State(
165 : kWait, 0, event.time_ms + kLongDelayMs,
166 : event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms,
167 2453 : 0);
168 : }
169 : case kWait:
170 1759560 : switch (event.type) {
171 : case kPossibleGarbage:
172 1746497 : return state;
173 : case kTimer:
174 47 : if (state.started_gcs >= kMaxNumberOfGCs) {
175 : return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms,
176 3 : event.committed_memory);
177 88 : } else if (event.can_start_incremental_gc &&
178 23 : (event.should_start_incremental_gc ||
179 : WatchdogGC(state, event))) {
180 12 : if (state.next_gc_start_ms <= event.time_ms) {
181 : return State(kRun, state.started_gcs + 1, 0.0,
182 10 : state.last_gc_time_ms, 0);
183 : } else {
184 2 : return state;
185 : }
186 : } else {
187 : return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
188 32 : state.last_gc_time_ms, 0);
189 : }
190 : case kMarkCompact:
191 : return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
192 13016 : event.time_ms, 0);
193 : }
194 : case kRun:
195 15 : if (event.type != kMarkCompact) {
196 5 : return state;
197 : } else {
198 10 : if (state.started_gcs < kMaxNumberOfGCs &&
199 4 : (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) {
200 : return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs,
201 7 : event.time_ms, 0);
202 : } else {
203 : return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms,
204 3 : event.committed_memory);
205 : }
206 : }
207 : }
208 0 : UNREACHABLE();
209 : }
210 :
211 2570 : void MemoryReducer::ScheduleTimer(double delay_ms) {
212 : DCHECK_LT(0, delay_ms);
213 5140 : if (heap()->IsTearingDown()) return;
214 : // Leave some room for precision error in task scheduler.
215 : const double kSlackMs = 100;
216 2570 : taskrunner_->PostDelayedTask(
217 : base::make_unique<MemoryReducer::TimerTask>(this),
218 10280 : (delay_ms + kSlackMs) / 1000.0);
219 : }
220 :
221 61034 : void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0, 0); }
222 :
223 : } // namespace internal
224 178779 : } // namespace v8
|