summaryrefslogtreecommitdiffstats
path: root/js/src/devtools/gc-ubench/perf.js
blob: dc370ee0da35b3418acb37fbccc20299b4d91626 (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
/* 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/. */

// Performance monitoring and calculation.

function round_up(val, interval) {
  return val + (interval - (val % interval));
}

// Class for inter-frame timing, which handles being paused and resumed.
var FrameTimer = class {
  constructor() {
    // Start time of the current active test, adjusted for any time spent
    // stopped (so `now - this.start` is how long the current active test
    // has run for.)
    this.start = undefined;

    // Timestamp of callback following the previous frame.
    this.prev = undefined;

    // Timestamp when drawing was paused, or zero if drawing is active.
    this.stopped = 0;
  }

  is_stopped() {
    return this.stopped != 0;
  }

  start_recording(now = gHost.now()) {
    this.start = this.prev = now;
  }

  on_frame_finished(now = gHost.now()) {
    const delay = now - this.prev;
    this.prev = now;
    return delay;
  }

  pause(now = gHost.now()) {
    this.stopped = now;
    // Abuse this.prev to store the time elapsed since the previous frame.
    // This will be used to adjust this.prev when we resume.
    this.prev = now - this.prev;
  }

  resume(now = gHost.now()) {
    this.prev = now - this.prev;
    const stop_duration = now - this.stopped;
    this.start += stop_duration;
    this.stopped = 0;
  }
};

// Per-frame time sampling infra.
var sampleTime = 16.666667; // ms
var sampleIndex = 0;

// Class for maintaining a rolling window of per-frame GC-related counters:
// inter-frame delay, minor/major/slice GC counts, cumulative bytes, etc.
var FrameHistory = class {
  constructor(numSamples) {
    // Private
    this._frameTimer = new FrameTimer();
    this._numSamples = numSamples;

    // Public API
    this.delays = new Array(numSamples);
    this.gcBytes = new Array(numSamples);
    this.mallocBytes = new Array(numSamples);
    this.gcs = new Array(numSamples);
    this.minorGCs = new Array(numSamples);
    this.majorGCs = new Array(numSamples);
    this.slices = new Array(numSamples);

    sampleIndex = 0;
    this.reset();
  }

  start(now = gHost.now()) {
    this._frameTimer.start_recording(now);
  }

  reset() {
    this.delays.fill(0);
    this.gcBytes.fill(0);
    this.mallocBytes.fill(0);
    this.gcs.fill(this.gcs[sampleIndex]);
    this.minorGCs.fill(this.minorGCs[sampleIndex]);
    this.majorGCs.fill(this.majorGCs[sampleIndex]);
    this.slices.fill(this.slices[sampleIndex]);

    sampleIndex = 0;
  }

  get numSamples() {
    return this._numSamples;
  }

  findMax(collection) {
    // Depends on having at least one non-negative entry, and unfilled
    // entries being <= max.
    var maxIndex = 0;
    for (let i = 0; i < this._numSamples; i++) {
      if (collection[i] >= collection[maxIndex]) {
        maxIndex = i;
      }
    }
    return maxIndex;
  }

  findMaxDelay() {
    return this.findMax(this.delays);
  }

  on_frame(now = gHost.now()) {
    const delay = this._frameTimer.on_frame_finished(now);

    // Total time elapsed while the active test has been running.
    var t = now - this._frameTimer.start;
    var newIndex = Math.round(t / sampleTime);
    while (sampleIndex < newIndex) {
      sampleIndex++;
      var idx = sampleIndex % this._numSamples;
      this.delays[idx] = delay;
      if (gHost.features.haveMemorySizes) {
        this.gcBytes[idx] = gHost.gcBytes;
        this.mallocBytes[idx] = gHost.mallocBytes;
      }
      if (gHost.features.haveGCCounts) {
        this.minorGCs[idx] = gHost.minorGCCount;
        this.majorGCs[idx] = gHost.majorGCCount;
        this.slices[idx] = gHost.GCSliceCount;
      }
    }

    return delay;
  }

  pause() {
    this._frameTimer.pause();
  }

  resume() {
    this._frameTimer.resume();
  }

  is_stopped() {
    return this._frameTimer.is_stopped();
  }
};

var PerfTracker = class {
  constructor() {
    // Private
    this._currentLoadStart = undefined;
    this._frameCount = undefined;
    this._mutating_ms = undefined;
    this._suspend_sec = undefined;
    this._minorGCs = undefined;
    this._majorGCs = undefined;

    // Public
    this.results = [];
  }

  on_load_start(load, now = gHost.now()) {
    this._currentLoadStart = now;
    this._frameCount = 0;
    this._mutating_ms = 0;
    this._suspend_sec = 0;
    this._majorGCs = gHost.majorGCCount;
    this._minorGCs = gHost.minorGCCount;
  }

  on_load_end(load, now = gHost.now()) {
    const elapsed_time = (now - this._currentLoadStart) / 1000;
    const full_time = round_up(elapsed_time, 1 / 60);
    const frame_60fps_limit = Math.round(full_time * 60);
    const dropped_60fps_frames = frame_60fps_limit - this._frameCount;
    const dropped_60fps_fraction = dropped_60fps_frames / frame_60fps_limit;

    const mutating_and_gc_fraction = this._mutating_ms / (full_time * 1000);

    const result = {
      load,
      elapsed_time,
      mutating: this._mutating_ms / 1000,
      mutating_and_gc_fraction,
      suspended: this._suspend_sec,
      full_time,
      frames: this._frameCount,
      dropped_60fps_frames,
      dropped_60fps_fraction,
      majorGCs: gHost.majorGCCount - this._majorGCs,
      minorGCs: gHost.minorGCCount - this._minorGCs,
    };
    this.results.push(result);

    this._currentLoadStart = undefined;
    this._frameCount = 0;

    return result;
  }

  after_suspend(wait_sec) {
    this._suspend_sec += wait_sec;
  }

  before_mutator(now = gHost.now()) {
    this._frameCount++;
  }

  after_mutator(start_time, end_time = gHost.now()) {
    this._mutating_ms += end_time - start_time;
  }
};