From a7bf50d1d14a4973c1c313052b6937c0fa9e7961 Mon Sep 17 00:00:00 2001 From: Toshihito Kikuchi Date: Thu, 22 Jul 2021 21:15:00 +0000 Subject: [PATCH] Bug 1715858 - Part1. Add a new telemetry histogram TAB_UNLOAD_TO_RELOAD. r=NeilDeakin The new histogram `TAB_UNLOAD_TO_RELOAD` records how long a tab had been unloaded until it was reload by a user. With this data, we can evaluate the selection logic to choose a tab to unload. For example, if many of unloaded tabs are reloaded within 30 seconds or so, we unload a wrong tab. Differential Revision: https://phabricator.services.mozilla.com/D120019 --- browser/base/content/tabbrowser-tab.js | 16 ++++++++++++++++ browser/base/content/tabbrowser.js | 1 + browser/modules/TabUnloader.jsm | 1 + .../modules/test/browser/browser_TabUnloader.js | 10 ++++++++++ toolkit/components/telemetry/Histograms.json | 12 ++++++++++++ 5 files changed, 40 insertions(+) diff --git a/browser/base/content/tabbrowser-tab.js b/browser/base/content/tabbrowser-tab.js index 320ca559d5fa..69c9f1ddd86e 100644 --- a/browser/base/content/tabbrowser-tab.js +++ b/browser/base/content/tabbrowser-tab.js @@ -265,6 +265,22 @@ this._lastAccessed = this.selected ? Infinity : aDate || Date.now(); } + updateLastUnloadedByTabUnloader() { + this._lastUnloaded = Date.now(); + } + + recordTimeFromUnloadToReload() { + if (!this._lastUnloaded) { + return; + } + + const diff_in_msec = Date.now() - this._lastUnloaded; + Services.telemetry + .getHistogramById("TAB_UNLOAD_TO_RELOAD") + .add(diff_in_msec / 1000); + delete this._lastUnloaded; + } + on_mouseover(event) { if (event.target.classList.contains("tab-close-button")) { this.mOverCloseButton = true; diff --git a/browser/base/content/tabbrowser.js b/browser/base/content/tabbrowser.js index feedf7874133..89bb7dcd9780 100644 --- a/browser/base/content/tabbrowser.js +++ b/browser/base/content/tabbrowser.js @@ -1156,6 +1156,7 @@ } if (!this._previewMode) { + newTab.recordTimeFromUnloadToReload(); newTab.updateLastAccessed(); oldTab.updateLastAccessed(); diff --git a/browser/modules/TabUnloader.jsm b/browser/modules/TabUnloader.jsm index 154e1143d374..3fc44305a298 100644 --- a/browser/modules/TabUnloader.jsm +++ b/browser/modules/TabUnloader.jsm @@ -279,6 +279,7 @@ var TabUnloader = { Services.console.logStringMessage( `TabUnloader discarded <${remoteType}>` ); + tabInfo.tab.updateLastUnloadedByTabUnloader(); return true; } } diff --git a/browser/modules/test/browser/browser_TabUnloader.js b/browser/modules/test/browser/browser_TabUnloader.js index 5b5ec4f17167..e67e79d59e98 100644 --- a/browser/modules/test/browser/browser_TabUnloader.js +++ b/browser/modules/test/browser/browser_TabUnloader.js @@ -209,6 +209,10 @@ add_task(async function test() { ok(!pinnedSoundTab.linkedPanel, "unloaded a pinned tab playing sound"); await compareTabOrder([]); // note that no tabs are returned when there are no discardable tabs. + const histogram = TelemetryTestUtils.getAndClearHistogram( + "TAB_UNLOAD_TO_RELOAD" + ); + // It's possible that we're already in the memory-pressure state // and we may receive the "ongoing" message. const message = await pressureAndObserve("memory-pressure"); @@ -224,6 +228,12 @@ add_task(async function test() { await BrowserTestUtils.switchTab(gBrowser, tab1); await BrowserTestUtils.switchTab(gBrowser, soundTab); await BrowserTestUtils.switchTab(gBrowser, pinnedTab); + + const hist = histogram.snapshot(); + const numEvents = Object.values(hist.values).reduce((a, b) => a + b); + Assert.equal(numEvents, 3, "three tabs have been reloaded."); + + // tab0 has never been unloaded. No data is added to the histogram. await BrowserTestUtils.switchTab(gBrowser, tab0); // Audio from the first sound tab was stopped when the tab was discarded earlier, diff --git a/toolkit/components/telemetry/Histograms.json b/toolkit/components/telemetry/Histograms.json index df944c60bfe0..84507fc9a972 100644 --- a/toolkit/components/telemetry/Histograms.json +++ b/toolkit/components/telemetry/Histograms.json @@ -4957,6 +4957,18 @@ "releaseChannelCollection": "opt-out", "description": "Number of tabs opened across all windows, collected at most every 5 minutes whenever the user interacts with the browser in the following ways: open tab/window, page load." }, + "TAB_UNLOAD_TO_RELOAD": { + "record_in_processes": ["main"], + "products": ["firefox"], + "alert_emails": ["tkikuchi@mozilla.com"], + "bug_numbers": [1715858], + "expires_in_version": "never", + "kind": "exponential", + "high": 100000, + "n_buckets": 50, + "releaseChannelCollection": "opt-out", + "description": "How long (sec) a tab had been unloaded until it was reloaded." + }, "LAZYLOAD_IMAGE_TOTAL": { "record_in_processes": ["main", "content"], "products": ["firefox"],