v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
memory-balancer.cc
Go to the documentation of this file.
1// Copyright 2023 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 "src/heap/heap-inl.h"
8#include "src/heap/heap.h"
9
10namespace v8 {
11namespace internal {
12
14 : heap_(heap), last_measured_at_(startup_time) {}
15
25
27 CHECK(major_allocation_rate_.has_value());
28 CHECK(major_gc_speed_.has_value());
29 const size_t computed_limit =
31 sqrt(live_memory_after_gc_ * (major_allocation_rate_.value().rate()) /
32 (major_gc_speed_.value().rate()) / v8_flags.memory_balancer_c_value);
33
34 // 2 MB of extra space.
35 // This allows the heap size to not decay to CurrentSizeOfObject()
36 // and prevents GC from triggering if, after a long period of idleness,
37 // a small allocation appears.
38 constexpr size_t kMinHeapExtraSpace = 2 * MB;
39 const size_t minimum_limit = live_memory_after_gc_ + kMinHeapExtraSpace;
40
41 size_t new_limit = std::max<size_t>(minimum_limit, computed_limit);
42 new_limit = std::min<size_t>(new_limit, heap_->max_old_generation_size());
43 new_limit = std::max<size_t>(new_limit, heap_->min_old_generation_size());
44
45 if (v8_flags.trace_memory_balancer) {
46 heap_->isolate()->PrintWithTimestamp(
47 "MemoryBalancer: allocation-rate=%.1lfKB/ms gc-speed=%.1lfKB/ms "
48 "minium-limit=%.1lfM computed-limit=%.1lfM new-limit=%.1lfM\n",
49 major_allocation_rate_.value().rate() / KB,
50 major_gc_speed_.value().rate() / KB,
51 static_cast<double>(minimum_limit) / MB,
52 static_cast<double>(computed_limit) / MB,
53 static_cast<double>(new_limit) / MB);
54 }
55
57 new_limit, new_limit + embedder_allocation_limit_);
58}
59
60void MemoryBalancer::UpdateGCSpeed(size_t major_gc_bytes,
61 base::TimeDelta major_gc_duration) {
62 if (!major_gc_speed_) {
64 major_gc_bytes, major_gc_duration.InMillisecondsF()};
65 } else {
66 major_gc_speed_->Update(major_gc_bytes, major_gc_duration.InMillisecondsF(),
68 }
69}
70
72 size_t major_allocation_bytes, base::TimeDelta major_allocation_duration) {
75 major_allocation_bytes, major_allocation_duration.InMillisecondsF()};
76 } else {
77 major_allocation_rate_->Update(major_allocation_bytes,
78 major_allocation_duration.InMillisecondsF(),
80 }
81}
82
85 auto time = base::TimeTicks::Now();
86 auto memory = heap_->OldGenerationSizeOfObjects();
87
88 const base::TimeDelta duration = time - last_measured_at_;
89 const size_t allocated_bytes =
90 memory > last_measured_memory_ ? memory - last_measured_memory_ : 0;
91 UpdateAllocationRate(allocated_bytes, duration);
92
93 last_measured_memory_ = memory;
97}
98
100 if (heartbeat_task_started_) return;
103 std::make_unique<HeartbeatTask>(heap_->isolate(), this), 1);
104}
105
107 : CancelableTask(isolate), mb_(mb) {}
108
110
111} // namespace internal
112} // namespace v8
void PostDelayedTask(std::unique_ptr< Task > task, double delay_in_seconds, const SourceLocation &location=SourceLocation::Current())
double InMillisecondsF() const
Definition time.cc:226
static TimeTicks Now()
Definition time.cc:736
void SetOldGenerationAndGlobalAllocationLimit(size_t new_old_generation_allocation_limit, size_t new_global_allocation_limit)
Definition heap.cc:1523
size_t min_old_generation_size() const
Definition heap.h:1944
size_t max_old_generation_size() const
Definition heap.h:1940
V8_EXPORT_PRIVATE size_t OldGenerationSizeOfObjects() const
Definition heap.cc:5153
std::shared_ptr< v8::TaskRunner > GetForegroundTaskRunner(TaskPriority priority=TaskPriority::kUserBlocking) const
Definition heap.cc:5903
Isolate * isolate() const
Definition heap-inl.h:61
HeartbeatTask(Isolate *isolate, MemoryBalancer *mb)
MemoryBalancer(Heap *heap, base::TimeTicks startup_time)
std::optional< SmoothedBytesAndDuration > major_gc_speed_
static constexpr double kMajorAllocationDecayRate
void UpdateGCSpeed(size_t major_gc_bytes, base::TimeDelta major_gc_duration)
void RecomputeLimits(size_t embedder_allocation_limit, base::TimeTicks time)
void UpdateAllocationRate(size_t major_allocation_bytes, base::TimeDelta major_allocation_duration)
std::optional< SmoothedBytesAndDuration > major_allocation_rate_
static constexpr double kMajorGCDecayRate
TimeRecord time
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 CHECK(condition)
Definition logging.h:124
Heap * heap_