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"
6
7 #include "src/flags/flags.h"
8 #include "src/utils/utils.h"
9
10 namespace v8 {
11 namespace internal {
12
13 const double GCIdleTimeHandler::kConservativeTimeRatio = 0.9;
14
Print()15 void GCIdleTimeHeapState::Print() {
16 PrintF("size_of_objects=%zu ", size_of_objects);
17 PrintF("incremental_marking_stopped=%d ", incremental_marking_stopped);
18 }
19
EstimateMarkingStepSize(double idle_time_in_ms,double marking_speed_in_bytes_per_ms)20 size_t GCIdleTimeHandler::EstimateMarkingStepSize(
21 double idle_time_in_ms, double marking_speed_in_bytes_per_ms) {
22 DCHECK_LT(0, idle_time_in_ms);
23
24 if (marking_speed_in_bytes_per_ms == 0) {
25 marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed;
26 }
27
28 double marking_step_size = marking_speed_in_bytes_per_ms * idle_time_in_ms;
29 if (marking_step_size >= kMaximumMarkingStepSize) {
30 return kMaximumMarkingStepSize;
31 }
32 return static_cast<size_t>(marking_step_size * kConservativeTimeRatio);
33 }
34
35 // The following logic is implemented by the controller:
36 // (1) If we don't have any idle time, do nothing, unless a context was
37 // disposed, incremental marking is stopped, and the heap is small. Then do
38 // a full GC.
39 // (2) If the context disposal rate is high and we cannot perform a full GC,
40 // we do nothing until the context disposal rate becomes lower.
41 // (3) If incremental marking is in progress, we perform a marking step.
Compute(double idle_time_in_ms,GCIdleTimeHeapState heap_state)42 GCIdleTimeAction GCIdleTimeHandler::Compute(double idle_time_in_ms,
43 GCIdleTimeHeapState heap_state) {
44 if (static_cast<int>(idle_time_in_ms) <= 0) {
45 return GCIdleTimeAction::kDone;
46 }
47
48 if (FLAG_incremental_marking && !heap_state.incremental_marking_stopped) {
49 return GCIdleTimeAction::kIncrementalStep;
50 }
51
52 return GCIdleTimeAction::kDone;
53 }
54
Enabled()55 bool GCIdleTimeHandler::Enabled() { return FLAG_incremental_marking; }
56
57 } // namespace internal
58 } // namespace v8
59