8f38b5ad4a5c49677c4b3749977350d576de9ce7
[platform/upstream/v8.git] / src / heap / incremental-marking.h
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.
4
5 #ifndef V8_HEAP_INCREMENTAL_MARKING_H_
6 #define V8_HEAP_INCREMENTAL_MARKING_H_
7
8 #include "src/cancelable-task.h"
9 #include "src/execution.h"
10 #include "src/heap/incremental-marking-job.h"
11 #include "src/objects.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // Forward declarations.
17 class MarkBit;
18 class PagedSpace;
19
20 class IncrementalMarking {
21  public:
22   enum State { STOPPED, SWEEPING, MARKING, COMPLETE };
23
24   enum CompletionAction { GC_VIA_STACK_GUARD, NO_GC_VIA_STACK_GUARD };
25
26   enum ForceMarkingAction { FORCE_MARKING, DO_NOT_FORCE_MARKING };
27
28   enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
29
30   enum GCRequestType { COMPLETE_MARKING, OVERAPPROXIMATION };
31
32   struct StepActions {
33     StepActions(CompletionAction complete_action_,
34                 ForceMarkingAction force_marking_,
35                 ForceCompletionAction force_completion_)
36         : completion_action(complete_action_),
37           force_marking(force_marking_),
38           force_completion(force_completion_) {}
39
40     CompletionAction completion_action;
41     ForceMarkingAction force_marking;
42     ForceCompletionAction force_completion;
43   };
44
45   static StepActions IdleStepActions();
46
47   explicit IncrementalMarking(Heap* heap);
48
49   static void Initialize();
50
51   State state() {
52     DCHECK(state_ == STOPPED || FLAG_incremental_marking);
53     return state_;
54   }
55
56   bool should_hurry() { return should_hurry_; }
57   void set_should_hurry(bool val) { should_hurry_ = val; }
58
59   bool weak_closure_was_overapproximated() const {
60     return weak_closure_was_overapproximated_;
61   }
62
63   void SetWeakClosureWasOverApproximatedForTesting(bool val) {
64     weak_closure_was_overapproximated_ = val;
65   }
66
67   inline bool IsStopped() { return state() == STOPPED; }
68
69   INLINE(bool IsMarking()) { return state() >= MARKING; }
70
71   inline bool IsMarkingIncomplete() { return state() == MARKING; }
72
73   inline bool IsComplete() { return state() == COMPLETE; }
74
75   inline bool IsReadyToOverApproximateWeakClosure() const {
76     return request_type_ == OVERAPPROXIMATION &&
77            !weak_closure_was_overapproximated_;
78   }
79
80   GCRequestType request_type() const { return request_type_; }
81
82   bool CanBeActivated();
83
84   bool ShouldActivateEvenWithoutIdleNotification();
85
86   bool WasActivated();
87
88   void Start(const char* reason = nullptr);
89
90   void MarkObjectGroups();
91
92   void UpdateMarkingDequeAfterScavenge();
93
94   void Hurry();
95
96   void Finalize();
97
98   void Stop();
99
100   void OverApproximateWeakClosure(CompletionAction action);
101
102   void MarkingComplete(CompletionAction action);
103
104   void Epilogue();
105
106   // Performs incremental marking steps of step_size_in_bytes as long as
107   // deadline_ins_ms is not reached. step_size_in_bytes can be 0 to compute
108   // an estimate increment. Returns the remaining time that cannot be used
109   // for incremental marking anymore because a single step would exceed the
110   // deadline.
111   double AdvanceIncrementalMarking(intptr_t step_size_in_bytes,
112                                    double deadline_in_ms,
113                                    StepActions step_actions);
114
115   // 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
134   // 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
138   void OldSpaceStep(intptr_t allocated);
139
140   intptr_t Step(intptr_t allocated, CompletionAction action,
141                 ForceMarkingAction marking = DO_NOT_FORCE_MARKING,
142                 ForceCompletionAction completion = FORCE_COMPLETION);
143
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
156   // Record a slot for compaction.  Returns false for objects that are
157   // guaranteed to be rescanned or not guaranteed to survive.
158   //
159   // No slots in white objects should be recorded, as some slots are typed and
160   // cannot be interpreted correctly if the underlying object does not survive
161   // the incremental cycle (stays white).
162   INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
163   INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
164   INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo,
165                                   Object* value));
166   INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
167                                      Code* value));
168
169
170   void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
171   void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
172                                Object* value);
173   void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
174   void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
175   void RecordCodeTargetPatch(Address pc, HeapObject* value);
176
177   void RecordWrites(HeapObject* obj);
178
179   void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
180
181   void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
182
183   inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
184     SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
185   }
186
187   inline void SetNewSpacePageFlags(MemoryChunk* chunk) {
188     SetNewSpacePageFlags(chunk, IsMarking());
189   }
190
191   bool IsCompacting() { return IsMarking() && is_compacting_; }
192
193   void ActivateGeneratedStub(Code* stub);
194
195   void NotifyOfHighPromotionRate();
196
197   void EnterNoMarkingScope() { no_marking_scope_depth_++; }
198
199   void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
200
201   void NotifyIncompleteScanOfObject(int unscanned_bytes) {
202     unscanned_bytes_of_large_object_ = unscanned_bytes;
203   }
204
205   void ClearIdleMarkingDelayCounter();
206
207   bool IsIdleMarkingDelayCounterLimitReached();
208
209   INLINE(static void MarkObject(Heap* heap, HeapObject* object));
210
211   Heap* heap() const { return heap_; }
212
213   IncrementalMarkingJob* incremental_marking_job() {
214     return &incremental_marking_job_;
215   }
216
217  private:
218   int64_t SpaceLeftInOldSpace();
219
220   void SpeedUp();
221
222   void ResetStepCounters();
223
224   void StartMarking();
225
226   void ActivateIncrementalWriteBarrier(PagedSpace* space);
227   static void ActivateIncrementalWriteBarrier(NewSpace* space);
228   void ActivateIncrementalWriteBarrier();
229
230   static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
231   static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
232   void DeactivateIncrementalWriteBarrier();
233
234   static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
235                                    bool is_compacting);
236
237   static void SetNewSpacePageFlags(MemoryChunk* chunk, bool is_marking);
238
239   INLINE(void ProcessMarkingDeque());
240
241   INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
242
243   INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
244
245   void IncrementIdleMarkingDelayCounter();
246
247   Heap* heap_;
248
249   State state_;
250   bool is_compacting_;
251
252   int steps_count_;
253   int64_t old_generation_space_available_at_start_of_incremental_;
254   int64_t old_generation_space_used_at_start_of_incremental_;
255   int64_t bytes_rescanned_;
256   bool should_hurry_;
257   int marking_speed_;
258   intptr_t bytes_scanned_;
259   intptr_t allocated_;
260   intptr_t write_barriers_invoked_since_last_step_;
261   size_t idle_marking_delay_counter_;
262
263   int no_marking_scope_depth_;
264
265   int unscanned_bytes_of_large_object_;
266
267   bool was_activated_;
268
269   bool weak_closure_was_overapproximated_;
270
271   int weak_closure_approximation_rounds_;
272
273   GCRequestType request_type_;
274
275   IncrementalMarkingJob incremental_marking_job_;
276
277   DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
278 };
279 }
280 }  // namespace v8::internal
281
282 #endif  // V8_HEAP_INCREMENTAL_MARKING_H_