Bug 1911021 - Add an API tp CycleCollectedJSContext to enable/disable tracing r=smaug

Basically, in order to allow turning on JS execution tracing from a convenient place
within the profiler, we want a threadsafe endpoint hanging off of the
`CycleCollectedJSContext`. The profiler holds onto a pointer to the `CycleCollectedJSContext`
controlled via `PROFILER_SET|CLEAR_JS_CONTEXT` and calls this from a background thread
when profiling starts, or from the cx's own thread if the profiler is already running
when it is registered.

Differential Revision: https://phabricator.services.mozilla.com/D222755
This commit is contained in:
alexical 2024-10-10 17:38:16 +00:00
parent 993ed9b460
commit bc46fe7d3c
2 changed files with 42 additions and 0 deletions

View File

@ -827,6 +827,35 @@ nsresult CycleCollectedJSContext::NotifyUnhandledRejections::Cancel() {
return NS_OK;
}
#ifdef MOZ_EXECUTION_TRACING
void CycleCollectedJSContext::BeginExecutionTracingAsync() {
mOwningThread->Dispatch(NS_NewRunnableFunction(
"CycleCollectedJSContext::BeginExecutionTracingAsync", [] {
CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get();
if (ccjs) {
JS_TracerBeginTracing(ccjs->Context());
}
}));
}
void CycleCollectedJSContext::EndExecutionTracingAsync() {
mOwningThread->Dispatch(NS_NewRunnableFunction(
"CycleCollectedJSContext::EndExecutionTracingAsync", [] {
CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get();
if (ccjs) {
JS_TracerEndTracing(ccjs->Context());
}
}));
}
#else
void CycleCollectedJSContext::BeginExecutionTracingAsync() {}
void CycleCollectedJSContext::EndExecutionTracingAsync() {}
#endif
class FinalizationRegistryCleanup::CleanupRunnable
: public DiscardableRunnable {
public:

View File

@ -279,6 +279,19 @@ class CycleCollectedJSContext : dom::PerThreadAtomCache, private JS::JobQueue {
MOZ_ASSERT_UNREACHABLE("Not supported");
}
// These two functions control a special flag variable which lets us turn
// tracing on and off from a thread other than this JSContext's main thread.
// This is useful because we want to be able to start tracing many threads
// all at once from the Gecko Profiler in Firefox.
//
// NOTE: the caller must ensure that this CycleCollectedJSContext is not
// being destroyed when this is called. At the time of this API being added,
// the only consumer is the Gecko Profiler, which guarantees this via a mutex
// around unregistering the context, which always occurs before the context
// is destroyed.
void BeginExecutionTracingAsync();
void EndExecutionTracingAsync();
private:
// JS::JobQueue implementation: see js/public/Promise.h.
// SpiderMonkey uses some of these methods to enqueue promise resolution jobs.