summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/analysis/memory_dump_heap_details_pane.html
blob: 7b53e66ab5cfa3f0693b40460410aec31fcff5c4 (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 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_heap_details_breakdown_view.html">
<link rel="import" href="/tracing/ui/analysis/memory_dump_heap_details_path_view.html">
<link rel="import" href="/tracing/ui/analysis/memory_dump_heap_details_util.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/drag_handle.html">
<link rel="import" href="/tracing/ui/base/info_bar.html">
<link rel="import" href="/tracing/value/numeric.html">

<dom-module id='tr-ui-a-memory-dump-heap-details-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;
      }

      #split_view {
        display: none;  /* Hide until memory allocator dumps are set. */
        flex: 1 0 auto;
        align-self: stretch;
        flex-direction: row;
      }

      #path_view {
        width: 50%;
      }

      #breakdown_view {
        flex: 1 1 auto;
        width: 0;
      }

      #path_view, #breakdown_view {
        overflow-x: auto;  /* Show scrollbar if necessary. */
      }
    </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>

      <div id="split_view">
        <tr-ui-a-memory-dump-heap-details-path-view id="path_view">
        </tr-ui-a-memory-dump-heap-details-path-view>
        <tr-ui-b-drag-handle id="drag_handle"></tr-ui-b-drag-handle>
        <tr-ui-a-memory-dump-heap-details-breakdown-view id="breakdown_view">
        </tr-ui-a-memory-dump-heap-details-breakdown-view>
      </div>
    </div>
  </template>
