| OLD | NEW |
| 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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 | 25 |
| 26 void MemoryReducer::TimerTask::RunInternal() { | 26 void MemoryReducer::TimerTask::RunInternal() { |
| 27 Heap* heap = memory_reducer_->heap(); | 27 Heap* heap = memory_reducer_->heap(); |
| 28 Event event; | 28 Event event; |
| 29 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); | 29 double time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
| 30 heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), | 30 heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), |
| 31 heap->OldGenerationAllocationCounter()); | 31 heap->OldGenerationAllocationCounter()); |
| 32 event.type = kTimer; | 32 event.type = kTimer; |
| 33 event.time_ms = time_ms; | 33 event.time_ms = time_ms; |
| 34 event.low_allocation_rate = heap->HasLowAllocationRate(); | 34 event.should_start_incremental_gc = |
| 35 heap->HasLowAllocationRate() || heap->ShouldOptimizeForMemoryUsage(); |
| 35 event.can_start_incremental_gc = | 36 event.can_start_incremental_gc = |
| 36 heap->incremental_marking()->IsStopped() && | 37 heap->incremental_marking()->IsStopped() && |
| 37 heap->incremental_marking()->CanBeActivated(); | 38 heap->incremental_marking()->CanBeActivated(); |
| 38 memory_reducer_->NotifyTimer(event); | 39 memory_reducer_->NotifyTimer(event); |
| 39 } | 40 } |
| 40 | 41 |
| 41 | 42 |
| 42 void MemoryReducer::NotifyTimer(const Event& event) { | 43 void MemoryReducer::NotifyTimer(const Event& event) { |
| 43 DCHECK_EQ(kTimer, event.type); | 44 DCHECK_EQ(kTimer, event.type); |
| 44 DCHECK_EQ(kWait, state_.action); | 45 DCHECK_EQ(kWait, state_.action); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 DCHECK_EQ(kContextDisposed, event.type); | 101 DCHECK_EQ(kContextDisposed, event.type); |
| 101 Action old_action = state_.action; | 102 Action old_action = state_.action; |
| 102 state_ = Step(state_, event); | 103 state_ = Step(state_, event); |
| 103 if (old_action != kWait && state_.action == kWait) { | 104 if (old_action != kWait && state_.action == kWait) { |
| 104 // If we are transitioning to the WAIT state, start the timer. | 105 // If we are transitioning to the WAIT state, start the timer. |
| 105 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); | 106 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); |
| 106 } | 107 } |
| 107 } | 108 } |
| 108 | 109 |
| 109 | 110 |
| 110 void MemoryReducer::NotifyBackgroundIdleNotification(const Event& event) { | |
| 111 DCHECK_EQ(kBackgroundIdleNotification, event.type); | |
| 112 Action old_action = state_.action; | |
| 113 int old_started_gcs = state_.started_gcs; | |
| 114 state_ = Step(state_, event); | |
| 115 if (old_action == kWait && state_.action == kWait && | |
| 116 old_started_gcs + 1 == state_.started_gcs) { | |
| 117 DCHECK(heap()->incremental_marking()->IsStopped()); | |
| 118 // TODO(ulan): Replace it with incremental marking GC once | |
| 119 // chromium:490559 is fixed. | |
| 120 if (event.time_ms > state_.last_gc_time_ms + kLongDelayMs) { | |
| 121 heap()->CollectAllGarbage(Heap::kReduceMemoryFootprintMask, | |
| 122 "memory reducer background GC"); | |
| 123 } else { | |
| 124 DCHECK(FLAG_incremental_marking); | |
| 125 heap()->StartIdleIncrementalMarking(); | |
| 126 if (FLAG_trace_gc_verbose) { | |
| 127 PrintIsolate(heap()->isolate(), | |
| 128 "Memory reducer: started GC #%d" | |
| 129 " (background idle)\n", | |
| 130 state_.started_gcs); | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 | |
| 137 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { | 111 bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { |
| 138 return state.last_gc_time_ms != 0 && | 112 return state.last_gc_time_ms != 0 && |
| 139 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; | 113 event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; |
| 140 } | 114 } |
| 141 | 115 |
| 142 | 116 |
| 143 // For specification of this function see the comment for MemoryReducer class. | 117 // For specification of this function see the comment for MemoryReducer class. |
| 144 MemoryReducer::State MemoryReducer::Step(const State& state, | 118 MemoryReducer::State MemoryReducer::Step(const State& state, |
| 145 const Event& event) { | 119 const Event& event) { |
| 146 if (!FLAG_incremental_marking || !FLAG_memory_reducer) { | 120 if (!FLAG_incremental_marking || !FLAG_memory_reducer) { |
| 147 return State(kDone, 0, 0, state.last_gc_time_ms); | 121 return State(kDone, 0, 0, state.last_gc_time_ms); |
| 148 } | 122 } |
| 149 switch (state.action) { | 123 switch (state.action) { |
| 150 case kDone: | 124 case kDone: |
| 151 if (event.type == kTimer || event.type == kBackgroundIdleNotification) { | 125 if (event.type == kTimer) { |
| 152 return state; | 126 return state; |
| 153 } else { | 127 } else { |
| 154 DCHECK(event.type == kContextDisposed || event.type == kMarkCompact); | 128 DCHECK(event.type == kContextDisposed || event.type == kMarkCompact); |
| 155 return State( | 129 return State( |
| 156 kWait, 0, event.time_ms + kLongDelayMs, | 130 kWait, 0, event.time_ms + kLongDelayMs, |
| 157 event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms); | 131 event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms); |
| 158 } | 132 } |
| 159 case kWait: | 133 case kWait: |
| 160 switch (event.type) { | 134 switch (event.type) { |
| 161 case kContextDisposed: | 135 case kContextDisposed: |
| 162 return state; | 136 return state; |
| 163 case kTimer: | 137 case kTimer: |
| 164 if (state.started_gcs >= kMaxNumberOfGCs) { | 138 if (state.started_gcs >= kMaxNumberOfGCs) { |
| 165 return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms); | 139 return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms); |
| 166 } else if (event.can_start_incremental_gc && | 140 } else if (event.can_start_incremental_gc && |
| 167 (event.low_allocation_rate || WatchdogGC(state, event))) { | 141 (event.should_start_incremental_gc || |
| 142 WatchdogGC(state, event))) { |
| 168 if (state.next_gc_start_ms <= event.time_ms) { | 143 if (state.next_gc_start_ms <= event.time_ms) { |
| 169 return State(kRun, state.started_gcs + 1, 0.0, | 144 return State(kRun, state.started_gcs + 1, 0.0, |
| 170 state.last_gc_time_ms); | 145 state.last_gc_time_ms); |
| 171 } else { | 146 } else { |
| 172 return state; | 147 return state; |
| 173 } | 148 } |
| 174 } else { | 149 } else { |
| 175 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, | 150 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
| 176 state.last_gc_time_ms); | 151 state.last_gc_time_ms); |
| 177 } | 152 } |
| 178 case kBackgroundIdleNotification: | |
| 179 if (event.can_start_incremental_gc && | |
| 180 state.started_gcs < kMaxNumberOfGCs) { | |
| 181 return State(kWait, state.started_gcs + 1, | |
| 182 event.time_ms + kLongDelayMs, state.last_gc_time_ms); | |
| 183 } else { | |
| 184 return state; | |
| 185 } | |
| 186 case kMarkCompact: | 153 case kMarkCompact: |
| 187 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, | 154 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
| 188 event.time_ms); | 155 event.time_ms); |
| 189 } | 156 } |
| 190 case kRun: | 157 case kRun: |
| 191 if (event.type != kMarkCompact) { | 158 if (event.type != kMarkCompact) { |
| 192 return state; | 159 return state; |
| 193 } else { | 160 } else { |
| 194 if (state.started_gcs < kMaxNumberOfGCs && | 161 if (state.started_gcs < kMaxNumberOfGCs && |
| 195 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { | 162 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 213 auto timer_task = new MemoryReducer::TimerTask(this); | 180 auto timer_task = new MemoryReducer::TimerTask(this); |
| 214 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( | 181 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
| 215 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); | 182 isolate, timer_task, (delay_ms + kSlackMs) / 1000.0); |
| 216 } | 183 } |
| 217 | 184 |
| 218 | 185 |
| 219 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } | 186 void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); } |
| 220 | 187 |
| 221 } // namespace internal | 188 } // namespace internal |
| 222 } // namespace v8 | 189 } // namespace v8 |
| OLD | NEW |