summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/analysis/memory_dump_heap_details_pane.html
blob: 05d4ad5d689b3ff29c81ecb2f7d418bca2fa5106 (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!DOCTYPE html>
<!--
Copyright 2015 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/color_scheme.html">
<link rel="import" href="/tracing/base/iteration_helpers.html">
<link rel="import" href="/tracing/base/multi_dimensional_view.html">
<link rel="import" href="/tracing/ui/analysis/memory_dump_sub_view_util.html">
<link rel="import" href="/tracing/ui/analysis/stacked_pane.html">
<link rel="import" href="/tracing/ui/base/dom_helpers.html">
<link rel='import' href='/tracing/ui/base/info_bar.html'>
<link rel="import" href="/tracing/ui/base/table.html">
<link rel="import" href="/tracing/value/numeric.html">
<link rel="import" href="/tracing/value/unit.html">

<polymer-element name="tr-ui-a-memory-dump-heap-details-pane"
    extends="tr-ui-a-stacked-pane">
  <template>
    <style>
      :host {
        display: flex;
        flex-direction: column;
      }

      #header {
        flex: 0 0 auto;
        display: flex;
        flex-direction: row;
        align-items: center;

        background-color: #eee;
        border-bottom: 1px solid #8e8e8e;
        border-top: 1px solid white;
      }

      #label {
        flex: 1 1 auto;
        padding: 8px;
        font-size:  15px;
        font-weight: bold;
      }

      #view_mode_container {
        display: none;
        flex: 0 0 auto;
        padding: 5px;
        font-size: 15px;
      }

      #contents {
        flex: 1 0 auto;
        align-self: stretch;
        font-size: 12px;
      }

      #info_text {
        padding: 8px;
        color: #666;
        font-style: italic;
        text-align: center;
      }

      #table {
        display: none;  /* Hide until memory allocator dumps are set. */
        flex: 1 0 auto;
        align-self: stretch;
      }
    </style>
    <div id="header">
      <div id="label">Heap details</div>
      <div id="view_mode_container">
        <span>View mode:</span>
        <!-- View mode selector (added in Polymer.ready()) -->
      </div>
    </div>
    <div id="contents">
      <tr-ui-b-info-bar id="info_bar" class="info-bar-hidden">
      </tr-ui-b-info-bar>
      <div id="info_text">No heap dump selected</div>
      <tr-ui-b-table id="table"></tr-ui-b-table>
    </div>
  </template>
</polymer-element>
<script>
'use strict';

