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 explicit IncrementalMarking(Heap* heap);
25 static void Initialize();
30 DCHECK(state_ == STOPPED || FLAG_incremental_marking);
34 bool should_hurry() { return should_hurry_; }
35 void set_should_hurry(bool val) { should_hurry_ = val; }
37 inline bool IsStopped() { return state() == STOPPED; }
39 INLINE(bool IsMarking()) { return state() >= MARKING; }
41 inline bool IsMarkingIncomplete() { return state() == MARKING; }
43 inline bool IsComplete() { return state() == COMPLETE; }
45 bool WorthActivating();
47 enum CompactionFlag { ALLOW_COMPACTION, PREVENT_COMPACTION };
49 void Start(CompactionFlag flag = ALLOW_COMPACTION);
53 void PrepareForScavenge();
55 void UpdateMarkingDequeAfterScavenge();
63 void MarkingComplete(CompletionAction action);
65 // It's hard to know how much work the incremental marker should do to make
66 // progress in the face of the mutator creating new work for it. We start
67 // of at a moderate rate of work and gradually increase the speed of the
68 // incremental marker until it completes.
69 // Do some marking every time this much memory has been allocated or that many
70 // heavy (color-checking) write barriers have been invoked.
71 static const intptr_t kAllocatedThreshold = 65536;
72 static const intptr_t kWriteBarriersInvokedThreshold = 32768;
73 // Start off by marking this many times more memory than has been allocated.
74 static const intptr_t kInitialMarkingSpeed = 1;
75 // But if we are promoting a lot of data we need to mark faster to keep up
76 // with the data that is entering the old space through promotion.
77 static const intptr_t kFastMarking = 3;
78 // After this many steps we increase the marking/allocating factor.
79 static const intptr_t kMarkingSpeedAccellerationInterval = 1024;
80 // This is how much we increase the marking/allocating factor by.
81 static const intptr_t kMarkingSpeedAccelleration = 2;
82 static const intptr_t kMaxMarkingSpeed = 1000;
84 void OldSpaceStep(intptr_t allocated);
86 void Step(intptr_t allocated, CompletionAction action,
87 bool force_marking = false);
89 inline void RestartIfNotMarking() {
90 if (state_ == COMPLETE) {
92 if (FLAG_trace_incremental_marking) {
93 PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
98 static void RecordWriteFromCode(HeapObject* obj, Object** slot,
101 // Record a slot for compaction. Returns false for objects that are
102 // guaranteed to be rescanned or not guaranteed to survive.
104 // No slots in white objects should be recorded, as some slots are typed and
105 // cannot be interpreted correctly if the underlying object does not survive
106 // the incremental cycle (stays white).
107 INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
108 INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
109 INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo,
111 INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
115 void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
116 void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
118 void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
119 void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
120 void RecordCodeTargetPatch(Address pc, HeapObject* value);
122 inline void RecordWrites(HeapObject* obj);
124 inline void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
126 inline void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
128 inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
129 SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
132 inline void SetNewSpacePageFlags(NewSpacePage* chunk) {
133 SetNewSpacePageFlags(chunk, IsMarking());
136 MarkingDeque* marking_deque() { return &marking_deque_; }
138 bool IsCompacting() { return IsMarking() && is_compacting_; }
140 void ActivateGeneratedStub(Code* stub);
142 void NotifyOfHighPromotionRate() {
144 if (marking_speed_ < kFastMarking) {
147 "Increasing marking speed to %d "
148 "due to high promotion rate\n",
149 static_cast<int>(kFastMarking));
151 marking_speed_ = kFastMarking;
156 void EnterNoMarkingScope() { no_marking_scope_depth_++; }
158 void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
160 void UncommitMarkingDeque();
162 void NotifyIncompleteScanOfObject(int unscanned_bytes) {
163 unscanned_bytes_of_large_object_ = unscanned_bytes;
167 int64_t SpaceLeftInOldSpace();
169 void ResetStepCounters();
171 void StartMarking(CompactionFlag flag);
173 void ActivateIncrementalWriteBarrier(PagedSpace* space);
174 static void ActivateIncrementalWriteBarrier(NewSpace* space);
175 void ActivateIncrementalWriteBarrier();
177 static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
178 static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
179 void DeactivateIncrementalWriteBarrier();
181 static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
184 static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking);
186 void EnsureMarkingDequeIsCommitted();
188 INLINE(void ProcessMarkingDeque());
190 INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
192 INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
199 base::VirtualMemory* marking_deque_memory_;
200 bool marking_deque_memory_committed_;
201 MarkingDeque marking_deque_;
204 int64_t old_generation_space_available_at_start_of_incremental_;
205 int64_t old_generation_space_used_at_start_of_incremental_;
206 int64_t bytes_rescanned_;
209 intptr_t bytes_scanned_;
211 intptr_t write_barriers_invoked_since_last_step_;
213 int no_marking_scope_depth_;
215 int unscanned_bytes_of_large_object_;
217 DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
220 } // namespace v8::internal
222 #endif // V8_HEAP_INCREMENTAL_MARKING_H_