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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
|
/*
* Copyright (C) 2012-2023 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 "ConservativeRoots.h"
#include "GCSegmentedArrayInlines.h"
#include "HeapAnalyzer.h"
#include "HeapCellInlines.h"
#include "HeapProfiler.h"
#include "IntegrityInlines.h"
#include "JSArray.h"
#include "JSCellInlines.h"
#include "JSString.h"
#include "MarkingConstraintSolver.h"
#include "SlotVisitorInlines.h"
#include "StopIfNecessaryTimer.h"
#include "VM.h"
#include <wtf/ListDump.h>
#include <wtf/Lock.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
WTF_MAKE_TZONE_ALLOCATED_IMPL(SlotVisitor);
#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(JSC::Heap& heap, CString codeName)
: Base(heap, codeName, heap.m_opaqueRoots)
, m_markingVersion(MarkedSpace::initialVersion)
#if ASSERT_ENABLED
, m_isCheckingForDefaultMarkViolation(false)
#endif
{
}
SlotVisitor::~SlotVisitor()
{
clearMarkStacks();
}
void SlotVisitor::didStartMarking()
{
auto scope = heap()->collectionScope();
if (scope) {
switch (*scope) {
case CollectionScope::Eden:
reset();
break;
case CollectionScope::Full:
m_extraMemorySize = 0;
break;
}
}
if (HeapProfiler* heapProfiler = vm().heapProfiler())
m_heapAnalyzer = heapProfiler->activeHeapAnalyzer();
m_markingVersion = heap()->objectSpace().markingVersion();
}
void SlotVisitor::reset()
{
AbstractSlotVisitor::reset();
m_bytesVisited = 0;
m_heapAnalyzer = nullptr;
RELEASE_ASSERT(!m_currentCell);
}
void SlotVisitor::clearMarkStacks()
{
forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
stack.clear();
return IterationStatus::Continue;
});
}
void SlotVisitor::append(const ConservativeRoots& conservativeRoots)
{
HeapCell** roots = conservativeRoots.roots();
size_t size = conservativeRoots.size();
for (size_t i = 0; i < size; ++i)
appendJSCellOrAuxiliary(roots[i]);
}
void SlotVisitor::appendJSCellOrAuxiliary(HeapCell* heapCell)
{
if (!heapCell)
return;
ASSERT(!m_isCheckingForDefaultMarkViolation);
auto validateCell = [&] (JSCell* jsCell) {
StructureID structureID = jsCell->structureID();
auto die = [&] (const char* text) {
WTF::dataFile().atomically(
[&] (PrintStream& out) {
out.print(text);
out.print("GC type: ", heap()->collectionScope(), "\n");
out.print("Object at: ", RawPointer(jsCell), "\n");
#if USE(JSVALUE64)
out.print("Structure ID: ", structureID.bits(), " (", RawPointer(structureID.decode()), ")\n");
#else
out.print("Structure: ", RawPointer(structureID.decode()), "\n");
#endif
out.print("Object contents:");
for (unsigned i = 0; i < 2; ++i)
out.print(" ", format("0x%016llx", std::bit_cast<uint64_t*>(jsCell)[i]));
out.print("\n");
CellContainer container = jsCell->cellContainer();
out.print("Is marked: ", container.isMarked(jsCell), "\n");
out.print("Is newly allocated: ", container.isNewlyAllocated(jsCell), "\n");
if (container.isMarkedBlock()) {
MarkedBlock& block = container.markedBlock();
out.print("Block: ", RawPointer(&block), "\n");
block.handle().dumpState(out);
out.print("\n");
out.print("Is marked raw: ", block.isMarkedRaw(jsCell), "\n");
out.print("Marking version: ", block.markingVersion(), "\n");
out.print("Heap marking version: ", heap()->objectSpace().markingVersion(), "\n");
out.print("Is newly allocated raw: ", block.isNewlyAllocated(jsCell), "\n");
out.print("Newly allocated version: ", block.newlyAllocatedVersion(), "\n");
out.print("Heap newly allocated version: ", heap()->objectSpace().newlyAllocatedVersion(), "\n");
}
UNREACHABLE_FOR_PLATFORM();
});
};
// It's not OK for the structure to be null at any GC scan point. We must not GC while
// an object is not fully initialized.
if (!structureID)
die("GC scan found corrupt object: structureID is zero!\n");
// It's not OK for the structure to be nuked at any GC scan point.
if (structureID.isNuked())
die("GC scan found object in bad state: structureID is nuked!\n");
// This detects the worst of the badness.
Integrity::auditStructureID(structureID);
};
// In debug mode, we validate before marking since this makes it clearer what the problem
// was. It's also slower, so we don't do it normally.
if (ASSERT_ENABLED && isJSCellKind(heapCell->cellKind()))
validateCell(static_cast<JSCell*>(heapCell));
if (Heap::testAndSetMarked(m_markingVersion, heapCell))
return;
switch (heapCell->cellKind()) {
case HeapCell::JSCell:
case HeapCell::JSCellWithIndexingHeader: {
// We have ample budget to perform validation here.
JSCell* jsCell = static_cast<JSCell*>(heapCell);
validateCell(jsCell);
Integrity::auditCell(vm(), jsCell);
jsCell->setCellState(CellState::PossiblyGrey);
appendToMarkStack(jsCell);
return;
}
case HeapCell::Auxiliary: {
noteLiveAuxiliaryCell(heapCell);
return;
} }
}
void SlotVisitor::appendSlow(JSCell* cell, Dependency dependency)
{
if (UNLIKELY(m_heapAnalyzer))
m_heapAnalyzer->analyzeEdge(m_currentCell, cell, rootMarkReason());
appendHiddenSlowImpl(cell, dependency);
}
void SlotVisitor::appendHiddenSlow(JSCell* cell, Dependency dependency)
{
appendHiddenSlowImpl(cell, dependency);
}
ALWAYS_INLINE void SlotVisitor::appendHiddenSlowImpl(JSCell* cell, Dependency dependency)
{
ASSERT(!m_isCheckingForDefaultMarkViolation);
#if ENABLE(GC_VALIDATION)
validate(cell);
#endif
if (cell->isPreciseAllocation())
setMarkedAndAppendToMarkStack(cell->preciseAllocation(), cell, dependency);
else
setMarkedAndAppendToMarkStack(cell->markedBlock(), cell, dependency);
}
template<typename ContainerType>
ALWAYS_INLINE void SlotVisitor::setMarkedAndAppendToMarkStack(ContainerType& container, JSCell* cell, Dependency dependency)
{
if (container.testAndSetMarked(cell, dependency))
return;
ASSERT(cell->structure());
// 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::PossiblyGrey);
appendToMarkStack(container, cell);
}
void SlotVisitor::appendToMarkStack(JSCell* cell)
{
if (cell->isPreciseAllocation())
appendToMarkStack(cell->preciseAllocation(), cell);
else
appendToMarkStack(cell->markedBlock(), cell);
}
template<typename ContainerType>
ALWAYS_INLINE void SlotVisitor::appendToMarkStack(ContainerType& container, JSCell* cell)
{
ASSERT(m_heap.isMarked(cell));
#if CPU(X86_64)
if (UNLIKELY(Options::dumpZappedCellCrashData())) {
if (UNLIKELY(cell->isZapped()))
reportZappedCellAndCrash(m_heap, cell);
}
#endif
ASSERT(!cell->isZapped());
container.noteMarked();
m_visitCount++;
m_bytesVisited += container.cellSize();
m_collectorStack.append(cell);
}
void SlotVisitor::markAuxiliary(const void* base)
{
HeapCell* cell = std::bit_cast<HeapCell*>(base);
ASSERT(cell->heap() == heap());
if (Heap::testAndSetMarked(m_markingVersion, cell))
return;
noteLiveAuxiliaryCell(cell);
}
void SlotVisitor::noteLiveAuxiliaryCell(HeapCell* cell)
{
// We get here once per GC under these circumstances:
//
// Eden collection: if the cell was allocated since the last collection and is live somehow.
//
// Full collection: if the cell is live somehow.
CellContainer container = cell->cellContainer();
container.assertValidCell(vm(), cell);
container.noteMarked();
m_visitCount++;
size_t cellSize = container.cellSize();
m_bytesVisited += cellSize;
m_nonCellVisitCount += cellSize;
}
class SetCurrentCellScope {
public:
SetCurrentCellScope(SlotVisitor& visitor, const JSCell* cell)
: m_visitor(visitor)
{
ASSERT(!m_visitor.m_currentCell);
m_visitor.m_currentCell = const_cast<JSCell*>(cell);
}
~SetCurrentCellScope()
{
ASSERT(m_visitor.m_currentCell);
m_visitor.m_currentCell = nullptr;
}
private:
SlotVisitor& m_visitor;
};
ALWAYS_INLINE void SlotVisitor::visitChildren(const JSCell* cell)
{
ASSERT(m_heap.isMarked(cell));
SetCurrentCellScope currentCellScope(*this, cell);
if (false) {
dataLog("Visiting ", RawPointer(cell));
if (!m_isFirstVisit)
dataLog(" (subsequent)");
dataLog("\n");
}
// Funny story: it's possible for the object to be black already, if we barrier the object at
// about the same time that it's marked. That's fine. It's a gnarly and super-rare race. It's
// not clear to me that it would be correct or profitable to bail here if the object is already
// black.
cell->setCellState(CellState::PossiblyBlack);
WTF::storeLoadFence();
switch (cell->type()) {
case StringType:
JSString::visitChildren(const_cast<JSCell*>(cell), *this);
break;
case FinalObjectType:
JSFinalObject::visitChildren(const_cast<JSCell*>(cell), *this);
break;
case ArrayType:
JSArray::visitChildren(const_cast<JSCell*>(cell), *this);
break;
default:
// FIXME: This could be so much better.
// https://bugs.webkit.org/show_bug.cgi?id=162462
#if CPU(X86_64)
if (UNLIKELY(Options::dumpZappedCellCrashData())) {
Structure* structure = cell->structure();
if (LIKELY(structure)) {
const MethodTable* methodTable = &structure->classInfoForCells()->methodTable;
methodTable->visitChildren(const_cast<JSCell*>(cell), *this);
break;
}
reportZappedCellAndCrash(m_heap, const_cast<JSCell*>(cell));
}
#endif
cell->methodTable()->visitChildren(const_cast<JSCell*>(cell), *this);
break;
}
if (UNLIKELY(m_heapAnalyzer)) {
if (m_isFirstVisit)
m_heapAnalyzer->analyzeNode(const_cast<JSCell*>(cell));
}
}
void SlotVisitor::visitAsConstraint(const JSCell* cell)
{
m_isFirstVisit = false;
visitChildren(cell);
}
inline void SlotVisitor::propagateExternalMemoryVisitedIfNecessary()
{
if (m_isFirstVisit) {
if (m_extraMemorySize.hasOverflowed())
heap()->reportExtraMemoryVisited(std::numeric_limits<size_t>::max());
else if (m_extraMemorySize)
heap()->reportExtraMemoryVisited(m_extraMemorySize);
m_extraMemorySize = 0;
}
}
void SlotVisitor::donateKnownParallel(MarkStackArray& from, MarkStackArray& to)
{
// 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 (from.size() < 2)
return;
// If there's already some shared work queued up, be conservative and assume
// that donating more is not profitable.
if (to.size())
return;
// If we're contending on the lock, be conservative and assume that another
// thread is already donating.
if (!m_heap.m_markingMutex.tryLock())
return;
Locker locker { AdoptLock, m_heap.m_markingMutex };
// Otherwise, assume that a thread will go idle soon, and donate.
from.donateSomeCellsTo(to);
m_heap.m_markingConditionVariable.notifyAll();
}
void SlotVisitor::donateKnownParallel()
{
forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
donateKnownParallel(stack, correspondingGlobalStack(stack));
return IterationStatus::Continue;
});
}
void SlotVisitor::updateMutatorIsStopped(const AbstractLocker&)
{
m_mutatorIsStopped = (m_heap.worldIsStopped() & m_canOptimizeForStoppedMutator);
}
void SlotVisitor::updateMutatorIsStopped()
{
if (mutatorIsStoppedIsUpToDate())
return;
updateMutatorIsStopped(Locker { m_rightToRun });
}
bool SlotVisitor::hasAcknowledgedThatTheMutatorIsResumed() const
{
return !m_mutatorIsStopped;
}
bool SlotVisitor::mutatorIsStoppedIsUpToDate() const
{
return m_mutatorIsStopped == (m_heap.worldIsStopped() & m_canOptimizeForStoppedMutator);
}
void SlotVisitor::optimizeForStoppedMutator()
{
m_canOptimizeForStoppedMutator = true;
}
NEVER_INLINE void SlotVisitor::drain(MonotonicTime timeout)
{
if (!m_isInParallelMode) {
dataLog("FATAL: attempting to drain when not in parallel mode.\n");
RELEASE_ASSERT_NOT_REACHED();
}
Locker locker { m_rightToRun };
while (!hasElapsed(timeout)) {
updateMutatorIsStopped(locker);
IterationStatus status = forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
if (stack.isEmpty())
return IterationStatus::Continue;
stack.refill();
m_isFirstVisit = (&stack == &m_collectorStack);
for (unsigned countdown = Options::minimumNumberOfScansBetweenRebalance(); stack.canRemoveLast() && countdown--;)
visitChildren(stack.removeLast());
return IterationStatus::Done;
});
propagateExternalMemoryVisitedIfNecessary();
if (status == IterationStatus::Continue)
break;
m_rightToRun.safepoint();
donateKnownParallel();
}
}
size_t SlotVisitor::performIncrementOfDraining(size_t bytesRequested)
{
RELEASE_ASSERT(m_isInParallelMode);
size_t cellsRequested = bytesRequested / MarkedBlock::atomSize;
{
Locker locker { m_heap.m_markingMutex };
forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
cellsRequested -= correspondingGlobalStack(stack).transferTo(stack, cellsRequested);
return cellsRequested ? IterationStatus::Continue : IterationStatus::Done;
});
}
size_t cellBytesVisited = 0;
m_nonCellVisitCount = 0;
auto bytesVisited = [&] () -> size_t {
return cellBytesVisited + m_nonCellVisitCount;
};
auto isDone = [&] () -> bool {
return bytesVisited() >= bytesRequested;
};
{
Locker locker { m_rightToRun };
while (!isDone()) {
updateMutatorIsStopped(locker);
IterationStatus status = forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
if (stack.isEmpty() || isDone())
return IterationStatus::Continue;
stack.refill();
m_isFirstVisit = (&stack == &m_collectorStack);
unsigned countdown = Options::minimumNumberOfScansBetweenRebalance();
while (countdown && stack.canRemoveLast() && !isDone()) {
const JSCell* cell = stack.removeLast();
cellBytesVisited += cell->cellSize();
visitChildren(cell);
countdown--;
}
return IterationStatus::Done;
});
propagateExternalMemoryVisitedIfNecessary();
if (status == IterationStatus::Continue)
break;
m_rightToRun.safepoint();
donateKnownParallel();
}
}
donateAll();
return bytesVisited();
}
bool SlotVisitor::didReachTermination()
{
Locker locker { m_heap.m_markingMutex };
return didReachTermination(locker);
}
bool SlotVisitor::didReachTermination(const AbstractLocker& locker)
{
return !m_heap.m_numberOfActiveParallelMarkers
&& !hasWork(locker);
}
bool SlotVisitor::hasWork(const AbstractLocker&)
{
return !isEmpty()
|| !m_heap.m_sharedCollectorMarkStack->isEmpty()
|| !m_heap.m_sharedMutatorMarkStack->isEmpty();
}
NEVER_INLINE SlotVisitor::SharedDrainResult SlotVisitor::drainFromShared(SharedDrainMode sharedDrainMode, MonotonicTime timeout)
{
ASSERT(m_isInParallelMode);
ASSERT(Options::numberOfGCMarkers());
bool isActive = false;
while (true) {
RefPtr<SharedTask<void(SlotVisitor&)>> bonusTask;
{
Locker locker { m_heap.m_markingMutex };
if (isActive)
m_heap.m_numberOfActiveParallelMarkers--;
m_heap.m_numberOfWaitingParallelMarkers++;
if (sharedDrainMode == MainDrain) {
while (true) {
if (hasElapsed(timeout))
return SharedDrainResult::TimedOut;
if (didReachTermination(locker)) {
m_heap.m_markingConditionVariable.notifyAll();
return SharedDrainResult::Done;
}
if (hasWork(locker))
break;
m_heap.m_markingConditionVariable.waitUntil(m_heap.m_markingMutex, timeout);
}
} else {
ASSERT(sharedDrainMode == HelperDrain);
if (hasElapsed(timeout))
return SharedDrainResult::TimedOut;
if (didReachTermination(locker)) {
m_heap.m_markingConditionVariable.notifyAll();
// If we're in concurrent mode, then we know that the mutator will eventually do
// the right thing because:
// - It's possible that the collector has the conn. In that case, the collector will
// wake up from the notification above. This will happen if the app released heap
// access. Native apps can spend a lot of time with heap access released.
// - It's possible that the mutator will allocate soon. Then it will check if we
// reached termination. This is the most likely outcome in programs that allocate
// a lot.
// - WebCore never releases access. But WebCore has a runloop. The runloop will check
// if we reached termination.
// So, this tells the runloop that it's got things to do.
m_heap.m_stopIfNecessaryTimer->scheduleSoon();
}
auto isReady = [&] () -> bool {
return hasWork(locker)
|| m_heap.m_bonusVisitorTask
|| m_heap.m_parallelMarkersShouldExit;
};
m_heap.m_markingConditionVariable.waitUntil(m_heap.m_markingMutex, timeout, isReady);
if (!hasWork(locker)
&& m_heap.m_bonusVisitorTask)
bonusTask = m_heap.m_bonusVisitorTask;
if (m_heap.m_parallelMarkersShouldExit)
return SharedDrainResult::Done;
}
if (!bonusTask && isEmpty()) {
forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
stack.stealSomeCellsFrom(
correspondingGlobalStack(stack),
m_heap.m_numberOfWaitingParallelMarkers);
return IterationStatus::Continue;
});
}
m_heap.m_numberOfActiveParallelMarkers++;
m_heap.m_numberOfWaitingParallelMarkers--;
}
if (bonusTask) {
bonusTask->run(*this);
// The main thread could still be running, and may run for a while. Unless we clear the task
// ourselves, we will keep looping around trying to run the task.
{
Locker locker { m_heap.m_markingMutex };
if (m_heap.m_bonusVisitorTask == bonusTask)
m_heap.m_bonusVisitorTask = nullptr;
bonusTask = nullptr;
m_heap.m_markingConditionVariable.notifyAll();
}
} else {
RELEASE_ASSERT(!isEmpty());
drain(timeout);
}
isActive = true;
}
}
SlotVisitor::SharedDrainResult SlotVisitor::drainInParallel(MonotonicTime timeout)
{
donateAndDrain(timeout);
return drainFromShared(MainDrain, timeout);
}
SlotVisitor::SharedDrainResult SlotVisitor::drainInParallelPassively(MonotonicTime timeout)
{
ASSERT(m_isInParallelMode);
ASSERT(Options::numberOfGCMarkers());
if (Options::numberOfGCMarkers() == 1
|| (m_heap.m_worldState.load() & Heap::mutatorWaitingBit)
|| !m_heap.hasHeapAccess()
|| m_heap.worldIsStopped()) {
// This is an optimization over drainInParallel() when we have a concurrent mutator but
// otherwise it is not profitable.
return drainInParallel(timeout);
}
donateAll(Locker { m_heap.m_markingMutex });
return waitForTermination(timeout);
}
SlotVisitor::SharedDrainResult SlotVisitor::waitForTermination(MonotonicTime timeout)
{
Locker locker { m_heap.m_markingMutex };
for (;;) {
if (hasElapsed(timeout))
return SharedDrainResult::TimedOut;
if (didReachTermination(locker)) {
m_heap.m_markingConditionVariable.notifyAll();
return SharedDrainResult::Done;
}
m_heap.m_markingConditionVariable.waitUntil(m_heap.m_markingMutex, timeout);
}
}
void SlotVisitor::donateAll()
{
if (isEmpty())
return;
donateAll(Locker { m_heap.m_markingMutex });
}
void SlotVisitor::donateAll(const AbstractLocker&)
{
forEachMarkStack(
[&] (MarkStackArray& stack) -> IterationStatus {
stack.transferTo(correspondingGlobalStack(stack));
return IterationStatus::Continue;
});
m_heap.m_markingConditionVariable.notifyAll();
}
void SlotVisitor::donate()
{
if (!m_isInParallelMode) {
dataLog("FATAL: Attempting to donate when not in parallel mode.\n");
RELEASE_ASSERT_NOT_REACHED();
}
if (Options::numberOfGCMarkers() == 1)
return;
donateKnownParallel();
}
void SlotVisitor::donateAndDrain(MonotonicTime timeout)
{
donate();
drain(timeout);
}
void SlotVisitor::didRace(const VisitRaceKey& race)
{
dataLogLnIf(Options::verboseVisitRace(), toCString("GC visit race: ", race));
Locker locker { heap()->m_raceMarkStackLock };
JSCell* cell = race.cell();
cell->setCellState(CellState::PossiblyGrey);
heap()->m_raceMarkStack->append(cell);
}
void SlotVisitor::dump(PrintStream& out) const
{
out.print("Collector: [", pointerListDump(collectorMarkStack()), "], Mutator: [", pointerListDump(mutatorMarkStack()), "]");
}
MarkStackArray& SlotVisitor::correspondingGlobalStack(MarkStackArray& stack)
{
if (&stack == &m_collectorStack)
return *m_heap.m_sharedCollectorMarkStack;
RELEASE_ASSERT(&stack == &m_mutatorStack);
return *m_heap.m_sharedMutatorMarkStack;
}
NO_RETURN_DUE_TO_CRASH void SlotVisitor::addParallelConstraintTask(RefPtr<SharedTask<void(AbstractSlotVisitor&)>>)
{
RELEASE_ASSERT_NOT_REACHED();
}
void SlotVisitor::addParallelConstraintTask(RefPtr<SharedTask<void(SlotVisitor&)>> task)
{
RELEASE_ASSERT(m_currentSolver);
RELEASE_ASSERT(m_currentConstraint);
RELEASE_ASSERT(task);
m_currentSolver->addParallelTask(task, *m_currentConstraint);
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|