tr.exportTo('tr.ui.analysis', function() {

  var ScalarNumeric = tr.v.ScalarNumeric;
  var sizeInBytes_smallerIsBetter =
      tr.v.Unit.byName.sizeInBytes_smallerIsBetter;
  var unitlessNumber_smallerIsBetter =
      tr.v.Unit.byName.unitlessNumber_smallerIsBetter;
  var MultiDimensionalViewBuilder = tr.b.MultiDimensionalViewBuilder;
  var TotalState = tr.b.MultiDimensionalViewNode.TotalState;

  /** @{enum} */
  var RowDimension = {
    ROOT: -1,
    STACK_FRAME: 0,
    OBJECT_TYPE: 1
  };

  var LATIN_SMALL_LETTER_F_WITH_HOOK = String.fromCharCode(0x0192);
  var CIRCLED_LATIN_CAPITAL_LETTER_T = String.fromCharCode(0x24C9);

  /** @{constructor} */
  function HeapDumpNodeTitleColumn(title) {
    tr.ui.analysis.TitleColumn.call(this, title);
  }

  HeapDumpNodeTitleColumn.prototype = {
    __proto__: tr.ui.analysis.TitleColumn.prototype,

    formatTitle: function(row) {
      var title = row.title;
      var dimension = row.dimension;
      switch (dimension) {
        case RowDimension.ROOT:
          return title;

        case RowDimension.STACK_FRAME:
        case RowDimension.OBJECT_TYPE:
          return this.formatSubRow_(title, dimension);

        default:
          throw new Error('Invalid row dimension: ' + row.dimension);
      }
    },

    cmp: function(rowA, rowB) {
      if (rowA.dimension !== rowB.dimension)
        return rowA.dimension - rowB.dimension;
      return tr.ui.analysis.TitleColumn.prototype.cmp.call(this, rowA, rowB);
    },

    formatSubRow_: function(title, dimension) {
      var titleEl = document.createElement('span');

      var symbolEl = document.createElement('span');
      var symbolColorName;
      if (dimension === RowDimension.STACK_FRAME) {
        symbolEl.textContent = LATIN_SMALL_LETTER_F_WITH_HOOK;
        symbolEl.title = 'Stack frame';
        symbolColorName = 'heap_dump_stack_frame';
      } else {
        symbolEl.textContent = CIRCLED_LATIN_CAPITAL_LETTER_T;
        symbolEl.title = 'Object type';
        symbolColorName = 'heap_dump_object_type';
      }
      symbolEl.style.color =
          tr.b.ColorScheme.getColorForReservedNameAsString(symbolColorName);
      symbolEl.style.paddingRight = '4px';
      symbolEl.style.cursor = 'help';
      symbolEl.style.weight = 'bold';
      titleEl.appendChild(symbolEl);

      titleEl.appendChild(document.createTextNode(title));

      return titleEl;
    }
  };

  /** @constructor */
  function AllocationCountColumn(name, cellPath, aggregationMode) {
    tr.ui.analysis.DetailsNumericMemoryColumn.call(
        this, name, cellPath, aggregationMode);
  }

  AllocationCountColumn.prototype = {
    __proto__: tr.ui.analysis.DetailsNumericMemoryColumn.prototype,

    getFormattingContext: function(unit) {
      return { minimumFractionDigits: 0 };
    }
  };

  var COLUMN_RULES = [
    {
      condition: 'Size',
      importance: 3,
      columnConstructor: tr.ui.analysis.DetailsNumericMemoryColumn
    },
    {
      condition: 'Count',
      importance: 2,
      columnConstructor: AllocationCountColumn
    },
    {
      condition: 'Average size per allocation',
      importance: 1,
      columnConstructor: tr.ui.analysis.DetailsNumericMemoryColumn
    },
    {
      importance: 0,
      columnConstructor: tr.ui.analysis.DetailsNumericMemoryColumn
    }
  ];

  Polymer('tr-ui-a-memory-dump-heap-details-pane', {
    created: function() {
      this.heapDumps_ = undefined;
      this.aggregationMode_ = undefined;
      this.viewMode_ = undefined;
    },

    ready: function() {
      this.$.table.selectionMode = tr.ui.b.TableFormat.SelectionMode.ROW;
      this.$.info_bar.message = 'Note: Values displayed in the heavy view ' +
          'are lower bounds (except for the root).';

      this.$.view_mode_container.appendChild(tr.ui.b.createSelector(
          this, 'viewMode', 'memoryDumpHeapDetailsPane.viewMode',
          MultiDimensionalViewBuilder.ViewType.TOP_DOWN_TREE_VIEW,
          [
            {
              label: 'Top-down (Tree)',
              value: MultiDimensionalViewBuilder.ViewType.TOP_DOWN_TREE_VIEW
            },
            {
              label: 'Top-down (Heavy)',
              value: MultiDimensionalViewBuilder.ViewType.TOP_DOWN_HEAVY_VIEW
            },
            {
              label: 'Bottom-up (Heavy)',
              value: MultiDimensionalViewBuilder.ViewType.BOTTOM_UP_HEAVY_VIEW
            }
          ]));
    },

    /**
     * Sets the heap dumps and schedules rebuilding the pane.
     *
     * The provided value should be a chronological list of heap dumps. All
     * dumps are assumed to belong to the same process and belong to the same
     * allocator. Example:
     *
     *   [
     *     tr.model.HeapDump {},  // Heap dump at timestamp 1.
     *     undefined,  // Heap dump not provided at timestamp 2.
     *     tr.model.HeapDump {},  // Heap dump at timestamp 3.
     *   ]
     */
    set heapDumps(heapDumps) {
      this.heapDumps_ = heapDumps;
      this.scheduleRebuildPane_();
    },

    get heapDumps() {
      return this.heapDumps_;
    },

    set aggregationMode(aggregationMode) {
      this.aggregationMode_ = aggregationMode;
      this.scheduleRebuildPane_();
    },

    get aggregationMode() {
      return this.aggregationMode_;
    },

    set viewMode(viewMode) {
      this.viewMode_ = viewMode;
      this.scheduleRebuildPane_();
    },

    get viewMode() {
      return this.viewMode_;
    },

    get heavyView() {
      switch (this.viewMode) {
        case MultiDimensionalViewBuilder.ViewType.TOP_DOWN_HEAVY_VIEW:
        case MultiDimensionalViewBuilder.ViewType.BOTTOM_UP_HEAVY_VIEW:
          return true;
        default:
          return false;
      }
    },

    rebuildPane_: function() {
      if (this.heapDumps_ === undefined ||
          this.heapDumps_.length === 0) {
        // Show the info text (hide the table and the view mode selector).
        this.$.info_text.style.display = 'block';
        this.$.table.style.display = 'none';
        this.$.view_mode_container.style.display = 'none';
        this.$.info_bar.visible = false;

        this.$.table.clear();
        this.$.table.rebuild();
        return;
      }

      // Show the table and the view mode selector (hide the info text).
      this.$.info_text.style.display = 'none';
      this.$.table.style.display = 'block';
      this.$.view_mode_container.style.display = 'block';

      // Show the info bar if in heavy view mode.
      this.$.info_bar.visible = this.heavyView;

      var stackFrameTrees = this.createStackFrameTrees_(this.heapDumps_);
      var rows = this.createRows_(stackFrameTrees);
      var columns = this.createColumns_(rows);

      this.$.table.tableRows = rows;
      this.$.table.tableColumns = columns;
      this.$.table.rebuild();
      tr.ui.analysis.expandTableRowsRecursively(this.$.table);
    },

    createStackFrameTrees_: function(heapDumps) {
      return heapDumps.map(function(heapDump) {
        if (heapDump === undefined)
          return undefined;

        var builder = new MultiDimensionalViewBuilder(
            2 /* dimensions (stack frames, object type) */,
            2 /* valueCount (size, count) */);

        // Build the heap tree.
        heapDump.entries.forEach(function(entry) {
          var leafStackFrame = entry.leafStackFrame;
          var stackTracePath = leafStackFrame === undefined ?
              [] : leafStackFrame.getUserFriendlyStackTrace().reverse();

          var objectTypeName = entry.objectTypeName;
          var objectTypeNamePath = objectTypeName === undefined ?
              [] : [objectTypeName];

          builder.addPath([stackTracePath, objectTypeNamePath],
              [entry.size, entry.count],
              MultiDimensionalViewBuilder.ValueKind.TOTAL);
        }, this);

        return builder.buildView(this.viewMode);
      }, this);
    },

    createRows_: function(stackFrameTrees) {
      var definedHeapDump = tr.b.findFirstInArray(this.heapDumps);
      if (definedHeapDump === undefined)
        return [];

      // The title of the root row is the name of the allocator.
      var rootRowTitle = definedHeapDump.allocatorName;
      return [this.createHeapRowRecursively_(
          stackFrameTrees, RowDimension.ROOT, rootRowTitle)];
    },

    createHeapRowRecursively_: function(nodes, dimension, title) {
      // Transform a chronological list of stack frame tree nodes into a
      // dictionary of cells (where each cell contains a chronological list
      // of the values of its numeric).
      var cells = tr.ui.analysis.createCells(nodes, function(node) {
        var size = node.values[0].total;
        var row = {
          'Size': new ScalarNumeric(sizeInBytes_smallerIsBetter, size)
        };
        var countValue = node.values[1];
        if (countValue.totalState >= this.minDisplayedTotalState_) {
          var count = countValue.total;
          row['Count'] = new ScalarNumeric(unitlessNumber_smallerIsBetter,
              count);
          row['Average size per allocation'] = new ScalarNumeric(
              sizeInBytes_smallerIsBetter, count === 0 ? 0 : size / count);
        }
        return row;
      }, this);

      var row = {
        dimension: dimension,
        title: title,
        contexts: nodes,
        cells: cells
      };

      // Recursively create sub-rows for children (if applicable).
      var stackFrameSubRows = this.createHeapDimensionSubRowsRecursively_(
          nodes, RowDimension.STACK_FRAME);
      var objectTypeSubRows = this.createHeapDimensionSubRowsRecursively_(
          nodes, RowDimension.OBJECT_TYPE);
      var subRows = stackFrameSubRows.concat(objectTypeSubRows);
      if (subRows.length > 0)
        row.subRows = subRows;

      return row;
    },

    get minDisplayedTotalState_() {
      if (this.heavyView) {
        // Show lower-bound and exact values in heavy views.
        return TotalState.LOWER_BOUND;
      } else {
        // Show only exact values in tree view.
        return TotalState.EXACT;
      }
    },

    createHeapDimensionSubRowsRecursively_: function(nodes, dimension) {
      // Sub-row name (list index) -> Timestamp (list index) -> Child
      // MultiDimensionalViewNode.
      var dimensionGroupedChildNodes = tr.b.dictionaryValues(
          tr.b.invertArrayOfDicts(nodes, function(node) {
            var childDict = {};
            var displayedChildrenTotalSize = 0;
            var displayedChildrenTotalCount = 0;
            var hasDisplayedChildren = false;
            var allDisplayedChildrenHaveDisplayedCounts = true;
            for (var child of node.children[dimension].values()) {
              if (child.values[0].totalState < this.minDisplayedTotalState_)
                continue;
              if (child.values[1].totalState < this.minDisplayedTotalState_)
                allDisplayedChildrenHaveDisplayedCounts = false;
              childDict[child.title[dimension]] = child;
              displayedChildrenTotalSize += child.values[0].total;
              displayedChildrenTotalCount += child.values[1].total;
              hasDisplayedChildren = true;
            }

            var nodeTotalSize = node.values[0].total;
            var nodeTotalCount = node.values[1].total;

            // Add '<other>' node if necessary in tree-view.
            var hasUnclassifiedSizeOrCount =
                displayedChildrenTotalSize < nodeTotalSize ||
                displayedChildrenTotalCount < nodeTotalCount;
            if (!this.heavyView && hasUnclassifiedSizeOrCount &&
                hasDisplayedChildren) {
              var otherTitle = node.title.slice();
              otherTitle[dimension] = '<other>';
              childDict['<other>'] = {
                title: otherTitle,
                values: [
                  {
                    self: 0,
                    total: nodeTotalSize - displayedChildrenTotalSize,
                    totalState: this.minDisplayedTotalState_
                  },
                  {
                    self: 0,
                    total: nodeTotalCount - displayedChildrenTotalCount,
                    // Don't show allocation count of the '<other>' node if
                    // there is a displayed child node that did NOT display
                    // allocation count.
                    totalState: allDisplayedChildrenHaveDisplayedCounts ?
                        this.minDisplayedTotalState_ : TotalState.NOT_PROVIDED
                  }
                ],
                children: [new Map(), new Map()]
              };
            }

            return childDict;
          }, this));

      // Sub-row name (list index) -> Sub-row.
      return dimensionGroupedChildNodes.map(function(subRowNodes) {
        var subRowTitle = tr.b.findFirstInArray(subRowNodes).title[dimension];
        return this.createHeapRowRecursively_(
            subRowNodes, dimension, subRowTitle);
      }, this);
    },

    createColumns_: function(rows) {
      var titleColumn = new HeapDumpNodeTitleColumn('Stack frame');
      titleColumn.width = '500px';

      var numericColumns = tr.ui.analysis.MemoryColumn.fromRows(
          rows, 'cells', this.aggregationMode_, COLUMN_RULES);
      tr.ui.analysis.MemoryColumn.spaceEqually(numericColumns);

      var columns = [titleColumn].concat(numericColumns);
      return columns;
    }
  });

  return {
    // Exported for testing.
    RowDimension: RowDimension,
    AllocationCountColumn: AllocationCountColumn
  };
});
</script>