V8 Project
gc-idle-time-handler.cc
Go to the documentation of this file.
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 
6 #include "src/heap/gc-tracer.h"
7 #include "src/utils.h"
8 
9 namespace v8 {
10 namespace internal {
11 
17 
18 
20  switch (type) {
21  case DONE:
22  PrintF("done");
23  break;
24  case DO_NOTHING:
25  PrintF("no action");
26  break;
28  PrintF("incremental marking with step %" V8_PTR_PREFIX "d", parameter);
29  break;
30  case DO_SCAVENGE:
31  PrintF("scavenge");
32  break;
33  case DO_FULL_GC:
34  PrintF("full GC");
35  break;
37  PrintF("finalize sweeping");
38  break;
39  }
40 }
41 
42 
44  size_t idle_time_in_ms, size_t marking_speed_in_bytes_per_ms) {
45  DCHECK(idle_time_in_ms > 0);
46 
47  if (marking_speed_in_bytes_per_ms == 0) {
48  marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed;
49  }
50 
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.
55  }
56 
57  if (marking_step_size > kMaximumMarkingStepSize)
59 
60  return static_cast<size_t>(marking_step_size * kConservativeTimeRatio);
61 }
62 
63 
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;
68  }
69  size_t result = size_of_objects / mark_compact_speed_in_bytes_per_ms;
70  return Min(result, kMaxMarkCompactTimeInMs);
71 }
72 
73 
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;
78  }
79  return new_space_size / scavenge_speed_in_bytes_per_ms;
80 }
81 
82 
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 *
89  return true;
90  }
91  return false;
92 }
93 
94 
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.
112  HeapState heap_state) {
113  if (idle_time_in_ms <= kMaxFrameRenderingIdleTime &&
115  heap_state.available_new_space_memory,
117  idle_time_in_ms >=
119  heap_state.scavenge_speed_in_bytes_per_ms)) {
121  }
123  if (EnoughGarbageSinceLastIdleRound() || heap_state.contexts_disposed > 0) {
124  StartIdleRound();
125  } else {
126  return GCIdleTimeAction::Done();
127  }
128  }
129 
130  if (idle_time_in_ms == 0) {
131  return GCIdleTimeAction::Nothing();
132  }
133 
134  if (heap_state.incremental_marking_stopped) {
135  size_t estimated_time_in_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
143  // the code space.
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 =
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();
153  }
154  }
155  if (!heap_state.can_start_incremental_marking) {
156  return GCIdleTimeAction::Nothing();
157  }
158  }
159  // TODO(hpayer): Estimate finalize sweeping time.
160  if (heap_state.sweeping_in_progress &&
161  idle_time_in_ms >= kMinTimeForFinalizeSweeping) {
163  }
164 
165  if (heap_state.incremental_marking_stopped &&
166  !heap_state.can_start_incremental_marking) {
167  return GCIdleTimeAction::Nothing();
168  }
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);
172 }
173 }
174 }
static GCIdleTimeAction Done()
static GCIdleTimeAction Scavenge()
static GCIdleTimeAction FinalizeSweeping()
static GCIdleTimeAction FullGC()
static GCIdleTimeAction Nothing()
static GCIdleTimeAction IncrementalMarking(intptr_t step_size)
static size_t EstimateScavengeTime(size_t new_space_size, size_t scavenger_speed_in_bytes_per_ms)
static const double kConservativeTimeRatio
static const size_t kInitialConservativeMarkingSpeed
static const size_t kInitialConservativeMarkCompactSpeed
static const size_t kMinTimeForFinalizeSweeping
static const size_t kMaxFrameRenderingIdleTime
GCIdleTimeAction Compute(size_t idle_time_in_ms, HeapState heap_state)
static const size_t kMaxMarkCompactTimeInMs
static const size_t kInitialConservativeScavengeSpeed
static size_t EstimateMarkCompactTime(size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms)
static const size_t kMaximumMarkingStepSize
static bool ScavangeMayHappenSoon(size_t available_new_space_memory, size_t new_space_allocation_throughput_in_bytes_per_ms)
static size_t EstimateMarkingStepSize(size_t idle_time_in_ms, size_t marking_speed_in_bytes_per_ms)
#define DCHECK(condition)
Definition: logging.h:205
#define V8_PTR_PREFIX
Definition: macros.h:360
static LifetimePosition Min(LifetimePosition a, LifetimePosition b)
void PrintF(const char *format,...)
Definition: utils.cc:80
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20