kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 95ef649 | 2016-03-15 15:26:36 +0000 | [diff] [blame] | 2 | * Copyright (C) 2003-2009, 2011, 2013-2016 Apple Inc. All rights reserved. |
eseidel | c504bad | 2007-10-24 21:31:27 +0000 | [diff] [blame] | 3 | * Copyright (C) 2007 Eric Seidel <eric@webkit.org> |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
mjs | cdff33b | 2006-01-23 21:41:36 +0000 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 18 | * |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
mjs | b64c50a | 2005-10-03 21:13:12 +0000 | [diff] [blame] | 21 | #include "config.h" |
ggaren@apple.com | a8b3854 | 2011-01-10 23:43:56 +0000 | [diff] [blame] | 22 | #include "Heap.h" |
darin | ff399e0 | 2002-11-23 07:49:05 +0000 | [diff] [blame] | 23 | |
ggaren@apple.com | 79ab2a9 | 2011-02-02 05:05:55 +0000 | [diff] [blame] | 24 | #include "CodeBlock.h" |
fpizlo@apple.com | 7b23164 | 2016-10-11 23:52:02 +0000 | [diff] [blame] | 25 | #include "CodeBlockSet.h" |
ggaren@apple.com | 0b32d09 | 2011-04-11 21:57:31 +0000 | [diff] [blame] | 26 | #include "ConservativeRoots.h" |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 27 | #include "DFGWorklist.h" |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 28 | #include "EdenGCActivityCallback.h" |
| 29 | #include "FullGCActivityCallback.h" |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 30 | #include "GCActivityCallback.h" |
fpizlo@apple.com | 0e0d931 | 2013-08-15 20:43:06 +0000 | [diff] [blame] | 31 | #include "GCIncomingRefCountedSetInlines.h" |
fpizlo@apple.com | c9f8a49 | 2016-09-24 00:47:30 +0000 | [diff] [blame] | 32 | #include "GCSegmentedArrayInlines.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 33 | #include "GCTypeMap.h" |
sbarati@apple.com | a3db465 | 2016-09-20 01:05:50 +0000 | [diff] [blame] | 34 | #include "HasOwnPropertyCache.h" |
fpizlo@apple.com | 5f86606 | 2015-09-26 18:07:09 +0000 | [diff] [blame] | 35 | #include "HeapHelperPool.h" |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 36 | #include "HeapIterationScope.h" |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 37 | #include "HeapProfiler.h" |
ggaren@apple.com | d2b624b | 2011-05-24 23:46:35 +0000 | [diff] [blame] | 38 | #include "HeapRootVisitor.h" |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 39 | #include "HeapSnapshot.h" |
mhahnenberg@apple.com | 3a2fb03 | 2012-10-05 17:35:49 +0000 | [diff] [blame] | 40 | #include "HeapStatistics.h" |
mark.lam@apple.com | 34e4667 | 2014-11-20 23:28:41 +0000 | [diff] [blame] | 41 | #include "HeapVerifier.h" |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 42 | #include "HelpingGCScope.h" |
mhahnenberg@apple.com | eb39abc | 2012-05-31 03:04:00 +0000 | [diff] [blame] | 43 | #include "IncrementalSweeper.h" |
weinig@apple.com | f5b90cd | 2009-01-16 00:24:24 +0000 | [diff] [blame] | 44 | #include "Interpreter.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 45 | #include "JITStubRoutineSet.h" |
fpizlo@apple.com | 46050f7 | 2016-06-17 04:48:47 +0000 | [diff] [blame] | 46 | #include "JITWorklist.h" |
joepeck@webkit.org | 14c5caf | 2015-10-12 21:29:21 +0000 | [diff] [blame] | 47 | #include "JSCInlines.h" |
ggaren@apple.com | f086387 | 2007-12-03 07:53:03 +0000 | [diff] [blame] | 48 | #include "JSGlobalObject.h" |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 49 | #include "JSLock.h" |
mhahnenberg@apple.com | 1629c06 | 2014-04-15 21:05:09 +0000 | [diff] [blame] | 50 | #include "JSVirtualMachineInternal.h" |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 51 | #include "MarkedSpaceInlines.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 52 | #include "SamplingProfiler.h" |
fpizlo@apple.com | 39303e0 | 2016-04-05 22:17:35 +0000 | [diff] [blame] | 53 | #include "ShadowChicken.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 54 | #include "SuperSampler.h" |
commit-queue@webkit.org | 2ed31ec | 2014-08-21 01:03:20 +0000 | [diff] [blame] | 55 | #include "TypeProfilerLog.h" |
oliver@apple.com | f0c01b8 | 2012-11-07 00:13:54 +0000 | [diff] [blame] | 56 | #include "UnlinkedCodeBlock.h" |
mhahnenberg@apple.com | 7a22bab | 2013-12-10 19:37:29 +0000 | [diff] [blame] | 57 | #include "VM.h" |
ggaren@apple.com | ab71e97 | 2012-04-28 05:57:46 +0000 | [diff] [blame] | 58 | #include "WeakSetInlines.h" |
ggaren@apple.com | f266349 | 2011-02-05 00:20:16 +0000 | [diff] [blame] | 59 | #include <algorithm> |
fpizlo@apple.com | 622ead7 | 2011-11-01 06:43:37 +0000 | [diff] [blame] | 60 | #include <wtf/CurrentTime.h> |
fpizlo@apple.com | 5e29b76 | 2016-03-18 00:53:24 +0000 | [diff] [blame] | 61 | #include <wtf/MainThread.h> |
fpizlo@apple.com | 74590cb | 2015-09-29 16:39:24 +0000 | [diff] [blame] | 62 | #include <wtf/ParallelVectorIterator.h> |
mhahnenberg@apple.com | 537512e | 2014-03-31 23:27:29 +0000 | [diff] [blame] | 63 | #include <wtf/ProcessID.h> |
joepeck@webkit.org | 14c5caf | 2015-10-12 21:29:21 +0000 | [diff] [blame] | 64 | #include <wtf/RAMSize.h> |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 65 | #include <wtf/SimpleStats.h> |
darin | d589360 | 2005-08-01 05:02:13 +0000 | [diff] [blame] | 66 | |
annulen@yandex.ru | 855ac99 | 2016-04-30 21:21:22 +0000 | [diff] [blame] | 67 | #if USE(FOUNDATION) |
ggaren@apple.com | 8ca8367 | 2016-04-21 04:40:18 +0000 | [diff] [blame] | 68 | #if __has_include(<objc/objc-internal.h>) |
| 69 | #include <objc/objc-internal.h> |
| 70 | #else |
| 71 | extern "C" void* objc_autoreleasePoolPush(void); |
| 72 | extern "C" void objc_autoreleasePoolPop(void *context); |
| 73 | #endif |
annulen@yandex.ru | 855ac99 | 2016-04-30 21:21:22 +0000 | [diff] [blame] | 74 | #endif // USE(FOUNDATION) |
ggaren@apple.com | 8ca8367 | 2016-04-21 04:40:18 +0000 | [diff] [blame] | 75 | |
ggaren@apple.com | f266349 | 2011-02-05 00:20:16 +0000 | [diff] [blame] | 76 | using namespace std; |
| 77 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 78 | namespace JSC { |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 79 | |
fpizlo@apple.com | 74590cb | 2015-09-29 16:39:24 +0000 | [diff] [blame] | 80 | namespace { |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 81 | |
ggaren@apple.com | 9a07093 | 2012-05-22 19:17:57 +0000 | [diff] [blame] | 82 | static const size_t largeHeapSize = 32 * MB; // About 1.5X the average webpage. |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 83 | const size_t smallHeapSize = 1 * MB; // Matches the FastMalloc per-thread cache. |
oliver@apple.com | 3eb8037 | 2011-10-12 01:11:04 +0000 | [diff] [blame] | 84 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 85 | size_t minHeapSize(HeapType heapType, size_t ramSize) |
fpizlo@apple.com | f49ce5c | 2011-08-02 20:40:17 +0000 | [diff] [blame] | 86 | { |
ggaren@apple.com | 9a07093 | 2012-05-22 19:17:57 +0000 | [diff] [blame] | 87 | if (heapType == LargeHeap) |
| 88 | return min(largeHeapSize, ramSize / 4); |
fpizlo@apple.com | c7b4721 | 2011-08-03 23:35:03 +0000 | [diff] [blame] | 89 | return smallHeapSize; |
fpizlo@apple.com | f49ce5c | 2011-08-02 20:40:17 +0000 | [diff] [blame] | 90 | } |
ggaren@apple.com | f266349 | 2011-02-05 00:20:16 +0000 | [diff] [blame] | 91 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 92 | size_t proportionalHeapSize(size_t heapSize, size_t ramSize) |
ggaren@apple.com | 9a07093 | 2012-05-22 19:17:57 +0000 | [diff] [blame] | 93 | { |
| 94 | // Try to stay under 1/2 RAM size to leave room for the DOM, rendering, networking, etc. |
| 95 | if (heapSize < ramSize / 4) |
| 96 | return 2 * heapSize; |
| 97 | if (heapSize < ramSize / 2) |
| 98 | return 1.5 * heapSize; |
| 99 | return 1.25 * heapSize; |
| 100 | } |
| 101 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 102 | bool isValidSharedInstanceThreadState(VM* vm) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 103 | { |
andersca@apple.com | b987aae | 2013-07-26 00:13:13 +0000 | [diff] [blame] | 104 | return vm->currentThreadIsHoldingAPILock(); |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 105 | } |
| 106 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 107 | bool isValidThreadState(VM* vm) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 108 | { |
barraclough@apple.com | 0cde90d | 2014-03-20 21:05:49 +0000 | [diff] [blame] | 109 | if (vm->atomicStringTable() != wtfThreadData().atomicStringTable()) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 110 | return false; |
| 111 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 112 | if (vm->isSharedInstance() && !isValidSharedInstanceThreadState(vm)) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 113 | return false; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 118 | void recordType(TypeCountSet& set, JSCell* cell) |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 119 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 120 | const char* typeName = "[unknown]"; |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 121 | const ClassInfo* info = cell->classInfo(); |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 122 | if (info && info->className) |
| 123 | typeName = info->className; |
| 124 | set.add(typeName); |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 125 | } |
| 126 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 127 | bool measurePhaseTiming() |
| 128 | { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | HashMap<const char*, GCTypeMap<SimpleStats>>& timingStats() |
| 133 | { |
| 134 | static HashMap<const char*, GCTypeMap<SimpleStats>>* result; |
| 135 | static std::once_flag once; |
| 136 | std::call_once( |
| 137 | once, |
| 138 | [] { |
| 139 | result = new HashMap<const char*, GCTypeMap<SimpleStats>>(); |
| 140 | }); |
| 141 | return *result; |
| 142 | } |
| 143 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 144 | SimpleStats& timingStats(const char* name, CollectionScope scope) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 145 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 146 | return timingStats().add(name, GCTypeMap<SimpleStats>()).iterator->value[scope]; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | class TimingScope { |
| 150 | public: |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 151 | TimingScope(Optional<CollectionScope> scope, const char* name) |
| 152 | : m_scope(scope) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 153 | , m_name(name) |
| 154 | { |
| 155 | if (measurePhaseTiming()) |
| 156 | m_before = monotonicallyIncreasingTimeMS(); |
| 157 | } |
| 158 | |
| 159 | TimingScope(Heap& heap, const char* name) |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 160 | : TimingScope(heap.collectionScope(), name) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 161 | { |
| 162 | } |
| 163 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 164 | void setScope(Optional<CollectionScope> scope) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 165 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 166 | m_scope = scope; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 169 | void setScope(Heap& heap) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 170 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 171 | setScope(heap.collectionScope()); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | ~TimingScope() |
| 175 | { |
| 176 | if (measurePhaseTiming()) { |
| 177 | double after = monotonicallyIncreasingTimeMS(); |
| 178 | double timing = after - m_before; |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 179 | SimpleStats& stats = timingStats(m_name, *m_scope); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 180 | stats.add(timing); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 181 | dataLog("[GC:", *m_scope, "] ", m_name, " took: ", timing, " ms (average ", stats.mean(), " ms).\n"); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | private: |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 185 | Optional<CollectionScope> m_scope; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 186 | double m_before; |
| 187 | const char* m_name; |
| 188 | }; |
| 189 | |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 190 | } // anonymous namespace |
| 191 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 192 | Heap::Heap(VM* vm, HeapType heapType) |
ggaren@apple.com | 9a07093 | 2012-05-22 19:17:57 +0000 | [diff] [blame] | 193 | : m_heapType(heapType) |
fpizlo@apple.com | dea6d26 | 2015-05-08 02:12:35 +0000 | [diff] [blame] | 194 | , m_ramSize(Options::forceRAMSize() ? Options::forceRAMSize() : ramSize()) |
ggaren@apple.com | 9a07093 | 2012-05-22 19:17:57 +0000 | [diff] [blame] | 195 | , m_minBytesPerCycle(minHeapSize(m_heapType, m_ramSize)) |
mhahnenberg@apple.com | 98daed0 | 2012-04-25 01:29:42 +0000 | [diff] [blame] | 196 | , m_sizeAfterLastCollect(0) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 197 | , m_sizeAfterLastFullCollect(0) |
| 198 | , m_sizeBeforeLastFullCollect(0) |
| 199 | , m_sizeAfterLastEdenCollect(0) |
| 200 | , m_sizeBeforeLastEdenCollect(0) |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 201 | , m_bytesAllocatedThisCycle(0) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 202 | , m_bytesAbandonedSinceLastFullCollect(0) |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 203 | , m_maxEdenSize(m_minBytesPerCycle) |
| 204 | , m_maxHeapSize(m_minBytesPerCycle) |
| 205 | , m_shouldDoFullCollection(false) |
mhahnenberg@apple.com | e2789cf | 2013-09-09 00:11:57 +0000 | [diff] [blame] | 206 | , m_totalBytesVisited(0) |
oliver@apple.com | 44d8954 | 2011-09-20 18:31:37 +0000 | [diff] [blame] | 207 | , m_objectSpace(this) |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 208 | , m_extraMemorySize(0) |
| 209 | , m_deprecatedExtraMemorySize(0) |
mark.lam@apple.com | 6aba436 | 2015-02-26 19:44:08 +0000 | [diff] [blame] | 210 | , m_machineThreads(this) |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 211 | , m_slotVisitor(*this) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 212 | , m_handleSet(vm) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 213 | , m_codeBlocks(std::make_unique<CodeBlockSet>()) |
| 214 | , m_jitStubRoutines(std::make_unique<JITStubRoutineSet>()) |
commit-queue@webkit.org | f42601f | 2011-07-12 22:35:39 +0000 | [diff] [blame] | 215 | , m_isSafeToCollect(false) |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 216 | , m_writeBarrierBuffer(256) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 217 | , m_vm(vm) |
mhahnenberg@apple.com | bce046b | 2014-03-21 21:53:25 +0000 | [diff] [blame] | 218 | // We seed with 10ms so that GCActivityCallback::didAllocate doesn't continuously |
| 219 | // schedule the timer if we've never done a collection. |
| 220 | , m_lastFullGCLength(0.01) |
| 221 | , m_lastEdenGCLength(0.01) |
fpizlo@apple.com | 7ca619d | 2016-10-25 23:22:48 +0000 | [diff] [blame] | 222 | #if USE(CF) |
| 223 | , m_runLoop(CFRunLoopGetCurrent()) |
| 224 | #endif // USE(CF) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 225 | , m_fullActivityCallback(GCActivityCallback::createFullTimer(this)) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 226 | , m_edenActivityCallback(GCActivityCallback::createEdenTimer(this)) |
carlosgc@webkit.org | 040a988 | 2015-11-26 13:52:45 +0000 | [diff] [blame] | 227 | , m_sweeper(std::make_unique<IncrementalSweeper>(this)) |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 228 | , m_deferralDepth(0) |
ggaren@apple.com | 8ca8367 | 2016-04-21 04:40:18 +0000 | [diff] [blame] | 229 | #if USE(FOUNDATION) |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 230 | , m_delayedReleaseRecursionCount(0) |
| 231 | #endif |
commit-queue@webkit.org | 5177507 | 2015-09-30 17:17:27 +0000 | [diff] [blame] | 232 | , m_helperClient(&heapHelperPool()) |
ap@webkit.org | 960c28e | 2008-06-19 17:29:29 +0000 | [diff] [blame] | 233 | { |
mark.lam@apple.com | 34e4667 | 2014-11-20 23:28:41 +0000 | [diff] [blame] | 234 | if (Options::verifyHeap()) |
| 235 | m_verifier = std::make_unique<HeapVerifier>(this, Options::numberOfGCCyclesToRecordForVerification()); |
ap@webkit.org | 960c28e | 2008-06-19 17:29:29 +0000 | [diff] [blame] | 236 | } |
darin | 070530c | 2002-10-07 21:06:29 +0000 | [diff] [blame] | 237 | |
ap@webkit.org | 0b7e63b | 2008-06-26 18:59:26 +0000 | [diff] [blame] | 238 | Heap::~Heap() |
| 239 | { |
akling@apple.com | 206b96f | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 240 | for (WeakBlock* block : m_logicallyEmptyWeakBlocks) |
akling@apple.com | 43fb352 | 2015-11-01 02:29:47 +0000 | [diff] [blame] | 241 | WeakBlock::destroy(*this, block); |
darin@apple.com | b6295d4 | 2008-10-02 23:48:47 +0000 | [diff] [blame] | 242 | } |
| 243 | |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 244 | bool Heap::isPagedOut(double deadline) |
| 245 | { |
fpizlo@apple.com | d1725cb | 2016-09-08 22:12:05 +0000 | [diff] [blame] | 246 | return m_objectSpace.isPagedOut(deadline); |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 247 | } |
| 248 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 249 | // The VM is being destroyed and the collector will never run again. |
ggaren@apple.com | c143e90 | 2012-04-28 20:51:27 +0000 | [diff] [blame] | 250 | // Run all pending finalizers now because we won't get another chance. |
| 251 | void Heap::lastChanceToFinalize() |
darin@apple.com | b6295d4 | 2008-10-02 23:48:47 +0000 | [diff] [blame] | 252 | { |
mark.lam@apple.com | 3072125 | 2013-11-21 05:29:42 +0000 | [diff] [blame] | 253 | RELEASE_ASSERT(!m_vm->entryScope); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 254 | RELEASE_ASSERT(!m_collectionScope); |
| 255 | RELEASE_ASSERT(m_mutatorState == MutatorState::Running); |
ap@webkit.org | 8672bcf | 2008-08-11 12:01:26 +0000 | [diff] [blame] | 256 | |
keith_miller@apple.com | 4697a6c | 2016-03-23 00:19:47 +0000 | [diff] [blame] | 257 | m_arrayBuffers.lastChanceToFinalize(); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 258 | m_codeBlocks->lastChanceToFinalize(); |
ggaren@apple.com | 96fa0e7 | 2012-05-23 20:47:46 +0000 | [diff] [blame] | 259 | m_objectSpace.lastChanceToFinalize(); |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 260 | releaseDelayedReleasedObjects(); |
akling@apple.com | e92da67 | 2015-04-04 23:16:21 +0000 | [diff] [blame] | 261 | |
| 262 | sweepAllLogicallyEmptyWeakBlocks(); |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void Heap::releaseDelayedReleasedObjects() |
| 266 | { |
ggaren@apple.com | 8ca8367 | 2016-04-21 04:40:18 +0000 | [diff] [blame] | 267 | #if USE(FOUNDATION) |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 268 | // We need to guard against the case that releasing an object can create more objects due to the |
| 269 | // release calling into JS. When those JS call(s) exit and all locks are being dropped we end up |
| 270 | // back here and could try to recursively release objects. We guard that with a recursive entry |
| 271 | // count. Only the initial call will release objects, recursive calls simple return and let the |
| 272 | // the initial call to the function take care of any objects created during release time. |
| 273 | // This also means that we need to loop until there are no objects in m_delayedReleaseObjects |
| 274 | // and use a temp Vector for the actual releasing. |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 275 | if (!m_delayedReleaseRecursionCount++) { |
| 276 | while (!m_delayedReleaseObjects.isEmpty()) { |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 277 | ASSERT(m_vm->currentThreadIsHoldingAPILock()); |
| 278 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 279 | Vector<RetainPtr<CFTypeRef>> objectsToRelease = WTFMove(m_delayedReleaseObjects); |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 280 | |
| 281 | { |
| 282 | // We need to drop locks before calling out to arbitrary code. |
| 283 | JSLock::DropAllLocks dropAllLocks(m_vm); |
| 284 | |
ggaren@apple.com | 8ca8367 | 2016-04-21 04:40:18 +0000 | [diff] [blame] | 285 | void* context = objc_autoreleasePoolPush(); |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 286 | objectsToRelease.clear(); |
ggaren@apple.com | 8ca8367 | 2016-04-21 04:40:18 +0000 | [diff] [blame] | 287 | objc_autoreleasePoolPop(context); |
msaboff@apple.com | 847d1cb | 2015-03-04 05:33:37 +0000 | [diff] [blame] | 288 | } |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | m_delayedReleaseRecursionCount--; |
| 292 | #endif |
ap@webkit.org | 0b7e63b | 2008-06-26 18:59:26 +0000 | [diff] [blame] | 293 | } |
| 294 | |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 295 | void Heap::reportExtraMemoryAllocatedSlowCase(size_t size) |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 296 | { |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 297 | didAllocate(size); |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 298 | collectIfNecessaryOrDefer(); |
mjs | 06ed466 | 2007-07-25 21:50:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 301 | void Heap::deprecatedReportExtraMemorySlowCase(size_t size) |
| 302 | { |
| 303 | m_deprecatedExtraMemorySize += size; |
| 304 | reportExtraMemoryAllocatedSlowCase(size); |
| 305 | } |
| 306 | |
mhahnenberg@apple.com | 98daed0 | 2012-04-25 01:29:42 +0000 | [diff] [blame] | 307 | void Heap::reportAbandonedObjectGraph() |
| 308 | { |
| 309 | // Our clients don't know exactly how much memory they |
| 310 | // are abandoning so we just guess for them. |
sbarati@apple.com | e229c4d | 2016-07-05 19:49:19 +0000 | [diff] [blame] | 311 | size_t abandonedBytes = static_cast<size_t>(0.1 * capacity()); |
mhahnenberg@apple.com | 98daed0 | 2012-04-25 01:29:42 +0000 | [diff] [blame] | 312 | |
| 313 | // We want to accelerate the next collection. Because memory has just |
| 314 | // been abandoned, the next collection has the potential to |
| 315 | // be more profitable. Since allocation is the trigger for collection, |
| 316 | // we hasten the next collection by pretending that we've allocated more memory. |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 317 | if (m_fullActivityCallback) { |
| 318 | m_fullActivityCallback->didAllocate( |
| 319 | m_sizeAfterLastCollect - m_sizeAfterLastFullCollect + m_bytesAllocatedThisCycle + m_bytesAbandonedSinceLastFullCollect); |
| 320 | } |
sbarati@apple.com | e229c4d | 2016-07-05 19:49:19 +0000 | [diff] [blame] | 321 | m_bytesAbandonedSinceLastFullCollect += abandonedBytes; |
mhahnenberg@apple.com | 98daed0 | 2012-04-25 01:29:42 +0000 | [diff] [blame] | 322 | } |
| 323 | |
ggaren@apple.com | dc067b6 | 2009-05-01 22:43:39 +0000 | [diff] [blame] | 324 | void Heap::protect(JSValue k) |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 325 | { |
mjs | 9f1f97a | 2007-03-20 23:57:01 +0000 | [diff] [blame] | 326 | ASSERT(k); |
andersca@apple.com | b987aae | 2013-07-26 00:13:13 +0000 | [diff] [blame] | 327 | ASSERT(m_vm->currentThreadIsHoldingAPILock()); |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 328 | |
weinig@apple.com | 92fdaef | 2009-01-20 00:54:18 +0000 | [diff] [blame] | 329 | if (!k.isCell()) |
ap@webkit.org | 84ea6b9 | 2008-06-04 16:29:49 +0000 | [diff] [blame] | 330 | return; |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 331 | |
weinig@apple.com | 92fdaef | 2009-01-20 00:54:18 +0000 | [diff] [blame] | 332 | m_protectedValues.add(k.asCell()); |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 333 | } |
| 334 | |
barraclough@apple.com | 88bb311 | 2010-03-24 07:11:51 +0000 | [diff] [blame] | 335 | bool Heap::unprotect(JSValue k) |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 336 | { |
mjs | 9f1f97a | 2007-03-20 23:57:01 +0000 | [diff] [blame] | 337 | ASSERT(k); |
andersca@apple.com | b987aae | 2013-07-26 00:13:13 +0000 | [diff] [blame] | 338 | ASSERT(m_vm->currentThreadIsHoldingAPILock()); |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 339 | |
weinig@apple.com | 92fdaef | 2009-01-20 00:54:18 +0000 | [diff] [blame] | 340 | if (!k.isCell()) |
barraclough@apple.com | 88bb311 | 2010-03-24 07:11:51 +0000 | [diff] [blame] | 341 | return false; |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 342 | |
barraclough@apple.com | 88bb311 | 2010-03-24 07:11:51 +0000 | [diff] [blame] | 343 | return m_protectedValues.remove(k.asCell()); |
mjs | c4bb295 | 2005-12-13 11:06:10 +0000 | [diff] [blame] | 344 | } |
| 345 | |
fpizlo@apple.com | 0e0d931 | 2013-08-15 20:43:06 +0000 | [diff] [blame] | 346 | void Heap::addReference(JSCell* cell, ArrayBuffer* buffer) |
| 347 | { |
| 348 | if (m_arrayBuffers.addReference(cell, buffer)) { |
| 349 | collectIfNecessaryOrDefer(); |
| 350 | didAllocate(buffer->gcSizeEstimateInBytes()); |
| 351 | } |
| 352 | } |
| 353 | |
fpizlo@apple.com | d13163d | 2011-09-03 05:14:04 +0000 | [diff] [blame] | 354 | void Heap::harvestWeakReferences() |
| 355 | { |
| 356 | m_slotVisitor.harvestWeakReferences(); |
| 357 | } |
| 358 | |
fpizlo@apple.com | f5e1fe1 | 2011-11-10 21:59:39 +0000 | [diff] [blame] | 359 | void Heap::finalizeUnconditionalFinalizers() |
| 360 | { |
| 361 | m_slotVisitor.finalizeUnconditionalFinalizers(); |
| 362 | } |
| 363 | |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 364 | void Heap::willStartIterating() |
mark.lam@apple.com | d4eb0d3 | 2013-04-17 16:32:44 +0000 | [diff] [blame] | 365 | { |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 366 | m_objectSpace.willStartIterating(); |
| 367 | } |
| 368 | |
| 369 | void Heap::didFinishIterating() |
| 370 | { |
| 371 | m_objectSpace.didFinishIterating(); |
mark.lam@apple.com | d4eb0d3 | 2013-04-17 16:32:44 +0000 | [diff] [blame] | 372 | } |
| 373 | |
fpizlo@apple.com | 46050f7 | 2016-06-17 04:48:47 +0000 | [diff] [blame] | 374 | void Heap::completeAllJITPlans() |
fpizlo@apple.com | 8e470a2 | 2015-09-21 20:10:04 +0000 | [diff] [blame] | 375 | { |
fpizlo@apple.com | 46050f7 | 2016-06-17 04:48:47 +0000 | [diff] [blame] | 376 | #if ENABLE(JIT) |
| 377 | JITWorklist::instance()->completeAllForVM(*m_vm); |
| 378 | #endif // ENABLE(JIT) |
fpizlo@apple.com | 8e470a2 | 2015-09-21 20:10:04 +0000 | [diff] [blame] | 379 | #if ENABLE(DFG_JIT) |
| 380 | DFG::completeAllPlansForVM(*m_vm); |
| 381 | #endif |
| 382 | } |
| 383 | |
akling@apple.com | 09cd9c0 | 2015-03-05 02:19:14 +0000 | [diff] [blame] | 384 | void Heap::markRoots(double gcStartTime, void* stackOrigin, void* stackTop, MachineThreads::RegisterState& calleeSavedRegisters) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 385 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 386 | TimingScope markRootsTimingScope(*this, "Heap::markRoots"); |
| 387 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 388 | ASSERT(isValidThreadState(m_vm)); |
ap@webkit.org | 01aff70 | 2008-08-20 07:23:06 +0000 | [diff] [blame] | 389 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 390 | HeapRootVisitor heapRootVisitor(m_slotVisitor); |
| 391 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 392 | { |
| 393 | TimingScope preConvergenceTimingScope(*this, "Heap::markRoots before convergence"); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 394 | |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 395 | #if ENABLE(DFG_JIT) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 396 | DFG::rememberCodeBlocks(*m_vm); |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 397 | #endif |
commit-queue@webkit.org | 0263ad3 | 2015-10-06 05:51:34 +0000 | [diff] [blame] | 398 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 399 | #if ENABLE(SAMPLING_PROFILER) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 400 | if (SamplingProfiler* samplingProfiler = m_vm->samplingProfiler()) { |
| 401 | // Note that we need to own the lock from now until we're done |
| 402 | // marking the SamplingProfiler's data because once we verify the |
| 403 | // SamplingProfiler's stack traces, we don't want it to accumulate |
| 404 | // more stack traces before we get the chance to mark it. |
| 405 | // This lock is released inside visitSamplingProfiler(). |
| 406 | samplingProfiler->getLock().lock(); |
| 407 | samplingProfiler->processUnverifiedStackTraces(); |
| 408 | } |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 409 | #endif // ENABLE(SAMPLING_PROFILER) |
| 410 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 411 | if (m_collectionScope == CollectionScope::Full) { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 412 | m_opaqueRoots.clear(); |
| 413 | m_slotVisitor.clearMarkStack(); |
| 414 | } |
| 415 | |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 416 | beginMarking(); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 417 | |
| 418 | m_parallelMarkersShouldExit = false; |
| 419 | |
| 420 | m_helperClient.setFunction( |
| 421 | [this] () { |
| 422 | SlotVisitor* slotVisitor; |
| 423 | { |
| 424 | LockHolder locker(m_parallelSlotVisitorLock); |
| 425 | if (m_availableParallelSlotVisitors.isEmpty()) { |
| 426 | std::unique_ptr<SlotVisitor> newVisitor = |
| 427 | std::make_unique<SlotVisitor>(*this); |
| 428 | slotVisitor = newVisitor.get(); |
| 429 | m_parallelSlotVisitors.append(WTFMove(newVisitor)); |
| 430 | } else |
| 431 | slotVisitor = m_availableParallelSlotVisitors.takeLast(); |
| 432 | } |
| 433 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 434 | WTF::registerGCThread(GCThreadType::Helper); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 435 | |
| 436 | { |
| 437 | ParallelModeEnabler parallelModeEnabler(*slotVisitor); |
| 438 | slotVisitor->didStartMarking(); |
| 439 | slotVisitor->drainFromShared(SlotVisitor::SlaveDrain); |
| 440 | } |
| 441 | |
| 442 | { |
| 443 | LockHolder locker(m_parallelSlotVisitorLock); |
| 444 | m_availableParallelSlotVisitors.append(slotVisitor); |
| 445 | } |
| 446 | }); |
| 447 | |
| 448 | m_slotVisitor.didStartMarking(); |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 449 | } |
fpizlo@apple.com | 5f86606 | 2015-09-26 18:07:09 +0000 | [diff] [blame] | 450 | |
oliver@apple.com | 7379261 | 2011-10-12 00:24:12 +0000 | [diff] [blame] | 451 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 452 | SuperSamplerScope superSamplerScope(false); |
| 453 | TimingScope convergenceTimingScope(*this, "Heap::markRoots convergence"); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 454 | ParallelModeEnabler enabler(m_slotVisitor); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 455 | |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 456 | m_slotVisitor.donateAndDrain(); |
fpizlo@apple.com | 7b23164 | 2016-10-11 23:52:02 +0000 | [diff] [blame] | 457 | |
| 458 | { |
| 459 | TimingScope preConvergenceTimingScope(*this, "Heap::markRoots conservative scan"); |
| 460 | ConservativeRoots conservativeRoots(*this); |
| 461 | SuperSamplerScope superSamplerScope(false); |
| 462 | gatherStackRoots(conservativeRoots, stackOrigin, stackTop, calleeSavedRegisters); |
| 463 | gatherJSStackRoots(conservativeRoots); |
| 464 | gatherScratchBufferRoots(conservativeRoots); |
| 465 | visitConservativeRoots(conservativeRoots); |
| 466 | |
| 467 | // We want to do this to conservatively ensure that we rescan any code blocks that are |
| 468 | // running right now. However, we need to be sure to do it *after* we mark the code block |
| 469 | // so that we know for sure if it really needs a barrier. |
| 470 | m_codeBlocks->writeBarrierCurrentlyExecuting(this); |
| 471 | } |
| 472 | |
mhahnenberg@apple.com | 1629c06 | 2014-04-15 21:05:09 +0000 | [diff] [blame] | 473 | visitExternalRememberedSet(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 474 | visitSmallStrings(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 475 | visitProtectedObjects(heapRootVisitor); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 476 | visitArgumentBuffers(heapRootVisitor); |
| 477 | visitException(heapRootVisitor); |
| 478 | visitStrongHandles(heapRootVisitor); |
| 479 | visitHandleStack(heapRootVisitor); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 480 | visitSamplingProfiler(); |
fpizlo@apple.com | 39303e0 | 2016-04-05 22:17:35 +0000 | [diff] [blame] | 481 | visitShadowChicken(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 482 | traceCodeBlocksAndJITStubRoutines(); |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 483 | m_slotVisitor.drainFromShared(SlotVisitor::MasterDrain); |
oliver@apple.com | 32b720a | 2011-10-05 19:07:18 +0000 | [diff] [blame] | 484 | } |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 485 | |
| 486 | TimingScope postConvergenceTimingScope(*this, "Heap::markRoots after convergence"); |
ggaren@apple.com | 6d8758c | 2011-04-14 23:20:25 +0000 | [diff] [blame] | 487 | |
ggaren@apple.com | 7621581 | 2012-04-04 05:28:13 +0000 | [diff] [blame] | 488 | // Weak references must be marked last because their liveness depends on |
| 489 | // the liveness of the rest of the object graph. |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 490 | visitWeakHandles(heapRootVisitor); |
ggaren@apple.com | 7621581 | 2012-04-04 05:28:13 +0000 | [diff] [blame] | 491 | |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 492 | { |
| 493 | std::lock_guard<Lock> lock(m_markingMutex); |
| 494 | m_parallelMarkersShouldExit = true; |
| 495 | m_markingConditionVariable.notifyAll(); |
| 496 | } |
commit-queue@webkit.org | 5177507 | 2015-09-30 17:17:27 +0000 | [diff] [blame] | 497 | m_helperClient.finish(); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 498 | updateObjectCounts(gcStartTime); |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 499 | endMarking(); |
mhahnenberg@apple.com | 02e39c7 | 2012-10-12 19:38:35 +0000 | [diff] [blame] | 500 | } |
| 501 | |
akling@apple.com | 09cd9c0 | 2015-03-05 02:19:14 +0000 | [diff] [blame] | 502 | void Heap::gatherStackRoots(ConservativeRoots& roots, void* stackOrigin, void* stackTop, MachineThreads::RegisterState& calleeSavedRegisters) |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 503 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 504 | m_jitStubRoutines->clearMarks(); |
| 505 | m_machineThreads.gatherConservativeRoots(roots, *m_jitStubRoutines, *m_codeBlocks, stackOrigin, stackTop, calleeSavedRegisters); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void Heap::gatherJSStackRoots(ConservativeRoots& roots) |
| 509 | { |
dbatyai.u-szeged@partner.samsung.com | 46f07e5 | 2014-06-19 16:32:31 +0000 | [diff] [blame] | 510 | #if !ENABLE(JIT) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 511 | m_vm->interpreter->cloopStack().gatherConservativeRoots(roots, *m_jitStubRoutines, *m_codeBlocks); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 512 | #else |
| 513 | UNUSED_PARAM(roots); |
| 514 | #endif |
| 515 | } |
| 516 | |
| 517 | void Heap::gatherScratchBufferRoots(ConservativeRoots& roots) |
| 518 | { |
| 519 | #if ENABLE(DFG_JIT) |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 520 | m_vm->gatherConservativeRoots(roots); |
| 521 | #else |
| 522 | UNUSED_PARAM(roots); |
| 523 | #endif |
| 524 | } |
| 525 | |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 526 | void Heap::beginMarking() |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 527 | { |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 528 | TimingScope timingScope(*this, "Heap::beginMarking"); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 529 | if (m_collectionScope == CollectionScope::Full) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 530 | m_codeBlocks->clearMarksForFullCollection(); |
| 531 | |
| 532 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 533 | TimingScope clearMarksTimingScope(*this, "m_objectSpace.beginMarking"); |
| 534 | m_objectSpace.beginMarking(); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 535 | } |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 536 | } |
| 537 | |
mhahnenberg@apple.com | 1629c06 | 2014-04-15 21:05:09 +0000 | [diff] [blame] | 538 | void Heap::visitExternalRememberedSet() |
| 539 | { |
| 540 | #if JSC_OBJC_API_ENABLED |
| 541 | scanExternalRememberedSet(*m_vm, m_slotVisitor); |
| 542 | #endif |
| 543 | } |
| 544 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 545 | void Heap::visitSmallStrings() |
| 546 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 547 | if (!m_vm->smallStrings.needsToBeVisited(*m_collectionScope)) |
mhahnenb@gmail.com | 3868776 | 2015-01-23 07:04:05 +0000 | [diff] [blame] | 548 | return; |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 549 | |
mhahnenb@gmail.com | 3868776 | 2015-01-23 07:04:05 +0000 | [diff] [blame] | 550 | m_vm->smallStrings.visitStrongReferences(m_slotVisitor); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 551 | if (Options::logGC() == GCLogging::Verbose) |
| 552 | dataLog("Small strings:\n", m_slotVisitor); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 553 | m_slotVisitor.donateAndDrain(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | void Heap::visitConservativeRoots(ConservativeRoots& roots) |
| 557 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 558 | m_slotVisitor.append(roots); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 559 | |
| 560 | if (Options::logGC() == GCLogging::Verbose) |
| 561 | dataLog("Conservative Roots:\n", m_slotVisitor); |
| 562 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 563 | m_slotVisitor.donateAndDrain(); |
| 564 | } |
| 565 | |
fpizlo@apple.com | 04a048c | 2014-04-28 19:01:07 +0000 | [diff] [blame] | 566 | void Heap::visitCompilerWorklistWeakReferences() |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 567 | { |
| 568 | #if ENABLE(DFG_JIT) |
mark.lam@apple.com | a906054 | 2014-04-24 00:43:15 +0000 | [diff] [blame] | 569 | for (auto worklist : m_suspendedCompilerWorklists) |
ggaren@apple.com | 51be9a2 | 2015-09-09 22:00:58 +0000 | [diff] [blame] | 570 | worklist->visitWeakReferences(m_slotVisitor); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 571 | |
| 572 | if (Options::logGC() == GCLogging::Verbose) |
| 573 | dataLog("DFG Worklists:\n", m_slotVisitor); |
fpizlo@apple.com | 04a048c | 2014-04-28 19:01:07 +0000 | [diff] [blame] | 574 | #endif |
| 575 | } |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 576 | |
fpizlo@apple.com | 04a048c | 2014-04-28 19:01:07 +0000 | [diff] [blame] | 577 | void Heap::removeDeadCompilerWorklistEntries() |
| 578 | { |
| 579 | #if ENABLE(DFG_JIT) |
fpizlo@apple.com | 04a048c | 2014-04-28 19:01:07 +0000 | [diff] [blame] | 580 | for (auto worklist : m_suspendedCompilerWorklists) |
| 581 | worklist->removeDeadPlans(*m_vm); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 582 | #endif |
| 583 | } |
| 584 | |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 585 | bool Heap::isHeapSnapshotting() const |
| 586 | { |
| 587 | HeapProfiler* heapProfiler = m_vm->heapProfiler(); |
| 588 | if (UNLIKELY(heapProfiler)) |
| 589 | return heapProfiler->activeSnapshotBuilder(); |
| 590 | return false; |
| 591 | } |
| 592 | |
commit-queue@webkit.org | c4c0f56 | 2016-03-07 23:45:38 +0000 | [diff] [blame] | 593 | struct GatherHeapSnapshotData : MarkedBlock::CountFunctor { |
| 594 | GatherHeapSnapshotData(HeapSnapshotBuilder& builder) |
| 595 | : m_builder(builder) |
| 596 | { |
| 597 | } |
| 598 | |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 599 | IterationStatus operator()(HeapCell* heapCell, HeapCell::Kind kind) const |
commit-queue@webkit.org | c4c0f56 | 2016-03-07 23:45:38 +0000 | [diff] [blame] | 600 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 601 | if (kind == HeapCell::JSCell) { |
| 602 | JSCell* cell = static_cast<JSCell*>(heapCell); |
| 603 | cell->methodTable()->heapSnapshot(cell, m_builder); |
| 604 | } |
commit-queue@webkit.org | c4c0f56 | 2016-03-07 23:45:38 +0000 | [diff] [blame] | 605 | return IterationStatus::Continue; |
| 606 | } |
| 607 | |
| 608 | HeapSnapshotBuilder& m_builder; |
| 609 | }; |
| 610 | |
| 611 | void Heap::gatherExtraHeapSnapshotData(HeapProfiler& heapProfiler) |
| 612 | { |
commit-queue@webkit.org | c4c0f56 | 2016-03-07 23:45:38 +0000 | [diff] [blame] | 613 | if (HeapSnapshotBuilder* builder = heapProfiler.activeSnapshotBuilder()) { |
| 614 | HeapIterationScope heapIterationScope(*this); |
| 615 | GatherHeapSnapshotData functor(*builder); |
| 616 | m_objectSpace.forEachLiveCell(heapIterationScope, functor); |
| 617 | } |
| 618 | } |
| 619 | |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 620 | struct RemoveDeadHeapSnapshotNodes : MarkedBlock::CountFunctor { |
| 621 | RemoveDeadHeapSnapshotNodes(HeapSnapshot& snapshot) |
| 622 | : m_snapshot(snapshot) |
| 623 | { |
| 624 | } |
| 625 | |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 626 | IterationStatus operator()(HeapCell* cell, HeapCell::Kind kind) const |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 627 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 628 | if (kind == HeapCell::JSCell) |
| 629 | m_snapshot.sweepCell(static_cast<JSCell*>(cell)); |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 630 | return IterationStatus::Continue; |
| 631 | } |
| 632 | |
| 633 | HeapSnapshot& m_snapshot; |
| 634 | }; |
| 635 | |
commit-queue@webkit.org | c4c0f56 | 2016-03-07 23:45:38 +0000 | [diff] [blame] | 636 | void Heap::removeDeadHeapSnapshotNodes(HeapProfiler& heapProfiler) |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 637 | { |
commit-queue@webkit.org | c4c0f56 | 2016-03-07 23:45:38 +0000 | [diff] [blame] | 638 | if (HeapSnapshot* snapshot = heapProfiler.mostRecentSnapshot()) { |
| 639 | HeapIterationScope heapIterationScope(*this); |
| 640 | RemoveDeadHeapSnapshotNodes functor(*snapshot); |
| 641 | m_objectSpace.forEachDeadCell(heapIterationScope, functor); |
| 642 | snapshot->shrinkToFit(); |
commit-queue@webkit.org | d38d068 | 2016-03-03 05:15:56 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 646 | void Heap::visitProtectedObjects(HeapRootVisitor& heapRootVisitor) |
| 647 | { |
andersca@apple.com | cca6e6d | 2014-03-05 18:59:58 +0000 | [diff] [blame] | 648 | for (auto& pair : m_protectedValues) |
mhahnenberg@apple.com | b5b2edc | 2014-03-05 16:46:23 +0000 | [diff] [blame] | 649 | heapRootVisitor.visit(&pair.key); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 650 | |
| 651 | if (Options::logGC() == GCLogging::Verbose) |
| 652 | dataLog("Protected Objects:\n", m_slotVisitor); |
| 653 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 654 | m_slotVisitor.donateAndDrain(); |
| 655 | } |
| 656 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 657 | void Heap::visitArgumentBuffers(HeapRootVisitor& visitor) |
| 658 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 659 | if (!m_markListSet || !m_markListSet->size()) |
| 660 | return; |
| 661 | |
| 662 | MarkedArgumentBuffer::markLists(visitor, *m_markListSet); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 663 | |
| 664 | if (Options::logGC() == GCLogging::Verbose) |
| 665 | dataLog("Argument Buffers:\n", m_slotVisitor); |
| 666 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 667 | m_slotVisitor.donateAndDrain(); |
| 668 | } |
| 669 | |
| 670 | void Heap::visitException(HeapRootVisitor& visitor) |
| 671 | { |
mark.lam@apple.com | b7a6737 | 2015-06-12 05:39:28 +0000 | [diff] [blame] | 672 | if (!m_vm->exception() && !m_vm->lastException()) |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 673 | return; |
| 674 | |
| 675 | visitor.visit(m_vm->addressOfException()); |
mark.lam@apple.com | b7a6737 | 2015-06-12 05:39:28 +0000 | [diff] [blame] | 676 | visitor.visit(m_vm->addressOfLastException()); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 677 | |
| 678 | if (Options::logGC() == GCLogging::Verbose) |
| 679 | dataLog("Exceptions:\n", m_slotVisitor); |
| 680 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 681 | m_slotVisitor.donateAndDrain(); |
| 682 | } |
| 683 | |
| 684 | void Heap::visitStrongHandles(HeapRootVisitor& visitor) |
| 685 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 686 | m_handleSet.visitStrongHandles(visitor); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 687 | |
| 688 | if (Options::logGC() == GCLogging::Verbose) |
| 689 | dataLog("Strong Handles:\n", m_slotVisitor); |
| 690 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 691 | m_slotVisitor.donateAndDrain(); |
| 692 | } |
| 693 | |
| 694 | void Heap::visitHandleStack(HeapRootVisitor& visitor) |
| 695 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 696 | m_handleStack.visit(visitor); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 697 | |
| 698 | if (Options::logGC() == GCLogging::Verbose) |
| 699 | dataLog("Handle Stack:\n", m_slotVisitor); |
| 700 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 701 | m_slotVisitor.donateAndDrain(); |
| 702 | } |
| 703 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 704 | void Heap::visitSamplingProfiler() |
| 705 | { |
| 706 | #if ENABLE(SAMPLING_PROFILER) |
| 707 | if (SamplingProfiler* samplingProfiler = m_vm->samplingProfiler()) { |
| 708 | ASSERT(samplingProfiler->getLock().isLocked()); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 709 | samplingProfiler->visit(m_slotVisitor); |
| 710 | if (Options::logGC() == GCLogging::Verbose) |
| 711 | dataLog("Sampling Profiler data:\n", m_slotVisitor); |
| 712 | |
| 713 | m_slotVisitor.donateAndDrain(); |
| 714 | samplingProfiler->getLock().unlock(); |
| 715 | } |
| 716 | #endif // ENABLE(SAMPLING_PROFILER) |
| 717 | } |
| 718 | |
fpizlo@apple.com | 39303e0 | 2016-04-05 22:17:35 +0000 | [diff] [blame] | 719 | void Heap::visitShadowChicken() |
| 720 | { |
| 721 | m_vm->shadowChicken().visitChildren(m_slotVisitor); |
| 722 | } |
| 723 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 724 | void Heap::traceCodeBlocksAndJITStubRoutines() |
| 725 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 726 | m_jitStubRoutines->traceMarkedStubRoutines(m_slotVisitor); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 727 | |
| 728 | if (Options::logGC() == GCLogging::Verbose) |
| 729 | dataLog("Code Blocks and JIT Stub Routines:\n", m_slotVisitor); |
| 730 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 731 | m_slotVisitor.donateAndDrain(); |
| 732 | } |
| 733 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 734 | void Heap::visitWeakHandles(HeapRootVisitor& visitor) |
| 735 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 736 | TimingScope timingScope(*this, "Heap::visitWeakHandles"); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 737 | while (true) { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 738 | { |
| 739 | TimingScope timingScope(*this, "m_objectSpace.visitWeakSets"); |
| 740 | m_objectSpace.visitWeakSets(visitor); |
| 741 | } |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 742 | harvestWeakReferences(); |
fpizlo@apple.com | 04a048c | 2014-04-28 19:01:07 +0000 | [diff] [blame] | 743 | visitCompilerWorklistWeakReferences(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 744 | if (m_slotVisitor.isEmpty()) |
| 745 | break; |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 746 | |
| 747 | if (Options::logGC() == GCLogging::Verbose) |
| 748 | dataLog("Live Weak Handles:\n", m_slotVisitor); |
| 749 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 750 | { |
| 751 | ParallelModeEnabler enabler(m_slotVisitor); |
| 752 | m_slotVisitor.donateAndDrain(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 753 | m_slotVisitor.drainFromShared(SlotVisitor::MasterDrain); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 758 | void Heap::updateObjectCounts(double gcStartTime) |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 759 | { |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 760 | if (Options::logGC() == GCLogging::Verbose) { |
| 761 | size_t visitCount = m_slotVisitor.visitCount(); |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 762 | visitCount += threadVisitCount(); |
ossy@webkit.org | 2bf2cbb | 2014-04-06 22:12:09 +0000 | [diff] [blame] | 763 | dataLogF("\nNumber of live Objects after GC %lu, took %.6f secs\n", static_cast<unsigned long>(visitCount), WTF::monotonicallyIncreasingTime() - gcStartTime); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 764 | } |
fpizlo@apple.com | dea6d26 | 2015-05-08 02:12:35 +0000 | [diff] [blame] | 765 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 766 | if (m_collectionScope == CollectionScope::Full) |
fpizlo@apple.com | dea6d26 | 2015-05-08 02:12:35 +0000 | [diff] [blame] | 767 | m_totalBytesVisited = 0; |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 768 | |
| 769 | m_totalBytesVisitedThisCycle = m_slotVisitor.bytesVisited() + threadBytesVisited(); |
fpizlo@apple.com | dea6d26 | 2015-05-08 02:12:35 +0000 | [diff] [blame] | 770 | |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 771 | m_totalBytesVisited += m_totalBytesVisitedThisCycle; |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 772 | } |
| 773 | |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 774 | void Heap::endMarking() |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 775 | { |
| 776 | m_slotVisitor.reset(); |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 777 | |
fpizlo@apple.com | 5f86606 | 2015-09-26 18:07:09 +0000 | [diff] [blame] | 778 | for (auto& parallelVisitor : m_parallelSlotVisitors) |
| 779 | parallelVisitor->reset(); |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 780 | |
| 781 | ASSERT(m_sharedMarkStack.isEmpty()); |
| 782 | m_weakReferenceHarvesters.removeAll(); |
fpizlo@apple.com | 71bb6d2 | 2016-09-13 04:33:19 +0000 | [diff] [blame] | 783 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 784 | m_objectSpace.endMarking(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 785 | } |
| 786 | |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 787 | size_t Heap::objectCount() |
mjs | 6d32b07 | 2002-11-20 09:34:02 +0000 | [diff] [blame] | 788 | { |
ggaren@apple.com | 041d0a2 | 2012-05-18 00:40:30 +0000 | [diff] [blame] | 789 | return m_objectSpace.objectCount(); |
mjs | 6d32b07 | 2002-11-20 09:34:02 +0000 | [diff] [blame] | 790 | } |
| 791 | |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 792 | size_t Heap::extraMemorySize() |
fpizlo@apple.com | 0e0d931 | 2013-08-15 20:43:06 +0000 | [diff] [blame] | 793 | { |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 794 | return m_extraMemorySize + m_deprecatedExtraMemorySize + m_arrayBuffers.size(); |
fpizlo@apple.com | 0e0d931 | 2013-08-15 20:43:06 +0000 | [diff] [blame] | 795 | } |
| 796 | |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 797 | size_t Heap::size() |
eric@webkit.org | 30dc2a3 | 2010-05-15 08:46:23 +0000 | [diff] [blame] | 798 | { |
fpizlo@apple.com | d1725cb | 2016-09-08 22:12:05 +0000 | [diff] [blame] | 799 | return m_objectSpace.size() + extraMemorySize(); |
eric@webkit.org | 30dc2a3 | 2010-05-15 08:46:23 +0000 | [diff] [blame] | 800 | } |
| 801 | |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 802 | size_t Heap::capacity() |
ggaren@apple.com | 0705026 | 2011-01-30 03:32:52 +0000 | [diff] [blame] | 803 | { |
fpizlo@apple.com | d1725cb | 2016-09-08 22:12:05 +0000 | [diff] [blame] | 804 | return m_objectSpace.capacity() + extraMemorySize(); |
darin | 52b4943 | 2002-05-09 00:11:06 +0000 | [diff] [blame] | 805 | } |
| 806 | |
ap@webkit.org | 960c28e | 2008-06-19 17:29:29 +0000 | [diff] [blame] | 807 | size_t Heap::protectedGlobalObjectCount() |
ggaren@apple.com | 6e4309e | 2008-01-11 09:52:27 +0000 | [diff] [blame] | 808 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 809 | size_t result = 0; |
| 810 | forEachProtectedCell( |
| 811 | [&] (JSCell* cell) { |
| 812 | if (cell->isObject() && asObject(cell)->isGlobalObject()) |
| 813 | result++; |
| 814 | }); |
| 815 | return result; |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 816 | } |
ggaren@apple.com | 79ab2a9 | 2011-02-02 05:05:55 +0000 | [diff] [blame] | 817 | |
ggaren@apple.com | 8a23d6a | 2011-06-09 19:44:37 +0000 | [diff] [blame] | 818 | size_t Heap::globalObjectCount() |
| 819 | { |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 820 | HeapIterationScope iterationScope(*this); |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 821 | size_t result = 0; |
| 822 | m_objectSpace.forEachLiveCell( |
| 823 | iterationScope, |
| 824 | [&] (HeapCell* heapCell, HeapCell::Kind kind) -> IterationStatus { |
| 825 | if (kind != HeapCell::JSCell) |
| 826 | return IterationStatus::Continue; |
| 827 | JSCell* cell = static_cast<JSCell*>(heapCell); |
| 828 | if (cell->isObject() && asObject(cell)->isGlobalObject()) |
| 829 | result++; |
| 830 | return IterationStatus::Continue; |
| 831 | }); |
| 832 | return result; |
ggaren@apple.com | 6e4309e | 2008-01-11 09:52:27 +0000 | [diff] [blame] | 833 | } |
| 834 | |
ap@webkit.org | 960c28e | 2008-06-19 17:29:29 +0000 | [diff] [blame] | 835 | size_t Heap::protectedObjectCount() |
darin | 52b4943 | 2002-05-09 00:11:06 +0000 | [diff] [blame] | 836 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 837 | size_t result = 0; |
| 838 | forEachProtectedCell( |
| 839 | [&] (JSCell*) { |
| 840 | result++; |
| 841 | }); |
| 842 | return result; |
ggaren@apple.com | b843ba8 | 2011-02-11 23:31:04 +0000 | [diff] [blame] | 843 | } |
| 844 | |
gyuyoung.kim@samsung.com | 221dd0b | 2014-11-01 00:26:36 +0000 | [diff] [blame] | 845 | std::unique_ptr<TypeCountSet> Heap::protectedObjectTypeCounts() |
ggaren@apple.com | b843ba8 | 2011-02-11 23:31:04 +0000 | [diff] [blame] | 846 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 847 | std::unique_ptr<TypeCountSet> result = std::make_unique<TypeCountSet>(); |
| 848 | forEachProtectedCell( |
| 849 | [&] (JSCell* cell) { |
| 850 | recordType(*result, cell); |
| 851 | }); |
| 852 | return result; |
oliver@apple.com | bf9b7f8 | 2011-04-14 20:17:11 +0000 | [diff] [blame] | 853 | } |
| 854 | |
gyuyoung.kim@samsung.com | 221dd0b | 2014-11-01 00:26:36 +0000 | [diff] [blame] | 855 | std::unique_ptr<TypeCountSet> Heap::objectTypeCounts() |
ggaren@apple.com | 0fabb55 | 2010-02-11 21:28:52 +0000 | [diff] [blame] | 856 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 857 | std::unique_ptr<TypeCountSet> result = std::make_unique<TypeCountSet>(); |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 858 | HeapIterationScope iterationScope(*this); |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 859 | m_objectSpace.forEachLiveCell( |
| 860 | iterationScope, |
| 861 | [&] (HeapCell* cell, HeapCell::Kind kind) -> IterationStatus { |
| 862 | if (kind == HeapCell::JSCell) |
| 863 | recordType(*result, static_cast<JSCell*>(cell)); |
| 864 | return IterationStatus::Continue; |
| 865 | }); |
| 866 | return result; |
ggaren@apple.com | 0fabb55 | 2010-02-11 21:28:52 +0000 | [diff] [blame] | 867 | } |
| 868 | |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 869 | void Heap::deleteAllCodeBlocks() |
mhahnenberg@apple.com | cab4752 | 2012-03-30 00:36:37 +0000 | [diff] [blame] | 870 | { |
ggaren@apple.com | dc5f426 | 2015-09-17 00:16:35 +0000 | [diff] [blame] | 871 | // If JavaScript is running, it's not safe to delete all JavaScript code, since |
ggaren@apple.com | 05627c5 | 2015-08-13 20:17:02 +0000 | [diff] [blame] | 872 | // we'll end up returning to deleted code. |
ggaren@apple.com | dc5f426 | 2015-09-17 00:16:35 +0000 | [diff] [blame] | 873 | RELEASE_ASSERT(!m_vm->entryScope); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 874 | ASSERT(!m_collectionScope); |
ggaren@apple.com | dc5f426 | 2015-09-17 00:16:35 +0000 | [diff] [blame] | 875 | |
fpizlo@apple.com | 46050f7 | 2016-06-17 04:48:47 +0000 | [diff] [blame] | 876 | completeAllJITPlans(); |
msaboff@apple.com | 03e811f | 2012-04-06 16:09:22 +0000 | [diff] [blame] | 877 | |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 878 | for (ExecutableBase* executable : m_executables) |
| 879 | executable->clearCode(); |
ggaren@apple.com | d714757 | 2012-06-08 18:17:16 +0000 | [diff] [blame] | 880 | } |
| 881 | |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 882 | void Heap::deleteAllUnlinkedCodeBlocks() |
commit-queue@webkit.org | c656b3d | 2015-08-20 21:09:19 +0000 | [diff] [blame] | 883 | { |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 884 | for (ExecutableBase* current : m_executables) { |
commit-queue@webkit.org | c656b3d | 2015-08-20 21:09:19 +0000 | [diff] [blame] | 885 | if (!current->isFunctionExecutable()) |
| 886 | continue; |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 887 | static_cast<FunctionExecutable*>(current)->unlinkedExecutable()->clearCode(); |
commit-queue@webkit.org | c656b3d | 2015-08-20 21:09:19 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
mhahnenberg@apple.com | 7d223bb | 2014-04-02 23:50:25 +0000 | [diff] [blame] | 891 | void Heap::clearUnmarkedExecutables() |
ggaren@apple.com | d714757 | 2012-06-08 18:17:16 +0000 | [diff] [blame] | 892 | { |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 893 | for (unsigned i = m_executables.size(); i--;) { |
| 894 | ExecutableBase* current = m_executables[i]; |
ggaren@apple.com | d714757 | 2012-06-08 18:17:16 +0000 | [diff] [blame] | 895 | if (isMarked(current)) |
| 896 | continue; |
| 897 | |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 898 | // Eagerly dereference the Executable's JITCode in order to run watchpoint |
| 899 | // destructors. Otherwise, watchpoints might fire for deleted CodeBlocks. |
| 900 | current->clearCode(); |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 901 | std::swap(m_executables[i], m_executables.last()); |
| 902 | m_executables.removeLast(); |
ggaren@apple.com | d714757 | 2012-06-08 18:17:16 +0000 | [diff] [blame] | 903 | } |
akling@apple.com | b549457 | 2016-01-30 20:48:11 +0000 | [diff] [blame] | 904 | |
| 905 | m_executables.shrinkToFit(); |
mhahnenberg@apple.com | 7d223bb | 2014-04-02 23:50:25 +0000 | [diff] [blame] | 906 | } |
ggaren@apple.com | d714757 | 2012-06-08 18:17:16 +0000 | [diff] [blame] | 907 | |
mhahnenberg@apple.com | 7d223bb | 2014-04-02 23:50:25 +0000 | [diff] [blame] | 908 | void Heap::deleteUnmarkedCompiledCode() |
| 909 | { |
mhahnenberg@apple.com | 7d223bb | 2014-04-02 23:50:25 +0000 | [diff] [blame] | 910 | clearUnmarkedExecutables(); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 911 | m_codeBlocks->deleteUnmarkedAndUnreferenced(*m_collectionScope); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 912 | m_jitStubRoutines->deleteUnmarkedJettisonedStubRoutines(); |
mhahnenberg@apple.com | cab4752 | 2012-03-30 00:36:37 +0000 | [diff] [blame] | 913 | } |
| 914 | |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 915 | void Heap::addToRememberedSet(const JSCell* cell) |
| 916 | { |
| 917 | ASSERT(cell); |
mark.lam@apple.com | ee3c410 | 2015-10-14 18:57:07 +0000 | [diff] [blame] | 918 | ASSERT(!Options::useConcurrentJIT() || !isCompilationThread()); |
fpizlo@apple.com | 261fc22 | 2016-10-12 23:56:34 +0000 | [diff] [blame] | 919 | ASSERT(cell->cellState() == CellState::AnthraciteOrBlack); |
fpizlo@apple.com | 3cb36ea | 2015-10-05 19:35:32 +0000 | [diff] [blame] | 920 | // Indicate that this object is grey and that it's one of the following: |
| 921 | // - A re-greyed object during a concurrent collection. |
| 922 | // - An old remembered object. |
| 923 | // "OldGrey" doesn't tell us which of these things is true, but we usually treat the two cases the |
| 924 | // same. |
| 925 | cell->setCellState(CellState::OldGrey); |
ggaren@apple.com | c00601e | 2015-10-05 17:22:20 +0000 | [diff] [blame] | 926 | m_slotVisitor.appendToMarkStack(const_cast<JSCell*>(cell)); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 927 | } |
| 928 | |
mark.lam@apple.com | 38ef954 | 2016-08-11 21:18:14 +0000 | [diff] [blame] | 929 | void Heap::collectAllGarbage() |
ggaren@apple.com | 9f98034 | 2008-10-15 23:33:07 +0000 | [diff] [blame] | 930 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 931 | SuperSamplerScope superSamplerScope(false); |
commit-queue@webkit.org | f42601f | 2011-07-12 22:35:39 +0000 | [diff] [blame] | 932 | if (!m_isSafeToCollect) |
| 933 | return; |
mhahnenberg@apple.com | 5bc6cc3 | 2014-01-07 17:39:52 +0000 | [diff] [blame] | 934 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 935 | collectWithoutAnySweep(CollectionScope::Full); |
mhahnenberg@apple.com | 5bc6cc3 | 2014-01-07 17:39:52 +0000 | [diff] [blame] | 936 | |
ggaren@apple.com | a1cb376 | 2015-03-13 20:14:39 +0000 | [diff] [blame] | 937 | DeferGCForAWhile deferGC(*this); |
mark.lam@apple.com | 767e425 | 2016-08-17 00:18:43 +0000 | [diff] [blame] | 938 | if (UNLIKELY(Options::useImmortalObjects())) |
| 939 | sweeper()->willFinishSweeping(); |
| 940 | else { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 941 | double before = 0; |
| 942 | if (Options::logGC()) { |
| 943 | dataLog("[Full sweep: ", capacity() / 1024, " kb "); |
| 944 | before = currentTimeMS(); |
| 945 | } |
mark.lam@apple.com | 767e425 | 2016-08-17 00:18:43 +0000 | [diff] [blame] | 946 | m_objectSpace.sweep(); |
| 947 | m_objectSpace.shrink(); |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 948 | if (Options::logGC()) { |
| 949 | double after = currentTimeMS(); |
| 950 | dataLog("=> ", capacity() / 1024, " kb, ", after - before, " ms]\n"); |
| 951 | } |
mark.lam@apple.com | 767e425 | 2016-08-17 00:18:43 +0000 | [diff] [blame] | 952 | } |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 953 | m_objectSpace.assertNoUnswept(); |
akling@apple.com | e92da67 | 2015-04-04 23:16:21 +0000 | [diff] [blame] | 954 | |
| 955 | sweepAllLogicallyEmptyWeakBlocks(); |
ggaren@apple.com | 7ddf61b | 2011-01-30 23:07:11 +0000 | [diff] [blame] | 956 | } |
| 957 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 958 | void Heap::collect(Optional<CollectionScope> scope) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 959 | { |
| 960 | SuperSamplerScope superSamplerScope(false); |
| 961 | if (!m_isSafeToCollect) |
| 962 | return; |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 963 | |
| 964 | collectWithoutAnySweep(scope); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 965 | } |
| 966 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 967 | NEVER_INLINE void Heap::collectWithoutAnySweep(Optional<CollectionScope> scope) |
akling@apple.com | 09cd9c0 | 2015-03-05 02:19:14 +0000 | [diff] [blame] | 968 | { |
| 969 | void* stackTop; |
| 970 | ALLOCATE_AND_GET_REGISTER_STATE(registers); |
| 971 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 972 | collectImpl(scope, wtfThreadData().stack().origin(), &stackTop, registers); |
akling@apple.com | 09cd9c0 | 2015-03-05 02:19:14 +0000 | [diff] [blame] | 973 | |
| 974 | sanitizeStackForVM(m_vm); |
| 975 | } |
| 976 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 977 | NEVER_INLINE void Heap::collectImpl(Optional<CollectionScope> scope, void* stackOrigin, void* stackTop, MachineThreads::RegisterState& calleeSavedRegisters) |
ggaren@apple.com | 7ddf61b | 2011-01-30 23:07:11 +0000 | [diff] [blame] | 978 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 979 | SuperSamplerScope superSamplerScope(false); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 980 | TimingScope collectImplTimingScope(scope, "Heap::collectImpl"); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 981 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 982 | #if ENABLE(ALLOCATION_LOGGING) |
| 983 | dataLogF("JSC GC starting collection.\n"); |
| 984 | #endif |
| 985 | |
fpizlo@apple.com | 0e0d931 | 2013-08-15 20:43:06 +0000 | [diff] [blame] | 986 | double before = 0; |
| 987 | if (Options::logGC()) { |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 988 | dataLog("[GC: ", capacity() / 1024, " kb "); |
fpizlo@apple.com | 0e0d931 | 2013-08-15 20:43:06 +0000 | [diff] [blame] | 989 | before = currentTimeMS(); |
| 990 | } |
| 991 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 992 | double gcStartTime; |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 993 | |
| 994 | if (vm()->typeProfiler()) { |
| 995 | DeferGCForAWhile awhile(*this); |
| 996 | vm()->typeProfilerLog()->processLogEntries(ASCIILiteral("GC")); |
| 997 | } |
| 998 | |
fpizlo@apple.com | 46050f7 | 2016-06-17 04:48:47 +0000 | [diff] [blame] | 999 | #if ENABLE(JIT) |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1000 | { |
| 1001 | DeferGCForAWhile awhile(*this); |
| 1002 | JITWorklist::instance()->completeAllForVM(*m_vm); |
| 1003 | } |
fpizlo@apple.com | 46050f7 | 2016-06-17 04:48:47 +0000 | [diff] [blame] | 1004 | #endif // ENABLE(JIT) |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1005 | |
| 1006 | vm()->shadowChicken().update(*vm(), vm()->topCallFrame); |
| 1007 | |
| 1008 | RELEASE_ASSERT(!m_deferralDepth); |
| 1009 | ASSERT(vm()->currentThreadIsHoldingAPILock()); |
| 1010 | RELEASE_ASSERT(vm()->atomicStringTable() == wtfThreadData().atomicStringTable()); |
| 1011 | ASSERT(m_isSafeToCollect); |
| 1012 | RELEASE_ASSERT(!m_collectionScope); |
| 1013 | |
| 1014 | suspendCompilerThreads(); |
| 1015 | willStartCollection(scope); |
| 1016 | { |
| 1017 | HelpingGCScope helpingHeapScope(*this); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1018 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1019 | collectImplTimingScope.setScope(*this); |
| 1020 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1021 | gcStartTime = WTF::monotonicallyIncreasingTime(); |
| 1022 | if (m_verifier) { |
| 1023 | // Verify that live objects from the last GC cycle haven't been corrupted by |
| 1024 | // mutators before we begin this new GC cycle. |
| 1025 | m_verifier->verify(HeapVerifier::Phase::BeforeGC); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1026 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1027 | m_verifier->initializeGCCycle(); |
| 1028 | m_verifier->gatherLiveObjects(HeapVerifier::Phase::BeforeMarking); |
| 1029 | } |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1030 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1031 | flushOldStructureIDTables(); |
| 1032 | stopAllocation(); |
| 1033 | prepareForMarking(); |
| 1034 | flushWriteBarrierBuffer(); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1035 | |
sbarati@apple.com | a3db465 | 2016-09-20 01:05:50 +0000 | [diff] [blame] | 1036 | if (HasOwnPropertyCache* cache = vm()->hasOwnPropertyCache()) |
| 1037 | cache->clear(); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1038 | |
| 1039 | markRoots(gcStartTime, stackOrigin, stackTop, calleeSavedRegisters); |
| 1040 | |
| 1041 | if (m_verifier) { |
| 1042 | m_verifier->gatherLiveObjects(HeapVerifier::Phase::AfterMarking); |
| 1043 | m_verifier->verify(HeapVerifier::Phase::AfterMarking); |
| 1044 | } |
| 1045 | |
| 1046 | if (vm()->typeProfiler()) |
| 1047 | vm()->typeProfiler()->invalidateTypeSetCache(); |
| 1048 | |
| 1049 | reapWeakHandles(); |
| 1050 | pruneStaleEntriesFromWeakGCMaps(); |
| 1051 | sweepArrayBuffers(); |
| 1052 | snapshotUnswept(); |
| 1053 | finalizeUnconditionalFinalizers(); |
| 1054 | removeDeadCompilerWorklistEntries(); |
| 1055 | deleteUnmarkedCompiledCode(); |
| 1056 | deleteSourceProviderCaches(); |
| 1057 | |
| 1058 | notifyIncrementalSweeper(); |
| 1059 | m_codeBlocks->writeBarrierCurrentlyExecuting(this); |
| 1060 | m_codeBlocks->clearCurrentlyExecuting(); |
| 1061 | |
| 1062 | prepareForAllocation(); |
| 1063 | updateAllocationLimits(); |
mark.lam@apple.com | 34e4667 | 2014-11-20 23:28:41 +0000 | [diff] [blame] | 1064 | } |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1065 | didFinishCollection(gcStartTime); |
| 1066 | resumeCompilerThreads(); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1067 | sweepLargeAllocations(); |
| 1068 | |
mark.lam@apple.com | 34e4667 | 2014-11-20 23:28:41 +0000 | [diff] [blame] | 1069 | if (m_verifier) { |
| 1070 | m_verifier->trimDeadObjects(); |
| 1071 | m_verifier->verify(HeapVerifier::Phase::AfterGC); |
| 1072 | } |
| 1073 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1074 | if (Options::logGC()) { |
| 1075 | double after = currentTimeMS(); |
| 1076 | dataLog(after - before, " ms]\n"); |
| 1077 | } |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (false) { |
| 1080 | dataLog("Heap state after GC:\n"); |
| 1081 | m_objectSpace.dumpBits(); |
| 1082 | } |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1085 | void Heap::sweepLargeAllocations() |
| 1086 | { |
| 1087 | m_objectSpace.sweepLargeAllocations(); |
| 1088 | } |
| 1089 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1090 | void Heap::suspendCompilerThreads() |
| 1091 | { |
fpizlo@apple.com | 1b84a42 | 2014-02-08 05:06:33 +0000 | [diff] [blame] | 1092 | #if ENABLE(DFG_JIT) |
mark.lam@apple.com | a906054 | 2014-04-24 00:43:15 +0000 | [diff] [blame] | 1093 | ASSERT(m_suspendedCompilerWorklists.isEmpty()); |
fpizlo@apple.com | 1b84a42 | 2014-02-08 05:06:33 +0000 | [diff] [blame] | 1094 | for (unsigned i = DFG::numberOfWorklists(); i--;) { |
mark.lam@apple.com | a906054 | 2014-04-24 00:43:15 +0000 | [diff] [blame] | 1095 | if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i)) { |
| 1096 | m_suspendedCompilerWorklists.append(worklist); |
fpizlo@apple.com | 1b84a42 | 2014-02-08 05:06:33 +0000 | [diff] [blame] | 1097 | worklist->suspendAllThreads(); |
mark.lam@apple.com | a906054 | 2014-04-24 00:43:15 +0000 | [diff] [blame] | 1098 | } |
mhahnenberg@apple.com | 7a22bab | 2013-12-10 19:37:29 +0000 | [diff] [blame] | 1099 | } |
fpizlo@apple.com | 1b84a42 | 2014-02-08 05:06:33 +0000 | [diff] [blame] | 1100 | #endif |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1101 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1102 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1103 | void Heap::willStartCollection(Optional<CollectionScope> scope) |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1104 | { |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1105 | if (Options::logGC()) |
| 1106 | dataLog("=> "); |
| 1107 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1108 | if (shouldDoFullCollection(scope)) { |
| 1109 | m_collectionScope = CollectionScope::Full; |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1110 | m_shouldDoFullCollection = false; |
| 1111 | if (Options::logGC()) |
| 1112 | dataLog("FullCollection, "); |
| 1113 | } else { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1114 | m_collectionScope = CollectionScope::Eden; |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1115 | if (Options::logGC()) |
| 1116 | dataLog("EdenCollection, "); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1117 | } |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1118 | if (m_collectionScope == CollectionScope::Full) { |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1119 | m_sizeBeforeLastFullCollect = m_sizeAfterLastCollect + m_bytesAllocatedThisCycle; |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 1120 | m_extraMemorySize = 0; |
| 1121 | m_deprecatedExtraMemorySize = 0; |
commit-queue@webkit.org | eab925d | 2016-06-23 20:05:23 +0000 | [diff] [blame] | 1122 | #if ENABLE(RESOURCE_USAGE) |
| 1123 | m_externalMemorySize = 0; |
| 1124 | #endif |
mhahnenberg@apple.com | 2b64eec0 | 2012-04-18 16:18:32 +0000 | [diff] [blame] | 1125 | |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1126 | if (m_fullActivityCallback) |
| 1127 | m_fullActivityCallback->willCollect(); |
| 1128 | } else { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1129 | ASSERT(m_collectionScope == CollectionScope::Eden); |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1130 | m_sizeBeforeLastEdenCollect = m_sizeAfterLastCollect + m_bytesAllocatedThisCycle; |
| 1131 | } |
| 1132 | |
| 1133 | if (m_edenActivityCallback) |
| 1134 | m_edenActivityCallback->willCollect(); |
joepeck@webkit.org | d3b8b37 | 2015-10-16 02:15:56 +0000 | [diff] [blame] | 1135 | |
| 1136 | for (auto* observer : m_observers) |
| 1137 | observer->willGarbageCollect(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1138 | } |
mhahnenberg@apple.com | 0eb606f | 2012-04-20 19:55:21 +0000 | [diff] [blame] | 1139 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1140 | void Heap::flushOldStructureIDTables() |
| 1141 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1142 | m_structureIDTable.flushOldTables(); |
| 1143 | } |
| 1144 | |
| 1145 | void Heap::flushWriteBarrierBuffer() |
| 1146 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1147 | if (m_collectionScope == CollectionScope::Eden) { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1148 | m_writeBarrierBuffer.flush(*this); |
| 1149 | return; |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1150 | } |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1151 | m_writeBarrierBuffer.reset(); |
| 1152 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1153 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1154 | void Heap::stopAllocation() |
| 1155 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1156 | m_objectSpace.stopAllocating(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1157 | } |
mhahnenberg@apple.com | 02e39c7 | 2012-10-12 19:38:35 +0000 | [diff] [blame] | 1158 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1159 | void Heap::prepareForMarking() |
| 1160 | { |
| 1161 | m_objectSpace.prepareForMarking(); |
| 1162 | } |
| 1163 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1164 | void Heap::reapWeakHandles() |
| 1165 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1166 | m_objectSpace.reapWeakSets(); |
| 1167 | } |
ggaren@apple.com | 39281e2 | 2012-05-24 21:18:10 +0000 | [diff] [blame] | 1168 | |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 1169 | void Heap::pruneStaleEntriesFromWeakGCMaps() |
| 1170 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1171 | if (m_collectionScope != CollectionScope::Full) |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | for (auto& pruneCallback : m_weakGCMaps.values()) |
| 1174 | pruneCallback(); |
| 1175 | } |
| 1176 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1177 | void Heap::sweepArrayBuffers() |
| 1178 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1179 | m_arrayBuffers.sweep(); |
| 1180 | } |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 1181 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 1182 | void Heap::snapshotUnswept() |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1183 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 1184 | TimingScope timingScope(*this, "Heap::snapshotUnswept"); |
| 1185 | m_objectSpace.snapshotUnswept(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1186 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1187 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1188 | void Heap::deleteSourceProviderCaches() |
| 1189 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1190 | m_vm->clearSourceProviderCaches(); |
| 1191 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1192 | |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1193 | void Heap::notifyIncrementalSweeper() |
| 1194 | { |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1195 | if (m_collectionScope == CollectionScope::Full) { |
akling@apple.com | e92da67 | 2015-04-04 23:16:21 +0000 | [diff] [blame] | 1196 | if (!m_logicallyEmptyWeakBlocks.isEmpty()) |
| 1197 | m_indexOfNextLogicallyEmptyWeakBlockToSweep = 0; |
akling@apple.com | e92da67 | 2015-04-04 23:16:21 +0000 | [diff] [blame] | 1198 | } |
akling@apple.com | bfe711c | 2015-05-20 20:30:42 +0000 | [diff] [blame] | 1199 | |
| 1200 | m_sweeper->startSweeping(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1201 | } |
mhahnenberg@apple.com | eb39abc | 2012-05-31 03:04:00 +0000 | [diff] [blame] | 1202 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 1203 | void Heap::prepareForAllocation() |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1204 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 1205 | m_objectSpace.prepareForAllocation(); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | void Heap::updateAllocationLimits() |
| 1209 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1210 | static const bool verbose = false; |
| 1211 | |
| 1212 | if (verbose) { |
| 1213 | dataLog("\n"); |
| 1214 | dataLog("bytesAllocatedThisCycle = ", m_bytesAllocatedThisCycle, "\n"); |
| 1215 | } |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Calculate our current heap size threshold for the purpose of figuring out when we should |
| 1218 | // run another collection. This isn't the same as either size() or capacity(), though it should |
| 1219 | // be somewhere between the two. The key is to match the size calculations involved calls to |
| 1220 | // didAllocate(), while never dangerously underestimating capacity(). In extreme cases of |
fpizlo@apple.com | d1725cb | 2016-09-08 22:12:05 +0000 | [diff] [blame] | 1221 | // fragmentation, we may have size() much smaller than capacity(). |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1222 | size_t currentHeapSize = 0; |
| 1223 | |
| 1224 | // For marked space, we use the total number of bytes visited. This matches the logic for |
| 1225 | // MarkedAllocator's calls to didAllocate(), which effectively accounts for the total size of |
| 1226 | // objects allocated rather than blocks used. This will underestimate capacity(), and in case |
| 1227 | // of fragmentation, this may be substantial. Fortunately, marked space rarely fragments because |
| 1228 | // cells usually have a narrow range of sizes. So, the underestimation is probably OK. |
| 1229 | currentHeapSize += m_totalBytesVisited; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1230 | if (verbose) |
| 1231 | dataLog("totalBytesVisited = ", m_totalBytesVisited, ", currentHeapSize = ", currentHeapSize, "\n"); |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1232 | |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1233 | // It's up to the user to ensure that extraMemorySize() ends up corresponding to allocation-time |
| 1234 | // extra memory reporting. |
| 1235 | currentHeapSize += extraMemorySize(); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (verbose) |
| 1238 | dataLog("extraMemorySize() = ", extraMemorySize(), ", currentHeapSize = ", currentHeapSize, "\n"); |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1239 | |
mhahnenberg@apple.com | 3a2fb03 | 2012-10-05 17:35:49 +0000 | [diff] [blame] | 1240 | if (Options::gcMaxHeapSize() && currentHeapSize > Options::gcMaxHeapSize()) |
| 1241 | HeapStatistics::exitWithFailure(); |
| 1242 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1243 | if (m_collectionScope == CollectionScope::Full) { |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1244 | // To avoid pathological GC churn in very small and very large heaps, we set |
| 1245 | // the new allocation limit based on the current size of the heap, with a |
| 1246 | // fixed minimum. |
| 1247 | m_maxHeapSize = max(minHeapSize(m_heapType, m_ramSize), proportionalHeapSize(currentHeapSize, m_ramSize)); |
fpizlo@apple.com | 595eebd | 2016-08-24 19:00:37 +0000 | [diff] [blame] | 1248 | if (verbose) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1249 | dataLog("Full: maxHeapSize = ", m_maxHeapSize, "\n"); |
| 1250 | m_maxEdenSize = m_maxHeapSize - currentHeapSize; |
| 1251 | if (verbose) |
| 1252 | dataLog("Full: maxEdenSize = ", m_maxEdenSize, "\n"); |
| 1253 | m_sizeAfterLastFullCollect = currentHeapSize; |
| 1254 | if (verbose) |
| 1255 | dataLog("Full: sizeAfterLastFullCollect = ", currentHeapSize, "\n"); |
| 1256 | m_bytesAbandonedSinceLastFullCollect = 0; |
| 1257 | if (verbose) |
| 1258 | dataLog("Full: bytesAbandonedSinceLastFullCollect = ", 0, "\n"); |
| 1259 | } else { |
| 1260 | ASSERT(currentHeapSize >= m_sizeAfterLastCollect); |
| 1261 | // Theoretically, we shouldn't ever scan more memory than the heap size we planned to have. |
| 1262 | // But we are sloppy, so we have to defend against the overflow. |
| 1263 | m_maxEdenSize = currentHeapSize > m_maxHeapSize ? 0 : m_maxHeapSize - currentHeapSize; |
| 1264 | if (verbose) |
| 1265 | dataLog("Eden: maxEdenSize = ", m_maxEdenSize, "\n"); |
| 1266 | m_sizeAfterLastEdenCollect = currentHeapSize; |
| 1267 | if (verbose) |
| 1268 | dataLog("Eden: sizeAfterLastEdenCollect = ", currentHeapSize, "\n"); |
| 1269 | double edenToOldGenerationRatio = (double)m_maxEdenSize / (double)m_maxHeapSize; |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1270 | double minEdenToOldGenerationRatio = 1.0 / 3.0; |
| 1271 | if (edenToOldGenerationRatio < minEdenToOldGenerationRatio) |
| 1272 | m_shouldDoFullCollection = true; |
fpizlo@apple.com | ed5fc47 | 2015-10-17 20:27:43 +0000 | [diff] [blame] | 1273 | // This seems suspect at first, but what it does is ensure that the nursery size is fixed. |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1274 | m_maxHeapSize += currentHeapSize - m_sizeAfterLastCollect; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1275 | if (verbose) |
| 1276 | dataLog("Eden: maxHeapSize = ", m_maxHeapSize, "\n"); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1277 | m_maxEdenSize = m_maxHeapSize - currentHeapSize; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1278 | if (verbose) |
| 1279 | dataLog("Eden: maxEdenSize = ", m_maxEdenSize, "\n"); |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1280 | if (m_fullActivityCallback) { |
| 1281 | ASSERT(currentHeapSize >= m_sizeAfterLastFullCollect); |
| 1282 | m_fullActivityCallback->didAllocate(currentHeapSize - m_sizeAfterLastFullCollect); |
| 1283 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
ggaren@apple.com | bd41f7e | 2013-01-30 00:36:50 +0000 | [diff] [blame] | 1286 | m_sizeAfterLastCollect = currentHeapSize; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1287 | if (verbose) |
| 1288 | dataLog("sizeAfterLastCollect = ", m_sizeAfterLastCollect, "\n"); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1289 | m_bytesAllocatedThisCycle = 0; |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1290 | |
| 1291 | if (Options::logGC()) |
commit-queue@webkit.org | 847f825 | 2015-10-17 18:42:52 +0000 | [diff] [blame] | 1292 | dataLog(currentHeapSize / 1024, " kb, "); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | void Heap::didFinishCollection(double gcStartTime) |
| 1296 | { |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1297 | double gcEndTime = WTF::monotonicallyIncreasingTime(); |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1298 | CollectionScope scope = *m_collectionScope; |
| 1299 | if (scope == CollectionScope::Full) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1300 | m_lastFullGCLength = gcEndTime - gcStartTime; |
| 1301 | else |
| 1302 | m_lastEdenGCLength = gcEndTime - gcStartTime; |
ggaren@apple.com | def139e | 2012-09-26 02:27:54 +0000 | [diff] [blame] | 1303 | |
commit-queue@webkit.org | eab925d | 2016-06-23 20:05:23 +0000 | [diff] [blame] | 1304 | #if ENABLE(RESOURCE_USAGE) |
| 1305 | ASSERT(externalMemorySize() <= extraMemorySize()); |
| 1306 | #endif |
| 1307 | |
mhahnenberg@apple.com | 3a2fb03 | 2012-10-05 17:35:49 +0000 | [diff] [blame] | 1308 | if (Options::recordGCPauseTimes()) |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1309 | HeapStatistics::recordGCPauseTime(gcStartTime, gcEndTime); |
ggaren@apple.com | def139e | 2012-09-26 02:27:54 +0000 | [diff] [blame] | 1310 | |
| 1311 | if (Options::useZombieMode()) |
| 1312 | zombifyDeadObjects(); |
| 1313 | |
mark.lam@apple.com | ee3c410 | 2015-10-14 18:57:07 +0000 | [diff] [blame] | 1314 | if (Options::dumpObjectStatistics()) |
| 1315 | HeapStatistics::dumpObjectStatistics(this); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 1316 | |
commit-queue@webkit.org | 2ad5c1e | 2016-03-15 21:48:15 +0000 | [diff] [blame] | 1317 | if (HeapProfiler* heapProfiler = m_vm->heapProfiler()) { |
| 1318 | gatherExtraHeapSnapshotData(*heapProfiler); |
| 1319 | removeDeadHeapSnapshotNodes(*heapProfiler); |
| 1320 | } |
| 1321 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1322 | RELEASE_ASSERT(m_collectionScope); |
| 1323 | m_collectionScope = Nullopt; |
joepeck@webkit.org | d3b8b37 | 2015-10-16 02:15:56 +0000 | [diff] [blame] | 1324 | |
| 1325 | for (auto* observer : m_observers) |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1326 | observer->didGarbageCollect(scope); |
mhahnenberg@apple.com | c63eba6 | 2014-02-28 16:56:17 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | void Heap::resumeCompilerThreads() |
| 1330 | { |
fpizlo@apple.com | 1b84a42 | 2014-02-08 05:06:33 +0000 | [diff] [blame] | 1331 | #if ENABLE(DFG_JIT) |
mark.lam@apple.com | a906054 | 2014-04-24 00:43:15 +0000 | [diff] [blame] | 1332 | for (auto worklist : m_suspendedCompilerWorklists) |
| 1333 | worklist->resumeAllThreads(); |
| 1334 | m_suspendedCompilerWorklists.clear(); |
fpizlo@apple.com | 1b84a42 | 2014-02-08 05:06:33 +0000 | [diff] [blame] | 1335 | #endif |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1338 | void Heap::setFullActivityCallback(PassRefPtr<FullGCActivityCallback> activityCallback) |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 1339 | { |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1340 | m_fullActivityCallback = activityCallback; |
ggaren@apple.com | 2ccf7a9 | 2010-08-03 20:34:17 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1343 | void Heap::setEdenActivityCallback(PassRefPtr<EdenGCActivityCallback> activityCallback) |
ggaren@apple.com | aa64ff3 | 2010-12-03 01:16:11 +0000 | [diff] [blame] | 1344 | { |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1345 | m_edenActivityCallback = activityCallback; |
| 1346 | } |
| 1347 | |
| 1348 | GCActivityCallback* Heap::fullActivityCallback() |
| 1349 | { |
| 1350 | return m_fullActivityCallback.get(); |
| 1351 | } |
| 1352 | |
| 1353 | GCActivityCallback* Heap::edenActivityCallback() |
| 1354 | { |
| 1355 | return m_edenActivityCallback.get(); |
ggaren@apple.com | aa64ff3 | 2010-12-03 01:16:11 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
gyuyoung.kim@samsung.com | 0503196 | 2014-11-12 03:13:47 +0000 | [diff] [blame] | 1358 | void Heap::setIncrementalSweeper(std::unique_ptr<IncrementalSweeper> sweeper) |
dbates@webkit.org | 0ba2704 | 2013-10-18 23:57:24 +0000 | [diff] [blame] | 1359 | { |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 1360 | m_sweeper = WTFMove(sweeper); |
dbates@webkit.org | 0ba2704 | 2013-10-18 23:57:24 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
mhahnenberg@apple.com | eb39abc | 2012-05-31 03:04:00 +0000 | [diff] [blame] | 1363 | IncrementalSweeper* Heap::sweeper() |
| 1364 | { |
mhahnenberg@apple.com | 765a7de | 2013-04-15 23:17:51 +0000 | [diff] [blame] | 1365 | return m_sweeper.get(); |
mhahnenberg@apple.com | eb39abc | 2012-05-31 03:04:00 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
msaboff@apple.com | fb5f4cc | 2012-05-14 23:47:53 +0000 | [diff] [blame] | 1368 | void Heap::setGarbageCollectionTimerEnabled(bool enable) |
| 1369 | { |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1370 | if (m_fullActivityCallback) |
| 1371 | m_fullActivityCallback->setEnabled(enable); |
| 1372 | if (m_edenActivityCallback) |
| 1373 | m_edenActivityCallback->setEnabled(enable); |
msaboff@apple.com | fb5f4cc | 2012-05-14 23:47:53 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 1376 | void Heap::didAllocate(size_t bytes) |
| 1377 | { |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1378 | if (m_edenActivityCallback) |
| 1379 | m_edenActivityCallback->didAllocate(m_bytesAllocatedThisCycle + m_bytesAbandonedSinceLastFullCollect); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 1380 | m_bytesAllocatedThisCycle += bytes; |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
ggaren@apple.com | 06a8bb6 | 2012-09-12 04:08:18 +0000 | [diff] [blame] | 1383 | bool Heap::isValidAllocation(size_t) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 1384 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 1385 | if (!isValidThreadState(m_vm)) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 1386 | return false; |
| 1387 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1388 | if (isCurrentThreadBusy()) |
ggaren@apple.com | 99cc9d9 | 2011-05-26 21:46:09 +0000 | [diff] [blame] | 1389 | return false; |
| 1390 | |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
ggaren@apple.com | 75bf76c | 2011-10-05 22:44:36 +0000 | [diff] [blame] | 1394 | void Heap::addFinalizer(JSCell* cell, Finalizer finalizer) |
| 1395 | { |
commit-queue@webkit.org | d5d496b | 2015-09-22 12:21:31 +0000 | [diff] [blame] | 1396 | WeakSet::allocate(cell, &m_finalizerOwner, reinterpret_cast<void*>(finalizer)); // Balanced by FinalizerOwner::finalize(). |
ggaren@apple.com | 75bf76c | 2011-10-05 22:44:36 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
commit-queue@webkit.org | d5d496b | 2015-09-22 12:21:31 +0000 | [diff] [blame] | 1399 | void Heap::FinalizerOwner::finalize(Handle<Unknown> handle, void* context) |
ggaren@apple.com | 75bf76c | 2011-10-05 22:44:36 +0000 | [diff] [blame] | 1400 | { |
commit-queue@webkit.org | d5d496b | 2015-09-22 12:21:31 +0000 | [diff] [blame] | 1401 | HandleSlot slot = handle.slot(); |
ggaren@apple.com | 75bf76c | 2011-10-05 22:44:36 +0000 | [diff] [blame] | 1402 | Finalizer finalizer = reinterpret_cast<Finalizer>(context); |
commit-queue@webkit.org | d5d496b | 2015-09-22 12:21:31 +0000 | [diff] [blame] | 1403 | finalizer(slot->asCell()); |
| 1404 | WeakSet::deallocate(WeakImpl::asWeakImpl(slot)); |
ggaren@apple.com | 75bf76c | 2011-10-05 22:44:36 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 1407 | void Heap::addExecutable(ExecutableBase* executable) |
mhahnenberg@apple.com | cab4752 | 2012-03-30 00:36:37 +0000 | [diff] [blame] | 1408 | { |
ggaren@apple.com | 5dd393e | 2015-08-24 21:43:13 +0000 | [diff] [blame] | 1409 | m_executables.append(executable); |
mhahnenberg@apple.com | cab4752 | 2012-03-30 00:36:37 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
cdumez@apple.com | ccfd3b4 | 2015-06-09 22:15:36 +0000 | [diff] [blame] | 1412 | void Heap::collectAllGarbageIfNotDoneRecently() |
| 1413 | { |
| 1414 | if (!m_fullActivityCallback) { |
| 1415 | collectAllGarbage(); |
| 1416 | return; |
| 1417 | } |
| 1418 | |
| 1419 | if (m_fullActivityCallback->didSyncGCRecently()) { |
| 1420 | // A synchronous GC was already requested recently so we merely accelerate next collection. |
| 1421 | reportAbandonedObjectGraph(); |
| 1422 | return; |
| 1423 | } |
| 1424 | |
| 1425 | m_fullActivityCallback->setDidSyncGCRecently(); |
| 1426 | collectAllGarbage(); |
| 1427 | } |
| 1428 | |
ggaren@apple.com | def139e | 2012-09-26 02:27:54 +0000 | [diff] [blame] | 1429 | class Zombify : public MarkedBlock::VoidFunctor { |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1430 | public: |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 1431 | inline void visit(HeapCell* cell) const |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1432 | { |
zandobersek@gmail.com | c5b35d3 | 2016-10-21 06:00:48 +0000 | [diff] [blame] | 1433 | void** current = reinterpret_cast_ptr<void**>(cell); |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1434 | |
| 1435 | // We want to maintain zapped-ness because that's how we know if we've called |
| 1436 | // the destructor. |
| 1437 | if (cell->isZapped()) |
| 1438 | current++; |
| 1439 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1440 | void* limit = static_cast<void*>(reinterpret_cast<char*>(cell) + cell->cellSize()); |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1441 | for (; current < limit; current++) |
mark.lam@apple.com | d7e9e16 | 2014-05-07 22:00:10 +0000 | [diff] [blame] | 1442 | *current = zombifiedBits; |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1443 | } |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 1444 | IterationStatus operator()(HeapCell* cell, HeapCell::Kind) const |
mark.lam@apple.com | f535b59 | 2015-04-22 20:05:06 +0000 | [diff] [blame] | 1445 | { |
| 1446 | visit(cell); |
| 1447 | return IterationStatus::Continue; |
| 1448 | } |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1449 | }; |
| 1450 | |
| 1451 | void Heap::zombifyDeadObjects() |
| 1452 | { |
ggaren@apple.com | def139e | 2012-09-26 02:27:54 +0000 | [diff] [blame] | 1453 | // Sweep now because destructors will crash once we're zombified. |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 1454 | m_objectSpace.sweep(); |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 1455 | HeapIterationScope iterationScope(*this); |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 1456 | m_objectSpace.forEachDeadCell(iterationScope, Zombify()); |
mhahnenberg@apple.com | 66b96f2 | 2012-09-07 05:53:25 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
mhahnenberg@apple.com | 4968e1a | 2013-12-18 22:50:40 +0000 | [diff] [blame] | 1459 | void Heap::flushWriteBarrierBuffer(JSCell* cell) |
| 1460 | { |
mhahnenberg@apple.com | 4968e1a | 2013-12-18 22:50:40 +0000 | [diff] [blame] | 1461 | m_writeBarrierBuffer.flush(*this); |
| 1462 | m_writeBarrierBuffer.add(cell); |
mhahnenberg@apple.com | 4968e1a | 2013-12-18 22:50:40 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1465 | bool Heap::shouldDoFullCollection(Optional<CollectionScope> scope) const |
mhahnenberg@apple.com | 60e8292 | 2014-02-20 22:53:21 +0000 | [diff] [blame] | 1466 | { |
mark.lam@apple.com | ee3c410 | 2015-10-14 18:57:07 +0000 | [diff] [blame] | 1467 | if (!Options::useGenerationalGC()) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1468 | return true; |
| 1469 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1470 | if (!scope) |
mhahnenberg@apple.com | 0d81ace | 2014-03-20 01:56:19 +0000 | [diff] [blame] | 1471 | return m_shouldDoFullCollection; |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame] | 1472 | return *scope == CollectionScope::Full; |
mhahnenberg@apple.com | 60e8292 | 2014-02-20 22:53:21 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
akling@apple.com | e92da67 | 2015-04-04 23:16:21 +0000 | [diff] [blame] | 1475 | void Heap::addLogicallyEmptyWeakBlock(WeakBlock* block) |
| 1476 | { |
| 1477 | m_logicallyEmptyWeakBlocks.append(block); |
| 1478 | } |
| 1479 | |
| 1480 | void Heap::sweepAllLogicallyEmptyWeakBlocks() |
| 1481 | { |
| 1482 | if (m_logicallyEmptyWeakBlocks.isEmpty()) |
| 1483 | return; |
| 1484 | |
| 1485 | m_indexOfNextLogicallyEmptyWeakBlockToSweep = 0; |
| 1486 | while (sweepNextLogicallyEmptyWeakBlock()) { } |
| 1487 | } |
| 1488 | |
| 1489 | bool Heap::sweepNextLogicallyEmptyWeakBlock() |
| 1490 | { |
| 1491 | if (m_indexOfNextLogicallyEmptyWeakBlockToSweep == WTF::notFound) |
| 1492 | return false; |
| 1493 | |
| 1494 | WeakBlock* block = m_logicallyEmptyWeakBlocks[m_indexOfNextLogicallyEmptyWeakBlockToSweep]; |
| 1495 | |
| 1496 | block->sweep(); |
| 1497 | if (block->isEmpty()) { |
| 1498 | std::swap(m_logicallyEmptyWeakBlocks[m_indexOfNextLogicallyEmptyWeakBlockToSweep], m_logicallyEmptyWeakBlocks.last()); |
| 1499 | m_logicallyEmptyWeakBlocks.removeLast(); |
akling@apple.com | 43fb352 | 2015-11-01 02:29:47 +0000 | [diff] [blame] | 1500 | WeakBlock::destroy(*this, block); |
akling@apple.com | e92da67 | 2015-04-04 23:16:21 +0000 | [diff] [blame] | 1501 | } else |
| 1502 | m_indexOfNextLogicallyEmptyWeakBlockToSweep++; |
| 1503 | |
| 1504 | if (m_indexOfNextLogicallyEmptyWeakBlockToSweep >= m_logicallyEmptyWeakBlocks.size()) { |
| 1505 | m_indexOfNextLogicallyEmptyWeakBlockToSweep = WTF::notFound; |
| 1506 | return false; |
| 1507 | } |
| 1508 | |
| 1509 | return true; |
| 1510 | } |
| 1511 | |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 1512 | size_t Heap::threadVisitCount() |
| 1513 | { |
| 1514 | unsigned long result = 0; |
fpizlo@apple.com | 5f86606 | 2015-09-26 18:07:09 +0000 | [diff] [blame] | 1515 | for (auto& parallelVisitor : m_parallelSlotVisitors) |
| 1516 | result += parallelVisitor->visitCount(); |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 1517 | return result; |
| 1518 | } |
| 1519 | |
| 1520 | size_t Heap::threadBytesVisited() |
| 1521 | { |
| 1522 | size_t result = 0; |
fpizlo@apple.com | 5f86606 | 2015-09-26 18:07:09 +0000 | [diff] [blame] | 1523 | for (auto& parallelVisitor : m_parallelSlotVisitors) |
| 1524 | result += parallelVisitor->bytesVisited(); |
fpizlo@apple.com | 42e88d0 | 2015-09-23 01:35:49 +0000 | [diff] [blame] | 1525 | return result; |
| 1526 | } |
| 1527 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 1528 | void Heap::forEachCodeBlockImpl(const ScopedLambda<bool(CodeBlock*)>& func) |
| 1529 | { |
| 1530 | // We don't know the full set of CodeBlocks until compilation has terminated. |
| 1531 | completeAllJITPlans(); |
| 1532 | |
| 1533 | return m_codeBlocks->iterate(func); |
| 1534 | } |
| 1535 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 1536 | void Heap::writeBarrierSlowPath(const JSCell* from) |
| 1537 | { |
| 1538 | if (UNLIKELY(barrierShouldBeFenced())) { |
| 1539 | // In this case, the barrierThreshold is the tautological threshold, so from could still be |
| 1540 | // not black. But we can't know for sure until we fire off a fence. |
| 1541 | WTF::storeLoadFence(); |
fpizlo@apple.com | 261fc22 | 2016-10-12 23:56:34 +0000 | [diff] [blame] | 1542 | if (from->cellState() != CellState::AnthraciteOrBlack) |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 1543 | return; |
| 1544 | } |
| 1545 | |
| 1546 | addToRememberedSet(from); |
| 1547 | } |
| 1548 | |
fpizlo@apple.com | 4dbb351 | 2016-10-22 19:34:46 +0000 | [diff] [blame] | 1549 | bool Heap::shouldCollect() |
| 1550 | { |
| 1551 | if (isDeferred()) |
| 1552 | return false; |
| 1553 | if (!m_isSafeToCollect) |
| 1554 | return false; |
| 1555 | if (collectionScope() || mutatorState() == MutatorState::HelpingGC) |
| 1556 | return false; |
| 1557 | if (Options::gcMaxHeapSize()) |
| 1558 | return m_bytesAllocatedThisCycle > Options::gcMaxHeapSize(); |
| 1559 | return m_bytesAllocatedThisCycle > m_maxEdenSize; |
| 1560 | } |
| 1561 | |
| 1562 | bool Heap::isCurrentThreadBusy() |
| 1563 | { |
| 1564 | return mayBeGCThread() || mutatorState() != MutatorState::Running; |
| 1565 | } |
| 1566 | |
| 1567 | void Heap::reportExtraMemoryVisited(CellState oldState, size_t size) |
| 1568 | { |
| 1569 | // We don't want to double-count the extra memory that was reported in previous collections. |
| 1570 | if (collectionScope() == CollectionScope::Eden && oldState == CellState::OldGrey) |
| 1571 | return; |
| 1572 | |
| 1573 | size_t* counter = &m_extraMemorySize; |
| 1574 | |
| 1575 | for (;;) { |
| 1576 | size_t oldSize = *counter; |
fpizlo@apple.com | afa9d88 | 2016-11-01 03:10:00 +0000 | [diff] [blame] | 1577 | if (WTF::atomicCompareExchangeWeakRelaxed(counter, oldSize, oldSize + size)) |
fpizlo@apple.com | 4dbb351 | 2016-10-22 19:34:46 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | #if ENABLE(RESOURCE_USAGE) |
| 1583 | void Heap::reportExternalMemoryVisited(CellState oldState, size_t size) |
| 1584 | { |
| 1585 | // We don't want to double-count the external memory that was reported in previous collections. |
| 1586 | if (collectionScope() == CollectionScope::Eden && oldState == CellState::OldGrey) |
| 1587 | return; |
| 1588 | |
| 1589 | size_t* counter = &m_externalMemorySize; |
| 1590 | |
| 1591 | for (;;) { |
| 1592 | size_t oldSize = *counter; |
fpizlo@apple.com | afa9d88 | 2016-11-01 03:10:00 +0000 | [diff] [blame] | 1593 | if (WTF::atomicCompareExchangeWeakRelaxed(counter, oldSize, oldSize + size)) |
fpizlo@apple.com | 4dbb351 | 2016-10-22 19:34:46 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | } |
| 1596 | } |
| 1597 | #endif |
| 1598 | |
| 1599 | bool Heap::collectIfNecessaryOrDefer(GCDeferralContext* deferralContext) |
| 1600 | { |
| 1601 | if (!shouldCollect()) |
| 1602 | return false; |
| 1603 | |
| 1604 | if (deferralContext) |
| 1605 | deferralContext->m_shouldGC = true; |
| 1606 | else |
| 1607 | collect(); |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | void Heap::collectAccordingToDeferGCProbability() |
| 1612 | { |
| 1613 | if (isDeferred() || !m_isSafeToCollect || collectionScope() || mutatorState() == MutatorState::HelpingGC) |
| 1614 | return; |
| 1615 | |
| 1616 | if (randomNumber() < Options::deferGCProbability()) { |
| 1617 | collect(); |
| 1618 | return; |
| 1619 | } |
| 1620 | |
| 1621 | // If our coin flip told us not to GC, we still might GC, |
| 1622 | // but we GC according to our memory pressure markers. |
| 1623 | collectIfNecessaryOrDefer(); |
| 1624 | } |
| 1625 | |
| 1626 | void Heap::decrementDeferralDepthAndGCIfNeeded() |
| 1627 | { |
| 1628 | decrementDeferralDepth(); |
| 1629 | if (UNLIKELY(Options::deferGCShouldCollectWithProbability())) |
| 1630 | collectAccordingToDeferGCProbability(); |
| 1631 | else |
| 1632 | collectIfNecessaryOrDefer(); |
| 1633 | } |
| 1634 | |
| 1635 | void Heap::registerWeakGCMap(void* weakGCMap, std::function<void()> pruningCallback) |
| 1636 | { |
| 1637 | m_weakGCMaps.add(weakGCMap, WTFMove(pruningCallback)); |
| 1638 | } |
| 1639 | |
| 1640 | void Heap::unregisterWeakGCMap(void* weakGCMap) |
| 1641 | { |
| 1642 | m_weakGCMaps.remove(weakGCMap); |
| 1643 | } |
| 1644 | |
| 1645 | void Heap::didAllocateBlock(size_t capacity) |
| 1646 | { |
| 1647 | #if ENABLE(RESOURCE_USAGE) |
| 1648 | m_blockBytesAllocated += capacity; |
| 1649 | #else |
| 1650 | UNUSED_PARAM(capacity); |
| 1651 | #endif |
| 1652 | } |
| 1653 | |
| 1654 | void Heap::didFreeBlock(size_t capacity) |
| 1655 | { |
| 1656 | #if ENABLE(RESOURCE_USAGE) |
| 1657 | m_blockBytesAllocated -= capacity; |
| 1658 | #else |
| 1659 | UNUSED_PARAM(capacity); |
| 1660 | #endif |
| 1661 | } |
| 1662 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 1663 | } // namespace JSC |