Bug 1518077 part 2 - Add MEMORY_JS_REALMS_{USER,SYSTEM} similar to the MEMORY_JS_COMPARTMENTS_{USER,SYSTEM} counts. r=njn,chutten
authorJan de Mooij <jdemooij@mozilla.com>
Tue, 08 Jan 2019 15:11:36 +0000
changeset 510025 270584ef33a99da992b93039d4bd63c3fc93a4d1
parent 510024 642591e716e111cf8943cc04ff31f3110d036143
child 510026 6bc15d28e6ec44b3c1e780deae80b51f6431a27e
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn, chutten
bugs1518077
milestone66.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 1518077 part 2 - Add MEMORY_JS_REALMS_{USER,SYSTEM} similar to the MEMORY_JS_COMPARTMENTS_{USER,SYSTEM} counts. r=njn,chutten Differential Revision: https://phabricator.services.mozilla.com/D15809
js/xpconnect/tests/unit/test_js_memory_telemetry.js
js/xpconnect/tests/unit/xpcshell.ini
toolkit/components/telemetry/Histograms.json
xpcom/base/MemoryTelemetry.cpp
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/unit/test_js_memory_telemetry.js
@@ -0,0 +1,55 @@
+"use strict";
+
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm", {});
+
+add_task(function test_compartment_realm_counts() {
+  const compsSystem = "MEMORY_JS_COMPARTMENTS_SYSTEM";
+  const compsUser = "MEMORY_JS_COMPARTMENTS_USER";
+  const realmsSystem = "MEMORY_JS_REALMS_SYSTEM";
+  const realmsUser = "MEMORY_JS_REALMS_USER";
+
+  Cu.forceShrinkingGC();
+
+  Services.telemetry.gatherMemory();
+  let snapshot1 = Services.telemetry.getSnapshotForHistograms("main", true).parent;
+
+  // We can't hard code exact counts, but we can check some basic invariants:
+  //
+  // * Compartments must contain at least one realm, so there must be more
+  //   realms than compartments.
+  // * There must be at least one system realm.
+
+  Assert.ok(snapshot1[realmsSystem].sum <= snapshot1[compsSystem].sum,
+            "Number of system compartments can't exceed number of system realms");
+  Assert.ok(snapshot1[realmsUser].sum <= snapshot1[compsUser].sum,
+            "Number of user compartments can't exceed number of user realms");
+  Assert.ok(snapshot1[realmsSystem].sum > 0,
+            "There must be at least one system realm");
+
+  // Now we create a bunch of sandboxes (more than one to be more resilient
+  // against GCs happening in the meantime), so we can check:
+  //
+  // * There are now more realms and user compartments than before. Not system
+  //   compartments, because system realms share a compartment.
+  // * The system compartment contains multiple realms.
+
+  let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
+  let arr = [];
+  for (let i = 0; i < 5; i++) {
+    arr.push(Cu.Sandbox(null));
+    arr.push(Cu.Sandbox(systemPrincipal));
+  }
+
+  Services.telemetry.gatherMemory();
+  let snapshot2 = Services.telemetry.getSnapshotForHistograms("main", true).parent;
+
+  for (let k of [realmsSystem, realmsUser, compsUser]) {
+    Assert.ok(snapshot2[k].sum > snapshot1[k].sum,
+              "There must be more compartments/realms now: " + k);
+  }
+
+  Assert.ok(snapshot2[realmsSystem].sum > snapshot2[compsSystem].sum,
+            "There must be more system realms than system compartments now");
+
+  arr[0].x = 10; // Ensure the JS engine keeps |arr| alive until this point.
+});
--- a/js/xpconnect/tests/unit/xpcshell.ini
+++ b/js/xpconnect/tests/unit/xpcshell.ini
@@ -115,16 +115,17 @@ head = head_ongc.js
 [test_URLSearchParams.js]
 [test_fileReader.js]
 [test_messageChannel.js]
 [test_crypto.js]
 [test_css.js]
 [test_rtcIdentityProvider.js]
 [test_sandbox_atob.js]
 [test_isProxy.js]
