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