OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 Opera Software AS. 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 HeapCompact_h |
| 6 #define HeapCompact_h |
| 7 |
| 8 #include "platform/PlatformExport.h" |
| 9 #include "platform/heap/BlinkGC.h" |
| 10 #include "wtf/DataLog.h" |
| 11 #include "wtf/PtrUtil.h" |
| 12 #include "wtf/ThreadingPrimitives.h" |
| 13 #include "wtf/Vector.h" |
| 14 |
| 15 #include <bitset> |
| 16 #include <utility> |
| 17 |
| 18 // Global dev/debug switches: |
| 19 |
| 20 // Set to 0 to prevent compaction GCs, disabling the heap compaction feature. |
| 21 #define ENABLE_HEAP_COMPACTION 1 |
| 22 |
| 23 // Emit debug info during compaction. |
| 24 #define DEBUG_HEAP_COMPACTION 0 |
| 25 |
| 26 // Emit stats on freelist occupancy. |
| 27 // 0 - disabled, 1 - minimal, 2 - verbose. |
| 28 #define DEBUG_HEAP_FREELIST 0 |
| 29 |
| 30 // Log the amount of time spent compacting. |
| 31 #define DEBUG_LOG_HEAP_COMPACTION_RUNNING_TIME 0 |
| 32 |
| 33 // Compact during all idle + precise GCs; for debugging. |
| 34 #define STRESS_TEST_HEAP_COMPACTION 0 |
| 35 |
| 36 #define LOG_HEAP_COMPACTION_INTERNAL(msg, ...) dataLogF(msg, ##__VA_ARGS__) |
| 37 |
| 38 #if DEBUG_HEAP_COMPACTION |
| 39 #define LOG_HEAP_COMPACTION(msg, ...) \ |
| 40 LOG_HEAP_COMPACTION_INTERNAL(msg, ##__VA_ARGS__) |
| 41 #else |
| 42 #define LOG_HEAP_COMPACTION(msg, ...) \ |
| 43 do { \ |
| 44 } while (0) |
| 45 #endif |
| 46 |
| 47 #if DEBUG_HEAP_FREELIST |
| 48 #define LOG_HEAP_FREELIST(msg, ...) \ |
| 49 LOG_HEAP_COMPACTION_INTERNAL(msg, ##__VA_ARGS__) |
| 50 #else |
| 51 #define LOG_HEAP_FREELIST(msg, ...) \ |
| 52 do { \ |
| 53 } while (0) |
| 54 #endif |
| 55 |
| 56 #if DEBUG_HEAP_FREELIST == 2 |
| 57 #define LOG_HEAP_FREELIST_VERBOSE(msg, ...) \ |
| 58 LOG_HEAP_COMPACTION_INTERNAL(msg, ##__VA_ARGS__) |
| 59 #else |
| 60 #define LOG_HEAP_FREELIST_VERBOSE(msg, ...) \ |
| 61 do { \ |
| 62 } while (0) |
| 63 #endif |
| 64 |
| 65 namespace blink { |
| 66 |
| 67 class NormalPageArena; |
| 68 class BasePage; |
| 69 class ThreadState; |
| 70 |
| 71 class PLATFORM_EXPORT HeapCompact final { |
| 72 public: |
| 73 static std::unique_ptr<HeapCompact> create() { |
| 74 return wrapUnique(new HeapCompact); |
| 75 } |
| 76 |
| 77 ~HeapCompact(); |
| 78 |
| 79 // Determine if a GC for the given type and reason should also perform |
| 80 // additional heap compaction. |
| 81 // |
| 82 bool shouldCompact(ThreadState*, BlinkGC::GCType, BlinkGC::GCReason); |
| 83 |
| 84 // Compaction should be performed as part of the ongoing GC, initialize |
| 85 // the heap compaction pass. Returns the appropriate visitor type to |
| 86 // use when running the marking phase. |
| 87 BlinkGC::GCType initialize(ThreadState*); |
| 88 |
| 89 // Returns true if the ongoing GC will perform compaction. |
| 90 bool isCompacting() const { return m_doCompact; } |
| 91 |
| 92 // Returns true if the ongoing GC will perform compaction for the given |
| 93 // heap arena. |
| 94 bool isCompactingArena(int arenaIndex) const { |
| 95 return m_doCompact && (m_compactableArenas & (0x1u << arenaIndex)); |
| 96 } |
| 97 |
| 98 // Returns |true| if the ongoing GC may compact the given arena/sub-heap. |
| 99 static bool isCompactableArena(int arenaIndex) { |
| 100 return arenaIndex >= BlinkGC::Vector1ArenaIndex && |
| 101 arenaIndex <= BlinkGC::HashTableArenaIndex; |
| 102 } |
| 103 |
| 104 // See |Heap::registerMovingObjectReference()| documentation. |
| 105 void registerMovingObjectReference(MovableReference* slot); |
| 106 |
| 107 // See |Heap::registerMovingObjectCallback()| documentation. |
| 108 void registerMovingObjectCallback(MovableReference, |
| 109 MovingObjectCallback, |
| 110 void* callbackData); |
| 111 |
| 112 // Register |slot| as containing a reference to the interior of a movable |
| 113 // object. |
| 114 // |
| 115 // |registerMovingObjectReference()| handles the common case of holding |
| 116 // an external reference to a backing store object. |registerRelocation()| |
| 117 // handles the relocation of external references into backing store |
| 118 // objects -- something not currently done & needed by the Blink codebase, |
| 119 // but kept open as a possibility..until further notice. |
| 120 void registerRelocation(MovableReference* slot); |
| 121 |
| 122 // Thread signalling that a compaction pass is being started or have |
| 123 // finished. |
| 124 // |
| 125 // A thread participating in a heap GC will wait on the completion |
| 126 // of compaction across all threads. No thread can be allowed to |
| 127 // potentially access another thread's heap arenas while they're |
| 128 // still being compacted. |
| 129 void startThreadCompaction(ThreadState*); |
| 130 void finishedThreadCompaction(ThreadState*); |
| 131 |
| 132 // Perform any relocation post-processing after having completed compacting |
| 133 // the given sub heap. Pass along the number of pages that were freed from |
| 134 // the arena, along with their total size. |
| 135 void finishedArenaCompaction(NormalPageArena*, |
| 136 size_t freedPages, |
| 137 size_t freedSize); |
| 138 |
| 139 // Register the heap page as containing live objects that will all be |
| 140 // compacted. When the GC is compacting, that is. |
| 141 void addCompactablePage(BasePage*); |
| 142 |
| 143 // Notify heap compaction that object at |from| has been relocated to.. |to|. |
| 144 // (Called by the sweep compaction pass.) |
| 145 void relocate(Address from, Address to); |
| 146 |
| 147 // For unit testing only: arrange for a compaction GC to be triggered |
| 148 // next time a non-conservative GC is run. Sets the compact-next flag |
| 149 // to the new value, returning old. |
| 150 static bool scheduleCompactionGCForTesting(bool); |
| 151 |
| 152 private: |
| 153 class MovableObjectFixups; |
| 154 |
| 155 HeapCompact(); |
| 156 |
| 157 // Sample the amount of fragmentation and heap memory currently residing |
| 158 // on the freelists of the arenas we're able to compact. The computed |
| 159 // numbers will be subsequently used to determine if a heap compaction |
| 160 // is on order (checkIfCompacting()). |
| 161 void updateHeapResidency(ThreadState*); |
| 162 |
| 163 // Parameters controlling when compaction should be done: |
| 164 |
| 165 // Number of GCs that must have passed since last compaction GC. |
| 166 static const int kGCCountSinceLastCompactionThreshold = 10; |
| 167 |
| 168 // Freelist size threshold that must be exceeded before compaction |
| 169 // should be considered. |
| 170 static const size_t kFreeListSizeThreshold = 512 * 1024; |
| 171 |
| 172 MovableObjectFixups& fixups(); |
| 173 |
| 174 std::unique_ptr<MovableObjectFixups> m_fixups; |
| 175 |
| 176 // Set to |true| when a compacting sweep will go ahead. |
| 177 bool m_doCompact; |
| 178 size_t m_gcCountSinceLastCompaction; |
| 179 |
| 180 // Lock protecting finishedThreadCompaction() signalling. |
| 181 Mutex m_mutex; |
| 182 |
| 183 // All threads performing a GC must synchronize on completion |
| 184 // of all heap compactions. Not doing so risks one thread resuming |
| 185 // the mutator, which could perform cross-thread access to a heap |
| 186 // that's still in the process of being compacted. |
| 187 ThreadCondition m_finished; |
| 188 |
| 189 // Number of heap threads participating in the compaction. |
| 190 int m_threadCount; |
| 191 |
| 192 // Last reported freelist size, across all compactable arenas. |
| 193 size_t m_freeListSize; |
| 194 |
| 195 // If compacting, i'th heap arena will be compacted |
| 196 // if corresponding bit is set. Indexes are in |
| 197 // the range of BlinkGC::HeapIndices. |
| 198 unsigned m_compactableArenas; |
| 199 |
| 200 // Stats, number of (complete) pages freed/decommitted + |
| 201 // bytes freed (which will include partial pages.) |
| 202 size_t m_freedPages; |
| 203 size_t m_freedSize; |
| 204 |
| 205 #if DEBUG_LOG_HEAP_COMPACTION_RUNNING_TIME |
| 206 int m_startCompaction; |
| 207 double m_startCompactionTimeMS; |
| 208 #endif |
| 209 |
| 210 static bool s_forceCompactionGC; |
| 211 }; |
| 212 |
| 213 } // namespace blink |
| 214 |
| 215 #endif // HeapCompact_h |
OLD | NEW |