| 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 "vm/HelperThreads.h" |
| 8 |
|
| 9 |
#include "mozilla/ReverseIterator.h" // mozilla::Reversed(...) |
| 10 |
#include "mozilla/ScopeExit.h" |
| 11 |
#include "mozilla/Span.h" // mozilla::Span<TaggedScriptThingIndex> |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
|
| 15 |
#include "frontend/CompilationStencil.h" // frontend::CompilationStencil |
| 16 |
#include "gc/GC.h" |
| 17 |
#include "gc/Zone.h" |
| 18 |
#include "jit/BaselineCompileTask.h" |
| 19 |
#include "jit/Ion.h" |
| 20 |
#include "jit/IonCompileTask.h" |
| 21 |
#include "jit/JitRuntime.h" |
| 22 |
#include "jit/JitScript.h" |
| 23 |
#include "js/CompileOptions.h" // JS::PrefableCompileOptions, JS::ReadOnlyCompileOptions |
| 24 |
#include "js/experimental/CompileScript.h" // JS::ThreadStackQuotaForSize |
| 25 |
#include "js/friend/StackLimits.h" // js::ReportOverRecursed |
| 26 |
#include "js/HelperThreadAPI.h" |
| 27 |
#include "js/Stack.h" |
| 28 |
#include "js/UniquePtr.h" |
| 29 |
#include "js/Utility.h" |
| 30 |
#include "threading/CpuCount.h" |
| 31 |
#include "vm/ErrorReporting.h" |
| 32 |
#include "vm/HelperThreadState.h" |
| 33 |
#include "vm/InternalThreadPool.h" |
| 34 |
#include "vm/MutexIDs.h" |
| 35 |
#include "wasm/WasmGenerator.h" |
| 36 |
|
| 37 |
using namespace js; |
| 38 |
|
| 39 |
using mozilla::TimeDuration; |
| 40 |
|
| 41 |
static void CancelOffThreadWasmCompleteTier2GeneratorLocked( |
| 42 |
AutoLockHelperThreadState& lock); |
| 43 |
static void CancelOffThreadWasmPartialTier2CompileLocked( |
| 44 |
AutoLockHelperThreadState& lock); |
| 45 |
|
| 46 |
// This file is structured as follows: |
| 47 |
// |
| 48 |
// (1) Methods for GlobalHelperThreadState, and top level scheduling logic |
| 49 |
// (2) Specifics for JS task classes |
| 50 |
// (3) Specifics for wasm task classes |
| 51 |
|
| 52 |
/////////////////////////////////////////////////////////////////////////// |
| 53 |
// // |
| 54 |
// GlobalHelperThreadState methods and top-level scheduling logic // |
| 55 |
// // |
| 56 |
/////////////////////////////////////////////////////////////////////////// |
| 57 |
|
| 58 |
namespace js { |
| 59 |
|
| 60 |
MOZ_RUNINIT Mutex gHelperThreadLock(mutexid::GlobalHelperThreadState); |
| 61 |
GlobalHelperThreadState* gHelperThreadState = nullptr; |
| 62 |
|
| 63 |
} // namespace js |
| 64 |
|
| 65 |
bool js::CreateHelperThreadsState() { |
| 66 |
MOZ_ASSERT(!gHelperThreadState); |
| 67 |
gHelperThreadState = js_new<GlobalHelperThreadState>(); |
| 68 |
return gHelperThreadState; |
| 69 |
} |
| 70 |
|
| 71 |
void js::DestroyHelperThreadsState() { |
| 72 |
AutoLockHelperThreadState lock; |
| 73 |
|
| 74 |
if (!gHelperThreadState) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
gHelperThreadState->finish(lock); |
| 79 |
js_delete(gHelperThreadState); |
| 80 |
gHelperThreadState = nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
bool js::EnsureHelperThreadsInitialized() { |
| 84 |
MOZ_ASSERT(gHelperThreadState); |
| 85 |
return gHelperThreadState->ensureInitialized(); |
| 86 |
} |
| 87 |
|
| 88 |
static size_t ClampDefaultCPUCount(size_t cpuCount) { |
| 89 |
// It's extremely rare for SpiderMonkey to have more than a few cores worth |
| 90 |
// of work. At higher core counts, performance can even decrease due to NUMA |
| 91 |
// (and SpiderMonkey's lack of NUMA-awareness), contention, and general lack |
| 92 |
// of optimization for high core counts. So to avoid wasting thread stack |
| 93 |
// resources (and cluttering gdb and core dumps), clamp to 8 cores for now. |
| 94 |
return std::min<size_t>(cpuCount, 8); |
| 95 |
} |
| 96 |
|
| 97 |
static size_t ThreadCountForCPUCount(size_t cpuCount) { |
| 98 |
// We need at least two threads for tier-2 wasm compilations, because |
| 99 |
// there's a master task that holds a thread while other threads do the |
| 100 |
// compilation. |
| 101 |
return std::max<size_t>(cpuCount, 2); |
| 102 |
} |
| 103 |
|
| 104 |
bool js::SetFakeCPUCount(size_t count) { |
| 105 |
HelperThreadState().setCpuCount(count); |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
void GlobalHelperThreadState::setCpuCount(size_t count) { |
| 110 |
// This must be called before any threads have been initialized. |
| 111 |
AutoLockHelperThreadState lock; |
| 112 |
MOZ_ASSERT(!isInitialized(lock)); |
| 113 |
|
| 114 |
// We can't do this if an external thread pool is in use. |
| 115 |
MOZ_ASSERT(!dispatchTaskCallback); |
| 116 |
|
| 117 |
cpuCount = count; |
| 118 |
threadCount = ThreadCountForCPUCount(count); |
| 119 |
} |
| 120 |
|
| 121 |
size_t js::GetHelperThreadCount() { return HelperThreadState().threadCount; } |
| 122 |
|
| 123 |
size_t js::GetHelperThreadCPUCount() { return HelperThreadState().cpuCount; } |
| 124 |
|
| 125 |
void JS::SetProfilingThreadCallbacks( |
| 126 |
JS::RegisterThreadCallback registerThread, |
| 127 |
JS::UnregisterThreadCallback unregisterThread) { |
| 128 |
HelperThreadState().registerThread = registerThread; |
| 129 |
HelperThreadState().unregisterThread = unregisterThread; |
| 130 |
} |
| 131 |
|
| 132 |
// Bug 1630189: Without MOZ_NEVER_INLINE, Windows PGO builds have a linking |
| 133 |
// error for HelperThreadTaskCallback. |
| 134 |
JS_PUBLIC_API MOZ_NEVER_INLINE void JS::SetHelperThreadTaskCallback( |
| 135 |
HelperThreadTaskCallback callback, size_t threadCount, size_t stackSize) { |
| 136 |
AutoLockHelperThreadState lock; |
| 137 |
HelperThreadState().setDispatchTaskCallback(callback, threadCount, stackSize, |
| 138 |
lock); |
| 139 |
} |
| 140 |
|
| 141 |
JS_PUBLIC_API MOZ_NEVER_INLINE const char* JS::GetHelperThreadTaskName( |
| 142 |
HelperThreadTask* task) { |
| 143 |
return task->getName(); |
| 144 |
} |
| 145 |
|
| 146 |
void GlobalHelperThreadState::setDispatchTaskCallback( |
| 147 |
JS::HelperThreadTaskCallback callback, size_t threadCount, size_t stackSize, |
| 148 |
const AutoLockHelperThreadState& lock) { |
| 149 |
MOZ_ASSERT(!isInitialized(lock)); |
| 150 |
MOZ_ASSERT(!dispatchTaskCallback); |
| 151 |
MOZ_ASSERT(threadCount != 0); |
| 152 |
MOZ_ASSERT(stackSize >= 16 * 1024); |
| 153 |
|
| 154 |
dispatchTaskCallback = callback; |
| 155 |
this->threadCount = threadCount; |
| 156 |
this->stackQuota = JS::ThreadStackQuotaForSize(stackSize); |
| 157 |
} |
| 158 |
|
| 159 |
bool GlobalHelperThreadState::ensureInitialized() { |
| 160 |
MOZ_ASSERT(CanUseExtraThreads()); |
| 161 |
MOZ_ASSERT(this == &HelperThreadState()); |
| 162 |
|
| 163 |
AutoLockHelperThreadState lock; |
| 164 |
|
| 165 |
if (isInitialized(lock)) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
for (size_t& i : runningTaskCount) { |
| 170 |
i = 0; |
| 171 |
} |
| 172 |
|
| 173 |
useInternalThreadPool_ = !dispatchTaskCallback; |
| 174 |
if (useInternalThreadPool(lock)) { |
| 175 |
if (!InternalThreadPool::Initialize(threadCount, lock)) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
MOZ_ASSERT(dispatchTaskCallback); |
| 181 |
|
| 182 |
if (!ensureThreadCount(threadCount, lock)) { |
| 183 |
finishThreads(lock); |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
MOZ_ASSERT(threadCount != 0); |
| 188 |
isInitialized_ = true; |
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
bool GlobalHelperThreadState::ensureThreadCount( |
| 193 |
size_t count, AutoLockHelperThreadState& lock) { |
| 194 |
if (!helperTasks_.reserve(count)) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
if (useInternalThreadPool(lock)) { |
| 199 |
InternalThreadPool& pool = InternalThreadPool::Get(); |
| 200 |
if (pool.threadCount(lock) < count) { |
| 201 |
if (!pool.ensureThreadCount(count, lock)) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
threadCount = pool.threadCount(lock); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
GlobalHelperThreadState::GlobalHelperThreadState() |
| 213 |
: cpuCount(0), |
| 214 |
threadCount(0), |
| 215 |
totalCountRunningTasks(0), |
| 216 |
registerThread(nullptr), |
| 217 |
unregisterThread(nullptr), |
| 218 |
wasmCompleteTier2GeneratorsFinished_(0) { |
| 219 |
MOZ_ASSERT(!gHelperThreadState); |
| 220 |
|
| 221 |
cpuCount = ClampDefaultCPUCount(GetCPUCount()); |
| 222 |
threadCount = ThreadCountForCPUCount(cpuCount); |
| 223 |
|
| 224 |
MOZ_ASSERT(cpuCount > 0, "GetCPUCount() seems broken"); |
| 225 |
} |
| 226 |
|
| 227 |
void GlobalHelperThreadState::finish(AutoLockHelperThreadState& lock) { |
| 228 |
if (!isInitialized(lock)) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
MOZ_ASSERT_IF(!JSRuntime::hasLiveRuntimes(), gcParallelMarkingThreads == 0); |
| 233 |
|
| 234 |
finishThreads(lock); |
| 235 |
|
| 236 |
// Make sure there are no Ion free tasks left. We check this here because, |
| 237 |
// unlike the other tasks, we don't explicitly block on this when |
| 238 |
// destroying a runtime. |
| 239 |
auto& freeList = ionFreeList(lock); |
| 240 |
while (!freeList.empty()) { |
| 241 |
UniquePtr<jit::IonFreeTask> task = std::move(freeList.back()); |
| 242 |
freeList.popBack(); |
| 243 |
jit::FreeIonCompileTasks(task->compileTasks()); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
void GlobalHelperThreadState::finishThreads(AutoLockHelperThreadState& lock) { |
| 248 |
waitForAllTasksLocked(lock); |
| 249 |
terminating_ = true; |
| 250 |
|
| 251 |
if (InternalThreadPool::IsInitialized()) { |
| 252 |
InternalThreadPool::ShutDown(lock); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
#ifdef DEBUG |
| 257 |
void GlobalHelperThreadState::assertIsLockedByCurrentThread() const { |
| 258 |
gHelperThreadLock.assertOwnedByCurrentThread(); |
| 259 |
} |
| 260 |
#endif // DEBUG |
| 261 |
|
| 262 |
void GlobalHelperThreadState::dispatch(const AutoLockHelperThreadState& lock) { |
| 263 |
if (helperTasks_.length() >= threadCount) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
HelperThreadTask* task = findHighestPriorityTask(lock); |
| 268 |
if (!task) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
#ifdef DEBUG |
| 273 |
MOZ_ASSERT(tasksPending_ < threadCount); |
| 274 |
tasksPending_++; |
| 275 |
#endif |
| 276 |
|
| 277 |
// Add task to list of running tasks immediately. |
| 278 |
helperTasks(lock).infallibleEmplaceBack(task); |
| 279 |
runningTaskCount[task->threadType()]++; |
| 280 |
totalCountRunningTasks++; |
| 281 |
|
| 282 |
lock.queueTaskToDispatch(task); |
| 283 |
} |
| 284 |
|
| 285 |
void GlobalHelperThreadState::wait( |
| 286 |
AutoLockHelperThreadState& lock, |
| 287 |
TimeDuration timeout /* = TimeDuration::Forever() */) { |
| 288 |
MOZ_ASSERT(!lock.hasQueuedTasks()); |
| 289 |
consumerWakeup.wait_for(lock, timeout); |
| 290 |
} |
| 291 |
|
| 292 |
void GlobalHelperThreadState::notifyAll(const AutoLockHelperThreadState&) { |
| 293 |
consumerWakeup.notify_all(); |
| 294 |
} |
| 295 |
|
| 296 |
void GlobalHelperThreadState::notifyOne(const AutoLockHelperThreadState&) { |
| 297 |
consumerWakeup.notify_one(); |
| 298 |
} |
| 299 |
|
| 300 |
bool GlobalHelperThreadState::hasActiveThreads( |
| 301 |
const AutoLockHelperThreadState& lock) { |
| 302 |
return !helperTasks(lock).empty(); |
| 303 |
} |
| 304 |
|
| 305 |
void js::WaitForAllHelperThreads() { HelperThreadState().waitForAllTasks(); } |
| 306 |
|
| 307 |
void js::WaitForAllHelperThreads(AutoLockHelperThreadState& lock) { |
| 308 |
HelperThreadState().waitForAllTasksLocked(lock); |
| 309 |
} |
| 310 |
|
| 311 |
void GlobalHelperThreadState::waitForAllTasks() { |
| 312 |
AutoLockHelperThreadState lock; |
| 313 |
waitForAllTasksLocked(lock); |
| 314 |
} |
| 315 |
|
| 316 |
void GlobalHelperThreadState::waitForAllTasksLocked( |
| 317 |
AutoLockHelperThreadState& lock) { |
| 318 |
CancelOffThreadWasmCompleteTier2GeneratorLocked(lock); |
| 319 |
CancelOffThreadWasmPartialTier2CompileLocked(lock); |
| 320 |
|
| 321 |
while (canStartTasks(lock) || hasActiveThreads(lock)) { |
| 322 |
wait(lock); |
| 323 |
} |
| 324 |
|
| 325 |
MOZ_ASSERT(tasksPending_ == 0); |
| 326 |
MOZ_ASSERT(gcParallelWorklist().isEmpty(lock)); |
| 327 |
MOZ_ASSERT(ionWorklist(lock).empty()); |
| 328 |
MOZ_ASSERT(wasmWorklist(lock, wasm::CompileState::EagerTier1).empty()); |
| 329 |
MOZ_ASSERT(promiseHelperTasks(lock).empty()); |
| 330 |
MOZ_ASSERT(compressionWorklist(lock).empty()); |
| 331 |
MOZ_ASSERT(ionFreeList(lock).empty()); |
| 332 |
MOZ_ASSERT(wasmWorklist(lock, wasm::CompileState::EagerTier2).empty()); |
| 333 |
MOZ_ASSERT(wasmCompleteTier2GeneratorWorklist(lock).empty()); |
| 334 |
MOZ_ASSERT(wasmPartialTier2CompileWorklist(lock).empty()); |
| 335 |
MOZ_ASSERT(!tasksPending_); |
| 336 |
MOZ_ASSERT(!hasActiveThreads(lock)); |
| 337 |
} |
| 338 |
|
| 339 |
// A task can be a "master" task, ie, it will block waiting for other worker |
| 340 |
// threads that perform work on its behalf. If so it must not take the last |
| 341 |
// available thread; there must always be at least one worker thread able to do |
| 342 |
// the actual work. (Or the system may deadlock.) |
| 343 |
// |
| 344 |
// If a task is a master task it *must* pass isMaster=true here, or perform a |
| 345 |
// similar calculation to avoid deadlock from starvation. |
| 346 |
// |
| 347 |
// isMaster should only be true if the thread calling checkTaskThreadLimit() is |
| 348 |
// a helper thread. |
| 349 |
// |
| 350 |
// NOTE: Calling checkTaskThreadLimit() from a helper thread in the dynamic |
| 351 |
// region after currentTask.emplace() and before currentTask.reset() may cause |
| 352 |
// it to return a different result than if it is called outside that dynamic |
| 353 |
// region, as the predicate inspects the values of the threads' currentTask |
| 354 |
// members. |
| 355 |
|
| 356 |
bool GlobalHelperThreadState::checkTaskThreadLimit( |
| 357 |
ThreadType threadType, size_t maxThreads, bool isMaster, |
| 358 |
const AutoLockHelperThreadState& lock) const { |
| 359 |
MOZ_ASSERT(maxThreads >= 1); |
| 360 |
MOZ_ASSERT(maxThreads <= threadCount); |
| 361 |
|
| 362 |
// Check thread limit for this task kind. |
| 363 |
size_t count = runningTaskCount[threadType]; |
| 364 |
if (count >= maxThreads) { |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
// Check overall idle thread count taking into account master threads. A |
| 369 |
// master thread must not use the last idle thread or it will deadlock itself. |
| 370 |
MOZ_ASSERT(threadCount >= totalCountRunningTasks); |
| 371 |
size_t idleCount = threadCount - totalCountRunningTasks; |
| 372 |
size_t idleRequired = isMaster ? 2 : 1; |
| 373 |
return idleCount >= idleRequired; |
| 374 |
} |
| 375 |
|
| 376 |
static inline bool IsHelperThreadSimulatingOOM(js::ThreadType threadType) { |
| 377 |
#if defined(DEBUG) || defined(JS_OOM_BREAKPOINT) |
| 378 |
return js::oom::simulator.targetThread() == threadType; |
| 379 |
#else |
| 380 |
return false; |
| 381 |
#endif |
| 382 |
} |
| 383 |
|
| 384 |
void GlobalHelperThreadState::addSizeOfIncludingThis( |
| 385 |
JS::GlobalStats* stats, const AutoLockHelperThreadState& lock) const { |
| 386 |
#ifdef DEBUG |
| 387 |
assertIsLockedByCurrentThread(); |
| 388 |
#endif |
| 389 |
|
| 390 |
mozilla::MallocSizeOf mallocSizeOf = stats->mallocSizeOf_; |
| 391 |
JS::HelperThreadStats& htStats = stats->helperThread; |
| 392 |
|
| 393 |
htStats.stateData += mallocSizeOf(this); |
| 394 |
|
| 395 |
if (InternalThreadPool::IsInitialized()) { |
| 396 |
htStats.stateData += |
| 397 |
InternalThreadPool::Get().sizeOfIncludingThis(mallocSizeOf, lock); |
| 398 |
} |
| 399 |
|
| 400 |
// Report memory used by various containers |
| 401 |
htStats.stateData += |
| 402 |
ionWorklist_.sizeOfExcludingThis(mallocSizeOf) + |
| 403 |
ionFinishedList_.sizeOfExcludingThis(mallocSizeOf) + |
| 404 |
ionFreeList_.sizeOfExcludingThis(mallocSizeOf) + |
| 405 |
wasmWorklist_tier1_.sizeOfExcludingThis(mallocSizeOf) + |
| 406 |
wasmWorklist_tier2_.sizeOfExcludingThis(mallocSizeOf) + |
| 407 |
wasmCompleteTier2GeneratorWorklist_.sizeOfExcludingThis(mallocSizeOf) + |
| 408 |
wasmPartialTier2CompileWorklist_.sizeOfExcludingThis(mallocSizeOf) + |
| 409 |
promiseHelperTasks_.sizeOfExcludingThis(mallocSizeOf) + |
| 410 |
compressionPendingList_.sizeOfExcludingThis(mallocSizeOf) + |
| 411 |
compressionWorklist_.sizeOfExcludingThis(mallocSizeOf) + |
| 412 |
compressionFinishedList_.sizeOfExcludingThis(mallocSizeOf) + |
| 413 |
gcParallelWorklist_.sizeOfExcludingThis(mallocSizeOf, lock) + |
| 414 |
helperTasks_.sizeOfExcludingThis(mallocSizeOf); |
| 415 |
|
| 416 |
// Report IonCompileTasks on wait lists |
| 417 |
for (auto task : ionWorklist_) { |
| 418 |
htStats.ionCompileTask += task->sizeOfExcludingThis(mallocSizeOf); |
| 419 |
} |
| 420 |
for (auto task : ionFinishedList_) { |
| 421 |
htStats.ionCompileTask += task->sizeOfExcludingThis(mallocSizeOf); |
| 422 |
} |
| 423 |
for (const auto& task : ionFreeList_) { |
| 424 |
for (auto* compileTask : task->compileTasks()) { |
| 425 |
htStats.ionCompileTask += compileTask->sizeOfExcludingThis(mallocSizeOf); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// Report wasm::CompileTasks on wait lists |
| 430 |
for (auto task : wasmWorklist_tier1_) { |
| 431 |
htStats.wasmCompile += task->sizeOfExcludingThis(mallocSizeOf); |
| 432 |
} |
| 433 |
for (auto task : wasmWorklist_tier2_) { |
| 434 |
htStats.wasmCompile += task->sizeOfExcludingThis(mallocSizeOf); |
| 435 |
} |
| 436 |
|
| 437 |
// Report number of helper threads. |
| 438 |
MOZ_ASSERT(htStats.idleThreadCount == 0); |
| 439 |
MOZ_ASSERT(threadCount >= totalCountRunningTasks); |
| 440 |
htStats.activeThreadCount = totalCountRunningTasks; |
| 441 |
htStats.idleThreadCount = threadCount - totalCountRunningTasks; |
| 442 |
} |
| 443 |
|
| 444 |
size_t GlobalHelperThreadState::maxBaselineCompilationThreads() const { |
| 445 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_BASELINE)) { |
| 446 |
return 1; |
| 447 |
} |
| 448 |
return threadCount; |
| 449 |
} |
| 450 |
|
| 451 |
size_t GlobalHelperThreadState::maxIonCompilationThreads() const { |
| 452 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_ION)) { |
| 453 |
return 1; |
| 454 |
} |
| 455 |
return threadCount; |
| 456 |
} |
| 457 |
|
| 458 |
size_t GlobalHelperThreadState::maxIonFreeThreads() const { |
| 459 |
// IonFree tasks are low priority. Limit to one thread to help avoid jemalloc |
| 460 |
// lock contention. |
| 461 |
return 1; |
| 462 |
} |
| 463 |
|
| 464 |
size_t GlobalHelperThreadState::maxPromiseHelperThreads() const { |
| 465 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_PROMISE_TASK)) { |
| 466 |
return 1; |
| 467 |
} |
| 468 |
return std::min(cpuCount, threadCount); |
| 469 |
} |
| 470 |
|
| 471 |
size_t GlobalHelperThreadState::maxDelazifyThreads() const { |
| 472 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_DELAZIFY)) { |
| 473 |
return 1; |
| 474 |
} |
| 475 |
return std::min(cpuCount, threadCount); |
| 476 |
} |
| 477 |
|
| 478 |
size_t GlobalHelperThreadState::maxCompressionThreads() const { |
| 479 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_COMPRESS)) { |
| 480 |
return 1; |
| 481 |
} |
| 482 |
|
| 483 |
// Compression is triggered on major GCs to compress ScriptSources. It is |
| 484 |
// considered low priority work. |
| 485 |
return 1; |
| 486 |
} |
| 487 |
|
| 488 |
size_t GlobalHelperThreadState::maxGCParallelThreads() const { |
| 489 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_GCPARALLEL)) { |
| 490 |
return 1; |
| 491 |
} |
| 492 |
return threadCount; |
| 493 |
} |
| 494 |
|
| 495 |
size_t GlobalHelperThreadState::maxWasmCompilationThreads() const { |
| 496 |
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_WASM_COMPILE_TIER1) || |
| 497 |
IsHelperThreadSimulatingOOM(js::THREAD_TYPE_WASM_COMPILE_TIER2)) { |
| 498 |
return 1; |
| 499 |
} |
| 500 |
return std::min(cpuCount, threadCount); |
| 501 |
} |
| 502 |
|
| 503 |
size_t js::GetMaxWasmCompilationThreads() { |
| 504 |
return HelperThreadState().maxWasmCompilationThreads(); |
| 505 |
} |
| 506 |
|
| 507 |
size_t GlobalHelperThreadState::maxWasmCompleteTier2GeneratorThreads() const { |
| 508 |
return MaxCompleteTier2GeneratorTasks; |
| 509 |
} |
| 510 |
|
| 511 |
size_t GlobalHelperThreadState::maxWasmPartialTier2CompileThreads() const { |
| 512 |
return MaxPartialTier2CompileTasks; |
| 513 |
} |
| 514 |
|
| 515 |
void GlobalHelperThreadState::trace(JSTracer* trc) { |
| 516 |
{ |
| 517 |
AutoLockHelperThreadState lock; |
| 518 |
|
| 519 |
#ifdef DEBUG |
| 520 |
// Since we hold the helper thread lock here we must disable GCMarker's |
| 521 |
// checking of the atom marking bitmap since that also relies on taking the |
| 522 |
// lock. |
| 523 |
GCMarker* marker = nullptr; |
| 524 |
if (trc->isMarkingTracer()) { |
| 525 |
marker = GCMarker::fromTracer(trc); |
| 526 |
marker->setCheckAtomMarking(false); |
| 527 |
} |
| 528 |
auto reenableAtomMarkingCheck = mozilla::MakeScopeExit([marker] { |
| 529 |
if (marker) { |
| 530 |
marker->setCheckAtomMarking(true); |
| 531 |
} |
| 532 |
}); |
| 533 |
#endif |
| 534 |
|
| 535 |
for (auto task : baselineWorklist(lock)) { |
| 536 |
task->trace(trc); |
| 537 |
} |
| 538 |
for (auto task : baselineFinishedList(lock)) { |
| 539 |
task->trace(trc); |
| 540 |
} |
| 541 |
|
| 542 |
for (auto task : ionWorklist(lock)) { |
| 543 |
task->alloc().lifoAlloc()->setReadWrite(); |
| 544 |
task->trace(trc); |
| 545 |
task->alloc().lifoAlloc()->setReadOnly(); |
| 546 |
} |
| 547 |
for (auto task : ionFinishedList(lock)) { |
| 548 |
task->trace(trc); |
| 549 |
} |
| 550 |
|
| 551 |
for (auto* helper : helperTasks(lock)) { |
| 552 |
if (helper->is<jit::IonCompileTask>()) { |
| 553 |
jit::IonCompileTask* ionCompileTask = helper->as<jit::IonCompileTask>(); |
| 554 |
ionCompileTask->alloc().lifoAlloc()->setReadWrite(); |
| 555 |
ionCompileTask->trace(trc); |
| 556 |
} else if (helper->is<jit::BaselineCompileTask>()) { |
| 557 |
helper->as<jit::BaselineCompileTask>()->trace(trc); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
// The lazy link list is only accessed on the main thread, so trace it after |
| 563 |
// releasing the lock. |
| 564 |
JSRuntime* rt = trc->runtime(); |
| 565 |
if (auto* jitRuntime = rt->jitRuntime()) { |
| 566 |
jit::IonCompileTask* task = jitRuntime->ionLazyLinkList(rt).getFirst(); |
| 567 |
while (task) { |
| 568 |
task->trace(trc); |
| 569 |
task = task->getNext(); |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// Definition of helper thread tasks. |
| 575 |
// |
| 576 |
// Priority is determined by the order they're listed here. |
| 577 |
const GlobalHelperThreadState::Selector GlobalHelperThreadState::selectors[] = { |
| 578 |
&GlobalHelperThreadState::maybeGetGCParallelTask, |
| 579 |
&GlobalHelperThreadState::maybeGetBaselineCompileTask, |
| 580 |
&GlobalHelperThreadState::maybeGetIonCompileTask, |
| 581 |
&GlobalHelperThreadState::maybeGetWasmTier1CompileTask, |
| 582 |
&GlobalHelperThreadState::maybeGetPromiseHelperTask, |
| 583 |
&GlobalHelperThreadState::maybeGetFreeDelazifyTask, |
| 584 |
&GlobalHelperThreadState::maybeGetDelazifyTask, |
| 585 |
&GlobalHelperThreadState::maybeGetCompressionTask, |
| 586 |
&GlobalHelperThreadState::maybeGetLowPrioIonCompileTask, |
| 587 |
&GlobalHelperThreadState::maybeGetIonFreeTask, |
| 588 |
&GlobalHelperThreadState::maybeGetWasmPartialTier2CompileTask, |
| 589 |
&GlobalHelperThreadState::maybeGetWasmTier2CompileTask, |
| 590 |
&GlobalHelperThreadState::maybeGetWasmCompleteTier2GeneratorTask}; |
| 591 |
|
| 592 |
bool GlobalHelperThreadState::canStartTasks( |
| 593 |
const AutoLockHelperThreadState& lock) { |
| 594 |
return canStartGCParallelTask(lock) || canStartBaselineCompileTask(lock) || |
| 595 |
canStartIonCompileTask(lock) || canStartWasmTier1CompileTask(lock) || |
| 596 |
canStartPromiseHelperTask(lock) || canStartFreeDelazifyTask(lock) || |
| 597 |
canStartDelazifyTask(lock) || canStartCompressionTask(lock) || |
| 598 |
canStartIonFreeTask(lock) || canStartWasmTier2CompileTask(lock) || |
| 599 |
canStartWasmCompleteTier2GeneratorTask(lock) || |
| 600 |
canStartWasmPartialTier2CompileTask(lock); |
| 601 |
} |
| 602 |
|
| 603 |
void JS::RunHelperThreadTask(HelperThreadTask* task) { |
| 604 |
MOZ_ASSERT(task); |
| 605 |
MOZ_ASSERT(CanUseExtraThreads()); |
| 606 |
|
| 607 |
AutoLockHelperThreadState lock; |
| 608 |
|
| 609 |
if (!gHelperThreadState || HelperThreadState().isTerminating(lock)) { |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
HelperThreadState().runOneTask(task, lock); |
| 614 |
HelperThreadState().dispatch(lock); |
| 615 |
} |
| 616 |
|
| 617 |
void GlobalHelperThreadState::runOneTask(HelperThreadTask* task, |
| 618 |
AutoLockHelperThreadState& lock) { |
| 619 |
#ifdef DEBUG |
| 620 |
MOZ_ASSERT(tasksPending_ > 0); |
| 621 |
tasksPending_--; |
| 622 |
#endif |
| 623 |
|
| 624 |
runTaskLocked(task, lock); |
| 625 |
|
| 626 |
notifyAll(lock); |
| 627 |
} |
| 628 |
|
| 629 |
HelperThreadTask* GlobalHelperThreadState::findHighestPriorityTask( |
| 630 |
const AutoLockHelperThreadState& locked) { |
| 631 |
// Return the highest priority task that is ready to start, or nullptr. |
| 632 |
|
| 633 |
for (const auto& selector : selectors) { |
| 634 |
if (auto* task = (this->*(selector))(locked)) { |
| 635 |
return task; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
return nullptr; |
| 640 |
} |
| 641 |
|
| 642 |
#ifdef DEBUG |
| 643 |
static bool VectorHasTask( |
| 644 |
const Vector<HelperThreadTask*, 0, SystemAllocPolicy>& tasks, |
| 645 |
HelperThreadTask* task) { |
| 646 |
for (HelperThreadTask* t : tasks) { |
| 647 |
if (t == task) { |
| 648 |
return true; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
return false; |
| 653 |
} |
| 654 |
#endif |
| 655 |
|
| 656 |
void GlobalHelperThreadState::runTaskLocked(HelperThreadTask* task, |
| 657 |
AutoLockHelperThreadState& locked) { |
| 658 |
ThreadType threadType = task->threadType(); |
| 659 |
|
| 660 |
MOZ_ASSERT(VectorHasTask(helperTasks(locked), task)); |
| 661 |
MOZ_ASSERT(totalCountRunningTasks != 0); |
| 662 |
MOZ_ASSERT(runningTaskCount[threadType] != 0); |
| 663 |
|
| 664 |
js::oom::SetThreadType(threadType); |
| 665 |
|
| 666 |
{ |
| 667 |
JS::AutoSuppressGCAnalysis nogc; |
| 668 |
task->runHelperThreadTask(locked); |
| 669 |
} |
| 670 |
|
| 671 |
js::oom::SetThreadType(js::THREAD_TYPE_NONE); |
| 672 |
|
| 673 |
helperTasks(locked).eraseIfEqual(task); |
| 674 |
totalCountRunningTasks--; |
| 675 |
runningTaskCount[threadType]--; |
| 676 |
} |
| 677 |
|
| 678 |
void AutoHelperTaskQueue::queueTaskToDispatch( |
| 679 |
JS::HelperThreadTask* task) const { |
| 680 |
// This is marked const because it doesn't release the mutex. |
| 681 |
|
| 682 |
task->onThreadPoolDispatch(); |
| 683 |
|
| 684 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 685 |
if (!tasksToDispatch.append(task)) { |
| 686 |
oomUnsafe.crash("AutoLockHelperThreadState::queueTaskToDispatch"); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
void AutoHelperTaskQueue::dispatchQueuedTasks() { |
| 691 |
// The hazard analysis can't tell that the callback doesn't GC. |
| 692 |
JS::AutoSuppressGCAnalysis nogc; |
| 693 |
|
| 694 |
for (size_t i = 0; i < tasksToDispatch.length(); i++) { |
| 695 |
HelperThreadState().dispatchTaskCallback(tasksToDispatch[i]); |
| 696 |
} |
| 697 |
tasksToDispatch.clear(); |
| 698 |
} |
| 699 |
|
| 700 |
/////////////////////////////////////////////////////////////////////////// |
| 701 |
// // |
| 702 |
// JS task definitions // |
| 703 |
// // |
| 704 |
/////////////////////////////////////////////////////////////////////////// |
| 705 |
|
| 706 |
//== IonCompileTask and CompilationSelector =============================== |
| 707 |
|
| 708 |
bool GlobalHelperThreadState::canStartIonCompileTask( |
| 709 |
const AutoLockHelperThreadState& lock) { |
| 710 |
return !ionWorklist(lock).empty() && |
| 711 |
checkTaskThreadLimit(THREAD_TYPE_ION, maxIonCompilationThreads(), |
| 712 |
lock); |
| 713 |
} |
| 714 |
|
| 715 |
static bool IonCompileTaskHasHigherPriority(jit::IonCompileTask* first, |
| 716 |
jit::IonCompileTask* second) { |
| 717 |
// Return true if priority(first) > priority(second). |
| 718 |
// |
| 719 |
// This method can return whatever it wants, though it really ought to be a |
| 720 |
// total order. The ordering is allowed to race (change on the fly), however. |
| 721 |
|
| 722 |
// A higher warm-up counter indicates a higher priority. |
| 723 |
jit::JitScript* firstJitScript = first->script()->jitScript(); |
| 724 |
jit::JitScript* secondJitScript = second->script()->jitScript(); |
| 725 |
return firstJitScript->warmUpCount() / first->script()->length() > |
| 726 |
secondJitScript->warmUpCount() / second->script()->length(); |
| 727 |
} |
| 728 |
|
| 729 |
jit::IonCompileTask* GlobalHelperThreadState::highestPriorityPendingIonCompile( |
| 730 |
const AutoLockHelperThreadState& lock, bool checkExecutionStatus) { |
| 731 |
auto& worklist = ionWorklist(lock); |
| 732 |
MOZ_ASSERT(!worklist.empty()); |
| 733 |
|
| 734 |
// Get the highest priority IonCompileTask which has not started compilation |
| 735 |
// yet. |
| 736 |
size_t index = worklist.length(); |
| 737 |
for (size_t i = 0; i < worklist.length(); i++) { |
| 738 |
if (checkExecutionStatus && !worklist[i]->isMainThreadRunningJS()) { |
| 739 |
continue; |
| 740 |
} |
| 741 |
if (i < index || |
| 742 |
IonCompileTaskHasHigherPriority(worklist[i], worklist[index])) { |
| 743 |
index = i; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
if (index == worklist.length()) { |
| 748 |
return nullptr; |
| 749 |
} |
| 750 |
jit::IonCompileTask* task = worklist[index]; |
| 751 |
worklist.erase(&worklist[index]); |
| 752 |
return task; |
| 753 |
} |
| 754 |
|
| 755 |
HelperThreadTask* GlobalHelperThreadState::maybeGetIonCompileTask( |
| 756 |
const AutoLockHelperThreadState& lock) { |
| 757 |
if (!canStartIonCompileTask(lock)) { |
| 758 |
return nullptr; |
| 759 |
} |
| 760 |
|
| 761 |
return highestPriorityPendingIonCompile(lock, |
| 762 |
/* checkExecutionStatus */ true); |
| 763 |
} |
| 764 |
|
| 765 |
HelperThreadTask* GlobalHelperThreadState::maybeGetLowPrioIonCompileTask( |
| 766 |
const AutoLockHelperThreadState& lock) { |
| 767 |
if (!canStartIonCompileTask(lock)) { |
| 768 |
return nullptr; |
| 769 |
} |
| 770 |
|
| 771 |
return highestPriorityPendingIonCompile(lock, |
| 772 |
/* checkExecutionStatus */ false); |
| 773 |
} |
| 774 |
|
| 775 |
bool GlobalHelperThreadState::submitTask( |
| 776 |
jit::IonCompileTask* task, const AutoLockHelperThreadState& locked) { |
| 777 |
MOZ_ASSERT(isInitialized(locked)); |
| 778 |
|
| 779 |
if (!ionWorklist(locked).append(task)) { |
| 780 |
return false; |
| 781 |
} |
| 782 |
|
| 783 |
// The build is moving off-thread. Freeze the LifoAlloc to prevent any |
| 784 |
// unwanted mutations. |
| 785 |
task->alloc().lifoAlloc()->setReadOnly(); |
| 786 |
|
| 787 |
dispatch(locked); |
| 788 |
return true; |
| 789 |
} |
| 790 |
|
| 791 |
bool js::StartOffThreadIonCompile(jit::IonCompileTask* task, |
| 792 |
const AutoLockHelperThreadState& lock) { |
| 793 |
return HelperThreadState().submitTask(task, lock); |
| 794 |
} |
| 795 |
|
| 796 |
/* |
| 797 |
* Move an IonCompilationTask for which compilation has either finished, failed, |
| 798 |
* or been cancelled into the global finished compilation list. All off thread |
| 799 |
* compilations which are started must eventually be finished. |
| 800 |
*/ |
| 801 |
void js::FinishOffThreadIonCompile(jit::IonCompileTask* task, |
| 802 |
const AutoLockHelperThreadState& lock) { |
| 803 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 804 |
if (!HelperThreadState().ionFinishedList(lock).append(task)) { |
| 805 |
oomUnsafe.crash("FinishOffThreadIonCompile"); |
| 806 |
} |
| 807 |
task->script() |
| 808 |
->runtimeFromAnyThread() |
| 809 |
->jitRuntime() |
| 810 |
->numFinishedOffThreadTasksRef(lock)++; |
| 811 |
} |
| 812 |
|
| 813 |
static JSRuntime* GetSelectorRuntime(const CompilationSelector& selector) { |
| 814 |
struct Matcher { |
| 815 |
JSRuntime* operator()(JSScript* script) { |
| 816 |
return script->runtimeFromMainThread(); |
| 817 |
} |
| 818 |
JSRuntime* operator()(Zone* zone) { return zone->runtimeFromMainThread(); } |
| 819 |
JSRuntime* operator()(ZonesInState zbs) { return zbs.runtime; } |
| 820 |
JSRuntime* operator()(JSRuntime* runtime) { return runtime; } |
| 821 |
}; |
| 822 |
|
| 823 |
return selector.match(Matcher()); |
| 824 |
} |
| 825 |
|
| 826 |
static bool IonCompileTaskMatches(const CompilationSelector& selector, |
| 827 |
jit::IonCompileTask* task) { |
| 828 |
struct TaskMatches { |
| 829 |
jit::IonCompileTask* task_; |
| 830 |
|
| 831 |
bool operator()(JSScript* script) { return script == task_->script(); } |
| 832 |
bool operator()(Zone* zone) { |
| 833 |
return zone == task_->script()->zoneFromAnyThread(); |
| 834 |
} |
| 835 |
bool operator()(JSRuntime* runtime) { |
| 836 |
return runtime == task_->script()->runtimeFromAnyThread(); |
| 837 |
} |
| 838 |
bool operator()(ZonesInState zbs) { |
| 839 |
return zbs.runtime == task_->script()->runtimeFromAnyThread() && |
| 840 |
zbs.state == task_->script()->zoneFromAnyThread()->gcState(); |
| 841 |
} |
| 842 |
}; |
| 843 |
|
| 844 |
return selector.match(TaskMatches{task}); |
| 845 |
} |
| 846 |
|
| 847 |
// If we're canceling Ion compilations for a zone/runtime, force a new |
| 848 |
// IonFreeTask even if there are just a few tasks. This lets us free as much |
| 849 |
// memory as possible. |
| 850 |
static bool ShouldForceIonFreeTask(const CompilationSelector& selector) { |
| 851 |
struct Matcher { |
| 852 |
bool operator()(JSScript* script) { return false; } |
| 853 |
bool operator()(Zone* zone) { return true; } |
| 854 |
bool operator()(ZonesInState zbs) { return true; } |
| 855 |
bool operator()(JSRuntime* runtime) { return true; } |
| 856 |
}; |
| 857 |
|
| 858 |
return selector.match(Matcher()); |
| 859 |
} |
| 860 |
|
| 861 |
void GlobalHelperThreadState::cancelOffThreadIonCompile( |
| 862 |
const CompilationSelector& selector) { |
| 863 |
jit::JitRuntime* jitRuntime = GetSelectorRuntime(selector)->jitRuntime(); |
| 864 |
MOZ_ASSERT(jitRuntime); |
| 865 |
|
| 866 |
AutoStartIonFreeTask freeTask(jitRuntime, ShouldForceIonFreeTask(selector)); |
| 867 |
|
| 868 |
{ |
| 869 |
AutoLockHelperThreadState lock; |
| 870 |
if (!isInitialized(lock)) { |
| 871 |
return; |
| 872 |
} |
| 873 |
|
| 874 |
/* Cancel any pending entries for which processing hasn't started. */ |
| 875 |
GlobalHelperThreadState::IonCompileTaskVector& worklist = ionWorklist(lock); |
| 876 |
for (size_t i = 0; i < worklist.length(); i++) { |
| 877 |
jit::IonCompileTask* task = worklist[i]; |
| 878 |
if (IonCompileTaskMatches(selector, task)) { |
| 879 |
// Once finished, tasks are added to a Linked list which is |
| 880 |
// allocated with the IonCompileTask class. The IonCompileTask is |
| 881 |
// allocated in the LifoAlloc so we need the LifoAlloc to be mutable. |
| 882 |
worklist[i]->alloc().lifoAlloc()->setReadWrite(); |
| 883 |
|
| 884 |
FinishOffThreadIonCompile(task, lock); |
| 885 |
remove(worklist, &i); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
/* Wait for in progress entries to finish up. */ |
| 890 |
bool cancelled; |
| 891 |
do { |
| 892 |
cancelled = false; |
| 893 |
for (auto* helper : helperTasks(lock)) { |
| 894 |
if (!helper->is<jit::IonCompileTask>()) { |
| 895 |
continue; |
| 896 |
} |
| 897 |
|
| 898 |
jit::IonCompileTask* ionCompileTask = helper->as<jit::IonCompileTask>(); |
| 899 |
if (IonCompileTaskMatches(selector, ionCompileTask)) { |
| 900 |
ionCompileTask->alloc().lifoAlloc()->setReadWrite(); |
| 901 |
ionCompileTask->mirGen().cancel(); |
| 902 |
cancelled = true; |
| 903 |
} |
| 904 |
} |
| 905 |
if (cancelled) { |
| 906 |
wait(lock); |
| 907 |
} |
| 908 |
} while (cancelled); |
| 909 |
|
| 910 |
/* Cancel code generation for any completed entries. */ |
| 911 |
GlobalHelperThreadState::IonCompileTaskVector& finished = |
| 912 |
ionFinishedList(lock); |
| 913 |
for (size_t i = 0; i < finished.length(); i++) { |
| 914 |
jit::IonCompileTask* task = finished[i]; |
| 915 |
if (IonCompileTaskMatches(selector, task)) { |
| 916 |
JSRuntime* rt = task->script()->runtimeFromAnyThread(); |
| 917 |
jitRuntime->numFinishedOffThreadTasksRef(lock)--; |
| 918 |
jit::FinishOffThreadTask(rt, freeTask, task); |
| 919 |
remove(finished, &i); |
| 920 |
} |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
/* Cancel lazy linking for pending tasks (attached to the ionScript). */ |
| 925 |
JSRuntime* runtime = GetSelectorRuntime(selector); |
| 926 |
jit::IonCompileTask* task = jitRuntime->ionLazyLinkList(runtime).getFirst(); |
| 927 |
while (task) { |
| 928 |
jit::IonCompileTask* next = task->getNext(); |
| 929 |
if (IonCompileTaskMatches(selector, task)) { |
| 930 |
jit::FinishOffThreadTask(runtime, freeTask, task); |
| 931 |
} |
| 932 |
task = next; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
static bool JitDataStructuresExist(const CompilationSelector& selector) { |
| 937 |
struct Matcher { |
| 938 |
bool operator()(JSScript* script) { return !!script->zone()->jitZone(); } |
| 939 |
bool operator()(Zone* zone) { return !!zone->jitZone(); } |
| 940 |
bool operator()(ZonesInState zbs) { return zbs.runtime->hasJitRuntime(); } |
| 941 |
bool operator()(JSRuntime* runtime) { return runtime->hasJitRuntime(); } |
| 942 |
}; |
| 943 |
|
| 944 |
return selector.match(Matcher()); |
| 945 |
} |
| 946 |
|
| 947 |
void js::CancelOffThreadIonCompile(const CompilationSelector& selector) { |
| 948 |
if (!JitDataStructuresExist(selector)) { |
| 949 |
return; |
| 950 |
} |
| 951 |
|
| 952 |
if (jit::IsPortableBaselineInterpreterEnabled()) { |
| 953 |
return; |
| 954 |
} |
| 955 |
|
| 956 |
HelperThreadState().cancelOffThreadIonCompile(selector); |
| 957 |
} |
| 958 |
|
| 959 |
#ifdef DEBUG |
| 960 |
bool GlobalHelperThreadState::hasOffThreadIonCompile( |
| 961 |
Zone* zone, AutoLockHelperThreadState& lock) { |
| 962 |
for (jit::IonCompileTask* task : ionWorklist(lock)) { |
| 963 |
if (task->script()->zoneFromAnyThread() == zone) { |
| 964 |
return true; |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
for (auto* helper : helperTasks(lock)) { |
| 969 |
if (helper->is<jit::IonCompileTask>()) { |
| 970 |
JSScript* script = helper->as<jit::IonCompileTask>()->script(); |
| 971 |
if (script->zoneFromAnyThread() == zone) { |
| 972 |
return true; |
| 973 |
} |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
for (jit::IonCompileTask* task : ionFinishedList(lock)) { |
| 978 |
if (task->script()->zoneFromAnyThread() == zone) { |
| 979 |
return true; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
JSRuntime* rt = zone->runtimeFromMainThread(); |
| 984 |
if (rt->hasJitRuntime()) { |
| 985 |
for (jit::IonCompileTask* task : rt->jitRuntime()->ionLazyLinkList(rt)) { |
| 986 |
if (task->script()->zone() == zone) { |
| 987 |
return true; |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
return false; |
| 993 |
} |
| 994 |
|
| 995 |
bool js::HasOffThreadIonCompile(Zone* zone) { |
| 996 |
if (jit::IsPortableBaselineInterpreterEnabled()) { |
| 997 |
return false; |
| 998 |
} |
| 999 |
|
| 1000 |
AutoLockHelperThreadState lock; |
| 1001 |
|
| 1002 |
if (!HelperThreadState().isInitialized(lock)) { |
| 1003 |
return false; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return HelperThreadState().hasOffThreadIonCompile(zone, lock); |
| 1007 |
} |
| 1008 |
#endif |
| 1009 |
|
| 1010 |
//== IonFreeTask ========================================================== |
| 1011 |
|
| 1012 |
bool GlobalHelperThreadState::canStartIonFreeTask( |
| 1013 |
const AutoLockHelperThreadState& lock) { |
| 1014 |
return !ionFreeList(lock).empty() && |
| 1015 |
checkTaskThreadLimit(THREAD_TYPE_ION_FREE, maxIonFreeThreads(), lock); |
| 1016 |
} |
| 1017 |
|
| 1018 |
HelperThreadTask* GlobalHelperThreadState::maybeGetIonFreeTask( |
| 1019 |
const AutoLockHelperThreadState& lock) { |
| 1020 |
if (!canStartIonFreeTask(lock)) { |
| 1021 |
return nullptr; |
| 1022 |
} |
| 1023 |
|
| 1024 |
UniquePtr<jit::IonFreeTask> task = std::move(ionFreeList(lock).back()); |
| 1025 |
ionFreeList(lock).popBack(); |
| 1026 |
return task.release(); |
| 1027 |
} |
| 1028 |
|
| 1029 |
void jit::JitRuntime::maybeStartIonFreeTask(bool force) { |
| 1030 |
IonFreeCompileTasks& tasks = ionFreeTaskBatch_.ref(); |
| 1031 |
if (tasks.empty()) { |
| 1032 |
return; |
| 1033 |
} |
| 1034 |
|
| 1035 |
// Start an IonFreeTask if we have at least eight tasks. If |force| is true we |
| 1036 |
// always start an IonFreeTask. |
| 1037 |
if (!force) { |
| 1038 |
constexpr size_t MinBatchSize = 8; |
| 1039 |
static_assert(IonFreeCompileTasks::InlineLength >= MinBatchSize, |
| 1040 |
"Minimum batch size shouldn't require malloc"); |
| 1041 |
if (tasks.length() < MinBatchSize) { |
| 1042 |
return; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
auto freeTask = js::MakeUnique<jit::IonFreeTask>(std::move(tasks)); |
| 1047 |
if (!freeTask) { |
| 1048 |
// Free compilation data on the main thread instead. |
| 1049 |
MOZ_ASSERT(!tasks.empty(), "shouldn't have moved tasks on OOM"); |
| 1050 |
jit::FreeIonCompileTasks(tasks); |
| 1051 |
tasks.clearAndFree(); |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
|
| 1055 |
AutoLockHelperThreadState lock; |
| 1056 |
if (!HelperThreadState().submitTask(std::move(freeTask), lock)) { |
| 1057 |
// If submitTask OOMs, then freeTask hasn't been moved so we can still use |
| 1058 |
// its task list. |
| 1059 |
jit::FreeIonCompileTasks(freeTask->compileTasks()); |
| 1060 |
} |
| 1061 |
|
| 1062 |
tasks.clearAndFree(); |
| 1063 |
} |
| 1064 |
|
| 1065 |
bool GlobalHelperThreadState::submitTask( |
| 1066 |
UniquePtr<jit::IonFreeTask>&& task, |
| 1067 |
const AutoLockHelperThreadState& locked) { |
| 1068 |
MOZ_ASSERT(isInitialized(locked)); |
| 1069 |
|
| 1070 |
if (!ionFreeList(locked).append(std::move(task))) { |
| 1071 |
return false; |
| 1072 |
} |
| 1073 |
|
| 1074 |
dispatch(locked); |
| 1075 |
return true; |
| 1076 |
} |
| 1077 |
|
| 1078 |
bool js::AutoStartIonFreeTask::addIonCompileToFreeTaskBatch( |
| 1079 |
jit::IonCompileTask* task) { |
| 1080 |
return jitRuntime_->addIonCompileToFreeTaskBatch(task); |
| 1081 |
} |
| 1082 |
|
| 1083 |
js::AutoStartIonFreeTask::~AutoStartIonFreeTask() { |
| 1084 |
jitRuntime_->maybeStartIonFreeTask(force_); |
| 1085 |
} |
| 1086 |
|
| 1087 |
//== BaselineCompileTask ================================================== |
| 1088 |
|
| 1089 |
bool GlobalHelperThreadState::canStartBaselineCompileTask( |
| 1090 |
const AutoLockHelperThreadState& lock) { |
| 1091 |
return !baselineWorklist(lock).empty() && |
| 1092 |
checkTaskThreadLimit(THREAD_TYPE_BASELINE, |
| 1093 |
maxBaselineCompilationThreads(), lock); |
| 1094 |
} |
| 1095 |
|
| 1096 |
HelperThreadTask* GlobalHelperThreadState::maybeGetBaselineCompileTask( |
| 1097 |
const AutoLockHelperThreadState& lock) { |
| 1098 |
if (!canStartBaselineCompileTask(lock)) { |
| 1099 |
return nullptr; |
| 1100 |
} |
| 1101 |
|
| 1102 |
return baselineWorklist(lock).popCopy(); |
| 1103 |
} |
| 1104 |
|
| 1105 |
bool GlobalHelperThreadState::submitTask( |
| 1106 |
jit::BaselineCompileTask* task, const AutoLockHelperThreadState& locked) { |
| 1107 |
MOZ_ASSERT(isInitialized(locked)); |
| 1108 |
|
| 1109 |
if (!baselineWorklist(locked).append(task)) { |
| 1110 |
return false; |
| 1111 |
} |
| 1112 |
|
| 1113 |
dispatch(locked); |
| 1114 |
return true; |
| 1115 |
} |
| 1116 |
|
| 1117 |
bool js::StartOffThreadBaselineCompile(jit::BaselineCompileTask* task, |
| 1118 |
const AutoLockHelperThreadState& lock) { |
| 1119 |
return HelperThreadState().submitTask(task, lock); |
| 1120 |
} |
| 1121 |
|
| 1122 |
/* |
| 1123 |
* Move a BaselineCompileTask for which compilation has either finished, failed, |
| 1124 |
* or been cancelled into the global finished compilation list. All off thread |
| 1125 |
* compilations which are started must eventually be finished. |
| 1126 |
*/ |
| 1127 |
void js::FinishOffThreadBaselineCompile(jit::BaselineCompileTask* task, |
| 1128 |
const AutoLockHelperThreadState& lock) { |
| 1129 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 1130 |
if (!HelperThreadState().baselineFinishedList(lock).append(task)) { |
| 1131 |
oomUnsafe.crash("FinishOffThreadBaselineCompile"); |
| 1132 |
} |
| 1133 |
task->runtimeFromAnyThread()->jitRuntime()->numFinishedOffThreadTasksRef( |
| 1134 |
lock)++; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static bool BaselineCompileTaskMatches(const CompilationSelector& selector, |
| 1138 |
jit::BaselineCompileTask* task) { |
| 1139 |
struct TaskMatches { |
| 1140 |
jit::BaselineCompileTask* task_; |
| 1141 |
|
| 1142 |
bool operator()(JSScript* script) { return task_->scriptMatches(script); } |
| 1143 |
bool operator()(Zone* zone) { return zone == task_->zoneFromAnyThread(); } |
| 1144 |
bool operator()(JSRuntime* runtime) { |
| 1145 |
return runtime == task_->runtimeFromAnyThread(); |
| 1146 |
} |
| 1147 |
bool operator()(ZonesInState zbs) { |
| 1148 |
return zbs.runtime == task_->runtimeFromAnyThread() && |
| 1149 |
zbs.state == task_->zoneFromAnyThread()->gcState(); |
| 1150 |
} |
| 1151 |
}; |
| 1152 |
|
| 1153 |
return selector.match(TaskMatches{task}); |
| 1154 |
} |
| 1155 |
|
| 1156 |
void GlobalHelperThreadState::cancelOffThreadBaselineCompile( |
| 1157 |
const CompilationSelector& selector) { |
| 1158 |
jit::JitRuntime* jitRuntime = GetSelectorRuntime(selector)->jitRuntime(); |
| 1159 |
MOZ_ASSERT(jitRuntime); |
| 1160 |
|
| 1161 |
{ |
| 1162 |
AutoLockHelperThreadState lock; |
| 1163 |
if (!isInitialized(lock)) { |
| 1164 |
return; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/* Cancel any pending entries for which processing hasn't started. */ |
| 1168 |
GlobalHelperThreadState::BaselineCompileTaskVector& worklist = |
| 1169 |
baselineWorklist(lock); |
| 1170 |
for (size_t i = 0; i < worklist.length(); i++) { |
| 1171 |
jit::BaselineCompileTask* task = worklist[i]; |
| 1172 |
if (BaselineCompileTaskMatches(selector, task)) { |
| 1173 |
FinishOffThreadBaselineCompile(task, lock); |
| 1174 |
remove(worklist, &i); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* Wait for in progress entries to finish up. */ |
| 1179 |
while (true) { |
| 1180 |
bool inProgress = false; |
| 1181 |
for (auto* helper : helperTasks(lock)) { |
| 1182 |
if (!helper->is<jit::BaselineCompileTask>()) { |
| 1183 |
continue; |
| 1184 |
} |
| 1185 |
|
| 1186 |
jit::BaselineCompileTask* task = helper->as<jit::BaselineCompileTask>(); |
| 1187 |
if (BaselineCompileTaskMatches(selector, task)) { |
| 1188 |
inProgress = true; |
| 1189 |
break; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
if (!inProgress) { |
| 1193 |
break; |
| 1194 |
} |
| 1195 |
wait(lock); |
| 1196 |
} |
| 1197 |
|
| 1198 |
/* Cancel linking for any completed entries. */ |
| 1199 |
GlobalHelperThreadState::BaselineCompileTaskVector& finished = |
| 1200 |
baselineFinishedList(lock); |
| 1201 |
for (size_t i = 0; i < finished.length(); i++) { |
| 1202 |
jit::BaselineCompileTask* task = finished[i]; |
| 1203 |
if (BaselineCompileTaskMatches(selector, task)) { |
| 1204 |
jitRuntime->numFinishedOffThreadTasksRef(lock)--; |
| 1205 |
jit::BaselineCompileTask::FinishOffThreadTask(task); |
| 1206 |
remove(finished, &i); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
void js::CancelOffThreadBaselineCompile(const CompilationSelector& selector) { |
| 1213 |
if (!JitDataStructuresExist(selector)) { |
| 1214 |
return; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (jit::IsPortableBaselineInterpreterEnabled()) { |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
|
| 1221 |
HelperThreadState().cancelOffThreadBaselineCompile(selector); |
| 1222 |
} |
| 1223 |
|
| 1224 |
//== DelazifyTask ========================================================= |
| 1225 |
|
| 1226 |
bool GlobalHelperThreadState::canStartDelazifyTask( |
| 1227 |
const AutoLockHelperThreadState& lock) { |
| 1228 |
return !delazifyWorklist(lock).isEmpty() && |
| 1229 |
checkTaskThreadLimit(THREAD_TYPE_DELAZIFY, maxDelazifyThreads(), |
| 1230 |
/*isMaster=*/true, lock); |
| 1231 |
} |
| 1232 |
|
| 1233 |
HelperThreadTask* GlobalHelperThreadState::maybeGetDelazifyTask( |
| 1234 |
const AutoLockHelperThreadState& lock) { |
| 1235 |
// NOTE: We want to span all cores availables with delazification tasks, in |
| 1236 |
// order to parse a maximum number of functions ahead of their executions. |
| 1237 |
// Thus, as opposed to parse task which have a higher priority, we are not |
| 1238 |
// exclusively executing these task on parse threads. |
| 1239 |
auto& worklist = delazifyWorklist(lock); |
| 1240 |
if (worklist.isEmpty()) { |
| 1241 |
return nullptr; |
| 1242 |
} |
| 1243 |
return worklist.popFirst(); |
| 1244 |
} |
| 1245 |
|
| 1246 |
void GlobalHelperThreadState::submitTask( |
| 1247 |
DelazifyTask* task, const AutoLockHelperThreadState& locked) { |
| 1248 |
delazifyWorklist(locked).insertBack(task); |
| 1249 |
dispatch(locked); |
| 1250 |
} |
| 1251 |
|
| 1252 |
void js::StartOffThreadDelazification( |
| 1253 |
JSContext* maybeCx, const JS::ReadOnlyCompileOptions& options, |
| 1254 |
frontend::InitialStencilAndDelazifications* stencils) { |
| 1255 |
// Skip delazify tasks if we parse everything on-demand or ahead. |
| 1256 |
auto strategy = options.eagerDelazificationStrategy(); |
| 1257 |
if (strategy == JS::DelazificationOption::OnDemandOnly || |
| 1258 |
strategy == JS::DelazificationOption::ParseEverythingEagerly) { |
| 1259 |
return; |
| 1260 |
} |
| 1261 |
|
| 1262 |
// Skip delazify task if code coverage is enabled. |
| 1263 |
if (maybeCx && maybeCx->realm()->collectCoverageForDebug()) { |
| 1264 |
return; |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (!CanUseExtraThreads()) { |
| 1268 |
return; |
| 1269 |
} |
| 1270 |
|
| 1271 |
JSRuntime* maybeRuntime = maybeCx ? maybeCx->runtime() : nullptr; |
| 1272 |
UniquePtr<DelazifyTask> task; |
| 1273 |
task = DelazifyTask::Create(maybeRuntime, options, stencils); |
| 1274 |
if (!task) { |
| 1275 |
return; |
| 1276 |
} |
| 1277 |
|
| 1278 |
// Schedule delazification task if there is any function to delazify. |
| 1279 |
if (!task->done()) { |
| 1280 |
AutoLockHelperThreadState lock; |
| 1281 |
HelperThreadState().submitTask(task.release(), lock); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
UniquePtr<DelazifyTask> DelazifyTask::Create( |
| 1286 |
JSRuntime* maybeRuntime, const JS::ReadOnlyCompileOptions& options, |
| 1287 |
frontend::InitialStencilAndDelazifications* stencils) { |
| 1288 |
UniquePtr<DelazifyTask> task; |
| 1289 |
task.reset(js_new<DelazifyTask>(maybeRuntime, options.prefableOptions())); |
| 1290 |
if (!task) { |
| 1291 |
return nullptr; |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (!task->init(options, stencils)) { |
| 1295 |
// In case of errors, skip this and delazify on-demand. |
| 1296 |
return nullptr; |
| 1297 |
} |
| 1298 |
|
| 1299 |
return task; |
| 1300 |
} |
| 1301 |
|
| 1302 |
DelazifyTask::DelazifyTask( |
| 1303 |
JSRuntime* maybeRuntime, |
| 1304 |
const JS::PrefableCompileOptions& initialPrefableOptions) |
| 1305 |
: maybeRuntime(maybeRuntime), |
| 1306 |
delazificationCx(initialPrefableOptions, HelperThreadState().stackQuota) { |
| 1307 |
} |
| 1308 |
|
| 1309 |
DelazifyTask::~DelazifyTask() { |
| 1310 |
// The LinkedListElement destructor will remove us from any list we are part |
| 1311 |
// of without synchronization, so ensure that doesn't happen. |
| 1312 |
MOZ_DIAGNOSTIC_ASSERT(!isInList()); |
| 1313 |
} |
| 1314 |
|
| 1315 |
bool DelazifyTask::init(const JS::ReadOnlyCompileOptions& options, |
| 1316 |
frontend::InitialStencilAndDelazifications* stencils) { |
| 1317 |
return delazificationCx.init(options, stencils); |
| 1318 |
} |
| 1319 |
|
| 1320 |
size_t DelazifyTask::sizeOfExcludingThis( |
| 1321 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1322 |
return delazificationCx.sizeOfExcludingThis(mallocSizeOf); |
| 1323 |
} |
| 1324 |
|
| 1325 |
void DelazifyTask::runHelperThreadTask(AutoLockHelperThreadState& lock) { |
| 1326 |
{ |
| 1327 |
AutoUnlockHelperThreadState unlock(lock); |
| 1328 |
// NOTE: We do not report errors beyond this scope, as there is no where |
| 1329 |
// to report these errors to. In the mean time, prevent the eager |
| 1330 |
// delazification from running after any kind of errors. |
| 1331 |
(void)runTask(); |
| 1332 |
} |
| 1333 |
|
| 1334 |
// If we should continue to delazify even more functions, then re-add this |
| 1335 |
// task to the vector of delazification tasks. This might happen when the |
| 1336 |
// DelazifyTask is interrupted by a higher priority task. (see |
| 1337 |
// mozilla::TaskController & mozilla::Task) |
| 1338 |
if (!delazificationCx.done()) { |
| 1339 |
HelperThreadState().submitTask(this, lock); |
| 1340 |
} else { |
| 1341 |
UniquePtr<FreeDelazifyTask> freeTask(js_new<FreeDelazifyTask>(this)); |
| 1342 |
if (freeTask) { |
| 1343 |
HelperThreadState().submitTask(std::move(freeTask), lock); |
| 1344 |
} |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
bool DelazifyTask::runTask() { return delazificationCx.delazify(); } |
| 1349 |
|
| 1350 |
bool DelazifyTask::done() const { return delazificationCx.done(); } |
| 1351 |
|
| 1352 |
void GlobalHelperThreadState::cancelPendingDelazifyTask( |
| 1353 |
JSRuntime* rt, AutoLockHelperThreadState& lock) { |
| 1354 |
auto& delazifyList = delazifyWorklist(lock); |
| 1355 |
|
| 1356 |
auto end = delazifyList.end(); |
| 1357 |
for (auto iter = delazifyList.begin(); iter != end;) { |
| 1358 |
DelazifyTask* task = *iter; |
| 1359 |
++iter; |
| 1360 |
if (task->runtimeMatchesOrNoRuntime(rt)) { |
| 1361 |
task->removeFrom(delazifyList); |
| 1362 |
js_delete(task); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
void GlobalHelperThreadState::waitUntilCancelledDelazifyTasks( |
| 1368 |
JSRuntime* rt, AutoLockHelperThreadState& lock) { |
| 1369 |
while (true) { |
| 1370 |
cancelPendingDelazifyTask(rt, lock); |
| 1371 |
|
| 1372 |
// If running tasks are delazifying any functions, then we have to wait |
| 1373 |
// until they complete to remove them from the pending list. DelazifyTask |
| 1374 |
// are inserting themself back to be processed once more after delazifying a |
| 1375 |
// function. |
| 1376 |
bool inProgress = false; |
| 1377 |
for (auto* helper : helperTasks(lock)) { |
| 1378 |
if (helper->is<DelazifyTask>() && |
| 1379 |
helper->as<DelazifyTask>()->runtimeMatchesOrNoRuntime(rt)) { |
| 1380 |
inProgress = true; |
| 1381 |
break; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
if (!inProgress) { |
| 1385 |
break; |
| 1386 |
} |
| 1387 |
|
| 1388 |
wait(lock); |
| 1389 |
} |
| 1390 |
|
| 1391 |
MOZ_ASSERT(!hasAnyDelazifyTask(rt, lock)); |
| 1392 |
} |
| 1393 |
|
| 1394 |
void GlobalHelperThreadState::waitUntilEmptyFreeDelazifyTaskVector( |
| 1395 |
AutoLockHelperThreadState& lock) { |
| 1396 |
while (true) { |
| 1397 |
bool inProgress = false; |
| 1398 |
if (!freeDelazifyTaskVector(lock).empty()) { |
| 1399 |
inProgress = true; |
| 1400 |
} |
| 1401 |
|
| 1402 |
// If running tasks are delazifying any functions, then we have to wait |
| 1403 |
// until they complete to remove them from the pending list. DelazifyTask |
| 1404 |
// are inserting themself back to be processed once more after delazifying a |
| 1405 |
// function. |
| 1406 |
for (auto* helper : helperTasks(lock)) { |
| 1407 |
if (helper->is<FreeDelazifyTask>()) { |
| 1408 |
inProgress = true; |
| 1409 |
break; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
if (!inProgress) { |
| 1413 |
break; |
| 1414 |
} |
| 1415 |
|
| 1416 |
wait(lock); |
| 1417 |
} |
| 1418 |
} |
| 1419 |
|
| 1420 |
void js::CancelOffThreadDelazify(JSRuntime* runtime) { |
| 1421 |
AutoLockHelperThreadState lock; |
| 1422 |
|
| 1423 |
if (!HelperThreadState().isInitialized(lock)) { |
| 1424 |
return; |
| 1425 |
} |
| 1426 |
|
| 1427 |
// Cancel all Delazify tasks from the given runtime, and wait if tasks are |
| 1428 |
// from the given runtime are being executed. |
| 1429 |
HelperThreadState().waitUntilCancelledDelazifyTasks(runtime, lock); |
| 1430 |
|
| 1431 |
// Empty the free list of delazify task, in case one of the delazify task |
| 1432 |
// ended and therefore did not returned to the pending list of delazify tasks. |
| 1433 |
HelperThreadState().waitUntilEmptyFreeDelazifyTaskVector(lock); |
| 1434 |
} |
| 1435 |
|
| 1436 |
bool GlobalHelperThreadState::hasAnyDelazifyTask( |
| 1437 |
JSRuntime* rt, AutoLockHelperThreadState& lock) { |
| 1438 |
for (auto task : delazifyWorklist(lock)) { |
| 1439 |
if (task->runtimeMatchesOrNoRuntime(rt)) { |
| 1440 |
return true; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
for (auto* helper : helperTasks(lock)) { |
| 1445 |
if (helper->is<DelazifyTask>() && |
| 1446 |
helper->as<DelazifyTask>()->runtimeMatchesOrNoRuntime(rt)) { |
| 1447 |
return true; |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
return false; |
| 1452 |
} |
| 1453 |
|
| 1454 |
void js::WaitForAllDelazifyTasks(JSRuntime* rt) { |
| 1455 |
AutoLockHelperThreadState lock; |
| 1456 |
if (!HelperThreadState().isInitialized(lock)) { |
| 1457 |
return; |
| 1458 |
} |
| 1459 |
|
| 1460 |
while (true) { |
| 1461 |
if (!HelperThreadState().hasAnyDelazifyTask(rt, lock)) { |
| 1462 |
break; |
| 1463 |
} |
| 1464 |
|
| 1465 |
HelperThreadState().wait(lock); |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
//== FreeDelazifyTask ===================================================== |
| 1470 |
|
| 1471 |
bool GlobalHelperThreadState::canStartFreeDelazifyTask( |
| 1472 |
const AutoLockHelperThreadState& lock) { |
| 1473 |
return !freeDelazifyTaskVector(lock).empty() && |
| 1474 |
checkTaskThreadLimit(THREAD_TYPE_DELAZIFY_FREE, maxDelazifyThreads(), |
| 1475 |
/*isMaster=*/true, lock); |
| 1476 |
} |
| 1477 |
|
| 1478 |
HelperThreadTask* GlobalHelperThreadState::maybeGetFreeDelazifyTask( |
| 1479 |
const AutoLockHelperThreadState& lock) { |
| 1480 |
auto& freeList = freeDelazifyTaskVector(lock); |
| 1481 |
if (!freeList.empty()) { |
| 1482 |
UniquePtr<FreeDelazifyTask> task = std::move(freeList.back()); |
| 1483 |
freeList.popBack(); |
| 1484 |
return task.release(); |
| 1485 |
} |
| 1486 |
return nullptr; |
| 1487 |
} |
| 1488 |
|
| 1489 |
bool GlobalHelperThreadState::submitTask( |
| 1490 |
UniquePtr<FreeDelazifyTask> task, const AutoLockHelperThreadState& locked) { |
| 1491 |
if (!freeDelazifyTaskVector(locked).append(std::move(task))) { |
| 1492 |
return false; |
| 1493 |
} |
| 1494 |
dispatch(locked); |
| 1495 |
return true; |
| 1496 |
} |
| 1497 |
|
| 1498 |
void FreeDelazifyTask::runHelperThreadTask(AutoLockHelperThreadState& locked) { |
| 1499 |
{ |
| 1500 |
AutoUnlockHelperThreadState unlock(locked); |
| 1501 |
js_delete(task); |
| 1502 |
task = nullptr; |
| 1503 |
} |
| 1504 |
|
| 1505 |
js_delete(this); |
| 1506 |
} |
| 1507 |
|
| 1508 |
//== PromiseHelperTask ==================================================== |
| 1509 |
|
| 1510 |
bool GlobalHelperThreadState::canStartPromiseHelperTask( |
| 1511 |
const AutoLockHelperThreadState& lock) { |
| 1512 |
// PromiseHelperTasks can be wasm compilation tasks that in turn block on |
| 1513 |
// wasm compilation so set isMaster = true. |
| 1514 |
return !promiseHelperTasks(lock).empty() && |
| 1515 |
checkTaskThreadLimit(THREAD_TYPE_PROMISE_TASK, |
| 1516 |
maxPromiseHelperThreads(), |
| 1517 |
/*isMaster=*/true, lock); |
| 1518 |
} |
| 1519 |
|
| 1520 |
HelperThreadTask* GlobalHelperThreadState::maybeGetPromiseHelperTask( |
| 1521 |
const AutoLockHelperThreadState& lock) { |
| 1522 |
if (!canStartPromiseHelperTask(lock)) { |
| 1523 |
return nullptr; |
| 1524 |
} |
| 1525 |
|
| 1526 |
return promiseHelperTasks(lock).popCopy(); |
| 1527 |
} |
| 1528 |
|
| 1529 |
bool GlobalHelperThreadState::submitTask(PromiseHelperTask* task) { |
| 1530 |
AutoLockHelperThreadState lock; |
| 1531 |
|
| 1532 |
if (!promiseHelperTasks(lock).append(task)) { |
| 1533 |
return false; |
| 1534 |
} |
| 1535 |
|
| 1536 |
dispatch(lock); |
| 1537 |
return true; |
| 1538 |
} |
| 1539 |
|
| 1540 |
void PromiseHelperTask::executeAndResolveAndDestroy(JSContext* cx) { |
| 1541 |
execute(); |
| 1542 |
run(cx, JS::Dispatchable::NotShuttingDown); |
| 1543 |
} |
| 1544 |
|
| 1545 |
void PromiseHelperTask::runHelperThreadTask(AutoLockHelperThreadState& lock) { |
| 1546 |
{ |
| 1547 |
AutoUnlockHelperThreadState unlock(lock); |
| 1548 |
execute(); |
| 1549 |
} |
| 1550 |
|
| 1551 |
// Don't release the lock between dispatching the resolve and destroy |
| 1552 |
// operation (which may start immediately on another thread) and returning |
| 1553 |
// from this method. |
| 1554 |
|
| 1555 |
dispatchResolveAndDestroy(lock); |
| 1556 |
} |
| 1557 |
|
| 1558 |
bool js::StartOffThreadPromiseHelperTask(JSContext* cx, |
| 1559 |
UniquePtr<PromiseHelperTask> task) { |
| 1560 |
// Execute synchronously if there are no helper threads. |
| 1561 |
if (!CanUseExtraThreads()) { |
| 1562 |
task.release()->executeAndResolveAndDestroy(cx); |
| 1563 |
return true; |
| 1564 |
} |
| 1565 |
|
| 1566 |
if (!HelperThreadState().submitTask(task.get())) { |
| 1567 |
ReportOutOfMemory(cx); |
| 1568 |
return false; |
| 1569 |
} |
| 1570 |
|
| 1571 |
(void)task.release(); |
| 1572 |
return true; |
| 1573 |
} |
| 1574 |
|
| 1575 |
bool js::StartOffThreadPromiseHelperTask(PromiseHelperTask* task) { |
| 1576 |
MOZ_ASSERT(CanUseExtraThreads()); |
| 1577 |
|
| 1578 |
return HelperThreadState().submitTask(task); |
| 1579 |
} |
| 1580 |
|
| 1581 |
//== SourceCompressionTask ================================================ |
| 1582 |
|
| 1583 |
bool GlobalHelperThreadState::canStartCompressionTask( |
| 1584 |
const AutoLockHelperThreadState& lock) { |
| 1585 |
return !compressionWorklist(lock).empty() && |
| 1586 |
checkTaskThreadLimit(THREAD_TYPE_COMPRESS, maxCompressionThreads(), |
| 1587 |
lock); |
| 1588 |
} |
| 1589 |
|
| 1590 |
HelperThreadTask* GlobalHelperThreadState::maybeGetCompressionTask( |
| 1591 |
const AutoLockHelperThreadState& lock) { |
| 1592 |
if (!canStartCompressionTask(lock)) { |
| 1593 |
return nullptr; |
| 1594 |
} |
| 1595 |
|
| 1596 |
auto& worklist = compressionWorklist(lock); |
| 1597 |
UniquePtr<SourceCompressionTask> task = std::move(worklist.back()); |
| 1598 |
worklist.popBack(); |
| 1599 |
return task.release(); |
| 1600 |
} |
| 1601 |
|
| 1602 |
bool GlobalHelperThreadState::submitTask( |
| 1603 |
UniquePtr<SourceCompressionTask> task, |
| 1604 |
const AutoLockHelperThreadState& locked) { |
| 1605 |
if (!compressionWorklist(locked).append(std::move(task))) { |
| 1606 |
return false; |
| 1607 |
} |
| 1608 |
|
| 1609 |
dispatch(locked); |
| 1610 |
return true; |
| 1611 |
} |
| 1612 |
|
| 1613 |
void GlobalHelperThreadState::startHandlingCompressionTasks( |
| 1614 |
ScheduleCompressionTask schedule, JSRuntime* maybeRuntime, |
| 1615 |
const AutoLockHelperThreadState& lock) { |
| 1616 |
MOZ_ASSERT((schedule == ScheduleCompressionTask::GC) == |
| 1617 |
(maybeRuntime != nullptr)); |
| 1618 |
|
| 1619 |
auto& pending = compressionPendingList(lock); |
| 1620 |
|
| 1621 |
for (size_t i = 0; i < pending.length(); i++) { |
| 1622 |
UniquePtr<SourceCompressionTask>& task = pending[i]; |
| 1623 |
if (schedule == ScheduleCompressionTask::API || |
| 1624 |
(task->runtimeMatches(maybeRuntime) && task->shouldStart())) { |
| 1625 |
// OOMing during appending results in the task not being scheduled |
| 1626 |
// and deleted. |
| 1627 |
(void)submitTask(std::move(task), lock); |
| 1628 |
remove(pending, &i); |
| 1629 |
} |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
void js::AttachFinishedCompressions(JSRuntime* runtime, |
| 1634 |
AutoLockHelperThreadState& lock) { |
| 1635 |
auto& finished = HelperThreadState().compressionFinishedList(lock); |
| 1636 |
for (size_t i = 0; i < finished.length(); i++) { |
| 1637 |
if (finished[i]->runtimeMatches(runtime)) { |
| 1638 |
UniquePtr<SourceCompressionTask> compressionTask(std::move(finished[i])); |
| 1639 |
HelperThreadState().remove(finished, &i); |
| 1640 |
compressionTask->complete(); |
| 1641 |
} |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
void js::SweepPendingCompressions(AutoLockHelperThreadState& lock) { |
| 1646 |
auto& pending = HelperThreadState().compressionPendingList(lock); |
| 1647 |
for (size_t i = 0; i < pending.length(); i++) { |
| 1648 |
if (pending[i]->shouldCancel()) { |
| 1649 |
HelperThreadState().remove(pending, &i); |
| 1650 |
} |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
void js::RunPendingSourceCompressions(JSRuntime* runtime) { |
| 1655 |
if (!CanUseExtraThreads()) { |
| 1656 |
return; |
| 1657 |
} |
| 1658 |
|
| 1659 |
AutoLockHelperThreadState lock; |
| 1660 |
HelperThreadState().runPendingSourceCompressions(runtime, lock); |
| 1661 |
} |
| 1662 |
|
| 1663 |
void GlobalHelperThreadState::runPendingSourceCompressions( |
| 1664 |
JSRuntime* runtime, AutoLockHelperThreadState& lock) { |
| 1665 |
startHandlingCompressionTasks( |
| 1666 |
GlobalHelperThreadState::ScheduleCompressionTask::API, nullptr, lock); |
| 1667 |
{ |
| 1668 |
// Dispatch tasks. |
| 1669 |
AutoUnlockHelperThreadState unlock(lock); |
| 1670 |
} |
| 1671 |
|
| 1672 |
// Wait until all tasks have started compression. |
| 1673 |
while (!compressionWorklist(lock).empty()) { |
| 1674 |
wait(lock); |
| 1675 |
} |
| 1676 |
|
| 1677 |
// Wait for all in-process compression tasks to complete. |
| 1678 |
waitForAllTasksLocked(lock); |
| 1679 |
|
| 1680 |
AttachFinishedCompressions(runtime, lock); |
| 1681 |
} |
| 1682 |
|
| 1683 |
bool js::EnqueueOffThreadCompression(JSContext* cx, |
| 1684 |
UniquePtr<SourceCompressionTask> task) { |
| 1685 |
AutoLockHelperThreadState lock; |
| 1686 |
|
| 1687 |
auto& pending = HelperThreadState().compressionPendingList(lock); |
| 1688 |
if (!pending.append(std::move(task))) { |
| 1689 |
ReportOutOfMemory(cx); |
| 1690 |
return false; |
| 1691 |
} |
| 1692 |
|
| 1693 |
return true; |
| 1694 |
} |
| 1695 |
|
| 1696 |
void js::StartHandlingCompressionsOnGC(JSRuntime* runtime) { |
| 1697 |
AutoLockHelperThreadState lock; |
| 1698 |
HelperThreadState().startHandlingCompressionTasks( |
| 1699 |
GlobalHelperThreadState::ScheduleCompressionTask::GC, runtime, lock); |
| 1700 |
} |
| 1701 |
|
| 1702 |
template <typename T> |
| 1703 |
static void ClearCompressionTaskList(T& list, JSRuntime* runtime) { |
| 1704 |
for (size_t i = 0; i < list.length(); i++) { |
| 1705 |
if (list[i]->runtimeMatches(runtime)) { |
| 1706 |
HelperThreadState().remove(list, &i); |
| 1707 |
} |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
void GlobalHelperThreadState::cancelOffThreadCompressions( |
| 1712 |
JSRuntime* runtime, AutoLockHelperThreadState& lock) { |
| 1713 |
// Cancel all pending compression tasks. |
| 1714 |
ClearCompressionTaskList(compressionPendingList(lock), runtime); |
| 1715 |
ClearCompressionTaskList(compressionWorklist(lock), runtime); |
| 1716 |
|
| 1717 |
// Cancel all in-process compression tasks and wait for them to join so we |
| 1718 |
// clean up the finished tasks. |
| 1719 |
while (true) { |
| 1720 |
bool inProgress = false; |
| 1721 |
for (auto* helper : helperTasks(lock)) { |
| 1722 |
if (!helper->is<SourceCompressionTask>()) { |
| 1723 |
continue; |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (helper->as<SourceCompressionTask>()->runtimeMatches(runtime)) { |
| 1727 |
inProgress = true; |
| 1728 |
} |
| 1729 |
} |
| 1730 |
|
| 1731 |
if (!inProgress) { |
| 1732 |
break; |
| 1733 |
} |
| 1734 |
|
| 1735 |
wait(lock); |
| 1736 |
} |
| 1737 |
|
| 1738 |
// Clean up finished tasks. |
| 1739 |
ClearCompressionTaskList(compressionFinishedList(lock), runtime); |
| 1740 |
} |
| 1741 |
|
| 1742 |
void js::CancelOffThreadCompressions(JSRuntime* runtime) { |
| 1743 |
if (!CanUseExtraThreads()) { |
| 1744 |
return; |
| 1745 |
} |
| 1746 |
|
| 1747 |
AutoLockHelperThreadState lock; |
| 1748 |
HelperThreadState().cancelOffThreadCompressions(runtime, lock); |
| 1749 |
} |
| 1750 |
|
| 1751 |
//== GCParallelTask ======================================================= |
| 1752 |
|
| 1753 |
bool GlobalHelperThreadState::canStartGCParallelTask( |
| 1754 |
const AutoLockHelperThreadState& lock) { |
| 1755 |
return !gcParallelWorklist().isEmpty(lock) && |
| 1756 |
checkTaskThreadLimit(THREAD_TYPE_GCPARALLEL, maxGCParallelThreads(), |
| 1757 |
lock); |
| 1758 |
} |
| 1759 |
|
| 1760 |
HelperThreadTask* GlobalHelperThreadState::maybeGetGCParallelTask( |
| 1761 |
const AutoLockHelperThreadState& lock) { |
| 1762 |
if (!canStartGCParallelTask(lock)) { |
| 1763 |
return nullptr; |
| 1764 |
} |
| 1765 |
|
| 1766 |
return gcParallelWorklist().popFirst(lock); |
| 1767 |
} |
| 1768 |
|
| 1769 |
bool GlobalHelperThreadState::submitTask( |
| 1770 |
GCParallelTask* task, const AutoLockHelperThreadState& locked) { |
| 1771 |
gcParallelWorklist().insertBack(task, locked); |
| 1772 |
dispatch(locked); |
| 1773 |
return true; |
| 1774 |
} |
| 1775 |
|
| 1776 |
/////////////////////////////////////////////////////////////////////////// |
| 1777 |
// // |
| 1778 |
// Wasm task definitions // |
| 1779 |
// // |
| 1780 |
/////////////////////////////////////////////////////////////////////////// |
| 1781 |
|
| 1782 |
//== WasmTier1CompileTask ================================================= |
| 1783 |
|
| 1784 |
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmTier1CompileTask( |
| 1785 |
const AutoLockHelperThreadState& lock) { |
| 1786 |
return maybeGetWasmCompile(lock, wasm::CompileState::EagerTier1); |
| 1787 |
} |
| 1788 |
|
| 1789 |
bool GlobalHelperThreadState::canStartWasmTier1CompileTask( |
| 1790 |
const AutoLockHelperThreadState& lock) { |
| 1791 |
return canStartWasmCompile(lock, wasm::CompileState::EagerTier1); |
| 1792 |
} |
| 1793 |
|
| 1794 |
//== WasmTier2CompileTask ================================================= |
| 1795 |
|
| 1796 |
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmTier2CompileTask( |
| 1797 |
const AutoLockHelperThreadState& lock) { |
| 1798 |
return maybeGetWasmCompile(lock, wasm::CompileState::EagerTier2); |
| 1799 |
} |
| 1800 |
|
| 1801 |
bool GlobalHelperThreadState::canStartWasmTier2CompileTask( |
| 1802 |
const AutoLockHelperThreadState& lock) { |
| 1803 |
return canStartWasmCompile(lock, wasm::CompileState::EagerTier2); |
| 1804 |
} |
| 1805 |
|
| 1806 |
//== WasmCompleteTier2GeneratorTask ======================================= |
| 1807 |
|
| 1808 |
bool GlobalHelperThreadState::canStartWasmCompleteTier2GeneratorTask( |
| 1809 |
const AutoLockHelperThreadState& lock) { |
| 1810 |
return !wasmCompleteTier2GeneratorWorklist(lock).empty() && |
| 1811 |
checkTaskThreadLimit(THREAD_TYPE_WASM_GENERATOR_COMPLETE_TIER2, |
| 1812 |
maxWasmCompleteTier2GeneratorThreads(), |
| 1813 |
/*isMaster=*/true, lock); |
| 1814 |
} |
| 1815 |
|
| 1816 |
HelperThreadTask* |
| 1817 |
GlobalHelperThreadState::maybeGetWasmCompleteTier2GeneratorTask( |
| 1818 |
const AutoLockHelperThreadState& lock) { |
| 1819 |
if (!canStartWasmCompleteTier2GeneratorTask(lock)) { |
| 1820 |
return nullptr; |
| 1821 |
} |
| 1822 |
|
| 1823 |
return wasmCompleteTier2GeneratorWorklist(lock).popCopy(); |
| 1824 |
} |
| 1825 |
|
| 1826 |
bool GlobalHelperThreadState::submitTask( |
| 1827 |
wasm::UniqueCompleteTier2GeneratorTask task) { |
| 1828 |
AutoLockHelperThreadState lock; |
| 1829 |
|
| 1830 |
MOZ_ASSERT(isInitialized(lock)); |
| 1831 |
|
| 1832 |
if (!wasmCompleteTier2GeneratorWorklist(lock).append(task.get())) { |
| 1833 |
return false; |
| 1834 |
} |
| 1835 |
(void)task.release(); |
| 1836 |
|
| 1837 |
dispatch(lock); |
| 1838 |
return true; |
| 1839 |
} |
| 1840 |
|
| 1841 |
void js::StartOffThreadWasmCompleteTier2Generator( |
| 1842 |
wasm::UniqueCompleteTier2GeneratorTask task) { |
| 1843 |
(void)HelperThreadState().submitTask(std::move(task)); |
| 1844 |
} |
| 1845 |
|
| 1846 |
void GlobalHelperThreadState::cancelOffThreadWasmCompleteTier2Generator( |
| 1847 |
AutoLockHelperThreadState& lock) { |
| 1848 |
// Remove pending tasks from the tier2 generator worklist and cancel and |
| 1849 |
// delete them. |
| 1850 |
{ |
| 1851 |
wasm::CompleteTier2GeneratorTaskPtrVector& worklist = |
| 1852 |
wasmCompleteTier2GeneratorWorklist(lock); |
| 1853 |
for (size_t i = 0; i < worklist.length(); i++) { |
| 1854 |
wasm::CompleteTier2GeneratorTask* task = worklist[i]; |
| 1855 |
remove(worklist, &i); |
| 1856 |
js_delete(task); |
| 1857 |
} |
| 1858 |
} |
| 1859 |
|
| 1860 |
// There is at most one running CompleteTier2Generator task and we assume that |
| 1861 |
// below. |
| 1862 |
static_assert(GlobalHelperThreadState::MaxCompleteTier2GeneratorTasks == 1, |
| 1863 |
"code must be generalized"); |
| 1864 |
|
| 1865 |
// If there is a running Tier2 generator task, shut it down in a predictable |
| 1866 |
// way. The task will be deleted by the normal deletion logic. |
| 1867 |
for (auto* helper : helperTasks(lock)) { |
| 1868 |
if (helper->is<wasm::CompleteTier2GeneratorTask>()) { |
| 1869 |
// Set a flag that causes compilation to shortcut itself. |
| 1870 |
helper->as<wasm::CompleteTier2GeneratorTask>()->cancel(); |
| 1871 |
|
| 1872 |
// Wait for the generator task to finish. This avoids a shutdown race |
| 1873 |
// where the shutdown code is trying to shut down helper threads and the |
| 1874 |
// ongoing tier2 compilation is trying to finish, which requires it to |
| 1875 |
// have access to helper threads. |
| 1876 |
uint32_t oldFinishedCount = wasmCompleteTier2GeneratorsFinished(lock); |
| 1877 |
while (wasmCompleteTier2GeneratorsFinished(lock) == oldFinishedCount) { |
| 1878 |
wait(lock); |
| 1879 |
} |
| 1880 |
|
| 1881 |
// At most one of these tasks. |
| 1882 |
break; |
| 1883 |
} |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
static void CancelOffThreadWasmCompleteTier2GeneratorLocked( |
| 1888 |
AutoLockHelperThreadState& lock) { |
| 1889 |
if (!HelperThreadState().isInitialized(lock)) { |
| 1890 |
return; |
| 1891 |
} |
| 1892 |
|
| 1893 |
HelperThreadState().cancelOffThreadWasmCompleteTier2Generator(lock); |
| 1894 |
} |
| 1895 |
|
| 1896 |
void js::CancelOffThreadWasmCompleteTier2Generator() { |
| 1897 |
AutoLockHelperThreadState lock; |
| 1898 |
CancelOffThreadWasmCompleteTier2GeneratorLocked(lock); |
| 1899 |
} |
| 1900 |
|
| 1901 |
//== WasmPartialTier2CompileTask ========================================== |
| 1902 |
|
| 1903 |
bool GlobalHelperThreadState::canStartWasmPartialTier2CompileTask( |
| 1904 |
const AutoLockHelperThreadState& lock) { |
| 1905 |
size_t maxThreads = maxWasmPartialTier2CompileThreads(); |
| 1906 |
// Avoid assertion failure in checkTaskThreadLimit(). |
| 1907 |
if (maxThreads > threadCount) { |
| 1908 |
maxThreads = threadCount; |
| 1909 |
} |
| 1910 |
return !wasmPartialTier2CompileWorklist(lock).empty() && |
| 1911 |
checkTaskThreadLimit(THREAD_TYPE_WASM_COMPILE_PARTIAL_TIER2, |
| 1912 |
maxThreads, /*isMaster=*/false, lock); |
| 1913 |
} |
| 1914 |
|
| 1915 |
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmPartialTier2CompileTask( |
| 1916 |
const AutoLockHelperThreadState& lock) { |
| 1917 |
if (!canStartWasmPartialTier2CompileTask(lock)) { |
| 1918 |
return nullptr; |
| 1919 |
} |
| 1920 |
|
| 1921 |
// Take the task at the start of the vector and slide the rest down. The |
| 1922 |
// vector is almost always small (fewer than 50 items) and most of the time |
| 1923 |
// has only one item, so this isn't a big expense. |
| 1924 |
wasm::PartialTier2CompileTaskPtrVector& worklist = |
| 1925 |
wasmPartialTier2CompileWorklist(lock); |
| 1926 |
MOZ_ASSERT(!worklist.empty()); |
| 1927 |
HelperThreadTask* task = worklist[0]; |
| 1928 |
worklist.erase(worklist.begin()); |
| 1929 |
return task; |
| 1930 |
} |
| 1931 |
|
| 1932 |
bool GlobalHelperThreadState::submitTask( |
| 1933 |
wasm::UniquePartialTier2CompileTask task) { |
| 1934 |
AutoLockHelperThreadState lock; |
| 1935 |
|
| 1936 |
MOZ_ASSERT(isInitialized(lock)); |
| 1937 |
|
| 1938 |
wasm::PartialTier2CompileTaskPtrVector& workList = |
| 1939 |
wasmPartialTier2CompileWorklist(lock); |
| 1940 |
|
| 1941 |
// Put the new task at the end of the vector. |
| 1942 |
// ::maybeGetWasmPartialTier2CompileTask pulls tasks from the front of the |
| 1943 |
// vector, hence giving FIFO behaviour. |
| 1944 |
if (!workList.append(task.get())) { |
| 1945 |
return false; |
| 1946 |
} |
| 1947 |
(void)task.release(); |
| 1948 |
|
| 1949 |
dispatch(lock); |
| 1950 |
return true; |
| 1951 |
} |
| 1952 |
|
| 1953 |
void js::StartOffThreadWasmPartialTier2Compile( |
| 1954 |
wasm::UniquePartialTier2CompileTask task) { |
| 1955 |
(void)HelperThreadState().submitTask(std::move(task)); |
| 1956 |
} |
| 1957 |
|
| 1958 |
void GlobalHelperThreadState::cancelOffThreadWasmPartialTier2Compile( |
| 1959 |
AutoLockHelperThreadState& lock) { |
| 1960 |
// Remove pending tasks from the partial tier2 compilation worklist and |
| 1961 |
// cancel and delete them. |
| 1962 |
wasm::PartialTier2CompileTaskPtrVector& worklist = |
| 1963 |
wasmPartialTier2CompileWorklist(lock); |
| 1964 |
for (size_t i = 0; i < worklist.length(); i++) { |
| 1965 |
wasm::PartialTier2CompileTask* task = worklist[i]; |
| 1966 |
remove(worklist, &i); |
| 1967 |
js_delete(task); |
| 1968 |
} |
| 1969 |
|
| 1970 |
// And remove running partial tier2 compilation tasks. They will be deleted |
| 1971 |
// by the normal deletion logic (in |
| 1972 |
// PartialTier2CompileTaskImpl::runHelperThreadTask). |
| 1973 |
bool anyCancelled; |
| 1974 |
do { |
| 1975 |
anyCancelled = false; |
| 1976 |
for (auto* helper : helperTasks(lock)) { |
| 1977 |
if (!helper->is<wasm::PartialTier2CompileTask>()) { |
| 1978 |
continue; |
| 1979 |
} |
| 1980 |
wasm::PartialTier2CompileTask* pt2CompileTask = |
| 1981 |
helper->as<wasm::PartialTier2CompileTask>(); |
| 1982 |
pt2CompileTask->cancel(); |
| 1983 |
anyCancelled = true; |
| 1984 |
} |
| 1985 |
if (anyCancelled) { |
| 1986 |
wait(lock); |
| 1987 |
} |
| 1988 |
} while (anyCancelled); |
| 1989 |
} |
| 1990 |
|
| 1991 |
static void CancelOffThreadWasmPartialTier2CompileLocked( |
| 1992 |
AutoLockHelperThreadState& lock) { |
| 1993 |
if (!HelperThreadState().isInitialized(lock)) { |
| 1994 |
return; |
| 1995 |
} |
| 1996 |
|
| 1997 |
HelperThreadState().cancelOffThreadWasmPartialTier2Compile(lock); |
| 1998 |
} |
| 1999 |
|
| 2000 |
void js::CancelOffThreadWasmPartialTier2Compile() { |
| 2001 |
AutoLockHelperThreadState lock; |
| 2002 |
CancelOffThreadWasmPartialTier2CompileLocked(lock); |
| 2003 |
} |
| 2004 |
|
| 2005 |
//== wasm task management ================================================= |
| 2006 |
|
| 2007 |
bool GlobalHelperThreadState::canStartWasmCompile( |
| 2008 |
const AutoLockHelperThreadState& lock, wasm::CompileState state) { |
| 2009 |
if (wasmWorklist(lock, state).empty()) { |
| 2010 |
return false; |
| 2011 |
} |
| 2012 |
|
| 2013 |
// Parallel compilation and background compilation should be disabled on |
| 2014 |
// unicore systems. |
| 2015 |
|
| 2016 |
MOZ_RELEASE_ASSERT(cpuCount > 1); |
| 2017 |
|
| 2018 |
// If CompleteTier2 is very backlogged we must give priority to it, since the |
| 2019 |
// CompleteTier2 queue holds onto Tier1 tasks. Indeed if CompleteTier2 is |
| 2020 |
// backlogged we will devote more resources to CompleteTier2 and not start |
| 2021 |
// any Tier1 work at all. |
| 2022 |
|
| 2023 |
bool completeTier2oversubscribed = |
| 2024 |
wasmCompleteTier2GeneratorWorklist(lock).length() > 20; |
| 2025 |
|
| 2026 |
// For Tier1 and Once compilation, honor the maximum allowed threads to |
| 2027 |
// compile wasm jobs at once, to avoid oversaturating the machine. |
| 2028 |
// |
| 2029 |
// For CompleteTier2 compilation we need to allow other things to happen too, |
| 2030 |
// so we do not allow all logical cores to be used for background work; |
| 2031 |
// instead we wish to use a fraction of the physical cores. We can't |
| 2032 |
// directly compute the physical cores from the logical cores, but 1/3 of the |
| 2033 |
// logical cores is a safe estimate for the number of physical cores |
| 2034 |
// available for background work. |
| 2035 |
|
| 2036 |
size_t physCoresAvailable = size_t(ceil(cpuCount / 3.0)); |
| 2037 |
|
| 2038 |
size_t threads; |
| 2039 |
ThreadType threadType; |
| 2040 |
if (state == wasm::CompileState::EagerTier2) { |
| 2041 |
if (completeTier2oversubscribed) { |
| 2042 |
threads = maxWasmCompilationThreads(); |
| 2043 |
} else { |
| 2044 |
threads = physCoresAvailable; |
| 2045 |
} |
| 2046 |
threadType = THREAD_TYPE_WASM_COMPILE_TIER2; |
| 2047 |
} else { |
| 2048 |
if (completeTier2oversubscribed) { |
| 2049 |
threads = 0; |
| 2050 |
} else { |
| 2051 |
threads = maxWasmCompilationThreads(); |
| 2052 |
} |
| 2053 |
threadType = THREAD_TYPE_WASM_COMPILE_TIER1; |
| 2054 |
} |
| 2055 |
|
| 2056 |
return threads != 0 && checkTaskThreadLimit(threadType, threads, lock); |
| 2057 |
} |
| 2058 |
|
| 2059 |
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmCompile( |
| 2060 |
const AutoLockHelperThreadState& lock, wasm::CompileState state) { |
| 2061 |
if (!canStartWasmCompile(lock, state)) { |
| 2062 |
return nullptr; |
| 2063 |
} |
| 2064 |
|
| 2065 |
return wasmWorklist(lock, state).popCopyFront(); |
| 2066 |
} |
| 2067 |
|
| 2068 |
size_t js::RemovePendingWasmCompileTasks( |
| 2069 |
const wasm::CompileTaskState& taskState, wasm::CompileState state, |
| 2070 |
const AutoLockHelperThreadState& lock) { |
| 2071 |
wasm::CompileTaskPtrFifo& worklist = |
| 2072 |
HelperThreadState().wasmWorklist(lock, state); |
| 2073 |
return worklist.eraseIf([&taskState](wasm::CompileTask* task) { |
| 2074 |
return &task->state == &taskState; |
| 2075 |
}); |
| 2076 |
} |
| 2077 |
|
| 2078 |
bool GlobalHelperThreadState::submitTask(wasm::CompileTask* task, |
| 2079 |
wasm::CompileState state) { |
| 2080 |
AutoLockHelperThreadState lock; |
| 2081 |
if (!wasmWorklist(lock, state).pushBack(task)) { |
| 2082 |
return false; |
| 2083 |
} |
| 2084 |
|
| 2085 |
dispatch(lock); |
| 2086 |
return true; |
| 2087 |
} |
| 2088 |
|
| 2089 |
bool js::StartOffThreadWasmCompile(wasm::CompileTask* task, |
| 2090 |
wasm::CompileState state) { |
| 2091 |
return HelperThreadState().submitTask(task, state); |
| 2092 |
} |