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 | #include "src/heap/gc-idle-time-handler.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
| 7 | #include "src/flags.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/heap/gc-tracer.h" |
| 9 | #include "src/utils.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
| 14 | const double GCIdleTimeHandler::kConservativeTimeRatio = 0.9; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15 | const size_t GCIdleTimeHandler::kMaxFinalIncrementalMarkCompactTimeInMs = 1000; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16 | const double GCIdleTimeHandler::kHighContextDisposalRate = 100; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | const size_t GCIdleTimeHandler::kMinTimeForOverApproximatingWeakClosureInMs = 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | void GCIdleTimeAction::Print() { |
| 21 | switch (type) { |
| 22 | case DONE: |
| 23 | PrintF("done"); |
| 24 | break; |
| 25 | case DO_NOTHING: |
| 26 | PrintF("no action"); |
| 27 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | case DO_INCREMENTAL_STEP: |
| 29 | PrintF("incremental step"); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 30 | if (additional_work) { |
| 31 | PrintF("; finalized marking"); |
| 32 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | case DO_FULL_GC: |
| 35 | PrintF("full GC"); |
| 36 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | void GCIdleTimeHeapState::Print() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 42 | PrintF("contexts_disposed=%d ", contexts_disposed); |
| 43 | PrintF("contexts_disposal_rate=%f ", contexts_disposal_rate); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 44 | PrintF("size_of_objects=%" PRIuS " ", size_of_objects); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 45 | PrintF("incremental_marking_stopped=%d ", incremental_marking_stopped); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | size_t GCIdleTimeHandler::EstimateMarkingStepSize( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 49 | double idle_time_in_ms, double marking_speed_in_bytes_per_ms) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | DCHECK(idle_time_in_ms > 0); |
| 51 | |
| 52 | if (marking_speed_in_bytes_per_ms == 0) { |
| 53 | marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed; |
| 54 | } |
| 55 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 56 | double marking_step_size = marking_speed_in_bytes_per_ms * idle_time_in_ms; |
| 57 | if (marking_step_size >= kMaximumMarkingStepSize) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | return kMaximumMarkingStepSize; |
| 59 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | return static_cast<size_t>(marking_step_size * kConservativeTimeRatio); |
| 61 | } |
| 62 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 63 | double GCIdleTimeHandler::EstimateFinalIncrementalMarkCompactTime( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 64 | size_t size_of_objects, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 65 | double final_incremental_mark_compact_speed_in_bytes_per_ms) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 66 | if (final_incremental_mark_compact_speed_in_bytes_per_ms == 0) { |
| 67 | final_incremental_mark_compact_speed_in_bytes_per_ms = |
| 68 | kInitialConservativeFinalIncrementalMarkCompactSpeed; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 70 | double result = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 71 | size_of_objects / final_incremental_mark_compact_speed_in_bytes_per_ms; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 72 | return Min<double>(result, kMaxFinalIncrementalMarkCompactTimeInMs); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 75 | bool GCIdleTimeHandler::ShouldDoContextDisposalMarkCompact( |
| 76 | int contexts_disposed, double contexts_disposal_rate) { |
| 77 | return contexts_disposed > 0 && contexts_disposal_rate > 0 && |
| 78 | contexts_disposal_rate < kHighContextDisposalRate; |
| 79 | } |
| 80 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 81 | bool GCIdleTimeHandler::ShouldDoFinalIncrementalMarkCompact( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 82 | double idle_time_in_ms, size_t size_of_objects, |
| 83 | double final_incremental_mark_compact_speed_in_bytes_per_ms) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 84 | return idle_time_in_ms >= |
| 85 | EstimateFinalIncrementalMarkCompactTime( |
| 86 | size_of_objects, |
| 87 | final_incremental_mark_compact_speed_in_bytes_per_ms); |
| 88 | } |
| 89 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | bool GCIdleTimeHandler::ShouldDoOverApproximateWeakClosure( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 91 | double idle_time_in_ms) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | // TODO(jochen): Estimate the time it will take to build the object groups. |
| 93 | return idle_time_in_ms >= kMinTimeForOverApproximatingWeakClosureInMs; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | GCIdleTimeAction GCIdleTimeHandler::NothingOrDone(double idle_time_in_ms) { |
| 98 | if (idle_time_in_ms >= kMinBackgroundIdleTime) { |
| 99 | return GCIdleTimeAction::Nothing(); |
| 100 | } |
| 101 | if (idle_times_which_made_no_progress_ >= kMaxNoProgressIdleTimes) { |
| 102 | return GCIdleTimeAction::Done(); |
| 103 | } else { |
| 104 | idle_times_which_made_no_progress_++; |
| 105 | return GCIdleTimeAction::Nothing(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 110 | // The following logic is implemented by the controller: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 111 | // (1) If we don't have any idle time, do nothing, unless a context was |
| 112 | // disposed, incremental marking is stopped, and the heap is small. Then do |
| 113 | // a full GC. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 114 | // (2) If the context disposal rate is high and we cannot perform a full GC, |
| 115 | // we do nothing until the context disposal rate becomes lower. |
| 116 | // (3) If the new space is almost full and we can affort a scavenge or if the |
| 117 | // next scavenge will very likely take long, then a scavenge is performed. |
| 118 | // (4) If sweeping is in progress and we received a large enough idle time |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | // request, we finalize sweeping here. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | // (5) If incremental marking is in progress, we perform a marking step. Note, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | // that this currently may trigger a full garbage collection. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 122 | GCIdleTimeAction GCIdleTimeHandler::Compute(double idle_time_in_ms, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | GCIdleTimeHeapState heap_state) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 124 | if (static_cast<int>(idle_time_in_ms) <= 0) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 125 | if (heap_state.incremental_marking_stopped) { |
| 126 | if (ShouldDoContextDisposalMarkCompact( |
| 127 | heap_state.contexts_disposed, |
| 128 | heap_state.contexts_disposal_rate)) { |
| 129 | return GCIdleTimeAction::FullGC(); |
| 130 | } |
| 131 | } |
| 132 | return GCIdleTimeAction::Nothing(); |
| 133 | } |
| 134 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | // We are in a context disposal GC scenario. Don't do anything if we do not |
| 136 | // get the right idle signal. |
| 137 | if (ShouldDoContextDisposalMarkCompact(heap_state.contexts_disposed, |
| 138 | heap_state.contexts_disposal_rate)) { |
| 139 | return NothingOrDone(idle_time_in_ms); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 141 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 142 | if (!FLAG_incremental_marking || heap_state.incremental_marking_stopped) { |
| 143 | return GCIdleTimeAction::Done(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 146 | return GCIdleTimeAction::IncrementalStep(); |
| 147 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 148 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | |
| 150 | } // namespace internal |
| 151 | } // namespace v8 |