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