Bug 1652613 - report slow script hang durations and only clear hang data after the observer notification, r=mconley
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 24 Jul 2020 00:01:57 +0000
changeset 541874 6e90fed2ff8ac39ca8af803c1906c634682e53ae
parent 541873 77c85c1ef3f00843f7daffd43591df738e9aedaa
child 541875 5bbd29a7db14dd5fb5b8eafeb4e8d4056c6f392d
push id122549
push usergijskruitbosch@gmail.com
push dateFri, 24 Jul 2020 00:56:58 +0000
treeherderautoland@74d2da543877 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs1652613
milestone80.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1652613 - report slow script hang durations and only clear hang data after the observer notification, r=mconley Differential Revision: https://phabricator.services.mozilla.com/D83989
dom/base/nsGlobalWindowInner.cpp
dom/base/nsGlobalWindowInner.h
dom/ipc/PProcessHangMonitor.ipdl
dom/ipc/ProcessHangMonitor.cpp
dom/ipc/ProcessHangMonitor.h
dom/ipc/nsIHangReport.idl
js/xpconnect/src/XPCJSContext.cpp
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -4715,17 +4715,18 @@ void nsGlobalWindowInner::FireOfflineSta
     name.AssignLiteral("online");
   }
   nsContentUtils::DispatchTrustedEvent(mDoc, static_cast<EventTarget*>(this),
                                        name, CanBubble::eNo, Cancelable::eNo);
 }
 
 nsGlobalWindowInner::SlowScriptResponse
 nsGlobalWindowInner::ShowSlowScriptDialog(JSContext* aCx,
-                                          const nsString& aAddonId) {
+                                          const nsString& aAddonId,
+                                          const double aDuration) {
   nsresult rv;
 
   if (Preferences::GetBool("dom.always_stop_slow_scripts")) {
     return KillSlowScript;
   }
 
   // If it isn't safe to run script, then it isn't safe to bring up the prompt
   // (since that spins the event loop). In that (rare) case, we just kill the
@@ -4763,17 +4764,18 @@ nsGlobalWindowInner::ShowSlowScriptDialo
   SetCursor("auto"_ns, IgnoreErrors());
 
   if (XRE_IsContentProcess() && ProcessHangMonitor::Get()) {
     ProcessHangMonitor::SlowScriptAction action;
     RefPtr<ProcessHangMonitor> monitor = ProcessHangMonitor::Get();
     nsIDocShell* docShell = GetDocShell();
     nsCOMPtr<nsIBrowserChild> child =
         docShell ? docShell->GetBrowserChild() : nullptr;
-    action = monitor->NotifySlowScript(child, filename.get(), aAddonId);
+    action =
+        monitor->NotifySlowScript(child, filename.get(), aAddonId, aDuration);
     if (action == ProcessHangMonitor::Terminate) {
       return KillSlowScript;
     }
     if (action == ProcessHangMonitor::TerminateGlobal) {
       return KillScriptGlobal;
     }
 
     if (action == ProcessHangMonitor::StartDebugger) {
--- a/dom/base/nsGlobalWindowInner.h
+++ b/dom/base/nsGlobalWindowInner.h
@@ -477,17 +477,18 @@ class nsGlobalWindowInner final : public
   enum SlowScriptResponse {
     ContinueSlowScript = 0,
     ContinueSlowScriptAndKeepNotifying,
     AlwaysContinueSlowScript,
     KillSlowScript,
     KillScriptGlobal
   };
   SlowScriptResponse ShowSlowScriptDialog(JSContext* aCx,
-                                          const nsString& aAddonId);
+                                          const nsString& aAddonId,
+                                          const double aDuration);
 
   // Inner windows only.
   void AddGamepad(uint32_t aIndex, mozilla::dom::Gamepad* aGamepad);
   void RemoveGamepad(uint32_t aIndex);
   void GetGamepads(nsTArray<RefPtr<mozilla::dom::Gamepad>>& aGamepads);
   already_AddRefed<mozilla::dom::Gamepad> GetGamepad(uint32_t aIndex);
   void SetHasSeenGamepadInput(bool aHasSeen);
   bool HasSeenGamepadInput();
--- a/dom/ipc/PProcessHangMonitor.ipdl
+++ b/dom/ipc/PProcessHangMonitor.ipdl
@@ -15,16 +15,17 @@ using mozilla::layers::LayersObserverEpo
 
 namespace mozilla {
 
 struct SlowScriptData
 {
   TabId tabId;
   nsCString filename;
   nsString addonId;
+  double duration;
 };
 
 struct PluginHangData
 {
   uint32_t pluginId;
   ProcessId contentProcessId;
 };
 
--- a/dom/ipc/ProcessHangMonitor.cpp
+++ b/dom/ipc/ProcessHangMonitor.cpp
@@ -84,19 +84,20 @@ class HangMonitorChild : public PProcess
   explicit HangMonitorChild(ProcessHangMonitor* aMonitor);
   ~HangMonitorChild() override;
 
   void Bind(Endpoint<PProcessHangMonitorChild>&& aEndpoint);
 
   typedef ProcessHangMonitor::SlowScriptAction SlowScriptAction;
   SlowScriptAction NotifySlowScript(nsIBrowserChild* aBrowserChild,
                                     const char* aFileName,
-                                    const nsString& aAddonId);
+                                    const nsString& aAddonId,
+                                    const double aDuration);
   void NotifySlowScriptAsync(TabId aTabId, const nsCString& aFileName,
-                             const nsString& aAddonId);
+                             const nsString& aAddonId, const double aDuration);
 
   bool IsDebuggerStartupComplete();
 
   void NotifyPluginHang(uint32_t aPluginId);
   void NotifyPluginHangAsync(uint32_t aPluginId);
 
   void ClearHang();
   void ClearHangAsync();
@@ -584,25 +585,27 @@ void HangMonitorChild::Bind(Endpoint<PPr
   MOZ_ASSERT(ok);
 
   sInitializing = false;
   mal.Notify();
 }
 
 void HangMonitorChild::NotifySlowScriptAsync(TabId aTabId,
                                              const nsCString& aFileName,
-                                             const nsString& aAddonId) {
+                                             const nsString& aAddonId,
+                                             const double aDuration) {
   if (mIPCOpen) {
-    Unused << SendHangEvidence(SlowScriptData(aTabId, aFileName, aAddonId));
+    Unused << SendHangEvidence(
+        SlowScriptData(aTabId, aFileName, aAddonId, aDuration));
   }
 }
 
 HangMonitorChild::SlowScriptAction HangMonitorChild::NotifySlowScript(
     nsIBrowserChild* aBrowserChild, const char* aFileName,
-    const nsString& aAddonId) {
+    const nsString& aAddonId, const double aDuration) {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
 
   mSentReport = true;
 
   {
     MonitorAutoLock lock(mMonitor);
 
     if (mTerminateScript) {
@@ -624,19 +627,20 @@ HangMonitorChild::SlowScriptAction HangM
   TabId id;
   if (aBrowserChild) {
     RefPtr<BrowserChild> browserChild =
         static_cast<BrowserChild*>(aBrowserChild);
     id = browserChild->GetTabId();
   }
   nsAutoCString filename(aFileName);
 
-  Dispatch(NewNonOwningRunnableMethod<TabId, nsCString, nsString>(
+  Dispatch(NewNonOwningRunnableMethod<TabId, nsCString, nsString, double>(
       "HangMonitorChild::NotifySlowScriptAsync", this,
-      &HangMonitorChild::NotifySlowScriptAsync, id, filename, aAddonId));
+      &HangMonitorChild::NotifySlowScriptAsync, id, filename, aAddonId,
+      aDuration));
   return SlowScriptAction::Continue;
 }
 
 bool HangMonitorChild::IsDebuggerStartupComplete() {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
 
   MonitorAutoLock lock(mMonitor);
 
@@ -846,21 +850,22 @@ void HangMonitorParent::SendHangNotifica
   nsCOMPtr<nsIObserverService> observerService =
       mozilla::services::GetObserverService();
   observerService->NotifyObservers(mProcess, "process-hang-report", nullptr);
 }
 
 void HangMonitorParent::ClearHangNotification() {
   // chrome process, main thread
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
-  mProcess->ClearHang();
 
   nsCOMPtr<nsIObserverService> observerService =
       mozilla::services::GetObserverService();
   observerService->NotifyObservers(mProcess, "clear-hang-report", nullptr);
+
+  mProcess->ClearHang();
 }
 
 // Take a minidump of the browser process if one wasn't already taken for the
 // plugin that caused the hang. Return false if a dump was already available or
 // true if new one has been taken.
 bool HangMonitorParent::TakeBrowserMinidump(const PluginHangData& aPhd,
                                             nsString& aCrashId) {
   MutexAutoLock lock(mBrowserCrashDumpHashLock);
@@ -1003,16 +1008,27 @@ HangMonitoredProcess::GetHangType(uint32
       MOZ_ASSERT_UNREACHABLE("Unexpected HangData type");
       return NS_ERROR_UNEXPECTED;
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
+HangMonitoredProcess::GetHangDuration(double* aHangDuration) {
+  MOZ_RELEASE_ASSERT(NS_IsMainThread());
+  if (mHangData.type() != HangData::TSlowScriptData) {
+    *aHangDuration = -1;
+  } else {
+    *aHangDuration = mHangData.get_SlowScriptData().duration();
+  }
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 HangMonitoredProcess::GetScriptBrowser(Element** aBrowser) {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   if (mHangData.type() != HangData::TSlowScriptData) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   TabId tabId = mHangData.get_SlowScriptData().tabId();
   if (!mContentParent) {
@@ -1262,20 +1278,20 @@ ProcessHangMonitor::Observe(nsISupports*
     nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
     obs->RemoveObserver(this, "xpcom-shutdown");
   }
   return NS_OK;
 }
 
 ProcessHangMonitor::SlowScriptAction ProcessHangMonitor::NotifySlowScript(
     nsIBrowserChild* aBrowserChild, const char* aFileName,
-    const nsString& aAddonId) {
+    const nsString& aAddonId, const double aDuration) {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   return HangMonitorChild::Get()->NotifySlowScript(aBrowserChild, aFileName,
-                                                   aAddonId);
+                                                   aAddonId, aDuration);
 }
 
 bool ProcessHangMonitor::IsDebuggerStartupComplete() {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   return HangMonitorChild::Get()->IsDebuggerStartupComplete();
 }
 
 bool ProcessHangMonitor::ShouldTimeOutCPOWs() {
--- a/dom/ipc/ProcessHangMonitor.h
+++ b/dom/ipc/ProcessHangMonitor.h
@@ -65,17 +65,18 @@ class ProcessHangMonitor final : public 
   enum SlowScriptAction {
     Continue,
     Terminate,
     StartDebugger,
     TerminateGlobal,
   };
   SlowScriptAction NotifySlowScript(nsIBrowserChild* aBrowserChild,
                                     const char* aFileName,
-                                    const nsString& aAddonId);
+                                    const nsString& aAddonId,
+                                    const double aDuration);
 
   void NotifyPluginHang(uint32_t aPluginId);
 
   bool IsDebuggerStartupComplete();
 
   void InitiateCPOWTimeout();
   bool ShouldTimeOutCPOWs();
 
--- a/dom/ipc/nsIHangReport.idl
+++ b/dom/ipc/nsIHangReport.idl
@@ -27,16 +27,18 @@ interface nsIHangReport : nsISupports
   // The type of hang being reported: SLOW_SCRIPT or PLUGIN_HANG.
   readonly attribute unsigned long hangType;
 
   // For SLOW_SCRIPT reports, these fields contain information about the
   // slow script.
   // Only valid for SLOW_SCRIPT reports.
   readonly attribute Element scriptBrowser;
   readonly attribute ACString scriptFileName;
+  // Duration of the hang so far.
+  readonly attribute double hangDuration;
   readonly attribute AString addonId;
 
   // For PLUGIN_HANGs, this field contains information about the plugin.
   // Only valid for PLUGIN_HANG reports.
   readonly attribute ACString pluginName;
 
   // Called by front end code when user ignores or cancels
   // the notification.
--- a/js/xpconnect/src/XPCJSContext.cpp
+++ b/js/xpconnect/src/XPCJSContext.cpp
@@ -696,18 +696,18 @@ bool XPCJSContext::InterruptCallback(JSC
   if (!chrome && !self->mTimeoutAccumulated) {
     uint32_t delay = uint32_t(self->mSlowScriptActualWait.ToMilliseconds() -
                               (limit * 1000.0));
     Telemetry::Accumulate(Telemetry::SLOW_SCRIPT_NOTIFY_DELAY, delay);
     self->mTimeoutAccumulated = true;
   }
 
   // Show the prompt to the user, and kill if requested.
-  nsGlobalWindowInner::SlowScriptResponse response =
-      win->ShowSlowScriptDialog(cx, addonId);
+  nsGlobalWindowInner::SlowScriptResponse response = win->ShowSlowScriptDialog(
+      cx, addonId, self->mSlowScriptActualWait.ToMilliseconds());
   if (response == nsGlobalWindowInner::KillSlowScript) {
     if (Preferences::GetBool("dom.global_stop_script", true)) {
       xpc::Scriptability::Get(global).Block();
     }
     return false;
   }
   if (response == nsGlobalWindowInner::KillScriptGlobal) {
     nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();