2 * Copyright (C) 2012, 2015-2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "SlotVisitor.h"
29 #include "ConservativeRoots.h"
30 #include "GCSegmentedArrayInlines.h"
31 #include "HeapCellInlines.h"
32 #include "HeapProfiler.h"
33 #include "HeapSnapshotBuilder.h"
35 #include "JSDestructibleObject.h"
38 #include "JSCInlines.h"
39 #include "SlotVisitorInlines.h"
40 #include "SuperSampler.h"
46 #if ENABLE(GC_VALIDATION)
47 static void validate(JSCell* cell)
51 if (!cell->structure()) {
52 dataLogF("cell at %p has a null structure\n" , cell);
56 // Both the cell's structure, and the cell's structure's structure should be the Structure Structure.
57 // I hate this sentence.
58 if (cell->structure()->structure()->JSCell::classInfo() != cell->structure()->JSCell::classInfo()) {
59 const char* parentClassName = 0;
60 const char* ourClassName = 0;
61 if (cell->structure()->structure() && cell->structure()->structure()->JSCell::classInfo())
62 parentClassName = cell->structure()->structure()->JSCell::classInfo()->className;
63 if (cell->structure()->JSCell::classInfo())
64 ourClassName = cell->structure()->JSCell::classInfo()->className;
65 dataLogF("parent structure (%p <%s>) of cell at %p doesn't match cell's structure (%p <%s>)\n",
66 cell->structure()->structure(), parentClassName, cell, cell->structure(), ourClassName);
70 // Make sure we can walk the ClassInfo chain
71 const ClassInfo* info = cell->classInfo();
72 do { } while ((info = info->parentClass));
76 SlotVisitor::SlotVisitor(Heap& heap)
81 , m_isInParallelMode(false)
82 , m_markingVersion(MarkedSpace::initialVersion)
85 , m_isCheckingForDefaultMarkViolation(false)
91 SlotVisitor::~SlotVisitor()
96 void SlotVisitor::didStartMarking()
98 if (heap()->operationInProgress() == FullCollection)
99 ASSERT(m_opaqueRoots.isEmpty()); // Should have merged by now.
103 if (HeapProfiler* heapProfiler = vm().heapProfiler())
104 m_heapSnapshotBuilder = heapProfiler->activeSnapshotBuilder();
106 m_markingVersion = heap()->objectSpace().markingVersion();
109 void SlotVisitor::reset()
114 m_heapSnapshotBuilder = nullptr;
115 ASSERT(!m_currentCell);
118 void SlotVisitor::clearMarkStack()
123 void SlotVisitor::append(ConservativeRoots& conservativeRoots)
125 HeapCell** roots = conservativeRoots.roots();
126 size_t size = conservativeRoots.size();
127 for (size_t i = 0; i < size; ++i)
128 appendJSCellOrAuxiliary(roots[i]);
131 void SlotVisitor::appendJSCellOrAuxiliary(HeapCell* heapCell)
136 ASSERT(!m_isCheckingForDefaultMarkViolation);
138 if (Heap::testAndSetMarked(m_markingVersion, heapCell))
141 switch (heapCell->cellKind()) {
142 case HeapCell::JSCell: {
143 JSCell* jsCell = static_cast<JSCell*>(heapCell);
145 if (!jsCell->structure()) {
146 ASSERT_NOT_REACHED();
150 jsCell->setCellState(CellState::NewGrey);
152 appendToMarkStack(jsCell);
156 case HeapCell::Auxiliary: {
157 noteLiveAuxiliaryCell(heapCell);
162 void SlotVisitor::append(JSValue value)
164 if (!value || !value.isCell())
167 if (UNLIKELY(m_heapSnapshotBuilder))
168 m_heapSnapshotBuilder->appendEdge(m_currentCell, value.asCell());
170 setMarkedAndAppendToMarkStack(value.asCell());
173 void SlotVisitor::appendHidden(JSValue value)
175 if (!value || !value.isCell())
178 setMarkedAndAppendToMarkStack(value.asCell());
181 void SlotVisitor::setMarkedAndAppendToMarkStack(JSCell* cell)
183 SuperSamplerScope superSamplerScope(false);
185 ASSERT(!m_isCheckingForDefaultMarkViolation);
189 #if ENABLE(GC_VALIDATION)
193 //dataLog(" Marking ", RawPointer(cell), "\n");
195 if (cell->isLargeAllocation())
196 setMarkedAndAppendToMarkStack(cell->largeAllocation(), cell);
198 setMarkedAndAppendToMarkStack(cell->markedBlock(), cell);
201 template<typename ContainerType>
202 ALWAYS_INLINE void SlotVisitor::setMarkedAndAppendToMarkStack(ContainerType& container, JSCell* cell)
204 container.aboutToMark(m_markingVersion);
206 if (container.testAndSetMarked(cell))
209 ASSERT(cell->structure());
211 // Indicate that the object is grey and that:
212 // In case of concurrent GC: it's the first time it is grey in this GC cycle.
213 // In case of eden collection: it's a new object that became grey rather than an old remembered object.
214 cell->setCellState(CellState::NewGrey);
216 appendToMarkStack(container, cell);
219 void SlotVisitor::appendToMarkStack(JSCell* cell)
221 if (cell->isLargeAllocation())
222 appendToMarkStack(cell->largeAllocation(), cell);
224 appendToMarkStack(cell->markedBlock(), cell);
227 template<typename ContainerType>
228 ALWAYS_INLINE void SlotVisitor::appendToMarkStack(ContainerType& container, JSCell* cell)
230 ASSERT(Heap::isMarkedConcurrently(cell));
231 ASSERT(!cell->isZapped());
233 container.noteMarked();
235 // FIXME: These "just work" because the GC resets these fields before doing anything else. But
236 // that won't be the case when we do concurrent GC.
238 m_bytesVisited += container.cellSize();
240 m_stack.append(cell);
242 if (UNLIKELY(m_heapSnapshotBuilder))
243 m_heapSnapshotBuilder->appendNode(cell);
246 void SlotVisitor::markAuxiliary(const void* base)
248 HeapCell* cell = bitwise_cast<HeapCell*>(base);
250 ASSERT(cell->heap() == heap());
252 if (Heap::testAndSetMarked(m_markingVersion, cell))
255 noteLiveAuxiliaryCell(cell);
258 void SlotVisitor::noteLiveAuxiliaryCell(HeapCell* cell)
260 // We get here once per GC under these circumstances:
262 // Eden collection: if the cell was allocated since the last collection and is live somehow.
264 // Full collection: if the cell is live somehow.
266 CellContainer container = cell->cellContainer();
268 container.noteMarked();
271 m_bytesVisited += container.cellSize();
274 class SetCurrentCellScope {
276 SetCurrentCellScope(SlotVisitor& visitor, const JSCell* cell)
279 ASSERT(!m_visitor.m_currentCell);
280 m_visitor.m_currentCell = const_cast<JSCell*>(cell);
283 ~SetCurrentCellScope()
285 ASSERT(m_visitor.m_currentCell);
286 m_visitor.m_currentCell = nullptr;
290 SlotVisitor& m_visitor;
294 ALWAYS_INLINE void SlotVisitor::visitChildren(const JSCell* cell)
296 ASSERT(Heap::isMarkedConcurrently(cell));
298 SetCurrentCellScope currentCellScope(*this, cell);
300 cell->setCellState(blacken(cell->cellState()));
302 // FIXME: Make this work on ARM also.
303 // https://bugs.webkit.org/show_bug.cgi?id=162461
305 WTF::storeLoadFence();
307 switch (cell->type()) {
309 JSString::visitChildren(const_cast<JSCell*>(cell), *this);
312 case FinalObjectType:
313 JSFinalObject::visitChildren(const_cast<JSCell*>(cell), *this);
317 JSArray::visitChildren(const_cast<JSCell*>(cell), *this);
321 // FIXME: This could be so much better.
322 // https://bugs.webkit.org/show_bug.cgi?id=162462
323 cell->methodTable()->visitChildren(const_cast<JSCell*>(cell), *this);
328 void SlotVisitor::donateKnownParallel()
330 // NOTE: Because we re-try often, we can afford to be conservative, and
331 // assume that donating is not profitable.
333 // Avoid locking when a thread reaches a dead end in the object graph.
334 if (m_stack.size() < 2)
337 // If there's already some shared work queued up, be conservative and assume
338 // that donating more is not profitable.
339 if (m_heap.m_sharedMarkStack.size())
342 // If we're contending on the lock, be conservative and assume that another
343 // thread is already donating.
344 std::unique_lock<Lock> lock(m_heap.m_markingMutex, std::try_to_lock);
345 if (!lock.owns_lock())
348 // Otherwise, assume that a thread will go idle soon, and donate.
349 m_stack.donateSomeCellsTo(m_heap.m_sharedMarkStack);
351 m_heap.m_markingConditionVariable.notifyAll();
354 void SlotVisitor::drain()
356 ASSERT(m_isInParallelMode);
358 while (!m_stack.isEmpty()) {
360 for (unsigned countdown = Options::minimumNumberOfScansBetweenRebalance(); m_stack.canRemoveLast() && countdown--;)
361 visitChildren(m_stack.removeLast());
362 donateKnownParallel();
365 mergeOpaqueRootsIfNecessary();
368 void SlotVisitor::drainFromShared(SharedDrainMode sharedDrainMode)
370 ASSERT(m_isInParallelMode);
372 ASSERT(Options::numberOfGCMarkers());
375 std::lock_guard<Lock> lock(m_heap.m_markingMutex);
376 m_heap.m_numberOfActiveParallelMarkers++;
380 std::unique_lock<Lock> lock(m_heap.m_markingMutex);
381 m_heap.m_numberOfActiveParallelMarkers--;
382 m_heap.m_numberOfWaitingParallelMarkers++;
384 // How we wait differs depending on drain mode.
385 if (sharedDrainMode == MasterDrain) {
386 // Wait until either termination is reached, or until there is some work
389 // Did we reach termination?
390 if (!m_heap.m_numberOfActiveParallelMarkers
391 && m_heap.m_sharedMarkStack.isEmpty()) {
392 // Let any sleeping slaves know it's time for them to return;
393 m_heap.m_markingConditionVariable.notifyAll();
397 // Is there work to be done?
398 if (!m_heap.m_sharedMarkStack.isEmpty())
402 m_heap.m_markingConditionVariable.wait(lock);
405 ASSERT(sharedDrainMode == SlaveDrain);
407 // Did we detect termination? If so, let the master know.
408 if (!m_heap.m_numberOfActiveParallelMarkers
409 && m_heap.m_sharedMarkStack.isEmpty())
410 m_heap.m_markingConditionVariable.notifyAll();
412 m_heap.m_markingConditionVariable.wait(
415 return !m_heap.m_sharedMarkStack.isEmpty()
416 || m_heap.m_parallelMarkersShouldExit;
419 // Is the current phase done? If so, return from this function.
420 if (m_heap.m_parallelMarkersShouldExit)
424 m_stack.stealSomeCellsFrom(
425 m_heap.m_sharedMarkStack, m_heap.m_numberOfWaitingParallelMarkers);
426 m_heap.m_numberOfActiveParallelMarkers++;
427 m_heap.m_numberOfWaitingParallelMarkers--;
434 void SlotVisitor::addOpaqueRoot(void* root)
436 if (Options::numberOfGCMarkers() == 1) {
437 // Put directly into the shared HashSet.
438 m_heap.m_opaqueRoots.add(root);
441 // Put into the local set, but merge with the shared one every once in
442 // a while to make sure that the local sets don't grow too large.
443 mergeOpaqueRootsIfProfitable();
444 m_opaqueRoots.add(root);
447 bool SlotVisitor::containsOpaqueRoot(void* root) const
449 ASSERT(!m_isInParallelMode);
450 ASSERT(m_opaqueRoots.isEmpty());
451 return m_heap.m_opaqueRoots.contains(root);
454 TriState SlotVisitor::containsOpaqueRootTriState(void* root) const
456 if (m_opaqueRoots.contains(root))
458 std::lock_guard<Lock> lock(m_heap.m_opaqueRootsMutex);
459 if (m_heap.m_opaqueRoots.contains(root))
461 return MixedTriState;
464 int SlotVisitor::opaqueRootCount()
466 ASSERT(!m_isInParallelMode);
467 ASSERT(m_opaqueRoots.isEmpty());
468 return m_heap.m_opaqueRoots.size();
471 void SlotVisitor::mergeOpaqueRootsIfNecessary()
473 if (m_opaqueRoots.isEmpty())
478 void SlotVisitor::mergeOpaqueRootsIfProfitable()
480 if (static_cast<unsigned>(m_opaqueRoots.size()) < Options::opaqueRootMergeThreshold())
485 void SlotVisitor::donate()
487 ASSERT(m_isInParallelMode);
488 if (Options::numberOfGCMarkers() == 1)
491 donateKnownParallel();
494 void SlotVisitor::donateAndDrain()
500 void SlotVisitor::mergeOpaqueRoots()
502 ASSERT(!m_opaqueRoots.isEmpty()); // Should only be called when opaque roots are non-empty.
504 std::lock_guard<Lock> lock(m_heap.m_opaqueRootsMutex);
505 for (auto* root : m_opaqueRoots)
506 m_heap.m_opaqueRoots.add(root);
508 m_opaqueRoots.clear();
511 void SlotVisitor::harvestWeakReferences()
513 for (WeakReferenceHarvester* current = m_heap.m_weakReferenceHarvesters.head(); current; current = current->next())
514 current->visitWeakReferences(*this);
517 void SlotVisitor::finalizeUnconditionalFinalizers()
519 while (m_heap.m_unconditionalFinalizers.hasNext())
520 m_heap.m_unconditionalFinalizers.removeNext()->finalizeUnconditionally();
523 void SlotVisitor::dump(PrintStream&) const
525 for (const JSCell* cell : markStack())
526 dataLog(*cell, "\n");