summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/extras/side_panel/time_summary_side_panel.html
blob: a0b6d9df6b6c616369bbbf4b69a7ab2d8112550e (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
432
433
434
435
436
437
438
<!DOCTYPE html>
<!--
Copyright (c) 2013 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/iteration_helpers.html">
<link rel="import" href="/tracing/base/statistics.html">
<link rel="import" href="/tracing/model/event_set.html">
<link rel="import" href="/tracing/ui/base/dom_helpers.html">
<link rel="import" href="/tracing/ui/base/pie_chart.html">
<link rel="import" href="/tracing/ui/side_panel/side_panel.html">
<link rel="import" href="/tracing/value/ui/scalar_span.html">
<link rel="import" href="/tracing/value/unit.html">

<polymer-element name="tr-ui-e-s-time-summary-side-panel"
    extends="tr-ui-side-panel">
  <template>
    <style>
    :host {
      flex-direction: column;
      display: flex;
    }
    toolbar {
      flex: 0 0 auto;
      border-bottom: 1px solid black;
      display: flex;
    }
    result-area {
      flex: 1 1 auto;
      display: block;
      min-height: 0;
      overflow-y: auto;
    }
    </style>

    <toolbar id='toolbar'></toolbar>
    <result-area id='result_area'></result-area>
  </template>

  <script>
  'use strict';
  (function() {
    var GROUP_BY_PROCESS_NAME = 'process';
    var GROUP_BY_THREAD_NAME = 'thread';

    var WALL_TIME_GROUPING_UNIT = 'Wall time';
    var CPU_TIME_GROUPING_UNIT = 'CPU time';

    /**
     * @constructor
     */
    function ResultsForGroup(model, name) {
      this.model = model;
      this.name = name;
      this.topLevelSlices = [];
      this.allSlices = [];
    }

    ResultsForGroup.prototype = {
      get wallTime() {
        var wallSum = tr.b.Statistics.sum(
            this.topLevelSlices, function(x) { return x.duration; });
        return wallSum;
      },

      get cpuTime() {
        var cpuDuration = 0;
        for (var i = 0; i < this.topLevelSlices.length; i++) {
          var x = this.topLevelSlices[i];
          // Only report thread-duration if we have it for all events.
          //
          // A thread_duration of 0 is valid, so this only returns 0 if it is
          // None.
          if (x.cpuDuration === undefined) {
            if (x.duration === undefined)
              continue;
            return 0;
          }
          cpuDuration += x.cpuDuration;
        }
        return cpuDuration;
      },

      appendGroupContents: function(group) {
        if (group.model != this.model)
          throw new Error('Models must be the same');

        group.allSlices.forEach(function(slice) {
          this.allSlices.push(slice);
        }, this);
        group.topLevelSlices.forEach(function(slice) {
          this.topLevelSlices.push(slice);
        }, this);
      },

      appendThreadSlices: function(rangeOfInterest, thread) {
        var tmp = this.getSlicesIntersectingRange(
            rangeOfInterest, thread.sliceGroup.slices);
        tmp.forEach(function(slice) {
          this.allSlices.push(slice);
        }, this);
        tmp = this.getSlicesIntersectingRange(
            rangeOfInterest, thread.sliceGroup.topLevelSlices);
        tmp.forEach(function(slice) {
          this.topLevelSlices.push(slice);
        }, this);
      },

      getSlicesIntersectingRange: function(rangeOfInterest, slices) {
        var slicesInFilterRange = [];
        for (var i = 0; i < slices.length; i++) {
          var slice = slices[i];
          if (rangeOfInterest.intersectsExplicitRangeInclusive(
                slice.start, slice.end))
            slicesInFilterRange.push(slice);
        }
        return slicesInFilterRange;
      }
    };

    Polymer({
      ready: function() {
        this.rangeOfInterest_ = new tr.b.Range();
        this.selection_ = undefined;
        this.groupBy_ = GROUP_BY_PROCESS_NAME;
        this.groupingUnit_ = CPU_TIME_GROUPING_UNIT;
        this.showCpuIdleTime_ = true;
        this.chart_ = undefined;

        var toolbarEl = this.$.toolbar;
        this.groupBySelector_ = tr.ui.b.createSelector(
            this, 'groupBy',
            'timeSummarySidePanel.groupBy', this.groupBy_,
            [{label: 'Group by process', value: GROUP_BY_PROCESS_NAME},
             {label: 'Group by thread', value: GROUP_BY_THREAD_NAME}
            ]);
        toolbarEl.appendChild(this.groupBySelector_);

        this.groupingUnitSelector_ = tr.ui.b.createSelector(
            this, 'groupingUnit',
            'timeSummarySidePanel.groupingUnit', this.groupingUnit_,
            [{label: 'Wall time', value: WALL_TIME_GROUPING_UNIT},
             {label: 'CPU time', value: CPU_TIME_GROUPING_UNIT}
            ]);
        toolbarEl.appendChild(this.groupingUnitSelector_);

        this.showCpuIdleTimeCheckbox_ = tr.ui.b.createCheckBox(
            this, 'showCpuIdleTime',
            'timeSummarySidePanel.showCpuIdleTime', this.showCpuIdleTime_,
            'Show CPU idle time');
        toolbarEl.appendChild(this.showCpuIdleTimeCheckbox_);
        this.updateShowCpuIdleTimeCheckboxVisibility_();
      },

      /**
       * This function takes an array of groups and merges smaller groups into
       * the provided 'Other' group item such that the remaining items are ready
       * for pie-chart consumption. Otherwise, the pie chart gets overwhelmed
       * with tons of little slices.
       */
      trimPieChartData: function(groups, otherGroup, getValue, opt_extraValue) {
        // Copy the array so it can be mutated.
        groups = groups.filter(function(d) {
          return getValue(d) != 0;
        });

        // Figure out total array range.
        var sum = tr.b.Statistics.sum(groups, getValue);
        if (opt_extraValue !== undefined)
          sum += opt_extraValue;

        // Sort by value.
        function compareByValue(a, b) {
          return getValue(a) - getValue(b);
        }
        groups.sort(compareByValue);

        // Now start fusing elements until none are less than threshold in size.
        var thresshold = 0.1 * sum;
        while (groups.length > 1) {
          var group = groups[0];
          if (getValue(group) >= thresshold)
            break;

          var v = getValue(group);
          if (v + getValue(otherGroup) > thresshold)
            break;

          // Remove the group from the list and add it to the 'Other' group.
          groups.splice(0, 1);
          otherGroup.appendGroupContents(group);
        }

        // Final return.
        if (getValue(otherGroup) > 0)
          groups.push(otherGroup);

        groups.sort(compareByValue);

        return groups;
      },

      generateResultsForGroup: function(model, name) {
        return new ResultsForGroup(model, name);
      },

      createPieChartFromResultGroups: function(
          groups, title, getValue, opt_extraData) {
        var chart = new tr.ui.b.PieChart();

        function pushDataForGroup(data, resultsForGroup, value) {
          data.push({
            label: resultsForGroup.name,
            value: value,
            valueText: tr.v.Unit.byName.timeDurationInMs.format(value),
            resultsForGroup: resultsForGroup
          });
        }
        chart.addEventListener('item-click', function(clickEvent) {
          var resultsForGroup = clickEvent.data.resultsForGroup;
          if (resultsForGroup === undefined)
            return;

          var event = new tr.model.RequestSelectionChangeEvent();
          event.selection = new tr.model.EventSet(resultsForGroup.allSlices);
          event.selection.timeSummaryGroupName = resultsForGroup.name;
          chart.dispatchEvent(event);
        });


        // Build chart data.
        var data = [];
        groups.forEach(function(resultsForGroup) {
          var value = getValue(resultsForGroup);
          if (value === 0)
            return;
          pushDataForGroup(data, resultsForGroup, value);
        });
        if (opt_extraData)
          data.push.apply(data, opt_extraData);

        chart.chartTitle = title;
        chart.data = data;
        return chart;
      },

      get model() {
        return this.model_;
      },

      set model(model) {
        this.model_ = model;
        this.updateContents_();
      },

      get groupBy() {
        return groupBy_;
      },

      set groupBy(groupBy) {
        this.groupBy_ = groupBy;
        if (this.groupBySelector_)
          this.groupBySelector_.selectedValue = groupBy;
        this.updateContents_();
      },

      get groupingUnit() {
        return groupingUnit_;
      },

      set groupingUnit(groupingUnit) {
        this.groupingUnit_ = groupingUnit;
        if (this.groupingUnitSelector_)
          this.groupingUnitSelector_.selectedValue = groupingUnit;
        this.updateShowCpuIdleTimeCheckboxVisibility_();
        this.updateContents_();
      },

      get showCpuIdleTime() {
        return this.showCpuIdleTime_;
      },

      set showCpuIdleTime(showCpuIdleTime) {
        this.showCpuIdleTime_ = showCpuIdleTime;
        if (this.showCpuIdleTimeCheckbox_)
          this.showCpuIdleTimeCheckbox_.checked = showCpuIdleTime;
        this.updateContents_();
      },

      updateShowCpuIdleTimeCheckboxVisibility_: function() {
        if (!this.showCpuIdleTimeCheckbox_)
          return;
        var visible = this.groupingUnit_ == CPU_TIME_GROUPING_UNIT;
        if (visible)
          this.showCpuIdleTimeCheckbox_.style.display = '';
        else
          this.showCpuIdleTimeCheckbox_.style.display = 'none';
      },

      getGroupNameForThread_: function(thread) {
        if (this.groupBy_ == GROUP_BY_THREAD_NAME)
          return thread.name ? thread.name : thread.userFriendlyName;

        if (this.groupBy_ == GROUP_BY_PROCESS_NAME)
          return thread.parent.userFriendlyName;
      },

      updateContents_: function() {
        var resultArea = this.$.result_area;
        this.chart_ = undefined;
        resultArea.textContent = '';

        if (this.model_ === undefined)
          return;

        var rangeOfInterest;
        if (this.rangeOfInterest_.isEmpty)
          rangeOfInterest = this.model_.bounds;
        else
          rangeOfInterest = this.rangeOfInterest_;

        var allGroup = this.generateResultsForGroup(this.model_, 'all');
        var resultsByGroupName = {};
        this.model_.getAllThreads().forEach(function(thread) {
          var groupName = this.getGroupNameForThread_(thread);
          if (resultsByGroupName[groupName] === undefined) {
            resultsByGroupName[groupName] = this.generateResultsForGroup(
                this.model_, groupName);
          }
          resultsByGroupName[groupName].appendThreadSlices(
              rangeOfInterest, thread);

          allGroup.appendThreadSlices(rangeOfInterest, thread);
        }, this);

        // Helper function for working with the produced group.
        var getValueFromGroup = function(group) {
          if (this.groupingUnit_ == WALL_TIME_GROUPING_UNIT)
            return group.wallTime;
          return group.cpuTime;
        }.bind(this);

        // Create summary.
        var summaryText = document.createElement('div');
        summaryText.appendChild(tr.ui.b.createSpan({
          textContent: 'Total ' + this.groupingUnit_ + ': ',
          bold: true}));
        summaryText.appendChild(tr.v.ui.createScalarSpan(
            getValueFromGroup(allGroup), {
              unit: tr.v.Unit.byName.timeDurationInMs,
              ownerDocument: this.ownerDocument
            }));
        resultArea.appendChild(summaryText);

        // If needed, add in the idle time.
        var extraValue = 0;
        var extraData = [];
        if (this.showCpuIdleTime_ &&
            this.groupingUnit_ === CPU_TIME_GROUPING_UNIT &&
            this.model.kernel.bestGuessAtCpuCount !== undefined) {
          var maxCpuTime = rangeOfInterest.range *
              this.model.kernel.bestGuessAtCpuCount;
          var idleTime = Math.max(0, maxCpuTime - allGroup.cpuTime);
          extraData.push({
            label: 'CPU Idle',
            value: idleTime,
            valueText: tr.v.Unit.byName.timeDurationInMs.format(idleTime)
          });
          extraValue += idleTime;
        }

        // Create the actual chart.
        var otherGroup = this.generateResultsForGroup(this.model_, 'Other');
        var groups = this.trimPieChartData(
            tr.b.dictionaryValues(resultsByGroupName),
            otherGroup,
            getValueFromGroup,
            extraValue);

        if (groups.length == 0) {
          resultArea.appendChild(tr.ui.b.createSpan({textContent: 'No data'}));
          return undefined;
        }

        this.chart_ = this.createPieChartFromResultGroups(
            groups,
            this.groupingUnit_ + ' breakdown by ' + this.groupBy_,
            getValueFromGroup, extraData);
        resultArea.appendChild(this.chart_);

        this.chart_.addEventListener('click', function() {
          var event = new tr.model.RequestSelectionChangeEvent();
          event.selection = new tr.c.EventSet([]);
          this.dispatchEvent(event);
        });
        this.chart_.setSize(this.chart_.getMinSize());
      },

      get selection() {
        return selection_;
      },

      set selection(selection) {
        this.selection_ = selection;

        if (this.chart_ === undefined)
          return;

        if (selection.timeSummaryGroupName)
          this.chart_.highlightedLegendKey = selection.timeSummaryGroupName;
        else
          this.chart_.highlightedLegendKey = undefined;
      },

      get rangeOfInterest() {
        return this.rangeOfInterest_;
      },

      set rangeOfInterest(rangeOfInterest) {
        this.rangeOfInterest_ = rangeOfInterest;
        this.updateContents_();
      },

      supportsModel: function(model) {
        return {
          supported: false
        };
      },

      get textLabel() {
        return 'Time Summary';
      }
    });
  }());
  </script>
</polymer-element>