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