OLD | NEW |
(Empty) | |
| 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/heap.h" |
| 9 #include "src/utils.h" |
| 10 #include "src/v8.h" |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 |
| 15 const int MemoryReducer::kLongDelayMs = 5000; |
| 16 const int MemoryReducer::kShortDelayMs = 500; |
| 17 const int MemoryReducer::kMaxNumberOfGCs = 3; |
| 18 |
| 19 |
| 20 void MemoryReducer::TimerTask::Run() { |
| 21 Heap* heap = memory_reducer_->heap(); |
| 22 Event event; |
| 23 event.type = kTimer; |
| 24 event.time_ms = heap->MonotonicallyIncreasingTimeInMs(); |
| 25 event.low_allocation_rate = heap->HasLowAllocationRate(); |
| 26 event.can_start_incremental_gc = |
| 27 heap->incremental_marking()->IsStopped() && |
| 28 heap->incremental_marking()->CanBeActivated(); |
| 29 memory_reducer_->NotifyTimer(event); |
| 30 } |
| 31 |
| 32 |
| 33 void MemoryReducer::NotifyTimer(const Event& event) { |
| 34 DCHECK_EQ(kTimer, event.type); |
| 35 DCHECK_EQ(kWait, state_.action); |
| 36 state_ = Step(state_, event); |
| 37 if (state_.action == kRun) { |
| 38 DCHECK(heap()->incremental_marking()->IsStopped()); |
| 39 DCHECK(FLAG_incremental_marking); |
| 40 heap()->StartIdleIncrementalMarking(); |
| 41 if (FLAG_trace_gc_verbose) { |
| 42 PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n", |
| 43 state_.started_gcs); |
| 44 } |
| 45 } else if (state_.action == kWait) { |
| 46 // Re-schedule the timer. |
| 47 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); |
| 48 if (FLAG_trace_gc_verbose) { |
| 49 PrintIsolate(heap()->isolate(), "Memory reducer: waiting for %.f ms\n", |
| 50 state_.next_gc_start_ms - event.time_ms); |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 55 |
| 56 void MemoryReducer::NotifyMarkCompact(const Event& event) { |
| 57 DCHECK_EQ(kMarkCompact, event.type); |
| 58 Action old_action = state_.action; |
| 59 state_ = Step(state_, event); |
| 60 if (old_action != kWait && state_.action == kWait) { |
| 61 // If we are transitioning to the WAIT state, start the timer. |
| 62 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); |
| 63 } |
| 64 if (old_action == kRun) { |
| 65 if (FLAG_trace_gc_verbose) { |
| 66 PrintIsolate(heap()->isolate(), "Memory reducer: finished GC #%d (%s)\n", |
| 67 state_.started_gcs, |
| 68 state_.action == kWait ? "will do more" : "done"); |
| 69 } |
| 70 } |
| 71 } |
| 72 |
| 73 |
| 74 void MemoryReducer::NotifyContextDisposed(const Event& event) { |
| 75 DCHECK_EQ(kContextDisposed, event.type); |
| 76 Action old_action = state_.action; |
| 77 state_ = Step(state_, event); |
| 78 if (old_action != kWait && state_.action == kWait) { |
| 79 // If we are transitioning to the WAIT state, start the timer. |
| 80 ScheduleTimer(state_.next_gc_start_ms - event.time_ms); |
| 81 } |
| 82 } |
| 83 |
| 84 |
| 85 // For specification of this function see the comment for MemoryReducer class. |
| 86 MemoryReducer::State MemoryReducer::Step(const State& state, |
| 87 const Event& event) { |
| 88 if (!FLAG_incremental_marking) { |
| 89 return State(kDone, 0, 0); |
| 90 } |
| 91 switch (state.action) { |
| 92 case kDone: |
| 93 if (event.type == kTimer) { |
| 94 return state; |
| 95 } else { |
| 96 DCHECK(event.type == kContextDisposed || event.type == kMarkCompact); |
| 97 return State(kWait, 0, event.time_ms + kLongDelayMs); |
| 98 } |
| 99 case kWait: |
| 100 if (event.type == kContextDisposed) { |
| 101 return state; |
| 102 } else if (event.type == kTimer && event.can_start_incremental_gc && |
| 103 event.low_allocation_rate) { |
| 104 if (state.next_gc_start_ms <= event.time_ms) { |
| 105 return State(kRun, state.started_gcs + 1, 0.0); |
| 106 } else { |
| 107 return state; |
| 108 } |
| 109 } else { |
| 110 return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs); |
| 111 } |
| 112 case kRun: |
| 113 if (event.type != kMarkCompact) { |
| 114 return state; |
| 115 } else { |
| 116 if (state.started_gcs < kMaxNumberOfGCs && |
| 117 (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) { |
| 118 return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs); |
| 119 } else { |
| 120 return State(kDone, 0, 0.0); |
| 121 } |
| 122 } |
| 123 } |
| 124 UNREACHABLE(); |
| 125 return State(kDone, 0, 0); // Make the compiler happy. |
| 126 } |
| 127 |
| 128 |
| 129 void MemoryReducer::ScheduleTimer(double delay_ms) { |
| 130 DCHECK(delay_ms > 0); |
| 131 // Leave some room for precision error in task scheduler. |
| 132 const double kSlackMs = 100; |
| 133 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate()); |
| 134 V8::GetCurrentPlatform()->CallDelayedOnForegroundThread( |
| 135 isolate, new MemoryReducer::TimerTask(this), |
| 136 (delay_ms + kSlackMs) / 1000.0); |
| 137 } |
| 138 |
| 139 } // internal |
| 140 } // v8 |
OLD | NEW |