c054fbd40fb30fefdb28b72d543cac2795253328
[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   explicit IncrementalMarking(Heap* heap);
24
25   static void Initialize();
26
27   void TearDown();
28
29   State state() {
30     DCHECK(state_ == STOPPED || FLAG_incremental_marking);
31     return state_;
32   }
33
34   bool should_hurry() { return should_hurry_; }
35   void set_should_hurry(bool val) { should_hurry_ = val; }
36
37   inline bool IsStopped() { return state() == STOPPED; }
38
39   INLINE(bool IsMarking()) { return state() >= MARKING; }
40
41   inline bool IsMarkingIncomplete() { return state() == MARKING; }
42
43   inline bool IsComplete() { return state() == COMPLETE; }
44
45   bool WorthActivating();
46
47   enum CompactionFlag { ALLOW_COMPACTION, PREVENT_COMPACTION };
48
49   void Start(CompactionFlag flag = ALLOW_COMPACTION);
50
51   void Stop();
52
53   void PrepareForScavenge();
54
55   void UpdateMarkingDequeAfterScavenge();
56
57   void Hurry();
58
59   void Finalize();
60
61   void Abort();
62
63   void MarkingComplete(CompletionAction action);
64
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;
83
84   void OldSpaceStep(intptr_t allocated);
85
86   void Step(intptr_t allocated, CompletionAction action,
87             bool force_marking = false);
88
89   inline void RestartIfNotMarking() {
90     if (state_ == COMPLETE) {
91       state_ = MARKING;
92       if (FLAG_trace_incremental_marking) {
93         PrintF("[IncrementalMarking] Restarting (new grey objects)\n");
94       }
95     }
96   }
97
98   static void RecordWriteFromCode(HeapObject* obj, Object** slot,
99                                   Isolate* isolate);
100
101   // Record a slot for compaction.  Returns false for objects that are
102   // guaranteed to be rescanned or not guaranteed to survive.
103   //
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,
110                                   Object* value));
111   INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot,
112                                      Code* value));
113
114
115   void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value);
116   void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo,
117                                Object* value);
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);
121
122   inline void RecordWrites(HeapObject* obj);
123
124   inline void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit);
125
126   inline void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit);
127
128   inline void SetOldSpacePageFlags(MemoryChunk* chunk) {
129     SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting());
130   }
131
132   inline void SetNewSpacePageFlags(NewSpacePage* chunk) {
133     SetNewSpacePageFlags(chunk, IsMarking());
134   }
135
136   MarkingDeque* marking_deque() { return &marking_deque_; }
137
138   bool IsCompacting() { return IsMarking() && is_compacting_; }
139
140   void ActivateGeneratedStub(Code* stub);
141
142   void NotifyOfHighPromotionRate() {
143     if (IsMarking()) {
144       if (marking_speed_ < kFastMarking) {
145         if (FLAG_trace_gc) {
146           PrintPID(
147               "Increasing marking speed to %d "
148               "due to high promotion rate\n",
149               static_cast<int>(kFastMarking));
150         }
151         marking_speed_ = kFastMarking;
152       }
153     }
154   }
155
156   void EnterNoMarkingScope() { no_marking_scope_depth_++; }
157
158   void LeaveNoMarkingScope() { no_marking_scope_depth_--; }
159
160   void UncommitMarkingDeque();
161
162   void NotifyIncompleteScanOfObject(int unscanned_bytes) {
163     unscanned_bytes_of_large_object_ = unscanned_bytes;
164   }
165
166  private:
167   int64_t SpaceLeftInOldSpace();
168
169   void ResetStepCounters();
170
171   void StartMarking(CompactionFlag flag);
172
173   void ActivateIncrementalWriteBarrier(PagedSpace* space);
174   static void ActivateIncrementalWriteBarrier(NewSpace* space);
175   void ActivateIncrementalWriteBarrier();
176
177   static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space);
178   static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space);
179   void DeactivateIncrementalWriteBarrier();
180
181   static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking,
182                                    bool is_compacting);
183
184   static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking);
185
186   void EnsureMarkingDequeIsCommitted();
187
188   INLINE(void ProcessMarkingDeque());
189
190   INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process));
191
192   INLINE(void VisitObject(Map* map, HeapObject* obj, int size));
193
194   Heap* heap_;
195
196   State state_;
197   bool is_compacting_;
198
199   base::VirtualMemory* marking_deque_memory_;
200   bool marking_deque_memory_committed_;
201   MarkingDeque marking_deque_;
202
203   int steps_count_;
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_;
207   bool should_hurry_;
208   int marking_speed_;
209   intptr_t bytes_scanned_;
210   intptr_t allocated_;
211   intptr_t write_barriers_invoked_since_last_step_;
212
213   int no_marking_scope_depth_;
214
215   int unscanned_bytes_of_large_object_;
216
217   DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking);
218 };
219 }
220 }  // namespace v8::internal
221
222 #endif  // V8_HEAP_INCREMENTAL_MARKING_H_