V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
incremental-marking.h
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 "src/cancelable-task.h"
9 #include "src/heap/heap.h"
10 #include "src/heap/incremental-marking-job.h"
11 #include "src/heap/mark-compact.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class HeapObject;
17 class MarkBit;
18 class Map;
19 class Object;
20 class PagedSpace;
21 
22 enum class StepOrigin { kV8, kTask };
23 enum class WorklistToProcess { kAll, kBailout };
24 
25 class V8_EXPORT_PRIVATE IncrementalMarking {
26  public:
27  enum State { STOPPED, SWEEPING, MARKING, COMPLETE };
28 
29  enum CompletionAction { GC_VIA_STACK_GUARD, NO_GC_VIA_STACK_GUARD };
30 
31  enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
32 
33  enum GCRequestType { NONE, COMPLETE_MARKING, FINALIZATION };
34 
35 #ifdef V8_CONCURRENT_MARKING
37 #else
39 #endif // V8_CONCURRENT_MARKING
42 
44  public:
46  : marking_(marking), paused_(false) {
47  if (marking_->black_allocation()) {
48  paused_ = true;
49  marking_->PauseBlackAllocation();
50  }
51  }
52 
54  if (paused_) {
55  marking_->StartBlackAllocation();
56  }
57  }
58 
59  private:
60  IncrementalMarking* marking_;
61  bool paused_;
62  };
63 
64  // It's hard to know how much work the incremental marker should do to make
65  // progress in the face of the mutator creating new work for it. We start
66  // of at a moderate rate of work and gradually increase the speed of the
67  // incremental marker until it completes.
68  // Do some marking every time this much memory has been allocated or that many
69  // heavy (color-checking) write barriers have been invoked.
70  static const size_t kYoungGenerationAllocatedThreshold = 64 * KB;
71  static const size_t kOldGenerationAllocatedThreshold = 256 * KB;
72  static const size_t kMinStepSizeInBytes = 64 * KB;
73 
74  static const int kStepSizeInMs = 1;
75  static const int kMaxStepSizeInMs = 5;
76 
77 #ifndef DEBUG
78  static const intptr_t kActivationThreshold = 8 * MB;
79 #else
80  static const intptr_t kActivationThreshold = 0;
81 #endif
82 
83 #ifdef V8_CONCURRENT_MARKING
84  static const AccessMode kAtomicity = AccessMode::ATOMIC;
85 #else
86  static const AccessMode kAtomicity = AccessMode::NON_ATOMIC;
87 #endif
88 
90  MarkCompactCollector::MarkingWorklist* marking_worklist,
91  WeakObjects* weak_objects);
92 
93  MarkingState* marking_state() { return &marking_state_; }
94 
95  AtomicMarkingState* atomic_marking_state() { return &atomic_marking_state_; }
96 
97  NonAtomicMarkingState* non_atomic_marking_state() {
98  return &non_atomic_marking_state_;
99  }
100 
101  void NotifyLeftTrimming(HeapObject* from, HeapObject* to);
102 
103  V8_INLINE void TransferColor(HeapObject* from, HeapObject* to);
104 
105  State state() const {
106  DCHECK(state_ == STOPPED || FLAG_incremental_marking);
107  return state_;
108  }
109 
110  bool should_hurry() const { return should_hurry_; }
111  void set_should_hurry(bool val) { should_hurry_ = val; }
112 
113  bool finalize_marking_completed() const {
114  return finalize_marking_completed_;
115  }
116 
117  void SetWeakClosureWasOverApproximatedForTesting(bool val) {
118  finalize_marking_completed_ = val;
119  }
120 
121  inline bool IsStopped() const { return state() == STOPPED; }
122 
123  inline bool IsSweeping() const { return state() == SWEEPING; }
124 
125  inline bool IsMarking() const { return state() >= MARKING; }
126 
127  inline bool IsMarkingIncomplete() const { return state() == MARKING; }
128 
129  inline bool IsComplete() const { return state() == COMPLETE; }
130 
131  inline bool IsReadyToOverApproximateWeakClosure() const {
132  return request_type_ == FINALIZATION && !finalize_marking_completed_;
133  }
134 
135  inline bool NeedsFinalization() {
136  return IsMarking() &&
137  (request_type_ == FINALIZATION || request_type_ == COMPLETE_MARKING);
138  }
139 
140  GCRequestType request_type() const { return request_type_; }
141 
142  void reset_request_type() { request_type_ = NONE; }
143 
144  bool CanBeActivated();
145 
146  bool WasActivated();
147 
148  void Start(GarbageCollectionReason gc_reason);
149 
150  void FinalizeIncrementally();
151 
152  void UpdateMarkingWorklistAfterScavenge();
153  void UpdateWeakReferencesAfterScavenge();
154  void UpdateMarkedBytesAfterScavenge(size_t dead_bytes_in_new_space);
155 
156  void Hurry();
157 
158  void Finalize();
159 
160  void Stop();
161 
162  void FinalizeMarking(CompletionAction action);
163 
164  void MarkingComplete(CompletionAction action);
165 
166  void Epilogue();
167 
168  // Performs incremental marking steps until deadline_in_ms is reached. It
169  // returns the remaining time that cannot be used for incremental marking
170  // anymore because a single step would exceed the deadline.
171  double AdvanceIncrementalMarking(double deadline_in_ms,
172  CompletionAction completion_action,
173  StepOrigin step_origin);
174 
175  void FinalizeSweeping();
176 
177  size_t Step(size_t bytes_to_process, CompletionAction action,
178  StepOrigin step_origin,
179  WorklistToProcess worklist_to_process = WorklistToProcess::kAll);
180 
181  bool ShouldDoEmbedderStep();
182  void EmbedderStep(double duration);
183 
184  inline void RestartIfNotMarking();
185 
186  // {slot_address} is a raw Address instead of a MaybeObjectSlot because
187  // this is called from generated code via ExternalReference.
188  static int RecordWriteFromCode(HeapObject* obj, Address slot_address,
189  Isolate* isolate);
190 
191  // Record a slot for compaction. Returns false for objects that are
192  // guaranteed to be rescanned or not guaranteed to survive.
193  //
194  // No slots in white objects should be recorded, as some slots are typed and
195  // cannot be interpreted correctly if the underlying object does not survive
196  // the incremental cycle (stays white).
197  V8_INLINE bool BaseRecordWrite(HeapObject* obj, Object* value);
198  V8_INLINE void RecordWrite(HeapObject* obj, ObjectSlot slot, Object* value);
199  V8_INLINE void RecordMaybeWeakWrite(HeapObject* obj, MaybeObjectSlot slot,
200  MaybeObject value);
201  void RevisitObject(HeapObject* obj);
202 
203  void RecordWriteSlow(HeapObject* obj, HeapObjectSlot slot, Object* value);
204  void RecordWriteIntoCode(Code host, RelocInfo* rinfo, HeapObject* value);
205 
206  // Returns true if the function succeeds in transitioning the object
207  // from white to grey.
208  bool WhiteToGreyAndPush(HeapObject* obj);
209 
210  // This function is used to color the object black before it undergoes an
211  // unsafe layout change. This is a part of synchronization protocol with
212  // the concurrent marker.
213  void MarkBlackAndPush(HeapObject* obj);
214 
215  bool IsCompacting() { return IsMarking() && is_compacting_; }
216 
217  void NotifyIncompleteScanOfObject(int unscanned_bytes) {
218  unscanned_bytes_of_large_object_ = unscanned_bytes;
219  }
220 
221  void ProcessBlackAllocatedObject(HeapObject* obj);
222 
223  Heap* heap() const { return heap_; }
224 
225  IncrementalMarkingJob* incremental_marking_job() {
226  return &incremental_marking_job_;
227  }
228 
229  bool black_allocation() { return black_allocation_; }
230 
231  void StartBlackAllocationForTesting() {
232  if (!black_allocation_) {
233  StartBlackAllocation();
234  }
235  }
236 
237  MarkCompactCollector::MarkingWorklist* marking_worklist() const {
238  return marking_worklist_;
239  }
240 
241  void Deactivate();
242 
243  private:
244  class Observer : public AllocationObserver {
245  public:
246  Observer(IncrementalMarking& incremental_marking, intptr_t step_size)
247  : AllocationObserver(step_size),
248  incremental_marking_(incremental_marking) {}
249 
250  void Step(int bytes_allocated, Address, size_t) override;
251 
252  private:
253  IncrementalMarking& incremental_marking_;
254  };
255 
256  void StartMarking();
257 
258  void StartBlackAllocation();
259  void PauseBlackAllocation();
260  void FinishBlackAllocation();
261 
262  void MarkRoots();
263  bool ShouldRetainMap(Map map, int age);
264  // Retain dying maps for <FLAG_retain_maps_for_n_gc> garbage collections to
265  // increase chances of reusing of map transition tree in future.
266  void RetainMaps();
267 
268  void ActivateIncrementalWriteBarrier(PagedSpace* space);
269  void ActivateIncrementalWriteBarrier(NewSpace* space);
270  void ActivateIncrementalWriteBarrier();
271 
272  void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
273  void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
274  void DeactivateIncrementalWriteBarrier();
275 
276  template <WorklistToProcess worklist_to_process = WorklistToProcess::kAll>
277  V8_INLINE intptr_t ProcessMarkingWorklist(
278  intptr_t bytes_to_process,
279  ForceCompletionAction completion = DO_NOT_FORCE_COMPLETION);
280 
281  V8_INLINE bool IsFixedArrayWithProgressBar(HeapObject* object);
282 
283  // Visits the object and returns its size.
284  V8_INLINE int VisitObject(Map map, HeapObject* obj);
285 
286  void IncrementIdleMarkingDelayCounter();
287 
288  void AdvanceIncrementalMarkingOnAllocation();
289 
290  size_t StepSizeToKeepUpWithAllocations();
291  size_t StepSizeToMakeProgress();
292 
293  void SetState(State s) {
294  state_ = s;
295  heap_->SetIsMarkingFlag(s >= MARKING);
296  }
297 
298  Heap* const heap_;
299  MarkCompactCollector::MarkingWorklist* const marking_worklist_;
300  WeakObjects* weak_objects_;
301 
302  double start_time_ms_;
303  size_t initial_old_generation_size_;
304  size_t old_generation_allocation_counter_;
305  size_t bytes_allocated_;
306  size_t bytes_marked_ahead_of_schedule_;
307  // A sample of concurrent_marking()->TotalMarkedBytes() at the last
308  // incremental marking step. It is used for updating
309  // bytes_marked_ahead_of_schedule_ with contribution of concurrent marking.
310  size_t bytes_marked_concurrently_;
311  size_t unscanned_bytes_of_large_object_;
312 
313  // Must use SetState() above to update state_
314  State state_;
315 
316  bool is_compacting_;
317  bool should_hurry_;
318  bool was_activated_;
319  bool black_allocation_;
320  bool finalize_marking_completed_;
321  bool trace_wrappers_toggle_;
322  IncrementalMarkingJob incremental_marking_job_;
323 
324  GCRequestType request_type_;
325 
326  Observer new_generation_observer_;
327  Observer old_generation_observer_;
328 
329  MarkingState marking_state_;
330  AtomicMarkingState atomic_marking_state_;
331  NonAtomicMarkingState non_atomic_marking_state_;
332 
333  DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
334 };
335 } // namespace internal
336 } // namespace v8
337 
338 #endif // V8_HEAP_INCREMENTAL_MARKING_H_
Definition: libplatform.h:13