</dom-module>
<script>
'use strict';

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

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

  /** @{constructor} */
  function HeapDumpTreeNode(
      stackFrameNodes, dimension, title, heavyView, parentNode) {
    this.dimension = dimension;
    this.title = title;
    this.parentNode = parentNode;

    this.heavyView_ = heavyView;
    this.stackFrameNodes_ = stackFrameNodes;
    this.lazyCells_ = undefined;
    this.lazyChildNodes_ = undefined;
  }

  HeapDumpTreeNode.prototype = {
    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;
      }
    },

    get childNodes() {
      if (!this.lazyChildNodes_) {
        this.lazyChildNodes_ = new Map();
        this.addDimensionChildNodes_(
            tr.ui.analysis.HeapDetailsRowDimension.STACK_FRAME, 0);
        this.addDimensionChildNodes_(
            tr.ui.analysis.HeapDetailsRowDimension.OBJECT_TYPE, 1);
        this.releaseStackFrameNodesIfPossible_();
      }
      return this.lazyChildNodes_;
    },

    get cells() {
      if (!this.lazyCells_) {
        this.addCells_();
        this.releaseStackFrameNodesIfPossible_();
      }
      return this.lazyCells_;
    },

    releaseStackFrameNodesIfPossible_: function() {
      if (this.lazyCells_ && this.lazyChildNodes_) {
        // Don't unnecessarily hold a reference to the stack frame nodes when
        // we don't need them anymore.
        this.stackFrameNodes_ = undefined;
      }
    },

    addDimensionChildNodes_: function(dimension, dimensionIndex) {
      // Child title -> Timestamp (list index) -> Child
      // MultiDimensionalViewNode.
      var dimensionChildTitleToStackFrameNodes = tr.b.invertArrayOfDicts(
          this.stackFrameNodes_,
          node => this.convertStackFrameNodeDimensionToChildDict_(
              node, dimensionIndex));

      // Child title (list index) -> Child HeapDumpTreeNode.
      var dimensionChildNodes = [];
      tr.b.iterItems(dimensionChildTitleToStackFrameNodes,
          function(childTitle, childStackFrameNodes) {
            dimensionChildNodes.push(new HeapDumpTreeNode(childStackFrameNodes,
                dimension, childTitle, this.heavyView_, this));
          }, this);
      this.lazyChildNodes_.set(dimension, dimensionChildNodes);
    },

    convertStackFrameNodeDimensionToChildDict_: function(
        stackFrameNode, dimensionIndex) {
      var childDict = {};

      var displayedChildrenTotalSize = 0;
      var displayedChildrenTotalCount = 0;
      var hasDisplayedChildren = false;
      var allDisplayedChildrenHaveDisplayedCounts = true;
      for (var child of stackFrameNode.children[dimensionIndex].values()) {
        if (child.values[0].totalState < this.minDisplayedTotalState_)
          continue;
        if (child.values[1].totalState < this.minDisplayedTotalState_)
          allDisplayedChildrenHaveDisplayedCounts = false;
        childDict[child.title[dimensionIndex]] = child;
        displayedChildrenTotalSize += child.values[0].total;
        displayedChildrenTotalCount += child.values[1].total;
        hasDisplayedChildren = true;
      }

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

      // Add '<other>' node if necessary in tree-view.
      var hasUnclassifiedSizeOrCount =
          displayedChildrenTotalSize < nodeTotalSize ||
          displayedChildrenTotalCount < nodeTotalCount;
      if (!this.heavyView_ && hasUnclassifiedSizeOrCount &&
          hasDisplayedChildren) {
        var otherTitle = stackFrameNode.title.slice();
        otherTitle[dimensionIndex] = '<other>';
        var otherNode = new tr.b.MultiDimensionalViewNode(otherTitle, 2);
        childDict[otherTitle[dimensionIndex]] = otherNode;

        // '<other>' node size.
        otherNode.values[0].total = nodeTotalSize - displayedChildrenTotalSize;
        otherNode.values[0].totalState = this.minDisplayedTotalState_;

        // '<other>' node allocation count.
        otherNode.values[1].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.
        otherNode.values[1].totalState =
            allDisplayedChildrenHaveDisplayedCounts ?
                this.minDisplayedTotalState_ : TotalState.NOT_PROVIDED;
      }

      return childDict;
    },

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

  Polymer({
    is: 'tr-ui-a-memory-dump-heap-details-pane',
    behaviors: [tr.ui.analysis.StackedPane],

    created: function() {
      this.heapDumps_ = undefined;
      this.viewMode_ = undefined;
      this.aggregationMode_ = undefined;
    },

    ready: function() {
      this.$.info_bar.message = 'Note: Values displayed in the heavy view ' +
          'are lower bounds (except for the root).';

      Polymer.dom(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
                }
              ]));

      this.$.drag_handle.target = this.$.path_view;
      this.$.drag_handle.horizontal = false;

      // If the user selects a node in the path view, show its children in the
      // breakdown view.
      this.$.path_view.addEventListener('selected-node-changed', (function(e) {
        this.$.breakdown_view.displayedNode = this.$.path_view.selectedNode;
      }).bind(this));

      // If the user double-clicks on a node in the breakdown view, select the
      // node in the path view.
      this.$.breakdown_view.addEventListener('enter-node', (function(e) {
        this.$.path_view.selectedNode = e.node;
      }).bind(this));
    },

    /**
     * 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.scheduleRebuild_();
    },

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

    set aggregationMode(aggregationMode) {
      this.aggregationMode_ = aggregationMode;
      this.$.path_view.aggregationMode = aggregationMode;
      this.$.breakdown_view.aggregationMode = aggregationMode;
    },

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

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

    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;
      }
    },

    onRebuild_: 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.$.split_view.style.display = 'none';
        this.$.view_mode_container.style.display = 'none';
        this.$.info_bar.visible = false;
        this.$.path_view.selectedNode = undefined;
        return;
      }

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

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

      this.$.path_view.selectedNode = this.createHeapTree_();
      this.$.path_view.rebuild();
      this.$.breakdown_view.rebuild();
    },

    createHeapTree_: function() {
      var definedHeapDump = tr.b.findFirstInArray(this.heapDumps_);
      if (definedHeapDump === undefined)
        return undefined;

      // The title of the root node is the name of the allocator.
      var rootRowTitle = definedHeapDump.allocatorName;

      var stackFrameTrees = this.createStackFrameTrees_(this.heapDumps_);

      return new HeapDumpTreeNode(stackFrameTrees,
          tr.ui.analysis.HeapDetailsRowDimension.ROOT, rootRowTitle,
          this.heavyView);
    },

    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);
    }
   });

  return {};
});
</script>