1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*
* Copyright (C) 2012, 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "SlotVisitor.h"
#include "SlotVisitorInlines.h"
#include "ConservativeRoots.h"
#include "CopiedBlockInlines.h"
#include "CopiedSpace.h"
#include "CopiedSpaceInlines.h"
#include "JSArray.h"
#include "JSDestructibleObject.h"
#include "VM.h"
#include "JSObject.h"
#include "JSString.h"
#include "JSCInlines.h"
#include <wtf/Lock.h>
namespace JSC {
#if ENABLE(GC_VALIDATION)
static void validate(JSCell* cell)
{
RELEASE_ASSERT(cell);
if (!cell->structure()) {
dataLogF("cell at %p has a null structure\n" , cell);
CRASH();
}
// Both the cell's structure, and the cell's structure's structure should be the Structure Structure.
// I hate this sentence.
if (cell->structure()->structure()->JSCell::classInfo() != cell->structure()->JSCell::classInfo()) {
const char* parentClassName = 0;
const char* ourClassName = 0;
if (cell->structure()->structure() && cell->structure()->structure()->JSCell::classInfo())
parentClassName = cell->structure()->structure()->JSCell::classInfo()->className;
if (cell->structure()->JSCell::classInfo())
ourClassName = cell->structure()->JSCell::classInfo()->className;
dataLogF("parent structure (%p <%s>) of cell at %p doesn't match cell's structure (%p <%s>)\n",
cell->structure()->structure(), parentClassName, cell, cell->structure(), ourClassName);
CRASH();
}
// Make sure we can walk the ClassInfo chain
const ClassInfo* info = cell->classInfo();
do { } while ((info = info->parentClass));
}
#endif
SlotVisitor::SlotVisitor(Heap& heap)
: m_stack()
, m_bytesVisited(0)
, m_bytesCopied(0)
, m_visitCount(0)
, m_isInParallelMode(false)
, m_heap(heap)
#if !ASSERT_DISABLED
, m_isCheckingForDefaultMarkViolation(false)
, m_isDraining(false)
#endif
{
}
SlotVisitor::~SlotVisitor()
{
clearMarkStack();
}
void SlotVisitor::didStartMarking()
{
if (heap()->operationInProgress() == FullCollection)
ASSERT(m_opaqueRoots.isEmpty()); // Should have merged by now.
}
void SlotVisitor::reset()
{
m_bytesVisited = 0;
m_bytesCopied = 0;
m_visitCount = 0;
ASSERT(m_stack.isEmpty());
}
void SlotVisitor::clearMarkStack()
{
m_stack.clear();
}
void SlotVisitor::append(ConservativeRoots& conservativeRoots)
{
JSCell** roots = conservativeRoots.roots();
size_t size = conservativeRoots.size();
for (size_t i = 0; i < size; ++i)
append(roots[i]);
}
void SlotVisitor::append(JSValue value)
{
if (!value || !value.isCell())
return;
setMarkedAndAppendToMarkStack(value.asCell());
}
void SlotVisitor::setMarkedAndAppendToMarkStack(JSCell* cell)
{
ASSERT(!m_isCheckingForDefaultMarkViolation);
if (!cell)
return;
#if ENABLE(GC_VALIDATION)
validate(cell);
#endif
if (Heap::testAndSetMarked(cell) || !cell->structure()) {
ASSERT(cell->structure());
return;
}
// Indicate that the object is grey and that:
// In case of concurrent GC: it's the first time it is grey in this GC cycle.
// In case of eden collection: it's a new object that became grey rather than an old remembered object.
cell->setCellState(CellState::NewGrey);
appendToMarkStack(cell);
}
void SlotVisitor::appendToMarkStack(JSCell* cell)
{
ASSERT(Heap::isMarked(cell));
ASSERT(!cell->isZapped());
m_visitCount++;
m_bytesVisited += MarkedBlock::blockFor(cell)->cellSize();
m_stack.append(cell);
}
ALWAYS_INLINE void SlotVisitor::visitChildren(const JSCell* cell)
{
ASSERT(Heap::isMarked(cell));
m_currentObjectCellStateBeforeVisiting = cell->cellState();
cell->setCellState(CellState::OldBlack);
if (isJSString(cell)) {
JSString::visitChildren(const_cast<JSCell*>(cell), *this);
return;
}
if (isJSFinalObject(cell)) {
JSFinalObject::visitChildren(const_cast<JSCell*>(cell), *this);
return;
}
if (isJSArray(cell)) {
JSArray::visitChildren(const_cast<JSCell*>(cell), *this);
return;
}
cell->methodTable()->visitChildren(const_cast<JSCell*>(cell), *this);
}
void SlotVisitor::donateKnownParallel()
{
// NOTE: Because we re-try often, we can afford to be conservative, and
// assume that donating is not profitable.
// Avoid locking when a thread reaches a dead end in the object graph.
if (m_stack.size() < 2)
return;
// If there's already some shared work queued up, be conservative and assume
// that donating more is not profitable.
if (m_heap.m_sharedMarkStack.size())
return;
// If we're contending on the lock, be conservative and assume that another
// thread is already donating.
std::unique_lock<Lock> lock(m_heap.m_markingMutex, std::try_to_lock);
if (!lock.owns_lock())
return;
// Otherwise, assume that a thread will go idle soon, and donate.
m_stack.donateSomeCellsTo(m_heap.m_sharedMarkStack);
m_heap.m_markingConditionVariable.notifyAll();
}
void SlotVisitor::drain()
{
ASSERT(m_isInParallelMode);
while (!m_stack.isEmpty()) {
m_stack.refill();
for (unsigned countdown = Options::minimumNumberOfScansBetweenRebalance(); m_stack.canRemoveLast() && countdown--;)
visitChildren(m_stack.removeLast());
donateKnownParallel();
}
mergeOpaqueRootsIfNecessary();
}
void SlotVisitor::drainFromShared(SharedDrainMode sharedDrainMode)
{
ASSERT(m_isInParallelMode);
ASSERT(Options::numberOfGCMarkers());
{
std::lock_guard<Lock> lock(m_heap.m_markingMutex);
m_heap.m_numberOfActiveParallelMarkers++;
}
while (true) {
{
std::unique_lock<Lock> lock(m_heap.m_markingMutex);
m_heap.m_numberOfActiveParallelMarkers--;
m_heap.m_numberOfWaitingParallelMarkers++;
// How we wait differs depending on drain mode.
if (sharedDrainMode == MasterDrain) {
// Wait until either termination is reached, or until there is some work
// for us to do.
while (true) {
// Did we reach termination?
if (!m_heap.m_numberOfActiveParallelMarkers
&& m_heap.m_sharedMarkStack.isEmpty()) {
// Let any sleeping slaves know it's time for them to return;
m_heap.m_markingConditionVariable.notifyAll();
return;
}
// Is there work to be done?
if (!m_heap.m_sharedMarkStack.isEmpty())
break;
// Otherwise wait.
m_heap.m_markingConditionVariable.wait(lock);
}
} else {
ASSERT(sharedDrainMode == SlaveDrain);
// Did we detect termination? If so, let the master know.
if (!m_heap.m_numberOfActiveParallelMarkers
&& m_heap.m_sharedMarkStack.isEmpty())
m_heap.m_markingConditionVariable.notifyAll();
m_heap.m_markingConditionVariable.wait(
lock,
[this] {
return !m_heap.m_sharedMarkStack.isEmpty()
|| m_heap.m_parallelMarkersShouldExit;
});
// Is the current phase done? If so, return from this function.
if (m_heap.m_parallelMarkersShouldExit)
return;
}
m_stack.stealSomeCellsFrom(
m_heap.m_sharedMarkStack, m_heap.m_numberOfWaitingParallelMarkers);
m_heap.m_numberOfActiveParallelMarkers++;
m_heap.m_numberOfWaitingParallelMarkers--;
}
drain();
}
}
void SlotVisitor::addOpaqueRoot(void* root)
{
if (Options::numberOfGCMarkers() == 1) {
// Put directly into the shared HashSet.
m_heap.m_opaqueRoots.add(root);
return;
}
// Put into the local set, but merge with the shared one every once in
// a while to make sure that the local sets don't grow too large.
mergeOpaqueRootsIfProfitable();
m_opaqueRoots.add(root);
}
bool SlotVisitor::containsOpaqueRoot(void* root) const
{
ASSERT(!m_isInParallelMode);
ASSERT(m_opaqueRoots.isEmpty());
return m_heap.m_opaqueRoots.contains(root);
}
TriState SlotVisitor::containsOpaqueRootTriState(void* root) const
{
if (m_opaqueRoots.contains(root))
return TrueTriState;
std::lock_guard<Lock> lock(m_heap.m_opaqueRootsMutex);
if (m_heap.m_opaqueRoots.contains(root))
return TrueTriState;
return MixedTriState;
}
int SlotVisitor::opaqueRootCount()
{
ASSERT(!m_isInParallelMode);
ASSERT(m_opaqueRoots.isEmpty());
return m_heap.m_opaqueRoots.size();
}
void SlotVisitor::mergeOpaqueRootsIfNecessary()
{
if (m_opaqueRoots.isEmpty())
return;
mergeOpaqueRoots();
}
void SlotVisitor::mergeOpaqueRootsIfProfitable()
{
if (static_cast<unsigned>(m_opaqueRoots.size()) < Options::opaqueRootMergeThreshold())
return;
mergeOpaqueRoots();
}
void SlotVisitor::donate()
{
ASSERT(m_isInParallelMode);
if (Options::numberOfGCMarkers() == 1)
return;
donateKnownParallel();
}
void SlotVisitor::donateAndDrain()
{
donate();
drain();
}
void SlotVisitor::copyLater(JSCell* owner, CopyToken token, void* ptr, size_t bytes)
{
ASSERT(bytes);
CopiedBlock* block = CopiedSpace::blockFor(ptr);
if (block->isOversize()) {
ASSERT(bytes <= block->size());
// FIXME: We should be able to shrink the allocation if bytes went below the block size.
// For now, we just make sure that our accounting of how much memory we are actually using
// is correct.
// https://bugs.webkit.org/show_bug.cgi?id=144749
bytes = block->size();
m_heap.m_storageSpace.pin(block);
}
ASSERT(heap()->m_storageSpace.contains(block));
LockHolder locker(&block->workListLock());
// We always report live bytes, except if during an eden collection we see an old object pointing to an
// old backing store and the old object is being marked because of the remembered set. Note that if we
// ask the object itself, it will always tell us that it's an old black object - because even during an
// eden collection we have already indicated that the object is old. That's why we use the
// SlotVisitor's cache of the object's old state.
if (heap()->operationInProgress() == FullCollection
|| !block->isOld()
|| m_currentObjectCellStateBeforeVisiting != CellState::OldGrey) {
m_bytesCopied += bytes;
block->reportLiveBytes(locker, owner, token, bytes);
}
}
void SlotVisitor::mergeOpaqueRoots()
{
ASSERT(!m_opaqueRoots.isEmpty()); // Should only be called when opaque roots are non-empty.
{
std::lock_guard<Lock> lock(m_heap.m_opaqueRootsMutex);
for (auto* root : m_opaqueRoots)
m_heap.m_opaqueRoots.add(root);
}
m_opaqueRoots.clear();
}
void SlotVisitor::harvestWeakReferences()
{
for (WeakReferenceHarvester* current = m_heap.m_weakReferenceHarvesters.head(); current; current = current->next())
current->visitWeakReferences(*this);
}
void SlotVisitor::finalizeUnconditionalFinalizers()
{
while (m_heap.m_unconditionalFinalizers.hasNext())
m_heap.m_unconditionalFinalizers.removeNext()->finalizeUnconditionally();
}
void SlotVisitor::dump(PrintStream&) const
{
for (const JSCell* cell : markStack())
dataLog(*cell, "\n");
}
} // namespace JSC
|