summaryrefslogtreecommitdiffstats
path: root/js/src/devtools/gc-ubench/harness.js
blob: 124baa17a539294b235abf5343003b4adff8f5b6 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Global defaults

// Allocate this much "garbage" per frame. This might correspond exactly to a
// number of objects/values, or it might be some set of objects, depending on
// the mutator in question.
var gDefaultGarbagePerFrame = "8K";

// In order to avoid a performance cliff between when the per-frame garbage
// fits in the nursery and when it doesn't, most mutators will collect multiple
// "piles" of garbage and round-robin through them, so that the per-frame
// garbage stays alive for some number of frames. There will still be some
// internal temporary allocations that don't end up in the piles; presumably,
// the nursery will take care of those.
//
// If the per-frame garbage is K and the number of piles is P, then some of the
// garbage will start getting tenured as long as P*K > size(nursery).
var gDefaultGarbagePiles = "8";

var gDefaultTestDuration = 8.0;

// The Host interface that provides functionality needed by the test harnesses
// (web + various shells). Subclasses should override with the appropriate
// functionality. The methods that throw an error must be implemented. The ones
// that return undefined are optional.
//
// Note that currently the web UI doesn't really use the scheduling pieces of
// this.
var Host = class {
  constructor() {}
  start_turn() {
    throw new Error("unimplemented");
  }
  end_turn() {
    throw new Error("unimplemented");
  }
  suspend(duration) {
    throw new Error("unimplemented");
  } // Shell driver only
  now() {
    return performance.now();
  }

  minorGCCount() {
    return undefined;
  }
  majorGCCount() {
    return undefined;
  }
  GCSliceCount() {
    return undefined;
  }

  features = {
    haveMemorySizes: false,
    haveGCCounts: false,
  };
};

function percent(x) {
  return `${(x*100).toFixed(2)}%`;
}

function parse_units(v) {
  if (!v.length) {
    return NaN;
  }
  var lastChar = v[v.length - 1].toLowerCase();
  if (!isNaN(parseFloat(lastChar))) {
    return parseFloat(v);
  }
  var units = parseFloat(v.substr(0, v.length - 1));
  if (lastChar == "k") {
    return units * 1e3;
  }
  if (lastChar == "m") {
    return units * 1e6;
  }
  if (lastChar == "g") {
    return units * 1e9;
  }
  return NaN;
}

var AllocationLoad = class {
  constructor(info, name) {
    this.load = info;
    this.load.name = this.load.name ?? name;

    this._garbagePerFrame =
      info.garbagePerFrame ||
      parse_units(info.defaultGarbagePerFrame || gDefaultGarbagePerFrame);
    this._garbagePiles =
      info.garbagePiles ||
      parse_units(info.defaultGarbagePiles || gDefaultGarbagePiles);
  }

  get name() {
    return this.load.name;
  }
  get description() {
    return this.load.description;
  }
  get garbagePerFrame() {
    return this._garbagePerFrame;
  }
  set garbagePerFrame(amount) {
    this._garbagePerFrame = amount;
  }
  get garbagePiles() {
    return this._garbagePiles;
  }
  set garbagePiles(amount) {
    this._garbagePiles = amount;
  }

  start() {
    this.load.load(this._garbagePiles);
  }

  stop() {
    this.load.unload();
  }

  reload() {
    this.stop();
    this.start();
  }

  tick() {
    this.load.makeGarbage(this._garbagePerFrame);
  }

  is_dummy_load() {
    return this.load.name == "noAllocation";
  }
};

