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
10namespace v8 {
11namespace internal {
12
13enum class GCIdleTimeAction : uint8_t {
14 kDone,
15 kIncrementalStep,
16 kFullGC,
17};
18
19class GCIdleTimeHeapState {
20 public:
21 void Print();
22
23 int contexts_disposed;
24 double contexts_disposal_rate;
25 size_t size_of_objects;
26 bool incremental_marking_stopped;
27};
28
29
30// The idle time handler makes decisions about which garbage collection
31// operations are executing during IdleNotification.
32class V8_EXPORT_PRIVATE GCIdleTimeHandler {
33 public:
34 // If we haven't recorded any incremental marking events yet, we carefully
35 // mark with a conservative lower bound for the marking speed.
36 static const size_t kInitialConservativeMarkingSpeed = 100 * KB;
37
38 // Maximum marking step size returned by EstimateMarkingStepSize.
39 static const size_t kMaximumMarkingStepSize = 700 * MB;
40
41 // We have to make sure that we finish the IdleNotification before
42 // idle_time_in_ms. Hence, we conservatively prune our workload estimate.
43 static const double kConservativeTimeRatio;
44
45 // If we haven't recorded any mark-compact events yet, we use
46 // conservative lower bound for the mark-compact speed.
47 static const size_t kInitialConservativeMarkCompactSpeed = 2 * MB;
48
49 // If we haven't recorded any final incremental mark-compact events yet, we
50 // use conservative lower bound for the mark-compact speed.
51 static const size_t kInitialConservativeFinalIncrementalMarkCompactSpeed =
52 2 * MB;
53
54 // Maximum final incremental mark-compact time returned by
55 // EstimateFinalIncrementalMarkCompactTime.
56 static const size_t kMaxFinalIncrementalMarkCompactTimeInMs;
57
58 // This is the maximum scheduled idle time. Note that it can be more than
59 // 16.66 ms when there is currently no rendering going on.
60 static const size_t kMaxScheduledIdleTime = 50;
61
62 // The maximum idle time when frames are rendered is 16.66ms.
63 static const size_t kMaxFrameRenderingIdleTime = 17;
64
65 static const int kMinBackgroundIdleTime = 900;
66
67 // An allocation throughput below kLowAllocationThroughput bytes/ms is
68 // considered low
69 static const size_t kLowAllocationThroughput = 1000;
70
71 static const size_t kMaxHeapSizeForContextDisposalMarkCompact = 100 * MB;
72
73 // If contexts are disposed at a higher rate a full gc is triggered.
74 static const double kHighContextDisposalRate;
75
76 // Incremental marking step time.
77 static const size_t kIncrementalMarkingStepTimeInMs = 1;
78
79 static const size_t kMinTimeForOverApproximatingWeakClosureInMs;
80
81 GCIdleTimeHandler() = default;
82
83 GCIdleTimeAction Compute(double idle_time_in_ms,
84 GCIdleTimeHeapState heap_state);
85
86 bool Enabled();
87
88 static size_t EstimateMarkingStepSize(double idle_time_in_ms,
89 double marking_speed_in_bytes_per_ms);
90
91 static double EstimateFinalIncrementalMarkCompactTime(
92 size_t size_of_objects, double mark_compact_speed_in_bytes_per_ms);
93
94 static bool ShouldDoContextDisposalMarkCompact(int context_disposed,
95 double contexts_disposal_rate,
96 size_t size_of_objects);
97
98 static bool ShouldDoFinalIncrementalMarkCompact(
99 double idle_time_in_ms, size_t size_of_objects,
100 double final_incremental_mark_compact_speed_in_bytes_per_ms);
101
102 static bool ShouldDoOverApproximateWeakClosure(double idle_time_in_ms);
103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(GCIdleTimeHandler);
106};
107
108} // namespace internal
109} // namespace v8
110
111#endif // V8_HEAP_GC_IDLE_TIME_HANDLER_H_
112