summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/browser/browser_TelemetryGC.js
blob: 262fd69ffaf2fafae365d564c7e28c03dd8943f9 (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
"use strict";

/*
 *********************************************************************************
 *                                                                               *
 *                                  WARNING                                      *
 *                                                                               *
 * If you adjust any of the constants here (slice limit, number of keys, etc.)   *
 * make sure to update the JSON schema at:                                       *
 * https://github.com/mozilla-services/mozilla-pipeline-schemas/blob/master/     *
 * telemetry/main.schema.json                                                    *
 *                                                                               *
 * Otherwise, pings may be dropped by the telemetry backend!                     *
 *                                                                               *
 ********************************************************************************/

const {GCTelemetry} = Cu.import("resource://gre/modules/GCTelemetry.jsm", {});

function check(entries) {
  const FIELDS = ["random", "worst"];

  // Check that all FIELDS are in |entries|.
  for (let f of FIELDS) {
    ok(f in entries, `${f} found in entries`);
  }

  // Check that only FIELDS are in |entries|.
  for (let k of Object.keys(entries)) {
    ok(FIELDS.includes(k), `${k} found in FIELDS`);
  }

  let foundGCs = 0;

  for (let f of FIELDS) {
    ok(Array.isArray(entries[f]), "have an array of GCs");

    ok(entries[f].length <= 2, "not too many GCs");

    for (let gc of entries[f]) {
      ok(gc !== null, "GC is non-null");

      foundGCs++;

      ok(Object.keys(gc).length <= 25, "number of keys in GC is not too large");

      // Sanity check the GC data.
      ok("total_time" in gc, "total_time field present");
      ok("max_pause" in gc, "max_pause field present");

      ok("slices" in gc, "slices field present");
      ok(Array.isArray(gc.slices), "slices is an array");
      ok(gc.slices.length > 0, "slices array non-empty");
      ok(gc.slices.length <= 4, "slices array is not too long");

      ok("totals" in gc, "totals field present");
      ok(typeof(gc.totals) == "object", "totals is an object");
      ok(Object.keys(gc.totals).length <= 65, "totals array is not too long");

      // Make sure we don't skip any big objects.
      for (let key in gc) {
        if (key != "slices" && key != "totals") {
          ok(typeof(gc[key]) != "object", `${key} property should be primitive`);
        }
      }

      let phases = new Set();

      for (let slice of gc.slices) {
        ok(Object.keys(slice).length <= 15, "slice is not too large");

        ok("pause" in slice, "pause field present in slice");
        ok("reason" in slice, "reason field present in slice");
        ok("times" in slice, "times field present in slice");

        // Make sure we don't skip any big objects.
        for (let key in slice) {
          if (key != "times") {
            ok(typeof(slice[key]) != "object", `${key} property should be primitive`);
          }
        }

        ok(Object.keys(slice.times).length <= 65, "no more than 65 phases");

        for (let phase in slice.times) {
          phases.add(phase);
          ok(typeof(slice.times[phase]) == "number", `${phase} property should be a number`);
        }
      }

      let totals = gc.totals;
      // Make sure we don't skip any big objects.
      for (let phase in totals) {
        ok(typeof(totals[phase]) == "number", `${phase} property should be a number`);
      }

      for (let phase of phases) {
        ok(phase in totals, `${phase} is in totals`);
      }
    }
  }

  ok(foundGCs > 0, "saw at least one GC");
}

add_task(function* test() {
  let multiprocess = Services.appinfo.browserTabsRemoteAutostart;

  // Set these prefs to ensure that we get measurements.
  const prefs = {"set": [["javascript.options.mem.notify", true]]};
  yield new Promise(resolve => SpecialPowers.pushPrefEnv(prefs, resolve));

  function runRemote(f) {
    gBrowser.selectedBrowser.messageManager.loadFrameScript(`data:,(${f})()`, false);
  }

  function initScript() {
    const {GCTelemetry} = Components.utils.import("resource://gre/modules/GCTelemetry.jsm", {});

    /*
     * Don't shut down GC telemetry if it was already running before the test!
     * Note: We need to use a multiline comment here since this code is turned into a data: URI.
     */
    let shutdown = GCTelemetry.init();

    function listener() {
      removeMessageListener("GCTelemTest:Shutdown", listener);
      if (shutdown) {
        GCTelemetry.shutdown();
      }
    }
    addMessageListener("GCTelemTest:Shutdown", listener);
  }

  if (multiprocess) {
    runRemote(initScript);
  }

  // Don't shut down GC telemetry if it was already running before the test!
  let shutdown = GCTelemetry.init();
  registerCleanupFunction(() => {
    if (shutdown) {
      GCTelemetry.shutdown();
    }

    gBrowser.selectedBrowser.messageManager.sendAsyncMessage("GCTelemTest:Shutdown");
  });

  let localPromise = new Promise(resolve => {
    function obs() {
      Services.obs.removeObserver(obs, "garbage-collection-statistics");
      resolve();
    }
    Services.obs.addObserver(obs, "garbage-collection-statistics", false);
  });

  let remotePromise;
  if (multiprocess) {
    remotePromise = new Promise(resolve => {
      function obs() {
        Services.ppmm.removeMessageListener("Telemetry:GCStatistics", obs);
        resolve();
      }
      Services.ppmm.addMessageListener("Telemetry:GCStatistics", obs);
    });
  } else {
    remotePromise = Promise.resolve();
  }

  // Make sure we have a GC to work with in both processes.
  Cu.forceGC();
  if (multiprocess) {
    runRemote(() => Components.utils.forceGC());
  }

  info("Waiting for GCs");

  yield Promise.all([localPromise, remotePromise]);

  let localEntries = GCTelemetry.entries("main", true);
  let remoteEntries = multiprocess ? GCTelemetry.entries("content", true) : localEntries;

  check(localEntries);
  check(remoteEntries);

  localEntries = GCTelemetry.entries("main", false);
  remoteEntries = multiprocess ? GCTelemetry.entries("content", false) : localEntries;

  is(localEntries.random.length, 0, "no random GCs after reset");
  is(localEntries.worst.length, 0, "no worst GCs after reset");

  is(remoteEntries.random.length, 0, "no random GCs after reset");
  is(remoteEntries.worst.length, 0, "no worst GCs after reset");
});