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.
5 #include "src/heap/gc-idle-time-handler.h"
6 #include "src/heap/gc-tracer.h"
12 const double GCIdleTimeHandler::kConservativeTimeRatio = 0.9;
13 const size_t GCIdleTimeHandler::kMaxMarkCompactTimeInMs = 1000;
14 const size_t GCIdleTimeHandler::kMinTimeForFinalizeSweeping = 100;
15 const int GCIdleTimeHandler::kMaxMarkCompactsInIdleRound = 7;
16 const int GCIdleTimeHandler::kIdleScavengeThreshold = 5;
19 void GCIdleTimeAction::Print() {
27 case DO_INCREMENTAL_MARKING:
28 PrintF("incremental marking with step %" V8_PTR_PREFIX "d", parameter);
36 case DO_FINALIZE_SWEEPING:
37 PrintF("finalize sweeping");
43 size_t GCIdleTimeHandler::EstimateMarkingStepSize(
44 size_t idle_time_in_ms, size_t marking_speed_in_bytes_per_ms) {
45 DCHECK(idle_time_in_ms > 0);
47 if (marking_speed_in_bytes_per_ms == 0) {
48 marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed;
51 size_t marking_step_size = marking_speed_in_bytes_per_ms * idle_time_in_ms;
52 if (marking_step_size / marking_speed_in_bytes_per_ms != idle_time_in_ms) {
53 // In the case of an overflow we return maximum marking step size.
54 return kMaximumMarkingStepSize;
57 if (marking_step_size > kMaximumMarkingStepSize)
58 return kMaximumMarkingStepSize;
60 return static_cast<size_t>(marking_step_size * kConservativeTimeRatio);
64 size_t GCIdleTimeHandler::EstimateMarkCompactTime(
65 size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms) {
66 if (mark_compact_speed_in_bytes_per_ms == 0) {
67 mark_compact_speed_in_bytes_per_ms = kInitialConservativeMarkCompactSpeed;
69 size_t result = size_of_objects / mark_compact_speed_in_bytes_per_ms;
70 return Min(result, kMaxMarkCompactTimeInMs);
74 size_t GCIdleTimeHandler::EstimateScavengeTime(
75 size_t new_space_size, size_t scavenge_speed_in_bytes_per_ms) {
76 if (scavenge_speed_in_bytes_per_ms == 0) {
77 scavenge_speed_in_bytes_per_ms = kInitialConservativeScavengeSpeed;
79 return new_space_size / scavenge_speed_in_bytes_per_ms;
83 bool GCIdleTimeHandler::ScavangeMayHappenSoon(
84 size_t available_new_space_memory,
85 size_t new_space_allocation_throughput_in_bytes_per_ms) {
86 if (available_new_space_memory <=
87 new_space_allocation_throughput_in_bytes_per_ms *
88 kMaxFrameRenderingIdleTime) {
95 // The following logic is implemented by the controller:
96 // (1) If the new space is almost full and we can effort a Scavenge, then a
97 // Scavenge is performed.
98 // (2) If there is currently no MarkCompact idle round going on, we start a
99 // new idle round if enough garbage was created or we received a context
100 // disposal event. Otherwise we do not perform garbage collection to keep
101 // system utilization low.
102 // (3) If incremental marking is done, we perform a full garbage collection
103 // if context was disposed or if we are allowed to still do full garbage
104 // collections during this idle round or if we are not allowed to start
105 // incremental marking. Otherwise we do not perform garbage collection to
106 // keep system utilization low.
107 // (4) If sweeping is in progress and we received a large enough idle time
108 // request, we finalize sweeping here.
109 // (5) If incremental marking is in progress, we perform a marking step. Note,
110 // that this currently may trigger a full garbage collection.
111 GCIdleTimeAction GCIdleTimeHandler::Compute(size_t idle_time_in_ms,
112 HeapState heap_state) {
113 if (idle_time_in_ms <= kMaxFrameRenderingIdleTime &&
114 ScavangeMayHappenSoon(
115 heap_state.available_new_space_memory,
116 heap_state.new_space_allocation_throughput_in_bytes_per_ms) &&
118 EstimateScavengeTime(heap_state.new_space_capacity,
119 heap_state.scavenge_speed_in_bytes_per_ms)) {
120 return GCIdleTimeAction::Scavenge();
122 if (IsMarkCompactIdleRoundFinished()) {
123 if (EnoughGarbageSinceLastIdleRound() || heap_state.contexts_disposed > 0) {
126 return GCIdleTimeAction::Done();
130 if (idle_time_in_ms == 0) {
131 return GCIdleTimeAction::Nothing();
134 if (heap_state.incremental_marking_stopped) {
135 size_t estimated_time_in_ms =
136 EstimateMarkCompactTime(heap_state.size_of_objects,
137 heap_state.mark_compact_speed_in_bytes_per_ms);
138 if (idle_time_in_ms >= estimated_time_in_ms ||
139 (heap_state.size_of_objects < kSmallHeapSize &&
140 heap_state.contexts_disposed > 0)) {
141 // If there are no more than two GCs left in this idle round and we are
142 // allowed to do a full GC, then make those GCs full in order to compact
144 // TODO(ulan): Once we enable code compaction for incremental marking, we
145 // can get rid of this special case and always start incremental marking.
146 int remaining_mark_sweeps =
147 kMaxMarkCompactsInIdleRound - mark_compacts_since_idle_round_started_;
148 if (heap_state.contexts_disposed > 0 ||
149 (idle_time_in_ms > kMaxFrameRenderingIdleTime &&
150 (remaining_mark_sweeps <= 2 ||
151 !heap_state.can_start_incremental_marking))) {
152 return GCIdleTimeAction::FullGC();
155 if (!heap_state.can_start_incremental_marking) {
156 return GCIdleTimeAction::Nothing();
159 // TODO(hpayer): Estimate finalize sweeping time.
160 if (heap_state.sweeping_in_progress &&
161 idle_time_in_ms >= kMinTimeForFinalizeSweeping) {
162 return GCIdleTimeAction::FinalizeSweeping();
165 if (heap_state.incremental_marking_stopped &&
166 !heap_state.can_start_incremental_marking) {
167 return GCIdleTimeAction::Nothing();
169 size_t step_size = EstimateMarkingStepSize(
170 idle_time_in_ms, heap_state.incremental_marking_speed_in_bytes_per_ms);
171 return GCIdleTimeAction::IncrementalMarking(step_size);