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 explicit IncrementalMarking(Heap* heap);
29 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 inline bool IsStopped() { return state() == STOPPED; }
43 INLINE(bool IsMarking()) { return state() >= MARKING; }
45 inline bool IsMarkingIncomplete() { return state() == MARKING; }
47 inline bool IsComplete() { return state() == COMPLETE; }
49 bool WorthActivating();
51 bool ShouldActivate();
53 enum CompactionFlag { ALLOW_COMPACTION, PREVENT_COMPACTION };
55 void Start(CompactionFlag flag = ALLOW_COMPACTION);
59 void PrepareForScavenge();
61 void UpdateMarkingDequeAfterScavenge();
69 void MarkingComplete(CompletionAction action);
71 // It's hard to know how much work the incremental marker should do to make
72 // progress in the face of the mutator creating new work for it. We start
73 // of at a moderate rate of work and gradually increase the speed of the
74 // incremental marker until it completes.
75 // Do some marking every time this much memory has been allocated or that many
76 // heavy (color-checking) write barriers have been invoked.
77 static const intptr_t kAllocatedThreshold = 65536;
78 static const intptr_t kWriteBarriersInvokedThreshold = 32768;
79 // Start off by marking this many times more memory than has been allocated.
80 static const intptr_t kInitialMarkingSpeed = 1;
81 // But if we are promoting a lot of data we need to mark faster to keep up
82 // with the data that is entering the old space through promotion.
83 static const intptr_t kFastMarking = 3;
84 // After this many steps we increase the marking/allocating factor.
85 static const intptr_t kMarkingSpeedAccellerationInterval = 1024;
86 // This is how much we increase the marking/allocating factor by.
87 static const intptr_t kMarkingSpeedAccelleration = 2;
88 static const intptr_t kMaxMarkingSpeed = 1000;
90 // This is the upper bound for how many times we allow finalization of
91 // incremental marking to be postponed.
92 static const size_t kMaxIdleMarkingDelayCounter = 3;
94 void OldSpaceStep(intptr_t allocated);
96 intptr_t Step(intptr_t allocated, CompletionAction action,
97 ForceMarkingAction marking = DO_NOT_FORCE_MARKING,
98 ForceCompletionAction completion = FORCE_COMPLETION);
100 inline void RestartIfNotMarking() {
101 if (state_ == COMPLETE) {
103 if (FLAG_trace_incremental_marking) {
104 PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
109 static void RecordWriteFromCode(HeapObject* obj, Object** slot,
112 // Record a slot for compaction. Returns false for objects that are
113 // guaranteed to be rescanned or not guaranteed to survive.
115 // No slots in white objects should be recorded, as some slots are typed and
116 // cannot be interpreted correctly if the underlying object does not survive
117 // the incremental cycle (stays white).
118 INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
119 INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
120 INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo,
122 INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
126 void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
127 void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
129 void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
130 void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
131 void RecordCodeTargetPatch(Address pc, HeapObject* value);
133 inline void RecordWrites(HeapObject* obj);
135 inline void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
137 inline void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
139 inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
140 SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
143 inline void SetNewSpacePageFlags(NewSpacePage* chunk) {
144 SetNewSpacePageFlags(chunk, IsMarking());
147 MarkingDeque* marking_deque() { return &marking_deque_; }
149 bool IsCompacting() { return IsMarking() && is_compacting_; }
151 void ActivateGeneratedStub(Code* stub);
153 void NotifyOfHighPromotionRate() {
155 if (marking_speed_ < kFastMarking) {
158 "Increasing marking speed to %d "
159 "due to high promotion rate\n",
160 static_cast<int>(kFastMarking));
162 marking_speed_ = kFastMarking;
167 void EnterNoMarkingScope() { no_marking_scope_depth_++; }
169 void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
171 void UncommitMarkingDeque();
173 void NotifyIncompleteScanOfObject(int unscanned_bytes) {
174 unscanned_bytes_of_large_object_ = unscanned_bytes;
177 void ClearIdleMarkingDelayCounter();
179 bool IsIdleMarkingDelayCounterLimitReached();
182 int64_t SpaceLeftInOldSpace();
186 void ResetStepCounters();
188 void StartMarking(CompactionFlag flag);
190 void ActivateIncrementalWriteBarrier(PagedSpace* space);
191 static void ActivateIncrementalWriteBarrier(NewSpace* space);
192 void ActivateIncrementalWriteBarrier();
194 static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
195 static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
196 void DeactivateIncrementalWriteBarrier();
198 static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
201 static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking);
203 void EnsureMarkingDequeIsCommitted();
205 INLINE(void ProcessMarkingDeque());
207 INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
209 INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
211 void IncrementIdleMarkingDelayCounter();
218 base::VirtualMemory* marking_deque_memory_;
219 bool marking_deque_memory_committed_;
220 MarkingDeque marking_deque_;
223 int64_t old_generation_space_available_at_start_of_incremental_;
224 int64_t old_generation_space_used_at_start_of_incremental_;
225 int64_t bytes_rescanned_;
228 intptr_t bytes_scanned_;
230 intptr_t write_barriers_invoked_since_last_step_;
231 size_t idle_marking_delay_counter_;
233 int no_marking_scope_depth_;
235 int unscanned_bytes_of_large_object_;
237 DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
240 } // namespace v8::internal
242 #endif // V8_HEAP_INCREMENTAL_MARKING_H_