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
|
/*
* Copyright (C) 2018-2019 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. ``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
* 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 "LocalAllocator.h"
#include "AllocatingScope.h"
#include "FreeListInlines.h"
#include "GCDeferralContext.h"
#include "LocalAllocatorInlines.h"
#include "Options.h"
#include "SuperSampler.h"
namespace JSC {
LocalAllocator::LocalAllocator(BlockDirectory* directory)
: m_directory(directory)
, m_freeList(directory->m_cellSize)
{
Locker locker { directory->m_localAllocatorsLock };
directory->m_localAllocators.append(this);
}
void LocalAllocator::reset()
{
m_freeList.clear();
m_currentBlock = nullptr;
m_lastActiveBlock = nullptr;
m_allocationCursor = 0;
}
LocalAllocator::~LocalAllocator()
{
if (isOnList()) {
Locker locker { m_directory->m_localAllocatorsLock };
remove();
}
bool ok = true;
if (!m_freeList.allocationWillFail()) {
dataLog("FATAL: ", RawPointer(this), "->~LocalAllocator has non-empty free-list.\n");
ok = false;
}
if (m_currentBlock) {
dataLog("FATAL: ", RawPointer(this), "->~LocalAllocator has non-null current block.\n");
ok = false;
}
if (m_lastActiveBlock) {
dataLog("FATAL: ", RawPointer(this), "->~LocalAllocator has non-null last active block.\n");
ok = false;
}
RELEASE_ASSERT(ok);
}
void LocalAllocator::stopAllocating()
{
ASSERT(!m_lastActiveBlock);
if (!m_currentBlock) {
ASSERT(m_freeList.allocationWillFail());
return;
}
m_currentBlock->stopAllocating(m_freeList);
m_lastActiveBlock = m_currentBlock;
m_currentBlock = nullptr;
m_freeList.clear();
}
void LocalAllocator::resumeAllocating()
{
if (!m_lastActiveBlock)
return;
m_lastActiveBlock->resumeAllocating(m_freeList);
m_currentBlock = m_lastActiveBlock;
m_lastActiveBlock = nullptr;
}
void LocalAllocator::prepareForAllocation()
{
reset();
}
void LocalAllocator::stopAllocatingForGood()
{
stopAllocating();
reset();
}
void* LocalAllocator::allocateSlowCase(JSC::Heap& heap, size_t cellSize, GCDeferralContext* deferralContext, AllocationFailureMode failureMode)
{
SuperSamplerScope superSamplerScope(false);
ASSERT(heap.vm().currentThreadIsHoldingAPILock());
doTestCollectionsIfNeeded(heap, deferralContext);
ASSERT(!m_directory->markedSpace().isIterating());
heap.didAllocate(m_freeList.originalSize());
didConsumeFreeList();
AllocatingScope helpingHeap(heap);
heap.collectIfNecessaryOrDefer(deferralContext);
// Goofy corner case: the GC called a callback and now this directory has a currentBlock. This only
// happens when running WebKit tests, which inject a callback into the GC's finalization.
if (UNLIKELY(m_currentBlock))
return allocate(heap, cellSize, deferralContext, failureMode);
void* result = tryAllocateWithoutCollecting(cellSize);
if (LIKELY(result != nullptr))
return result;
// FIXME GlobalGC: Need to synchronize here to when allocating from the BlockDirectory in the server.
Subspace* subspace = m_directory->m_subspace;
if (subspace->isIsoSubspace()) {
if (void* result = static_cast<IsoSubspace*>(subspace)->tryAllocateLowerTierPrecise(cellSize))
return result;
}
ASSERT(!subspace->isPreciseOnly());
ASSERT_WITH_MESSAGE(cellSize == m_directory->cellSize(), "non-preciseOnly allocations should match allocator's the size class");
MarkedBlock::Handle* block = m_directory->tryAllocateBlock(heap);
if (!block) {
if (failureMode == AllocationFailureMode::Assert)
RELEASE_ASSERT_NOT_REACHED();
else
return nullptr;
}
m_directory->addBlock(block);
result = allocateIn(block, cellSize);
ASSERT(result);
return result;
}
void LocalAllocator::didConsumeFreeList()
{
if (m_currentBlock)
m_currentBlock->didConsumeFreeList();
m_freeList.clear();
m_currentBlock = nullptr;
}
void* LocalAllocator::tryAllocateWithoutCollecting(size_t cellSize)
{
// FIXME: GlobalGC
// FIXME: If we wanted this to be used for real multi-threaded allocations then we would have to
// come up with some concurrency protocol here. That protocol would need to be able to handle:
//
// - The basic case of multiple LocalAllocators trying to do an allocationCursor search on the
// same bitvector. That probably needs the bitvector lock at least.
//
// - The harder case of some LocalAllocator triggering a steal from a different BlockDirectory
// via a search in the AlignedMemoryAllocator's list. Who knows what locks that needs.
//
// One way to make this work is to have a single per-Heap lock that protects all mutator lock
// allocation slow paths. That would probably be scalable enough for years. It would certainly be
// for using TLC allocation from JIT threads.
// https://bugs.webkit.org/show_bug.cgi?id=181635
SuperSamplerScope superSamplerScope(false);
ASSERT(!m_currentBlock);
ASSERT(m_freeList.allocationWillFail());
for (;;) {
MarkedBlock::Handle* block = m_directory->findBlockForAllocation(*this);
if (!block)
break;
if (void* result = tryAllocateIn(block, cellSize))
return result;
}
if (Options::stealEmptyBlocksFromOtherAllocators()) {
if (MarkedBlock::Handle* block = m_directory->m_subspace->findEmptyBlockToSteal()) {
RELEASE_ASSERT(block->alignedMemoryAllocator() == m_directory->m_subspace->alignedMemoryAllocator());
block->sweep(nullptr);
// It's good that this clears canAllocateButNotEmpty as well as all other bits,
// because there is a remote chance that a block may have both canAllocateButNotEmpty
// and empty set at the same time.
block->removeFromDirectory();
m_directory->addBlock(block);
return allocateIn(block, cellSize);
}
}
return nullptr;
}
void* LocalAllocator::allocateIn(MarkedBlock::Handle* block, size_t cellSize)
{
void* result = tryAllocateIn(block, cellSize);
RELEASE_ASSERT(result);
return result;
}
void* LocalAllocator::tryAllocateIn(MarkedBlock::Handle* block, size_t cellSize)
{
ASSERT(block);
ASSERT(!block->isFreeListed());
m_directory->assertIsMutatorOrMutatorIsStopped();
ASSERT(m_directory->isInUse(block));
block->sweep(&m_freeList);
// It's possible to stumble on a completely full block. Marking tries to retire these, but
// that algorithm is racy and may forget to do it sometimes.
if (m_freeList.allocationWillFail()) {
ASSERT(block->isFreeListed());
block->unsweepWithNoNewlyAllocated();
ASSERT(!block->isFreeListed());
ASSERT(!m_directory->isEmpty(block));
ASSERT(!m_directory->isCanAllocateButNotEmpty(block));
return nullptr;
}
m_currentBlock = block;
void* result = m_freeList.allocateWithCellSize(
[]() -> HeapCell* {
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}, cellSize);
// FIXME: We should make this work with thread safety analysis.
m_directory->m_bits.setIsEden(m_currentBlock->index(), true);
m_directory->markedSpace().didAllocateInBlock(m_currentBlock);
return result;
}
void LocalAllocator::doTestCollectionsIfNeeded(JSC::Heap& heap, GCDeferralContext* deferralContext)
{
if (LIKELY(!Options::slowPathAllocsBetweenGCs()))
return;
static unsigned allocationCount = 0;
if (!allocationCount) {
if (!heap.isDeferred()) {
if (deferralContext)
deferralContext->m_shouldGC = true;
else
heap.collectNow(Sync, CollectionScope::Full);
}
}
if (++allocationCount >= Options::slowPathAllocsBetweenGCs())
allocationCount = 0;
}
} // namespace JSC
|