Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #ifndef V8_HEAP_CLEANUP_GC_H | |
| 6 #define V8_HEAP_CLEANUP_GC_H | |
| 7 | |
| 8 #include "include/v8-platform.h" | |
| 9 #include "src/base/macros.h" | |
| 10 | |
| 11 namespace v8 { | |
| 12 namespace internal { | |
| 13 | |
| 14 class Heap; | |
| 15 | |
| 16 | |
| 17 // The goal of the CleanupGC class is to detect transition of the mutator | |
|
Hannes Payer (out of office)
2015/07/01 12:03:16
Hmm... I am not a big fan of the name CleanupGC. W
ulan
2015/07/01 12:51:48
Renamed to MemoryReducer as discussed.
| |
| 18 // from high allocation phase to low allocation phase and to collect potential | |
| 19 // garbage created in the high allocation phase. | |
| 20 // | |
| 21 // The class implements an automaton with the following states and transitions. | |
| 22 // | |
| 23 // States: | |
| 24 // - DONE | |
| 25 // - WAIT <started_gcs> <next_gc_start_ms> | |
| 26 // - RUN <started_gcs> | |
| 27 // The <started_gcs> is an integer in range from 0..kMaxNumberOfGCs that stores | |
| 28 // the number of GCs initiated by the CleanupGC since it left the DONE state. | |
| 29 // The <next_gc_start_ms> is a double that stores the earliest time the next GC | |
| 30 // can be initiated by the CleanupGC. | |
| 31 // The DONE state means that the CleanupGC is not active. | |
| 32 // The WAIT state means that the CleanupGC is waiting for mutator allocation | |
| 33 // rate to drop. The check for the allocation rate happens in the timer task | |
| 34 // callback. | |
| 35 // The RUN state means that the CleanupGC started incremental marking and is | |
| 36 // waiting for it to finish. Incremental marking steps are performed as usual | |
| 37 // in the idle notification and in the mutator. | |
| 38 // | |
| 39 // Transitions: | |
| 40 // DONE -> WAIT 0 (now_ms + long_delay_ms) happens: | |
| 41 // - on context disposal, | |
| 42 // - at the end of mark-compact GC initiated by the mutator. | |
| 43 // This signals that there is potential garbage to be collected. | |
| 44 // | |
| 45 // WAIT n x -> WAIT n (now_ms + long_delay_ms) happens: | |
| 46 // - on any GC initiated by the mutator, | |
| 47 // - in the timer callback if the mutator allocation rate is high or | |
| 48 // incremental GC is in progress. | |
| 49 // | |
| 50 // WAIT n x -> RUN (n+1) happens: | |
| 51 // - in the timer callback if (the mutator allocation rate is low or | |
| 52 // the heap fragmentation is high) and now_ms >= x and | |
| 53 // there is no incremental GC in progress. | |
| 54 // The CleanupGC starts incremental marking on this transition. | |
| 55 // | |
| 56 // RUN n -> DONE happens: | |
| 57 // - at end of the incremental GC initiated by the CleanupGC if | |
| 58 // (n > 1 and there is no more garbage to be collected) or | |
| 59 // n == kMaxNumberOfGCs. | |
| 60 // RUN n -> WAIT n (now_ms + short_delay_ms) happens: | |
| 61 // - at end of the incremental GC initiated by the CleanupGC if | |
| 62 // (n == 1 or there is more garbage to be collected) and | |
| 63 // n < kMaxNumberOfGCs. | |
| 64 // | |
| 65 // now_ms is the current time, long_delay_ms and short_delay_ms are constants. | |
| 66 class CleanupGC { | |
| 67 public: | |
| 68 enum Action { kDone, kWait, kRun }; | |
| 69 | |
| 70 struct State { | |
| 71 explicit State(Action action, int started_gcs, int next_gc_start_ms) | |
| 72 : action(action), | |
| 73 started_gcs(started_gcs), | |
| 74 next_gc_start_ms(next_gc_start_ms) {} | |
| 75 Action action; | |
| 76 int started_gcs; | |
| 77 double next_gc_start_ms; | |
| 78 }; | |
| 79 | |
| 80 enum EventType { | |
| 81 kTimer, | |
| 82 kMarkCompact, | |
| 83 kScavenge, | |
| 84 kContextDisposed, | |
| 85 }; | |
| 86 | |
| 87 struct Event { | |
| 88 EventType type; | |
| 89 double time_ms; | |
| 90 bool low_allocation_rate; | |
| 91 bool high_fragmentation; | |
| 92 bool next_gc_likely_to_collect_more; | |
| 93 bool incremental_gc_in_progress; | |
| 94 }; | |
| 95 | |
| 96 explicit CleanupGC(Heap* heap) : heap_(heap), state_(kDone, 0, 0.0) {} | |
| 97 // Callbacks. | |
| 98 void NotifyTimer(const Event& event); | |
| 99 void NotifyMarkCompact(const Event& event); | |
| 100 void NotifyScavenge(const Event& event); | |
| 101 void NotifyContextDisposed(const Event& event); | |
| 102 // The step function that computes the next state from the current state and | |
| 103 // the incoming event. | |
| 104 static State Step(const State& state, const Event& event); | |
| 105 // Posts a timer task that will call NotifyTimer after the given delay. | |
| 106 void ScheduleTimer(double delay_ms); | |
| 107 | |
| 108 static const int kLongDelayMs; | |
| 109 static const int kShortDelayMs; | |
| 110 static const int kMaxNumberOfGCs; | |
| 111 | |
| 112 Heap* heap() { return heap_; } | |
| 113 | |
| 114 private: | |
| 115 class TimerTask : public v8::Task { | |
| 116 public: | |
| 117 explicit TimerTask(CleanupGC* cleanup_gc) : cleanup_gc_(cleanup_gc) {} | |
| 118 virtual ~TimerTask() {} | |
| 119 | |
| 120 private: | |
| 121 // v8::Task overrides. | |
| 122 void Run() override; | |
| 123 CleanupGC* cleanup_gc_; | |
| 124 DISALLOW_COPY_AND_ASSIGN(TimerTask); | |
| 125 }; | |
| 126 | |
| 127 Heap* heap_; | |
| 128 State state_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(CleanupGC); | |
| 131 }; | |
| 132 | |
| 133 } // namespace internal | |
| 134 } // namespace v8 | |
| 135 | |
| 136 #endif // V8_HEAP_CLEANUP_GC_H | |
| OLD | NEW |