v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
heap-growing.cc
Go to the documentation of this file.
1// Copyright 2020 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
7#include <cmath>
8#include <memory>
9
11#include "src/base/macros.h"
14#include "src/heap/cppgc/heap.h"
17
18namespace cppgc {
19namespace internal {
20
21namespace {
22// Minimum ratio between limit for incremental GC and limit for atomic GC
23// (to guarantee that limits are not to close to each other).
24constexpr double kMaximumLimitRatioForIncrementalGC = 0.9;
25// Minimum ratio between limit for incremental GC and limit for atomic GC
26// (to guarantee that limit is not too close to current allocated size).
27constexpr double kMinimumLimitRatioForIncrementalGC = 0.5;
28} // namespace
29
32 public:
37
40
41 void AllocatedObjectSizeIncreased(size_t) final;
42 // Only trigger GC on growing.
43 void AllocatedObjectSizeDecreased(size_t) final {}
44 void ResetAllocatedObjectSize(size_t) final;
45
46 size_t limit_for_atomic_gc() const { return limit_for_atomic_gc_; }
48
49 void DisableForTesting();
50
51 private:
52 void ConfigureLimit(size_t allocated_object_size);
53
56 // Allow 1 MB heap by default;
57 size_t initial_heap_size_ = 1 * kMB;
58 size_t limit_for_atomic_gc_ = 0; // See ConfigureLimit().
59 size_t limit_for_incremental_gc_ = 0; // See ConfigureLimit().
60
62
64
67};
68
70 GarbageCollector* collector, StatsCollector* stats_collector,
72 cppgc::Heap::MarkingType marking_support,
73 cppgc::Heap::SweepingType sweeping_support)
74 : collector_(collector),
75 stats_collector_(stats_collector),
76 gc_task_handle_(SingleThreadedHandle::NonEmptyTag{}),
77 marking_support_(marking_support),
78 sweeping_support_(sweeping_support) {
79 if (constraints.initial_heap_size_bytes > 0) {
80 initial_heap_size_ = constraints.initial_heap_size_bytes;
81 }
82 constexpr size_t kNoAllocatedBytes = 0;
83 ConfigureLimit(kNoAllocatedBytes);
84 stats_collector->RegisterObserver(this);
85}
86
90
92 if (disabled_for_testing_) return;
93 size_t allocated_object_size = stats_collector_->allocated_object_size();
94 if (allocated_object_size > limit_for_atomic_gc_) {
95 collector_->CollectGarbage(
96 {CollectionType::kMajor, StackState::kMayContainHeapPointers,
97 GCConfig::MarkingType::kAtomic, sweeping_support_});
98 } else if (allocated_object_size > limit_for_incremental_gc_) {
99 if (marking_support_ == cppgc::Heap::MarkingType::kAtomic) return;
100 collector_->StartIncrementalGarbageCollection(
101 {CollectionType::kMajor, StackState::kMayContainHeapPointers,
102 marking_support_, sweeping_support_});
103 }
104}
105
107 size_t allocated_object_size) {
108 ConfigureLimit(allocated_object_size);
109}
110
112 size_t allocated_object_size) {
113 const size_t size = std::max(allocated_object_size, initial_heap_size_);
114 limit_for_atomic_gc_ = std::max(static_cast<size_t>(size * kGrowingFactor),
115 size + kMinLimitIncrease);
116 // Estimate when to start incremental GC based on current allocation speed.
117 // Ideally we start incremental GC such that it is ready to finalize no
118 // later than when we reach |limit_for_atomic_gc_|. However, we need to cap
119 // |limit_for_incremental_gc_| within a range to prevent:
120 // 1) |limit_for_incremental_gc_| being too close to |limit_for_atomic_gc_|
121 // such that incremental gc gets nothing done before reaching
122 // |limit_for_atomic_gc_| (in case where the allocation rate is very low).
123 // 2) |limit_for_incremental_gc_| being too close to |size| such that GC is
124 // essentially always running and write barriers are always active (in
125 // case allocation rate is very high).
126 size_t estimated_bytes_allocated_during_incremental_gc =
128 .InMillisecondsF() *
129 stats_collector_->GetRecentAllocationSpeedInBytesPerMs());
130 size_t limit_incremental_gc_based_on_allocation_rate =
131 limit_for_atomic_gc_ - estimated_bytes_allocated_during_incremental_gc;
132 size_t maximum_limit_incremental_gc =
133 size + (limit_for_atomic_gc_ - size) * kMaximumLimitRatioForIncrementalGC;
134 size_t minimum_limit_incremental_gc =
135 size + (limit_for_atomic_gc_ - size) * kMinimumLimitRatioForIncrementalGC;
136 limit_for_incremental_gc_ =
137 std::max(minimum_limit_incremental_gc,
138 std::min(maximum_limit_incremental_gc,
139 limit_incremental_gc_based_on_allocation_rate));
140}
141
143 disabled_for_testing_ = true;
144}
145
147 StatsCollector* stats_collector,
149 cppgc::Heap::MarkingType marking_support,
150 cppgc::Heap::SweepingType sweeping_support)
151 : impl_(std::make_unique<HeapGrowing::HeapGrowingImpl>(
152 collector, stats_collector, constraints, marking_support,
153 sweeping_support)) {}
154
155HeapGrowing::~HeapGrowing() = default;
156
158 return impl_->limit_for_atomic_gc();
159}
161 return impl_->limit_for_incremental_gc();
162}
163
164void HeapGrowing::DisableForTesting() { impl_->DisableForTesting(); }
165
166// static
167constexpr double HeapGrowing::kGrowingFactor;
168
169} // namespace internal
170} // namespace cppgc
MarkingType
Definition heap.h:60
SweepingType
Definition heap.h:80
HeapGrowingImpl(const HeapGrowingImpl &)=delete
HeapGrowingImpl(GarbageCollector *, StatsCollector *, cppgc::Heap::ResourceConstraints, cppgc::Heap::MarkingType, cppgc::Heap::SweepingType)
void ConfigureLimit(size_t allocated_object_size)
HeapGrowingImpl & operator=(const HeapGrowingImpl &)=delete
const cppgc::Heap::MarkingType marking_support_
const cppgc::Heap::SweepingType sweeping_support_
static constexpr size_t kMinLimitIncrease
std::unique_ptr< HeapGrowingImpl > impl_
static constexpr double kGrowingFactor
HeapGrowing(GarbageCollector *, StatsCollector *, cppgc::Heap::ResourceConstraints, cppgc::Heap::MarkingType, cppgc::Heap::SweepingType)
size_t limit_for_incremental_gc() const
void RegisterObserver(AllocationObserver *)
static constexpr v8::base::TimeDelta kEstimatedMarkingTime
StatsCollector * stats_collector_
Definition sweeper.cc:595
MarkCompactCollector * collector_
constexpr size_t kMB
Definition globals.h:21
STL namespace.