summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/metrics/v8/gc_metric.html
blob: d842445ab160a354c7ca70aa951c3586c91d4d35 (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
<!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/range.html">
<link rel="import" href="/tracing/metrics/metric_registry.html">
<link rel="import" href="/tracing/metrics/v8/utils.html">
<link rel="import" href="/tracing/value/numeric.html">
<link rel="import" href="/tracing/value/unit.html">
<link rel="import" href="/tracing/value/value.html">

<script>
'use strict';

tr.exportTo('tr.metrics.v8', function() {
  // The time window size for mutator utilization computation.
  // It is equal to the duration of one frame corresponding to 60 FPS rendering.
  var TARGET_FPS = 60;
  var MS_PER_SECOND = 1000;
  var WINDOW_SIZE_MS = MS_PER_SECOND / TARGET_FPS;

  function gcMetric(values, model) {
    addDurationOfTopEvents(values, model);
    addTotalDurationOfTopEvents(values, model);
    addDurationOfSubEvents(values, model);
    addIdleTimesOfTopEvents(values, model);
    addTotalIdleTimesOfTopEvents(values, model);
    addV8ExecuteMutatorUtilization(values, model);
  }

  tr.metrics.MetricRegistry.register(gcMetric);

  var timeDurationInMs_smallerIsBetter =
      tr.v.Unit.byName.timeDurationInMs_smallerIsBetter;
  var percentage_biggerIsBetter =
      tr.v.Unit.byName.normalizedPercentage_biggerIsBetter;

  var numericBuilder = new tr.v.NumericBuilder(
      timeDurationInMs_smallerIsBetter, 0);
  // 0.1 steps from 0 to 20 since it is the most common range.
  numericBuilder.addLinearBins(20, 200);
  // Exponentially increasing steps from 20 to 200.
  numericBuilder.addExponentialBins(200, 100);

  function createNumericForTopEventTime() {
    var n = numericBuilder.build();
    n.customizeSummaryOptions({
        avg: true,
        count: true,
        max: true,
        min: false,
        std: true,
        sum: true,
        percentile: [0.90]});
    return n;
  }

  function createNumericForSubEventTime() {
    var n = numericBuilder.build();
    n.customizeSummaryOptions({
        avg: true,
        count: false,
        max: true,
        min: false,
        std: false,
        sum: false,
        percentile: [0.90]
    });
    return n;
  }

  function createNumericForIdleTime() {
    var n = numericBuilder.build();
    n.customizeSummaryOptions({
        avg: true,
        count: false,
        max: true,
        min: false,
        std: false,
        sum: true,
        percentile: []
    });
    return n;
  }

  function createPercentage(numerator, denominator) {
    var percentage = denominator === 0 ? 0 : numerator / denominator * 100;
    return new tr.v.ScalarNumeric(percentage_biggerIsBetter, percentage);
  }

  /**
   * Example output:
   * - Animation-v8_gc_full_mark_compactor.
   */
  function addDurationOfTopEvents(values, model) {
    groupAndProcessEvents(model,
      tr.metrics.v8.utils.isTopGarbageCollectionEvent,
      tr.metrics.v8.utils.topGarbageCollectionEventName,
      function(stageTitle, name, events) {
        var cpuDuration = createNumericForTopEventTime();
        events.forEach(function(event) {
          cpuDuration.add(event.cpuDuration);
        });
        values.addValue(new tr.v.NumericValue(
            stageTitle + '-' + name, cpuDuration));
      }
    );
  }

  /**
   * Example output:
   * - Animation:v8_gc_total
   */
  function addTotalDurationOfTopEvents(values, model) {
    groupAndProcessEvents(model,
      tr.metrics.v8.utils.isTopGarbageCollectionEvent,
      event => 'v8-gc-total',
      function(stageTitle, name, events) {
        var cpuDuration = createNumericForTopEventTime();
        events.forEach(function(event) {
          cpuDuration.add(event.cpuDuration);
        });
        values.addValue(new tr.v.NumericValue(
            stageTitle + '-' + name, cpuDuration));
      }
    );
  }

  /**
   * Example output:
   * - Animation-v8-gc-full-mark-compactor-evacuate.
   */
  function addDurationOfSubEvents(values, model) {
    groupAndProcessEvents(model,
      tr.metrics.v8.utils.isSubGarbageCollectionEvent,
      tr.metrics.v8.utils.subGarbageCollectionEventName,
      function(stageTitle, name, events) {
        var cpuDuration = createNumericForSubEventTime();
        events.forEach(function(event) {
          cpuDuration.add(event.cpuDuration);
        });
        values.addValue(new tr.v.NumericValue(
            stageTitle + '-' + name, cpuDuration));
      }
    );
  }

  /**
   * Example output:
   * - Animation-v8-gc-full-mark-compactor_idle_deadline_overrun,
   * - Animation-v8-gc-full-mark-compactor_outside_idle,
   * - Animation-v8-gc-full-mark-compactor_percentage_idle.
   */
  function addIdleTimesOfTopEvents(values, model) {
    groupAndProcessEvents(model,
      tr.metrics.v8.utils.isTopGarbageCollectionEvent,
      tr.metrics.v8.utils.topGarbageCollectionEventName,
      function(stageTitle, name, events) {
        addIdleTimes(values, model, stageTitle, name, events);
      }
    );
  }

  /**
   * Example output:
   * - Animation-v8-gc-total_idle_deadline_overrun,
   * - Animation-v8-gc-total_outside_idle,
   * - Animation-v8-gc-total_percentage_idle.
   */
  function addTotalIdleTimesOfTopEvents(values, model) {
    groupAndProcessEvents(model,
      tr.metrics.v8.utils.isTopGarbageCollectionEvent,
      event => 'v8-gc-total',
      function(stageTitle, name, events) {
        addIdleTimes(values, model, stageTitle, name, events);
      }
    );
  }

  function addIdleTimes(values, model, stageTitle, name, events) {
    var cpuDuration = createNumericForIdleTime();
    var insideIdle = createNumericForIdleTime();
    var outsideIdle = createNumericForIdleTime();
    var idleDeadlineOverrun = createNumericForIdleTime();
    events.forEach(function(event) {
      var idleTask = tr.metrics.v8.utils.findParent(
          event, tr.metrics.v8.utils.isIdleTask);
      var inside = 0;
      var overrun = 0;
      if (idleTask) {
        var allottedTime = idleTask['args']['allotted_time_ms'];
        if (event.duration > allottedTime) {
          overrun = event.duration - allottedTime;
          // Don't count time over the deadline as being inside idle time.
          // Since the deadline should be relative to wall clock we
          // compare allotted_time_ms with wall duration instead of thread
          // duration, and then assume the thread duration was inside idle
          // for the same percentage of time.
          inside = event.cpuDuration * allottedTime / event.duration;
        } else {
          inside = event.cpuDuration;
        }
      }
      cpuDuration.add(event.cpuDuration);
      insideIdle.add(inside);
      outsideIdle.add(event.cpuDuration - inside);
      idleDeadlineOverrun.add(overrun);
    });
    values.addValue(new tr.v.NumericValue(
        stageTitle + '-' + name + '_idle_deadline_overrun',
        idleDeadlineOverrun));
    values.addValue(new tr.v.NumericValue(
        stageTitle + '-' + name + '_outside_idle', outsideIdle));
    var percentage = createPercentage(insideIdle.sum,
                                      cpuDuration.sum);
    values.addValue(new tr.v.NumericValue(
        stageTitle + '-' + name + '_percentage_idle', percentage));
  }

  function addV8ExecuteMutatorUtilization(values, model) {
    groupAndProcessEvents(model,
        tr.metrics.v8.utils.isTopV8ExecuteEvent,
        event => 'v8-execute',
        function(stageTitle, name, events) {
          events.sort((a, b) => a.start - b.start);
          var time = 0;
          var pauses = [];
          // Glue together the v8.execute events and adjust the GC pause
          // times accordingly.
          events.forEach(function(topEvent) {
            topEvent.iterateAllDescendents(function(e) {
              if (tr.metrics.v8.utils.isTopGarbageCollectionEvent(e)) {
                pauses.push({ start: e.start - topEvent.start + time,
                              end: e.end - topEvent.start + time });
              }
            });
            time += topEvent.duration;
          });
          // Now we have one big v8.execute interval from 0 to |time| and
          // a list of GC pauses.
          var mutatorUtilization = tr.metrics.v8.utils.mutatorUtilization(
              0, time, WINDOW_SIZE_MS, pauses);
          [0.90, 0.95, 0.99].forEach(function(percent) {
            var value = new tr.v.ScalarNumeric(percentage_biggerIsBetter,
                mutatorUtilization.percentile(1.0 - percent) * 100);
            values.addValue(new tr.v.NumericValue(
                stageTitle + '-v8-execute-mutator-utilization_pct_0' +
                percent * 100,
                value));
          });
          var value = new tr.v.ScalarNumeric(percentage_biggerIsBetter,
              mutatorUtilization.min);
          values.addValue(new tr.v.NumericValue(
              stageTitle + '-v8-execute-mutator-utilization_min', value));
        }
    );
  }

  /**
   * Filters events using the |filterCallback|, then groups events by the user
   * expectation stage title and the name computed using the |nameCallback|,
   * and then invokes the |processCallback| with the grouped events.
   * @param {Function} filterCallback Takes an event and returns a boolean.
   * @param {Function} nameCallback Takes event and returns a string.
   * @param {Function} processCallback Takes a stage title, a name, and
   *                   an array of events.
   */
  function groupAndProcessEvents(model, filterCallback,
                                 nameCallback, processCallback) {
    // Two level map: stageTitle -> name -> [events].
    var stageTitleToNameToEvents = {};
    model.userModel.expectations.forEach(function(ue) {
      stageTitleToNameToEvents[ue.stageTitle] =
        stageTitleToNameToEvents[ue.stageTitle] || {};
      var nameToEvents = stageTitleToNameToEvents[ue.stageTitle];
      ue.associatedEvents.forEach(function(event) {
        if (!filterCallback(event)) return;
        var name = nameCallback(event);
        nameToEvents[name] = nameToEvents[name] || [];
        nameToEvents[name].push(event);
      });
    });
    tr.b.iterItems(stageTitleToNameToEvents,
      function(stageTitle, nameToEvents) {
        tr.b.iterItems(nameToEvents, function(name, events) {
          processCallback(stageTitle, name, events);
        });
      }
    );
  }

  return {
    gcMetric: gcMetric,
    WINDOW_SIZE_MS: WINDOW_SIZE_MS // For testing purposes only.
  };
});
</script>