| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 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 #ifndef V8_HEAP_GC_IDLE_TIME_HANDLER_H_ | 5 #ifndef V8_HEAP_GC_IDLE_TIME_HANDLER_H_ |
| 6 #define V8_HEAP_GC_IDLE_TIME_HANDLER_H_ | 6 #define V8_HEAP_GC_IDLE_TIME_HANDLER_H_ |
| 7 | 7 |
| 8 #include "src/globals.h" | 8 #include "src/globals.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| 11 namespace internal { | 11 namespace internal { |
| 12 | 12 |
| 13 enum GCIdleTimeActionType { | 13 enum GCIdleTimeActionType { |
| 14 DONE, | 14 DONE, |
| 15 DO_NOTHING, | 15 DO_NOTHING, |
| 16 DO_INCREMENTAL_MARKING, | 16 DO_INCREMENTAL_MARKING, |
| 17 DO_SCAVENGE, | 17 DO_SCAVENGE, |
| 18 DO_FULL_GC, | 18 DO_FULL_GC, |
| 19 DO_FINALIZE_SWEEPING | 19 DO_FINALIZE_SWEEPING |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 | 22 |
| 23 class GCIdleTimeAction { | 23 class GCIdleTimeAction { |
| 24 public: | 24 public: |
| 25 static GCIdleTimeAction Done() { | 25 static GCIdleTimeAction Done() { |
| 26 GCIdleTimeAction result; | 26 GCIdleTimeAction result; |
| 27 result.type = DONE; | 27 result.type = DONE; |
| 28 result.parameter = 0; | 28 result.parameter = 0; |
| 29 result.additional_work = false; | 29 result.additional_work = false; |
| 30 result.reduce_memory = false; |
| 30 return result; | 31 return result; |
| 31 } | 32 } |
| 32 | 33 |
| 33 static GCIdleTimeAction Nothing() { | 34 static GCIdleTimeAction Nothing() { |
| 34 GCIdleTimeAction result; | 35 GCIdleTimeAction result; |
| 35 result.type = DO_NOTHING; | 36 result.type = DO_NOTHING; |
| 36 result.parameter = 0; | 37 result.parameter = 0; |
| 37 result.additional_work = false; | 38 result.additional_work = false; |
| 39 result.reduce_memory = false; |
| 38 return result; | 40 return result; |
| 39 } | 41 } |
| 40 | 42 |
| 41 static GCIdleTimeAction IncrementalMarking(intptr_t step_size) { | 43 static GCIdleTimeAction IncrementalMarking(intptr_t step_size, |
| 44 bool reduce_memory) { |
| 42 GCIdleTimeAction result; | 45 GCIdleTimeAction result; |
| 43 result.type = DO_INCREMENTAL_MARKING; | 46 result.type = DO_INCREMENTAL_MARKING; |
| 44 result.parameter = step_size; | 47 result.parameter = step_size; |
| 45 result.additional_work = false; | 48 result.additional_work = false; |
| 49 result.reduce_memory = reduce_memory; |
| 46 return result; | 50 return result; |
| 47 } | 51 } |
| 48 | 52 |
| 49 static GCIdleTimeAction Scavenge() { | 53 static GCIdleTimeAction Scavenge() { |
| 50 GCIdleTimeAction result; | 54 GCIdleTimeAction result; |
| 51 result.type = DO_SCAVENGE; | 55 result.type = DO_SCAVENGE; |
| 52 result.parameter = 0; | 56 result.parameter = 0; |
| 53 result.additional_work = false; | 57 result.additional_work = false; |
| 58 // TODO(ulan): add reduce_memory argument and shrink new space size if |
| 59 // reduce_memory = true. |
| 60 result.reduce_memory = false; |
| 54 return result; | 61 return result; |
| 55 } | 62 } |
| 56 | 63 |
| 57 static GCIdleTimeAction FullGC() { | 64 static GCIdleTimeAction FullGC(bool reduce_memory) { |
| 58 GCIdleTimeAction result; | 65 GCIdleTimeAction result; |
| 59 result.type = DO_FULL_GC; | 66 result.type = DO_FULL_GC; |
| 60 result.parameter = 0; | 67 result.parameter = 0; |
| 61 result.additional_work = false; | 68 result.additional_work = false; |
| 69 result.reduce_memory = reduce_memory; |
| 62 return result; | 70 return result; |
| 63 } | 71 } |
| 64 | 72 |
| 65 static GCIdleTimeAction FinalizeSweeping() { | 73 static GCIdleTimeAction FinalizeSweeping() { |
| 66 GCIdleTimeAction result; | 74 GCIdleTimeAction result; |
| 67 result.type = DO_FINALIZE_SWEEPING; | 75 result.type = DO_FINALIZE_SWEEPING; |
| 68 result.parameter = 0; | 76 result.parameter = 0; |
| 69 result.additional_work = false; | 77 result.additional_work = false; |
| 78 result.reduce_memory = false; |
| 70 return result; | 79 return result; |
| 71 } | 80 } |
| 72 | 81 |
| 73 void Print(); | 82 void Print(); |
| 74 | 83 |
| 75 GCIdleTimeActionType type; | 84 GCIdleTimeActionType type; |
| 76 intptr_t parameter; | 85 intptr_t parameter; |
| 77 bool additional_work; | 86 bool additional_work; |
| 87 bool reduce_memory; |
| 78 }; | 88 }; |
| 79 | 89 |
| 80 | 90 |
| 81 class GCTracer; | 91 class GCTracer; |
| 82 | 92 |
| 83 // The idle time handler makes decisions about which garbage collection | 93 // The idle time handler makes decisions about which garbage collection |
| 84 // operations are executing during IdleNotification. | 94 // operations are executing during IdleNotification. |
| 85 class GCIdleTimeHandler { | 95 class GCIdleTimeHandler { |
| 86 public: | 96 public: |
| 87 // If we haven't recorded any incremental marking events yet, we carefully | 97 // If we haven't recorded any incremental marking events yet, we carefully |
| (...skipping 23 matching lines...) Expand all Loading... |
| 111 // EstimateFinalIncrementalMarkCompactTime. | 121 // EstimateFinalIncrementalMarkCompactTime. |
| 112 static const size_t kMaxFinalIncrementalMarkCompactTimeInMs; | 122 static const size_t kMaxFinalIncrementalMarkCompactTimeInMs; |
| 113 | 123 |
| 114 // This is the maximum scheduled idle time. Note that it can be more than | 124 // This is the maximum scheduled idle time. Note that it can be more than |
| 115 // 16.66 ms when there is currently no rendering going on. | 125 // 16.66 ms when there is currently no rendering going on. |
| 116 static const size_t kMaxScheduledIdleTime = 50; | 126 static const size_t kMaxScheduledIdleTime = 50; |
| 117 | 127 |
| 118 // The maximum idle time when frames are rendered is 16.66ms. | 128 // The maximum idle time when frames are rendered is 16.66ms. |
| 119 static const size_t kMaxFrameRenderingIdleTime = 17; | 129 static const size_t kMaxFrameRenderingIdleTime = 17; |
| 120 | 130 |
| 121 static const int kMinBackgroundIdleTime = 900; | |
| 122 | |
| 123 // We conservatively assume that in the next kTimeUntilNextIdleEvent ms | 131 // We conservatively assume that in the next kTimeUntilNextIdleEvent ms |
| 124 // no idle notification happens. | 132 // no idle notification happens. |
| 125 static const size_t kTimeUntilNextIdleEvent = 100; | 133 static const size_t kTimeUntilNextIdleEvent = 100; |
| 126 | 134 |
| 127 // If we haven't recorded any scavenger events yet, we use a conservative | 135 // If we haven't recorded any scavenger events yet, we use a conservative |
| 128 // lower bound for the scavenger speed. | 136 // lower bound for the scavenger speed. |
| 129 static const size_t kInitialConservativeScavengeSpeed = 100 * KB; | 137 static const size_t kInitialConservativeScavengeSpeed = 100 * KB; |
| 130 | 138 |
| 131 // The minimum size of allocated new space objects to trigger a scavenge. | 139 // The minimum size of allocated new space objects to trigger a scavenge. |
| 132 static const size_t kMinimumNewSpaceSizeToPerformScavenge = MB / 2; | 140 static const size_t kMinimumNewSpaceSizeToPerformScavenge = MB / 2; |
| 133 | 141 |
| 134 // If contexts are disposed at a higher rate a full gc is triggered. | 142 // If contexts are disposed at a higher rate a full gc is triggered. |
| 135 static const double kHighContextDisposalRate; | 143 static const double kHighContextDisposalRate; |
| 136 | 144 |
| 137 // Incremental marking step time. | 145 // Incremental marking step time. |
| 138 static const size_t kIncrementalMarkingStepTimeInMs = 1; | 146 static const size_t kIncrementalMarkingStepTimeInMs = 1; |
| 139 | 147 |
| 140 static const size_t kMinTimeForOverApproximatingWeakClosureInMs; | 148 static const size_t kMinTimeForOverApproximatingWeakClosureInMs; |
| 141 | 149 |
| 150 // The number of idle MarkCompact GCs to perform before transitioning to |
| 151 // the kDone mode. |
| 152 static const int kMaxIdleMarkCompacts = 3; |
| 153 |
| 154 // The number of mutator MarkCompact GCs before transitioning to the |
| 155 // kReduceLatency mode. |
| 156 static const int kMarkCompactsBeforeMutatorIsActive = 1; |
| 157 |
| 158 // Mutator is considered idle if |
| 159 // 1) there are N idle notification with time >= kMinBackgroundIdleTime, |
| 160 // 2) or there are M idle notifications with time >= kMinLongIdleTime |
| 161 // without any mutator GC in between. |
| 162 // Where N = kBackgroundIdleNotificationsBeforeMutatorIsIdle, |
| 163 // M = kLongIdleNotificationsBeforeMutatorIsIdle |
| 164 static const int kMinLongIdleTime = kMaxFrameRenderingIdleTime + 1; |
| 165 static const int kMinBackgroundIdleTime = 900; |
| 166 static const int kBackgroundIdleNotificationsBeforeMutatorIsIdle = 2; |
| 167 static const int kLongIdleNotificationsBeforeMutatorIsIdle = 50; |
| 142 // Number of times we will return a Nothing action in the current mode | 168 // Number of times we will return a Nothing action in the current mode |
| 143 // despite having idle time available before we returning a Done action to | 169 // despite having idle time available before we returning a Done action to |
| 144 // ensure we don't keep scheduling idle tasks and making no progress. | 170 // ensure we don't keep scheduling idle tasks and making no progress. |
| 145 static const int kMaxNoProgressIdleTimes = 10; | 171 static const int kMaxNoProgressIdleTimesPerMode = 10; |
| 146 | 172 |
| 147 class HeapState { | 173 class HeapState { |
| 148 public: | 174 public: |
| 149 void Print(); | 175 void Print(); |
| 150 | 176 |
| 151 int contexts_disposed; | 177 int contexts_disposed; |
| 152 double contexts_disposal_rate; | 178 double contexts_disposal_rate; |
| 153 size_t size_of_objects; | 179 size_t size_of_objects; |
| 154 bool incremental_marking_stopped; | 180 bool incremental_marking_stopped; |
| 181 bool can_start_incremental_marking; |
| 155 bool sweeping_in_progress; | 182 bool sweeping_in_progress; |
| 156 bool sweeping_completed; | 183 bool sweeping_completed; |
| 157 bool has_low_allocation_rate; | 184 bool has_low_allocation_rate; |
| 158 size_t mark_compact_speed_in_bytes_per_ms; | 185 size_t mark_compact_speed_in_bytes_per_ms; |
| 159 size_t incremental_marking_speed_in_bytes_per_ms; | 186 size_t incremental_marking_speed_in_bytes_per_ms; |
| 160 size_t final_incremental_mark_compact_speed_in_bytes_per_ms; | 187 size_t final_incremental_mark_compact_speed_in_bytes_per_ms; |
| 161 size_t scavenge_speed_in_bytes_per_ms; | 188 size_t scavenge_speed_in_bytes_per_ms; |
| 162 size_t used_new_space_size; | 189 size_t used_new_space_size; |
| 163 size_t new_space_capacity; | 190 size_t new_space_capacity; |
| 164 size_t new_space_allocation_throughput_in_bytes_per_ms; | 191 size_t new_space_allocation_throughput_in_bytes_per_ms; |
| 165 }; | 192 }; |
| 166 | 193 |
| 167 GCIdleTimeHandler() : idle_times_which_made_no_progress_(0) {} | 194 GCIdleTimeHandler() |
| 195 : idle_mark_compacts_(0), |
| 196 mark_compacts_(0), |
| 197 scavenges_(0), |
| 198 long_idle_notifications_(0), |
| 199 background_idle_notifications_(0), |
| 200 idle_times_which_made_no_progress_per_mode_(0), |
| 201 next_gc_likely_to_collect_more_(false), |
| 202 mode_(kReduceLatency) {} |
| 168 | 203 |
| 169 GCIdleTimeAction Compute(double idle_time_in_ms, HeapState heap_state); | 204 GCIdleTimeAction Compute(double idle_time_in_ms, HeapState heap_state); |
| 170 | 205 |
| 171 void ResetNoProgressCounter() { idle_times_which_made_no_progress_ = 0; } | 206 void NotifyIdleMarkCompact() { ++idle_mark_compacts_; } |
| 207 |
| 208 void NotifyMarkCompact(bool next_gc_likely_to_collect_more) { |
| 209 next_gc_likely_to_collect_more_ = next_gc_likely_to_collect_more; |
| 210 ++mark_compacts_; |
| 211 } |
| 212 |
| 213 void NotifyScavenge() { ++scavenges_; } |
| 172 | 214 |
| 173 static size_t EstimateMarkingStepSize(size_t idle_time_in_ms, | 215 static size_t EstimateMarkingStepSize(size_t idle_time_in_ms, |
| 174 size_t marking_speed_in_bytes_per_ms); | 216 size_t marking_speed_in_bytes_per_ms); |
| 175 | 217 |
| 176 static size_t EstimateMarkCompactTime( | 218 static size_t EstimateMarkCompactTime( |
| 177 size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms); | 219 size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms); |
| 178 | 220 |
| 179 static size_t EstimateFinalIncrementalMarkCompactTime( | 221 static size_t EstimateFinalIncrementalMarkCompactTime( |
| 180 size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms); | 222 size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms); |
| 181 | 223 |
| 182 static bool ShouldDoMarkCompact(size_t idle_time_in_ms, | 224 static bool ShouldDoMarkCompact(size_t idle_time_in_ms, |
| 183 size_t size_of_objects, | 225 size_t size_of_objects, |
| 184 size_t mark_compact_speed_in_bytes_per_ms); | 226 size_t mark_compact_speed_in_bytes_per_ms); |
| 185 | 227 |
| 186 static bool ShouldDoContextDisposalMarkCompact(int context_disposed, | 228 static bool ShouldDoContextDisposalMarkCompact(int context_disposed, |
| 187 double contexts_disposal_rate); | 229 double contexts_disposal_rate); |
| 188 | 230 |
| 189 static bool ShouldDoFinalIncrementalMarkCompact( | 231 static bool ShouldDoFinalIncrementalMarkCompact( |
| 190 size_t idle_time_in_ms, size_t size_of_objects, | 232 size_t idle_time_in_ms, size_t size_of_objects, |
| 191 size_t final_incremental_mark_compact_speed_in_bytes_per_ms); | 233 size_t final_incremental_mark_compact_speed_in_bytes_per_ms); |
| 192 | 234 |
| 193 static bool ShouldDoOverApproximateWeakClosure(size_t idle_time_in_ms); | 235 static bool ShouldDoOverApproximateWeakClosure(size_t idle_time_in_ms); |
| 194 | 236 |
| 195 static bool ShouldDoScavenge( | 237 static bool ShouldDoScavenge( |
| 196 size_t idle_time_in_ms, size_t new_space_size, size_t used_new_space_size, | 238 size_t idle_time_in_ms, size_t new_space_size, size_t used_new_space_size, |
| 197 size_t scavenger_speed_in_bytes_per_ms, | 239 size_t scavenger_speed_in_bytes_per_ms, |
| 198 size_t new_space_allocation_throughput_in_bytes_per_ms); | 240 size_t new_space_allocation_throughput_in_bytes_per_ms); |
| 199 | 241 |
| 242 enum Mode { kReduceLatency, kReduceMemory, kDone }; |
| 243 |
| 244 Mode mode() { return mode_; } |
| 245 |
| 200 private: | 246 private: |
| 247 bool IsMutatorActive(int contexts_disposed, int gcs); |
| 248 bool IsMutatorIdle(int long_idle_notifications, |
| 249 int background_idle_notifications, int gcs); |
| 250 void UpdateCounters(double idle_time_in_ms); |
| 251 void ResetCounters(); |
| 252 Mode NextMode(const HeapState& heap_state); |
| 253 GCIdleTimeAction Action(double idle_time_in_ms, const HeapState& heap_state, |
| 254 bool reduce_memory); |
| 201 GCIdleTimeAction NothingOrDone(); | 255 GCIdleTimeAction NothingOrDone(); |
| 202 | 256 |
| 203 // Idle notifications with no progress. | 257 int idle_mark_compacts_; |
| 204 int idle_times_which_made_no_progress_; | 258 int mark_compacts_; |
| 259 int scavenges_; |
| 260 // The number of long idle notifications with no GC happening |
| 261 // between the notifications. |
| 262 int long_idle_notifications_; |
| 263 // The number of background idle notifications with no GC happening |
| 264 // between the notifications. |
| 265 int background_idle_notifications_; |
| 266 // Idle notifications with no progress in the current mode. |
| 267 int idle_times_which_made_no_progress_per_mode_; |
| 268 |
| 269 bool next_gc_likely_to_collect_more_; |
| 270 |
| 271 Mode mode_; |
| 205 | 272 |
| 206 DISALLOW_COPY_AND_ASSIGN(GCIdleTimeHandler); | 273 DISALLOW_COPY_AND_ASSIGN(GCIdleTimeHandler); |
| 207 }; | 274 }; |
| 208 | 275 |
| 209 } // namespace internal | 276 } // namespace internal |
| 210 } // namespace v8 | 277 } // namespace v8 |
| 211 | 278 |
| 212 #endif // V8_HEAP_GC_IDLE_TIME_HANDLER_H_ | 279 #endif // V8_HEAP_GC_IDLE_TIME_HANDLER_H_ |
| OLD | NEW |