d6dfe17c7f2472ebb9ce76bdab694e1cb5a00708
[platform/upstream/nodejs.git] / deps / v8 / 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
9 #include "src/execution.h"
10 #include "src/heap/mark-compact.h"
11 #include "src/objects.h"
12
13 namespace v8 {
14 namespace internal {
15
16
17 class IncrementalMarking {
18  public:
19   enum State { STOPPED, SWEEPING, MARKING, COMPLETE };
20
21   enum CompletionAction { GC_VIA_STACK_GUARD, NO_GC_VIA_STACK_GUARD };
22
23   enum ForceMarkingAction { FORCE_MARKING, DO_NOT_FORCE_MARKING };
24
25   enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
26
27   enum GCRequestType { COMPLETE_MARKING, OVERAPPROXIMATION };
28
29   explicit IncrementalMarking(Heap* heap);
30
31   static void Initialize();
32
33   State state() {
34     DCHECK(state_ == STOPPED || FLAG_incremental_marking);
35     return state_;
36   }
37
38   bool should_hurry() { return should_hurry_; }
39   void set_should_hurry(bool val) { should_hurry_ = val; }
40
41   bool weak_closure_was_overapproximated() const {
42     return weak_closure_was_overapproximated_;
43   }
44   void set_weak_closure_was_overapproximated(bool val) {
45     weak_closure_was_overapproximated_ = val;
46   }
47
48   inline bool IsStopped() { return state() == STOPPED; }
49
50   INLINE(bool IsMarking()) { return state() >= MARKING; }
51
52   inline bool IsMarkingIncomplete() { return state() == MARKING; }
53
54   inline bool IsComplete() { return state() == COMPLETE; }
55
56   GCRequestType request_type() const { return request_type_; }
57
58   bool WorthActivating();
59
60   bool ShouldActivate();
61
62   bool WasActivated();
63
64   enum CompactionFlag { ALLOW_COMPACTION, PREVENT_COMPACTION };
65
66   void Start(CompactionFlag flag = ALLOW_COMPACTION);
67
68   void Stop();
69
70   void PrepareForScavenge();
71
72   void UpdateMarkingDequeAfterScavenge();
73
74   void Hurry();
75
76   void Finalize();
77
78   void Abort();
79
80   void OverApproximateWeakClosure();
81
82   void MarkingComplete(CompletionAction action);
83
84   void Epilogue();
85
86   // It's hard to know how much work the incremental marker should do to make
87   // progress in the face of the mutator creating new work for it.  We start
88   // of at a moderate rate of work and gradually increase the speed of the
89   // incremental marker until it completes.
90   // Do some marking every time this much memory has been allocated or that many
91   // heavy (color-checking) write barriers have been invoked.
92   static const intptr_t kAllocatedThreshold = 65536;
93   static const intptr_t kWriteBarriersInvokedThreshold = 32768;
94   // Start off by marking this many times more memory than has been allocated.
95   static const intptr_t kInitialMarkingSpeed = 1;
96   // But if we are promoting a lot of data we need to mark faster to keep up
97   // with the data that is entering the old space through promotion.
98   static const intptr_t kFastMarking = 3;
99   // After this many steps we increase the marking/allocating factor.
100   static const intptr_t kMarkingSpeedAccellerationInterval = 1024;
101   // This is how much we increase the marking/allocating factor by.
102   static const intptr_t kMarkingSpeedAccelleration = 2;
103   static const intptr_t kMaxMarkingSpeed = 1000;
104
105   // This is the upper bound for how many times we allow finalization of
106   // incremental marking to be postponed.
107   static const size_t kMaxIdleMarkingDelayCounter = 3;
108
109   void OldSpaceStep(intptr_t allocated);
110
111   intptr_t Step(intptr_t allocated, CompletionAction action,
112                 ForceMarkingAction marking = DO_NOT_FORCE_MARKING,
113                 ForceCompletionAction completion = FORCE_COMPLETION);
114
115   inline void RestartIfNotMarking() {
116     if (state_ == COMPLETE) {
117       state_ = MARKING;
118       if (FLAG_trace_incremental_marking) {
119         PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
120       }
121     }
122   }
123
124   static void RecordWriteFromCode(HeapObject* obj, Object** slot,
125                                   Isolate* isolate);
126
127   // Record a slot for compaction.  Returns false for objects that are
128   // guaranteed to be rescanned or not guaranteed to survive.
129   //
130   // No slots in white objects should be recorded, as some slots are typed and
131   // cannot be interpreted correctly if the underlying object does not survive
132   // the incremental cycle (stays white).
133   INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value));
134   INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value));
135   INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo,
136                                   Object* value));
137   INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
138                                      Code* value));
139
140
141   void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
142   void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
143                                Object* value);
144   void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value);
145   void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value);
146   void RecordCodeTargetPatch(Address pc, HeapObject* value);
147
148   inline void RecordWrites(HeapObject* obj);
149
150   inline void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
151
152   inline void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
153
154   inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
155     SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
156   }
157
158   inline void SetNewSpacePageFlags(NewSpacePage* chunk) {
159     SetNewSpacePageFlags(chunk, IsMarking());
160   }
161
162   bool IsCompacting() { return IsMarking() && is_compacting_; }
163
164   void ActivateGeneratedStub(Code* stub);
165
166   void NotifyOfHighPromotionRate() {
167     if (IsMarking()) {
168       if (marking_speed_ < kFastMarking) {
169         if (FLAG_trace_gc) {
170           PrintPID(
171               "Increasing marking speed to %d "
172               "due to high promotion rate\n",
173               static_cast<int>(kFastMarking));
174         }
175         marking_speed_ = kFastMarking;
176       }
177     }
178   }
179
180   void EnterNoMarkingScope() { no_marking_scope_depth_++; }
181
182   void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
183
184   void NotifyIncompleteScanOfObject(int unscanned_bytes) {
185     unscanned_bytes_of_large_object_ = unscanned_bytes;
186   }
187
188   void ClearIdleMarkingDelayCounter();
189
190   bool IsIdleMarkingDelayCounterLimitReached();
191
192  private:
193   int64_t SpaceLeftInOldSpace();
194
195   void SpeedUp();
196
197   void ResetStepCounters();
198
199   void StartMarking(CompactionFlag flag);
200
201   void ActivateIncrementalWriteBarrier(PagedSpace* space);
202   static void ActivateIncrementalWriteBarrier(NewSpace* space);
203   void ActivateIncrementalWriteBarrier();
204
205   static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
206   static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
207   void DeactivateIncrementalWriteBarrier();
208
209   static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
210                                    bool is_compacting);
211
212   static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking);
213
214   INLINE(void ProcessMarkingDeque());
215
216   INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
217
218   INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
219
220   void IncrementIdleMarkingDelayCounter();
221
222   Heap* heap_;
223
224   State state_;
225   bool is_compacting_;
226
227   int steps_count_;
228   int64_t old_generation_space_available_at_start_of_incremental_;
229   int64_t old_generation_space_used_at_start_of_incremental_;
230   int64_t bytes_rescanned_;
231   bool should_hurry_;
232   int marking_speed_;
233   intptr_t bytes_scanned_;
234   intptr_t allocated_;
235   intptr_t write_barriers_invoked_since_last_step_;
236   size_t idle_marking_delay_counter_;
237
238   int no_marking_scope_depth_;
239
240   int unscanned_bytes_of_large_object_;
241
242   bool was_activated_;
243
244   bool weak_closure_was_overapproximated_;
245
246   GCRequestType request_type_;
247
248   DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
249 };
250 }
251 }  // namespace v8::internal
252
253 #endif  // V8_HEAP_INCREMENTAL_MARKING_H_