Skip to content

Instantly share code, notes, and snippets.

@mstange
Last active January 25, 2019 01:23
Show Gist options
  • Save mstange/10e8ee1155a8e01e0216bf49502cb3c8 to your computer and use it in GitHub Desktop.
Save mstange/10e8ee1155a8e01e0216bf49502cb3c8 to your computer and use it in GitHub Desktop.
# HG changeset patch
# User Markus Stange <mstange@themasta.com>
# Date 1548379388 18000
# Thu Jan 24 20:23:08 2019 -0500
# Node ID e442cd9c43e361fec584d473888cbf07b5910f1e
# Parent 19bfb22ffae28a0a6ec449064daff863213269a6
Bug 1514007 - Annotate <script> evaluation profiler labels with async/defer. r?smaug
This also moves the label from ScriptLoader::ProcessScriptElement to
ScriptLoader::EvaluateScript so that it also kicks in for scripts that are run
from NotifyOffThreadScriptLoadCompletedRunnable::Run.
Differential Revision: https://phabricator.services.mozilla.com/D14924
diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp
--- a/dom/script/ScriptLoader.cpp
+++ b/dom/script/ScriptLoader.cpp
@@ -65,16 +65,19 @@
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Attributes.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/Unused.h"
#include "nsIScriptError.h"
#include "nsIOutputStream.h"
+#ifdef MOZ_GECKO_PROFILER
+#include "ProfilerMarkerPayload.h"
+#endif
using JS::SourceText;
using mozilla::Telemetry::LABELS_DOM_SCRIPT_PRELOAD_RESULT;
namespace mozilla {
namespace dom {
@@ -1426,24 +1429,16 @@ bool ScriptLoader::ProcessScriptElement(
// Check to see if scripts has been turned off.
if (!mEnabled || !mDocument->IsScriptEnabled()) {
return false;
}
NS_ASSERTION(!aElement->IsMalformed(), "Executing malformed script");
- nsAutoCString url;
- nsCOMPtr<nsIURI> scriptURI = aElement->GetScriptURI();
- if (scriptURI) {
- scriptURI->GetAsciiSpec(url);
- }
- AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("ScriptLoader::ProcessScriptElement",
- JS, url);
-
nsCOMPtr<nsIContent> scriptContent = do_QueryInterface(aElement);
nsAutoString type;
bool hasType = aElement->GetScriptType(type);
ScriptKind scriptKind = aElement->GetScriptIsModule() ? ScriptKind::eModule
: ScriptKind::eClassic;
@@ -2454,16 +2449,95 @@ static nsresult ExecuteCompiledScript(JS
// Create a ClassicScript object and associate it with the JSScript.
RefPtr<ClassicScript> classicScript =
new ClassicScript(aRequest->mFetchOptions, aRequest->mBaseURL);
classicScript->AssociateWithScript(script);
return aExec.ExecScript();
}
+static void GetProfilerLabelForRequest(ScriptLoadRequest* aRequest,
+ nsACString& aOutString) {
+ aOutString.Append("<script");
+ if (aRequest->IsAsyncScript()) {
+ aOutString.Append(" async");
+ } else if (aRequest->IsDeferredScript()) {
+ aOutString.Append(" defer");
+ }
+ if (aRequest->IsModuleRequest()) {
+ aOutString.Append(" type=\"module\"");
+ }
+
+ nsAutoCString url;
+ if (aRequest->mURI) {
+ aRequest->mURI->GetAsciiSpec(url);
+ } else {
+ url = "<unknown>";
+ }
+
+ if (aRequest->mIsInline) {
+ aOutString.Append("> inline at line ");
+ aOutString.AppendInt(aRequest->mLineNo);
+ aOutString.Append(" of ");
+ aOutString.Append(url);
+ } else {
+ aOutString.Append(" src=\"");
+ aOutString.Append(url);
+ aOutString.Append("\">");
+ }
+}
+
+#ifdef MOZ_GECKO_PROFILER
+class MOZ_RAII AutoProfilerTextTracing {
+ public:
+ AutoProfilerTextTracing(const char* aMarkerName,
+ js::ProfilingStackFrame::Category aCategory,
+ const nsACString& aText,
+ const mozilla::Maybe<nsID>& aDocShellId,
+ const mozilla::Maybe<uint32_t>& aDocShellHistoryId
+ MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
+ : mMarkerName(aMarkerName),
+ mCategory(aCategory),
+ mText(aText),
+ mStartTime(mozilla::TimeStamp::Now()),
+ mDocShellId(aDocShellId),
+ mDocShellHistoryId(aDocShellHistoryId) {
+ MOZ_GUARD_OBJECT_NOTIFIER_INIT;
+ }
+
+ ~AutoProfilerTextTracing() {
+ profiler_add_marker(
+ mMarkerName,
+ js::ProfilingStackFrame::Category::LAYOUT,
+ MakeUnique<TextMarkerPayload>(mText, mStartTime, TimeStamp::Now(),
+ mDocShellId, mDocShellHistoryId));
+ }
+
+ protected:
+ MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
+ const char* mMarkerName;
+ const js::ProfilingStackFrame::Category mCategory;
+ nsCString mText;
+ mozilla::TimeStamp mStartTime;
+ const mozilla::Maybe<nsID> mDocShellId;
+ const mozilla::Maybe<uint32_t> mDocShellHistoryId;
+};
+
+# define AUTO_PROFILER_TEXT_TRACING_DOCSHELL(markerName, category, text, \
+ docShell) \
+ DECLARE_DOCSHELL_AND_HISTORY_ID(docShell); \
+ AutoProfilerTextTracing PROFILER_RAII( \
+ markerName, \
+ js::ProfilingStackFrame::Category::category, \
+ text, docShellId, docShellHistoryId)
+#else
+# define AUTO_PROFILER_TEXT_TRACING_DOCSHELL(markerName, category, text, \
+ docShell)
+#endif
+
nsresult ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) {
using namespace mozilla::Telemetry;
MOZ_ASSERT(aRequest->IsReadyToRun());
// We need a document to evaluate scripts.
if (!mDocument) {
return NS_ERROR_FAILURE;
}
@@ -2488,20 +2562,26 @@ nsresult ScriptLoader::EvaluateScript(Sc
// Make sure context is a strong reference since we access it after
// we've executed a script, which may cause all other references to
// the context to go away.
nsCOMPtr<nsIScriptContext> context = globalObject->GetScriptContext();
if (!context) {
return NS_ERROR_FAILURE;
}
+ nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow();
+ nsIDocShell* docShell = window ? window->GetDocShell() : nullptr;
+
// New script entry point required, due to the "Create a script" sub-step of
// http://www.whatwg.org/specs/web-apps/current-work/#execute-the-script-block
nsAutoMicroTask mt;
- AutoEntryScript aes(globalObject, "<script> element", true);
+ nsAutoCString profilerLabelString;
+ GetProfilerLabelForRequest(aRequest, profilerLabelString);
+ AUTO_PROFILER_TEXT_TRACING_DOCSHELL("Script", JS, profilerLabelString, docShell);
+ AutoEntryScript aes(globalObject, profilerLabelString.get(), true);
JSContext* cx = aes.cx();
JS::Rooted<JSObject*> global(cx, globalObject->GetGlobalJSObject());
AutoSetProcessingScriptTag setProcessingScriptTag(context);
nsresult rv;
{
if (aRequest->IsModuleRequest()) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment