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
732 * @param {function(!WebInspector.HeapSnapshotItem):boolean=} filter
734 WebInspector.HeapSnapshotFilteredIterator = function(iterator, filter)
736 this._iterator = iterator;
737 this._filter = filter;
738 this._skipFilteredItems();
741 WebInspector.HeapSnapshotFilteredIterator.prototype = {
747 return this._iterator.hasNext();
751 * @return {!WebInspector.HeapSnapshotItem}
755 return this._iterator.item();
760 this._iterator.next();
761 this._skipFilteredItems();
764 _skipFilteredItems: function()
766 while (this._iterator.hasNext() && !this._filter(this._iterator.item())) {
767 this._iterator.next();
774 * @param {!WebInspector.HeapSnapshotWorkerDispatcher=} dispatcher
777 WebInspector.HeapSnapshotProgress = function(dispatcher)
779 this._dispatcher = dispatcher;
782 WebInspector.HeapSnapshotProgress.prototype = {
784 * @param {string} status
786 updateStatus: function(status)
788 this._sendUpdateEvent(WebInspector.UIString(status));
792 * @param {string} title
793 * @param {number} value
794 * @param {number} total
796 updateProgress: function(title, value, total)
798 var percentValue = ((total ? (value / total) : 0) * 100).toFixed(0);
799 this._sendUpdateEvent(WebInspector.UIString(title, percentValue));
803 * @param {string} text
805 _sendUpdateEvent: function(text)
807 // May be undefined in tests.
808 if (this._dispatcher)
809 this._dispatcher.sendEvent(WebInspector.HeapSnapshotProgressEvent.Update, text);
815 * @param {!Object} profile
816 * @param {!WebInspector.HeapSnapshotProgress} progress
819 WebInspector.HeapSnapshot = function(profile, progress)
821 /** @type {!Uint32Array} */
822 this.nodes = profile.nodes;
823 /** @type {!Uint32Array} */
824 this.containmentEdges = profile.edges;
825 /** @type {!HeapSnapshotMetainfo} */
826 this._metaNode = profile.snapshot.meta;
827 /** @type {!Array.<string>} */
828 this.strings = profile.strings;
829 this._progress = progress;
831 this._noDistance = -5;
832 this._rootNodeIndex = 0;
833 if (profile.snapshot.root_index)
834 this._rootNodeIndex = profile.snapshot.root_index;
836 this._snapshotDiffs = {};
837 this._aggregatesForDiff = null;
838 this._aggregates = {};
839 this._aggregatesSortedFlags = {};
843 if (profile.snapshot.trace_function_count) {
844 this._progress.updateStatus("Buiding allocation statistics\u2026");
845 var nodes = this.nodes;
846 var nodesLength = nodes.length;
847 var nodeFieldCount = this._nodeFieldCount;
848 var node = this.rootNode();
849 var liveObjects = {};
850 for (var nodeIndex = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
851 node.nodeIndex = nodeIndex;
852 var traceNodeId = node.traceNodeId();
853 var stats = liveObjects[traceNodeId];
855 liveObjects[traceNodeId] = stats = { count: 0, size: 0, ids: []};
858 stats.size += node.selfSize();
859 stats.ids.push(node.id());
861 this._allocationProfile = new WebInspector.AllocationProfile(profile, liveObjects);
862 this._progress.updateStatus("Done");
869 function HeapSnapshotMetainfo()
872 this.node_fields = [];
873 this.node_types = [];
874 this.edge_fields = [];
875 this.edge_types = [];
876 this.trace_function_info_fields = [];
877 this.trace_node_fields = [];
878 this.type_strings = {};
884 function HeapSnapshotHeader()
888 this.meta = new HeapSnapshotMetainfo();
893 WebInspector.HeapSnapshot.prototype = {
896 var meta = this._metaNode;
898 this._nodeTypeOffset = meta.node_fields.indexOf("type");
899 this._nodeNameOffset = meta.node_fields.indexOf("name");
900 this._nodeIdOffset = meta.node_fields.indexOf("id");
901 this._nodeSelfSizeOffset = meta.node_fields.indexOf("self_size");
902 this._nodeEdgeCountOffset = meta.node_fields.indexOf("edge_count");
903 this._nodeTraceNodeIdOffset = meta.node_fields.indexOf("trace_node_id");
904 this._nodeFieldCount = meta.node_fields.length;
906 this._nodeTypes = meta.node_types[this._nodeTypeOffset];
907 this._nodeHiddenType = this._nodeTypes.indexOf("hidden");
908 this._nodeObjectType = this._nodeTypes.indexOf("object");
909 this._nodeNativeType = this._nodeTypes.indexOf("native");
910 this._nodeConsStringType = this._nodeTypes.indexOf("concatenated string");
911 this._nodeSlicedStringType = this._nodeTypes.indexOf("sliced string");
912 this._nodeCodeType = this._nodeTypes.indexOf("code");
913 this._nodeSyntheticType = this._nodeTypes.indexOf("synthetic");
915 this._edgeFieldsCount = meta.edge_fields.length;
916 this._edgeTypeOffset = meta.edge_fields.indexOf("type");
917 this._edgeNameOffset = meta.edge_fields.indexOf("name_or_index");
918 this._edgeToNodeOffset = meta.edge_fields.indexOf("to_node");
920 this._edgeTypes = meta.edge_types[this._edgeTypeOffset];
921 this._edgeTypes.push("invisible");
922 this._edgeElementType = this._edgeTypes.indexOf("element");
923 this._edgeHiddenType = this._edgeTypes.indexOf("hidden");
924 this._edgeInternalType = this._edgeTypes.indexOf("internal");
925 this._edgeShortcutType = this._edgeTypes.indexOf("shortcut");
926 this._edgeWeakType = this._edgeTypes.indexOf("weak");
927 this._edgeInvisibleType = this._edgeTypes.indexOf("invisible");
929 this.nodeCount = this.nodes.length / this._nodeFieldCount;
930 this._edgeCount = this.containmentEdges.length / this._edgeFieldsCount;
932 this._progress.updateStatus("Building edge indexes\u2026");
933 this._buildEdgeIndexes();
934 this._progress.updateStatus("Building retainers\u2026");
935 this._buildRetainers();
936 this._progress.updateStatus("Calculating node flags\u2026");
937 this._calculateFlags();
938 this._progress.updateStatus("Calculating distances\u2026");
939 this._calculateDistances();
940 this._progress.updateStatus("Building postorder index\u2026");
941 var result = this._buildPostOrderIndex();
942 // Actually it is array that maps node ordinal number to dominator node ordinal number.
943 this._progress.updateStatus("Building dominator tree\u2026");
944 this._dominatorsTree = this._buildDominatorTree(result.postOrderIndex2NodeOrdinal, result.nodeOrdinal2PostOrderIndex);
945 this._progress.updateStatus("Calculating retained sizes\u2026");
946 this._calculateRetainedSizes(result.postOrderIndex2NodeOrdinal);
947 this._progress.updateStatus("Buiding dominated nodes\u2026");
948 this._buildDominatedNodes();
949 this._progress.updateStatus("Calculating statistics\u2026");
950 this._calculateStatistics();
951 this._progress.updateStatus("Finished processing.");
954 _buildEdgeIndexes: function()
956 var nodes = this.nodes;
957 var nodeCount = this.nodeCount;
958 var firstEdgeIndexes = this._firstEdgeIndexes = new Uint32Array(nodeCount + 1);
959 var nodeFieldCount = this._nodeFieldCount;
960 var edgeFieldsCount = this._edgeFieldsCount;
961 var nodeEdgeCountOffset = this._nodeEdgeCountOffset;
962 firstEdgeIndexes[nodeCount] = this.containmentEdges.length;
963 for (var nodeOrdinal = 0, edgeIndex = 0; nodeOrdinal < nodeCount; ++nodeOrdinal) {
964 firstEdgeIndexes[nodeOrdinal] = edgeIndex;
965 edgeIndex += nodes[nodeOrdinal * nodeFieldCount + nodeEdgeCountOffset] * edgeFieldsCount;
969 _buildRetainers: function()
971 var retainingNodes = this._retainingNodes = new Uint32Array(this._edgeCount);
972 var retainingEdges = this._retainingEdges = new Uint32Array(this._edgeCount);
973 // Index of the first retainer in the _retainingNodes and _retainingEdges
974 // arrays. Addressed by retained node index.
975 var firstRetainerIndex = this._firstRetainerIndex = new Uint32Array(this.nodeCount + 1);
977 var containmentEdges = this.containmentEdges;
978 var edgeFieldsCount = this._edgeFieldsCount;
979 var nodeFieldCount = this._nodeFieldCount;
980 var edgeToNodeOffset = this._edgeToNodeOffset;
981 var firstEdgeIndexes = this._firstEdgeIndexes;
982 var nodeCount = this.nodeCount;
984 for (var toNodeFieldIndex = edgeToNodeOffset, l = containmentEdges.length; toNodeFieldIndex < l; toNodeFieldIndex += edgeFieldsCount) {
985 var toNodeIndex = containmentEdges[toNodeFieldIndex];
986 if (toNodeIndex % nodeFieldCount)
987 throw new Error("Invalid toNodeIndex " + toNodeIndex);
988 ++firstRetainerIndex[toNodeIndex / nodeFieldCount];
990 for (var i = 0, firstUnusedRetainerSlot = 0; i < nodeCount; i++) {
991 var retainersCount = firstRetainerIndex[i];
992 firstRetainerIndex[i] = firstUnusedRetainerSlot;
993 retainingNodes[firstUnusedRetainerSlot] = retainersCount;
994 firstUnusedRetainerSlot += retainersCount;
996 firstRetainerIndex[nodeCount] = retainingNodes.length;
998 var nextNodeFirstEdgeIndex = firstEdgeIndexes[0];
999 for (var srcNodeOrdinal = 0; srcNodeOrdinal < nodeCount; ++srcNodeOrdinal) {
1000 var firstEdgeIndex = nextNodeFirstEdgeIndex;
1001 nextNodeFirstEdgeIndex = firstEdgeIndexes[srcNodeOrdinal + 1];
1002 var srcNodeIndex = srcNodeOrdinal * nodeFieldCount;
1003 for (var edgeIndex = firstEdgeIndex; edgeIndex < nextNodeFirstEdgeIndex; edgeIndex += edgeFieldsCount) {
1004 var toNodeIndex = containmentEdges[edgeIndex + edgeToNodeOffset];
1005 if (toNodeIndex % nodeFieldCount)
1006 throw new Error("Invalid toNodeIndex " + toNodeIndex);
1007 var firstRetainerSlotIndex = firstRetainerIndex[toNodeIndex / nodeFieldCount];
1008 var nextUnusedRetainerSlotIndex = firstRetainerSlotIndex + (--retainingNodes[firstRetainerSlotIndex]);
1009 retainingNodes[nextUnusedRetainerSlotIndex] = srcNodeIndex;
1010 retainingEdges[nextUnusedRetainerSlotIndex] = edgeIndex;
1016 * @param {number=} nodeIndex
1018 createNode: function(nodeIndex)
1020 throw new Error("Not implemented");
1024 * @param {number} edgeIndex
1025 * @return {!WebInspector.JSHeapSnapshotEdge}
1027 createEdge: function(edgeIndex)
1029 throw new Error("Not implemented");
1033 * @param {number} retainerIndex
1034 * @return {!WebInspector.JSHeapSnapshotRetainerEdge}
1036 createRetainingEdge: function(retainerIndex)
1038 throw new Error("Not implemented");
1041 _allNodes: function()
1043 return new WebInspector.HeapSnapshotNodeIterator(this.rootNode());
1047 * @return {!WebInspector.HeapSnapshotNode}
1049 rootNode: function()
1051 return this.createNode(this._rootNodeIndex);
1056 return this._rootNodeIndex;
1061 return this.rootNode().retainedSize();
1064 _getDominatedIndex: function(nodeIndex)
1066 if (nodeIndex % this._nodeFieldCount)
1067 throw new Error("Invalid nodeIndex: " + nodeIndex);
1068 return this._firstDominatedNodeIndex[nodeIndex / this._nodeFieldCount];
1072 * @param {!WebInspector.HeapSnapshotCommon.NodeFilter} nodeFilter
1073 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.Aggregate>}
1075 aggregatesWithFilter: function(nodeFilter)
1077 var minNodeId = nodeFilter.minNodeId;
1078 var maxNodeId = nodeFilter.maxNodeId;
1079 var allocationNodeId = nodeFilter.allocationNodeId;
1082 if (typeof allocationNodeId === "number") {
1083 filter = this._createAllocationStackFilter(allocationNodeId);
1084 } else if (typeof minNodeId === "number" && typeof maxNodeId === "number") {
1085 key = minNodeId + ".." + maxNodeId;
1086 filter = this._createNodeIdFilter(minNodeId, maxNodeId);
1090 return this.aggregates(false, key, filter);
1094 * @param {number} minNodeId
1095 * @param {number} maxNodeId
1096 * @return {function(!WebInspector.HeapSnapshotNode):boolean}
1098 _createNodeIdFilter: function(minNodeId, maxNodeId)
1101 * @param {!WebInspector.HeapSnapshotNode} node
1104 function nodeIdFilter(node)
1107 return id > minNodeId && id <= maxNodeId;
1109 return nodeIdFilter;
1113 * @param {number} bottomUpAllocationNodeId
1114 * @return {function(!WebInspector.HeapSnapshotNode):boolean|undefined}
1116 _createAllocationStackFilter: function(bottomUpAllocationNodeId)
1118 var traceIds = this._allocationProfile.traceIds(bottomUpAllocationNodeId);
1119 if (!traceIds.length)
1122 for (var i = 0; i < traceIds.length; i++)
1123 set[traceIds[i]] = true;
1125 * @param {!WebInspector.HeapSnapshotNode} node
1128 function traceIdFilter(node)
1130 return !!set[node.traceNodeId()];
1132 return traceIdFilter;
1136 * @param {boolean} sortedIndexes
1137 * @param {string=} key
1138 * @param {function(!WebInspector.HeapSnapshotNode):boolean=} filter
1139 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.Aggregate>}
1141 aggregates: function(sortedIndexes, key, filter)
1143 var aggregatesByClassName = key && this._aggregates[key];
1144 if (!aggregatesByClassName) {
1145 var aggregates = this._buildAggregates(filter);
1146 this._calculateClassesRetainedSize(aggregates.aggregatesByClassIndex, filter);
1147 aggregatesByClassName = aggregates.aggregatesByClassName;
1149 this._aggregates[key] = aggregatesByClassName;
1152 if (sortedIndexes && (!key || !this._aggregatesSortedFlags[key])) {
1153 this._sortAggregateIndexes(aggregatesByClassName);
1155 this._aggregatesSortedFlags[key] = sortedIndexes;
1157 return aggregatesByClassName;
1161 * @return {!Array.<!WebInspector.HeapSnapshotCommon.SerializedAllocationNode>}
1163 allocationTracesTops: function()
1165 return this._allocationProfile.serializeTraceTops();
1169 * @param {number} nodeId
1170 * @return {!WebInspector.HeapSnapshotCommon.AllocationNodeCallers}
1172 allocationNodeCallers: function(nodeId)
1174 return this._allocationProfile.serializeCallers(nodeId);
1178 * @param {number} nodeIndex
1179 * @return {?Array.<!WebInspector.HeapSnapshotCommon.AllocationStackFrame>}
1181 allocationStack: function(nodeIndex)
1183 var node = this.createNode(nodeIndex);
1184 var allocationNodeId = node.traceNodeId();
1185 if (!allocationNodeId)
1187 return this._allocationProfile.serializeAllocationStack(allocationNodeId);
1191 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.AggregateForDiff>}
1193 aggregatesForDiff: function()
1195 if (this._aggregatesForDiff)
1196 return this._aggregatesForDiff;
1198 var aggregatesByClassName = this.aggregates(true, "allObjects");
1199 this._aggregatesForDiff = {};
1201 var node = this.createNode();
1202 for (var className in aggregatesByClassName) {
1203 var aggregate = aggregatesByClassName[className];
1204 var indexes = aggregate.idxs;
1205 var ids = new Array(indexes.length);
1206 var selfSizes = new Array(indexes.length);
1207 for (var i = 0; i < indexes.length; i++) {
1208 node.nodeIndex = indexes[i];
1210 selfSizes[i] = node.selfSize();
1213 this._aggregatesForDiff[className] = {
1216 selfSizes: selfSizes
1219 return this._aggregatesForDiff;
1223 * @param {!WebInspector.HeapSnapshotNode} node
1224 * @return {!boolean}
1226 _isUserRoot: function(node)
1232 * @param {function(!WebInspector.HeapSnapshotNode)} action
1233 * @param {boolean=} userRootsOnly
1235 forEachRoot: function(action, userRootsOnly)
1237 for (var iter = this.rootNode().edges(); iter.hasNext(); iter.next()) {
1238 var node = iter.edge.node();
1239 if (!userRootsOnly || this._isUserRoot(node))
1244 _calculateDistances: function()
1246 var nodeFieldCount = this._nodeFieldCount;
1247 var nodeCount = this.nodeCount;
1248 var distances = this._nodeDistances = new Int32Array(nodeCount);
1249 var noDistance = this._noDistance;
1250 for (var i = 0; i < nodeCount; ++i)
1251 distances[i] = noDistance;
1253 var nodesToVisit = new Uint32Array(this.nodeCount);
1254 var nodesToVisitLength = 0;
1257 * @param {number} distance
1258 * @param {!WebInspector.HeapSnapshotNode} node
1260 function enqueueNode(distance, node)
1262 var ordinal = node.ordinal();
1263 if (distances[ordinal] !== noDistance)
1265 distances[ordinal] = distance;
1266 nodesToVisit[nodesToVisitLength++] = node.nodeIndex;
1269 this.forEachRoot(enqueueNode.bind(null, 1), true);
1270 this._bfs(nodesToVisit, nodesToVisitLength, distances);
1272 // bfs for the rest of objects
1273 nodesToVisitLength = 0;
1274 this.forEachRoot(enqueueNode.bind(null, WebInspector.HeapSnapshotCommon.baseSystemDistance), false);
1275 this._bfs(nodesToVisit, nodesToVisitLength, distances);
1279 * @param {!Uint32Array} nodesToVisit
1280 * @param {!number} nodesToVisitLength
1281 * @param {!Int32Array} distances
1283 _bfs: function(nodesToVisit, nodesToVisitLength, distances)
1285 // Preload fields into local variables for better performance.
1286 var edgeFieldsCount = this._edgeFieldsCount;
1287 var nodeFieldCount = this._nodeFieldCount;
1288 var containmentEdges = this.containmentEdges;
1289 var firstEdgeIndexes = this._firstEdgeIndexes;
1290 var edgeToNodeOffset = this._edgeToNodeOffset;
1291 var edgeTypeOffset = this._edgeTypeOffset;
1292 var nodeCount = this.nodeCount;
1293 var containmentEdgesLength = containmentEdges.length;
1294 var edgeWeakType = this._edgeWeakType;
1295 var noDistance = this._noDistance;
1298 while (index < nodesToVisitLength) {
1299 var nodeIndex = nodesToVisit[index++]; // shift generates too much garbage.
1300 var nodeOrdinal = nodeIndex / nodeFieldCount;
1301 var distance = distances[nodeOrdinal] + 1;
1302 var firstEdgeIndex = firstEdgeIndexes[nodeOrdinal];
1303 var edgesEnd = firstEdgeIndexes[nodeOrdinal + 1];
1304 for (var edgeIndex = firstEdgeIndex; edgeIndex < edgesEnd; edgeIndex += edgeFieldsCount) {
1305 var edgeType = containmentEdges[edgeIndex + edgeTypeOffset];
1306 if (edgeType == edgeWeakType)
1308 var childNodeIndex = containmentEdges[edgeIndex + edgeToNodeOffset];
1309 var childNodeOrdinal = childNodeIndex / nodeFieldCount;
1310 if (distances[childNodeOrdinal] !== noDistance)
1312 distances[childNodeOrdinal] = distance;
1313 nodesToVisit[nodesToVisitLength++] = childNodeIndex;
1316 if (nodesToVisitLength > nodeCount)
1317 throw new Error("BFS failed. Nodes to visit (" + nodesToVisitLength + ") is more than nodes count (" + nodeCount + ")");
1320 _buildAggregates: function(filter)
1322 var aggregates = {};
1323 var aggregatesByClassName = {};
1324 var classIndexes = [];
1325 var nodes = this.nodes;
1326 var mapAndFlag = this.userObjectsMapAndFlag();
1327 var flags = mapAndFlag ? mapAndFlag.map : null;
1328 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1329 var nodesLength = nodes.length;
1330 var nodeNativeType = this._nodeNativeType;
1331 var nodeFieldCount = this._nodeFieldCount;
1332 var selfSizeOffset = this._nodeSelfSizeOffset;
1333 var nodeTypeOffset = this._nodeTypeOffset;
1334 var node = this.rootNode();
1335 var nodeDistances = this._nodeDistances;
1337 for (var nodeIndex = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
1338 var nodeOrdinal = nodeIndex / nodeFieldCount;
1339 if (flags && !(flags[nodeOrdinal] & flag))
1341 node.nodeIndex = nodeIndex;
1342 if (filter && !filter(node))
1344 var selfSize = nodes[nodeIndex + selfSizeOffset];
1345 if (!selfSize && nodes[nodeIndex + nodeTypeOffset] !== nodeNativeType)
1347 var classIndex = node.classIndex();
1348 if (!(classIndex in aggregates)) {
1349 var nodeType = node.type();
1350 var nameMatters = nodeType === "object" || nodeType === "native";
1353 distance: nodeDistances[nodeOrdinal],
1357 name: nameMatters ? node.name() : null,
1360 aggregates[classIndex] = value;
1361 classIndexes.push(classIndex);
1362 aggregatesByClassName[node.className()] = value;
1364 var clss = aggregates[classIndex];
1365 clss.distance = Math.min(clss.distance, nodeDistances[nodeOrdinal]);
1367 clss.self += selfSize;
1368 clss.idxs.push(nodeIndex);
1372 // Shave off provisionally allocated space.
1373 for (var i = 0, l = classIndexes.length; i < l; ++i) {
1374 var classIndex = classIndexes[i];
1375 aggregates[classIndex].idxs = aggregates[classIndex].idxs.slice();
1377 return {aggregatesByClassName: aggregatesByClassName, aggregatesByClassIndex: aggregates};
1380 _calculateClassesRetainedSize: function(aggregates, filter)
1382 var rootNodeIndex = this._rootNodeIndex;
1383 var node = this.createNode(rootNodeIndex);
1384 var list = [rootNodeIndex];
1387 var seenClassNameIndexes = {};
1388 var nodeFieldCount = this._nodeFieldCount;
1389 var nodeTypeOffset = this._nodeTypeOffset;
1390 var nodeNativeType = this._nodeNativeType;
1391 var dominatedNodes = this._dominatedNodes;
1392 var nodes = this.nodes;
1393 var mapAndFlag = this.userObjectsMapAndFlag();
1394 var flags = mapAndFlag ? mapAndFlag.map : null;
1395 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1396 var firstDominatedNodeIndex = this._firstDominatedNodeIndex;
1398 while (list.length) {
1399 var nodeIndex = list.pop();
1400 node.nodeIndex = nodeIndex;
1401 var classIndex = node.classIndex();
1402 var seen = !!seenClassNameIndexes[classIndex];
1403 var nodeOrdinal = nodeIndex / nodeFieldCount;
1404 var dominatedIndexFrom = firstDominatedNodeIndex[nodeOrdinal];
1405 var dominatedIndexTo = firstDominatedNodeIndex[nodeOrdinal + 1];
1408 (!flags || (flags[nodeOrdinal] & flag)) &&
1409 (!filter || filter(node)) &&
1410 (node.selfSize() || nodes[nodeIndex + nodeTypeOffset] === nodeNativeType)
1412 aggregates[classIndex].maxRet += node.retainedSize();
1413 if (dominatedIndexFrom !== dominatedIndexTo) {
1414 seenClassNameIndexes[classIndex] = true;
1415 sizes.push(list.length);
1416 classes.push(classIndex);
1419 for (var i = dominatedIndexFrom; i < dominatedIndexTo; i++)
1420 list.push(dominatedNodes[i]);
1422 var l = list.length;
1423 while (sizes[sizes.length - 1] === l) {
1425 classIndex = classes.pop();
1426 seenClassNameIndexes[classIndex] = false;
1431 _sortAggregateIndexes: function(aggregates)
1433 var nodeA = this.createNode();
1434 var nodeB = this.createNode();
1435 for (var clss in aggregates)
1436 aggregates[clss].idxs.sort(
1437 function(idxA, idxB) {
1438 nodeA.nodeIndex = idxA;
1439 nodeB.nodeIndex = idxB;
1440 return nodeA.id() < nodeB.id() ? -1 : 1;
1444 _buildPostOrderIndex: function()
1446 var nodeFieldCount = this._nodeFieldCount;
1447 var nodes = this.nodes;
1448 var nodeCount = this.nodeCount;
1449 var rootNodeOrdinal = this._rootNodeIndex / nodeFieldCount;
1451 var edgeFieldsCount = this._edgeFieldsCount;
1452 var edgeTypeOffset = this._edgeTypeOffset;
1453 var edgeToNodeOffset = this._edgeToNodeOffset;
1454 var edgeShortcutType = this._edgeShortcutType;
1455 var firstEdgeIndexes = this._firstEdgeIndexes;
1456 var containmentEdges = this.containmentEdges;
1458 var mapAndFlag = this.userObjectsMapAndFlag();
1459 var flags = mapAndFlag ? mapAndFlag.map : null;
1460 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1462 var stackNodes = new Uint32Array(nodeCount);
1463 var stackCurrentEdge = new Uint32Array(nodeCount);
1464 var postOrderIndex2NodeOrdinal = new Uint32Array(nodeCount);
1465 var nodeOrdinal2PostOrderIndex = new Uint32Array(nodeCount);
1466 var visited = new Uint8Array(nodeCount);
1467 var postOrderIndex = 0;
1470 stackNodes[0] = rootNodeOrdinal;
1471 stackCurrentEdge[0] = firstEdgeIndexes[rootNodeOrdinal];
1472 visited[rootNodeOrdinal] = 1;
1474 while (stackTop >= 0) {
1475 var nodeOrdinal = stackNodes[stackTop];
1476 var edgeIndex = stackCurrentEdge[stackTop];
1477 var edgesEnd = firstEdgeIndexes[nodeOrdinal + 1];
1479 if (edgeIndex < edgesEnd) {
1480 stackCurrentEdge[stackTop] += edgeFieldsCount;
1481 if (nodeOrdinal !== rootNodeOrdinal && containmentEdges[edgeIndex + edgeTypeOffset] === edgeShortcutType)
1483 var childNodeIndex = containmentEdges[edgeIndex + edgeToNodeOffset];
1484 var childNodeOrdinal = childNodeIndex / nodeFieldCount;
1485 if (visited[childNodeOrdinal])
1487 var nodeFlag = !flags || (flags[nodeOrdinal] & flag);
1488 var childNodeFlag = !flags || (flags[childNodeOrdinal] & flag);
1489 // We are skipping the edges from non-page-owned nodes to page-owned nodes.
1490 // Otherwise the dominators for the objects that also were retained by debugger would be affected.
1491 if (nodeOrdinal !== rootNodeOrdinal && childNodeFlag && !nodeFlag)
1494 stackNodes[stackTop] = childNodeOrdinal;
1495 stackCurrentEdge[stackTop] = firstEdgeIndexes[childNodeOrdinal];
1496 visited[childNodeOrdinal] = 1;
1498 // Done with all the node children
1499 nodeOrdinal2PostOrderIndex[nodeOrdinal] = postOrderIndex;
1500 postOrderIndex2NodeOrdinal[postOrderIndex++] = nodeOrdinal;
1505 if (postOrderIndex !== nodeCount) {
1506 console.log("Error: Corrupted snapshot. " + (nodeCount - postOrderIndex) + " nodes are unreachable from the root:");
1507 var dumpNode = this.rootNode();
1508 for (var i = 0; i < nodeCount; ++i) {
1510 // Fix it by giving the node a postorder index anyway.
1511 nodeOrdinal2PostOrderIndex[i] = postOrderIndex;
1512 postOrderIndex2NodeOrdinal[postOrderIndex++] = i;
1513 dumpNode.nodeIndex = i * nodeFieldCount;
1514 console.log(JSON.stringify(dumpNode.serialize()));
1515 for (var retainers = dumpNode.retainers(); retainers.hasNext(); retainers = retainers.item().node().retainers())
1516 console.log(" edgeName: " + retainers.item().name() + " nodeClassName: " + retainers.item().node().className());
1521 return {postOrderIndex2NodeOrdinal: postOrderIndex2NodeOrdinal, nodeOrdinal2PostOrderIndex: nodeOrdinal2PostOrderIndex};
1524 // The algorithm is based on the article:
1525 // K. Cooper, T. Harvey and K. Kennedy "A Simple, Fast Dominance Algorithm"
1526 // Softw. Pract. Exper. 4 (2001), pp. 1-10.
1528 * @param {!Array.<number>} postOrderIndex2NodeOrdinal
1529 * @param {!Array.<number>} nodeOrdinal2PostOrderIndex
1531 _buildDominatorTree: function(postOrderIndex2NodeOrdinal, nodeOrdinal2PostOrderIndex)
1533 var nodeFieldCount = this._nodeFieldCount;
1534 var nodes = this.nodes;
1535 var firstRetainerIndex = this._firstRetainerIndex;
1536 var retainingNodes = this._retainingNodes;
1537 var retainingEdges = this._retainingEdges;
1538 var edgeFieldsCount = this._edgeFieldsCount;
1539 var edgeTypeOffset = this._edgeTypeOffset;
1540 var edgeToNodeOffset = this._edgeToNodeOffset;
1541 var edgeShortcutType = this._edgeShortcutType;
1542 var firstEdgeIndexes = this._firstEdgeIndexes;
1543 var containmentEdges = this.containmentEdges;
1544 var containmentEdgesLength = this.containmentEdges.length;
1545 var rootNodeIndex = this._rootNodeIndex;
1547 var mapAndFlag = this.userObjectsMapAndFlag();
1548 var flags = mapAndFlag ? mapAndFlag.map : null;
1549 var flag = mapAndFlag ? mapAndFlag.flag : 0;
1551 var nodesCount = postOrderIndex2NodeOrdinal.length;
1552 var rootPostOrderedIndex = nodesCount - 1;
1553 var noEntry = nodesCount;
1554 var dominators = new Uint32Array(nodesCount);
1555 for (var i = 0; i < rootPostOrderedIndex; ++i)
1556 dominators[i] = noEntry;
1557 dominators[rootPostOrderedIndex] = rootPostOrderedIndex;
1559 // The affected array is used to mark entries which dominators
1560 // have to be racalculated because of changes in their retainers.
1561 var affected = new Uint8Array(nodesCount);
1564 { // Mark the root direct children as affected.
1565 nodeOrdinal = this._rootNodeIndex / nodeFieldCount;
1566 var beginEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal] + edgeToNodeOffset;
1567 var endEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal + 1];
1568 for (var toNodeFieldIndex = beginEdgeToNodeFieldIndex;
1569 toNodeFieldIndex < endEdgeToNodeFieldIndex;
1570 toNodeFieldIndex += edgeFieldsCount) {
1571 var childNodeOrdinal = containmentEdges[toNodeFieldIndex] / nodeFieldCount;
1572 affected[nodeOrdinal2PostOrderIndex[childNodeOrdinal]] = 1;
1579 for (var postOrderIndex = rootPostOrderedIndex - 1; postOrderIndex >= 0; --postOrderIndex) {
1580 if (affected[postOrderIndex] === 0)
1582 affected[postOrderIndex] = 0;
1583 // If dominator of the entry has already been set to root,
1584 // then it can't propagate any further.
1585 if (dominators[postOrderIndex] === rootPostOrderedIndex)
1587 nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1588 var nodeFlag = !flags || (flags[nodeOrdinal] & flag);
1589 var newDominatorIndex = noEntry;
1590 var beginRetainerIndex = firstRetainerIndex[nodeOrdinal];
1591 var endRetainerIndex = firstRetainerIndex[nodeOrdinal + 1];
1592 for (var retainerIndex = beginRetainerIndex; retainerIndex < endRetainerIndex; ++retainerIndex) {
1593 var retainerEdgeIndex = retainingEdges[retainerIndex];
1594 var retainerEdgeType = containmentEdges[retainerEdgeIndex + edgeTypeOffset];
1595 var retainerNodeIndex = retainingNodes[retainerIndex];
1596 if (retainerNodeIndex !== rootNodeIndex && retainerEdgeType === edgeShortcutType)
1598 var retainerNodeOrdinal = retainerNodeIndex / nodeFieldCount;
1599 var retainerNodeFlag = !flags || (flags[retainerNodeOrdinal] & flag);
1600 // We are skipping the edges from non-page-owned nodes to page-owned nodes.
1601 // Otherwise the dominators for the objects that also were retained by debugger would be affected.
1602 if (retainerNodeIndex !== rootNodeIndex && nodeFlag && !retainerNodeFlag)
1604 var retanerPostOrderIndex = nodeOrdinal2PostOrderIndex[retainerNodeOrdinal];
1605 if (dominators[retanerPostOrderIndex] !== noEntry) {
1606 if (newDominatorIndex === noEntry)
1607 newDominatorIndex = retanerPostOrderIndex;
1609 while (retanerPostOrderIndex !== newDominatorIndex) {
1610 while (retanerPostOrderIndex < newDominatorIndex)
1611 retanerPostOrderIndex = dominators[retanerPostOrderIndex];
1612 while (newDominatorIndex < retanerPostOrderIndex)
1613 newDominatorIndex = dominators[newDominatorIndex];
1616 // If idom has already reached the root, it doesn't make sense
1617 // to check other retainers.
1618 if (newDominatorIndex === rootPostOrderedIndex)
1622 if (newDominatorIndex !== noEntry && dominators[postOrderIndex] !== newDominatorIndex) {
1623 dominators[postOrderIndex] = newDominatorIndex;
1625 nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1626 beginEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal] + edgeToNodeOffset;
1627 endEdgeToNodeFieldIndex = firstEdgeIndexes[nodeOrdinal + 1];
1628 for (var toNodeFieldIndex = beginEdgeToNodeFieldIndex;
1629 toNodeFieldIndex < endEdgeToNodeFieldIndex;
1630 toNodeFieldIndex += edgeFieldsCount) {
1631 var childNodeOrdinal = containmentEdges[toNodeFieldIndex] / nodeFieldCount;
1632 affected[nodeOrdinal2PostOrderIndex[childNodeOrdinal]] = 1;
1638 var dominatorsTree = new Uint32Array(nodesCount);
1639 for (var postOrderIndex = 0, l = dominators.length; postOrderIndex < l; ++postOrderIndex) {
1640 nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1641 dominatorsTree[nodeOrdinal] = postOrderIndex2NodeOrdinal[dominators[postOrderIndex]];
1643 return dominatorsTree;
1646 _calculateRetainedSizes: function(postOrderIndex2NodeOrdinal)
1648 var nodeCount = this.nodeCount;
1649 var nodes = this.nodes;
1650 var nodeSelfSizeOffset = this._nodeSelfSizeOffset;
1651 var nodeFieldCount = this._nodeFieldCount;
1652 var dominatorsTree = this._dominatorsTree;
1653 var retainedSizes = this._retainedSizes = new Float64Array(nodeCount);
1655 for (var nodeOrdinal = 0; nodeOrdinal < nodeCount; ++nodeOrdinal)
1656 retainedSizes[nodeOrdinal] = nodes[nodeOrdinal * nodeFieldCount + nodeSelfSizeOffset];
1658 // Propagate retained sizes for each node excluding root.
1659 for (var postOrderIndex = 0; postOrderIndex < nodeCount - 1; ++postOrderIndex) {
1660 var nodeOrdinal = postOrderIndex2NodeOrdinal[postOrderIndex];
1661 var dominatorOrdinal = dominatorsTree[nodeOrdinal];
1662 retainedSizes[dominatorOrdinal] += retainedSizes[nodeOrdinal];
1666 _buildDominatedNodes: function()
1668 // Builds up two arrays:
1669 // - "dominatedNodes" is a continuous array, where each node owns an
1670 // interval (can be empty) with corresponding dominated nodes.
1671 // - "indexArray" is an array of indexes in the "dominatedNodes"
1672 // with the same positions as in the _nodeIndex.
1673 var indexArray = this._firstDominatedNodeIndex = new Uint32Array(this.nodeCount + 1);
1674 // All nodes except the root have dominators.
1675 var dominatedNodes = this._dominatedNodes = new Uint32Array(this.nodeCount - 1);
1677 // Count the number of dominated nodes for each node. Skip the root (node at
1678 // index 0) as it is the only node that dominates itself.
1679 var nodeFieldCount = this._nodeFieldCount;
1680 var dominatorsTree = this._dominatorsTree;
1682 var fromNodeOrdinal = 0;
1683 var toNodeOrdinal = this.nodeCount;
1684 var rootNodeOrdinal = this._rootNodeIndex / nodeFieldCount;
1685 if (rootNodeOrdinal === fromNodeOrdinal)
1686 fromNodeOrdinal = 1;
1687 else if (rootNodeOrdinal === toNodeOrdinal - 1)
1688 toNodeOrdinal = toNodeOrdinal - 1;
1690 throw new Error("Root node is expected to be either first or last");
1691 for (var nodeOrdinal = fromNodeOrdinal; nodeOrdinal < toNodeOrdinal; ++nodeOrdinal)
1692 ++indexArray[dominatorsTree[nodeOrdinal]];
1693 // Put in the first slot of each dominatedNodes slice the count of entries
1694 // that will be filled.
1695 var firstDominatedNodeIndex = 0;
1696 for (var i = 0, l = this.nodeCount; i < l; ++i) {
1697 var dominatedCount = dominatedNodes[firstDominatedNodeIndex] = indexArray[i];
1698 indexArray[i] = firstDominatedNodeIndex;
1699 firstDominatedNodeIndex += dominatedCount;
1701 indexArray[this.nodeCount] = dominatedNodes.length;
1702 // Fill up the dominatedNodes array with indexes of dominated nodes. Skip the root (node at
1703 // index 0) as it is the only node that dominates itself.
1704 for (var nodeOrdinal = fromNodeOrdinal; nodeOrdinal < toNodeOrdinal; ++nodeOrdinal) {
1705 var dominatorOrdinal = dominatorsTree[nodeOrdinal];
1706 var dominatedRefIndex = indexArray[dominatorOrdinal];
1707 dominatedRefIndex += (--dominatedNodes[dominatedRefIndex]);
1708 dominatedNodes[dominatedRefIndex] = nodeOrdinal * nodeFieldCount;
1712 _calculateFlags: function()
1714 throw new Error("Not implemented");
1717 _calculateStatistics: function()
1719 throw new Error("Not implemented");
1722 userObjectsMapAndFlag: function()
1724 throw new Error("Not implemented");
1728 * @param {string} baseSnapshotId
1729 * @param {!Object.<string, !WebInspector.HeapSnapshotCommon.AggregateForDiff>} baseSnapshotAggregates
1730 * @return {!Object.<string, !WebInspector.HeapSnapshotCommon.Diff>}
1732 calculateSnapshotDiff: function(baseSnapshotId, baseSnapshotAggregates)
1734 var snapshotDiff = this._snapshotDiffs[baseSnapshotId];
1736 return snapshotDiff;
1739 var aggregates = this.aggregates(true, "allObjects");
1740 for (var className in baseSnapshotAggregates) {
1741 var baseAggregate = baseSnapshotAggregates[className];
1742 var diff = this._calculateDiffForClass(baseAggregate, aggregates[className]);
1744 snapshotDiff[className] = diff;
1746 var emptyBaseAggregate = new WebInspector.HeapSnapshotCommon.AggregateForDiff();
1747 for (var className in aggregates) {
1748 if (className in baseSnapshotAggregates)
1750 snapshotDiff[className] = this._calculateDiffForClass(emptyBaseAggregate, aggregates[className]);
1753 this._snapshotDiffs[baseSnapshotId] = snapshotDiff;
1754 return snapshotDiff;
1758 * @param {!WebInspector.HeapSnapshotCommon.AggregateForDiff} baseAggregate
1759 * @param {!WebInspector.HeapSnapshotCommon.Aggregate} aggregate
1760 * @return {?WebInspector.HeapSnapshotCommon.Diff}
1762 _calculateDiffForClass: function(baseAggregate, aggregate)
1764 var baseIds = baseAggregate.ids;
1765 var baseIndexes = baseAggregate.indexes;
1766 var baseSelfSizes = baseAggregate.selfSizes;
1768 var indexes = aggregate ? aggregate.idxs : [];
1770 var i = 0, l = baseIds.length;
1771 var j = 0, m = indexes.length;
1772 var diff = new WebInspector.HeapSnapshotCommon.Diff();
1774 var nodeB = this.createNode(indexes[j]);
1775 while (i < l && j < m) {
1776 var nodeAId = baseIds[i];
1777 if (nodeAId < nodeB.id()) {
1778 diff.deletedIndexes.push(baseIndexes[i]);
1779 diff.removedCount++;
1780 diff.removedSize += baseSelfSizes[i];
1782 } else if (nodeAId > nodeB.id()) { // Native nodes(e.g. dom groups) may have ids less than max JS object id in the base snapshot
1783 diff.addedIndexes.push(indexes[j]);
1785 diff.addedSize += nodeB.selfSize();
1786 nodeB.nodeIndex = indexes[++j];
1787 } else { // nodeAId === nodeB.id()
1789 nodeB.nodeIndex = indexes[++j];
1793 diff.deletedIndexes.push(baseIndexes[i]);
1794 diff.removedCount++;
1795 diff.removedSize += baseSelfSizes[i];
1799 diff.addedIndexes.push(indexes[j]);
1801 diff.addedSize += nodeB.selfSize();
1802 nodeB.nodeIndex = indexes[++j];
1804 diff.countDelta = diff.addedCount - diff.removedCount;
1805 diff.sizeDelta = diff.addedSize - diff.removedSize;
1806 if (!diff.addedCount && !diff.removedCount)
1811 _nodeForSnapshotObjectId: function(snapshotObjectId)
1813 for (var it = this._allNodes(); it.hasNext(); it.next()) {
1814 if (it.node.id() === snapshotObjectId)
1821 * @param {string} snapshotObjectId
1824 nodeClassName: function(snapshotObjectId)
1826 var node = this._nodeForSnapshotObjectId(snapshotObjectId);
1828 return node.className();
1833 * @param {string} name
1834 * @return {!Array.<number>}
1836 idsOfObjectsWithName: function(name)
1839 for (var it = this._allNodes(); it.hasNext(); it.next()) {
1840 if (it.item().name() === name)
1841 ids.push(it.item().id());
1847 * @param {number} nodeIndex
1848 * @return {!WebInspector.HeapSnapshotEdgesProvider}
1850 createEdgesProvider: function(nodeIndex)
1852 var node = this.createNode(nodeIndex);
1853 var filter = this.containmentEdgesFilter();
1854 var indexProvider = new WebInspector.HeapSnapshotEdgeIndexProvider(this);
1855 return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
1859 * @param {number} nodeIndex
1860 * @param {?function(!WebInspector.HeapSnapshotEdge):boolean} filter
1861 * @return {!WebInspector.HeapSnapshotEdgesProvider}
1863 createEdgesProviderForTest: function(nodeIndex, filter)
1865 var node = this.createNode(nodeIndex);
1866 var indexProvider = new WebInspector.HeapSnapshotEdgeIndexProvider(this);
1867 return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
1871 * @return {?function(!WebInspector.HeapSnapshotEdge):boolean}
1873 retainingEdgesFilter: function()
1879 * @return {?function(!WebInspector.HeapSnapshotEdge):boolean}
1881 containmentEdgesFilter: function()
1887 * @param {number} nodeIndex
1888 * @return {!WebInspector.HeapSnapshotEdgesProvider}
1890 createRetainingEdgesProvider: function(nodeIndex)
1892 var node = this.createNode(nodeIndex);
1893 var filter = this.retainingEdgesFilter();
1894 var indexProvider = new WebInspector.HeapSnapshotRetainerEdgeIndexProvider(this);
1895 return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.retainers(), indexProvider);
1899 * @param {string} baseSnapshotId
1900 * @param {string} className
1901 * @return {!WebInspector.HeapSnapshotNodesProvider}
1903 createAddedNodesProvider: function(baseSnapshotId, className)
1905 var snapshotDiff = this._snapshotDiffs[baseSnapshotId];
1906 var diffForClass = snapshotDiff[className];
1907 return new WebInspector.HeapSnapshotNodesProvider(this, null, diffForClass.addedIndexes);
1911 * @param {!Array.<number>} nodeIndexes
1912 * @return {!WebInspector.HeapSnapshotNodesProvider}
1914 createDeletedNodesProvider: function(nodeIndexes)
1916 return new WebInspector.HeapSnapshotNodesProvider(this, null, nodeIndexes);
1920 * @return {?function(!WebInspector.HeapSnapshotNode):boolean}
1922 classNodesFilter: function()
1928 * @param {string} className
1929 * @param {!WebInspector.HeapSnapshotCommon.NodeFilter} nodeFilter
1930 * @return {!WebInspector.HeapSnapshotNodesProvider}
1932 createNodesProviderForClass: function(className, nodeFilter)
1934 return new WebInspector.HeapSnapshotNodesProvider(this, this.classNodesFilter(), this.aggregatesWithFilter(nodeFilter)[className].idxs);
1940 _maxJsNodeId: function()
1942 var nodeFieldCount = this._nodeFieldCount;
1943 var nodes = this.nodes;
1944 var nodesLength = nodes.length;
1946 for (var nodeIndex = this._nodeIdOffset; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
1947 var nextId = nodes[nodeIndex];
1948 // JS objects have odd ids, skip native objects.
1949 if (nextId % 2 === 0)
1958 * @return {!WebInspector.HeapSnapshotCommon.StaticData}
1960 updateStaticData: function()
1962 return new WebInspector.HeapSnapshotCommon.StaticData(this.nodeCount, this._rootNodeIndex, this.totalSize, this._maxJsNodeId());
1968 * @param {!WebInspector.HeapSnapshotItemIterator} iterator
1969 * @param {!WebInspector.HeapSnapshotItemIndexProvider} indexProvider
1971 WebInspector.HeapSnapshotItemProvider = function(iterator, indexProvider)
1973 this._iterator = iterator;
1974 this._indexProvider = indexProvider;
1975 this._isEmpty = !iterator.hasNext();
1976 /** @type {?Array.<number>} */
1977 this._iterationOrder = null;
1978 this._currentComparator = null;
1979 this._sortedPrefixLength = 0;
1980 this._sortedSuffixLength = 0;
1983 WebInspector.HeapSnapshotItemProvider.prototype = {
1984 _createIterationOrder: function()
1986 if (this._iterationOrder)
1988 this._iterationOrder = [];
1989 for (var iterator = this._iterator; iterator.hasNext(); iterator.next())
1990 this._iterationOrder.push(iterator.item().itemIndex());
1998 return this._isEmpty;
2002 * @param {number} begin
2003 * @param {number} end
2004 * @return {!WebInspector.HeapSnapshotCommon.ItemsRange}
2006 serializeItemsRange: function(begin, end)
2008 this._createIterationOrder();
2010 throw new Error("Start position > end position: " + begin + " > " + end);
2011 if (end > this._iterationOrder.length)
2012 end = this._iterationOrder.length;
2013 if (this._sortedPrefixLength < end && begin < this._iterationOrder.length - this._sortedSuffixLength) {
2014 this.sort(this._currentComparator, this._sortedPrefixLength, this._iterationOrder.length - 1 - this._sortedSuffixLength, begin, end - 1);
2015 if (begin <= this._sortedPrefixLength)
2016 this._sortedPrefixLength = end;
2017 if (end >= this._iterationOrder.length - this._sortedSuffixLength)
2018 this._sortedSuffixLength = this._iterationOrder.length - begin;
2020 var position = begin;
2021 var count = end - begin;
2022 var result = new Array(count);
2023 var iterator = this._iterator;
2024 for (var i = 0 ; i < count; ++i) {
2025 var itemIndex = this._iterationOrder[position++];
2026 var item = this._indexProvider.itemForIndex(itemIndex);
2027 result[i] = item.serialize();
2029 return new WebInspector.HeapSnapshotCommon.ItemsRange(begin, end, this._iterationOrder.length, result);
2032 sortAndRewind: function(comparator)
2034 this._currentComparator = comparator;
2035 this._sortedPrefixLength = 0;
2036 this._sortedSuffixLength = 0;
2042 * @extends {WebInspector.HeapSnapshotItemProvider}
2043 * @param {!WebInspector.HeapSnapshot} snapshot
2044 * @param {?function(!WebInspector.HeapSnapshotEdge):boolean} filter
2045 * @param {!WebInspector.HeapSnapshotEdgeIterator} edgesIter
2046 * @param {!WebInspector.HeapSnapshotItemIndexProvider} indexProvider
2048 WebInspector.HeapSnapshotEdgesProvider = function(snapshot, filter, edgesIter, indexProvider)
2050 this.snapshot = snapshot;
2051 var iter = filter ? new WebInspector.HeapSnapshotFilteredIterator(edgesIter, /** @type {function(!WebInspector.HeapSnapshotItem):boolean} */ (filter)) : edgesIter;
2052 WebInspector.HeapSnapshotItemProvider.call(this, iter, indexProvider);
2055 WebInspector.HeapSnapshotEdgesProvider.prototype = {
2057 * @param {!WebInspector.HeapSnapshotCommon.ComparatorConfig} comparator
2058 * @param {number} leftBound
2059 * @param {number} rightBound
2060 * @param {number} windowLeft
2061 * @param {number} windowRight
2063 sort: function(comparator, leftBound, rightBound, windowLeft, windowRight)
2065 var fieldName1 = comparator.fieldName1;
2066 var fieldName2 = comparator.fieldName2;
2067 var ascending1 = comparator.ascending1;
2068 var ascending2 = comparator.ascending2;
2070 var edgeA = this._iterator.item().clone();
2071 var edgeB = edgeA.clone();
2072 var nodeA = this.snapshot.createNode();
2073 var nodeB = this.snapshot.createNode();
2075 function compareEdgeFieldName(ascending, indexA, indexB)
2077 edgeA.edgeIndex = indexA;
2078 edgeB.edgeIndex = indexB;
2079 if (edgeB.name() === "__proto__") return -1;
2080 if (edgeA.name() === "__proto__") return 1;
2082 edgeA.hasStringName() === edgeB.hasStringName() ?
2083 (edgeA.name() < edgeB.name() ? -1 : (edgeA.name() > edgeB.name() ? 1 : 0)) :
2084 (edgeA.hasStringName() ? -1 : 1);
2085 return ascending ? result : -result;
2088 function compareNodeField(fieldName, ascending, indexA, indexB)
2090 edgeA.edgeIndex = indexA;
2091 nodeA.nodeIndex = edgeA.nodeIndex();
2092 var valueA = nodeA[fieldName]();
2094 edgeB.edgeIndex = indexB;
2095 nodeB.nodeIndex = edgeB.nodeIndex();
2096 var valueB = nodeB[fieldName]();
2098 var result = valueA < valueB ? -1 : (valueA > valueB ? 1 : 0);
2099 return ascending ? result : -result;
2102 function compareEdgeAndNode(indexA, indexB) {
2103 var result = compareEdgeFieldName(ascending1, indexA, indexB);
2105 result = compareNodeField(fieldName2, ascending2, indexA, indexB);
2107 return indexA - indexB;
2111 function compareNodeAndEdge(indexA, indexB) {
2112 var result = compareNodeField(fieldName1, ascending1, indexA, indexB);
2114 result = compareEdgeFieldName(ascending2, indexA, indexB);
2116 return indexA - indexB;
2120 function compareNodeAndNode(indexA, indexB) {
2121 var result = compareNodeField(fieldName1, ascending1, indexA, indexB);
2123 result = compareNodeField(fieldName2, ascending2, indexA, indexB);
2125 return indexA - indexB;
2129 if (fieldName1 === "!edgeName")
2130 this._iterationOrder.sortRange(compareEdgeAndNode, leftBound, rightBound, windowLeft, windowRight);
2131 else if (fieldName2 === "!edgeName")
2132 this._iterationOrder.sortRange(compareNodeAndEdge, leftBound, rightBound, windowLeft, windowRight);
2134 this._iterationOrder.sortRange(compareNodeAndNode, leftBound, rightBound, windowLeft, windowRight);
2137 __proto__: WebInspector.HeapSnapshotItemProvider.prototype
2143 * @extends {WebInspector.HeapSnapshotItemProvider}
2144 * @param {!WebInspector.HeapSnapshot} snapshot
2145 * @param {?function(!WebInspector.HeapSnapshotNode):boolean} filter
2146 * @param {(!Array.<number>|!Uint32Array)} nodeIndexes
2148 WebInspector.HeapSnapshotNodesProvider = function(snapshot, filter, nodeIndexes)
2150 this.snapshot = snapshot;
2151 var indexProvider = new WebInspector.HeapSnapshotNodeIndexProvider(snapshot);
2152 var it = new WebInspector.HeapSnapshotIndexRangeIterator(indexProvider, nodeIndexes);
2155 it = new WebInspector.HeapSnapshotFilteredIterator(it, /** @type {function(!WebInspector.HeapSnapshotItem):boolean} */ (filter));
2156 WebInspector.HeapSnapshotItemProvider.call(this, it, indexProvider);
2159 WebInspector.HeapSnapshotNodesProvider.prototype = {
2161 * @param {string} snapshotObjectId
2164 nodePosition: function(snapshotObjectId)
2166 this._createIterationOrder();
2167 var node = this.snapshot.createNode();
2168 for (var i = 0; i < this._iterationOrder.length; i++) {
2169 node.nodeIndex = this._iterationOrder[i];
2170 if (node.id() === snapshotObjectId)
2173 if (i === this._iterationOrder.length)
2175 var targetNodeIndex = this._iterationOrder[i];
2176 var smallerCount = 0;
2177 var compare = this._buildCompareFunction(this._currentComparator);
2178 for (var i = 0; i < this._iterationOrder.length; i++) {
2179 if (compare(this._iterationOrder[i], targetNodeIndex) < 0)
2182 return smallerCount;
2186 * @return {function(number,number):number}
2188 _buildCompareFunction: function(comparator)
2190 var nodeA = this.snapshot.createNode();
2191 var nodeB = this.snapshot.createNode();
2192 var fieldAccessor1 = nodeA[comparator.fieldName1];
2193 var fieldAccessor2 = nodeA[comparator.fieldName2];
2194 var ascending1 = comparator.ascending1 ? 1 : -1;
2195 var ascending2 = comparator.ascending2 ? 1 : -1;
2198 * @param {function():*} fieldAccessor
2199 * @param {number} ascending
2202 function sortByNodeField(fieldAccessor, ascending)
2204 var valueA = fieldAccessor.call(nodeA);
2205 var valueB = fieldAccessor.call(nodeB);
2206 return valueA < valueB ? -ascending : (valueA > valueB ? ascending : 0);
2210 * @param {number} indexA
2211 * @param {number} indexB
2214 function sortByComparator(indexA, indexB)
2216 nodeA.nodeIndex = indexA;
2217 nodeB.nodeIndex = indexB;
2218 var result = sortByNodeField(fieldAccessor1, ascending1);
2220 result = sortByNodeField(fieldAccessor2, ascending2);
2221 return result || indexA - indexB;
2224 return sortByComparator;
2228 * @param {!WebInspector.HeapSnapshotCommon.ComparatorConfig} comparator
2229 * @param {number} leftBound
2230 * @param {number} rightBound
2231 * @param {number} windowLeft
2232 * @param {number} windowRight
2234 sort: function(comparator, leftBound, rightBound, windowLeft, windowRight)
2236 this._iterationOrder.sortRange(this._buildCompareFunction(comparator), leftBound, rightBound, windowLeft, windowRight);
2239 __proto__: WebInspector.HeapSnapshotItemProvider.prototype