Debian Codegraph @

js/src/vm/HelperThreads.h

raw
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 * API for managing off-thread work.
9 */
10
11 #ifndef vm_HelperThreads_h
12 #define vm_HelperThreads_h
13
14 #include "mozilla/Variant.h"
15
16 #include "js/AllocPolicy.h"
17 #include "js/HelperThreadAPI.h"
18 #include "js/shadow/Zone.h"
19 #include "js/UniquePtr.h"
20 #include "js/Vector.h"
21 #include "threading/LockGuard.h"
22 #include "threading/Mutex.h"
23 #include "wasm/WasmConstants.h"
24
25 namespace mozilla {
26 union Utf8Unit;
27 }
28
29 namespace JS {
30 class JS_PUBLIC_API ReadOnlyCompileOptions;
31 class JS_PUBLIC_API ReadOnlyDecodeOptions;
32 class Zone;
33
34 template <typename UnitT>
35 class SourceText;
36 } // namespace JS
37
38 namespace js {
39
40 class AutoLockHelperThreadState;
41 struct PromiseHelperTask;
42 class SourceCompressionTask;
43
44 namespace frontend {
45 struct InitialStencilAndDelazifications;
46 }
47
48 namespace gc {
49 class GCRuntime;
50 }
51
52 namespace jit {
53 class BaselineCompileTask;
54 class IonCompileTask;
55 class IonFreeTask;
56 class JitRuntime;
57 using IonFreeCompileTasks = Vector<IonCompileTask*, 8, SystemAllocPolicy>;
58 } // namespace jit
59
60 namespace wasm {
61 struct CompileTask;
62 struct CompileTaskState;
63 struct CompleteTier2GeneratorTask;
64 using UniqueCompleteTier2GeneratorTask = UniquePtr<CompleteTier2GeneratorTask>;
65 struct PartialTier2CompileTask;
66 using UniquePartialTier2CompileTask = UniquePtr<PartialTier2CompileTask>;
67 } // namespace wasm
68
69 /*
70 * Lock protecting all mutable shared state accessed by helper threads, and used
71 * by all condition variables.
72 */
73 extern Mutex gHelperThreadLock MOZ_UNANNOTATED;
74
75 // Set of tasks to dispatch when the helper thread state lock is released.
76 class AutoHelperTaskQueue {
77 public:
78 ~AutoHelperTaskQueue() { dispatchQueuedTasks(); }
79 bool hasQueuedTasks() const { return !tasksToDispatch.empty(); }
80 void queueTaskToDispatch(JS::HelperThreadTask* task) const;
81 void dispatchQueuedTasks();
82
83 private:
84 // TODO: Convert this to use a linked list.
85 mutable Vector<JS::HelperThreadTask*, 1, SystemAllocPolicy> tasksToDispatch;
86 };
87
88 // A lock guard for data protected by the helper thread lock.
89 //
90 // This can also queue helper thread tasks to be triggered when the lock is
91 // released.
92 class MOZ_RAII AutoLockHelperThreadState
93 : public AutoHelperTaskQueue, // Must come before LockGuard.
94 public LockGuard<Mutex> {
95 public:
96 AutoLockHelperThreadState() : LockGuard<Mutex>(gHelperThreadLock) {}
97 AutoLockHelperThreadState(const AutoLockHelperThreadState&) = delete;
98
99 private:
100 friend class UnlockGuard<AutoLockHelperThreadState>;
101 void unlock() {
102 LockGuard<Mutex>::unlock();
103 dispatchQueuedTasks();
104 }
105
106 friend class GlobalHelperThreadState;
107 };
108
109 using AutoUnlockHelperThreadState = UnlockGuard<AutoLockHelperThreadState>;
110
111 // Create data structures used by helper threads.
112 bool CreateHelperThreadsState();
113
114 // Destroy data structures used by helper threads.
115 void DestroyHelperThreadsState();
116
117 // Initialize helper threads unless already initialized.
118 bool EnsureHelperThreadsInitialized();
119
120 size_t GetHelperThreadCount();
121 size_t GetHelperThreadCPUCount();
122 size_t GetMaxWasmCompilationThreads();
123
124 // This allows the JS shell to override GetCPUCount() when passed the
125 // --thread-count=N option.
126 bool SetFakeCPUCount(size_t count);
127
128 // Enqueues a wasm compilation task.
129 bool StartOffThreadWasmCompile(wasm::CompileTask* task,
130 wasm::CompileState state);
131
132 // Remove any pending wasm compilation tasks queued with
133 // StartOffThreadWasmCompile that match the arguments. Return the number
134 // removed.
135 size_t RemovePendingWasmCompileTasks(const wasm::CompileTaskState& taskState,
136 wasm::CompileState state,
137 const AutoLockHelperThreadState& lock);
138
139 // Enqueues a wasm Complete Tier-2 compilation task. This (logically, at
140 // least) manages a set of sub-tasks that perform compilation of groups of
141 // functions.
142 void StartOffThreadWasmCompleteTier2Generator(
143 wasm::UniqueCompleteTier2GeneratorTask task);
144
145 // Enqueues a wasm Partial Tier-2 compilation task. This compiles one
146 // function, doing so itself, without any sub-tasks.
147 void StartOffThreadWasmPartialTier2Compile(
148 wasm::UniquePartialTier2CompileTask task);
149
150 // Cancel all background Wasm Complete Tier-2 compilations, both the generator
151 // task and the individual compilation tasks.
152 void CancelOffThreadWasmCompleteTier2Generator();
153
154 // Cancel a single background Wasm Partial Tier-2 compilation.
155 void CancelOffThreadWasmPartialTier2Compile();
156
157 /*
158 * If helper threads are available, call execute() then dispatchResolve() on the
159 * given task in a helper thread. If no helper threads are available, the given
160 * task is executed and resolved synchronously.
161 *
162 * This function takes ownership of task unconditionally; if it fails, task is
163 * deleted.
164 */
165 bool StartOffThreadPromiseHelperTask(JSContext* cx,
166 UniquePtr<PromiseHelperTask> task);
167
168 /*
169 * Like the JSContext-accepting version, but only safe to use when helper
170 * threads are available, so we can be sure we'll never need to fall back on
171 * synchronous execution.
172 *
173 * This function can be called from any thread, but takes ownership of the task
174 * only on success. On OOM, it is the caller's responsibility to arrange for the
175 * task to be cleaned up properly.
176 */
177 bool StartOffThreadPromiseHelperTask(PromiseHelperTask* task);
178
179 /*
180 * Schedule an off-thread Baseline compilation for a script, given a task.
181 */
182 bool StartOffThreadBaselineCompile(jit::BaselineCompileTask* task,
183 const AutoLockHelperThreadState& lock);
184
185 void FinishOffThreadBaselineCompile(jit::BaselineCompileTask* task,
186 const AutoLockHelperThreadState& lock);
187
188 /*
189 * Schedule an off-thread Ion compilation for a script, given a task.
190 */
191 bool StartOffThreadIonCompile(jit::IonCompileTask* task,
192 const AutoLockHelperThreadState& lock);
193
194 void FinishOffThreadIonCompile(jit::IonCompileTask* task,
195 const AutoLockHelperThreadState& lock);
196
197 // RAII class to handle batching compile tasks and starting an IonFreeTask.
198 class MOZ_RAII AutoStartIonFreeTask {
199 jit::JitRuntime* jitRuntime_;
200
201 // If true, start an IonFreeTask even if the batch is small.
202 bool force_;
203
204 public:
205 explicit AutoStartIonFreeTask(jit::JitRuntime* jitRuntime, bool force = false)
206 : jitRuntime_(jitRuntime), force_(force) {}
207 ~AutoStartIonFreeTask();
208
209 [[nodiscard]] bool addIonCompileToFreeTaskBatch(jit::IonCompileTask* task);
210 };
211
212 struct ZonesInState {
213 JSRuntime* runtime;
214 JS::shadow::Zone::GCState state;
215 };
216
217 using CompilationSelector =
218 mozilla::Variant<JSScript*, JS::Zone*, ZonesInState, JSRuntime*>;
219
220 /*
221 * Cancel scheduled or in progress Ion compilations.
222 */
223 void CancelOffThreadIonCompile(const CompilationSelector& selector);
224
225 inline void CancelOffThreadIonCompile(JSScript* script) {
226 CancelOffThreadIonCompile(CompilationSelector(script));
227 }
228
229 inline void CancelOffThreadIonCompile(JS::Zone* zone) {
230 CancelOffThreadIonCompile(CompilationSelector(zone));
231 }
232
233 inline void CancelOffThreadIonCompile(JSRuntime* runtime,
234 JS::shadow::Zone::GCState state) {
235 CancelOffThreadIonCompile(CompilationSelector(ZonesInState{runtime, state}));
236 }
237
238 inline void CancelOffThreadIonCompile(JSRuntime* runtime) {
239 CancelOffThreadIonCompile(CompilationSelector(runtime));
240 }
241
242 #ifdef DEBUG
243 bool HasOffThreadIonCompile(JS::Zone* zone);
244 #endif
245
246 /*
247 * Cancel scheduled or in progress Baseline compilations.
248 */
249 void CancelOffThreadBaselineCompile(const CompilationSelector& selector);
250
251 inline void CancelOffThreadBaselineCompile(JSScript* script) {
252 CancelOffThreadBaselineCompile(CompilationSelector(script));
253 }
254
255 inline void CancelOffThreadBaselineCompile(JS::Zone* zone) {
256 CancelOffThreadBaselineCompile(CompilationSelector(zone));
257 }
258
259 inline void CancelOffThreadBaselineCompile(JSRuntime* runtime,
260 JS::shadow::Zone::GCState state) {
261 CancelOffThreadBaselineCompile(
262 CompilationSelector(ZonesInState{runtime, state}));
263 }
264
265 inline void CancelOffThreadBaselineCompile(JSRuntime* runtime) {
266 CancelOffThreadBaselineCompile(CompilationSelector(runtime));
267 }
268
269 /*
270 * Cancel baseline and Ion compilations.
271 */
272 inline void CancelOffThreadCompile(JSRuntime* runtime,
273 JS::shadow::Zone::GCState state) {
274 CancelOffThreadBaselineCompile(
275 CompilationSelector(ZonesInState{runtime, state}));
276 CancelOffThreadIonCompile(CompilationSelector(ZonesInState{runtime, state}));
277 }
278
279 inline void CancelOffThreadCompile(JSRuntime* runtime) {
280 CancelOffThreadBaselineCompile(runtime);
281 CancelOffThreadIonCompile(runtime);
282 }
283
284 /*
285 * Cancel all scheduled or in progress eager delazification phases for a
286 * runtime.
287 */
288 void CancelOffThreadDelazify(JSRuntime* runtime);
289
290 /*
291 * Wait for all delazification to complete.
292 */
293 void WaitForAllDelazifyTasks(JSRuntime* rt);
294
295 // Start off-thread delazification task, to race the delazification of inner
296 // functions.
297 void StartOffThreadDelazification(
298 JSContext* maybeCx, const JS::ReadOnlyCompileOptions& options,
299 frontend::InitialStencilAndDelazifications* stencils);
300
301 // Drain the task queues and wait for all helper threads to finish running.
302 //
303 // Note that helper threads are shared between runtimes and it's possible that
304 // another runtime could saturate the helper thread system and cause this to
305 // never return.
306 void WaitForAllHelperThreads();
307 void WaitForAllHelperThreads(AutoLockHelperThreadState& lock);
308
309 // Enqueue a compression job to be processed later. These are started at the
310 // start of the major GC after the next one.
311 bool EnqueueOffThreadCompression(JSContext* cx,
312 UniquePtr<SourceCompressionTask> task);
313
314 // Start handling any compression tasks for this runtime. Called at the start of
315 // major GC.
316 void StartHandlingCompressionsOnGC(JSRuntime* rt);
317
318 // Cancel all scheduled, in progress, or finished compression tasks for
319 // runtime.
320 void CancelOffThreadCompressions(JSRuntime* runtime);
321
322 void AttachFinishedCompressions(JSRuntime* runtime,
323 AutoLockHelperThreadState& lock);
324
325 // Sweep pending tasks that are holding onto should-be-dead ScriptSources.
326 void SweepPendingCompressions(AutoLockHelperThreadState& lock);
327
328 // Run all pending source compression tasks synchronously, for testing purposes
329 void RunPendingSourceCompressions(JSRuntime* runtime);
330
331 // False if the off-thread source compression mechanism isn't being used. This
332 // happens on low core count machines where we are concerned about blocking
333 // main-thread execution.
334 bool IsOffThreadSourceCompressionEnabled();
335
336 void AttachFinishedBaselineCompilations(JSContext* cx,
337 AutoLockHelperThreadState& lock);
338
339 } // namespace js
340
341 #endif /* vm_HelperThreads_h */