Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 4 | Use of this source code is governed by a BSD-style license that can be |
| 5 | found in the LICENSE file. |
| 6 | --> |
| 7 | |
| 8 | <link rel="import" href="/base/base.html"> |
| 9 | <script> |
| 10 | 'use strict'; |
| 11 | |
| 12 | /** |
| 13 | * @fileoverview Helper code for memory dump sub-views. |
| 14 | */ |
| 15 | tv.exportTo('tv.c.analysis', function() { |
| 16 | |
| 17 | /** |
| 18 | * A table builder column for displaying memory dump data. |
| 19 | * |
| 20 | * @constructor |
| 21 | */ |
| 22 | function MemoryColumn(title, units, cellGetter) { |
| 23 | this.title = title; |
| 24 | this.units = units; |
| 25 | this.cell = cellGetter; |
| 26 | } |
| 27 | |
| 28 | MemoryColumn.fromRows = function(rows, cellKey) { |
| 29 | var columnTraits = {}; |
| 30 | |
| 31 | function gatherTraits(row) { |
| 32 | if (row === undefined) |
| 33 | return; |
| 34 | var attrCells = row[cellKey]; |
| 35 | tv.b.iterItems(attrCells, function(attrName, attrCell) { |
| 36 | if (attrCell === undefined) |
| 37 | return; |
| 38 | var attrValue = attrCell.attr; |
| 39 | if (attrValue === undefined) |
| 40 | return; |
| 41 | var existingTraits = columnTraits[attrName]; |
| 42 | if (existingTraits === undefined) { |
| 43 | columnTraits[attrName] = { |
| 44 | constructor: attrValue.constructor, |
| 45 | units: attrValue.units |
| 46 | }; |
| 47 | return; |
| 48 | } |
| 49 | if (existingTraits.constructor !== attrValue.constructor || |
| 50 | existingTraits.units !== attrValue.units) { |
| 51 | existingTraits.constructor = tv.c.trace_model.UnknownAttribute; |
| 52 | existingTraits.units = undefined; |
| 53 | } |
| 54 | }); |
| 55 | if (row.subRows !== undefined) |
| 56 | row.subRows.forEach(gatherTraits); |
| 57 | }; |
| 58 | rows.forEach(gatherTraits); |
| 59 | |
| 60 | var columns = []; |
| 61 | tv.b.iterItems(columnTraits, function(columnName, columnTraits) { |
| 62 | var cellGetter = fieldGetter(cellKey, columnName); |
| 63 | columns.push(MemoryColumn.fromAttributeTraits( |
| 64 | columnName, columnTraits, cellGetter)); |
| 65 | }); |
| 66 | |
| 67 | return columns; |
| 68 | }; |
| 69 | |
| 70 | MemoryColumn.fromAttributeTraits = function(title, traits, cellGetter) { |
| 71 | var constructor; |
| 72 | if (traits.constructor === tv.c.trace_model.ScalarAttribute) |
| 73 | constructor = ScalarMemoryColumn; |
| 74 | else |
| 75 | constructor = MemoryColumn; |
| 76 | return new constructor(title, traits.units, cellGetter); |
| 77 | }; |
| 78 | |
| 79 | MemoryColumn.spaceEqually = function(columns) { |
| 80 | var columnWidth = (100 / columns.length).toFixed(3) + '%'; |
| 81 | columns.forEach(function(column) { |
| 82 | column.width = columnWidth; |
| 83 | }); |
| 84 | }; |
| 85 | |
| 86 | MemoryColumn.prototype = { |
| 87 | attr: function(row) { |
| 88 | var cell = this.cell(row); |
| 89 | if (cell === undefined) |
| 90 | return undefined; |
| 91 | return cell.attr; |
| 92 | }, |
| 93 | |
| 94 | value: function(row) { |
| 95 | var attr = this.attr(row); |
| 96 | if (attr === undefined) |
| 97 | return ''; |
| 98 | return this.formatDefinedAttribute(attr); |
| 99 | }, |
| 100 | |
| 101 | formatDefinedAttribute: function(attr) { |
| 102 | return String(attr.value); |
| 103 | }, |
| 104 | |
| 105 | cmp: function(rowA, rowB) { |
| 106 | var attrA = this.attr(rowA); |
| 107 | var attrB = this.attr(rowB); |
| 108 | if (attrA === undefined && attrB === undefined) |
| 109 | return 0; |
| 110 | if (attrA === undefined) |
| 111 | return -1; |
| 112 | if (attrB === undefined) |
| 113 | return 1; |
| 114 | return this.compareDefinedAttributes(attrA, attrB); |
| 115 | }, |
| 116 | |
| 117 | compareDefinedAttributes: function(attrA, attrB) { |
| 118 | var strA = String(attrA.value); |
| 119 | var strB = String(attrB.value); |
| 120 | return strA.localeCompare(strB); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * @constructor |
| 126 | */ |
| 127 | function ScalarMemoryColumn(title, units, cellGetter) { |
| 128 | MemoryColumn.call(this, title, units, cellGetter); |
| 129 | } |
| 130 | |
| 131 | ScalarMemoryColumn.prototype = { |
| 132 | __proto__: MemoryColumn.prototype, |
| 133 | |
| 134 | formatDefinedAttribute: function(attr) { |
| 135 | if (this.units === 'bytes') { |
| 136 | var sizeEl = document.createElement('tv-c-a-size-span'); |
| 137 | sizeEl.numBytes = attr.value; |
| 138 | return sizeEl; |
| 139 | } |
| 140 | return MemoryColumn.prototype.formatDefinedAttribute.call(this, attr); |
| 141 | }, |
| 142 | |
| 143 | compareDefinedAttributes: function(attrA, attrB) { |
| 144 | return attrA.value - attrB.value; |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * @constructor |
| 150 | */ |
| 151 | function MemoryCell(attr) { |
| 152 | this.attr = attr; |
| 153 | } |
| 154 | |
| 155 | function fieldGetter(/* fields */) { |
| 156 | var fields = tv.b.asArray(arguments); |
| 157 | return function(row) { |
| 158 | var value = row; |
| 159 | for (var i = 0; i < fields.length; i++) |
| 160 | value = value[fields[i]]; |
| 161 | return value; |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | return { |
| 166 | MemoryColumn: MemoryColumn, |
| 167 | ScalarMemoryColumn: ScalarMemoryColumn, |
| 168 | MemoryCell: MemoryCell, |
| 169 | fieldGetter: fieldGetter |
| 170 | }; |
| 171 | }); |
| 172 | </script> |