Bug 1809861 - Part 1: Simplify and clean up off-main thread script compilation r=smaug

The main change in this patch is to prevent access to main-thread objects
while off-thread. This is done by using nsMainThreadPtrHandle to wrap main
thread pointers in the runnable. This prevents access to their targets when
off thread and ensure they are only released on the main thread.

This means that mRunnable is now only accessed on the main thread and so it
doesn't need to be atomic and can be a normal RefPtr. We also don't need to
leak a reference to it in AttemptOffThreadScriptCompile.

This also requires that timing data is moved to the runnable.

Cancellation should always have happened by unlink or destruction of
ScriptLoadContext so handling for that is removed.

Differential Revision: https://phabricator.services.mozilla.com/D166667
This commit is contained in:
Jon Coppeard
2023-01-17 17:06:50 +00:00
parent b11c3c27de
commit f23f44668a
4 changed files with 151 additions and 177 deletions

View File

@@ -1387,19 +1387,20 @@ ReferrerPolicy ScriptLoader::GetReferrerPolicy(nsIScriptElement* aElement) {
namespace {
class NotifyOffThreadScriptLoadCompletedRunnable : public Runnable {
RefPtr<ScriptLoadRequest> mRequest;
RefPtr<ScriptLoader> mLoader;
nsMainThreadPtrHandle<ScriptLoadRequest> mRequest;
nsMainThreadPtrHandle<ScriptLoader> mLoader;
nsCOMPtr<nsISerialEventTarget> mEventTarget;
JS::OffThreadToken* mToken;
TimeStamp mStartTime;
TimeStamp mStopTime;
public:
ScriptLoadRequest* GetScriptLoadRequest() { return mRequest; }
NotifyOffThreadScriptLoadCompletedRunnable(ScriptLoadRequest* aRequest,
ScriptLoader* aLoader)
: Runnable("dom::NotifyOffThreadScriptLoadCompletedRunnable"),
mRequest(aRequest),
mLoader(aLoader),
mRequest(
new nsMainThreadPtrHolder<ScriptLoadRequest>("mRequest", aRequest)),
mLoader(new nsMainThreadPtrHolder<ScriptLoader>("mLoader", aLoader)),
mToken(nullptr) {
MOZ_ASSERT(NS_IsMainThread());
if (DocGroup* docGroup = aLoader->GetDocGroup()) {
@@ -1407,7 +1408,8 @@ class NotifyOffThreadScriptLoadCompletedRunnable : public Runnable {
}
}
virtual ~NotifyOffThreadScriptLoadCompletedRunnable();
void RecordStartTime() { mStartTime = TimeStamp::Now(); }
void RecordStopTime() { mStopTime = TimeStamp::Now(); }
void SetToken(JS::OffThreadToken* aToken) {
MOZ_ASSERT(aToken && !mToken);
@@ -1466,7 +1468,7 @@ void ScriptLoader::CancelScriptLoadRequests() {
}
nsresult ScriptLoader::ProcessOffThreadRequest(ScriptLoadRequest* aRequest) {
MOZ_ASSERT(aRequest->mState == ScriptLoadRequest::State::Compiling);
MOZ_ASSERT(aRequest->IsCompiling());
MOZ_ASSERT(!aRequest->GetScriptLoadContext()->mWasCompiledOMT);
if (aRequest->IsCanceled()) {
@@ -1526,81 +1528,56 @@ nsresult ScriptLoader::ProcessOffThreadRequest(ScriptLoadRequest* aRequest) {
return NS_OK;
}
NotifyOffThreadScriptLoadCompletedRunnable::
~NotifyOffThreadScriptLoadCompletedRunnable() {
if (MOZ_UNLIKELY(mRequest || mLoader) && !NS_IsMainThread()) {
NS_ReleaseOnMainThread(
"NotifyOffThreadScriptLoadCompletedRunnable::mRequest",
mRequest.forget());
NS_ReleaseOnMainThread(
"NotifyOffThreadScriptLoadCompletedRunnable::mLoader",
mLoader.forget());
}
}
NS_IMETHODIMP
NotifyOffThreadScriptLoadCompletedRunnable::Run() {
MOZ_ASSERT(NS_IsMainThread());
// We want these to be dropped on the main thread, once we return from this
// function.
RefPtr<ScriptLoadRequest> request = std::move(mRequest);
RefPtr<ScriptLoadContext> context = mRequest->GetScriptLoadContext();
MOZ_ASSERT_IF(context->mRunnable, context->mRunnable == this);
MOZ_ASSERT_IF(context->mOffThreadToken, context->mOffThreadToken == mToken);
// Runnable pointer should have been cleared in the offthread callback.
MOZ_ASSERT(!request->GetScriptLoadContext()->mRunnable);
// Clear the pointer to the runnable. The final reference will be released
// when this method returns.
context->mRunnable = nullptr;
if (!context->mOffThreadToken) {
// Request has been cancelled by MaybeCancelOffThreadScript.
return NS_OK;
}
if (profiler_is_active()) {
ProfilerString8View scriptSourceString;
if (request->IsTextSource()) {
if (mRequest->IsTextSource()) {
scriptSourceString = "ScriptCompileOffThread";
} else {
MOZ_ASSERT(request->IsBytecode());
MOZ_ASSERT(mRequest->IsBytecode());
scriptSourceString = "BytecodeDecodeOffThread";
}
nsAutoCString profilerLabelString;
request->GetScriptLoadContext()->GetProfilerLabel(profilerLabelString);
PROFILER_MARKER_TEXT(
scriptSourceString, JS,
MarkerTiming::Interval(
request->GetScriptLoadContext()->mOffThreadParseStartTime,
request->GetScriptLoadContext()->mOffThreadParseStopTime),
profilerLabelString);
mRequest->GetScriptLoadContext()->GetProfilerLabel(profilerLabelString);
PROFILER_MARKER_TEXT(scriptSourceString, JS,
MarkerTiming::Interval(mStartTime, mStopTime),
profilerLabelString);
}
RefPtr<ScriptLoader> loader = std::move(mLoader);
nsresult rv = mLoader->ProcessOffThreadRequest(mRequest);
// Request was already cancelled at some earlier point.
if (!request->GetScriptLoadContext()->mOffThreadToken) {
return NS_OK;
}
return loader->ProcessOffThreadRequest(request);
mRequest = nullptr;
mLoader = nullptr;
return rv;
}
static void OffThreadScriptLoaderCallback(JS::OffThreadToken* aToken,
void* aCallbackData) {
RefPtr<NotifyOffThreadScriptLoadCompletedRunnable> aRunnable = dont_AddRef(
static_cast<NotifyOffThreadScriptLoadCompletedRunnable*>(aCallbackData));
MOZ_ASSERT(
aRunnable.get() ==
aRunnable->GetScriptLoadRequest()->GetScriptLoadContext()->mRunnable);
aRunnable->GetScriptLoadRequest()
->GetScriptLoadContext()
->mOffThreadParseStopTime = TimeStamp::Now();
RefPtr<NotifyOffThreadScriptLoadCompletedRunnable> aRunnable =
static_cast<NotifyOffThreadScriptLoadCompletedRunnable*>(aCallbackData);
LogRunnable::Run run(aRunnable);
aRunnable->RecordStopTime();
aRunnable->SetToken(aToken);
// If mRunnable was cleared then request was canceled so do nothing.
if (!aRunnable->GetScriptLoadRequest()
->GetScriptLoadContext()
->mRunnable.exchange(nullptr)) {
return;
}
NotifyOffThreadScriptLoadCompletedRunnable::Dispatch(aRunnable.forget());
}
@@ -1660,108 +1637,25 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
RefPtr<NotifyOffThreadScriptLoadCompletedRunnable> runnable =
new NotifyOffThreadScriptLoadCompletedRunnable(aRequest, this);
// Emulate dispatch. CompileOffThreadModule will call
// Emulate dispatch. CompileOffThreadModule will call
// OffThreadScriptLoaderCallback were we will emulate run.
LogRunnable::LogDispatch(runnable);
aRequest->GetScriptLoadContext()->mOffThreadParseStartTime = TimeStamp::Now();
runnable->RecordStartTime();
// Save the runnable so it can be properly cleared during cancellation.
aRequest->GetScriptLoadContext()->mRunnable = runnable.get();
auto signalOOM = mozilla::MakeScopeExit(
[&aRequest]() { aRequest->GetScriptLoadContext()->mRunnable = nullptr; });
JS::OffThreadToken* token = nullptr;
rv = StartOffThreadCompilation(cx, aRequest, options, runnable, &token);
NS_ENSURE_SUCCESS(rv, rv);
MOZ_ASSERT(token);
// The conditions should match ScriptLoadContext::MaybeCancelOffThreadScript.
if (aRequest->IsBytecode()) {
JS::DecodeOptions decodeOptions(options);
aRequest->GetScriptLoadContext()->mOffThreadToken =
JS::DecodeStencilOffThread(cx, decodeOptions, aRequest->mScriptBytecode,
aRequest->mBytecodeOffset,
OffThreadScriptLoaderCallback,
static_cast<void*>(runnable));
if (!aRequest->GetScriptLoadContext()->mOffThreadToken) {
return NS_ERROR_OUT_OF_MEMORY;
}
} else if (aRequest->IsModuleRequest()) {
MOZ_ASSERT(aRequest->IsTextSource());
MaybeSourceText maybeSource;
nsresult rv = aRequest->GetScriptSource(cx, &maybeSource);
NS_ENSURE_SUCCESS(rv, rv);
auto compile = [&](auto& source) {
return JS::CompileModuleToStencilOffThread(
cx, options, source, OffThreadScriptLoaderCallback, runnable.get());
};
MOZ_ASSERT(!maybeSource.empty());
JS::OffThreadToken* token = maybeSource.mapNonEmpty(compile);
if (!token) {
return NS_ERROR_OUT_OF_MEMORY;
}
aRequest->GetScriptLoadContext()->mOffThreadToken = token;
} else {
MOZ_ASSERT(aRequest->IsTextSource());
if (ShouldApplyDelazifyStrategy(aRequest)) {
ApplyDelazifyStrategy(&options);
mTotalFullParseSize +=
aRequest->ScriptTextLength() > 0
? static_cast<uint32_t>(aRequest->ScriptTextLength())
: 0;
LOG(
("ScriptLoadRequest (%p): non-on-demand-only Parsing Enabled for "
"url=%s mTotalFullParseSize=%u",
aRequest, aRequest->mURI->GetSpecOrDefault().get(),
mTotalFullParseSize));
}
MaybeSourceText maybeSource;
nsresult rv = aRequest->GetScriptSource(cx, &maybeSource);
NS_ENSURE_SUCCESS(rv, rv);
if (StaticPrefs::dom_expose_test_interfaces()) {
switch (options.eagerDelazificationStrategy()) {
case JS::DelazificationOption::OnDemandOnly:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_on_demand_only");
break;
case JS::DelazificationOption::CheckConcurrentWithOnDemand:
case JS::DelazificationOption::ConcurrentDepthFirst:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_concurrent_depth_first");
break;
case JS::DelazificationOption::ConcurrentLargeFirst:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_concurrent_large_first");
break;
case JS::DelazificationOption::ParseEverythingEagerly:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_parse_everything_eagerly");
break;
}
}
auto compile = [&](auto& source) {
return JS::CompileToStencilOffThread(
cx, options, source, OffThreadScriptLoaderCallback, runnable.get());
};
MOZ_ASSERT(!maybeSource.empty());
JS::OffThreadToken* token = maybeSource.mapNonEmpty(compile);
if (!token) {
return NS_ERROR_OUT_OF_MEMORY;
}
aRequest->GetScriptLoadContext()->mOffThreadToken = token;
}
signalOOM.release();
aRequest->GetScriptLoadContext()->mOffThreadToken = token;
aRequest->GetScriptLoadContext()->mRunnable = runnable;
aRequest->GetScriptLoadContext()->BlockOnload(mDocument);
// Once the compilation is finished, an event would be added to the event loop
// to call ScriptLoader::ProcessOffThreadRequest with the same request.
// Once the compilation is finished, a callback will dispatch the runnable to
// the main thread to call ScriptLoader::ProcessOffThreadRequest for the
// request.
aRequest->mState = ScriptLoadRequest::State::Compiling;
// Requests that are not tracked elsewhere are added to a list while they are
@@ -1776,10 +1670,88 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
}
*aCouldCompileOut = true;
Unused << runnable.forget();
return NS_OK;
}
static inline nsresult CompileResultForToken(void* aToken) {
return aToken ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
nsresult ScriptLoader::StartOffThreadCompilation(
JSContext* aCx, ScriptLoadRequest* aRequest, JS::CompileOptions& aOptions,
Runnable* aRunnable, JS::OffThreadToken** aTokenOut) {
const JS::OffThreadCompileCallback callback = OffThreadScriptLoaderCallback;
if (aRequest->IsBytecode()) {
JS::DecodeOptions decodeOptions(aOptions);
*aTokenOut = JS::DecodeStencilOffThread(
aCx, decodeOptions, aRequest->mScriptBytecode,
aRequest->mBytecodeOffset, callback, aRunnable);
return CompileResultForToken(*aTokenOut);
}
MaybeSourceText maybeSource;
nsresult rv = aRequest->GetScriptSource(aCx, &maybeSource);
NS_ENSURE_SUCCESS(rv, rv);
if (aRequest->IsModuleRequest()) {
auto compile = [&](auto& source) {
return JS::CompileModuleToStencilOffThread(aCx, aOptions, source,
callback, aRunnable);
};
MOZ_ASSERT(!maybeSource.empty());
*aTokenOut = maybeSource.mapNonEmpty(compile);
return CompileResultForToken(*aTokenOut);
}
if (ShouldApplyDelazifyStrategy(aRequest)) {
ApplyDelazifyStrategy(&aOptions);
mTotalFullParseSize +=
aRequest->ScriptTextLength() > 0
? static_cast<uint32_t>(aRequest->ScriptTextLength())
: 0;
LOG(
("ScriptLoadRequest (%p): non-on-demand-only Parsing Enabled for "
"url=%s mTotalFullParseSize=%u",
aRequest, aRequest->mURI->GetSpecOrDefault().get(),
mTotalFullParseSize));
}
if (StaticPrefs::dom_expose_test_interfaces()) {
switch (aOptions.eagerDelazificationStrategy()) {
case JS::DelazificationOption::OnDemandOnly:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_on_demand_only");
break;
case JS::DelazificationOption::CheckConcurrentWithOnDemand:
case JS::DelazificationOption::ConcurrentDepthFirst:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_concurrent_depth_first");
break;
case JS::DelazificationOption::ConcurrentLargeFirst:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_concurrent_large_first");
break;
case JS::DelazificationOption::ParseEverythingEagerly:
TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"delazification_parse_everything_eagerly");
break;
}
}
auto compile = [&](auto& source) {
return JS::CompileToStencilOffThread(aCx, aOptions, source, callback,
aRunnable);
};
MOZ_ASSERT(!maybeSource.empty());
*aTokenOut = maybeSource.mapNonEmpty(compile);
return CompileResultForToken(*aTokenOut);
}
nsresult ScriptLoader::CompileOffThreadOrProcessRequest(
ScriptLoadRequest* aRequest) {
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),