forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarker.h
More file actions
285 lines (229 loc) Β· 9.14 KB
/
marker.h
File metadata and controls
285 lines (229 loc) Β· 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_HEAP_CPPGC_MARKER_H_
#define V8_HEAP_CPPGC_MARKER_H_
#include <memory>
#include "include/cppgc/heap.h"
#include "include/cppgc/platform.h"
#include "include/cppgc/visitor.h"
#include "src/base/macros.h"
#include "src/base/platform/time.h"
#include "src/heap/base/incremental-marking-schedule.h"
#include "src/heap/base/worklist.h"
#include "src/heap/cppgc/concurrent-marker.h"
#include "src/heap/cppgc/globals.h"
#include "src/heap/cppgc/heap-config.h"
#include "src/heap/cppgc/marking-state.h"
#include "src/heap/cppgc/marking-visitor.h"
#include "src/heap/cppgc/marking-worklists.h"
#include "src/heap/cppgc/stats-collector.h"
#include "src/heap/cppgc/task-handle.h"
namespace cppgc {
namespace internal {
class HeapBase;
// Marking algorithm. Example for a valid call sequence creating the marking
// phase:
// 1. StartMarking()
// 2. AdvanceMarkingWithLimits() [Optional, depending on environment.]
// 3. EnterAtomicPause()
// 4. AdvanceMarkingWithLimits() [Optional]
// 5. EnterProcessGlobalAtomicPause()
// 6. AdvanceMarkingWithLimits()
// 7. LeaveAtomicPause()
//
// Alternatively, FinishMarking() combines steps 3.-7.
//
// The marker protects cross-thread roots from being created between 5.-7. This
// currently requires entering a process-global atomic pause.
class V8_EXPORT_PRIVATE MarkerBase {
public:
class IncrementalMarkingTask;
enum class WriteBarrierType {
kDijkstra,
kSteele,
};
// Pauses concurrent marking if running while this scope is active.
class PauseConcurrentMarkingScope final {
public:
explicit PauseConcurrentMarkingScope(MarkerBase&);
~PauseConcurrentMarkingScope();
private:
MarkerBase& marker_;
const bool resume_on_exit_;
};
virtual ~MarkerBase();
MarkerBase(const MarkerBase&) = delete;
MarkerBase& operator=(const MarkerBase&) = delete;
template <typename Class>
Class& To() {
return *static_cast<Class*>(this);
}
// Signals entering the atomic marking pause. The method
// - stops incremental/concurrent marking;
// - flushes back any in-construction worklists if needed;
// - Updates the MarkingConfig if the stack state has changed;
// - marks local roots
void EnterAtomicPause(StackState);
// Enters the process-global pause. The phase marks cross-thread roots and
// acquires a lock that prevents any cross-thread references from being
// created.
//
// The phase is ended with `LeaveAtomicPause()`.
void EnterProcessGlobalAtomicPause();
// Re-enable concurrent marking assuming it isn't enabled yet in GC cycle.
void ReEnableConcurrentMarking();
// Makes marking progress. A `marked_bytes_limit` of 0 means that the limit
// is determined by the internal marking scheduler.
//
// TODO(chromium:1056170): Remove TimeDelta argument when unified heap no
// longer uses it.
bool AdvanceMarkingWithLimits(
v8::base::TimeDelta = kMaximumIncrementalStepDuration,
size_t marked_bytes_limit = 0);
// Returns the size of the bytes marked in the last invocation of
// `AdvanceMarkingWithLimits()`.
size_t last_bytes_marked() const { return last_bytes_marked_; }
// Signals leaving the atomic marking pause. This method expects no more
// objects to be marked and merely updates marking states if needed.
void LeaveAtomicPause();
// Initialize marking according to the given config. This method will
// trigger incremental/concurrent marking if needed.
void StartMarking();
// Combines:
// - EnterAtomicPause()
// - EnterProcessGlobalAtomicPause()
// - AdvanceMarkingWithLimits()
// - ProcessWeakness()
// - LeaveAtomicPause()
void FinishMarking(StackState);
void ProcessCrossThreadWeaknessIfNeeded();
void ProcessWeakness();
bool JoinConcurrentMarkingIfNeeded();
void NotifyConcurrentMarkingOfWorkIfNeeded(cppgc::TaskPriority);
template <WriteBarrierType type>
inline void WriteBarrierForObject(HeapObjectHeader&);
HeapBase& heap() { return heap_; }
cppgc::Visitor& Visitor() { return visitor(); }
bool IsMarking() const { return is_marking_; }
void SetMainThreadMarkingDisabledForTesting(bool);
void WaitForConcurrentMarkingForTesting();
void ClearAllWorklistsForTesting();
bool IncrementalMarkingStepForTesting(StackState);
MarkingWorklists& MarkingWorklistsForTesting() { return marking_worklists_; }
MutatorMarkingState& MutatorMarkingStateForTesting() {
return mutator_marking_state_;
}
protected:
class IncrementalMarkingAllocationObserver final
: public StatsCollector::AllocationObserver {
public:
static constexpr size_t kMinAllocatedBytesPerStep = 256 * kKB;
explicit IncrementalMarkingAllocationObserver(MarkerBase& marker);
void AllocatedObjectSizeIncreased(size_t delta) final;
private:
MarkerBase& marker_;
size_t current_allocated_size_ = 0;
};
using IncrementalMarkingTaskHandle = SingleThreadedHandle;
static constexpr v8::base::TimeDelta kMaximumIncrementalStepDuration =
v8::base::TimeDelta::FromMilliseconds(2);
MarkerBase(HeapBase&, cppgc::Platform*, MarkingConfig);
virtual cppgc::Visitor& visitor() = 0;
virtual ConservativeTracingVisitor& conservative_visitor() = 0;
virtual heap::base::StackVisitor& stack_visitor() = 0;
virtual ConcurrentMarkerBase& concurrent_marker() = 0;
virtual heap::base::IncrementalMarkingSchedule& schedule() = 0;
// Processes the worklists with given deadlines. The deadlines are only
// checked every few objects.
// - `marked_bytes_deadline`: Only process this many bytes. Ignored for
// processing concurrent bailout objects.
// - `time_deadline`: Time deadline that is always respected.
bool ProcessWorklistsWithDeadline(size_t marked_bytes_deadline,
v8::base::TimeTicks time_deadline);
void AdvanceMarkingWithLimitsEpilogue();
void VisitLocalRoots(StackState);
void VisitCrossThreadRoots();
void MarkNotFullyConstructedObjects();
virtual void ScheduleIncrementalMarkingTask();
bool IncrementalMarkingStep(StackState);
void AdvanceMarkingOnAllocation();
virtual void AdvanceMarkingOnAllocationImpl();
void HandleNotFullyConstructedObjects();
void MarkStrongCrossThreadRoots();
HeapBase& heap_;
MarkingConfig config_ = MarkingConfig::Default();
cppgc::Platform* platform_;
std::shared_ptr<cppgc::TaskRunner> foreground_task_runner_;
IncrementalMarkingTaskHandle incremental_marking_handle_;
IncrementalMarkingAllocationObserver incremental_marking_allocation_observer_;
MarkingWorklists marking_worklists_;
MutatorMarkingState mutator_marking_state_;
size_t last_bytes_marked_ = 0;
bool is_marking_{false};
bool main_marking_disabled_for_testing_{false};
bool visited_cross_thread_persistents_in_atomic_pause_{false};
bool processed_cross_thread_weakness_{false};
};
class V8_EXPORT_PRIVATE Marker final : public MarkerBase {
public:
Marker(HeapBase&, cppgc::Platform*, MarkingConfig = MarkingConfig::Default());
protected:
cppgc::Visitor& visitor() final { return marking_visitor_; }
ConservativeTracingVisitor& conservative_visitor() final {
return conservative_marking_visitor_;
}
heap::base::StackVisitor& stack_visitor() final {
return conservative_marking_visitor_;
}
ConcurrentMarkerBase& concurrent_marker() final { return concurrent_marker_; }
heap::base::IncrementalMarkingSchedule& schedule() final {
return *schedule_.get();
}
private:
MutatorMarkingVisitor marking_visitor_;
ConservativeMarkingVisitor conservative_marking_visitor_;
std::unique_ptr<heap::base::IncrementalMarkingSchedule> schedule_;
ConcurrentMarker concurrent_marker_;
};
template <MarkerBase::WriteBarrierType type>
void MarkerBase::WriteBarrierForObject(HeapObjectHeader& header) {
// The barrier optimizes for the bailout cases:
// - kDijkstra: Marked objects.
// - kSteele: Unmarked objects.
switch (type) {
case MarkerBase::WriteBarrierType::kDijkstra:
if (!header.TryMarkAtomic()) {
return;
}
break;
case MarkerBase::WriteBarrierType::kSteele:
if (!header.IsMarked<AccessMode::kAtomic>()) {
return;
}
break;
}
// The barrier fired. Filter out in-construction objects here. This possibly
// requires unmarking the object again.
if (V8_UNLIKELY(header.IsInConstruction<AccessMode::kNonAtomic>())) {
// In construction objects are traced only if they are unmarked. If marking
// reaches this object again when it is fully constructed, it will re-mark
// it and tracing it as a previously not fully constructed object would know
// to bail out.
header.Unmark<AccessMode::kAtomic>();
mutator_marking_state_.not_fully_constructed_worklist()
.Push<AccessMode::kAtomic>(&header);
return;
}
switch (type) {
case MarkerBase::WriteBarrierType::kDijkstra:
mutator_marking_state_.write_barrier_worklist().Push(&header);
break;
case MarkerBase::WriteBarrierType::kSteele:
mutator_marking_state_.retrace_marked_objects_worklist().Push(&header);
break;
}
}
} // namespace internal
} // namespace cppgc
#endif // V8_HEAP_CPPGC_MARKER_H_