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_
9 #include "src/execution.h"
10 #include "src/heap/mark-compact.h"
11 #include "src/objects.h"
17 class IncrementalMarking {
19 enum State { STOPPED, SWEEPING, MARKING, COMPLETE };
21 enum CompletionAction { GC_VIA_STACK_GUARD, NO_GC_VIA_STACK_GUARD };
23 enum ForceMarkingAction { FORCE_MARKING, DO_NOT_FORCE_MARKING };
25 enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
27 enum GCRequestType { COMPLETE_MARKING, OVERAPPROXIMATION };
29 explicit IncrementalMarking(Heap* heap);
31 static void Initialize();
34 DCHECK(state_ == STOPPED || FLAG_incremental_marking);
38 bool should_hurry() { return should_hurry_; }
39 void set_should_hurry(bool val) { should_hurry_ = val; }
41 bool weak_closure_was_overapproximated() const {
42 return weak_closure_was_overapproximated_;
44 void set_weak_closure_was_overapproximated(bool val) {
45 weak_closure_was_overapproximated_ = val;
48 inline bool IsStopped() { return state() == STOPPED; }
50 INLINE(bool IsMarking()) { return state() >= MARKING; }
52 inline bool IsMarkingIncomplete() { return state() == MARKING; }
54 inline bool IsComplete() { return state() == COMPLETE; }
56 GCRequestType request_type() const { return request_type_; }
58 bool WorthActivating();
60 bool ShouldActivate();
64 enum CompactionFlag { ALLOW_COMPACTION, PREVENT_COMPACTION };
66 void Start(CompactionFlag flag = ALLOW_COMPACTION);
70 void PrepareForScavenge();
72 void UpdateMarkingDequeAfterScavenge();
80 void OverApproximateWeakClosure();
82 void MarkingComplete(CompletionAction action);
86 // It's hard to know how much work the incremental marker should do to make
87 // progress in the face of the mutator creating new work for it. We start
88 // of at a moderate rate of work and gradually increase the speed of the
89 // incremental marker until it completes.
90 // Do some marking every time this much memory has been allocated or that many
91 // heavy (color-checking) write barriers have been invoked.
92 static const intptr_t kAllocatedThreshold = 65536;
93 static const intptr_t kWriteBarriersInvokedThreshold = 32768;
94 // Start off by marking this many times more memory than has been allocated.
95 static const intptr_t kInitialMarkingSpeed = 1;
96 // But if we are promoting a lot of data we need to mark faster to keep up
97 // with the data that is entering the old space through promotion.
98 static const intptr_t kFastMarking = 3;
99 // After this many steps we increase the marking/allocating factor.
100 static const intptr_t kMarkingSpeedAccellerationInterval = 1024;
101 // This is how much we increase the marking/allocating factor by.
102 static const intptr_t kMarkingSpeedAccelleration = 2;
103 static const intptr_t kMaxMarkingSpeed = 1000;
105 // This is the upper bound for how many times we allow finalization of
106 // incremental marking to be postponed.
107 static const size_t kMaxIdleMarkingDelayCounter = 3;
109 void OldSpaceStep(intptr_t allocated);
111 intptr_t Step(intptr_t allocated, CompletionAction action,
112 ForceMarkingAction marking = DO_NOT_FORCE_MARKING,
113 ForceCompletionAction completion = FORCE_COMPLETION);
115 inline void RestartIfNotMarking() {
116 if (state_ == COMPLETE) {
118 if (FLAG_trace_incremental_marking) {
119 PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
124 static void RecordWriteFromCode(HeapObject* obj, Object** slot,
127 // Record a slot for compaction. Returns false for objects that are
128 // guaranteed to be rescanned or not guaranteed to survive.
130 // No slots in white objects should be recorded, as some slots are typed and
131 // cannot be interpreted correctly if the underlying object does not survive
132 // the incremental cycle (stays white).
133 INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
134 INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
135 INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo,
137 INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
141 void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
142 void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
144 void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
145 void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
146 void RecordCodeTargetPatch(Address pc, HeapObject* value);
148 inline void RecordWrites(HeapObject* obj);
150 inline void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
152 inline void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
154 inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
155 SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
158 inline void SetNewSpacePageFlags(NewSpacePage* chunk) {
159 SetNewSpacePageFlags(chunk, IsMarking());
162 bool IsCompacting() { return IsMarking() && is_compacting_; }
164 void ActivateGeneratedStub(Code* stub);
166 void NotifyOfHighPromotionRate() {
168 if (marking_speed_ < kFastMarking) {
171 "Increasing marking speed to %d "
172 "due to high promotion rate\n",
173 static_cast<int>(kFastMarking));
175 marking_speed_ = kFastMarking;
180 void EnterNoMarkingScope() { no_marking_scope_depth_++; }
182 void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
184 void NotifyIncompleteScanOfObject(int unscanned_bytes) {
185 unscanned_bytes_of_large_object_ = unscanned_bytes;
188 void ClearIdleMarkingDelayCounter();
190 bool IsIdleMarkingDelayCounterLimitReached();
193 int64_t SpaceLeftInOldSpace();
197 void ResetStepCounters();
199 void StartMarking(CompactionFlag flag);
201 void ActivateIncrementalWriteBarrier(PagedSpace* space);
202 static void ActivateIncrementalWriteBarrier(NewSpace* space);
203 void ActivateIncrementalWriteBarrier();
205 static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
206 static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
207 void DeactivateIncrementalWriteBarrier();
209 static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
212 static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking);
214 INLINE(void ProcessMarkingDeque());
216 INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
218 INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
220 void IncrementIdleMarkingDelayCounter();
228 int64_t old_generation_space_available_at_start_of_incremental_;
229 int64_t old_generation_space_used_at_start_of_incremental_;
230 int64_t bytes_rescanned_;
233 intptr_t bytes_scanned_;
235 intptr_t write_barriers_invoked_since_last_step_;
236 size_t idle_marking_delay_counter_;
238 int no_marking_scope_depth_;
240 int unscanned_bytes_of_large_object_;
244 bool weak_closure_was_overapproximated_;
246 GCRequestType request_type_;
248 DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
251 } // namespace v8::internal
253 #endif // V8_HEAP_INCREMENTAL_MARKING_H_