+[test_js_memory_telemetry.js]
 [test_getObjectPrincipal.js]
 [test_sandbox_name.js]
 [test_watchdog_enable.js]
 head = head_watchdog.js
 [test_watchdog_disable.js]
 head = head_watchdog.js
 [test_watchdog_toggle.js]
 head = head_watchdog.js
--- a/toolkit/components/telemetry/Histograms.json
+++ b/toolkit/components/telemetry/Histograms.json
@@ -1103,16 +1103,36 @@
     "record_in_processes": ["main", "content"],
     "alert_emails": ["memshrink-telemetry-alerts@mozilla.com"],
     "expires_in_version": "never",
     "kind": "exponential",
     "high": 1000,
     "n_buckets": 50,
     "description": "Total JavaScript compartments used for web pages"
   },
+  "MEMORY_JS_REALMS_SYSTEM": {
+    "record_in_processes": ["main", "content"],
+    "alert_emails": ["memshrink-telemetry-alerts@mozilla.com", "jdemooij@mozilla.com"],
+    "bug_numbers": [1518077],
+    "expires_in_version": "never",
+    "kind": "exponential",
+    "high": 1000,
+    "n_buckets": 50,
+    "description": "Total JavaScript realms used for add-ons and internals."
+  },
+  "MEMORY_JS_REALMS_USER": {
+    "record_in_processes": ["main", "content"],
+    "alert_emails": ["memshrink-telemetry-alerts@mozilla.com", "jdemooij@mozilla.com"],
+    "bug_numbers": [1518077],
+    "expires_in_version": "never",
+    "kind": "exponential",
+    "high": 1000,
+    "n_buckets": 50,
+    "description": "Total JavaScript realms used for web pages."
+  },
   "MEMORY_JS_GC_HEAP": {
     "record_in_processes": ["main", "content"],
     "alert_emails": ["memshrink-telemetry-alerts@mozilla.com"],
     "expires_in_version": "never",
     "kind": "exponential",
     "low": 1024,
     "high": 16777216,
     "n_buckets": 200,
--- a/xpcom/base/MemoryTelemetry.cpp
+++ b/xpcom/base/MemoryTelemetry.cpp
@@ -268,16 +268,18 @@ nsresult MemoryTelemetry::GatherReports(
 
   // Collect cheap or main-thread only metrics synchronously, on the main
   // thread.
   RECORD(MEMORY_JS_GC_HEAP, JSMainRuntimeGCHeap, UNITS_BYTES);
   RECORD(MEMORY_JS_COMPARTMENTS_SYSTEM, JSMainRuntimeCompartmentsSystem,
          UNITS_COUNT);
   RECORD(MEMORY_JS_COMPARTMENTS_USER, JSMainRuntimeCompartmentsUser,
          UNITS_COUNT);
+  RECORD(MEMORY_JS_REALMS_SYSTEM, JSMainRuntimeRealmsSystem, UNITS_COUNT);
+  RECORD(MEMORY_JS_REALMS_USER, JSMainRuntimeRealmsUser, UNITS_COUNT);
   RECORD(MEMORY_IMAGES_CONTENT_USED_UNCOMPRESSED, ImagesContentUsedUncompressed,
          UNITS_BYTES);
   RECORD(MEMORY_STORAGE_SQLITE, StorageSQLite, UNITS_BYTES);
 #ifdef XP_WIN
   RECORD(LOW_MEMORY_EVENTS_VIRTUAL, LowMemoryEventsVirtual,
          UNITS_COUNT_CUMULATIVE);
   RECORD(LOW_MEMORY_EVENTS_COMMIT_SPACE, LowMemoryEventsCommitSpace,
          UNITS_COUNT_CUMULATIVE);