blob: f10150da347465c851dc2fcf3baf56c7bc4ecc28 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
4
5#ifndef V8_HEAP_INCREMENTAL_MARKING_H_
6#define V8_HEAP_INCREMENTAL_MARKING_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/cancelable-task.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/execution.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010010#include "src/heap/heap.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/heap/incremental-marking-job.h"
12#include "src/heap/spaces.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/objects.h"
14
15namespace v8 {
16namespace internal {
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018// Forward declarations.
19class MarkBit;
20class PagedSpace;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021
22class IncrementalMarking {
23 public:
24 enum State { STOPPED, SWEEPING, MARKING, COMPLETE };
25
26 enum CompletionAction { GC_VIA_STACK_GUARD, NO_GC_VIA_STACK_GUARD };
27
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028 enum ForceMarkingAction { FORCE_MARKING, DO_NOT_FORCE_MARKING };
29
30 enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
31
Ben Murdochda12d292016-06-02 14:46:10 +010032 enum GCRequestType { NONE, COMPLETE_MARKING, FINALIZATION };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033
34 struct StepActions {
35 StepActions(CompletionAction complete_action_,
36 ForceMarkingAction force_marking_,
37 ForceCompletionAction force_completion_)
38 : completion_action(complete_action_),
39 force_marking(force_marking_),
40 force_completion(force_completion_) {}
41
42 CompletionAction completion_action;
43 ForceMarkingAction force_marking;
44 ForceCompletionAction force_completion;
45 };
46
47 static StepActions IdleStepActions();
48
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 explicit IncrementalMarking(Heap* heap);
50
51 static void Initialize();
52
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 State state() {
54 DCHECK(state_ == STOPPED || FLAG_incremental_marking);
55 return state_;
56 }
57
58 bool should_hurry() { return should_hurry_; }
59 void set_should_hurry(bool val) { should_hurry_ = val; }
60
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 bool finalize_marking_completed() const {
62 return finalize_marking_completed_;
63 }
64
65 void SetWeakClosureWasOverApproximatedForTesting(bool val) {
66 finalize_marking_completed_ = val;
67 }
68
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 inline bool IsStopped() { return state() == STOPPED; }
70
71 INLINE(bool IsMarking()) { return state() >= MARKING; }
72
73 inline bool IsMarkingIncomplete() { return state() == MARKING; }
74
75 inline bool IsComplete() { return state() == COMPLETE; }
76
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 inline bool IsReadyToOverApproximateWeakClosure() const {
78 return request_type_ == FINALIZATION && !finalize_marking_completed_;
79 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 GCRequestType request_type() const { return request_type_; }
82
Ben Murdochda12d292016-06-02 14:46:10 +010083 void reset_request_type() { request_type_ = NONE; }
84
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 bool CanBeActivated();
86
87 bool ShouldActivateEvenWithoutIdleNotification();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089 bool WasActivated();
90
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 void Start(const char* reason = nullptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 void FinalizeIncrementally();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094
95 void UpdateMarkingDequeAfterScavenge();
96
97 void Hurry();
98
99 void Finalize();
100
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101 void Stop();
102
103 void FinalizeMarking(CompletionAction action);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104
105 void MarkingComplete(CompletionAction action);
106
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107 void Epilogue();
108
Ben Murdochda12d292016-06-02 14:46:10 +0100109 // Performs incremental marking steps until deadline_in_ms is reached. It
110 // returns the remaining time that cannot be used for incremental marking
111 // anymore because a single step would exceed the deadline.
112 double AdvanceIncrementalMarking(double deadline_in_ms,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 StepActions step_actions);
114
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 // 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;
133
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400134 // 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;
137
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 void OldSpaceStep(intptr_t allocated);
139
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400140 intptr_t Step(intptr_t allocated, CompletionAction action,
141 ForceMarkingAction marking = DO_NOT_FORCE_MARKING,
142 ForceCompletionAction completion = FORCE_COMPLETION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143
144 inline void RestartIfNotMarking() {
145 if (state_ == COMPLETE) {
146 state_ = MARKING;
147 if (FLAG_trace_incremental_marking) {
148 PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
149 }
150 }
151 }
152
153 static void RecordWriteFromCode(HeapObject* obj, Object** slot,
154 Isolate* isolate);
155
Ben Murdoch097c5b22016-05-18 11:27:45 +0100156 static void RecordWriteOfCodeEntryFromCode(JSFunction* host, Object** slot,
157 Isolate* isolate);
158
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 // Record a slot for compaction. Returns false for objects that are
160 // guaranteed to be rescanned or not guaranteed to survive.
161 //
162 // No slots in white objects should be recorded, as some slots are typed and
163 // cannot be interpreted correctly if the underlying object does not survive
164 // the incremental cycle (stays white).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 INLINE(bool BaseRecordWrite(HeapObject* obj, Object* value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
Ben Murdochda12d292016-06-02 14:46:10 +0100167 INLINE(void RecordWriteIntoCode(Code* host, RelocInfo* rinfo, Object* value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
169 Code* value));
170
171
172 void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
Ben Murdochda12d292016-06-02 14:46:10 +0100173 void RecordWriteIntoCodeSlow(Code* host, RelocInfo* rinfo, Object* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
175 void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
176 void RecordCodeTargetPatch(Address pc, HeapObject* value);
177
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000178 void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179
180 inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
181 SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
182 }
183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 inline void SetNewSpacePageFlags(MemoryChunk* chunk) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 SetNewSpacePageFlags(chunk, IsMarking());
186 }
187
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 bool IsCompacting() { return IsMarking() && is_compacting_; }
189
190 void ActivateGeneratedStub(Code* stub);
191
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 void NotifyOfHighPromotionRate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 void NotifyIncompleteScanOfObject(int unscanned_bytes) {
195 unscanned_bytes_of_large_object_ = unscanned_bytes;
196 }
197
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400198 void ClearIdleMarkingDelayCounter();
199
200 bool IsIdleMarkingDelayCounterLimitReached();
201
Ben Murdochda12d292016-06-02 14:46:10 +0100202 static void MarkObject(Heap* heap, HeapObject* object);
203
204 void IterateBlackObject(HeapObject* object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205
206 Heap* heap() const { return heap_; }
207
208 IncrementalMarkingJob* incremental_marking_job() {
209 return &incremental_marking_job_;
210 }
211
Ben Murdochda12d292016-06-02 14:46:10 +0100212 bool black_allocation() { return black_allocation_; }
213
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 private:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100215 class Observer : public AllocationObserver {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 public:
217 Observer(IncrementalMarking& incremental_marking, intptr_t step_size)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100218 : AllocationObserver(step_size),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 incremental_marking_(incremental_marking) {}
220
221 void Step(int bytes_allocated, Address, size_t) override {
222 incremental_marking_.Step(bytes_allocated,
223 IncrementalMarking::GC_VIA_STACK_GUARD);
224 }
225
226 private:
227 IncrementalMarking& incremental_marking_;
228 };
229
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 int64_t SpaceLeftInOldSpace();
231
232 void SpeedUp();
233
234 void ResetStepCounters();
235
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000236 void StartMarking();
237
Ben Murdochda12d292016-06-02 14:46:10 +0100238 void StartBlackAllocation();
239 void FinishBlackAllocation();
240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241 void MarkRoots();
242 void MarkObjectGroups();
243 void ProcessWeakCells();
244 // Retain dying maps for <FLAG_retain_maps_for_n_gc> garbage collections to
245 // increase chances of reusing of map transition tree in future.
246 void RetainMaps();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247
248 void ActivateIncrementalWriteBarrier(PagedSpace* space);
249 static void ActivateIncrementalWriteBarrier(NewSpace* space);
250 void ActivateIncrementalWriteBarrier();
251
252 static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
253 static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
254 void DeactivateIncrementalWriteBarrier();
255
256 static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
257 bool is_compacting);
258
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 static void SetNewSpacePageFlags(MemoryChunk* chunk, bool is_marking);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 INLINE(void ProcessMarkingDeque());
262
263 INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
264
265 INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
266
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400267 void IncrementIdleMarkingDelayCounter();
268
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 Heap* heap_;
270
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000271 Observer observer_;
272
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000273 State state_;
274 bool is_compacting_;
275
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 int steps_count_;
277 int64_t old_generation_space_available_at_start_of_incremental_;
278 int64_t old_generation_space_used_at_start_of_incremental_;
279 int64_t bytes_rescanned_;
280 bool should_hurry_;
281 int marking_speed_;
282 intptr_t bytes_scanned_;
283 intptr_t allocated_;
284 intptr_t write_barriers_invoked_since_last_step_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400285 size_t idle_marking_delay_counter_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 int unscanned_bytes_of_large_object_;
288
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400289 bool was_activated_;
290
Ben Murdochda12d292016-06-02 14:46:10 +0100291 bool black_allocation_;
292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 bool finalize_marking_completed_;
294
295 int incremental_marking_finalization_rounds_;
296
297 GCRequestType request_type_;
298
299 IncrementalMarkingJob incremental_marking_job_;
300
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
302};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303} // namespace internal
304} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305
306#endif // V8_HEAP_INCREMENTAL_MARKING_H_