v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
incremental-marking.h
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
5#ifndef V8_HEAP_INCREMENTAL_MARKING_H_
6#define V8_HEAP_INCREMENTAL_MARKING_H_
7
8#include <cstdint>
9#include <optional>
10
11#include "src/base/hashing.h"
12#include "src/base/logging.h"
15#include "src/common/globals.h"
16#include "src/heap/heap.h"
21
22namespace v8 {
23namespace internal {
24
25class HeapObject;
26class MarkBit;
27class Map;
28class Object;
29class PagedSpace;
30
31// Describes in which context IncrementalMarking::Step() is used in. This
32// information is used when marking finishes and for marking progress
33// heuristics.
34enum class StepOrigin {
35 // The caller of Step() is not allowed to complete marking right away. A task
36 // is scheduled to complete the GC. When the task isn't
37 // run soon enough, the stack guard mechanism will be used.
38 kV8,
39
40 // The caller of Step() will complete marking by running the GC right
41 // afterwards.
42 kTask
43};
44
45constexpr const char* ToString(StepOrigin step_origin) {
46 switch (step_origin) {
47 case StepOrigin::kV8:
48 return "V8";
50 return "task";
51 }
52}
53
55 public:
56 IncrementalMarking(Heap* heap, WeakObjects* weak_objects);
57
60
61 MarkingMode marking_mode() const { return marking_mode_; }
62
63 bool IsMinorMarking() const {
64 return marking_mode_ == MarkingMode::kMinorMarking;
65 }
66 bool IsMajorMarking() const {
67 return marking_mode_ == MarkingMode::kMajorMarking;
68 }
69
70 bool IsStopped() const { return !IsMarking(); }
71 bool IsMarking() const { return marking_mode_ != MarkingMode::kNoMarking; }
73 return IsMajorMarking() && ShouldFinalize();
74 }
75
77 return major_collection_requested_via_stack_guard_;
78 }
79
80 // Checks whether incremental marking is safe to be started and whether it
81 // should be started.
82 bool CanAndShouldBeStarted() const;
83 void Start(GarbageCollector garbage_collector,
84 GarbageCollectionReason gc_reason);
85 // Returns true if incremental marking was running and false otherwise.
86 bool Stop();
87
88 // Performs incremental marking step and finalizes marking if complete.
89 void AdvanceAndFinalizeIfComplete();
90
91 // Performs incremental marking step and finalizes marking if the stack guard
92 // was already armed. If marking is complete but the stack guard wasn't armed
93 // yet, a finalization task is scheduled.
94 void AdvanceAndFinalizeIfNecessary();
95
96 // Performs incremental marking step and schedules job for finalization if
97 // marking completes.
98 void AdvanceOnAllocation();
99
100 bool IsCompacting() { return IsMajorMarking() && is_compacting_; }
101
102 Heap* heap() const { return heap_; }
103 Isolate* isolate() const;
104
106 return incremental_marking_job_.get();
107 }
108
109 bool black_allocation() { return black_allocation_; }
110
111 bool IsBelowActivationThresholds() const;
112
113 void MarkBlackBackground(Tagged<HeapObject> obj, int object_size);
114
115 void MarkRootsForTesting();
116
117 // Performs incremental marking step for unit tests.
118 void AdvanceForTesting(v8::base::TimeDelta max_duration,
119 size_t max_bytes_to_mark = SIZE_MAX);
120
121 uint64_t current_trace_id() const { return current_trace_id_.value(); }
122
123 std::shared_ptr<::heap::base::IncrementalMarkingSchedule> schedule() {
124 return schedule_;
125 }
126
127 private:
128 class Observer final : public AllocationObserver {
129 public:
130 Observer(IncrementalMarking* incremental_marking, intptr_t step_size);
131 ~Observer() override = default;
132 void Step(int bytes_allocated, Address, size_t) override;
133
134 private:
136 };
137
138 void StartMarkingMajor();
139 void StartMarkingMinor();
140
141 // Checks whether incremental marking is safe to be started.
142 bool CanBeStarted() const;
143
144 void StartBlackAllocation();
145 void PauseBlackAllocation();
146 void FinishBlackAllocation();
147
148 void StartPointerTableBlackAllocation();
149 void StopPointerTableBlackAllocation();
150
151 void MarkRoots();
152
154
155 // Fetches marked byte counters from the concurrent marker.
156 void FetchBytesMarkedConcurrently();
157 size_t GetScheduledBytes(StepOrigin step_origin);
158
159 bool ShouldFinalize() const;
160
161 bool ShouldWaitForTask();
162 bool TryInitializeTaskTimeout();
163
164 // Returns the actual used time and actually marked bytes.
165 std::pair<v8::base::TimeDelta, size_t> CppHeapStep(
166 v8::base::TimeDelta max_duration, size_t marked_bytes_limit);
167
168 void Step(v8::base::TimeDelta max_duration, size_t max_bytes_to_process,
169 StepOrigin step_origin);
170
171 size_t OldGenerationSizeOfObjects() const;
172
175 return current_local_marking_worklists_;
176 }
177
178 Heap* const heap_;
182 MarkingWorklists::Local* current_local_marking_worklists_ = nullptr;
185 size_t main_thread_marked_bytes_ = 0;
186 // A sample of concurrent_marking()->TotalMarkedBytes() at the last
187 // incremental marking step.
188 size_t bytes_marked_concurrently_ = 0;
189 MarkingMode marking_mode_ = MarkingMode::kNoMarking;
190
191 bool is_compacting_ = false;
192 bool black_allocation_ = false;
193 bool completion_task_scheduled_ = false;
195 bool major_collection_requested_via_stack_guard_ = false;
196 std::unique_ptr<IncrementalMarkingJob> incremental_marking_job_;
200 std::unordered_map<MutablePageMetadata*, intptr_t,
203 std::shared_ptr<::heap::base::IncrementalMarkingSchedule> schedule_;
204 std::optional<uint64_t> current_trace_id_;
205
207};
208
209} // namespace internal
210} // namespace v8
211
212#endif // V8_HEAP_INCREMENTAL_MARKING_H_
std::shared_ptr<::heap::base::IncrementalMarkingSchedule > schedule_
IncrementalMarking & operator=(const IncrementalMarking &)=delete
MarkCompactCollector *const major_collector_
IncrementalMarking(const IncrementalMarking &)=delete
std::unique_ptr< IncrementalMarkingJob > incremental_marking_job_
MinorMarkSweepCollector *const minor_collector_
std::optional< uint64_t > current_trace_id_
std::shared_ptr<::heap::base::IncrementalMarkingSchedule > schedule()
MarkingWorklists::Local * local_marking_worklists() const
IncrementalMarkingJob * incremental_marking_job() const
std::unordered_map< MutablePageMetadata *, intptr_t, base::hash< MutablePageMetadata * > > background_live_bytes_
Isolate * isolate
Schedule const *const schedule_
NonAtomicMarkingState * marking_state_
GarbageCollectionReason
Definition globals.h:1428
constexpr const char * ToString(DeoptimizeKind kind)
Definition globals.h:880
#define V8_EXPORT_PRIVATE
Definition macros.h:460
Heap * heap_