| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "gc/Allocator.h" |
| 8 |
|
| 9 |
#include "mozilla/DebugOnly.h" |
| 10 |
#include "mozilla/OperatorNewExtensions.h" |
| 11 |
#include "mozilla/TimeStamp.h" |
| 12 |
|
| 13 |
#include "gc/GCInternals.h" |
| 14 |
#include "gc/GCLock.h" |
| 15 |
#include "gc/GCProbes.h" |
| 16 |
#include "gc/Nursery.h" |
| 17 |
#include "threading/CpuCount.h" |
| 18 |
#include "util/Poison.h" |
| 19 |
#include "vm/BigIntType.h" |
| 20 |
#include "vm/FrameIter.h" |
| 21 |
#include "vm/Runtime.h" |
| 22 |
#include "vm/StringType.h" |
| 23 |
|
| 24 |
#include "gc/ArenaList-inl.h" |
| 25 |
#include "gc/Heap-inl.h" |
| 26 |
#include "gc/PrivateIterators-inl.h" |
| 27 |
#include "vm/JSContext-inl.h" |
| 28 |
#include "vm/JSScript-inl.h" |
| 29 |
|
| 30 |
using mozilla::TimeStamp; |
| 31 |
|
| 32 |
using namespace js; |
| 33 |
using namespace js::gc; |
| 34 |
|
| 35 |
// Return a Heap value that can be compared numerically with an |
| 36 |
// allocation's requested heap to determine whether to allocate in the nursery |
| 37 |
// or the tenured heap. |
| 38 |
// |
| 39 |
// If nursery allocation is allowed this returns Heap::Tenured, meaning only |
| 40 |
// Heap::Tenured allocations will be tenured. If nursery allocation is not |
| 41 |
// allowed this returns Heap::Default, meaning all allocations are tenured. |
| 42 |
static Heap MinHeapToTenure(bool allowNurseryAlloc) { |
| 43 |
static_assert(Heap::Tenured > Heap::Default); |
| 44 |
return allowNurseryAlloc ? Heap::Tenured : Heap::Default; |
| 45 |
} |
| 46 |
|
| 47 |
void Zone::setNurseryAllocFlags(bool allocObjects, bool allocStrings, |
| 48 |
bool allocBigInts) { |
| 49 |
allocNurseryObjects_ = allocObjects; |
| 50 |
allocNurseryStrings_ = allocStrings; |
| 51 |
allocNurseryBigInts_ = allocBigInts; |
| 52 |
|
| 53 |
minObjectHeapToTenure_ = MinHeapToTenure(allocNurseryObjects()); |
| 54 |
minStringHeapToTenure_ = MinHeapToTenure(allocNurseryStrings()); |
| 55 |
minBigintHeapToTenure_ = MinHeapToTenure(allocNurseryBigInts()); |
| 56 |
} |
| 57 |
|
| 58 |
#define INSTANTIATE_ALLOC_NURSERY_CELL(traceKind, allowGc) \ |
| 59 |
template void* \ |
| 60 |
gc::CellAllocator::AllocNurseryOrTenuredCell<traceKind, allowGc>( \ |
| 61 |
JSContext*, AllocKind, size_t, gc::Heap, AllocSite*); |
| 62 |
INSTANTIATE_ALLOC_NURSERY_CELL(JS::TraceKind::Object, NoGC) |
| 63 |
INSTANTIATE_ALLOC_NURSERY_CELL(JS::TraceKind::Object, CanGC) |
| 64 |
INSTANTIATE_ALLOC_NURSERY_CELL(JS::TraceKind::String, NoGC) |
| 65 |
INSTANTIATE_ALLOC_NURSERY_CELL(JS::TraceKind::String, CanGC) |
| 66 |
INSTANTIATE_ALLOC_NURSERY_CELL(JS::TraceKind::BigInt, NoGC) |
| 67 |
INSTANTIATE_ALLOC_NURSERY_CELL(JS::TraceKind::BigInt, CanGC) |
| 68 |
#undef INSTANTIATE_ALLOC_NURSERY_CELL |
| 69 |
|
| 70 |
// Attempt to allocate a new cell in the nursery. If there is not enough room in |
| 71 |
// the nursery or there is an OOM, this method will return nullptr. |
| 72 |
template <AllowGC allowGC> |
| 73 |
/* static */ |
| 74 |
MOZ_NEVER_INLINE void* CellAllocator::RetryNurseryAlloc(JSContext* cx, |
| 75 |
JS::TraceKind traceKind, |
| 76 |
AllocKind allocKind, |
| 77 |
size_t thingSize, |
| 78 |
AllocSite* site) { |
| 79 |
MOZ_ASSERT(cx->isNurseryAllocAllowed()); |
| 80 |
|
| 81 |
Zone* zone = site->zone(); |
| 82 |
MOZ_ASSERT(!zone->isAtomsZone()); |
| 83 |
MOZ_ASSERT(zone->allocKindInNursery(traceKind)); |
| 84 |
|
| 85 |
Nursery& nursery = cx->nursery(); |
| 86 |
JS::GCReason reason = nursery.handleAllocationFailure(); |
| 87 |
if (reason == JS::GCReason::NO_REASON) { |
| 88 |
void* ptr = nursery.tryAllocateCell(site, thingSize, traceKind); |
| 89 |
MOZ_ASSERT(ptr); |
| 90 |
return ptr; |
| 91 |
} |
| 92 |
|
| 93 |
// Our most common non-jit allocation path is NoGC; thus, if we fail the |
| 94 |
// alloc and cannot GC, we *must* return nullptr here so that the caller |
| 95 |
// will do a CanGC allocation to clear the nursery. Failing to do so will |
| 96 |
// cause all allocations on this path to land in Tenured, and we will not |
| 97 |
// get the benefit of the nursery. |
| 98 |
if constexpr (!allowGC) { |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
if (!cx->suppressGC) { |
| 103 |
cx->runtime()->gc.minorGC(reason); |
| 104 |
|
| 105 |
// Exceeding gcMaxBytes while tenuring can disable the Nursery. |
| 106 |
if (zone->allocKindInNursery(traceKind)) { |
| 107 |
void* ptr = cx->nursery().allocateCell(site, thingSize, traceKind); |
| 108 |
if (ptr) { |
| 109 |
return ptr; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// As a final fallback, allocate the cell in the tenured heap. |
| 115 |
return AllocTenuredCellForNurseryAlloc<allowGC>(cx, allocKind); |
| 116 |
} |
| 117 |
|
| 118 |
template void* CellAllocator::RetryNurseryAlloc<NoGC>(JSContext* cx, |
| 119 |
JS::TraceKind traceKind, |
| 120 |
AllocKind allocKind, |
| 121 |
size_t thingSize, |
| 122 |
AllocSite* site); |
| 123 |
template void* CellAllocator::RetryNurseryAlloc<CanGC>(JSContext* cx, |
| 124 |
JS::TraceKind traceKind, |
| 125 |
AllocKind allocKind, |
| 126 |
size_t thingSize, |
| 127 |
AllocSite* site); |
| 128 |
|
| 129 |
static inline void MajorGCIfRequested(JSContext* cx) { |
| 130 |
// Invoking the interrupt callback can fail and we can't usefully |
| 131 |
// handle that here. Just check in case we need to collect instead. |
| 132 |
if (cx->hasPendingInterrupt(InterruptReason::MajorGC)) { |
| 133 |
cx->runtime()->gc.gcIfRequested(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
template <AllowGC allowGC> |
| 138 |
MOZ_NEVER_INLINE void* gc::CellAllocator::AllocTenuredCellForNurseryAlloc( |
| 139 |
JSContext* cx, gc::AllocKind kind) { |
| 140 |
if constexpr (allowGC) { |
| 141 |
MajorGCIfRequested(cx); |
| 142 |
} |
| 143 |
|
| 144 |
return AllocTenuredCellUnchecked<allowGC>(cx->zone(), kind); |
| 145 |
} |
| 146 |
template void* gc::CellAllocator::AllocTenuredCellForNurseryAlloc<NoGC>( |
| 147 |
JSContext*, AllocKind); |
| 148 |
template void* gc::CellAllocator::AllocTenuredCellForNurseryAlloc<CanGC>( |
| 149 |
JSContext*, AllocKind); |
| 150 |
|
| 151 |
#ifdef DEBUG |
| 152 |
static bool IsAtomsZoneKind(AllocKind kind) { |
| 153 |
return kind == AllocKind::ATOM || kind == AllocKind::FAT_INLINE_ATOM || |
| 154 |
kind == AllocKind::SYMBOL; |
| 155 |
} |
| 156 |
#endif |
| 157 |
|
| 158 |
template <AllowGC allowGC> |
| 159 |
void* gc::CellAllocator::AllocTenuredCell(JSContext* cx, gc::AllocKind kind) { |
| 160 |
MOZ_ASSERT(!IsNurseryAllocable(kind)); |
| 161 |
MOZ_ASSERT_IF(cx->zone()->isAtomsZone(), |
| 162 |
IsAtomsZoneKind(kind) || kind == AllocKind::JITCODE); |
| 163 |
MOZ_ASSERT_IF(!cx->zone()->isAtomsZone(), !IsAtomsZoneKind(kind)); |
| 164 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime())); |
| 165 |
|
| 166 |
if constexpr (allowGC) { |
| 167 |
PreAllocGCChecks(cx); |
| 168 |
} |
| 169 |
|
| 170 |
if (!CheckForSimulatedFailure(cx, allowGC)) { |
| 171 |
return nullptr; |
| 172 |
} |
| 173 |
|
| 174 |
if constexpr (allowGC) { |
| 175 |
MajorGCIfRequested(cx); |
| 176 |
} |
| 177 |
|
| 178 |
return AllocTenuredCellUnchecked<allowGC>(cx->zone(), kind); |
| 179 |
} |
| 180 |
template void* gc::CellAllocator::AllocTenuredCell<NoGC>(JSContext*, AllocKind); |
| 181 |
template void* gc::CellAllocator::AllocTenuredCell<CanGC>(JSContext*, |
| 182 |
AllocKind); |
| 183 |
|
| 184 |
template <AllowGC allowGC> |
| 185 |
/* static */ |
| 186 |
void* CellAllocator::AllocTenuredCellUnchecked(JS::Zone* zone, AllocKind kind) { |
| 187 |
// Bump allocate in the arena's current free-list span. |
| 188 |
void* ptr = zone->arenas.freeLists().allocate(kind); |
| 189 |
if (MOZ_UNLIKELY(!ptr)) { |
| 190 |
// Get the next available free list and allocate out of it. This may acquire |
| 191 |
// a new arena, which will lock the chunk list. If there are no chunks |
| 192 |
// available it may also allocate new memory directly. |
| 193 |
ptr = GCRuntime::refillFreeList(zone, kind); |
| 194 |
|
| 195 |
if (MOZ_UNLIKELY(!ptr)) { |
| 196 |
if constexpr (allowGC) { |
| 197 |
return RetryTenuredAlloc(zone, kind); |
| 198 |
} |
| 199 |
|
| 200 |
return nullptr; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#ifdef DEBUG |
| 205 |
CheckIncrementalZoneState(zone, ptr); |
| 206 |
#endif |
| 207 |
|
| 208 |
gcprobes::TenuredAlloc(ptr, kind); |
| 209 |
|
| 210 |
// We count this regardless of the profiler's state, assuming that it costs |
| 211 |
// just as much to count it, as to check the profiler's state and decide not |
| 212 |
// to count it. |
| 213 |
zone->noteTenuredAlloc(); |
| 214 |
|
| 215 |
return ptr; |
| 216 |
} |
| 217 |
template void* CellAllocator::AllocTenuredCellUnchecked<NoGC>(JS::Zone* zone, |
| 218 |
AllocKind kind); |
| 219 |
template void* CellAllocator::AllocTenuredCellUnchecked<CanGC>(JS::Zone* zone, |
| 220 |
AllocKind kind); |
| 221 |
/* static */ |
| 222 |
MOZ_NEVER_INLINE void* CellAllocator::RetryTenuredAlloc(JS::Zone* zone, |
| 223 |
AllocKind kind) { |
| 224 |
JSRuntime* runtime = zone->runtimeFromMainThread(); |
| 225 |
runtime->gc.attemptLastDitchGC(); |
| 226 |
|
| 227 |
void* ptr = AllocTenuredCellUnchecked<NoGC>(zone, kind); |
| 228 |
if (!ptr) { |
| 229 |
ReportOutOfMemory(runtime->mainContextFromOwnThread()); |
| 230 |
return nullptr; |
| 231 |
} |
| 232 |
|
| 233 |
return ptr; |
| 234 |
} |
| 235 |
|
| 236 |
void GCRuntime::attemptLastDitchGC() { |
| 237 |
// Either there was no memory available for a new chunk or the heap hit its |
| 238 |
// size limit. Try to perform an all-compartments, non-incremental, shrinking |
| 239 |
// GC and wait for it to finish. |
| 240 |
|
| 241 |
if (!lastLastDitchTime.IsNull() && |
| 242 |
TimeStamp::Now() - lastLastDitchTime <= tunables.minLastDitchGCPeriod()) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
JS::PrepareForFullGC(rt->mainContextFromOwnThread()); |
| 247 |
gc(JS::GCOptions::Shrink, JS::GCReason::LAST_DITCH); |
| 248 |
waitBackgroundAllocEnd(); |
| 249 |
waitBackgroundFreeEnd(); |
| 250 |
|
| 251 |
lastLastDitchTime = mozilla::TimeStamp::Now(); |
| 252 |
} |
| 253 |
|
| 254 |
#ifdef JS_GC_ZEAL |
| 255 |
|
| 256 |
/* static */ |
| 257 |
AllocSite* CellAllocator::MaybeGenerateMissingAllocSite(JSContext* cx, |
| 258 |
JS::TraceKind traceKind, |
| 259 |
AllocSite* site) { |
| 260 |
MOZ_ASSERT(site); |
| 261 |
|
| 262 |
if (!cx->runtime()->gc.tunables.generateMissingAllocSites()) { |
| 263 |
return site; |
| 264 |
} |
| 265 |
|
| 266 |
if (!site->isUnknown()) { |
| 267 |
return site; |
| 268 |
} |
| 269 |
|
| 270 |
if (cx->inUnsafeCallWithABI) { |
| 271 |
return site; |
| 272 |
} |
| 273 |
|
| 274 |
FrameIter frame(cx); |
| 275 |
if (frame.done() || !frame.isBaseline()) { |
| 276 |
return site; |
| 277 |
} |
| 278 |
|
| 279 |
MOZ_ASSERT(site == cx->zone()->unknownAllocSite(traceKind)); |
| 280 |
MOZ_ASSERT(frame.hasScript()); |
| 281 |
|
| 282 |
JSScript* script = frame.script(); |
| 283 |
if (cx->zone() != script->zone()) { |
| 284 |
return site; // Skip cross-zone allocation. |
| 285 |
} |
| 286 |
|
| 287 |
uint32_t pcOffset = script->pcToOffset(frame.pc()); |
| 288 |
if (!script->hasBaselineScript() || pcOffset > AllocSite::MaxValidPCOffset) { |
| 289 |
return site; |
| 290 |
} |
| 291 |
|
| 292 |
AllocSite* missingSite = |
| 293 |
GetOrCreateMissingAllocSite(cx, script, pcOffset, traceKind); |
| 294 |
if (!missingSite) { |
| 295 |
return site; |
| 296 |
} |
| 297 |
|
| 298 |
return missingSite; |
| 299 |
} |
| 300 |
|
| 301 |
#endif // JS_GC_ZEAL |
| 302 |
|
| 303 |
#ifdef DEBUG |
| 304 |
/* static */ |
| 305 |
void CellAllocator::CheckIncrementalZoneState(JS::Zone* zone, void* ptr) { |
| 306 |
MOZ_ASSERT(ptr); |
| 307 |
TenuredCell* cell = reinterpret_cast<TenuredCell*>(ptr); |
| 308 |
ArenaChunkBase* chunk = detail::GetCellChunkBase(cell); |
| 309 |
if (zone->isGCMarkingOrSweeping()) { |
| 310 |
MOZ_ASSERT(chunk->markBits.isMarkedBlack(cell)); |
| 311 |
} else { |
| 312 |
MOZ_ASSERT(!chunk->markBits.isMarkedAny(cell)); |
| 313 |
} |
| 314 |
} |
| 315 |
#endif |
| 316 |
|
| 317 |
void* js::gc::AllocateTenuredCellInGC(Zone* zone, AllocKind thingKind) { |
| 318 |
void* ptr = zone->arenas.allocateFromFreeList(thingKind); |
| 319 |
if (!ptr) { |
| 320 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 321 |
ptr = GCRuntime::refillFreeListInGC(zone, thingKind); |
| 322 |
if (!ptr) { |
| 323 |
oomUnsafe.crash(ChunkSize, "Failed to allocate new chunk during GC"); |
| 324 |
} |
| 325 |
} |
| 326 |
return ptr; |
| 327 |
} |
| 328 |
|
| 329 |
// /////////// Arena -> Thing Allocator ////////////////////////////////////// |
| 330 |
|
| 331 |
void GCRuntime::startBackgroundAllocTaskIfIdle() { |
| 332 |
AutoLockHelperThreadState lock; |
| 333 |
if (!allocTask.wasStarted(lock)) { |
| 334 |
// Join the previous invocation of the task. This will return immediately |
| 335 |
// if the thread has never been started. |
| 336 |
allocTask.joinWithLockHeld(lock); |
| 337 |
allocTask.startWithLockHeld(lock); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/* static */ |
| 342 |
void* GCRuntime::refillFreeList(JS::Zone* zone, AllocKind thingKind) { |
| 343 |
MOZ_ASSERT(zone->arenas.freeLists().isEmpty(thingKind)); |
| 344 |
|
| 345 |
// It should not be possible to allocate on the main thread while we are |
| 346 |
// inside a GC. |
| 347 |
MOZ_ASSERT(!JS::RuntimeHeapIsCollecting(), "allocating while under GC"); |
| 348 |
|
| 349 |
return zone->arenas.refillFreeListAndAllocate( |
| 350 |
thingKind, ShouldCheckThresholds::CheckThresholds, StallAndRetry::No); |
| 351 |
} |
| 352 |
|
| 353 |
/* static */ |
| 354 |
void* GCRuntime::refillFreeListInGC(Zone* zone, AllocKind thingKind) { |
| 355 |
// Called when tenuring nursery cells and during compacting GC. |
| 356 |
MOZ_ASSERT_IF(!JS::RuntimeHeapIsMinorCollecting(), |
| 357 |
!zone->runtimeFromMainThread()->gc.isBackgroundSweeping()); |
| 358 |
|
| 359 |
// Since this needs to succeed we pass StallAndRetry::Yes. |
| 360 |
return zone->arenas.refillFreeListAndAllocate( |
| 361 |
thingKind, ShouldCheckThresholds::DontCheckThresholds, |
| 362 |
StallAndRetry::Yes); |
| 363 |
} |
| 364 |
|
| 365 |
void* ArenaLists::refillFreeListAndAllocate( |
| 366 |
AllocKind thingKind, ShouldCheckThresholds checkThresholds, |
| 367 |
StallAndRetry stallAndRetry) { |
| 368 |
MOZ_ASSERT(freeLists().isEmpty(thingKind)); |
| 369 |
|
| 370 |
GCRuntime* gc = &runtimeFromAnyThread()->gc; |
| 371 |
|
| 372 |
retry_loop: |
| 373 |
Arena* arena = arenaList(thingKind).takeInitialNonFullArena(); |
| 374 |
if (arena) { |
| 375 |
// Empty arenas should be immediately freed. |
| 376 |
MOZ_ASSERT(!arena->isEmpty()); |
| 377 |
return freeLists().setArenaAndAllocate(arena, thingKind); |
| 378 |
} |
| 379 |
|
| 380 |
// If we have just finished background sweep then merge the swept arenas in |
| 381 |
// and retry. |
| 382 |
if (MOZ_UNLIKELY(concurrentUse(thingKind) == |
| 383 |
ConcurrentUse::BackgroundFinalizeFinished)) { |
| 384 |
ArenaList sweptArenas; |
| 385 |
{ |
| 386 |
AutoLockGC lock(gc); |
| 387 |
sweptArenas = std::move(collectingArenaList(thingKind)); |
| 388 |
} |
| 389 |
concurrentUse(thingKind) = ConcurrentUse::None; |
| 390 |
if (!sweptArenas.isEmpty()) { |
| 391 |
mergeSweptArenas(thingKind, sweptArenas); |
| 392 |
goto retry_loop; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
// Use the current chunk if set. |
| 397 |
ArenaChunk* chunk = gc->currentChunk_; |
| 398 |
MOZ_ASSERT_IF(chunk, gc->isCurrentChunk(chunk)); |
| 399 |
|
| 400 |
if (!chunk) { |
| 401 |
// The chunk lists can be accessed by background sweeping and background |
| 402 |
// chunk allocation. Take the GC lock to synchronize access. |
| 403 |
AutoLockGCBgAlloc lock(gc); |
| 404 |
|
| 405 |
chunk = gc->pickChunk(stallAndRetry, lock); |
| 406 |
if (!chunk) { |
| 407 |
return nullptr; |
| 408 |
} |
| 409 |
|
| 410 |
gc->setCurrentChunk(chunk, lock); |
| 411 |
} |
| 412 |
|
| 413 |
MOZ_ASSERT(gc->isCurrentChunk(chunk)); |
| 414 |
|
| 415 |
// Although our chunk should definitely have enough space for another arena, |
| 416 |
// there are other valid reasons why ArenaChunk::allocateArena() may fail. |
| 417 |
arena = gc->allocateArena(chunk, zone_, thingKind, checkThresholds); |
| 418 |
if (!arena) { |
| 419 |
return nullptr; |
| 420 |
} |
| 421 |
|
| 422 |
arena->init(gc, zone_, thingKind); |
| 423 |
|
| 424 |
ArenaList& al = arenaList(thingKind); |
| 425 |
MOZ_ASSERT(!al.hasNonFullArenas()); |
| 426 |
al.pushBack(arena); |
| 427 |
|
| 428 |
return freeLists().setArenaAndAllocate(arena, thingKind); |
| 429 |
} |
| 430 |
|
| 431 |
inline void* FreeLists::setArenaAndAllocate(Arena* arena, AllocKind kind) { |
| 432 |
#ifdef DEBUG |
| 433 |
auto* old = freeLists_[kind]; |
| 434 |
if (!old->isEmpty()) { |
| 435 |
old->getArena()->checkNoMarkedFreeCells(); |
| 436 |
} |
| 437 |
#endif |
| 438 |
|
| 439 |
FreeSpan* span = arena->getFirstFreeSpan(); |
| 440 |
freeLists_[kind] = span; |
| 441 |
|
| 442 |
Zone* zone = arena->zone(); |
| 443 |
if (MOZ_UNLIKELY(zone->isGCMarkingOrSweeping())) { |
| 444 |
arena->arenaAllocatedDuringGC(); |
| 445 |
} |
| 446 |
|
| 447 |
TenuredCell* thing = span->allocate(Arena::thingSize(kind)); |
| 448 |
MOZ_ASSERT(thing); // This allocation is infallible. |
| 449 |
|
| 450 |
return thing; |
| 451 |
} |
| 452 |
|
| 453 |
void Arena::arenaAllocatedDuringGC() { |
| 454 |
// Ensure that anything allocated during the mark or sweep phases of an |
| 455 |
// incremental GC will be marked black by pre-marking all free cells in the |
| 456 |
// arena we are about to allocate from. |
| 457 |
|
| 458 |
MOZ_ASSERT(zone()->isGCMarkingOrSweeping()); |
| 459 |
for (ArenaFreeCellIter cell(this); !cell.done(); cell.next()) { |
| 460 |
MOZ_ASSERT(!cell->isMarkedAny()); |
| 461 |
cell->markBlack(); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// /////////// ArenaChunk -> Arena Allocator ///////////////////////////////// |
| 466 |
|
| 467 |
bool GCRuntime::wantBackgroundAllocation(const AutoLockGC& lock) const { |
| 468 |
// To minimize memory waste, we do not want to run the background chunk |
| 469 |
// allocation if we already have some empty chunks or when the runtime has |
| 470 |
// a small heap size (and therefore likely has a small growth rate). |
| 471 |
return allocTask.enabled() && |
| 472 |
emptyChunks(lock).count() < minEmptyChunkCount(lock) && |
| 473 |
(fullChunks(lock).count() + availableChunks(lock).count()) >= 4; |
| 474 |
} |
| 475 |
|
| 476 |
// Allocate a new arena but don't initialize it. |
| 477 |
Arena* GCRuntime::allocateArena(ArenaChunk* chunk, Zone* zone, |
| 478 |
AllocKind thingKind, |
| 479 |
ShouldCheckThresholds checkThresholds) { |
| 480 |
MOZ_ASSERT(chunk->hasAvailableArenas()); |
| 481 |
|
| 482 |
// Fail the allocation if we are over our heap size limits. |
| 483 |
if ((checkThresholds != ShouldCheckThresholds::DontCheckThresholds) && |
| 484 |
(heapSize.bytes() >= tunables.gcMaxBytes())) { |
| 485 |
return nullptr; |
| 486 |
} |
| 487 |
|
| 488 |
Arena* arena = chunk->allocateArena(this, zone, thingKind); |
| 489 |
|
| 490 |
if (IsBufferAllocKind(thingKind)) { |
| 491 |
// Try to keep GC scheduling the same to minimize benchmark noise. |
| 492 |
// Keep this in sync with Arena::release. |
| 493 |
size_t usableSize = ArenaSize - Arena::firstThingOffset(thingKind); |
| 494 |
zone->mallocHeapSize.addBytes(usableSize); |
| 495 |
} else { |
| 496 |
zone->gcHeapSize.addGCArena(heapSize); |
| 497 |
} |
| 498 |
|
| 499 |
// Trigger an incremental slice if needed. |
| 500 |
if (checkThresholds != ShouldCheckThresholds::DontCheckThresholds) { |
| 501 |
maybeTriggerGCAfterAlloc(zone); |
| 502 |
} |
| 503 |
|
| 504 |
return arena; |
| 505 |
} |
| 506 |
|
| 507 |
Arena* ArenaChunk::allocateArena(GCRuntime* gc, Zone* zone, |
| 508 |
AllocKind thingKind) { |
| 509 |
MOZ_ASSERT(info.isCurrentChunk); |
| 510 |
MOZ_ASSERT(hasAvailableArenas()); |
| 511 |
|
| 512 |
if (info.numArenasFreeCommitted == 0) { |
| 513 |
commitOnePage(gc); |
| 514 |
MOZ_ASSERT(info.numArenasFreeCommitted == ArenasPerPage); |
| 515 |
} |
| 516 |
|
| 517 |
MOZ_ASSERT(info.numArenasFreeCommitted > 0); |
| 518 |
Arena* arena = fetchNextFreeArena(gc); |
| 519 |
|
| 520 |
updateCurrentChunkAfterAlloc(gc); |
| 521 |
|
| 522 |
verify(); |
| 523 |
|
| 524 |
return arena; |
| 525 |
} |
| 526 |
|
| 527 |
void ArenaChunk::commitOnePage(GCRuntime* gc) { |
| 528 |
MOZ_ASSERT(info.numArenasFreeCommitted == 0); |
| 529 |
MOZ_ASSERT(info.numArenasFree >= ArenasPerPage); |
| 530 |
|
| 531 |
uint32_t pageIndex = decommittedPages.FindFirst(); |
| 532 |
MOZ_ASSERT(pageIndex < PagesPerChunk); |
| 533 |
MOZ_ASSERT(decommittedPages[pageIndex]); |
| 534 |
|
| 535 |
if (DecommitEnabled()) { |
| 536 |
MarkPagesInUseSoft(pageAddress(pageIndex), PageSize); |
| 537 |
} |
| 538 |
|
| 539 |
decommittedPages[pageIndex] = false; |
| 540 |
|
| 541 |
for (size_t i = 0; i < ArenasPerPage; i++) { |
| 542 |
size_t arenaIndex = pageToArenaIndex(pageIndex) + i; |
| 543 |
MOZ_ASSERT(!freeCommittedArenas[arenaIndex]); |
| 544 |
freeCommittedArenas[arenaIndex] = true; |
| 545 |
++info.numArenasFreeCommitted; |
| 546 |
} |
| 547 |
|
| 548 |
verify(); |
| 549 |
} |
| 550 |
|
| 551 |
Arena* ArenaChunk::fetchNextFreeArena(GCRuntime* gc) { |
| 552 |
MOZ_ASSERT(info.numArenasFreeCommitted > 0); |
| 553 |
MOZ_ASSERT(info.numArenasFreeCommitted <= info.numArenasFree); |
| 554 |
|
| 555 |
size_t index = freeCommittedArenas.FindFirst(); |
| 556 |
MOZ_ASSERT(index < ArenasPerChunk); |
| 557 |
|
| 558 |
MOZ_ASSERT(freeCommittedArenas[index]); |
| 559 |
freeCommittedArenas[index] = false; |
| 560 |
|
| 561 |
return &arenas[index]; |
| 562 |
} |
| 563 |
|
| 564 |
// /////////// System -> ArenaChunk Allocator //////////////////////////////// |
| 565 |
|
| 566 |
ArenaChunk* GCRuntime::takeOrAllocChunk(StallAndRetry stallAndRetry, |
| 567 |
AutoLockGCBgAlloc& lock) { |
| 568 |
ArenaChunk* chunk = getOrAllocChunk(stallAndRetry, lock); |
| 569 |
if (!chunk) { |
| 570 |
return nullptr; |
| 571 |
} |
| 572 |
|
| 573 |
emptyChunks(lock).remove(chunk); |
| 574 |
return chunk; |
| 575 |
} |
| 576 |
|
| 577 |
ArenaChunk* GCRuntime::getOrAllocChunk(StallAndRetry stallAndRetry, |
| 578 |
AutoLockGCBgAlloc& lock) { |
| 579 |
ArenaChunk* chunk; |
| 580 |
if (!emptyChunks(lock).empty()) { |
| 581 |
chunk = emptyChunks(lock).head(); |
| 582 |
// Reinitialize ChunkBase; arenas are all free and may or may not be |
| 583 |
// committed. |
| 584 |
SetMemCheckKind(chunk, sizeof(ChunkBase), MemCheckKind::MakeUndefined); |
| 585 |
chunk->initBaseForArenaChunk(rt); |
| 586 |
MOZ_ASSERT(chunk->isEmpty()); |
| 587 |
} else { |
| 588 |
void* ptr = ArenaChunk::allocate(this, stallAndRetry); |
| 589 |
if (!ptr) { |
| 590 |
return nullptr; |
| 591 |
} |
| 592 |
|
| 593 |
chunk = ArenaChunk::emplace(ptr, this, /* allMemoryCommitted = */ true); |
| 594 |
MOZ_ASSERT(chunk->isEmpty()); |
| 595 |
emptyChunks(lock).push(chunk); |
| 596 |
} |
| 597 |
|
| 598 |
if (wantBackgroundAllocation(lock)) { |
| 599 |
lock.tryToStartBackgroundAllocation(); |
| 600 |
} |
| 601 |
|
| 602 |
return chunk; |
| 603 |
} |
| 604 |
|
| 605 |
void GCRuntime::recycleChunk(ArenaChunk* chunk, const AutoLockGC& lock) { |
| 606 |
#ifdef DEBUG |
| 607 |
MOZ_ASSERT(chunk->isEmpty()); |
| 608 |
MOZ_ASSERT(!chunk->info.isCurrentChunk); |
| 609 |
chunk->verify(); |
| 610 |
#endif |
| 611 |
|
| 612 |
// Poison ChunkBase to catch use after free. |
| 613 |
AlwaysPoison(chunk, JS_FREED_CHUNK_PATTERN, sizeof(ChunkBase), |
| 614 |
MemCheckKind::MakeNoAccess); |
| 615 |
|
| 616 |
emptyChunks(lock).push(chunk); |
| 617 |
} |
| 618 |
|
| 619 |
ArenaChunk* GCRuntime::pickChunk(StallAndRetry stallAndRetry, |
| 620 |
AutoLockGCBgAlloc& lock) { |
| 621 |
if (availableChunks(lock).count()) { |
| 622 |
ArenaChunk* chunk = availableChunks(lock).head(); |
| 623 |
availableChunks(lock).remove(chunk); |
| 624 |
return chunk; |
| 625 |
} |
| 626 |
|
| 627 |
ArenaChunk* chunk = takeOrAllocChunk(stallAndRetry, lock); |
| 628 |
if (!chunk) { |
| 629 |
return nullptr; |
| 630 |
} |
| 631 |
|
| 632 |
#ifdef DEBUG |
| 633 |
chunk->verify(); |
| 634 |
MOZ_ASSERT(chunk->isEmpty()); |
| 635 |
#endif |
| 636 |
|
| 637 |
return chunk; |
| 638 |
} |
| 639 |
|
| 640 |
BackgroundAllocTask::BackgroundAllocTask(GCRuntime* gc, ChunkPool& pool) |
| 641 |
: GCParallelTask(gc, gcstats::PhaseKind::NONE), |
| 642 |
chunkPool_(pool), |
| 643 |
enabled_(CanUseExtraThreads() && GetCPUCount() >= 2) { |
| 644 |
// This can occur outside GCs so doesn't have a stats phase. |
| 645 |
} |
| 646 |
|
| 647 |
void BackgroundAllocTask::run(AutoLockHelperThreadState& lock) { |
| 648 |
AutoUnlockHelperThreadState unlock(lock); |
| 649 |
|
| 650 |
AutoLockGC gcLock(gc); |
| 651 |
while (!isCancelled() && gc->wantBackgroundAllocation(gcLock)) { |
| 652 |
ArenaChunk* chunk; |
| 653 |
{ |
| 654 |
AutoUnlockGC unlock(gcLock); |
| 655 |
void* ptr = ArenaChunk::allocate(gc, StallAndRetry::No); |
| 656 |
if (!ptr) { |
| 657 |
break; |
| 658 |
} |
| 659 |
chunk = ArenaChunk::emplace(ptr, gc, /* allMemoryCommitted = */ true); |
| 660 |
} |
| 661 |
chunkPool_.ref().push(chunk); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* static */ |
| 666 |
void* ArenaChunk::allocate(GCRuntime* gc, StallAndRetry stallAndRetry) { |
| 667 |
void* chunk = MapAlignedPages(ChunkSize, ChunkSize, stallAndRetry); |
| 668 |
if (!chunk) { |
| 669 |
return nullptr; |
| 670 |
} |
| 671 |
|
| 672 |
gc->stats().count(gcstats::COUNT_NEW_CHUNK); |
| 673 |
return chunk; |
| 674 |
} |
| 675 |
|
| 676 |
static inline bool ShouldDecommitNewChunk(bool allMemoryCommitted, |
| 677 |
const GCSchedulingState& state) { |
| 678 |
if (!DecommitEnabled()) { |
| 679 |
return false; |
| 680 |
} |
| 681 |
|
| 682 |
return !allMemoryCommitted || !state.inHighFrequencyGCMode(); |
| 683 |
} |
| 684 |
|
| 685 |
ArenaChunk* ArenaChunk::emplace(void* ptr, GCRuntime* gc, |
| 686 |
bool allMemoryCommitted) { |
| 687 |
/* The chunk may still have some regions marked as no-access. */ |
| 688 |
MOZ_MAKE_MEM_UNDEFINED(ptr, ChunkSize); |
| 689 |
|
| 690 |
/* |
| 691 |
* Poison the chunk. Note that decommitAllArenas() below will mark the |
| 692 |
* arenas as inaccessible (for memory sanitizers). |
| 693 |
*/ |
| 694 |
Poison(ptr, JS_FRESH_TENURED_PATTERN, ChunkSize, MemCheckKind::MakeUndefined); |
| 695 |
|
| 696 |
ArenaChunk* chunk = new (mozilla::KnownNotNull, ptr) ArenaChunk(gc->rt); |
| 697 |
|
| 698 |
if (ShouldDecommitNewChunk(allMemoryCommitted, gc->schedulingState)) { |
| 699 |
// Decommit the arenas. We do this after poisoning so that if the OS does |
| 700 |
// not have to recycle the pages, we still get the benefit of poisoning. |
| 701 |
chunk->decommitAllArenas(); |
| 702 |
} else { |
| 703 |
chunk->initAsCommitted(); |
| 704 |
} |
| 705 |
|
| 706 |
MOZ_ASSERT(chunk->isEmpty()); |
| 707 |
chunk->verify(); |
| 708 |
|
| 709 |
return chunk; |
| 710 |
} |
| 711 |
|
| 712 |
void ArenaChunk::decommitAllArenas() { |
| 713 |
MOZ_ASSERT(isEmpty()); |
| 714 |
MarkPagesUnusedSoft(&arenas[0], ArenasPerChunk * ArenaSize); |
| 715 |
initAsDecommitted(); |
| 716 |
} |
| 717 |
|
| 718 |
void ArenaChunkBase::initAsDecommitted() { |
| 719 |
// Set the state of all arenas to free and decommitted. They might not |
| 720 |
// actually be decommitted, but in that case the re-commit operation is a |
| 721 |
// no-op so it doesn't matter. |
| 722 |
decommittedPages.SetAll(); |
| 723 |
freeCommittedArenas.ResetAll(); |
| 724 |
info.numArenasFree = ArenasPerChunk; |
| 725 |
info.numArenasFreeCommitted = 0; |
| 726 |
} |
| 727 |
|
| 728 |
void ArenaChunkBase::initAsCommitted() { |
| 729 |
// Set the state of all arenas to free and committed. They might have |
| 730 |
// been decommitted, but in that case the re-commit operation is a |
| 731 |
// no-op so it doesn't matter. |
| 732 |
decommittedPages.ResetAll(); |
| 733 |
freeCommittedArenas.SetAll(); |
| 734 |
info.numArenasFree = ArenasPerChunk; |
| 735 |
info.numArenasFreeCommitted = ArenasPerChunk; |
| 736 |
} |