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_INCREMENTAL_MARKING_H_
6 #define V8_INCREMENTAL_MARKING_H_
10 #include "mark-compact.h"
17 class IncrementalMarking {
26 enum CompletionAction {
31 explicit IncrementalMarking(Heap* heap);
33 static void Initialize();
38 ASSERT(state_ == STOPPED || FLAG_incremental_marking);
42 bool should_hurry() { return should_hurry_; }
43 void set_should_hurry(bool val) { should_hurry_ = val; }
45 inline bool IsStopped() { return state() == STOPPED; }
47 INLINE(bool IsMarking()) { return state() >= MARKING; }
49 inline bool IsMarkingIncomplete() { return state() == MARKING; }
51 inline bool IsComplete() { return state() == COMPLETE; }
53 bool WorthActivating();
55 enum CompactionFlag { ALLOW_COMPACTION, PREVENT_COMPACTION };
57 void Start(CompactionFlag flag = ALLOW_COMPACTION);
61 void PrepareForScavenge();
63 void UpdateMarkingDequeAfterScavenge();
71 void MarkingComplete(CompletionAction action);
73 // It's hard to know how much work the incremental marker should do to make
74 // progress in the face of the mutator creating new work for it. We start
75 // of at a moderate rate of work and gradually increase the speed of the
76 // incremental marker until it completes.
77 // Do some marking every time this much memory has been allocated or that many
78 // heavy (color-checking) write barriers have been invoked.
79 static const intptr_t kAllocatedThreshold = 65536;
80 static const intptr_t kWriteBarriersInvokedThreshold = 32768;
81 // Start off by marking this many times more memory than has been allocated.
82 static const intptr_t kInitialMarkingSpeed = 1;
83 // But if we are promoting a lot of data we need to mark faster to keep up
84 // with the data that is entering the old space through promotion.
85 static const intptr_t kFastMarking = 3;
86 // After this many steps we increase the marking/allocating factor.
87 static const intptr_t kMarkingSpeedAccellerationInterval = 1024;
88 // This is how much we increase the marking/allocating factor by.
89 static const intptr_t kMarkingSpeedAccelleration = 2;
90 static const intptr_t kMaxMarkingSpeed = 1000;
92 void OldSpaceStep(intptr_t allocated);
94 void Step(intptr_t allocated, CompletionAction action);
96 inline void RestartIfNotMarking() {
97 if (state_ == COMPLETE) {
99 if (FLAG_trace_incremental_marking) {
100 PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
105 static void RecordWriteFromCode(HeapObject* obj,
109 // Record a slot for compaction. Returns false for objects that are
110 // guaranteed to be rescanned or not guaranteed to survive.
112 // No slots in white objects should be recorded, as some slots are typed and
113 // cannot be interpreted correctly if the underlying object does not survive
114 // the incremental cycle (stays white).
115 INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
116 INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
117 INLINE(void RecordWriteIntoCode(HeapObject* obj,
120 INLINE(void RecordWriteOfCodeEntry(JSFunction* host,
125 void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
126 void RecordWriteIntoCodeSlow(HeapObject* obj,
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 int steps_count() {
143 inline double steps_took() {
147 inline double longest_step() {
148 return longest_step_;
151 inline int steps_count_since_last_gc() {
152 return steps_count_since_last_gc_;
155 inline double steps_took_since_last_gc() {
156 return steps_took_since_last_gc_;
159 inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
160 SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
163 inline void SetNewSpacePageFlags(NewSpacePage* chunk) {
164 SetNewSpacePageFlags(chunk, IsMarking());
167 MarkingDeque* marking_deque() { return &marking_deque_; }
169 bool IsCompacting() { return IsMarking() && is_compacting_; }
171 void ActivateGeneratedStub(Code* stub);
173 void NotifyOfHighPromotionRate() {
175 if (marking_speed_ < kFastMarking) {
177 PrintPID("Increasing marking speed to %d "
178 "due to high promotion rate\n",
179 static_cast<int>(kFastMarking));
181 marking_speed_ = kFastMarking;
186 void EnterNoMarkingScope() {
187 no_marking_scope_depth_++;
190 void LeaveNoMarkingScope() {
191 no_marking_scope_depth_--;
194 void UncommitMarkingDeque();
196 void NotifyIncompleteScanOfObject(int unscanned_bytes) {
197 unscanned_bytes_of_large_object_ = unscanned_bytes;
201 int64_t SpaceLeftInOldSpace();
203 void ResetStepCounters();
205 void StartMarking(CompactionFlag flag);
207 void ActivateIncrementalWriteBarrier(PagedSpace* space);
208 static void ActivateIncrementalWriteBarrier(NewSpace* space);
209 void ActivateIncrementalWriteBarrier();
211 static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
212 static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
213 void DeactivateIncrementalWriteBarrier();
215 static void SetOldSpacePageFlags(MemoryChunk* chunk,
219 static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking);
221 void EnsureMarkingDequeIsCommitted();
223 INLINE(void ProcessMarkingDeque());
225 INLINE(void ProcessMarkingDeque(intptr_t bytes_to_process));
227 INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
234 VirtualMemory* marking_deque_memory_;
235 bool marking_deque_memory_committed_;
236 MarkingDeque marking_deque_;
240 double longest_step_;
241 int64_t old_generation_space_available_at_start_of_incremental_;
242 int64_t old_generation_space_used_at_start_of_incremental_;
243 int steps_count_since_last_gc_;
244 double steps_took_since_last_gc_;
245 int64_t bytes_rescanned_;
248 intptr_t bytes_scanned_;
250 intptr_t write_barriers_invoked_since_last_step_;
252 int no_marking_scope_depth_;
254 int unscanned_bytes_of_large_object_;
256 DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
259 } } // namespace v8::internal
261 #endif // V8_INCREMENTAL_MARKING_H_