Index: src/heap/memory-reducer.cc |
diff --git a/src/heap/memory-reducer.cc b/src/heap/memory-reducer.cc |
index c89d1424e7c5e78e303ae64b7043669571bb4035..df827bae29e48cb5eb1daf516ea756f5d80d5f3b 100644 |
--- a/src/heap/memory-reducer.cc |
+++ b/src/heap/memory-reducer.cc |
@@ -14,6 +14,7 @@ namespace internal { |
const int MemoryReducer::kLongDelayMs = 5000; |
const int MemoryReducer::kShortDelayMs = 500; |
+const int MemoryReducer::kWatchdogDelayMs = 100000; |
const int MemoryReducer::kMaxNumberOfGCs = 3; |
@@ -21,8 +22,11 @@ void MemoryReducer::TimerTask::Run() { |
if (heap_is_torn_down_) return; |
Heap* heap = memory_reducer_->heap(); |
Event event; |
+ double time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
+ heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(), |
+ heap->OldGenerationAllocationCounter()); |
event.type = kTimer; |
- event.time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
+ event.time_ms = time_ms; |
event.low_allocation_rate = heap->HasLowAllocationRate(); |
event.can_start_incremental_gc = |
heap->incremental_marking()->IsStopped() && |
@@ -93,23 +97,36 @@ void MemoryReducer::NotifyBackgroundIdleNotification(const Event& event) { |
if (old_action == kWait && state_.action == kWait && |
old_started_gcs + 1 == state_.started_gcs) { |
DCHECK(heap()->incremental_marking()->IsStopped()); |
- DCHECK(FLAG_incremental_marking); |
- heap()->StartIdleIncrementalMarking(); |
- if (FLAG_trace_gc_verbose) { |
- PrintIsolate(heap()->isolate(), |
- "Memory reducer: started GC #%d" |
- " (background idle)\n", |
- state_.started_gcs); |
+ // TODO(ulan): Replace it with incremental marking GC once |
+ // chromium:490559 is fixed. |
+ if (event.time_ms > state_.last_gc_time_ms + kLongDelayMs) { |
+ heap()->CollectAllGarbage(Heap::kReduceMemoryFootprintMask, |
+ "memory reducer background GC"); |
+ } else { |
+ DCHECK(FLAG_incremental_marking); |
+ heap()->StartIdleIncrementalMarking(); |
+ if (FLAG_trace_gc_verbose) { |
+ PrintIsolate(heap()->isolate(), |
+ "Memory reducer: started GC #%d" |
+ " (background idle)\n", |
+ state_.started_gcs); |
+ } |
} |
} |
} |
+bool MemoryReducer::WatchdogGC(const State& state, const Event& event) { |
+ return state.last_gc_time_ms != 0 && |
+ event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs; |
+} |
+ |
+ |
// For specification of this function see the comment for MemoryReducer class. |
MemoryReducer::State MemoryReducer::Step(const State& state, |
const Event& event) { |
if (!FLAG_incremental_marking) { |
- return State(kDone, 0, 0); |
+ return State(kDone, 0, 0, state.last_gc_time_ms); |
} |
switch (state.action) { |
case kDone: |
@@ -117,7 +134,9 @@ MemoryReducer::State MemoryReducer::Step(const State& state, |
return state; |
} else { |
DCHECK(event.type == kContextDisposed || event.type == kMarkCompact); |
- return State(kWait, 0, event.time_ms + kLongDelayMs); |
+ return State( |
+ kWait, 0, event.time_ms + kLongDelayMs, |
+ event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms); |
} |
case kWait: |
switch (event.type) { |
@@ -125,28 +144,30 @@ MemoryReducer::State MemoryReducer::Step(const State& state, |
return state; |
case kTimer: |
if (state.started_gcs >= kMaxNumberOfGCs) { |
- return State(kDone, 0, 0.0); |
+ return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms); |
} else if (event.can_start_incremental_gc && |
- event.low_allocation_rate) { |
+ (event.low_allocation_rate || WatchdogGC(state, event))) { |
if (state.next_gc_start_ms <= event.time_ms) { |
- return State(kRun, state.started_gcs + 1, 0.0); |
+ return State(kRun, state.started_gcs + 1, 0.0, |
+ state.last_gc_time_ms); |
} else { |
return state; |
} |
} else { |
- return State(kWait, state.started_gcs, |
- event.time_ms + kLongDelayMs); |
+ return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
+ state.last_gc_time_ms); |
} |
case kBackgroundIdleNotification: |
if (event.can_start_incremental_gc && |
state.started_gcs < kMaxNumberOfGCs) { |
return State(kWait, state.started_gcs + 1, |
- event.time_ms + kLongDelayMs); |
+ event.time_ms + kLongDelayMs, state.last_gc_time_ms); |
} else { |
return state; |
} |
case kMarkCompact: |
- return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs); |
+ return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs, |
+ event.time_ms); |
} |
case kRun: |
if (event.type != kMarkCompact) { |
@@ -154,14 +175,15 @@ MemoryReducer::State MemoryReducer::Step(const State& state, |
} else { |
if (state.started_gcs < kMaxNumberOfGCs && |
(event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { |
- return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs); |
+ return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs, |
+ event.time_ms); |
} else { |
- return State(kDone, 0, 0.0); |
+ return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms); |
} |
} |
} |
UNREACHABLE(); |
- return State(kDone, 0, 0); // Make the compiler happy. |
+ return State(kDone, 0, 0, 0.0); // Make the compiler happy. |
} |
@@ -189,7 +211,7 @@ void MemoryReducer::TearDown() { |
pending_task_->NotifyHeapTearDown(); |
pending_task_ = nullptr; |
} |
- state_ = State(kDone, 0, 0); |
+ state_ = State(kDone, 0, 0, 0.0); |
} |
} // internal |