2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 WebInspector.HeapSnapshotItem = function() { }
36 WebInspector.HeapSnapshotItem.prototype = {
40 itemIndex: function() { },
45 serialize: function() { }
50 * @implements {WebInspector.HeapSnapshotItem}
51 * @param {!WebInspector.HeapSnapshot} snapshot
52 * @param {number=} edgeIndex
54 WebInspector.HeapSnapshotEdge = function(snapshot, edgeIndex)
56 this._snapshot = snapshot;
57 this._edges = snapshot._containmentEdges;
58 this.edgeIndex = edgeIndex || 0;
61 WebInspector.HeapSnapshotEdge.prototype = {
63 * @return {!WebInspector.HeapSnapshotEdge}
67 return new WebInspector.HeapSnapshotEdge(this._snapshot, this.edgeIndex);
73 hasStringName: function()
75 throw new Error("Not implemented");
83 throw new Error("Not implemented");
87 * @return {!WebInspector.HeapSnapshotNode}
91 return this._snapshot.createNode(this.nodeIndex());
99 return this._edges[this.edgeIndex + this._snapshot._edgeToNodeOffset];
107 return "HeapSnapshotEdge: " + this.name();
115 return this._snapshot._edgeTypes[this._type()];
122 itemIndex: function()
124 return this.edgeIndex;
129 * @return {!WebInspector.HeapSnapshotCommon.Edge}
131 serialize: function()
133 return new WebInspector.HeapSnapshotCommon.Edge(this.name(), this.node().serialize(), this.type(), this.edgeIndex);
138 return this._edges[this.edgeIndex + this._snapshot._edgeTypeOffset];
146 WebInspector.HeapSnapshotItemIterator = function() { }
148 WebInspector.HeapSnapshotItemIterator.prototype = {
152 hasNext: function() { },
155 * @return {!WebInspector.HeapSnapshotItem}
157 item: function() { },
166 WebInspector.HeapSnapshotItemIndexProvider = function() { }
168 WebInspector.HeapSnapshotItemIndexProvider.prototype = {
170 * @param {number} newIndex
171 * @return {!WebInspector.HeapSnapshotItem}
173 itemForIndex: function(newIndex) { },
178 * @implements {WebInspector.HeapSnapshotItemIndexProvider}
179 * @param {!WebInspector.HeapSnapshot} snapshot
181 WebInspector.HeapSnapshotNodeIndexProvider = function(snapshot)
183 this._node = snapshot.createNode();
186 WebInspector.HeapSnapshotNodeIndexProvider.prototype = {
188 * @param {number} index
189 * @return {!WebInspector.HeapSnapshotNode}
191 itemForIndex: function(index)
193 this._node.nodeIndex = index;
201 * @implements {WebInspector.HeapSnapshotItemIndexProvider}
202 * @param {!WebInspector.HeapSnapshot} snapshot
204 WebInspector.HeapSnapshotEdgeIndexProvider = function(snapshot)
206 this._edge = snapshot.createEdge(0);
209 WebInspector.HeapSnapshotEdgeIndexProvider.prototype = {
211 * @param {number} index
212 * @return {!WebInspector.HeapSnapshotEdge}
214 itemForIndex: function(index)
216 this._edge.edgeIndex = index;
224 * @implements {WebInspector.HeapSnapshotItemIndexProvider}
225 * @param {!WebInspector.HeapSnapshot} snapshot
227 WebInspector.HeapSnapshotRetainerEdgeIndexProvider = function(snapshot)
229 this._retainerEdge = snapshot.createRetainingEdge(0);
232 WebInspector.HeapSnapshotRetainerEdgeIndexProvider.prototype = {
234 * @param {number} index
235 * @return {!WebInspector.HeapSnapshotRetainerEdge}
237 itemForIndex: function(index)
239 this._retainerEdge.setRetainerIndex(index);
240 return this._retainerEdge;
247 * @implements {WebInspector.HeapSnapshotItemIterator}
248 * @param {!WebInspector.HeapSnapshotNode} node
250 WebInspector.HeapSnapshotEdgeIterator = function(node)
252 this._sourceNode = node;
253 this.edge = node._snapshot.createEdge(node._edgeIndexesStart());
256 WebInspector.HeapSnapshotEdgeIterator.prototype = {
262 return this.edge.edgeIndex < this._sourceNode._edgeIndexesEnd();
266 * @return {!WebInspector.HeapSnapshotEdge}
275 this.edge.edgeIndex += this.edge._snapshot._edgeFieldsCount;
281 * @implements {WebInspector.HeapSnapshotItem}
282 * @param {!WebInspector.HeapSnapshot} snapshot
283 * @param {number} retainerIndex
285 WebInspector.HeapSnapshotRetainerEdge = function(snapshot, retainerIndex)
287 this._snapshot = snapshot;
288 this.setRetainerIndex(retainerIndex);
291 WebInspector.HeapSnapshotRetainerEdge.prototype = {
293 * @return {!WebInspector.HeapSnapshotRetainerEdge}
297 return new WebInspector.HeapSnapshotRetainerEdge(this._snapshot, this.retainerIndex());
303 hasStringName: function()
305 return this._edge().hasStringName();
313 return this._edge().name();
317 * @return {!WebInspector.HeapSnapshotNode}
327 nodeIndex: function()
329 return this._retainingNodeIndex;
335 retainerIndex: function()
337 return this._retainerIndex;
341 * @param {number} retainerIndex
343 setRetainerIndex: function(retainerIndex)
345 if (retainerIndex === this._retainerIndex)
347 this._retainerIndex = retainerIndex;
348 this._globalEdgeIndex = this._snapshot._retainingEdges[retainerIndex];
349 this._retainingNodeIndex = this._snapshot._retainingNodes[retainerIndex];
350 this._edgeInstance = null;
351 this._nodeInstance = null;
355 * @param {number} edgeIndex
357 set edgeIndex(edgeIndex)
359 this.setRetainerIndex(edgeIndex);
364 if (!this._nodeInstance)
365 this._nodeInstance = this._snapshot.createNode(this._retainingNodeIndex);
366 return this._nodeInstance;
371 if (!this._edgeInstance)
372 this._edgeInstance = this._snapshot.createEdge(this._globalEdgeIndex);
373 return this._edgeInstance;
381 return this._edge().toString();
388 itemIndex: function()
390 return this._retainerIndex;
395 * @return {!WebInspector.HeapSnapshotCommon.Edge}
397 serialize: function()
399 return new WebInspector.HeapSnapshotCommon.Edge(this.name(), this.node().serialize(), this.type(), this._globalEdgeIndex);
407 return this._edge().type();
413 * @implements {WebInspector.HeapSnapshotItemIterator}
414 * @param {!WebInspector.HeapSnapshotNode} retainedNode
416 WebInspector.HeapSnapshotRetainerEdgeIterator = function(retainedNode)
418 var snapshot = retainedNode._snapshot;
419 var retainedNodeOrdinal = retainedNode._ordinal();
420 var retainerIndex = snapshot._firstRetainerIndex[retainedNodeOrdinal];
421 this._retainersEnd = snapshot._firstRetainerIndex[retainedNodeOrdinal + 1];
422 this.retainer = snapshot.createRetainingEdge(retainerIndex);
425 WebInspector.HeapSnapshotRetainerEdgeIterator.prototype = {
431 return this.retainer.retainerIndex() < this._retainersEnd;
435 * @return {!WebInspector.HeapSnapshotRetainerEdge}
439 return this.retainer;
444 this.retainer.setRetainerIndex(this.retainer.retainerIndex() + 1);
450 * @implements {WebInspector.HeapSnapshotItem}
451 * @param {!WebInspector.HeapSnapshot} snapshot
452 * @param {number=} nodeIndex
454 WebInspector.HeapSnapshotNode = function(snapshot, nodeIndex)
456 this._snapshot = snapshot;
457 this.nodeIndex = nodeIndex || 0;
460 WebInspector.HeapSnapshotNode.prototype = {
466 return this._snapshot._nodeDistances[this.nodeIndex / this._snapshot._nodeFieldCount];
472 className: function()
474 throw new Error("Not implemented");
480 classIndex: function()
482 throw new Error("Not implemented");
488 dominatorIndex: function()
490 var nodeFieldCount = this._snapshot._nodeFieldCount;
491 return this._snapshot._dominatorsTree[this.nodeIndex / this._snapshot._nodeFieldCount] * nodeFieldCount;
495 * @return {!WebInspector.HeapSnapshotEdgeIterator}
499 return new WebInspector.HeapSnapshotEdgeIterator(this);
505 edgesCount: function()
507 return (this._edgeIndexesEnd() - this._edgeIndexesStart()) / this._snapshot._edgeFieldsCount;
515 throw new Error("Not implemented");
523 return this.nodeIndex === this._snapshot._rootNodeIndex;
531 return this._snapshot._strings[this._name()];
537 retainedSize: function()
539 return this._snapshot._retainedSizes[this._ordinal()];
543 * @return {!WebInspector.HeapSnapshotRetainerEdgeIterator}
545 retainers: function()
547 return new WebInspector.HeapSnapshotRetainerEdgeIterator(this);
553 retainersCount: function()
555 var snapshot = this._snapshot;
556 var ordinal = this._ordinal();
557 return snapshot._firstRetainerIndex[ordinal + 1] - snapshot._firstRetainerIndex[ordinal];
565 var snapshot = this._snapshot;
566 return snapshot._nodes[this.nodeIndex + snapshot._nodeSelfSizeOffset];
574 return this._snapshot._nodeTypes[this._type()];
580 traceNodeId: function()
582 var snapshot = this._snapshot;
583 return snapshot._nodes[this.nodeIndex + snapshot._nodeTraceNodeIdOffset];
590 itemIndex: function()
592 return this.nodeIndex;
597 * @return {!WebInspector.HeapSnapshotCommon.Node}
599 serialize: function()
601 return new WebInspector.HeapSnapshotCommon.Node(this.id(), this.name(), this.distance(), this.nodeIndex, this.retainedSize(), this.selfSize(), this.type());
609 var snapshot = this._snapshot;
610 return snapshot._nodes[this.nodeIndex + snapshot._nodeNameOffset];
616 _edgeIndexesStart: function()
618 return this._snapshot._firstEdgeIndexes[this._ordinal()];
624 _edgeIndexesEnd: function()
626 return this._snapshot._firstEdgeIndexes[this._ordinal() + 1];
634 return this.nodeIndex / this._snapshot._nodeFieldCount;
640 _nextNodeIndex: function()
642 return this.nodeIndex + this._snapshot._nodeFieldCount;
650 var snapshot = this._snapshot;
651 return snapshot._nodes[this.nodeIndex + snapshot._nodeTypeOffset];
657 * @implements {WebInspector.HeapSnapshotItemIterator}
658 * @param {!WebInspector.HeapSnapshotNode} node
660 WebInspector.HeapSnapshotNodeIterator = function(node)
663 this._nodesLength = node._snapshot._nodes.length;
666 WebInspector.HeapSnapshotNodeIterator.prototype = {
672 return this.node.nodeIndex < this._nodesLength;
676 * @return {!WebInspector.HeapSnapshotNode}
685 this.node.nodeIndex = this.node._nextNodeIndex();
692 * @implements {WebInspector.HeapSnapshotItemIterator}
693 * @param {!WebInspector.HeapSnapshotItemIndexProvider} itemProvider
694 * @param {!Array.<number>|!Uint32Array} indexes
696 WebInspector.HeapSnapshotIndexRangeIterator = function(itemProvider, indexes)
698 this._itemProvider = itemProvider;
699 this._indexes = indexes;
703 WebInspector.HeapSnapshotIndexRangeIterator.prototype = {
709 return this._position < this._indexes.length
713 * @return {!WebInspector.HeapSnapshotItem}
717 var index = this._indexes[this._position];
718 return this._itemProvider.itemForIndex(index);
730 * @implements {WebInspector.HeapSnapshotItemIterator}
731 * @param {!WebInspector.HeapSnapshotItemIterator} iterator
733 WebInspector.HeapSnapshotFilteredIterator = function(iterator, filter)
735 this._iterator = iterator;
736 this._filter = filter;
737 this._skipFilteredItems();
740 WebInspector.HeapSnapshotFilteredIterator.prototype = {
746 return this._iterator.hasNext();
750 * @return {!WebInspector.HeapSnapshotItem}
754 return this._iterator.item();
759 this._iterator.next();
760 this._skipFilteredItems();
763 _skipFilteredItems: function()
765 while (this._iterator.hasNext() && !this._filter(this._iterator.item())) {
766 this._iterator.next();
773 * @param {!WebInspector.HeapSnapshotWorkerDispatcher=} dispatcher
776 WebInspector.HeapSnapshotProgress = function(dispatcher)
778 this._dispatcher = dispatcher;
781 WebInspector.HeapSnapshotProgress.prototype = {
783 * @param {string} status
785 updateStatus: function(status)
787 this._sendUpdateEvent(WebInspector.UIString(status));
791 * @param {string} title
792 * @param {number} value
793 * @param {number} total
795 updateProgress: function(title, value, total)
797 var percentValue = ((total ? (value / total) : 0) * 100).toFixed(0);
798 this._sendUpdateEvent(WebInspector.UIString(title, percentValue));
802 * @param {string} text
804 _sendUpdateEvent: function(text)
806 // May be undefined in tests.
807 if (this._dispatcher)
808 this._dispatcher.sendEvent(WebInspector.HeapSnapshotProgressEvent.Update, text);
814 * @param {!WebInspector.HeapSnapshotProgress} progress
815 * @param {boolean} showHiddenData
818 WebInspector.HeapSnapshot = function(profile, progress, showHiddenData)
820 this._nodes = profile.nodes;
821 this._containmentEdges = profile.edges;
822 /** @type {!HeapSnapshotMetainfo} */
823 this._metaNode = profile.snapshot.meta;
824 this._strings = profile.strings;
825 this._progress = progress;
827 this._noDistance = -5;
828 this._rootNodeIndex = 0;
829 if (profile.snapshot.root_index)
830 this._rootNodeIndex = profile.snapshot.root_index;
832 this._snapshotDiffs = {};
833 this._aggregatesForDiff = null;
834 this._aggregates = {};
835 this._aggregatesSortedFlags = {};
836 this._showHiddenData = showHiddenData;
840 if (profile.snapshot.trace_function_count) {
841 this._progress.updateStatus("Buiding allocation statistics\u2026");
842 var nodes = this._nodes;
843 var nodesLength = nodes.length;
844 var nodeFieldCount = this._nodeFieldCount;
845 var node = this.rootNode();
846 var liveObjects = {};
847 for (var nodeIndex = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
848 node.nodeIndex = nodeIndex;
849 var traceNodeId = node.traceNodeId();
850 var stats = liveObjects[traceNodeId];
852 liveObjects[traceNodeId] = stats = { count: 0, size: 0, ids: []};
855 stats.size += node.selfSize();
856 stats.ids.push(node.id());
858 this._allocationProfile = new WebInspector.AllocationProfile(profile, liveObjects);
859 this._progress.updateStatus("Done");
866 function HeapSnapshotMetainfo()
869 this.node_fields = [];
870 this.node_types = [];
871 this.edge_fields = [];
872 this.edge_types = [];
873 this.trace_function_info_fields = [];
874 this.trace_node_fields = [];
875 this.type_strings = {};
881 function HeapSnapshotHeader()
885 this.meta = new HeapSnapshotMetainfo();
890 WebInspector.HeapSnapshot.prototype = {
893 var meta = this._metaNode;
895 this._nodeTypeOffset = meta.node_fields.indexOf("type");
896 this._nodeNameOffset = meta.node_fields.indexOf("name");
897 this._nodeIdOffset = meta.node_fields.indexOf("id");
898 this._nodeSelfSizeOffset = meta.node_fields.indexOf("self_size");
899 this._nodeEdgeCountOffset = meta.node_fields.indexOf("edge_count");
900 this._nodeTraceNodeIdOffset = meta.node_fields.indexOf("trace_node_id");
901 this._nodeFieldCount = meta.node_fields.length;
903 this._nodeTypes = meta.node_types[this._nodeTypeOffset];
904 this._nodeHiddenType = this._nodeTypes.indexOf("hidden");
905 this._nodeObjectType = this._nodeTypes.indexOf("object");
906 this._nodeNativeType = this._nodeTypes.indexOf("native");
907 this._nodeConsStringType = this._nodeTypes.indexOf("concatenated string");
908 this._nodeSlicedStringType = this._nodeTypes.indexOf("sliced string");
909 this._nodeCodeType = this._nodeTypes.indexOf("code");
910 this._nodeSyntheticType = this._nodeTypes.indexOf("synthetic");
912 this._edgeFieldsCount = meta.edge_fields.length;
913 this._edgeTypeOffset = meta.edge_fields.indexOf("type");
914 this._edgeNameOffset = meta.edge_fields.indexOf("name_or_index");
915 this._edgeToNodeOffset = meta.edge_fields.indexOf("to_node");
917 this._edgeTypes = meta.edge_types[this._edgeTypeOffset];
918 this._edgeTypes.push("invisible");
919 this._edgeElementType = this._edgeTypes.indexOf("element");
920 this._edgeHiddenType = this._edgeTypes.indexOf("hidden");
921 this._edgeInternalType = this._edgeTypes.indexOf("internal");
922 this._edgeShortcutType = this._edgeTypes.indexOf("shortcut");
923 this._edgeWeakType = this._edgeTypes.indexOf("weak");
924 this._edgeInvisibleType = this._edgeTypes.indexOf("invisible");
926 this.nodeCount = this._nodes.length / this._nodeFieldCount;
927 this._edgeCount = this._containmentEdges.length / this._edgeFieldsCount;
929 this._progress.updateStatus("Building edge indexes\u2026");
930 this._buildEdgeIndexes();
931 this._progress.updateStatus("Building retainers\u2026");
932 this._buildRetainers();
933 this._progress.updateStatus("Calculating node flags\u2026");
934 this._calculateFlags();
935 this._progress.updateStatus("Calculating distances\u2026");
936 this._calculateDistances();
937 this._progress.updateStatus("Building postorder index\u2026");
938 var result = this._buildPostOrderIndex();
939 // Actually it is array that maps node ordinal number to dominator node ordinal number.
940 this._progress.updateStatus("Building dominator tree\u2026");
941 this._dominatorsTree = this._buildDominatorTree(result.postOrderIndex2NodeOrdinal, result.nodeOrdinal2PostOrderIndex);
942 this._progress.updateStatus("Calculating retained sizes\u2026");
943 this._calculateRetainedSizes(result.postOrderIndex2NodeOrdinal);
944 this._progress.updateStatus("Buiding dominated nodes\u2026");
945 this._buildDominatedNodes();
946 this._progress.updateStatus("Calculating statistics\u2026");
947 this._calculateStatistics();
948 this._progress.updateStatus("Finished processing.");
951 _buildEdgeIndexes: function()
953 var nodes = this._nodes;
954 var nodeCount = this.nodeCount;
955 var firstEdgeIndexes = this._firstEdgeIndexes = new Uint32Array(nodeCount + 1);
956 var nodeFieldCount = this._nodeFieldCount;
957 var edgeFieldsCount = this._edgeFieldsCount;
958 var nodeEdgeCountOffset = this._nodeEdgeCountOffset;
959 firstEdgeIndexes[nodeCount] = this._containmentEdges.length;
960 for (var nodeOrdinal = 0, edgeIndex = 0; nodeOrdinal < nodeCount; ++nodeOrdinal) {
961 firstEdgeIndexes[nodeOrdinal] = edgeIndex;
962 edgeIndex += nodes[nodeOrdinal * nodeFieldCount + nodeEdgeCountOffset] * edgeFieldsCount;
966 _buildRetainers: function()
968 var retainingNodes = this._retainingNodes = new Uint32Array(this._edgeCount);
969 var retainingEdges = this._retainingEdges = new Uint32Array(this._edgeCount);
970 // Index of the first retainer in the _retainingNodes and _retainingEdges
971 // arrays. Addressed by retained node index.
972 var firstRetainerIndex = this._firstRetainerIndex = new Uint32Array(this.nodeCount + 1);
974 var containmentEdges = this._containmentEdges;
975 var edgeFieldsCount = this._edgeFieldsCount;
976 var nodeFieldCount = this._nodeFieldCount;
977 var edgeToNodeOffset = this._edgeToNodeOffset;
978 var firstEdgeIndexes = this._firstEdgeIndexes;
979 var nodeCount = this.nodeCount;
981 for (var toNodeFieldIndex = edgeToNodeOffset, l = containmentEdges.length; toNodeFieldIndex < l; toNodeFieldIndex += edgeFieldsCount) {
982 var toNodeIndex = containmentEdges[toNodeFieldIndex];
983 if (toNodeIndex % nodeFieldCount)
984 throw new Error("Invalid toNodeIndex " + toNodeIndex);
985 ++firstRetainerIndex[toNodeIndex / nodeFieldCount];
987 for (var i = 0, firstUnusedRetainerSlot = 0; i < nodeCount; i++) {
988 var retainersCount = firstRetainerIndex[i];
989 firstRetainerIndex[i] = firstUnusedRetainerSlot;
990 retainingNodes[firstUnusedRetainerSlot] = retainersCount;
991 firstUnusedRetainerSlot += retainersCount;
993 firstRetainerIndex[nodeCount] = retainingNodes.length;
995 var nextNodeFirstEdgeIndex = firstEdgeIndexes[0];
996 for (var srcNodeOrdinal = 0; srcNodeOrdinal < nodeCount; ++srcNodeOrdinal) {
997 var firstEdgeIndex = nextNodeFirstEdgeIndex;
998 nextNodeFirstEdgeIndex = firstEdgeIndexes[srcNodeOrdinal + 1];
999 var srcNodeIndex = srcNodeOrdinal * nodeFieldCount;
1000 for (var edgeIndex = firstEdgeIndex; edgeIndex < nextNodeFirstEdgeIndex; edgeIndex += edgeFieldsCount) {
1001 var toNodeIndex = containmentEdges[edgeIndex + edgeToNodeOffset];
1002 if (toNodeIndex % nodeFieldCount)
1003 throw new Error("Invalid toNodeIndex " + toNodeIndex);
1004 var firstRetainerSlotIndex = firstRetainerIndex[toNodeIndex / nodeFieldCount];
1005 var nextUnusedRetainerSlotIndex = firstRetainerSlotIndex + (--retainingNodes[firstRetainerSlotIndex]);
1006 retainingNodes[nextUnusedRetainerSlotIndex] = srcNodeIndex;
1007 retainingEdges[nextUnusedRetainerSlotIndex] = edgeIndex;
1013 * @param {number=} nodeIndex
1015 createNode: function(nodeIndex)
1017 throw new Error("Not implemented");
1021 * @param {number} edgeIndex
1022 * @return {!WebInspector.JSHeapSnapshotEdge}
1024 createEdge: function(edgeIndex)
1026 throw new Error("Not implemented");
1030 * @param {number} retainerIndex
1031 * @return {!WebInspector.JSHeapSnapshotRetainerEdge}
1033 createRetainingEdge: function(retainerIndex)
1035 throw new Error("Not implemented");
1041 delete this._strings;
1042 delete this._retainingEdges;
1043 delete this._retainingNodes;
1044 delete this._firstRetainerIndex;
1045 delete this._aggregates;
1046 delete this._aggregatesSortedFlags;
1047 delete this._dominatedNodes;
1048 delete this._firstDominatedNodeIndex;
1049 delete this._nodeDistances;
1050 delete this._dominatorsTree;
1053 _allNodes: function()
1055 return new WebInspector.HeapSnapshotNodeIterator(this.rootNode());
1059 * @return {!WebInspector.HeapSnapshotNode}
1061 rootNode: function()
1063 return this.createNode(this._rootNodeIndex);
1068 return this._rootNodeIndex;
1073 return this.rootNode().retainedSize();
1076 _getDominatedIndex: function(nodeIndex)
1078 if (nodeIndex % this._nodeFieldCount)
1079 throw new Error("Invalid nodeIndex: " + nodeIndex);
1080 return this._firstDominatedNodeIndex[nodeIndex / this._nodeFieldCount];
1084 * @param {!WebInspector.HeapSnapshotNode} node
1085 * @return {!Uint32Array}
1087 _dominatedNodesOfNode: function(node)
1089 var dominatedIndexFrom = this._getDominatedIndex(node.nodeIndex);
1090 var dominatedIndexTo = this._getDominatedIndex(node._nextNodeIndex());
1091 return this._dominatedNodes.subarray(dominatedIndexFrom, dominatedIndexTo);
1095 * @param {!WebInspector.HeapSnapshotCommon.NodeFilter} nodeFilter
1096 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.Aggregate>}
1098 aggregatesWithFilter: function(nodeFilter)
1100 var minNodeId = nodeFilter.minNodeId;
1101 var maxNodeId = nodeFilter.maxNodeId;
1102 var allocationNodeId = nodeFilter.allocationNodeId;
1105 if (typeof allocationNodeId === "number") {
1106 filter = this._createAllocationStackFilter(allocationNodeId);
1107 } else if (typeof minNodeId === "number" && typeof maxNodeId === "number") {
1108 key = minNodeId + ".." + maxNodeId;
1109 filter = this._createNodeIdFilter(minNodeId, maxNodeId);
1113 return this.aggregates(false, key, filter);
1117 * @param {number} minNodeId
1118 * @param {number} maxNodeId
1119 * @return {function(!WebInspector.HeapSnapshotNode):boolean}
1121 _createNodeIdFilter: function(minNodeId, maxNodeId)
1124 * @param {!WebInspector.HeapSnapshotNode} node
1127 function nodeIdFilter(node)
1130 return id > minNodeId && id <= maxNodeId;
1132 return nodeIdFilter;
1136 * @param {number} bottomUpAllocationNodeId
1137 * @return {function(!WebInspector.HeapSnapshotNode):boolean|undefined}
1139 _createAllocationStackFilter: function(bottomUpAllocationNodeId)
1141 var traceIds = this._allocationProfile.traceIds(bottomUpAllocationNodeId);
1142 if (!traceIds.length)
1145 for (var i = 0; i < traceIds.length; i++)
1146 set[traceIds[i]] = true;
1148 * @param {!WebInspector.HeapSnapshotNode} node
1151 function traceIdFilter(node)
1153 return !!set[node.traceNodeId()];
1155 return traceIdFilter;
1159 * @param {boolean} sortedIndexes
1160 * @param {string=} key
1161 * @param {function(!WebInspector.HeapSnapshotNode):boolean=} filter
1162 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.Aggregate>}
1164 aggregates: function(sortedIndexes, key, filter)
1166 var aggregatesByClassName = key && this._aggregates[key];
1167 if (!aggregatesByClassName) {
1168 var aggregates = this._buildAggregates(filter);
1169 this._calculateClassesRetainedSize(aggregates.aggregatesByClassIndex, filter);
1170 aggregatesByClassName = aggregates.aggregatesByClassName;
1172 this._aggregates[key] = aggregatesByClassName;
1175 if (sortedIndexes && (!key || !this._aggregatesSortedFlags[key])) {
1176 this._sortAggregateIndexes(aggregatesByClassName);
1178 this._aggregatesSortedFlags[key] = sortedIndexes;
1180 return aggregatesByClassName;
1184 * @return {!Array.<!WebInspector.HeapSnapshotCommon.SerializedAllocationNode>}
1186 allocationTracesTops: function()
1188 return this._allocationProfile.serializeTraceTops();
1192 * @param {number} nodeId
1193 * @return {!WebInspector.HeapSnapshotCommon.AllocationNodeCallers}
1195 allocationNodeCallers: function(nodeId)
1197 return this._allocationProfile.serializeCallers(nodeId);
1201 * @param {number} nodeIndex
1202 * @return {?Array.<!WebInspector.HeapSnapshotCommon.AllocationStackFrame>}
1204 allocationStack: function(nodeIndex)
1206 var node = this.createNode(nodeIndex);
1207 var allocationNodeId = node.traceNodeId();
1208 if (!allocationNodeId)
1210 return this._allocationProfile.serializeAllocationStack(allocationNodeId);
1214 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.AggregateForDiff>}
1216 aggregatesForDiff: function()
1218 if (this._aggregatesForDiff)
1219 return this._aggregatesForDiff;
1221 var aggregatesByClassName = this.aggregates(true, "allObjects");
1222 this._aggregatesForDiff = {};
1224 var node = this.createNode();
1225 for (var className in aggregatesByClassName) {
1226 var aggregate = aggregatesByClassName[className];
1227 var indexes = aggregate.idxs;
1228 var ids = new Array(indexes.length);
1229 var selfSizes = new Array(indexes.length);
1230 for (var i = 0; i < indexes.length; i++) {
1231 node.nodeIndex = indexes[i];
1233 selfSizes[i] = node.selfSize();
1236 this._aggregatesForDiff[className] = {
1239 selfSizes: selfSizes
1242 return this._aggregatesForDiff;
1246 * @param {!WebInspector.HeapSnapshotNode} node
1247 * @return {!boolean}
1249 _isUserRoot: function(node)
1255 * @param {function(!WebInspector.HeapSnapshotNode)} action
1256 * @param {boolean=} userRootsOnly
1258 forEachRoot: function(action, userRootsOnly)
1260 for (var iter = this.rootNode().edges(); iter.hasNext(); iter.next()) {
1261 var node = iter.edge.node();
1262 if (!userRootsOnly || this._isUserRoot(node))
1267 _calculateDistances: function()
1269 var nodeFieldCount = this._nodeFieldCount;
1270 var nodeCount = this.nodeCount;
1271 var distances = this._nodeDistances = new Int32Array(nodeCount);
1272 var noDistance = this._noDistance;
1273 for (var i = 0; i < nodeCount; ++i)
1274 distances[i] = noDistance;
1276 var nodesToVisit = new Uint32Array(this.nodeCount);
1277 var nodesToVisitLength = 0;
1280 * @param {number} distance
1281 * @param {!WebInspector.HeapSnapshotNode} node
1283 function enqueueNode(distance, node)
1285 var ordinal = node._ordinal();
1286 if (distances[ordinal] !== noDistance)
1288 distances[ordinal] = distance;
1289 nodesToVisit[nodesToVisitLength++] = node.nodeIndex;
1292 this.forEachRoot(enqueueNode.bind(null, 1), true);
1293 this._bfs(nodesToVisit, nodesToVisitLength, distances);
1295 // bfs for the rest of objects
1296 nodesToVisitLength = 0;
1297 this.forEachRoot(enqueueNode.bind(null, WebInspector.HeapSnapshotCommon.baseSystemDistance), false);
1298 this._bfs(nodesToVisit, nodesToVisitLength, distances);
1302 * @param {!Uint32Array} nodesToVisit
1303 * @param {!number} nodesToVisitLength
1304 * @param {!Int32Array} distances
1306 _bfs: function(nodesToVisit, nodesToVisitLength, distances)
1308 // Preload fields into local variables for better performance.
1309 var edgeFieldsCount = this._edgeFieldsCount;
1310 var nodeFieldCount = this._nodeFieldCount;
1311 var containmentEdges = this._containmentEdges;
1312 var firstEdgeIndexes = this._firstEdgeIndexes;
1313 var edgeToNodeOffset = this._edgeToNodeOffset;
1314 var edgeTypeOffset = this._edgeTypeOffset;
1315 var nodeCount = this.nodeCount;
1316 var containmentEdgesLength = containmentEdges.length;
1317 var edgeWeakType = this._edgeWeakType;
1318 var noDistance = this._noDistance;
1321 while (index < nodesToVisitLength) {
1322 var nodeIndex = nodesToVisit[index++]; // shift generates too much garbage.
1323 var nodeOrdinal = nodeIndex / nodeFieldCount;
1324 var distance = distances[nodeOrdinal] + 1;
1325 var firstEdgeIndex = firstEdgeIndexes[nodeOrdinal];
1326 var edgesEnd = firstEdgeIndexes[nodeOrdinal + 1];
1327 for (var edgeIndex = firstEdgeIndex; edgeIndex < edgesEnd; edgeIndex += edgeFieldsCount) {
1328 var edgeType = containmentEdges[edgeIndex + edgeTypeOffset];
1329 if (edgeType == edgeWeakType)
1331 var childNodeIndex = containmentEdges[edgeIndex + edgeToNodeOffset];
1332 var childNodeOrdinal = childNodeIndex / nodeFieldCount;
1333 if (distances[childNodeOrdinal] !== noDistance)
1335 distances[childNodeOrdinal] = distance;
1336 nodesToVisit[nodesToVisitLength++] = childNodeIndex;
1339 if (nodesToVisitLength > nodeCount)
1340 throw new Error("BFS failed. Nodes to visit (" + nodesToVisitLength + ") is more than nodes count (" + nodeCount + ")");
1343 _buildAggregates: function(filter)
1345 var aggregates = {};
1346 var aggregatesByClassName = {};
1347 var classIndexes = [];
1348 var nodes = this._nodes;
1349 var mapAndFlag = this.userObjectsMapAndFlag();
1350 var flags = mapAndFlag ? mapAndFlag.map : null;
1351 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1352 var nodesLength = nodes.length;
1353 var nodeNativeType = this._nodeNativeType;
1354 var nodeFieldCount = this._nodeFieldCount;
1355 var selfSizeOffset = this._nodeSelfSizeOffset;
1356 var nodeTypeOffset = this._nodeTypeOffset;
1357 var node = this.rootNode();
1358 var nodeDistances = this._nodeDistances;
1360 for (var nodeIndex = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
1361 var nodeOrdinal = nodeIndex / nodeFieldCount;
1362 if (flags && !(flags[nodeOrdinal] & flag))
1364 node.nodeIndex = nodeIndex;
1365 if (filter && !filter(node))
1367 var selfSize = nodes[nodeIndex + selfSizeOffset];
1368 if (!selfSize && nodes[nodeIndex + nodeTypeOffset] !== nodeNativeType)
1370 var classIndex = node.classIndex();
1371 if (!(classIndex in aggregates)) {
1372 var nodeType = node.type();
1373 var nameMatters = nodeType === "object" || nodeType === "native";
1376 distance: nodeDistances[nodeOrdinal],
1380 name: nameMatters ? node.name() : null,
1383 aggregates[classIndex] = value;
1384 classIndexes.push(classIndex);
1385 aggregatesByClassName[node.className()] = value;
1387 var clss = aggregates[classIndex];
1388 clss.distance = Math.min(clss.distance, nodeDistances[nodeOrdinal]);
1390 clss.self += selfSize;
1391 clss.idxs.push(nodeIndex);
1395 // Shave off provisionally allocated space.
1396 for (var i = 0, l = classIndexes.length; i < l; ++i) {
1397 var classIndex = classIndexes[i];
1398 aggregates[classIndex].idxs = aggregates[classIndex].idxs.slice();
1400 return {aggregatesByClassName: aggregatesByClassName, aggregatesByClassIndex: aggregates};
1403 _calculateClassesRetainedSize: function(aggregates, filter)
1405 var rootNodeIndex = this._rootNodeIndex;
1406 var node = this.createNode(rootNodeIndex);
1407 var list = [rootNodeIndex];
1410 var seenClassNameIndexes = {};
1411 var nodeFieldCount = this._nodeFieldCount;
1412 var nodeTypeOffset = this._nodeTypeOffset;
1413 var nodeNativeType = this._nodeNativeType;
1414 var dominatedNodes = this._dominatedNodes;
1415 var nodes = this._nodes;
1416 var mapAndFlag = this.userObjectsMapAndFlag();
1417 var flags = mapAndFlag ? mapAndFlag.map : null;
1418 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1419 var firstDominatedNodeIndex = this._firstDominatedNodeIndex;
1421 while (list.length) {
1422 var nodeIndex = list.pop();
1423 node.nodeIndex = nodeIndex;
1424 var classIndex = node.classIndex();
1425 var seen = !!seenClassNameIndexes[classIndex];
1426 var nodeOrdinal = nodeIndex / nodeFieldCount;
1427 var dominatedIndexFrom = firstDominatedNodeIndex[nodeOrdinal];
1428 var dominatedIndexTo = firstDominatedNodeIndex[nodeOrdinal + 1];
1431 (!flags || (flags[nodeOrdinal] & flag)) &&
1432 (!filter || filter(node)) &&
1433 (node.selfSize() || nodes[nodeIndex + nodeTypeOffset] === nodeNativeType)
1435 aggregates[classIndex].maxRet += node.retainedSize();
1436 if (dominatedIndexFrom !== dominatedIndexTo) {
1437 seenClassNameIndexes[classIndex] = true;
1438 sizes.push(list.length);
1439 classes.push(classIndex);
1442 for (var i = dominatedIndexFrom; i < dominatedIndexTo; i++)
1443 list.push(dominatedNodes[i]);
1445 var l = list.length;
1446 while (sizes[sizes.length - 1] === l) {
1448 classIndex = classes.pop();
1449 seenClassNameIndexes[classIndex] = false;
1454 _sortAggregateIndexes: function(aggregates)
1456 var nodeA = this.createNode();
1457 var nodeB = this.createNode();
1458 for (var clss in aggregates)
1459 aggregates[clss].idxs.sort(
1460 function(idxA, idxB) {
1461 nodeA.nodeIndex = idxA;
1462 nodeB.nodeIndex = idxB;
1463 return nodeA.id() < nodeB.id() ? -1 : 1;
1467 _buildPostOrderIndex: function()
1469 var nodeFieldCount = this._nodeFieldCount;
1470 var nodes = this._nodes;
1471 var nodeCount = this.nodeCount;
1472 var rootNodeOrdinal = this._rootNodeIndex / nodeFieldCount;
1474 var edgeFieldsCount = this._edgeFieldsCount;
1475 var edgeTypeOffset = this._edgeTypeOffset;
1476 var edgeToNodeOffset = this._edgeToNodeOffset;
1477 var edgeShortcutType = this._edgeShortcutType;
1478 var firstEdgeIndexes = this._firstEdgeIndexes;
1479 var containmentEdges = this._containmentEdges;
1481 var mapAndFlag = this.userObjectsMapAndFlag();
1482 var flags = mapAndFlag ? mapAndFlag.map : null;
1483 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1485 var stackNodes = new Uint32Array(nodeCount);
1486 var stackCurrentEdge = new Uint32Array(nodeCount);
1487 var postOrderIndex2NodeOrdinal = new Uint32Array(nodeCount);
1488 var nodeOrdinal2PostOrderIndex = new Uint32Array(nodeCount);
1489 var visited = new Uint8Array(nodeCount);
1490 var postOrderIndex = 0;
1493 stackNodes[0] = rootNodeOrdinal;
1494 stackCurrentEdge[0] = firstEdgeIndexes[rootNodeOrdinal];
1495 visited[rootNodeOrdinal] = 1;
1497 while (stackTop >= 0) {
1498 var nodeOrdinal = stackNodes[stackTop];
1499 var edgeIndex = stackCurrentEdge[stackTop];
1500 var edgesEnd = firstEdgeIndexes[nodeOrdinal + 1];
1502 if (edgeIndex < edgesEnd) {
1503 stackCurrentEdge[stackTop] += edgeFieldsCount;
1504 if (nodeOrdinal !== rootNodeOrdinal && containmentEdges[edgeIndex + edgeTypeOffset] === edgeShortcutType)
1506 var childNodeIndex = containmentEdges[edgeIndex + edgeToNodeOffset];
1507 var childNodeOrdinal = childNodeIndex / nodeFieldCount;
1508 if (visited[childNodeOrdinal])
1510 var nodeFlag = !flags || (flags[nodeOrdinal] & flag);
1511 var childNodeFlag = !flags || (flags[childNodeOrdinal] & flag);
1512 // We are skipping the edges from non-page-owned nodes to page-owned nodes.
1513 // Otherwise the dominators for the objects that also were retained by debugger would be affected.
1514 if (nodeOrdinal !== rootNodeOrdinal && childNodeFlag && !nodeFlag)
1517 stackNodes[stackTop] = childNodeOrdinal;
1518 stackCurrentEdge[stackTop] = firstEdgeIndexes[childNodeOrdinal];
1519 visited[childNodeOrdinal] = 1;
1521 // Done with all the node children
1522 nodeOrdinal2PostOrderIndex[nodeOrdinal] = postOrderIndex;
1523 postOrderIndex2NodeOrdinal[postOrderIndex++] = nodeOrdinal;
1528 if (postOrderIndex !== nodeCount) {
1529 console.log("Error: Corrupted snapshot. " + (nodeCount - postOrderIndex) + " nodes are unreachable from the root:");
1530 var dumpNode = this.rootNode();
1531 for (var i = 0; i < nodeCount; ++i) {
1533 // Fix it by giving the node a postorder index anyway.
1534 nodeOrdinal2PostOrderIndex[i] = postOrderIndex;
1535 postOrderIndex2NodeOrdinal[postOrderIndex++] = i;
1536 dumpNode.nodeIndex = i * nodeFieldCount;
1537 console.log(JSON.stringify(dumpNode.serialize()));
1538 for (var retainers = dumpNode.retainers(); retainers.hasNext(); retainers = retainers.item().node().retainers())
1539 console.log(" edgeName: " + retainers.item().name() + " nodeClassName: " + retainers.item().node().className());
1544 return {postOrderIndex2NodeOrdinal: postOrderIndex2NodeOrdinal, nodeOrdinal2PostOrderIndex: nodeOrdinal2PostOrderIndex};
1547 // The algorithm is based on the article:
1548 // K. Cooper, T. Harvey and K. Kennedy "A Simple, Fast Dominance Algorithm"
1549 // Softw. Pract. Exper. 4 (2001), pp. 1-10.
1551 * @param {!Array.<number>} postOrderIndex2NodeOrdinal
1552 * @param {!Array.<number>} nodeOrdinal2PostOrderIndex
1554 _buildDominatorTree: function(postOrderIndex2NodeOrdinal, nodeOrdinal2PostOrderIndex)
1556 var nodeFieldCount = this._nodeFieldCount;
1557 var nodes = this._nodes;
1558 var firstRetainerIndex = this._firstRetainerIndex;
1559 var retainingNodes = this._retainingNodes;
1560 var retainingEdges = this._retainingEdges;
1561 var edgeFieldsCount = this._edgeFieldsCount;
1562 var edgeTypeOffset = this._edgeTypeOffset;
1563 var edgeToNodeOffset = this._edgeToNodeOffset;
1564 var edgeShortcutType = this._edgeShortcutType;
1565 var firstEdgeIndexes = this._firstEdgeIndexes;
1566 var containmentEdges = this._containmentEdges;
1567 var containmentEdgesLength = this._containmentEdges.length;
1568 var rootNodeIndex = this._rootNodeIndex;
1570 var mapAndFlag = this.userObjectsMapAndFlag();
1571 var flags = mapAndFlag ? mapAndFlag.map : null;
1572 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1574 var nodesCount = postOrderIndex2NodeOrdinal.length;
1575 var rootPostOrderedIndex = nodesCount - 1;
1576 var noEntry = nodesCount;
1577 var dominators = new Uint32Array(nodesCount);
1578 for (var i = 0; i < rootPostOrderedIndex; ++i)
1579 dominators[i] = noEntry;
1580 dominators[rootPostOrderedIndex] = rootPostOrderedIndex;
1582 // The affected array is used to mark entries which dominators
1583 // have to be racalculated because of changes in their retainers.
1584 var affected = new Uint8Array(nodesCount);
1587 { // Mark the root direct children as affected.
1588 nodeOrdinal = this._rootNodeIndex / nodeFieldCount;
1589 var beginEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal] + edgeToNodeOffset;
1590 var endEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal + 1];
1591 for (var toNodeFieldIndex = beginEdgeToNodeFieldIndex;
1592 toNodeFieldIndex < endEdgeToNodeFieldIndex;
1593 toNodeFieldIndex += edgeFieldsCount) {
1594 var childNodeOrdinal = containmentEdges[toNodeFieldIndex] / nodeFieldCount;
1595 affected[nodeOrdinal2PostOrderIndex[childNodeOrdinal]] = 1;
1602 for (var postOrderIndex = rootPostOrderedIndex - 1; postOrderIndex >= 0; --postOrderIndex) {
1603 if (affected[postOrderIndex] === 0)
1605 affected[postOrderIndex] = 0;
1606 // If dominator of the entry has already been set to root,
1607 // then it can't propagate any further.
1608 if (dominators[postOrderIndex] === rootPostOrderedIndex)
1610 nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1611 var nodeFlag = !flags || (flags[nodeOrdinal] & flag);
1612 var newDominatorIndex = noEntry;
1613 var beginRetainerIndex = firstRetainerIndex[nodeOrdinal];
1614 var endRetainerIndex = firstRetainerIndex[nodeOrdinal + 1];
1615 for (var retainerIndex = beginRetainerIndex; retainerIndex < endRetainerIndex; ++retainerIndex) {
1616 var retainerEdgeIndex = retainingEdges[retainerIndex];
1617 var retainerEdgeType = containmentEdges[retainerEdgeIndex + edgeTypeOffset];
1618 var retainerNodeIndex = retainingNodes[retainerIndex];
1619 if (retainerNodeIndex !== rootNodeIndex && retainerEdgeType === edgeShortcutType)
1621 var retainerNodeOrdinal = retainerNodeIndex / nodeFieldCount;
1622 var retainerNodeFlag = !flags || (flags[retainerNodeOrdinal] & flag);
1623 // We are skipping the edges from non-page-owned nodes to page-owned nodes.
1624 // Otherwise the dominators for the objects that also were retained by debugger would be affected.
1625 if (retainerNodeIndex !== rootNodeIndex && nodeFlag && !retainerNodeFlag)
1627 var retanerPostOrderIndex = nodeOrdinal2PostOrderIndex[retainerNodeOrdinal];
1628 if (dominators[retanerPostOrderIndex] !== noEntry) {
1629 if (newDominatorIndex === noEntry)
1630 newDominatorIndex = retanerPostOrderIndex;
1632 while (retanerPostOrderIndex !== newDominatorIndex) {
1633 while (retanerPostOrderIndex < newDominatorIndex)
1634 retanerPostOrderIndex = dominators[retanerPostOrderIndex];
1635 while (newDominatorIndex < retanerPostOrderIndex)
1636 newDominatorIndex = dominators[newDominatorIndex];
1639 // If idom has already reached the root, it doesn't make sense
1640 // to check other retainers.
1641 if (newDominatorIndex === rootPostOrderedIndex)
1645 if (newDominatorIndex !== noEntry && dominators[postOrderIndex] !== newDominatorIndex) {
1646 dominators[postOrderIndex] = newDominatorIndex;
1648 nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1649 beginEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal] + edgeToNodeOffset;
1650 endEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal + 1];
1651 for (var toNodeFieldIndex = beginEdgeToNodeFieldIndex;
1652 toNodeFieldIndex < endEdgeToNodeFieldIndex;
1653 toNodeFieldIndex += edgeFieldsCount) {
1654 var childNodeOrdinal = containmentEdges[toNodeFieldIndex] / nodeFieldCount;
1655 affected[nodeOrdinal2PostOrderIndex[childNodeOrdinal]] = 1;
1661 var dominatorsTree = new Uint32Array(nodesCount);
1662 for (var postOrderIndex = 0, l = dominators.length; postOrderIndex < l; ++postOrderIndex) {
1663 nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1664 dominatorsTree[nodeOrdinal] = postOrderIndex2NodeOrdinal[dominators[postOrderIndex]];
1666 return dominatorsTree;
1669 _calculateRetainedSizes: function(postOrderIndex2NodeOrdinal)
1671 var nodeCount = this.nodeCount;
1672 var nodes = this._nodes;
1673 var nodeSelfSizeOffset = this._nodeSelfSizeOffset;
1674 var nodeFieldCount = this._nodeFieldCount;
1675 var dominatorsTree = this._dominatorsTree;
1676 var retainedSizes = this._retainedSizes = new Float64Array(nodeCount);
1678 for (var nodeOrdinal = 0; nodeOrdinal < nodeCount; ++nodeOrdinal)
1679 retainedSizes[nodeOrdinal] = nodes[nodeOrdinal * nodeFieldCount + nodeSelfSizeOffset];
1681 // Propagate retained sizes for each node excluding root.
1682 for (var postOrderIndex = 0; postOrderIndex < nodeCount - 1; ++postOrderIndex) {
1683 var nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1684 var dominatorOrdinal = dominatorsTree[nodeOrdinal];
1685 retainedSizes[dominatorOrdinal] += retainedSizes[nodeOrdinal];
1689 _buildDominatedNodes: function()
1691 // Builds up two arrays:
1692 // - "dominatedNodes" is a continuous array, where each node owns an
1693 // interval (can be empty) with corresponding dominated nodes.
1694 // - "indexArray" is an array of indexes in the "dominatedNodes"
1695 // with the same positions as in the _nodeIndex.
1696 var indexArray = this._firstDominatedNodeIndex = new Uint32Array(this.nodeCount + 1);
1697 // All nodes except the root have dominators.
1698 var dominatedNodes = this._dominatedNodes = new Uint32Array(this.nodeCount - 1);
1700 // Count the number of dominated nodes for each node. Skip the root (node at
1701 // index 0) as it is the only node that dominates itself.
1702 var nodeFieldCount = this._nodeFieldCount;
1703 var dominatorsTree = this._dominatorsTree;
1705 var fromNodeOrdinal = 0;
1706 var toNodeOrdinal = this.nodeCount;
1707 var rootNodeOrdinal = this._rootNodeIndex / nodeFieldCount;
1708 if (rootNodeOrdinal === fromNodeOrdinal)
1709 fromNodeOrdinal = 1;
1710 else if (rootNodeOrdinal === toNodeOrdinal - 1)
1711 toNodeOrdinal = toNodeOrdinal - 1;
1713 throw new Error("Root node is expected to be either first or last");
1714 for (var nodeOrdinal = fromNodeOrdinal; nodeOrdinal < toNodeOrdinal; ++nodeOrdinal)
1715 ++indexArray[dominatorsTree[nodeOrdinal]];
1716 // Put in the first slot of each dominatedNodes slice the count of entries
1717 // that will be filled.
1718 var firstDominatedNodeIndex = 0;
1719 for (var i = 0, l = this.nodeCount; i < l; ++i) {
1720 var dominatedCount = dominatedNodes[firstDominatedNodeIndex] = indexArray[i];
1721 indexArray[i] = firstDominatedNodeIndex;
1722 firstDominatedNodeIndex += dominatedCount;
1724 indexArray[this.nodeCount] = dominatedNodes.length;
1725 // Fill up the dominatedNodes array with indexes of dominated nodes. Skip the root (node at
1726 // index 0) as it is the only node that dominates itself.
1727 for (var nodeOrdinal = fromNodeOrdinal; nodeOrdinal < toNodeOrdinal; ++nodeOrdinal) {
1728 var dominatorOrdinal = dominatorsTree[nodeOrdinal];
1729 var dominatedRefIndex = indexArray[dominatorOrdinal];
1730 dominatedRefIndex += (--dominatedNodes[dominatedRefIndex]);
1731 dominatedNodes[dominatedRefIndex] = nodeOrdinal * nodeFieldCount;
1735 _calculateFlags: function()
1737 throw new Error("Not implemented");
1740 _calculateStatistics: function()
1742 throw new Error("Not implemented");
1745 userObjectsMapAndFlag: function()
1747 throw new Error("Not implemented");
1751 * @param {string} baseSnapshotId
1752 * @param {!Object.<string, !WebInspector.HeapSnapshotCommon.AggregateForDiff>} baseSnapshotAggregates
1753 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.Diff>}
1755 calculateSnapshotDiff: function(baseSnapshotId, baseSnapshotAggregates)
1757 var snapshotDiff = this._snapshotDiffs[baseSnapshotId];
1759 return snapshotDiff;
1762 var aggregates = this.aggregates(true, "allObjects");
1763 for (var className in baseSnapshotAggregates) {
1764 var baseAggregate = baseSnapshotAggregates[className];
1765 var diff = this._calculateDiffForClass(baseAggregate, aggregates[className]);
1767 snapshotDiff[className] = diff;
1769 var emptyBaseAggregate = new WebInspector.HeapSnapshotCommon.AggregateForDiff();
1770 for (var className in aggregates) {
1771 if (className in baseSnapshotAggregates)
1773 snapshotDiff[className] = this._calculateDiffForClass(emptyBaseAggregate, aggregates[className]);
1776 this._snapshotDiffs[baseSnapshotId] = snapshotDiff;
1777 return snapshotDiff;
1781 * @param {!WebInspector.HeapSnapshotCommon.AggregateForDiff} baseAggregate
1782 * @param {!WebInspector.HeapSnapshotCommon.Aggregate} aggregate
1783 * @return {?WebInspector.HeapSnapshotCommon.Diff}
1785 _calculateDiffForClass: function(baseAggregate, aggregate)
1787 var baseIds = baseAggregate.ids;
1788 var baseIndexes = baseAggregate.indexes;
1789 var baseSelfSizes = baseAggregate.selfSizes;
1791 var indexes = aggregate ? aggregate.idxs : [];
1793 var i = 0, l = baseIds.length;
1794 var j = 0, m = indexes.length;
1795 var diff = new WebInspector.HeapSnapshotCommon.Diff();
1797 var nodeB = this.createNode(indexes[j]);
1798 while (i < l && j < m) {
1799 var nodeAId = baseIds[i];
1800 if (nodeAId < nodeB.id()) {
1801 diff.deletedIndexes.push(baseIndexes[i]);
1802 diff.removedCount++;
1803 diff.removedSize += baseSelfSizes[i];
1805 } else if (nodeAId > nodeB.id()) { // Native nodes(e.g. dom groups) may have ids less than max JS object id in the base snapshot
1806 diff.addedIndexes.push(indexes[j]);
1808 diff.addedSize += nodeB.selfSize();
1809 nodeB.nodeIndex = indexes[++j];
1810 } else { // nodeAId === nodeB.id()
1812 nodeB.nodeIndex = indexes[++j];
1816 diff.deletedIndexes.push(baseIndexes[i]);
1817 diff.removedCount++;
1818 diff.removedSize += baseSelfSizes[i];
1822 diff.addedIndexes.push(indexes[j]);
1824 diff.addedSize += nodeB.selfSize();
1825 nodeB.nodeIndex = indexes[++j];
1827 diff.countDelta = diff.addedCount - diff.removedCount;
1828 diff.sizeDelta = diff.addedSize - diff.removedSize;
1829 if (!diff.addedCount && !diff.removedCount)
1834 _nodeForSnapshotObjectId: function(snapshotObjectId)
1836 for (var it = this._allNodes(); it.hasNext(); it.next()) {
1837 if (it.node.id() === snapshotObjectId)
1844 * @param {string} snapshotObjectId
1847 nodeClassName: function(snapshotObjectId)
1849 var node = this._nodeForSnapshotObjectId(snapshotObjectId);
1851 return node.className();
1856 * @param {string} name
1857 * @return {!Array.<number>}
1859 idsOfObjectsWithName: function(name)
1862 for (var it = this._allNodes(); it.hasNext(); it.next()) {
1863 if (it.item().name() === name)
1864 ids.push(it.item().id());
1870 * @param {string} snapshotObjectId
1871 * @return {?Array.<string>}
1873 dominatorIdsForNode: function(snapshotObjectId)
1875 var node = this._nodeForSnapshotObjectId(snapshotObjectId);
1879 while (!node.isRoot()) {
1880 result.push(node.id());
1881 node.nodeIndex = node.dominatorIndex();
1887 * @param {number} nodeIndex
1888 * @return {!WebInspector.HeapSnapshotEdgesProvider}
1890 createEdgesProvider: function(nodeIndex)
1892 var node = this.createNode(nodeIndex);
1893 var filter = this.containmentEdgesFilter();
1894 var indexProvider = new WebInspector.HeapSnapshotEdgeIndexProvider(this);
1895 return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
1899 * @param {number} nodeIndex
1900 * @return {!WebInspector.HeapSnapshotEdgesProvider}
1902 createEdgesProviderForTest: function(nodeIndex, filter)
1904 var node = this.createNode(nodeIndex);
1905 var indexProvider = new WebInspector.HeapSnapshotEdgeIndexProvider(this);
1906 return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
1910 * @return {?function(!WebInspector.HeapSnapshotEdge):boolean}
1912 retainingEdgesFilter: function()
1918 * @return {?function(!WebInspector.HeapSnapshotEdge):boolean}
1920 containmentEdgesFilter: function()
1926 * @param {number} nodeIndex
1927 * @return {!WebInspector.HeapSnapshotEdgesProvider}
1929 createRetainingEdgesProvider: function(nodeIndex)
1931 var node = this.createNode(nodeIndex);
1932 var filter = this.retainingEdgesFilter();
1933 var indexProvider = new WebInspector.HeapSnapshotRetainerEdgeIndexProvider(this);
1934 return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.retainers(), indexProvider);
1938 * @param {string} baseSnapshotId
1939 * @param {string} className
1940 * @return {!WebInspector.HeapSnapshotNodesProvider}
1942 createAddedNodesProvider: function(baseSnapshotId, className)
1944 var snapshotDiff = this._snapshotDiffs[baseSnapshotId];
1945 var diffForClass = snapshotDiff[className];
1946 return new WebInspector.HeapSnapshotNodesProvider(this, null, diffForClass.addedIndexes);
1950 * @param {!Array.<number>} nodeIndexes
1951 * @return {!WebInspector.HeapSnapshotNodesProvider}
1953 createDeletedNodesProvider: function(nodeIndexes)
1955 return new WebInspector.HeapSnapshotNodesProvider(this, null, nodeIndexes);
1959 * @return {?function(!WebInspector.HeapSnapshotNode):boolean}
1961 classNodesFilter: function()
1967 * @param {string} className
1968 * @param {!WebInspector.HeapSnapshotCommon.NodeFilter} nodeFilter
1969 * @return {!WebInspector.HeapSnapshotNodesProvider}
1971 createNodesProviderForClass: function(className, nodeFilter)
1973 return new WebInspector.HeapSnapshotNodesProvider(this, this.classNodesFilter(), this.aggregatesWithFilter(nodeFilter)[className].idxs);
1977 * @param {number} nodeIndex
1978 * @return {!WebInspector.HeapSnapshotNodesProvider}
1980 createNodesProviderForDominator: function(nodeIndex)
1982 var node = this.createNode(nodeIndex);
1983 return new WebInspector.HeapSnapshotNodesProvider(this, null, this._dominatedNodesOfNode(node));
1989 _maxJsNodeId: function()
1991 var nodeFieldCount = this._nodeFieldCount;
1992 var nodes = this._nodes;
1993 var nodesLength = nodes.length;
1995 for (var nodeIndex = this._nodeIdOffset; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
1996 var nextId = nodes[nodeIndex];
1997 // JS objects have odd ids, skip native objects.
1998 if (nextId % 2 === 0)
2007 * @return {!WebInspector.HeapSnapshotCommon.StaticData}
2009 updateStaticData: function()
2011 return new WebInspector.HeapSnapshotCommon.StaticData(this.nodeCount, this._rootNodeIndex, this.totalSize, this._maxJsNodeId());
2017 * @param {!WebInspector.HeapSnapshotItemIterator} iterator
2018 * @param {!WebInspector.HeapSnapshotItemIndexProvider} indexProvider
2020 WebInspector.HeapSnapshotItemProvider = function(iterator, indexProvider)
2022 this._iterator = iterator;
2023 this._indexProvider = indexProvider;
2024 this._isEmpty = !iterator.hasNext();
2025 /** @type {?Array.<number>} */
2026 this._iterationOrder = null;
2027 this._currentComparator = null;
2028 this._sortedPrefixLength = 0;
2029 this._sortedSuffixLength = 0;
2032 WebInspector.HeapSnapshotItemProvider.prototype = {
2033 _createIterationOrder: function()
2035 if (this._iterationOrder)
2037 this._iterationOrder = [];
2038 for (var iterator = this._iterator; iterator.hasNext(); iterator.next())
2039 this._iterationOrder.push(iterator.item().itemIndex());
2047 return this._isEmpty;
2051 * @param {number} begin
2052 * @param {number} end
2053 * @return {!WebInspector.HeapSnapshotCommon.ItemsRange}
2055 serializeItemsRange: function(begin, end)
2057 this._createIterationOrder();
2059 throw new Error("Start position > end position: " + begin + " > " + end);
2060 if (end > this._iterationOrder.length)
2061 end = this._iterationOrder.length;
2062 if (this._sortedPrefixLength < end && begin < this._iterationOrder.length - this._sortedSuffixLength) {
2063 this.sort(this._currentComparator, this._sortedPrefixLength, this._iterationOrder.length - 1 - this._sortedSuffixLength, begin, end - 1);
2064 if (begin <= this._sortedPrefixLength)
2065 this._sortedPrefixLength = end;
2066 if (end >= this._iterationOrder.length - this._sortedSuffixLength)
2067 this._sortedSuffixLength = this._iterationOrder.length - begin;
2069 var position = begin;
2070 var count = end - begin;
2071 var result = new Array(count);
2072 var iterator = this._iterator;
2073 for (var i = 0 ; i < count; ++i) {
2074 var itemIndex = this._iterationOrder[position++];
2075 var item = this._indexProvider.itemForIndex(itemIndex);
2076 result[i] = item.serialize();
2078 return new WebInspector.HeapSnapshotCommon.ItemsRange(begin, end, this._iterationOrder.length, result);
2081 sortAndRewind: function(comparator)
2083 this._currentComparator = comparator;
2084 this._sortedPrefixLength = 0;
2085 this._sortedSuffixLength = 0;
2091 * @extends {WebInspector.HeapSnapshotItemProvider}
2092 * @param {!WebInspector.HeapSnapshotItemIndexProvider} indexProvider
2094 WebInspector.HeapSnapshotEdgesProvider = function(snapshot, filter, edgesIter, indexProvider)
2096 this.snapshot = snapshot;
2098 edgesIter = new WebInspector.HeapSnapshotFilteredIterator(edgesIter, filter);
2099 WebInspector.HeapSnapshotItemProvider.call(this, edgesIter, indexProvider);
2102 WebInspector.HeapSnapshotEdgesProvider.prototype = {
2104 * @param {!WebInspector.HeapSnapshotCommon.ComparatorConfig} comparator
2105 * @param {number} leftBound
2106 * @param {number} rightBound
2107 * @param {number} windowLeft
2108 * @param {number} windowRight
2110 sort: function(comparator, leftBound, rightBound, windowLeft, windowRight)
2112 var fieldName1 = comparator.fieldName1;
2113 var fieldName2 = comparator.fieldName2;
2114 var ascending1 = comparator.ascending1;
2115 var ascending2 = comparator.ascending2;
2117 var edgeA = this._iterator.item().clone();
2118 var edgeB = edgeA.clone();
2119 var nodeA = this.snapshot.createNode();
2120 var nodeB = this.snapshot.createNode();
2122 function compareEdgeFieldName(ascending, indexA, indexB)
2124 edgeA.edgeIndex = indexA;
2125 edgeB.edgeIndex = indexB;
2126 if (edgeB.name() === "__proto__") return -1;
2127 if (edgeA.name() === "__proto__") return 1;
2129 edgeA.hasStringName() === edgeB.hasStringName() ?
2130 (edgeA.name() < edgeB.name() ? -1 : (edgeA.name() > edgeB.name() ? 1 : 0)) :
2131 (edgeA.hasStringName() ? -1 : 1);
2132 return ascending ? result : -result;
2135 function compareNodeField(fieldName, ascending, indexA, indexB)
2137 edgeA.edgeIndex = indexA;
2138 nodeA.nodeIndex = edgeA.nodeIndex();
2139 var valueA = nodeA[fieldName]();
2141 edgeB.edgeIndex = indexB;
2142 nodeB.nodeIndex = edgeB.nodeIndex();
2143 var valueB = nodeB[fieldName]();
2145 var result = valueA < valueB ? -1 : (valueA > valueB ? 1 : 0);
2146 return ascending ? result : -result;
2149 function compareEdgeAndNode(indexA, indexB) {
2150 var result = compareEdgeFieldName(ascending1, indexA, indexB);
2152 result = compareNodeField(fieldName2, ascending2, indexA, indexB);
2154 return indexA - indexB;
2158 function compareNodeAndEdge(indexA, indexB) {
2159 var result = compareNodeField(fieldName1, ascending1, indexA, indexB);
2161 result = compareEdgeFieldName(ascending2, indexA, indexB);
2163 return indexA - indexB;
2167 function compareNodeAndNode(indexA, indexB) {
2168 var result = compareNodeField(fieldName1, ascending1, indexA, indexB);
2170 result = compareNodeField(fieldName2, ascending2, indexA, indexB);
2172 return indexA - indexB;
2176 if (fieldName1 === "!edgeName")
2177 this._iterationOrder.sortRange(compareEdgeAndNode, leftBound, rightBound, windowLeft, windowRight);
2178 else if (fieldName2 === "!edgeName")
2179 this._iterationOrder.sortRange(compareNodeAndEdge, leftBound, rightBound, windowLeft, windowRight);
2181 this._iterationOrder.sortRange(compareNodeAndNode, leftBound, rightBound, windowLeft, windowRight);
2184 __proto__: WebInspector.HeapSnapshotItemProvider.prototype
2190 * @extends {WebInspector.HeapSnapshotItemProvider}
2191 * @param {?function(!WebInspector.HeapSnapshotNode):boolean} filter
2192 * @param {(!Array.<number>|!Uint32Array)} nodeIndexes
2194 WebInspector.HeapSnapshotNodesProvider = function(snapshot, filter, nodeIndexes)
2196 this.snapshot = snapshot;
2197 var indexProvider = new WebInspector.HeapSnapshotNodeIndexProvider(snapshot);
2198 var it = new WebInspector.HeapSnapshotIndexRangeIterator(indexProvider, nodeIndexes);
2200 it = new WebInspector.HeapSnapshotFilteredIterator(it, filter);
2201 WebInspector.HeapSnapshotItemProvider.call(this, it, indexProvider);
2204 WebInspector.HeapSnapshotNodesProvider.prototype = {
2206 * @param {string} snapshotObjectId
2209 nodePosition: function(snapshotObjectId)
2211 this._createIterationOrder();
2212 var node = this.snapshot.createNode();
2213 for (var i = 0; i < this._iterationOrder.length; i++) {
2214 node.nodeIndex = this._iterationOrder[i];
2215 if (node.id() === snapshotObjectId)
2218 if (i === this._iterationOrder.length)
2220 var targetNodeIndex = this._iterationOrder[i];
2221 var smallerCount = 0;
2222 var compare = this._buildCompareFunction(this._currentComparator);
2223 for (var i = 0; i < this._iterationOrder.length; i++) {
2224 if (compare(this._iterationOrder[i], targetNodeIndex) < 0)
2227 return smallerCount;
2231 * @return {function(number,number):number}
2233 _buildCompareFunction: function(comparator)
2235 var nodeA = this.snapshot.createNode();
2236 var nodeB = this.snapshot.createNode();
2237 var fieldAccessor1 = nodeA[comparator.fieldName1];
2238 var fieldAccessor2 = nodeA[comparator.fieldName2];
2239 var ascending1 = comparator.ascending1 ? 1 : -1;
2240 var ascending2 = comparator.ascending2 ? 1 : -1;
2243 * @param {function():*} fieldAccessor
2244 * @param {number} ascending
2247 function sortByNodeField(fieldAccessor, ascending)
2249 var valueA = fieldAccessor.call(nodeA);
2250 var valueB = fieldAccessor.call(nodeB);
2251 return valueA < valueB ? -ascending : (valueA > valueB ? ascending : 0);
2255 * @param {number} indexA
2256 * @param {number} indexB
2259 function sortByComparator(indexA, indexB)
2261 nodeA.nodeIndex = indexA;
2262 nodeB.nodeIndex = indexB;
2263 var result = sortByNodeField(fieldAccessor1, ascending1);
2265 result = sortByNodeField(fieldAccessor2, ascending2);
2266 return result || indexA - indexB;
2269 return sortByComparator;
2273 * @param {number} leftBound
2274 * @param {number} rightBound
2275 * @param {number} windowLeft
2276 * @param {number} windowRight
2278 sort: function(comparator, leftBound, rightBound, windowLeft, windowRight)
2280 this._iterationOrder.sortRange(this._buildCompareFunction(comparator), leftBound, rightBound, windowLeft, windowRight);
2283 __proto__: WebInspector.HeapSnapshotItemProvider.prototype