| 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 |
/* |
| 8 |
* Definitions for managing off-thread work using a process wide list of |
| 9 |
* worklist items and pool of threads. Worklist items are engine internal, and |
| 10 |
* are distinct from e.g. web workers. |
| 11 |
*/ |
| 12 |
|
| 13 |
#ifndef vm_HelperThreadState_h |
| 14 |
#define vm_HelperThreadState_h |
| 15 |
|
| 16 |
#include "mozilla/AlreadyAddRefed.h" // already_AddRefed |
| 17 |
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_CRASH |
| 18 |
#include "mozilla/Attributes.h" // MOZ_RAII |
| 19 |
#include "mozilla/EnumeratedArray.h" // mozilla::EnumeratedArray |
| 20 |
#include "mozilla/LinkedList.h" // mozilla::LinkedList, mozilla::LinkedListElement |
| 21 |
#include "mozilla/MemoryReporting.h" // mozilla::MallocSizeOf |
| 22 |
#include "mozilla/RefPtr.h" // RefPtr |
| 23 |
#include "mozilla/TimeStamp.h" // mozilla::TimeDuration |
| 24 |
|
| 25 |
#include <stddef.h> // size_t |
| 26 |
#include <stdint.h> // uint32_t, uint64_t |
| 27 |
#include <utility> // std::move |
| 28 |
|
| 29 |
#include "ds/Fifo.h" // Fifo |
| 30 |
#include "frontend/CompilationStencil.h" // frontend::InitialStencilAndDelazifications |
| 31 |
#include "gc/GCRuntime.h" // gc::GCRuntime |
| 32 |
#include "js/AllocPolicy.h" // SystemAllocPolicy |
| 33 |
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions |
| 34 |
#include "js/experimental/JSStencil.h" // JS::InstantiationStorage |
| 35 |
#include "js/HelperThreadAPI.h" // JS::HelperThreadTaskCallback |
| 36 |
#include "js/MemoryMetrics.h" // JS::GlobalStats |
| 37 |
#include "js/ProfilingStack.h" // JS::RegisterThreadCallback, JS::UnregisterThreadCallback |
| 38 |
#include "js/RootingAPI.h" // JS::Handle |
| 39 |
#include "js/UniquePtr.h" // UniquePtr |
| 40 |
#include "js/Utility.h" // ThreadType |
| 41 |
#include "threading/ConditionVariable.h" // ConditionVariable |
| 42 |
#include "threading/ProtectedData.h" // WriteOnceData |
| 43 |
#include "vm/ConcurrentDelazification.h" // DelazificationContext |
| 44 |
#include "vm/HelperThreads.h" // AutoLockHelperThreadState, AutoUnlockHelperThreadState |
| 45 |
#include "vm/HelperThreadTask.h" // HelperThreadTask |
| 46 |
#include "vm/JSContext.h" // JSContext |
| 47 |
#include "vm/JSScript.h" // ScriptSource |
| 48 |
#include "vm/Runtime.h" // JSRuntime |
| 49 |
#include "vm/SharedImmutableStringsCache.h" // SharedImmutableString |
| 50 |
#include "wasm/WasmConstants.h" // wasm::CompileState |
| 51 |
|
| 52 |
class JSTracer; |
| 53 |
|
| 54 |
namespace js { |
| 55 |
|
| 56 |
struct DelazifyTask; |
| 57 |
struct FreeDelazifyTask; |
| 58 |
struct PromiseHelperTask; |
| 59 |
class PromiseObject; |
| 60 |
|
| 61 |
namespace jit { |
| 62 |
class BaselineCompileTask; |
| 63 |
class IonCompileTask; |
| 64 |
class IonFreeTask; |
| 65 |
} // namespace jit |
| 66 |
|
| 67 |
namespace wasm { |
| 68 |
|
| 69 |
struct CompileTask; |
| 70 |
using CompileTaskPtrFifo = Fifo<CompileTask*, 0, SystemAllocPolicy>; |
| 71 |
|
| 72 |
struct CompleteTier2GeneratorTask : public HelperThreadTask { |
| 73 |
virtual ~CompleteTier2GeneratorTask() = default; |
| 74 |
virtual void cancel() = 0; |
| 75 |
const char* getName() override { return "CompleteTier2GeneratorTask"; } |
| 76 |
}; |
| 77 |
|
| 78 |
using UniqueCompleteTier2GeneratorTask = UniquePtr<CompleteTier2GeneratorTask>; |
| 79 |
using CompleteTier2GeneratorTaskPtrVector = |
| 80 |
Vector<CompleteTier2GeneratorTask*, 0, SystemAllocPolicy>; |
| 81 |
|
| 82 |
struct PartialTier2CompileTask : public HelperThreadTask { |
| 83 |
virtual ~PartialTier2CompileTask() = default; |
| 84 |
virtual void cancel() = 0; |
| 85 |
const char* getName() override { return "PartialTier2CompileTask"; } |
| 86 |
}; |
| 87 |
|
| 88 |
using UniquePartialTier2CompileTask = UniquePtr<PartialTier2CompileTask>; |
| 89 |
using PartialTier2CompileTaskPtrVector = |
| 90 |
Vector<PartialTier2CompileTask*, 0, SystemAllocPolicy>; |
| 91 |
|
| 92 |
} // namespace wasm |
| 93 |
|
| 94 |
// Per-process state for off thread work items. |
| 95 |
class GlobalHelperThreadState { |
| 96 |
public: |
| 97 |
// A single complete tier-2 ModuleGenerator job spawns many compilation jobs, |
| 98 |
// and we do not want to allow more than one such ModuleGenerator to run at a |
| 99 |
// time. |
| 100 |
static const size_t MaxCompleteTier2GeneratorTasks = 1; |
| 101 |
|
| 102 |
// The number of partial tier 2 compilation tasks that can run |
| 103 |
// simultaneously. This constant specifies unfortunately both the default |
| 104 |
// and the maximum. |
| 105 |
static const size_t MaxPartialTier2CompileTasks = 1; |
| 106 |
|
| 107 |
// Number of CPUs to treat this machine as having when creating threads. |
| 108 |
// May be accessed without locking. |
| 109 |
size_t cpuCount; |
| 110 |
|
| 111 |
// Number of threads to create. May be accessed without locking. |
| 112 |
size_t threadCount; |
| 113 |
|
| 114 |
// Thread stack quota to use when running tasks. |
| 115 |
size_t stackQuota; |
| 116 |
|
| 117 |
bool terminating_ = false; |
| 118 |
|
| 119 |
using BaselineCompileTaskVector = |
| 120 |
Vector<jit::BaselineCompileTask*, 1, SystemAllocPolicy>; |
| 121 |
using IonCompileTaskVector = |
| 122 |
Vector<jit::IonCompileTask*, 0, SystemAllocPolicy>; |
| 123 |
using IonFreeTaskVector = |
| 124 |
Vector<js::UniquePtr<jit::IonFreeTask>, 0, SystemAllocPolicy>; |
| 125 |
using DelazifyTaskList = mozilla::LinkedList<DelazifyTask>; |
| 126 |
using FreeDelazifyTaskVector = |
| 127 |
Vector<js::UniquePtr<FreeDelazifyTask>, 1, SystemAllocPolicy>; |
| 128 |
using SourceCompressionTaskVector = |
| 129 |
Vector<UniquePtr<SourceCompressionTask>, 0, SystemAllocPolicy>; |
| 130 |
using PromiseHelperTaskVector = |
| 131 |
Vector<PromiseHelperTask*, 0, SystemAllocPolicy>; |
| 132 |
|
| 133 |
// Count of running task by each threadType. |
| 134 |
mozilla::EnumeratedArray<ThreadType, size_t, |
| 135 |
size_t(ThreadType::THREAD_TYPE_MAX)> |
| 136 |
runningTaskCount; |
| 137 |
size_t totalCountRunningTasks; |
| 138 |
|
| 139 |
WriteOnceData<JS::RegisterThreadCallback> registerThread; |
| 140 |
WriteOnceData<JS::UnregisterThreadCallback> unregisterThread; |
| 141 |
|
| 142 |
// Count of helper threads 'reserved' for parallel marking. This is used to |
| 143 |
// prevent too many runtimes trying to mark in parallel at once. Does not stop |
| 144 |
// threads from being used for other kinds of task, including GC tasks. |
| 145 |
HelperThreadLockData<size_t> gcParallelMarkingThreads; |
| 146 |
|
| 147 |
private: |
| 148 |
// The lists below are all protected by |lock|. |
| 149 |
|
| 150 |
// Baseline compilation worklist and finished jobs. |
| 151 |
BaselineCompileTaskVector baselineWorklist_, baselineFinishedList_; |
| 152 |
|
| 153 |
// Ion compilation worklist and finished jobs. |
| 154 |
IonCompileTaskVector ionWorklist_, ionFinishedList_; |
| 155 |
IonFreeTaskVector ionFreeList_; |
| 156 |
|
| 157 |
// wasm worklists. |
| 158 |
wasm::CompileTaskPtrFifo wasmWorklist_tier1_; |
| 159 |
wasm::CompileTaskPtrFifo wasmWorklist_tier2_; |
| 160 |
wasm::CompleteTier2GeneratorTaskPtrVector wasmCompleteTier2GeneratorWorklist_; |
| 161 |
wasm::PartialTier2CompileTaskPtrVector wasmPartialTier2CompileWorklist_; |
| 162 |
|
| 163 |
// Count of finished CompleteTier2Generator tasks. |
| 164 |
uint32_t wasmCompleteTier2GeneratorsFinished_; |
| 165 |
|
| 166 |
// Async tasks that, upon completion, are dispatched back to the JSContext's |
| 167 |
// owner thread via embedding callbacks instead of a finished list. |
| 168 |
PromiseHelperTaskVector promiseHelperTasks_; |
| 169 |
|
| 170 |
// Script worklist, which might still have function to delazify. |
| 171 |
DelazifyTaskList delazifyWorklist_; |
| 172 |
// Ideally an instance should not have a method to free it-self as, the method |
| 173 |
// has a this pointer, which aliases the deleted instance, and that the method |
| 174 |
// might have some of its fields aliased on the stack. |
| 175 |
// |
| 176 |
// Delazification task are complex and have a lot of fields. To reduce the |
| 177 |
// risk of having aliased fields on the stack while deleting instances of a |
| 178 |
// DelazifyTask, we have FreeDelazifyTask. While FreeDelazifyTask suffer from |
| 179 |
// the same problem, the limited scope of their actions should mitigate the |
| 180 |
// risk. |
| 181 |
FreeDelazifyTaskVector freeDelazifyTaskVector_; |
| 182 |
|
| 183 |
// Source compression worklist of tasks that we do not yet know can start. |
| 184 |
SourceCompressionTaskVector compressionPendingList_; |
| 185 |
|
| 186 |
// Source compression worklist of tasks that can start. |
| 187 |
SourceCompressionTaskVector compressionWorklist_; |
| 188 |
|
| 189 |
// Finished source compression tasks. |
| 190 |
SourceCompressionTaskVector compressionFinishedList_; |
| 191 |
|
| 192 |
// GC tasks needing to be done in parallel. These are first queued in the |
| 193 |
// GCRuntime before being dispatched to the helper thread system. |
| 194 |
GCParallelTaskList gcParallelWorklist_; |
| 195 |
|
| 196 |
using HelperThreadTaskVector = |
| 197 |
Vector<HelperThreadTask*, 0, SystemAllocPolicy>; |
| 198 |
// Vector of running HelperThreadTask. |
| 199 |
// This is used to get the HelperThreadTask that are currently running. |
| 200 |
HelperThreadTaskVector helperTasks_; |
| 201 |
|
| 202 |
// Callback to dispatch a task to a thread pool. Set by |
| 203 |
// JS::SetHelperThreadTaskCallback. If this is not set the internal thread |
| 204 |
// pool is used. |
| 205 |
JS::HelperThreadTaskCallback dispatchTaskCallback = nullptr; |
| 206 |
friend class AutoHelperTaskQueue; |
| 207 |
|
| 208 |
// Condition variable for notifiying the main thread that a helper task has |
| 209 |
// completed some work. |
| 210 |
js::ConditionVariable consumerWakeup; |
| 211 |
|
| 212 |
#ifdef DEBUG |
| 213 |
// The number of tasks dispatched to the thread pool that have not started |
| 214 |
// running yet. |
| 215 |
size_t tasksPending_ = 0; |
| 216 |
#endif |
| 217 |
|
| 218 |
bool isInitialized_ = false; |
| 219 |
|
| 220 |
bool useInternalThreadPool_ = true; |
| 221 |
|
| 222 |
public: |
| 223 |
void addSizeOfIncludingThis(JS::GlobalStats* stats, |
| 224 |
const AutoLockHelperThreadState& lock) const; |
| 225 |
|
| 226 |
size_t maxBaselineCompilationThreads() const; |
| 227 |
size_t maxIonCompilationThreads() const; |
| 228 |
size_t maxIonFreeThreads() const; |
| 229 |
size_t maxWasmCompilationThreads() const; |
| 230 |
size_t maxWasmCompleteTier2GeneratorThreads() const; |
| 231 |
size_t maxWasmPartialTier2CompileThreads() const; |
| 232 |
size_t maxPromiseHelperThreads() const; |
| 233 |
size_t maxDelazifyThreads() const; |
| 234 |
size_t maxCompressionThreads() const; |
| 235 |
size_t maxGCParallelThreads() const; |
| 236 |
|
| 237 |
GlobalHelperThreadState(); |
| 238 |
|
| 239 |
bool isInitialized(const AutoLockHelperThreadState& lock) const { |
| 240 |
return isInitialized_; |
| 241 |
} |
| 242 |
|
| 243 |
[[nodiscard]] bool ensureInitialized(); |
| 244 |
[[nodiscard]] bool ensureThreadCount(size_t count, |
| 245 |
AutoLockHelperThreadState& lock); |
| 246 |
void finish(AutoLockHelperThreadState& lock); |
| 247 |
void finishThreads(AutoLockHelperThreadState& lock); |
| 248 |
|
| 249 |
void setCpuCount(size_t count); |
| 250 |
|
| 251 |
void setDispatchTaskCallback(JS::HelperThreadTaskCallback callback, |
| 252 |
size_t threadCount, size_t stackSize, |
| 253 |
const AutoLockHelperThreadState& lock); |
| 254 |
|
| 255 |
void destroyHelperContexts(AutoLockHelperThreadState& lock); |
| 256 |
|
| 257 |
#ifdef DEBUG |
| 258 |
void assertIsLockedByCurrentThread() const; |
| 259 |
#endif |
| 260 |
|
| 261 |
void wait(AutoLockHelperThreadState& lock, |
| 262 |
mozilla::TimeDuration timeout = mozilla::TimeDuration::Forever()); |
| 263 |
void notifyAll(const AutoLockHelperThreadState&); |
| 264 |
|
| 265 |
bool useInternalThreadPool(const AutoLockHelperThreadState& lock) const { |
| 266 |
return useInternalThreadPool_; |
| 267 |
} |
| 268 |
|
| 269 |
bool isTerminating(const AutoLockHelperThreadState& locked) const { |
| 270 |
return terminating_; |
| 271 |
} |
| 272 |
|
| 273 |
private: |
| 274 |
void notifyOne(const AutoLockHelperThreadState&); |
| 275 |
|
| 276 |
public: |
| 277 |
// Helper method for removing items from the vectors below while iterating |
| 278 |
// over them. |
| 279 |
template <typename T> |
| 280 |
static void remove(T& vector, size_t* index) { |
| 281 |
// Self-moving is undefined behavior. |
| 282 |
if (*index != vector.length() - 1) { |
| 283 |
vector[*index] = std::move(vector.back()); |
| 284 |
} |
| 285 |
(*index)--; |
| 286 |
vector.popBack(); |
| 287 |
} |
| 288 |
|
| 289 |
BaselineCompileTaskVector& baselineWorklist( |
| 290 |
const AutoLockHelperThreadState&) { |
| 291 |
return baselineWorklist_; |
| 292 |
} |
| 293 |
BaselineCompileTaskVector& baselineFinishedList( |
| 294 |
const AutoLockHelperThreadState&) { |
| 295 |
return baselineFinishedList_; |
| 296 |
} |
| 297 |
IonCompileTaskVector& ionWorklist(const AutoLockHelperThreadState&) { |
| 298 |
return ionWorklist_; |
| 299 |
} |
| 300 |
IonCompileTaskVector& ionFinishedList(const AutoLockHelperThreadState&) { |
| 301 |
return ionFinishedList_; |
| 302 |
} |
| 303 |
IonFreeTaskVector& ionFreeList(const AutoLockHelperThreadState&) { |
| 304 |
return ionFreeList_; |
| 305 |
} |
| 306 |
|
| 307 |
wasm::CompileTaskPtrFifo& wasmWorklist(const AutoLockHelperThreadState&, |
| 308 |
wasm::CompileState state) { |
| 309 |
switch (state) { |
| 310 |
case wasm::CompileState::Once: |
| 311 |
case wasm::CompileState::EagerTier1: |
| 312 |
case wasm::CompileState::LazyTier1: |
| 313 |
return wasmWorklist_tier1_; |
| 314 |
case wasm::CompileState::EagerTier2: |
| 315 |
case wasm::CompileState::LazyTier2: |
| 316 |
return wasmWorklist_tier2_; |
| 317 |
default: |
| 318 |
MOZ_CRASH(); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
wasm::CompleteTier2GeneratorTaskPtrVector& wasmCompleteTier2GeneratorWorklist( |
| 323 |
const AutoLockHelperThreadState&) { |
| 324 |
return wasmCompleteTier2GeneratorWorklist_; |
| 325 |
} |
| 326 |
|
| 327 |
wasm::PartialTier2CompileTaskPtrVector& wasmPartialTier2CompileWorklist( |
| 328 |
const AutoLockHelperThreadState&) { |
| 329 |
return wasmPartialTier2CompileWorklist_; |
| 330 |
} |
| 331 |
|
| 332 |
void incWasmCompleteTier2GeneratorsFinished( |
| 333 |
const AutoLockHelperThreadState&) { |
| 334 |
wasmCompleteTier2GeneratorsFinished_++; |
| 335 |
} |
| 336 |
|
| 337 |
uint32_t wasmCompleteTier2GeneratorsFinished( |
| 338 |
const AutoLockHelperThreadState&) const { |
| 339 |
return wasmCompleteTier2GeneratorsFinished_; |
| 340 |
} |
| 341 |
|
| 342 |
PromiseHelperTaskVector& promiseHelperTasks( |
| 343 |
const AutoLockHelperThreadState&) { |
| 344 |
return promiseHelperTasks_; |
| 345 |
} |
| 346 |
|
| 347 |
DelazifyTaskList& delazifyWorklist(const AutoLockHelperThreadState&) { |
| 348 |
return delazifyWorklist_; |
| 349 |
} |
| 350 |
|
| 351 |
FreeDelazifyTaskVector& freeDelazifyTaskVector( |
| 352 |
const AutoLockHelperThreadState&) { |
| 353 |
return freeDelazifyTaskVector_; |
| 354 |
} |
| 355 |
|
| 356 |
SourceCompressionTaskVector& compressionPendingList( |
| 357 |
const AutoLockHelperThreadState&) { |
| 358 |
return compressionPendingList_; |
| 359 |
} |
| 360 |
|
| 361 |
SourceCompressionTaskVector& compressionWorklist( |
| 362 |
const AutoLockHelperThreadState&) { |
| 363 |
return compressionWorklist_; |
| 364 |
} |
| 365 |
|
| 366 |
SourceCompressionTaskVector& compressionFinishedList( |
| 367 |
const AutoLockHelperThreadState&) { |
| 368 |
return compressionFinishedList_; |
| 369 |
} |
| 370 |
|
| 371 |
private: |
| 372 |
GCParallelTaskList& gcParallelWorklist() { return gcParallelWorklist_; } |
| 373 |
|
| 374 |
HelperThreadTaskVector& helperTasks(const AutoLockHelperThreadState&) { |
| 375 |
return helperTasks_; |
| 376 |
} |
| 377 |
|
| 378 |
bool canStartWasmCompile(const AutoLockHelperThreadState& lock, |
| 379 |
wasm::CompileState state); |
| 380 |
|
| 381 |
bool canStartWasmTier1CompileTask(const AutoLockHelperThreadState& lock); |
| 382 |
bool canStartWasmTier2CompileTask(const AutoLockHelperThreadState& lock); |
| 383 |
bool canStartWasmCompleteTier2GeneratorTask( |
| 384 |
const AutoLockHelperThreadState& lock); |
| 385 |
bool canStartWasmPartialTier2CompileTask( |
| 386 |
const AutoLockHelperThreadState& lock); |
| 387 |
bool canStartPromiseHelperTask(const AutoLockHelperThreadState& lock); |
| 388 |
bool canStartBaselineCompileTask(const AutoLockHelperThreadState& lock); |
| 389 |
bool canStartIonCompileTask(const AutoLockHelperThreadState& lock); |
| 390 |
bool canStartIonFreeTask(const AutoLockHelperThreadState& lock); |
| 391 |
bool canStartFreeDelazifyTask(const AutoLockHelperThreadState& lock); |
| 392 |
bool canStartDelazifyTask(const AutoLockHelperThreadState& lock); |
| 393 |
bool canStartCompressionTask(const AutoLockHelperThreadState& lock); |
| 394 |
bool canStartGCParallelTask(const AutoLockHelperThreadState& lock); |
| 395 |
|
| 396 |
HelperThreadTask* maybeGetWasmCompile(const AutoLockHelperThreadState& lock, |
| 397 |
wasm::CompileState state); |
| 398 |
|
| 399 |
HelperThreadTask* maybeGetWasmTier1CompileTask( |
| 400 |
const AutoLockHelperThreadState& lock); |
| 401 |
HelperThreadTask* maybeGetWasmTier2CompileTask( |
| 402 |
const AutoLockHelperThreadState& lock); |
| 403 |
HelperThreadTask* maybeGetWasmCompleteTier2GeneratorTask( |
| 404 |
const AutoLockHelperThreadState& lock); |
| 405 |
HelperThreadTask* maybeGetWasmPartialTier2CompileTask( |
| 406 |
const AutoLockHelperThreadState& lock); |
| 407 |
HelperThreadTask* maybeGetPromiseHelperTask( |
| 408 |
const AutoLockHelperThreadState& lock); |
| 409 |
HelperThreadTask* maybeGetBaselineCompileTask( |
| 410 |
const AutoLockHelperThreadState& lock); |
| 411 |
HelperThreadTask* maybeGetIonCompileTask( |
| 412 |
const AutoLockHelperThreadState& lock); |
| 413 |
HelperThreadTask* maybeGetLowPrioIonCompileTask( |
| 414 |
const AutoLockHelperThreadState& lock); |
| 415 |
HelperThreadTask* maybeGetIonFreeTask(const AutoLockHelperThreadState& lock); |
| 416 |
HelperThreadTask* maybeGetFreeDelazifyTask( |
| 417 |
const AutoLockHelperThreadState& lock); |
| 418 |
HelperThreadTask* maybeGetDelazifyTask(const AutoLockHelperThreadState& lock); |
| 419 |
HelperThreadTask* maybeGetCompressionTask( |
| 420 |
const AutoLockHelperThreadState& lock); |
| 421 |
HelperThreadTask* maybeGetGCParallelTask( |
| 422 |
const AutoLockHelperThreadState& lock); |
| 423 |
|
| 424 |
jit::IonCompileTask* highestPriorityPendingIonCompile( |
| 425 |
const AutoLockHelperThreadState& lock, bool checkExecutionStatus); |
| 426 |
|
| 427 |
bool checkTaskThreadLimit(ThreadType threadType, size_t maxThreads, |
| 428 |
bool isMaster, |
| 429 |
const AutoLockHelperThreadState& lock) const; |
| 430 |
bool checkTaskThreadLimit(ThreadType threadType, size_t maxThreads, |
| 431 |
const AutoLockHelperThreadState& lock) const { |
| 432 |
return checkTaskThreadLimit(threadType, maxThreads, /* isMaster */ false, |
| 433 |
lock); |
| 434 |
} |
| 435 |
|
| 436 |
bool hasActiveThreads(const AutoLockHelperThreadState&); |
| 437 |
bool canStartTasks(const AutoLockHelperThreadState& locked); |
| 438 |
|
| 439 |
public: |
| 440 |
// Used by a major GC to signal processing enqueued compression tasks. |
| 441 |
enum class ScheduleCompressionTask { GC, API }; |
| 442 |
void startHandlingCompressionTasks(ScheduleCompressionTask schedule, |
| 443 |
JSRuntime* maybeRuntime, |
| 444 |
const AutoLockHelperThreadState& lock); |
| 445 |
|
| 446 |
void runPendingSourceCompressions(JSRuntime* runtime, |
| 447 |
AutoLockHelperThreadState& lock); |
| 448 |
|
| 449 |
void trace(JSTracer* trc); |
| 450 |
|
| 451 |
void waitForAllTasks(); |
| 452 |
void waitForAllTasksLocked(AutoLockHelperThreadState&); |
| 453 |
|
| 454 |
#ifdef DEBUG |
| 455 |
bool hasOffThreadIonCompile(Zone* zone, AutoLockHelperThreadState& lock); |
| 456 |
#endif |
| 457 |
|
| 458 |
void cancelOffThreadBaselineCompile(const CompilationSelector& selector); |
| 459 |
void cancelOffThreadIonCompile(const CompilationSelector& selector); |
| 460 |
void cancelOffThreadWasmCompleteTier2Generator( |
| 461 |
AutoLockHelperThreadState& lock); |
| 462 |
void cancelOffThreadWasmPartialTier2Compile(AutoLockHelperThreadState& lock); |
| 463 |
|
| 464 |
bool hasAnyDelazifyTask(JSRuntime* rt, AutoLockHelperThreadState& lock); |
| 465 |
void cancelPendingDelazifyTask(JSRuntime* rt, |
| 466 |
AutoLockHelperThreadState& lock); |
| 467 |
void waitUntilCancelledDelazifyTasks(JSRuntime* rt, |
| 468 |
AutoLockHelperThreadState& lock); |
| 469 |
void waitUntilEmptyFreeDelazifyTaskVector(AutoLockHelperThreadState& lock); |
| 470 |
|
| 471 |
void cancelOffThreadCompressions(JSRuntime* runtime, |
| 472 |
AutoLockHelperThreadState& lock); |
| 473 |
|
| 474 |
void triggerFreeUnusedMemory(); |
| 475 |
|
| 476 |
bool submitTask(wasm::UniqueCompleteTier2GeneratorTask task); |
| 477 |
bool submitTask(wasm::UniquePartialTier2CompileTask task); |
| 478 |
bool submitTask(wasm::CompileTask* task, wasm::CompileState state); |
| 479 |
bool submitTask(jit::BaselineCompileTask* task, |
| 480 |
const AutoLockHelperThreadState& locked); |
| 481 |
bool submitTask(UniquePtr<jit::IonFreeTask>&& task, |
| 482 |
const AutoLockHelperThreadState& lock); |
| 483 |
bool submitTask(jit::IonCompileTask* task, |
| 484 |
const AutoLockHelperThreadState& locked); |
| 485 |
bool submitTask(UniquePtr<SourceCompressionTask> task, |
| 486 |
const AutoLockHelperThreadState& locked); |
| 487 |
void submitTask(DelazifyTask* task, const AutoLockHelperThreadState& locked); |
| 488 |
bool submitTask(UniquePtr<FreeDelazifyTask> task, |
| 489 |
const AutoLockHelperThreadState& locked); |
| 490 |
bool submitTask(PromiseHelperTask* task); |
| 491 |
bool submitTask(GCParallelTask* task, |
| 492 |
const AutoLockHelperThreadState& locked); |
| 493 |
|
| 494 |
void runOneTask(HelperThreadTask* task, AutoLockHelperThreadState& lock); |
| 495 |
void dispatch(const AutoLockHelperThreadState& locked); |
| 496 |
|
| 497 |
private: |
| 498 |
HelperThreadTask* findHighestPriorityTask( |
| 499 |
const AutoLockHelperThreadState& locked); |
| 500 |
|
| 501 |
void runTaskLocked(HelperThreadTask* task, AutoLockHelperThreadState& lock); |
| 502 |
|
| 503 |
using Selector = HelperThreadTask* ( |
| 504 |
GlobalHelperThreadState::*)(const AutoLockHelperThreadState&); |
| 505 |
static const Selector selectors[]; |
| 506 |
}; |
| 507 |
|
| 508 |
static inline bool IsHelperThreadStateInitialized() { |
| 509 |
extern GlobalHelperThreadState* gHelperThreadState; |
| 510 |
return gHelperThreadState; |
| 511 |
} |
| 512 |
|
| 513 |
static inline GlobalHelperThreadState& HelperThreadState() { |
| 514 |
extern GlobalHelperThreadState* gHelperThreadState; |
| 515 |
|
| 516 |
MOZ_ASSERT(gHelperThreadState); |
| 517 |
return *gHelperThreadState; |
| 518 |
} |
| 519 |
|
| 520 |
// Eagerly delazify functions, and send the result back to the runtime which |
| 521 |
// requested the stencil to be parsed, by filling the stencil cache. |
| 522 |
// |
| 523 |
// This task is scheduled multiple times, each time it is scheduled, it |
| 524 |
// delazifies a single function. Once the function is delazified, it schedules |
| 525 |
// the inner functions of the delazified function for delazification using the |
| 526 |
// DelazifyStrategy. The DelazifyStrategy is responsible for ordering and |
| 527 |
// filtering functions to be delazified. |
| 528 |
// |
| 529 |
// When no more function have to be delazified, a FreeDelazifyTask is scheduled |
| 530 |
// to remove the memory held by the DelazifyTask. |
| 531 |
struct DelazifyTask : public mozilla::LinkedListElement<DelazifyTask>, |
| 532 |
public HelperThreadTask { |
| 533 |
// HelperThreads are shared between all runtimes in the process so explicitly |
| 534 |
// track which one we are associated with. |
| 535 |
JSRuntime* maybeRuntime = nullptr; |
| 536 |
|
| 537 |
DelazificationContext delazificationCx; |
| 538 |
|
| 539 |
// Create a new DelazifyTask and initialize it. |
| 540 |
// |
| 541 |
// In case of early failure, no errors are reported, as a DelazifyTask is an |
| 542 |
// optimization and the VM should remain working even without this |
| 543 |
// optimization in place. |
| 544 |
static UniquePtr<DelazifyTask> Create( |
| 545 |
JSRuntime* maybeRuntime, const JS::ReadOnlyCompileOptions& options, |
| 546 |
frontend::InitialStencilAndDelazifications* stencils); |
| 547 |
|
| 548 |
DelazifyTask(JSRuntime* maybeRuntime, |
| 549 |
const JS::PrefableCompileOptions& initialPrefableOptions); |
| 550 |
~DelazifyTask(); |
| 551 |
|
| 552 |
[[nodiscard]] bool init(const JS::ReadOnlyCompileOptions& options, |
| 553 |
frontend::InitialStencilAndDelazifications* stencils); |
| 554 |
|
| 555 |
bool runtimeMatchesOrNoRuntime(JSRuntime* rt) { |
| 556 |
return !maybeRuntime || maybeRuntime == rt; |
| 557 |
} |
| 558 |
|
| 559 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 560 |
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { |
| 561 |
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf); |
| 562 |
} |
| 563 |
|
| 564 |
void runHelperThreadTask(AutoLockHelperThreadState& locked) override; |
| 565 |
[[nodiscard]] bool runTask(); |
| 566 |
ThreadType threadType() override { return ThreadType::THREAD_TYPE_DELAZIFY; } |
| 567 |
|
| 568 |
bool done() const; |
| 569 |
|
| 570 |
const char* getName() override { return "DelazifyTask"; } |
| 571 |
}; |
| 572 |
|
| 573 |
// The FreeDelazifyTask exists as this is a bad practice to `js_delete(this)`, |
| 574 |
// as fields might be aliased across the destructor, such as with RAII guards. |
| 575 |
// The FreeDelazifyTask limits the risk of adding these kind of issues by |
| 576 |
// limiting the number of fields to the DelazifyTask pointer, before deleting |
| 577 |
// it-self. |
| 578 |
struct FreeDelazifyTask : public HelperThreadTask { |
| 579 |
DelazifyTask* task; |
| 580 |
|
| 581 |
explicit FreeDelazifyTask(DelazifyTask* t) : task(t) {} |
| 582 |
void runHelperThreadTask(AutoLockHelperThreadState& locked) override; |
| 583 |
ThreadType threadType() override { |
| 584 |
return ThreadType::THREAD_TYPE_DELAZIFY_FREE; |
| 585 |
} |
| 586 |
|
| 587 |
const char* getName() override { return "FreeDelazifyTask"; } |
| 588 |
}; |
| 589 |
|
| 590 |
// It is not desirable to eagerly compress: if lazy functions that are tied to |
| 591 |
// the ScriptSource were to be executed relatively soon after parsing, they |
| 592 |
// would need to block on decompression, which hurts responsiveness. |
| 593 |
// |
| 594 |
// To this end, compression tasks are heap allocated and enqueued in a pending |
| 595 |
// list by ScriptSource::setSourceCopy. When a major GC occurs, we schedule |
| 596 |
// pending compression tasks and move the ones that are ready to be compressed |
| 597 |
// to the worklist. Currently, a compression task is considered ready 2 major |
| 598 |
// GCs after being enqueued. Completed tasks are handled during the sweeping |
| 599 |
// phase by AttachCompressedSourcesTask, which runs in parallel with other GC |
| 600 |
// sweeping tasks. |
| 601 |
class SourceCompressionTask : public HelperThreadTask { |
| 602 |
friend class HelperThread; |
| 603 |
friend class ScriptSource; |
| 604 |
|
| 605 |
// The runtime that the ScriptSource is associated with, in the sense that |
| 606 |
// it uses the runtime's immutable string cache. |
| 607 |
JSRuntime* runtime_; |
| 608 |
|
| 609 |
// The major GC number of the runtime when the task was enqueued. |
| 610 |
uint64_t majorGCNumber_; |
| 611 |
|
| 612 |
// The source to be compressed. |
| 613 |
RefPtr<ScriptSource> source_; |
| 614 |
|
| 615 |
// The resultant compressed string. If the compressed string is larger |
| 616 |
// than the original, or we OOM'd during compression, or nothing else |
| 617 |
// except the task is holding the ScriptSource alive when scheduled to |
| 618 |
// compress, this will remain None upon completion. |
| 619 |
SharedImmutableString resultString_; |
| 620 |
|
| 621 |
public: |
| 622 |
// The majorGCNumber is used for scheduling tasks. |
| 623 |
SourceCompressionTask(JSRuntime* rt, ScriptSource* source) |
| 624 |
: runtime_(rt), majorGCNumber_(rt->gc.majorGCCount()), source_(source) { |
| 625 |
source->noteSourceCompressionTask(); |
| 626 |
} |
| 627 |
virtual ~SourceCompressionTask() = default; |
| 628 |
|
| 629 |
bool runtimeMatches(JSRuntime* runtime) const { return runtime == runtime_; } |
| 630 |
bool shouldStart() const { |
| 631 |
// We wait 2 major GCs to start compressing, in order to avoid immediate |
| 632 |
// compression. If the script source has no other references then don't |
| 633 |
// compress it and let SweepPendingCompressions remove this task. |
| 634 |
return !shouldCancel() && runtime_->gc.majorGCCount() > majorGCNumber_ + 1; |
| 635 |
} |
| 636 |
|
| 637 |
bool shouldCancel() const { |
| 638 |
// If the refcount is exactly 1, then nothing else is holding on to the |
| 639 |
// ScriptSource, so no reason to compress it and we should cancel the task. |
| 640 |
return source_->refs == 1; |
| 641 |
} |
| 642 |
|
| 643 |
void runTask(); |
| 644 |
void runHelperThreadTask(AutoLockHelperThreadState& locked) override; |
| 645 |
void complete(); |
| 646 |
|
| 647 |
ThreadType threadType() override { return ThreadType::THREAD_TYPE_COMPRESS; } |
| 648 |
|
| 649 |
const char* getName() override { return "SourceCompressionTask"; } |
| 650 |
|
| 651 |
private: |
| 652 |
struct PerformTaskWork; |
| 653 |
friend struct PerformTaskWork; |
| 654 |
|
| 655 |
// The work algorithm, aware whether it's compressing one-byte UTF-8 source |
| 656 |
// text or UTF-16, for CharT either Utf8Unit or char16_t. Invoked by |
| 657 |
// work() after doing a type-test of the ScriptSource*. |
| 658 |
template <typename CharT> |
| 659 |
void workEncodingSpecific(); |
| 660 |
}; |
| 661 |
|
| 662 |
// A PromiseHelperTask is an OffThreadPromiseTask that executes a single job on |
| 663 |
// a helper thread. Call js::StartOffThreadPromiseHelperTask to submit a |
| 664 |
// PromiseHelperTask for execution. |
| 665 |
// |
| 666 |
// Concrete subclasses must implement execute and OffThreadPromiseTask::resolve. |
| 667 |
// The helper thread will call execute() to do the main work. Then, the thread |
| 668 |
// of the JSContext used to create the PromiseHelperTask will call resolve() to |
| 669 |
// resolve promise according to those results. |
| 670 |
struct PromiseHelperTask : OffThreadPromiseTask, public HelperThreadTask { |
| 671 |
PromiseHelperTask(JSContext* cx, JS::Handle<PromiseObject*> promise) |
| 672 |
: OffThreadPromiseTask(cx, promise) {} |
| 673 |
|
| 674 |
// To be called on a helper thread and implemented by the derived class. |
| 675 |
virtual void execute() = 0; |
| 676 |
|
| 677 |
// May be called in the absence of helper threads or off-thread promise |
| 678 |
// support to synchronously execute and resolve a PromiseTask. |
| 679 |
// |
| 680 |
// Warning: After this function returns, 'this' can be deleted at any time, so |
| 681 |
// the caller must immediately return from the stream callback. |
| 682 |
void executeAndResolveAndDestroy(JSContext* cx); |
| 683 |
|
| 684 |
void runHelperThreadTask(AutoLockHelperThreadState& locked) override; |
| 685 |
ThreadType threadType() override { return THREAD_TYPE_PROMISE_TASK; } |
| 686 |
|
| 687 |
const char* getName() override { return "PromiseHelperTask"; } |
| 688 |
}; |
| 689 |
|
| 690 |
} /* namespace js */ |
| 691 |
|
| 692 |
#endif /* vm_HelperThreadState_h */ |