Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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_GC_IDLE_TIME_HANDLER_H_ |
| 6 | #define V8_HEAP_GC_IDLE_TIME_HANDLER_H_ |
| 7 | |
| 8 | #include "src/globals.h" |
| 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
| 13 | enum GCIdleTimeActionType { |
| 14 | DONE, |
| 15 | DO_NOTHING, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | DO_INCREMENTAL_STEP, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | DO_FULL_GC, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | |
| 21 | class GCIdleTimeAction { |
| 22 | public: |
| 23 | static GCIdleTimeAction Done() { |
| 24 | GCIdleTimeAction result; |
| 25 | result.type = DONE; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 26 | result.additional_work = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | return result; |
| 28 | } |
| 29 | |
| 30 | static GCIdleTimeAction Nothing() { |
| 31 | GCIdleTimeAction result; |
| 32 | result.type = DO_NOTHING; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 33 | result.additional_work = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | return result; |
| 35 | } |
| 36 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | static GCIdleTimeAction IncrementalStep() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | GCIdleTimeAction result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 39 | result.type = DO_INCREMENTAL_STEP; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 40 | result.additional_work = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | return result; |
| 42 | } |
| 43 | |
| 44 | static GCIdleTimeAction FullGC() { |
| 45 | GCIdleTimeAction result; |
| 46 | result.type = DO_FULL_GC; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 47 | result.additional_work = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | return result; |
| 49 | } |
| 50 | |
| 51 | void Print(); |
| 52 | |
| 53 | GCIdleTimeActionType type; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 54 | bool additional_work; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | class GCIdleTimeHeapState { |
| 59 | public: |
| 60 | void Print(); |
| 61 | |
| 62 | int contexts_disposed; |
| 63 | double contexts_disposal_rate; |
| 64 | size_t size_of_objects; |
| 65 | bool incremental_marking_stopped; |
| 66 | }; |
| 67 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | |
| 69 | // The idle time handler makes decisions about which garbage collection |
| 70 | // operations are executing during IdleNotification. |
| 71 | class GCIdleTimeHandler { |
| 72 | public: |
| 73 | // If we haven't recorded any incremental marking events yet, we carefully |
| 74 | // mark with a conservative lower bound for the marking speed. |
| 75 | static const size_t kInitialConservativeMarkingSpeed = 100 * KB; |
| 76 | |
| 77 | // Maximum marking step size returned by EstimateMarkingStepSize. |
| 78 | static const size_t kMaximumMarkingStepSize = 700 * MB; |
| 79 | |
| 80 | // We have to make sure that we finish the IdleNotification before |
| 81 | // idle_time_in_ms. Hence, we conservatively prune our workload estimate. |
| 82 | static const double kConservativeTimeRatio; |
| 83 | |
| 84 | // If we haven't recorded any mark-compact events yet, we use |
| 85 | // conservative lower bound for the mark-compact speed. |
| 86 | static const size_t kInitialConservativeMarkCompactSpeed = 2 * MB; |
| 87 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 88 | // If we haven't recorded any final incremental mark-compact events yet, we |
| 89 | // use conservative lower bound for the mark-compact speed. |
| 90 | static const size_t kInitialConservativeFinalIncrementalMarkCompactSpeed = |
| 91 | 2 * MB; |
| 92 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 93 | // Maximum final incremental mark-compact time returned by |
| 94 | // EstimateFinalIncrementalMarkCompactTime. |
| 95 | static const size_t kMaxFinalIncrementalMarkCompactTimeInMs; |
| 96 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | // This is the maximum scheduled idle time. Note that it can be more than |
| 98 | // 16.66 ms when there is currently no rendering going on. |
| 99 | static const size_t kMaxScheduledIdleTime = 50; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 101 | // The maximum idle time when frames are rendered is 16.66ms. |
| 102 | static const size_t kMaxFrameRenderingIdleTime = 17; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 104 | static const int kMinBackgroundIdleTime = 900; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 105 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 106 | // An allocation throughput below kLowAllocationThroughput bytes/ms is |
| 107 | // considered low |
| 108 | static const size_t kLowAllocationThroughput = 1000; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 110 | // If contexts are disposed at a higher rate a full gc is triggered. |
| 111 | static const double kHighContextDisposalRate; |
| 112 | |
| 113 | // Incremental marking step time. |
| 114 | static const size_t kIncrementalMarkingStepTimeInMs = 1; |
| 115 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | static const size_t kMinTimeForOverApproximatingWeakClosureInMs; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 117 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | // Number of times we will return a Nothing action in the current mode |
| 119 | // despite having idle time available before we returning a Done action to |
| 120 | // ensure we don't keep scheduling idle tasks and making no progress. |
| 121 | static const int kMaxNoProgressIdleTimes = 10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 122 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | GCIdleTimeHandler() : idle_times_which_made_no_progress_(0) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 124 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | GCIdleTimeAction Compute(double idle_time_in_ms, |
| 126 | GCIdleTimeHeapState heap_state); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 127 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | void ResetNoProgressCounter() { idle_times_which_made_no_progress_ = 0; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 130 | static size_t EstimateMarkingStepSize(double idle_time_in_ms, |
| 131 | double marking_speed_in_bytes_per_ms); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 133 | static double EstimateFinalIncrementalMarkCompactTime( |
| 134 | size_t size_of_objects, double mark_compact_speed_in_bytes_per_ms); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 135 | |
| 136 | static bool ShouldDoContextDisposalMarkCompact(int context_disposed, |
| 137 | double contexts_disposal_rate); |
| 138 | |
| 139 | static bool ShouldDoFinalIncrementalMarkCompact( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 140 | double idle_time_in_ms, size_t size_of_objects, |
| 141 | double final_incremental_mark_compact_speed_in_bytes_per_ms); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 142 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 143 | static bool ShouldDoOverApproximateWeakClosure(double idle_time_in_ms); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | |
| 145 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 146 | GCIdleTimeAction NothingOrDone(double idle_time_in_ms); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 148 | // Idle notifications with no progress. |
| 149 | int idle_times_which_made_no_progress_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(GCIdleTimeHandler); |
| 152 | }; |
| 153 | |
| 154 | } // namespace internal |
| 155 | } // namespace v8 |
| 156 | |
| 157 | #endif // V8_HEAP_GC_IDLE_TIME_HANDLER_H_ |