summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutmemory/tests/test_memoryReporters.xhtml
blob: bc715b67784882db6d266e13bb3706e685a5b05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<window title="Memory reporters"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- This file tests (in a rough fashion) whether the memory reporters are
       producing sensible results.  test_aboutmemory.xhtml tests the
       presentation of memory reports in about:memory. -->

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <!-- In bug 773533, <marquee> elements crashed the JS memory reporter -->
  <marquee>Marquee</marquee>
  </body>

  <!-- some URIs that should be anonymized in anonymous mode -->
  <iframe id="amFrame"  height="200" src="http://example.org:80"></iframe>
  <iframe id="amFrame"  height="200" src="https://example.com:443"></iframe>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[

  "use strict";

  const NONHEAP = Ci.nsIMemoryReporter.KIND_NONHEAP;
  const HEAP    = Ci.nsIMemoryReporter.KIND_HEAP;
  const OTHER   = Ci.nsIMemoryReporter.KIND_OTHER;

  const BYTES = Ci.nsIMemoryReporter.UNITS_BYTES;
  const COUNT = Ci.nsIMemoryReporter.UNITS_COUNT;
  const COUNT_CUMULATIVE = Ci.nsIMemoryReporter.UNITS_COUNT_CUMULATIVE;
  const PERCENTAGE = Ci.nsIMemoryReporter.UNITS_PERCENTAGE;

  // Use backslashes instead of forward slashes due to memory reporting's hacky
  // handling of URLs.
  const XUL_NS =
    "http:\\\\www.mozilla.org\\keymaster\\gatekeeper\\there.is.only.xul";

  SimpleTest.waitForExplicitFinish();

  let vsizeAmounts = [];
  let residentAmounts = [];
  let heapAllocatedAmounts = [];
  let storageSqliteAmounts = [];

  let jsGcHeapUsedGcThingsTotal = 0;
  let jsGcHeapUsedGcThings = {};

  let present = {}

  // Generate a long, random string.  We'll check that this string is
  // reported in at least one of the memory reporters.
  let bigString = "";
  while (bigString.length < 10000) {
    bigString += Math.random();
  }
  let bigStringPrefix = bigString.substring(0, 100);

  // Generate many copies of two distinctive short strings, "!)(*&" and
  // "@)(*&". We'll check that these strings are reported in at least one of
  // the memory reporters. We will create these strings in the tenured heap to
  // avoid nursery string deduplication.
  let shortStrings = [];
  let newString = Cu.getJSTestingFunctions().newString;
  for (let i = 0; i < 10000; i++) {
    let str = (Math.random() > 0.5 ? "!" : "@") + ")(*&";
    shortStrings.push(newString(str, {tenured: true}));
  }

  // Strings in the nursery are not reported, so make sure the above test
  // strings are tenured (not all are created tenured).
  Cu.forceGC();

  let mySandbox = Cu.Sandbox(document.nodePrincipal,
                    { sandboxName: "this-is-a-sandbox-name" });

  function handleReportNormal(aProcess, aPath, aKind, aUnits, aAmount)
  {
    if (aProcess.startsWith(`Utility `)) {
      // The Utility process that runs the ORB JavaScript validator starts on first
      // idle in the parent process. This makes it notoriously hard to know _if_ it
      // actually has started. So we bail. See Bug 1813985.
      return;
    }
    // Record the values of some notable reporters.
    if (aPath === "vsize") {
      vsizeAmounts.push(aAmount);
    } else if (aPath === "resident") {
      residentAmounts.push(aAmount);
    } else if (aPath.search(/^js-main-runtime-gc-heap-committed\/used\/gc-things\//) >= 0) {
      jsGcHeapUsedGcThingsTotal += aAmount;
      jsGcHeapUsedGcThings[aPath] = (jsGcHeapUsedGcThings[aPath] | 0) + 1;
    } else if (aPath === "heap-allocated") {
      heapAllocatedAmounts.push(aAmount);
    } else if (aPath === "storage-sqlite") {
      storageSqliteAmounts.push(aAmount);

    // Check the presence of some other notable reporters.
    } else if (aPath.search(/^explicit\/js-non-window\/.*realm\(/) >= 0) {
      present.jsNonWindowRealms = true;
    } else if (aPath.search(/^explicit\/window-objects\/top\(.*\/js-realm\(/) >= 0) {
      present.windowObjectsJsRealms = true;
    } else if (aPath.search(/^explicit\/storage\/sqlite\/places.sqlite/) >= 0) {
      present.places = true;
    } else if (aPath.search(/^explicit\/images/) >= 0) {
      present.images = true;
    } else if (aPath.search(/^explicit\/atoms\/dynamic-objects-and-chars$/) >= 0) {
      present.dynamicObjectsAndChars = true;
    } else if (/\[System Principal\].*this-is-a-sandbox-name/.test(aPath)) {
      // A system compartment with a location (such as a sandbox) should
      // show that location.
      present.sandboxLocation = true;
    } else if (aPath.includes(bigStringPrefix)) {
      present.bigString = true;
    } else if (aPath.includes("!)(*&")) {
      present.smallString1 = true;
    } else if (aPath.includes("@)(*&")) {
      present.smallString2 = true;
    }

    // Shouldn't get any anonymized paths.
    if (aPath.includes('<anonymized')) {
        present.anonymizedWhenUnnecessary = aPath;
    }
  }

  function handleReportAnonymized(aProcess, aPath)
  {
    // Path might include an xmlns using http, which is safe to ignore.
    let reducedPath = aPath.replace(XUL_NS, "");

    // Shouldn't get http: or https: in any paths.
    if (reducedPath.includes('http:')) {
        present.httpWhenAnonymized = aPath;
    }

    // file: URLs should have their path anonymized.
    if (reducedPath.search('file:..[^<]') !== -1) {
        present.unanonymizedFilePathWhenAnonymized = aPath;
    }
  }

  let mgr = Cc["@mozilla.org/memory-reporter-manager;1"].
            getService(Ci.nsIMemoryReporterManager);

  let amounts = [
    "vsize",
    "vsizeMaxContiguous",
    "resident",
    "residentFast",
    "residentPeak",
    "residentUnique",
    "heapAllocated",
    "heapOverheadFraction",
    "JSMainRuntimeGCHeap",
    "JSMainRuntimeTemporaryPeak",
    "JSMainRuntimeRealmsSystem",
    "JSMainRuntimeRealmsUser",
    "imagesContentUsedUncompressed",
    "storageSQLite",
    "lowMemoryEventsPhysical",
    "ghostWindows",
    "pageFaultsHard",
  ];
  for (let i = 0; i < amounts.length; i++) {
    try {
      // If mgr[amounts[i]] throws an exception, just move on -- some amounts
      // aren't available on all platforms.  But if the attribute simply
      // isn't present, that indicates the distinguished amounts have changed
      // and this file hasn't been updated appropriately.
      let dummy = mgr[amounts[i]];
      ok(dummy !== undefined,
         "accessed an unknown distinguished amount: " + amounts[i]);
    } catch (ex) {
    }
  }

  // Run sizeOfTab() to make sure it doesn't crash.  We can't check the result
  // values because they're non-deterministic.
  let jsObjectsSize = {};
  let jsStringsSize = {};
  let jsOtherSize = {};
  let domSize = {};
  let styleSize = {};
  let otherSize = {};
  let totalSize = {};
  let jsMilliseconds = {};
  let nonJSMilliseconds = {};
  mgr.sizeOfTab(window, jsObjectsSize, jsStringsSize, jsOtherSize,
                domSize, styleSize, otherSize, totalSize,
                jsMilliseconds, nonJSMilliseconds);

  let asyncSteps = [
    getReportsNormal,
    getReportsAnonymized,
    checkResults,
    test_register_strong,
    test_register_strong, // Make sure re-registering works
    test_register_weak,
    SimpleTest.finish
  ];

  function runNext() {
    setTimeout(asyncSteps.shift(), 0);
  }

  function getReportsNormal()
  {
    mgr.getReports(handleReportNormal, null,
                   runNext, null,
                   /* anonymize = */ false);
  }

  function getReportsAnonymized()
  {
    mgr.getReports(handleReportAnonymized, null,
                   runNext, null,
                   /* anonymize = */ true);
  }

  function checkSizeReasonable(aName, aAmount)
  {
    // Check the size is reasonable -- i.e. not ridiculously large or small.
    ok(100 * 1000 <= aAmount && aAmount <= 10 * 1000 * 1000 * 1000,
       aName + "'s size is reasonable");
  }

  function checkSpecialReport(aName, aAmounts, aCanBeUnreasonable)
  {
    let socketProcessRunning = 0;
    if (SpecialPowers.Services.io.socketProcessLaunched) {
      socketProcessRunning = 1;
    }
    is(aAmounts.length, 1 + socketProcessRunning,
       aName + " has " + aAmounts.length + " report");
    let n = aAmounts[0];
    if (!aCanBeUnreasonable) {
      checkSizeReasonable(aName, n);
    }
  }

  function checkResults()
  {
    try {
      // Nb: mgr.heapAllocated will throw NS_ERROR_NOT_AVAILABLE if this is a
      // --disable-jemalloc build.  Allow for skipping this test on that
      // exception, but *only* that exception.
      // eslint-disable-next-line no-unused-vars
      let dummy = mgr.heapAllocated;
      checkSpecialReport("heap-allocated", heapAllocatedAmounts);
    } catch (ex) {
      is(ex.result, Cr.NS_ERROR_NOT_AVAILABLE, "mgr.heapAllocated exception");
    }
    // vsize may be unreasonable if ASAN is enabled
    checkSpecialReport("vsize",          vsizeAmounts, /*canBeUnreasonable*/true);
    checkSpecialReport("resident",       residentAmounts);

    for (var reporter in jsGcHeapUsedGcThings) {
      ok(jsGcHeapUsedGcThings[reporter] == 1);
    }
    checkSizeReasonable("js-main-runtime-gc-heap-committed/used/gc-things",
                        jsGcHeapUsedGcThingsTotal);

    ok(present.jsNonWindowRealms,           "js-non-window realms are present");
    ok(present.windowObjectsJsRealms,       "window-objects/.../js realms are present");
    ok(present.places,                      "places is present");
    ok(present.images,                      "images is present");
    ok(present.dynamicObjectsAndChars,      "dynamic-objects-and-chars is present");
    ok(present.sandboxLocation,             "sandbox locations are present");
    ok(present.bigString,                   "large string is present");
    ok(present.smallString1,                "small string 1 is present");
    ok(present.smallString2,                "small string 2 is present");

    ok(!present.anonymizedWhenUnnecessary,
       "anonymized paths are not present when unnecessary. Failed case: " +
       present.anonymizedWhenUnnecessary);
    ok(!present.httpWhenAnonymized,
       "http URLs are anonymized when necessary. Failed case: " +
       present.httpWhenAnonymized);
    ok(!present.unanonymizedFilePathWhenAnonymized,
       "file URLs are anonymized when necessary. Failed case: " +
       present.unanonymizedFilePathWhenAnonymized);

    runNext();
  }

  // Reporter registration tests

  // collectReports() calls to the test reporter.
  let called = 0;

  // The test memory reporter, testing the various report units.
  // Also acts as a report collector, verifying the reported values match the
  // expected ones after passing through XPConnect / nsMemoryReporterManager
  // and back.
  function MemoryReporterAndCallback() {
    this.seen = 0;
  }
  MemoryReporterAndCallback.prototype = {
    // The test reports.
    // Each test key corresponds to the path of the report.  |amount| is a
    // function called when generating the report.  |expected| is a function
    // to be tested when receiving a report during collection.  If |expected| is
    // omitted the |amount| will be checked instead.
    tests: {
      "test-memory-reporter-bytes1": {
        units: BYTES,
        amount: () => 0
      },
      "test-memory-reporter-bytes2": {
        units: BYTES,
        amount: () => (1<<30) * 8 // awkward way to say 8G in JS
      },
      "test-memory-reporter-counter": {
        units: COUNT,
        amount: () => 2
      },
      "test-memory-reporter-ccounter": {
        units: COUNT_CUMULATIVE,
        amount: () => ++called,
        expected: () => called
      },
      "test-memory-reporter-percentage": {
        units: PERCENTAGE,
        amount: () => 9999
      }
    },
    // nsIMemoryReporter
    collectReports(callback, data) {
      for (let path of Object.keys(this.tests)) {
        try {
          let test = this.tests[path];
          callback.callback(
            "", // Process. Should be "" initially.
            path,
            OTHER,
            test.units,
            test.amount(),
            "Test " + path + ".",
            data);
        }
        catch (ex) {
          ok(false, ex);
        }
      }
    },
    // nsIHandleReportCallback
    callback(process, path, kind, units, amount) {
      if (path in this.tests) {
        this.seen++;
        let test = this.tests[path];
        ok(units === test.units, "Test reporter units match");
        ok(amount === (test.expected || test.amount)(),
           "Test reporter values match: " + amount);
      }
    },
    // Checks that the callback has seen the expected number of reports, and
    // resets the callback counter.
    // @param expected  Optional.  Expected number of reports the callback
    //                  should have processed.
    finish(expected) {
      if (expected === undefined) {
        expected = Object.keys(this.tests).length;
      }
      is(expected, this.seen,
         "Test reporter called the correct number of times: " + expected);
      this.seen = 0;
    }
  };

  // General memory reporter + registerStrongReporter tests.
  function test_register_strong() {
    let reporterAndCallback = new MemoryReporterAndCallback();
    // Registration works.
    mgr.registerStrongReporter(reporterAndCallback);

    // Check the generated reports.
    mgr.getReports(reporterAndCallback, null,
      () => {
        reporterAndCallback.finish();
        window.setTimeout(test_unregister_strong, 0, reporterAndCallback);
      }, null,
      /* anonymize = */ false);
  }

  function test_unregister_strong(aReporterAndCallback)
  {
    mgr.unregisterStrongReporter(aReporterAndCallback);

    // The reporter was unregistered, hence there shouldn't be any reports from
    // the test reporter.
    mgr.getReports(aReporterAndCallback, null,
      () => {
        aReporterAndCallback.finish(0);
        runNext();
      }, null,
      /* anonymize = */ false);
  }

  // Check that you cannot register JS components as weak reporters.
  function test_register_weak() {
    let reporterAndCallback = new MemoryReporterAndCallback();
    try {
      // Should fail! nsMemoryReporterManager will only hold a raw pointer to
      // "weak" reporters.  When registering a weak reporter, XPConnect will
      // create a WrappedJS for JS components.  This WrappedJS would be
      // successfully registered with the manager, only to be destroyed
      // immediately after, which would eventually lead to a crash when
      // collecting the reports.  Therefore nsMemoryReporterManager should
      // reject WrappedJS reporters, which is what is tested here.
      // See bug 950391 comment #0.
      mgr.registerWeakReporter(reporterAndCallback);
      ok(false, "Shouldn't be allowed to register a JS component (WrappedJS)");
    }
    catch (ex) {
      ok(ex.message.includes("NS_ERROR_"),
         "WrappedJS reporter got rejected: " + ex);
    }

    runNext();
  }

  // Kick-off the async tests.
  runNext();

  ]]>
  </script>
</window>