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.
5 #ifndef V8_HEAP_INCREMENTAL_MARKING_H_
6 #define V8_HEAP_INCREMENTAL_MARKING_H_
8 #include "src/cancelable-task.h"
9 #include "src/execution.h"
10 #include "src/heap/incremental-marking-job.h"
11 #include "src/objects.h"
16 // Forward declarations.
20 class IncrementalMarking {
22 enum State { STOPPED, SWEEPING, MARKING, COMPLETE };
24 enum CompletionAction { GC_VIA_STACK_GUARD, NO_GC_VIA_STACK_GUARD };
26 enum ForceMarkingAction { FORCE_MARKING, DO_NOT_FORCE_MARKING };
28 enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
30 enum GCRequestType { COMPLETE_MARKING, OVERAPPROXIMATION };
33 StepActions(CompletionAction complete_action_,
34 ForceMarkingAction force_marking_,
35 ForceCompletionAction force_completion_)
36 : completion_action(complete_action_),
37 force_marking(force_marking_),
38 force_completion(force_completion_) {}
40 CompletionAction completion_action;
41 ForceMarkingAction force_marking;
42 ForceCompletionAction force_completion;
45 static StepActions IdleStepActions();
47 explicit IncrementalMarking(Heap* heap);
49 static void Initialize();
52 DCHECK(state_ == STOPPED || FLAG_incremental_marking);
56 bool should_hurry() { return should_hurry_; }
57 void set_should_hurry(bool val) { should_hurry_ = val; }
59 bool weak_closure_was_overapproximated() const {
60 return weak_closure_was_overapproximated_;
63 void SetWeakClosureWasOverApproximatedForTesting(bool val) {
64 weak_closure_was_overapproximated_ = val;
67 inline bool IsStopped() { return state() == STOPPED; }
69 INLINE(bool IsMarking()) { return state() >= MARKING; }
71 inline bool IsMarkingIncomplete() { return state() == MARKING; }
73 inline bool IsComplete() { return state() == COMPLETE; }
75 inline bool IsReadyToOverApproximateWeakClosure() const {
76 return request_type_ == OVERAPPROXIMATION &&
77 !weak_closure_was_overapproximated_;
80 GCRequestType request_type() const { return request_type_; }
82 bool CanBeActivated();
84 bool ShouldActivateEvenWithoutIdleNotification();
88 void Start(const char* reason = nullptr);
90 void MarkObjectGroups();
92 void UpdateMarkingDequeAfterScavenge();
100 void OverApproximateWeakClosure(CompletionAction action);
102 void MarkingComplete(CompletionAction action);
106 // Performs incremental marking steps of step_size_in_bytes as long as
107 // deadline_ins_ms is not reached. step_size_in_bytes can be 0 to compute
108 // an estimate increment. Returns the remaining time that cannot be used
109 // for incremental marking anymore because a single step would exceed the
111 double AdvanceIncrementalMarking(intptr_t step_size_in_bytes,
112 double deadline_in_ms,
113 StepActions step_actions);
115 // It's hard to know how much work the incremental marker should do to make
116 // progress in the face of the mutator creating new work for it. We start
117 // of at a moderate rate of work and gradually increase the speed of the
118 // incremental marker until it completes.
119 // Do some marking every time this much memory has been allocated or that many
120 // heavy (color-checking) write barriers have been invoked.
121 static const intptr_t kAllocatedThreshold = 65536;
122 static const intptr_t kWriteBarriersInvokedThreshold = 32768;
123 // Start off by marking this many times more memory than has been allocated.
124 static const intptr_t kInitialMarkingSpeed = 1;
125 // But if we are promoting a lot of data we need to mark faster to keep up
126 // with the data that is entering the old space through promotion.
127 static const intptr_t kFastMarking = 3;
128 // After this many steps we increase the marking/allocating factor.
129 static const intptr_t kMarkingSpeedAccellerationInterval = 1024;
130 // This is how much we increase the marking/allocating factor by.
131 static const intptr_t kMarkingSpeedAccelleration = 2;
132 static const intptr_t kMaxMarkingSpeed = 1000;
134 // This is the upper bound for how many times we allow finalization of
135 // incremental marking to be postponed.
136 static const size_t kMaxIdleMarkingDelayCounter = 3;
138 void OldSpaceStep(intptr_t allocated);
140 intptr_t Step(intptr_t allocated, CompletionAction action,
141 ForceMarkingAction marking = DO_NOT_FORCE_MARKING,
142 ForceCompletionAction completion = FORCE_COMPLETION);
144 inline void RestartIfNotMarking() {
145 if (state_ == COMPLETE) {
147 if (FLAG_trace_incremental_marking) {
148 PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
153 static void RecordWriteFromCode(HeapObject* obj, Object** slot,
156 // Record a slot for compaction. Returns false for objects that are
157 // guaranteed to be rescanned or not guaranteed to survive.
159 // No slots in white objects should be recorded, as some slots are typed and
160 // cannot be interpreted correctly if the underlying object does not survive
161 // the incremental cycle (stays white).
162 INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
163 INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
164 INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo,
166 INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
170 void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
171 void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
173 void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
174 void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
175 void RecordCodeTargetPatch(Address pc, HeapObject* value);
177 void RecordWrites(HeapObject* obj);
179 void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
181 void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
183 inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
184 SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
187 inline void SetNewSpacePageFlags(MemoryChunk* chunk) {
188 SetNewSpacePageFlags(chunk, IsMarking());
191 bool IsCompacting() { return IsMarking() && is_compacting_; }
193 void ActivateGeneratedStub(Code* stub);
195 void NotifyOfHighPromotionRate();
197 void EnterNoMarkingScope() { no_marking_scope_depth_++; }
199 void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
201 void NotifyIncompleteScanOfObject(int unscanned_bytes) {
202 unscanned_bytes_of_large_object_ = unscanned_bytes;
205 void ClearIdleMarkingDelayCounter();
207 bool IsIdleMarkingDelayCounterLimitReached();
209 INLINE(static void MarkObject(Heap* heap, HeapObject* object));
211 Heap* heap() const { return heap_; }
213 IncrementalMarkingJob* incremental_marking_job() {
214 return &incremental_marking_job_;
218 int64_t SpaceLeftInOldSpace();
222 void ResetStepCounters();
226 void ActivateIncrementalWriteBarrier(PagedSpace* space);
227 static void ActivateIncrementalWriteBarrier(NewSpace* space);
228 void ActivateIncrementalWriteBarrier();
230 static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
231 static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
232 void DeactivateIncrementalWriteBarrier();
234 static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
237 static void SetNewSpacePageFlags(MemoryChunk* chunk, bool is_marking);
239 INLINE(void ProcessMarkingDeque());
241 INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
243 INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
245 void IncrementIdleMarkingDelayCounter();
253 int64_t old_generation_space_available_at_start_of_incremental_;
254 int64_t old_generation_space_used_at_start_of_incremental_;
255 int64_t bytes_rescanned_;
258 intptr_t bytes_scanned_;
260 intptr_t write_barriers_invoked_since_last_step_;
261 size_t idle_marking_delay_counter_;
263 int no_marking_scope_depth_;
265 int unscanned_bytes_of_large_object_;
269 bool weak_closure_was_overapproximated_;
271 int weak_closure_approximation_rounds_;
273 GCRequestType request_type_;
275 IncrementalMarkingJob incremental_marking_job_;
277 DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
279 } // namespace internal
282 #endif // V8_HEAP_INCREMENTAL_MARKING_H_