Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 23 | enum ForceMarkingAction { FORCE_MARKING, DO_NOT_FORCE_MARKING }; |
| 24 | |
| 25 | enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION }; |
| 26 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | explicit IncrementalMarking(Heap* heap); |
| 28 | |
| 29 | static void Initialize(); |
| 30 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | State state() { |
| 32 | DCHECK(state_ == STOPPED || FLAG_incremental_marking); |
| 33 | return state_; |
| 34 | } |
| 35 | |
| 36 | bool should_hurry() { return should_hurry_; } |
| 37 | void set_should_hurry(bool val) { should_hurry_ = val; } |
| 38 | |
| 39 | inline bool IsStopped() { return state() == STOPPED; } |
| 40 | |
| 41 | INLINE(bool IsMarking()) { return state() >= MARKING; } |
| 42 | |
| 43 | inline bool IsMarkingIncomplete() { return state() == MARKING; } |
| 44 | |
| 45 | inline bool IsComplete() { return state() == COMPLETE; } |
| 46 | |
| 47 | bool WorthActivating(); |
| 48 | |
| 49 | bool ShouldActivate(); |
| 50 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 51 | bool WasActivated(); |
| 52 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 71 | void Epilogue(); |
| 72 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | // It's hard to know how much work the incremental marker should do to make |
| 74 | // progress in the face of the mutator creating new work for it. We start |
| 75 | // of at a moderate rate of work and gradually increase the speed of the |
| 76 | // incremental marker until it completes. |
| 77 | // Do some marking every time this much memory has been allocated or that many |
| 78 | // heavy (color-checking) write barriers have been invoked. |
| 79 | static const intptr_t kAllocatedThreshold = 65536; |
| 80 | static const intptr_t kWriteBarriersInvokedThreshold = 32768; |
| 81 | // Start off by marking this many times more memory than has been allocated. |
| 82 | static const intptr_t kInitialMarkingSpeed = 1; |
| 83 | // But if we are promoting a lot of data we need to mark faster to keep up |
| 84 | // with the data that is entering the old space through promotion. |
| 85 | static const intptr_t kFastMarking = 3; |
| 86 | // After this many steps we increase the marking/allocating factor. |
| 87 | static const intptr_t kMarkingSpeedAccellerationInterval = 1024; |
| 88 | // This is how much we increase the marking/allocating factor by. |
| 89 | static const intptr_t kMarkingSpeedAccelleration = 2; |
| 90 | static const intptr_t kMaxMarkingSpeed = 1000; |
| 91 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 92 | // This is the upper bound for how many times we allow finalization of |
| 93 | // incremental marking to be postponed. |
| 94 | static const size_t kMaxIdleMarkingDelayCounter = 3; |
| 95 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | void OldSpaceStep(intptr_t allocated); |
| 97 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 98 | intptr_t Step(intptr_t allocated, CompletionAction action, |
| 99 | ForceMarkingAction marking = DO_NOT_FORCE_MARKING, |
| 100 | ForceCompletionAction completion = FORCE_COMPLETION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | |
| 102 | inline void RestartIfNotMarking() { |
| 103 | if (state_ == COMPLETE) { |
| 104 | state_ = MARKING; |
| 105 | if (FLAG_trace_incremental_marking) { |
| 106 | PrintF("[IncrementalMarking] Restarting (new grey objects)\n"); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void RecordWriteFromCode(HeapObject* obj, Object** slot, |
| 112 | Isolate* isolate); |
| 113 | |
| 114 | // Record a slot for compaction. Returns false for objects that are |
| 115 | // guaranteed to be rescanned or not guaranteed to survive. |
| 116 | // |
| 117 | // No slots in white objects should be recorded, as some slots are typed and |
| 118 | // cannot be interpreted correctly if the underlying object does not survive |
| 119 | // the incremental cycle (stays white). |
| 120 | INLINE(bool BaseRecordWrite(HeapObject* obj, Object** slot, Object* value)); |
| 121 | INLINE(void RecordWrite(HeapObject* obj, Object** slot, Object* value)); |
| 122 | INLINE(void RecordWriteIntoCode(HeapObject* obj, RelocInfo* rinfo, |
| 123 | Object* value)); |
| 124 | INLINE(void RecordWriteOfCodeEntry(JSFunction* host, Object** slot, |
| 125 | Code* value)); |
| 126 | |
| 127 | |
| 128 | void RecordWriteSlow(HeapObject* obj, Object** slot, Object* value); |
| 129 | void RecordWriteIntoCodeSlow(HeapObject* obj, RelocInfo* rinfo, |
| 130 | Object* value); |
| 131 | void RecordWriteOfCodeEntrySlow(JSFunction* host, Object** slot, Code* value); |
| 132 | void RecordCodeTargetPatch(Code* host, Address pc, HeapObject* value); |
| 133 | void RecordCodeTargetPatch(Address pc, HeapObject* value); |
| 134 | |
| 135 | inline void RecordWrites(HeapObject* obj); |
| 136 | |
| 137 | inline void BlackToGreyAndUnshift(HeapObject* obj, MarkBit mark_bit); |
| 138 | |
| 139 | inline void WhiteToGreyAndPush(HeapObject* obj, MarkBit mark_bit); |
| 140 | |
| 141 | inline void SetOldSpacePageFlags(MemoryChunk* chunk) { |
| 142 | SetOldSpacePageFlags(chunk, IsMarking(), IsCompacting()); |
| 143 | } |
| 144 | |
| 145 | inline void SetNewSpacePageFlags(NewSpacePage* chunk) { |
| 146 | SetNewSpacePageFlags(chunk, IsMarking()); |
| 147 | } |
| 148 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | void NotifyIncompleteScanOfObject(int unscanned_bytes) { |
| 172 | unscanned_bytes_of_large_object_ = unscanned_bytes; |
| 173 | } |
| 174 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 175 | void ClearIdleMarkingDelayCounter(); |
| 176 | |
| 177 | bool IsIdleMarkingDelayCounterLimitReached(); |
| 178 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | private: |
| 180 | int64_t SpaceLeftInOldSpace(); |
| 181 | |
| 182 | void SpeedUp(); |
| 183 | |
| 184 | void ResetStepCounters(); |
| 185 | |
| 186 | void StartMarking(CompactionFlag flag); |
| 187 | |
| 188 | void ActivateIncrementalWriteBarrier(PagedSpace* space); |
| 189 | static void ActivateIncrementalWriteBarrier(NewSpace* space); |
| 190 | void ActivateIncrementalWriteBarrier(); |
| 191 | |
| 192 | static void DeactivateIncrementalWriteBarrierForSpace(PagedSpace* space); |
| 193 | static void DeactivateIncrementalWriteBarrierForSpace(NewSpace* space); |
| 194 | void DeactivateIncrementalWriteBarrier(); |
| 195 | |
| 196 | static void SetOldSpacePageFlags(MemoryChunk* chunk, bool is_marking, |
| 197 | bool is_compacting); |
| 198 | |
| 199 | static void SetNewSpacePageFlags(NewSpacePage* chunk, bool is_marking); |
| 200 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 201 | INLINE(void ProcessMarkingDeque()); |
| 202 | |
| 203 | INLINE(intptr_t ProcessMarkingDeque(intptr_t bytes_to_process)); |
| 204 | |
| 205 | INLINE(void VisitObject(Map* map, HeapObject* obj, int size)); |
| 206 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 207 | void IncrementIdleMarkingDelayCounter(); |
| 208 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | Heap* heap_; |
| 210 | |
| 211 | State state_; |
| 212 | bool is_compacting_; |
| 213 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | int steps_count_; |
| 215 | int64_t old_generation_space_available_at_start_of_incremental_; |
| 216 | int64_t old_generation_space_used_at_start_of_incremental_; |
| 217 | int64_t bytes_rescanned_; |
| 218 | bool should_hurry_; |
| 219 | int marking_speed_; |
| 220 | intptr_t bytes_scanned_; |
| 221 | intptr_t allocated_; |
| 222 | intptr_t write_barriers_invoked_since_last_step_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 223 | size_t idle_marking_delay_counter_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 224 | |
| 225 | int no_marking_scope_depth_; |
| 226 | |
| 227 | int unscanned_bytes_of_large_object_; |
| 228 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 229 | bool was_activated_; |
| 230 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 231 | DISALLOW_IMPLICIT_CONSTRUCTORS(IncrementalMarking); |
| 232 | }; |
| 233 | } |
| 234 | } // namespace v8::internal |
| 235 | |
| 236 | #endif // V8_HEAP_INCREMENTAL_MARKING_H_ |