var AllocationLoadManager = class {
  constructor(tests) {
    this._loads = new Map();
    for (const [name, info] of tests.entries()) {
      this._loads.set(name, new AllocationLoad(info, name));
    }
    this._active = undefined;
    this._paused = false;

    // Public API
    this.sequencer = null;
    this.testDurationMS = gDefaultTestDuration * 1000;
  }

  getByName(name) {
    const mutator = this._loads.get(name);
    if (!mutator) {
      throw new Error(`invalid mutator '${name}'`);
    }
    return mutator;
  }

  activeLoad() {
    return this._active;
  }

  setActiveLoad(mutator) {
    if (this._active) {
      this._active.stop();
    }
    this._active = mutator;
    this._active.start();
  }

  deactivateLoad() {
    this._active.stop();
    this._active = undefined;
  }

  get paused() {
    return this._paused;
  }
  set paused(pause) {
    this._paused = pause;
  }

  load_running() {
    return this._active;
  }

  change_garbagePiles(amount) {
    if (this._active) {
      this._active.garbagePiles = amount;
      this._active.reload();
    }
  }

  change_garbagePerFrame(amount) {
    if (this._active) {
      this._active.garbagePerFrame = amount;
    }
  }

  tick(now = gHost.now()) {
    this.lastActive = this._active;
    let completed = false;

    if (this.sequencer) {
      if (this.sequencer.tick(now)) {
        completed = true;
        if (this.sequencer.current) {
          this.setActiveLoad(this.sequencer.current);
        } else {
          this.deactivateLoad();
        }
        if (this.sequencer.done()) {
          this.sequencer = null;
        }
      }
    }

    if (this._active && !this._paused) {
      this._active.tick();
    }

    return completed;
  }

  startSequencer(sequencer, now = gHost.now()) {
    this.sequencer = sequencer;
    this.sequencer.start(now);
    this.setActiveLoad(this.sequencer.current);
  }

  stopped() {
    return !this.sequencer || this.sequencer.done();
  }

  currentLoadRemaining(now = gHost.now()) {
    if (this.stopped()) {
      return 0;
    }

    // TODO: The web UI displays a countdown to the end of the current mutator.
    // This won't work for potential future things like "run until 3 major GCs
    // have been seen", so the API will need to be modified to provide
    // information in that case.
    return this.testDurationMS - this.sequencer.currentLoadElapsed(now);
  }
};

// Current test state.
var gLoadMgr = undefined;

function format_with_units(n, label, shortlabel, kbase) {
  function format(n, prefix, unit) {
    let s = Number.isInteger(n) ? n.toString() : n.toFixed(2);
    return `${s}${prefix}${unit}`;
  }

  if (n < kbase * 4) {
    return `${n} ${label}`;
  } else if (n < kbase ** 2 * 4) {
    return format(n / kbase, 'K', shortlabel);
  } else if (n < kbase ** 3 * 4) {
    return format(n / kbase ** 2, 'M', shortlabel);
  }
  return format(n / kbase ** 3, 'G', shortlabel);
}

function format_bytes(bytes) {
  return format_with_units(bytes, "bytes", "B", 1024);
}

function format_num(n) {
  return format_with_units(n, "", "", 1000);
}

function update_histogram(histogram, delay) {
  // Round to a whole number of 10us intervals to provide enough resolution to
  // capture a 16ms target with adequate accuracy.
  delay = Math.round(delay * 100) / 100;
  var current = histogram.has(delay) ? histogram.get(delay) : 0;
  histogram.set(delay, ++current);
}

// Compute a score based on the total ms we missed frames by per second.
function compute_test_score(histogram) {
  var score = 0;
  for (let [delay, count] of histogram) {
    score += Math.abs((delay - 1000 / 60) * count);
  }
  score = score / (gLoadMgr.testDurationMS / 1000);
  return Math.round(score * 1000) / 1000;
}

// Build a spark-lines histogram for the test results to show with the aggregate score.
function compute_spark_histogram_percents(histogram) {
  var ranges = [
    [-99999999, 16.6],
    [16.6, 16.8],
    [16.8, 25],
    [25, 33.4],
    [33.4, 60],
    [60, 100],
    [100, 300],
    [300, 99999999],
  ];
  var rescaled = new Map();
  for (let [delay] of histogram) {
    for (var i = 0; i < ranges.length; ++i) {
      var low = ranges[i][0];
      var high = ranges[i][1];
      if (low <= delay && delay < high) {
        update_histogram(rescaled, i);
        break;
      }
    }
  }
  var total = 0;
  for (const [, count] of rescaled) {
    total += count;
  }

  var spark = [];
  for (let i = 0; i < ranges.length; ++i) {
    const amt = rescaled.has(i) ? rescaled.get(i) : 0;
    spark.push(amt / total);
  }

  return spark;
}