v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
heap-controller.cc
Go to the documentation of this file.
1// Copyright 2012 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
8#include "src/heap/spaces.h"
10
11namespace v8 {
12namespace internal {
13
14template <typename Trait>
16 Heap* heap, size_t max_heap_size, std::optional<double> gc_speed,
17 double mutator_speed, Heap::HeapGrowingMode growing_mode) {
18 const double max_factor = MaxGrowingFactor(max_heap_size);
19 double factor = DynamicGrowingFactor(gc_speed, mutator_speed, max_factor);
20 switch (growing_mode) {
21 case Heap::HeapGrowingMode::kConservative:
22 case Heap::HeapGrowingMode::kSlow:
23 factor = std::min({factor, Trait::kConservativeGrowingFactor});
24 break;
25 case Heap::HeapGrowingMode::kMinimal:
26 factor = Trait::kMinGrowingFactor;
27 break;
28 case Heap::HeapGrowingMode::kDefault:
29 break;
30 }
31 if (v8_flags.heap_growing_percent > 0) {
32 factor = 1.0 + v8_flags.heap_growing_percent / 100.0;
33 }
34 if (V8_UNLIKELY(v8_flags.trace_gc_verbose)) {
35 Isolate::FromHeap(heap)->PrintWithTimestamp(
36 "[%s] factor %.1f based on mu=%.3f, speed_ratio=%.f "
37 "(gc=%.f, mutator=%.f)\n",
38 Trait::kName, factor, Trait::kTargetMutatorUtilization,
39 gc_speed.value_or(0) / mutator_speed, gc_speed.value_or(0),
40 mutator_speed);
41 }
42 return factor;
43}
44
45template <typename Trait>
46double MemoryController<Trait>::MaxGrowingFactor(size_t max_heap_size) {
47 constexpr double kMinSmallFactor = 1.3;
48 constexpr double kMaxSmallFactor = 2.0;
49 constexpr double kHighFactor = 4.0;
50
51 // If we are on a device with lots of memory, we allow a high heap
52 // growing factor.
53 if (max_heap_size >= Trait::kMaxSize) {
54 return kHighFactor;
55 }
56
57 size_t max_size = std::max({max_heap_size, Trait::kMinSize});
58
59 DCHECK_GE(max_size, Trait::kMinSize);
60 DCHECK_LT(max_size, Trait::kMaxSize);
61
62 // On smaller devices we linearly scale the factor: C+(D-C)*(X-A)/(B-A)
63 double factor = kMinSmallFactor + (kMaxSmallFactor - kMinSmallFactor) *
64 (max_size - Trait::kMinSize) /
65 (Trait::kMaxSize - Trait::kMinSize);
66 return factor;
67}
68
69// Given GC speed in bytes per ms, the allocation throughput in bytes per ms
70// (mutator speed), this function returns the heap growing factor that will
71// achieve the target_mutator_utilization_ if the GC speed and the mutator speed
72// remain the same until the next GC.
73//
74// For a fixed time-frame T = TM + TG, the mutator utilization is the ratio
75// TM / (TM + TG), where TM is the time spent in the mutator and TG is the
76// time spent in the garbage collector.
77//
78// Let MU be target_mutator_utilization_, the desired mutator utilization for
79// the time-frame from the end of the current GC to the end of the next GC.
80// Based on the MU we can compute the heap growing factor F as
81//
82// F = R * (1 - MU) / (R * (1 - MU) - MU), where R = gc_speed / mutator_speed.
83//
84// This formula can be derived as follows.
85//
86// F = Limit / Live by definition, where the Limit is the allocation limit,
87// and the Live is size of live objects.
88// Let’s assume that we already know the Limit. Then:
89// TG = Limit / gc_speed
90// TM = (TM + TG) * MU, by definition of MU.
91// TM = TG * MU / (1 - MU)
92// TM = Limit * MU / (gc_speed * (1 - MU))
93// On the other hand, if the allocation throughput remains constant:
94// Limit = Live + TM * allocation_throughput = Live + TM * mutator_speed
95// Solving it for TM, we get
96// TM = (Limit - Live) / mutator_speed
97// Combining the two equation for TM:
98// (Limit - Live) / mutator_speed = Limit * MU / (gc_speed * (1 - MU))
99// (Limit - Live) = Limit * MU * mutator_speed / (gc_speed * (1 - MU))
100// substitute R = gc_speed / mutator_speed
101// (Limit - Live) = Limit * MU / (R * (1 - MU))
102// substitute F = Limit / Live
103// F - 1 = F * MU / (R * (1 - MU))
104// F - F * MU / (R * (1 - MU)) = 1
105// F * (1 - MU / (R * (1 - MU))) = 1
106// F * (R * (1 - MU) - MU) / (R * (1 - MU)) = 1
107// F = R * (1 - MU) / (R * (1 - MU) - MU)
108template <typename Trait>
110 std::optional<double> gc_speed, double mutator_speed, double max_factor) {
111 DCHECK_LE(Trait::kMinGrowingFactor, max_factor);
112 DCHECK_GE(Trait::kMaxGrowingFactor, max_factor);
113 if (!gc_speed || mutator_speed == 0) return max_factor;
114
115 const double speed_ratio = *gc_speed / mutator_speed;
116
117 const double a = speed_ratio * (1 - Trait::kTargetMutatorUtilization);
118 const double b = speed_ratio * (1 - Trait::kTargetMutatorUtilization) -
119 Trait::kTargetMutatorUtilization;
120
121 // The factor is a / b, but we need to check for small b first.
122 double factor = (a < b * max_factor) ? a / b : max_factor;
123 DCHECK_LE(factor, max_factor);
124 factor = std::max({factor, Trait::kMinGrowingFactor});
125 return factor;
126}
127
128template <typename Trait>
130 Heap::HeapGrowingMode growing_mode) {
131 const size_t kRegularAllocationLimitGrowingStep = 8;
132 const size_t kLowMemoryAllocationLimitGrowingStep = 2;
133 size_t limit = (PageMetadata::kPageSize > MB ? PageMetadata::kPageSize : MB);
134 return limit * (growing_mode == Heap::HeapGrowingMode::kConservative
135 ? kLowMemoryAllocationLimitGrowingStep
136 : kRegularAllocationLimitGrowingStep);
137}
138
139template <typename Trait>
141 Heap* heap, size_t current_size, uint64_t limit, size_t min_size,
142 size_t max_size, size_t new_space_capacity,
143 Heap::HeapGrowingMode growing_mode) {
144 CHECK_LT(0, current_size);
145 limit = std::max(limit, static_cast<uint64_t>(current_size) +
146 MinimumAllocationLimitGrowingStep(growing_mode)) +
147 new_space_capacity;
148 const uint64_t halfway_to_the_max =
149 (static_cast<uint64_t>(current_size) + max_size) / 2;
150 const uint64_t limit_or_halfway =
151 std::min<uint64_t>(limit, halfway_to_the_max);
152 const size_t result =
153 static_cast<size_t>(std::max<uint64_t>(limit_or_halfway, min_size));
154 if (V8_UNLIKELY(v8_flags.trace_gc_verbose)) {
155 Isolate::FromHeap(heap)->PrintWithTimestamp(
156 "[%s] Limit: old size: %zu KB, new limit: %zu KB\n", Trait::kName,
157 current_size / KB, result / KB);
158 }
159 return result;
160}
161
164
165} // namespace internal
166} // namespace v8
static Isolate * FromHeap(const Heap *heap)
Definition isolate.h:1202
static double GrowingFactor(Heap *heap, size_t max_heap_size, std::optional< double > gc_speed, double mutator_speed, Heap::HeapGrowingMode growing_mode)
static double DynamicGrowingFactor(std::optional< double > gc_speed, double mutator_speed, double max_factor)
static size_t MinimumAllocationLimitGrowingStep(Heap::HeapGrowingMode growing_mode)
static size_t BoundAllocationLimit(Heap *heap, size_t current_size, uint64_t limit, size_t min_size, size_t max_size, size_t new_space_capacity, Heap::HeapGrowingMode growing_mode)
static double MaxGrowingFactor(size_t max_heap_size)
ZoneVector< RpoNumber > & result
V8_EXPORT_PRIVATE FlagValues v8_flags
too high values may cause the compiler to set high thresholds for inlining to as much as possible avoid inlined allocation of objects that cannot escape trace load stores from virtual maglev objects use TurboFan fast string builder analyze liveness of environment slots and zap dead values trace TurboFan load elimination emit data about basic block usage in builtins to this enable builtin reordering when run mksnapshot flag for emit warnings when applying builtin profile data verify register allocation in TurboFan randomly schedule instructions to stress dependency tracking enable store store elimination in TurboFan rewrite far to near simulate GC compiler thread race related to allow float parameters to be passed in simulator mode JS Wasm Run additional turbo_optimize_inlined_js_wasm_wrappers enable experimental feedback collection in generic lowering enable Turboshaft s WasmLoadElimination enable Turboshaft s low level load elimination for JS enable Turboshaft s escape analysis for string concatenation use enable Turbolev features that we want to ship in the not too far future trace individual Turboshaft reduction steps trace intermediate Turboshaft reduction steps invocation count threshold for early optimization Enables optimizations which favor memory size over execution speed Enables sampling allocation profiler with X as a sample interval min size of a semi the new space consists of two semi spaces max size of the Collect garbage after Collect garbage after keeps maps alive for< n > old space garbage collections print one detailed trace line in allocation gc speed threshold for starting incremental marking via a task in percent of available threshold for starting incremental marking immediately in percent of available Use a single schedule for determining a marking schedule between JS and C objects schedules the minor GC task with kUserVisible priority max worker number of concurrent for NumberOfWorkerThreads start background threads that allocate memory concurrent_array_buffer_sweeping use parallel threads to clear weak refs in the atomic pause trace progress of the incremental marking trace object counts and memory usage * MB
Definition flags.cc:2197
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define CHECK_LT(lhs, rhs)
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_UNLIKELY(condition)
Definition v8config.h:660