HeapSnapshotGridNodes.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (C) 2011 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  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
  13. * distribution.
  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.
  17. *
  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.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.DataGridNode}
  33. * @param {WebInspector.HeapSnapshotSortableDataGrid} tree
  34. * @param {boolean} hasChildren
  35. */
  36. WebInspector.HeapSnapshotGridNode = function(tree, hasChildren)
  37. {
  38. WebInspector.DataGridNode.call(this, null, hasChildren);
  39. this._dataGrid = tree;
  40. this._instanceCount = 0;
  41. this._savedChildren = null;
  42. /**
  43. * List of position ranges for all visible nodes: [startPos1, endPos1),...,[startPosN, endPosN)
  44. * Position is an item position in the provider.
  45. */
  46. this._retrievedChildrenRanges = [];
  47. }
  48. WebInspector.HeapSnapshotGridNode.Events = {
  49. PopulateComplete: "PopulateComplete"
  50. }
  51. WebInspector.HeapSnapshotGridNode.prototype = {
  52. /**
  53. * @return {WebInspector.HeapSnapshotProviderProxy}
  54. */
  55. createProvider: function()
  56. {
  57. throw new Error("Needs implemented.");
  58. },
  59. /**
  60. * @return {WebInspector.HeapSnapshotProviderProxy}
  61. */
  62. _provider: function()
  63. {
  64. if (!this._providerObject)
  65. this._providerObject = this.createProvider();
  66. return this._providerObject;
  67. },
  68. createCell: function(columnIdentifier)
  69. {
  70. var cell = WebInspector.DataGridNode.prototype.createCell.call(this, columnIdentifier);
  71. if (this._searchMatched)
  72. cell.addStyleClass("highlight");
  73. return cell;
  74. },
  75. collapse: function()
  76. {
  77. WebInspector.DataGridNode.prototype.collapse.call(this);
  78. this._dataGrid.updateVisibleNodes();
  79. },
  80. dispose: function()
  81. {
  82. if (this._provider())
  83. this._provider().dispose();
  84. for (var node = this.children[0]; node; node = node.traverseNextNode(true, this, true))
  85. if (node.dispose)
  86. node.dispose();
  87. },
  88. _reachableFromWindow: false,
  89. queryObjectContent: function(callback)
  90. {
  91. },
  92. /**
  93. * @override
  94. */
  95. wasDetached: function()
  96. {
  97. this._dataGrid.nodeWasDetached(this);
  98. },
  99. _toPercentString: function(num)
  100. {
  101. return num.toFixed(0) + "\u2009%"; // \u2009 is a thin space.
  102. },
  103. /**
  104. * @param {number} nodePosition
  105. */
  106. childForPosition: function(nodePosition)
  107. {
  108. var indexOfFirsChildInRange = 0;
  109. for (var i = 0; i < this._retrievedChildrenRanges.length; i++) {
  110. var range = this._retrievedChildrenRanges[i];
  111. if (range.from <= nodePosition && nodePosition < range.to) {
  112. var childIndex = indexOfFirsChildInRange + nodePosition - range.from;
  113. return this.children[childIndex];
  114. }
  115. indexOfFirsChildInRange += range.to - range.from + 1;
  116. }
  117. return null;
  118. },
  119. _createValueCell: function(columnIdentifier)
  120. {
  121. var cell = document.createElement("td");
  122. cell.className = columnIdentifier + "-column";
  123. if (this.dataGrid.snapshot.totalSize !== 0) {
  124. var div = document.createElement("div");
  125. var valueSpan = document.createElement("span");
  126. valueSpan.textContent = this.data[columnIdentifier];
  127. div.appendChild(valueSpan);
  128. var percentColumn = columnIdentifier + "-percent";
  129. if (percentColumn in this.data) {
  130. var percentSpan = document.createElement("span");
  131. percentSpan.className = "percent-column";
  132. percentSpan.textContent = this.data[percentColumn];
  133. div.appendChild(percentSpan);
  134. div.addStyleClass("heap-snapshot-multiple-values");
  135. }
  136. cell.appendChild(div);
  137. }
  138. return cell;
  139. },
  140. populate: function(event)
  141. {
  142. if (this._populated)
  143. return;
  144. this._populated = true;
  145. function sorted()
  146. {
  147. this._populateChildren();
  148. }
  149. this._provider().sortAndRewind(this.comparator(), sorted.bind(this));
  150. },
  151. expandWithoutPopulate: function(callback)
  152. {
  153. // Make sure default populate won't take action.
  154. this._populated = true;
  155. this.expand();
  156. this._provider().sortAndRewind(this.comparator(), callback);
  157. },
  158. /**
  159. * @param {?number} fromPosition
  160. * @param {?number} toPosition
  161. */
  162. _populateChildren: function(fromPosition, toPosition, afterPopulate)
  163. {
  164. fromPosition = fromPosition || 0;
  165. toPosition = toPosition || fromPosition + this._dataGrid.defaultPopulateCount();
  166. var firstNotSerializedPosition = fromPosition;
  167. function serializeNextChunk()
  168. {
  169. if (firstNotSerializedPosition >= toPosition)
  170. return;
  171. var end = Math.min(firstNotSerializedPosition + this._dataGrid.defaultPopulateCount(), toPosition);
  172. this._provider().serializeItemsRange(firstNotSerializedPosition, end, childrenRetrieved.bind(this));
  173. firstNotSerializedPosition = end;
  174. }
  175. function insertRetrievedChild(item, insertionIndex)
  176. {
  177. if (this._savedChildren) {
  178. var hash = this._childHashForEntity(item);
  179. if (hash in this._savedChildren) {
  180. this.insertChild(this._savedChildren[hash], insertionIndex);
  181. return;
  182. }
  183. }
  184. this.insertChild(this._createChildNode(item), insertionIndex);
  185. }
  186. function insertShowMoreButton(from, to, insertionIndex)
  187. {
  188. var button = new WebInspector.ShowMoreDataGridNode(this._populateChildren.bind(this), from, to, this._dataGrid.defaultPopulateCount());
  189. this.insertChild(button, insertionIndex);
  190. }
  191. function childrenRetrieved(items)
  192. {
  193. var itemIndex = 0;
  194. var itemPosition = items.startPosition;
  195. var insertionIndex = 0;
  196. if (!this._retrievedChildrenRanges.length) {
  197. if (items.startPosition > 0) {
  198. this._retrievedChildrenRanges.push({from: 0, to: 0});
  199. insertShowMoreButton.call(this, 0, items.startPosition, insertionIndex++);
  200. }
  201. this._retrievedChildrenRanges.push({from: items.startPosition, to: items.endPosition});
  202. for (var i = 0, l = items.length; i < l; ++i)
  203. insertRetrievedChild.call(this, items[i], insertionIndex++);
  204. if (items.endPosition < items.totalLength)
  205. insertShowMoreButton.call(this, items.endPosition, items.totalLength, insertionIndex++);
  206. } else {
  207. var rangeIndex = 0;
  208. var found = false;
  209. var range;
  210. while (rangeIndex < this._retrievedChildrenRanges.length) {
  211. range = this._retrievedChildrenRanges[rangeIndex];
  212. if (range.to >= itemPosition) {
  213. found = true;
  214. break;
  215. }
  216. insertionIndex += range.to - range.from;
  217. // Skip the button if there is one.
  218. if (range.to < items.totalLength)
  219. insertionIndex += 1;
  220. ++rangeIndex;
  221. }
  222. if (!found || items.startPosition < range.from) {
  223. // Update previous button.
  224. this.children[insertionIndex - 1].setEndPosition(items.startPosition);
  225. insertShowMoreButton.call(this, items.startPosition, found ? range.from : items.totalLength, insertionIndex);
  226. range = {from: items.startPosition, to: items.startPosition};
  227. if (!found)
  228. rangeIndex = this._retrievedChildrenRanges.length;
  229. this._retrievedChildrenRanges.splice(rangeIndex, 0, range);
  230. } else {
  231. insertionIndex += itemPosition - range.from;
  232. }
  233. // At this point insertionIndex is always an index before button or between nodes.
  234. // Also it is always true here that range.from <= itemPosition <= range.to
  235. // Stretch the range right bound to include all new items.
  236. while (range.to < items.endPosition) {
  237. // Skip already added nodes.
  238. var skipCount = range.to - itemPosition;
  239. insertionIndex += skipCount;
  240. itemIndex += skipCount;
  241. itemPosition = range.to;
  242. // We're at the position before button: ...<?node>x<button>
  243. var nextRange = this._retrievedChildrenRanges[rangeIndex + 1];
  244. var newEndOfRange = nextRange ? nextRange.from : items.totalLength;
  245. if (newEndOfRange > items.endPosition)
  246. newEndOfRange = items.endPosition;
  247. while (itemPosition < newEndOfRange) {
  248. insertRetrievedChild.call(this, items[itemIndex++], insertionIndex++);
  249. ++itemPosition;
  250. }
  251. // Merge with the next range.
  252. if (nextRange && newEndOfRange === nextRange.from) {
  253. range.to = nextRange.to;
  254. // Remove "show next" button if there is one.
  255. this.removeChild(this.children[insertionIndex]);
  256. this._retrievedChildrenRanges.splice(rangeIndex + 1, 1);
  257. } else {
  258. range.to = newEndOfRange;
  259. // Remove or update next button.
  260. if (newEndOfRange === items.totalLength)
  261. this.removeChild(this.children[insertionIndex]);
  262. else
  263. this.children[insertionIndex].setStartPosition(items.endPosition);
  264. }
  265. }
  266. }
  267. // TODO: fix this.
  268. this._instanceCount += items.length;
  269. if (firstNotSerializedPosition < toPosition) {
  270. serializeNextChunk.call(this);
  271. return;
  272. }
  273. if (afterPopulate)
  274. afterPopulate();
  275. this.dispatchEventToListeners(WebInspector.HeapSnapshotGridNode.Events.PopulateComplete);
  276. }
  277. serializeNextChunk.call(this);
  278. },
  279. _saveChildren: function()
  280. {
  281. this._savedChildren = null;
  282. for (var i = 0, childrenCount = this.children.length; i < childrenCount; ++i) {
  283. var child = this.children[i];
  284. if (!child.expanded)
  285. continue;
  286. if (!this._savedChildren)
  287. this._savedChildren = {};
  288. this._savedChildren[this._childHashForNode(child)] = child;
  289. }
  290. },
  291. sort: function()
  292. {
  293. this._dataGrid.recursiveSortingEnter();
  294. function afterSort()
  295. {
  296. this._saveChildren();
  297. this.removeChildren();
  298. this._retrievedChildrenRanges = [];
  299. function afterPopulate()
  300. {
  301. for (var i = 0, l = this.children.length; i < l; ++i) {
  302. var child = this.children[i];
  303. if (child.expanded)
  304. child.sort();
  305. }
  306. this._dataGrid.recursiveSortingLeave();
  307. }
  308. var instanceCount = this._instanceCount;
  309. this._instanceCount = 0;
  310. this._populateChildren(0, instanceCount, afterPopulate.bind(this));
  311. }
  312. this._provider().sortAndRewind(this.comparator(), afterSort.bind(this));
  313. },
  314. __proto__: WebInspector.DataGridNode.prototype
  315. }
  316. /**
  317. * @constructor
  318. * @extends {WebInspector.HeapSnapshotGridNode}
  319. * @param {WebInspector.HeapSnapshotSortableDataGrid} tree
  320. */
  321. WebInspector.HeapSnapshotGenericObjectNode = function(tree, node)
  322. {
  323. this.snapshotNodeIndex = 0;
  324. WebInspector.HeapSnapshotGridNode.call(this, tree, false);
  325. // node is null for DataGrid root nodes.
  326. if (!node)
  327. return;
  328. this._name = node.name;
  329. this._displayName = node.displayName;
  330. this._type = node.type;
  331. this._distance = node.distance;
  332. this._shallowSize = node.selfSize;
  333. this._retainedSize = node.retainedSize;
  334. this.snapshotNodeId = node.id;
  335. this.snapshotNodeIndex = node.nodeIndex;
  336. if (this._type === "string")
  337. this._reachableFromWindow = true;
  338. else if (this._type === "object" && this._name.startsWith("Window")) {
  339. this._name = this.shortenWindowURL(this._name, false);
  340. this._reachableFromWindow = true;
  341. } else if (node.canBeQueried)
  342. this._reachableFromWindow = true;
  343. if (node.detachedDOMTreeNode)
  344. this.detachedDOMTreeNode = true;
  345. };
  346. WebInspector.HeapSnapshotGenericObjectNode.prototype = {
  347. createCell: function(columnIdentifier)
  348. {
  349. var cell = columnIdentifier !== "object" ? this._createValueCell(columnIdentifier) : this._createObjectCell();
  350. if (this._searchMatched)
  351. cell.addStyleClass("highlight");
  352. return cell;
  353. },
  354. _createObjectCell: function()
  355. {
  356. var cell = document.createElement("td");
  357. cell.className = "object-column";
  358. var div = document.createElement("div");
  359. div.className = "source-code event-properties";
  360. div.style.overflow = "visible";
  361. var data = this.data["object"];
  362. if (this._prefixObjectCell)
  363. this._prefixObjectCell(div, data);
  364. var valueSpan = document.createElement("span");
  365. valueSpan.className = "value console-formatted-" + data.valueStyle;
  366. valueSpan.textContent = data.value;
  367. div.appendChild(valueSpan);
  368. if (this.data.displayName) {
  369. var nameSpan = document.createElement("span");
  370. nameSpan.className = "name console-formatted-name";
  371. nameSpan.textContent = " " + this.data.displayName;
  372. div.appendChild(nameSpan);
  373. }
  374. var idSpan = document.createElement("span");
  375. idSpan.className = "console-formatted-id";
  376. idSpan.textContent = " @" + data["nodeId"];
  377. div.appendChild(idSpan);
  378. if (this._postfixObjectCell)
  379. this._postfixObjectCell(div, data);
  380. cell.appendChild(div);
  381. cell.addStyleClass("disclosure");
  382. if (this.depth)
  383. cell.style.setProperty("padding-left", (this.depth * this.dataGrid.indentWidth) + "px");
  384. cell.heapSnapshotNode = this;
  385. return cell;
  386. },
  387. get data()
  388. {
  389. var data = this._emptyData();
  390. var value = this._name;
  391. var valueStyle = "object";
  392. switch (this._type) {
  393. case "string":
  394. value = "\"" + value + "\"";
  395. valueStyle = "string";
  396. break;
  397. case "regexp":
  398. value = "/" + value + "/";
  399. valueStyle = "string";
  400. break;
  401. case "closure":
  402. value = "function" + (value ? " " : "") + value + "()";
  403. valueStyle = "function";
  404. break;
  405. case "number":
  406. valueStyle = "number";
  407. break;
  408. case "hidden":
  409. valueStyle = "null";
  410. break;
  411. case "array":
  412. if (!value)
  413. value = "[]";
  414. else
  415. value += "[]";
  416. break;
  417. };
  418. if (this._reachableFromWindow)
  419. valueStyle += " highlight";
  420. if (value === "Object")
  421. value = "";
  422. if (this.detachedDOMTreeNode)
  423. valueStyle += " detached-dom-tree-node";
  424. data["object"] = { valueStyle: valueStyle, value: value, nodeId: this.snapshotNodeId };
  425. data["displayName"] = this._displayName;
  426. data["distance"] = this._distance;
  427. data["shallowSize"] = Number.withThousandsSeparator(this._shallowSize);
  428. data["retainedSize"] = Number.withThousandsSeparator(this._retainedSize);
  429. data["shallowSize-percent"] = this._toPercentString(this._shallowSizePercent);
  430. data["retainedSize-percent"] = this._toPercentString(this._retainedSizePercent);
  431. return this._enhanceData ? this._enhanceData(data) : data;
  432. },
  433. queryObjectContent: function(callback, objectGroupName)
  434. {
  435. if (this._type === "string")
  436. callback(WebInspector.RemoteObject.fromPrimitiveValue(this._name));
  437. else {
  438. function formatResult(error, object)
  439. {
  440. if (!error && object.type)
  441. callback(WebInspector.RemoteObject.fromPayload(object), !!error);
  442. else
  443. callback(WebInspector.RemoteObject.fromPrimitiveValue(WebInspector.UIString("Not available")));
  444. }
  445. HeapProfilerAgent.getObjectByHeapObjectId(String(this.snapshotNodeId), objectGroupName, formatResult);
  446. }
  447. },
  448. get _retainedSizePercent()
  449. {
  450. return this._retainedSize / this.dataGrid.snapshot.totalSize * 100.0;
  451. },
  452. get _shallowSizePercent()
  453. {
  454. return this._shallowSize / this.dataGrid.snapshot.totalSize * 100.0;
  455. },
  456. updateHasChildren: function()
  457. {
  458. function isEmptyCallback(isEmpty)
  459. {
  460. this.hasChildren = !isEmpty;
  461. }
  462. this._provider().isEmpty(isEmptyCallback.bind(this));
  463. },
  464. shortenWindowURL: function(fullName, hasObjectId)
  465. {
  466. var startPos = fullName.indexOf("/");
  467. var endPos = hasObjectId ? fullName.indexOf("@") : fullName.length;
  468. if (startPos !== -1 && endPos !== -1) {
  469. var fullURL = fullName.substring(startPos + 1, endPos).trimLeft();
  470. var url = fullURL.trimURL();
  471. if (url.length > 40)
  472. url = url.centerEllipsizedToLength(40);
  473. return fullName.substr(0, startPos + 2) + url + fullName.substr(endPos);
  474. } else
  475. return fullName;
  476. },
  477. __proto__: WebInspector.HeapSnapshotGridNode.prototype
  478. }
  479. /**
  480. * @constructor
  481. * @extends {WebInspector.HeapSnapshotGenericObjectNode}
  482. * @param {WebInspector.HeapSnapshotSortableDataGrid} tree
  483. * @param {boolean} isFromBaseSnapshot
  484. */
  485. WebInspector.HeapSnapshotObjectNode = function(tree, isFromBaseSnapshot, edge, parentGridNode)
  486. {
  487. WebInspector.HeapSnapshotGenericObjectNode.call(this, tree, edge.node);
  488. this._referenceName = edge.name;
  489. this._referenceType = edge.type;
  490. this._distance = edge.distance;
  491. this.showRetainingEdges = tree.showRetainingEdges;
  492. this._isFromBaseSnapshot = isFromBaseSnapshot;
  493. this._parentGridNode = parentGridNode;
  494. this._cycledWithAncestorGridNode = this._findAncestorWithSameSnapshotNodeId();
  495. if (!this._cycledWithAncestorGridNode)
  496. this.updateHasChildren();
  497. }
  498. WebInspector.HeapSnapshotObjectNode.prototype = {
  499. /**
  500. * @return {WebInspector.HeapSnapshotProviderProxy}
  501. */
  502. createProvider: function()
  503. {
  504. var tree = this._dataGrid;
  505. var showHiddenData = WebInspector.settings.showHeapSnapshotObjectsHiddenProperties.get();
  506. var snapshot = this._isFromBaseSnapshot ? tree.baseSnapshot : tree.snapshot;
  507. if (this.showRetainingEdges)
  508. return snapshot.createRetainingEdgesProvider(this.snapshotNodeIndex, showHiddenData);
  509. else
  510. return snapshot.createEdgesProvider(this.snapshotNodeIndex, showHiddenData);
  511. },
  512. _findAncestorWithSameSnapshotNodeId: function()
  513. {
  514. var ancestor = this._parentGridNode;
  515. while (ancestor) {
  516. if (ancestor.snapshotNodeId === this.snapshotNodeId)
  517. return ancestor;
  518. ancestor = ancestor._parentGridNode;
  519. }
  520. return null;
  521. },
  522. _createChildNode: function(item)
  523. {
  524. return new WebInspector.HeapSnapshotObjectNode(this._dataGrid, this._isFromBaseSnapshot, item, this);
  525. },
  526. _childHashForEntity: function(edge)
  527. {
  528. var prefix = this.showRetainingEdges ? edge.node.id + "#" : "";
  529. return prefix + edge.type + "#" + edge.name;
  530. },
  531. _childHashForNode: function(childNode)
  532. {
  533. var prefix = this.showRetainingEdges ? childNode.snapshotNodeId + "#" : "";
  534. return prefix + childNode._referenceType + "#" + childNode._referenceName;
  535. },
  536. comparator: function()
  537. {
  538. var sortAscending = this._dataGrid.isSortOrderAscending();
  539. var sortColumnIdentifier = this._dataGrid.sortColumnIdentifier();
  540. var sortFields = {
  541. object: ["!edgeName", sortAscending, "retainedSize", false],
  542. count: ["!edgeName", true, "retainedSize", false],
  543. shallowSize: ["selfSize", sortAscending, "!edgeName", true],
  544. retainedSize: ["retainedSize", sortAscending, "!edgeName", true],
  545. distance: ["distance", sortAscending, "_name", true]
  546. }[sortColumnIdentifier] || ["!edgeName", true, "retainedSize", false];
  547. return WebInspector.HeapSnapshotFilteredOrderedIterator.prototype.createComparator(sortFields);
  548. },
  549. _emptyData: function()
  550. {
  551. return { count: "", addedCount: "", removedCount: "", countDelta: "", addedSize: "", removedSize: "", sizeDelta: "" };
  552. },
  553. _enhanceData: function(data)
  554. {
  555. var name = this._referenceName;
  556. if (name === "") name = "(empty)";
  557. var nameClass = "name";
  558. switch (this._referenceType) {
  559. case "context":
  560. nameClass = "console-formatted-number";
  561. break;
  562. case "internal":
  563. case "hidden":
  564. nameClass = "console-formatted-null";
  565. break;
  566. case "element":
  567. name = "[" + name + "]";
  568. break;
  569. }
  570. data["object"].nameClass = nameClass;
  571. data["object"].name = name;
  572. data["distance"] = this._distance;
  573. return data;
  574. },
  575. _prefixObjectCell: function(div, data)
  576. {
  577. if (this._cycledWithAncestorGridNode)
  578. div.className += " cycled-ancessor-node";
  579. var nameSpan = document.createElement("span");
  580. nameSpan.className = data.nameClass;
  581. nameSpan.textContent = data.name;
  582. div.appendChild(nameSpan);
  583. var separatorSpan = document.createElement("span");
  584. separatorSpan.className = "grayed";
  585. separatorSpan.textContent = this.showRetainingEdges ? " in " : " :: ";
  586. div.appendChild(separatorSpan);
  587. },
  588. __proto__: WebInspector.HeapSnapshotGenericObjectNode.prototype
  589. }
  590. /**
  591. * @constructor
  592. * @extends {WebInspector.HeapSnapshotGenericObjectNode}
  593. */
  594. WebInspector.HeapSnapshotInstanceNode = function(tree, baseSnapshot, snapshot, node)
  595. {
  596. WebInspector.HeapSnapshotGenericObjectNode.call(this, tree, node);
  597. this._baseSnapshotOrSnapshot = baseSnapshot || snapshot;
  598. this._isDeletedNode = !!baseSnapshot;
  599. this.updateHasChildren();
  600. };
  601. WebInspector.HeapSnapshotInstanceNode.prototype = {
  602. createProvider: function()
  603. {
  604. var showHiddenData = WebInspector.settings.showHeapSnapshotObjectsHiddenProperties.get();
  605. return this._baseSnapshotOrSnapshot.createEdgesProvider(
  606. this.snapshotNodeIndex,
  607. showHiddenData);
  608. },
  609. _createChildNode: function(item)
  610. {
  611. return new WebInspector.HeapSnapshotObjectNode(this._dataGrid, this._isDeletedNode, item, null);
  612. },
  613. _childHashForEntity: function(edge)
  614. {
  615. return edge.type + "#" + edge.name;
  616. },
  617. _childHashForNode: function(childNode)
  618. {
  619. return childNode._referenceType + "#" + childNode._referenceName;
  620. },
  621. comparator: function()
  622. {
  623. var sortAscending = this._dataGrid.isSortOrderAscending();
  624. var sortColumnIdentifier = this._dataGrid.sortColumnIdentifier();
  625. var sortFields = {
  626. object: ["!edgeName", sortAscending, "retainedSize", false],
  627. distance: ["distance", sortAscending, "retainedSize", false],
  628. count: ["!edgeName", true, "retainedSize", false],
  629. addedSize: ["selfSize", sortAscending, "!edgeName", true],
  630. removedSize: ["selfSize", sortAscending, "!edgeName", true],
  631. shallowSize: ["selfSize", sortAscending, "!edgeName", true],
  632. retainedSize: ["retainedSize", sortAscending, "!edgeName", true]
  633. }[sortColumnIdentifier] || ["!edgeName", true, "retainedSize", false];
  634. return WebInspector.HeapSnapshotFilteredOrderedIterator.prototype.createComparator(sortFields);
  635. },
  636. _emptyData: function()
  637. {
  638. return {count: "", countDelta: "", sizeDelta: ""};
  639. },
  640. _enhanceData: function(data)
  641. {
  642. if (this._isDeletedNode) {
  643. data["addedCount"] = "";
  644. data["addedSize"] = "";
  645. data["removedCount"] = "\u2022";
  646. data["removedSize"] = Number.withThousandsSeparator(this._shallowSize);
  647. } else {
  648. data["addedCount"] = "\u2022";
  649. data["addedSize"] = Number.withThousandsSeparator(this._shallowSize);
  650. data["removedCount"] = "";
  651. data["removedSize"] = "";
  652. }
  653. return data;
  654. },
  655. get isDeletedNode()
  656. {
  657. return this._isDeletedNode;
  658. },
  659. __proto__: WebInspector.HeapSnapshotGenericObjectNode.prototype
  660. }
  661. /**
  662. * @constructor
  663. * @extends {WebInspector.HeapSnapshotGridNode}
  664. */
  665. WebInspector.HeapSnapshotConstructorNode = function(tree, className, aggregate, aggregatesKey)
  666. {
  667. WebInspector.HeapSnapshotGridNode.call(this, tree, aggregate.count > 0);
  668. this._name = className;
  669. this._aggregatesKey = aggregatesKey;
  670. this._distance = aggregate.distance;
  671. this._count = aggregate.count;
  672. this._shallowSize = aggregate.self;
  673. this._retainedSize = aggregate.maxRet;
  674. }
  675. WebInspector.HeapSnapshotConstructorNode.prototype = {
  676. /**
  677. * @override
  678. * @return {WebInspector.HeapSnapshotProviderProxy}
  679. */
  680. createProvider: function()
  681. {
  682. return this._dataGrid.snapshot.createNodesProviderForClass(this._name, this._aggregatesKey)
  683. },
  684. /**
  685. * @param {number} snapshotObjectId
  686. */
  687. revealNodeBySnapshotObjectId: function(snapshotObjectId)
  688. {
  689. function didExpand()
  690. {
  691. this._provider().nodePosition(snapshotObjectId, didGetNodePosition.bind(this));
  692. }
  693. function didGetNodePosition(nodePosition)
  694. {
  695. if (nodePosition === -1)
  696. this.collapse();
  697. else
  698. this._populateChildren(nodePosition, null, didPopulateChildren.bind(this, nodePosition));
  699. }
  700. function didPopulateChildren(nodePosition)
  701. {
  702. var indexOfFirsChildInRange = 0;
  703. for (var i = 0; i < this._retrievedChildrenRanges.length; i++) {
  704. var range = this._retrievedChildrenRanges[i];
  705. if (range.from <= nodePosition && nodePosition < range.to) {
  706. var childIndex = indexOfFirsChildInRange + nodePosition - range.from;
  707. var instanceNode = this.children[childIndex];
  708. this._dataGrid.highlightNode(instanceNode);
  709. return;
  710. }
  711. indexOfFirsChildInRange += range.to - range.from + 1;
  712. }
  713. }
  714. this.expandWithoutPopulate(didExpand.bind(this));
  715. },
  716. createCell: function(columnIdentifier)
  717. {
  718. var cell = columnIdentifier !== "object" ? this._createValueCell(columnIdentifier) : WebInspector.HeapSnapshotGridNode.prototype.createCell.call(this, columnIdentifier);
  719. if (this._searchMatched)
  720. cell.addStyleClass("highlight");
  721. return cell;
  722. },
  723. _createChildNode: function(item)
  724. {
  725. return new WebInspector.HeapSnapshotInstanceNode(this._dataGrid, null, this._dataGrid.snapshot, item);
  726. },
  727. comparator: function()
  728. {
  729. var sortAscending = this._dataGrid.isSortOrderAscending();
  730. var sortColumnIdentifier = this._dataGrid.sortColumnIdentifier();
  731. var sortFields = {
  732. object: ["id", sortAscending, "retainedSize", false],
  733. distance: ["distance", true, "retainedSize", false],
  734. count: ["id", true, "retainedSize", false],
  735. shallowSize: ["selfSize", sortAscending, "id", true],
  736. retainedSize: ["retainedSize", sortAscending, "id", true]
  737. }[sortColumnIdentifier];
  738. return WebInspector.HeapSnapshotFilteredOrderedIterator.prototype.createComparator(sortFields);
  739. },
  740. _childHashForEntity: function(node)
  741. {
  742. return node.id;
  743. },
  744. _childHashForNode: function(childNode)
  745. {
  746. return childNode.snapshotNodeId;
  747. },
  748. get data()
  749. {
  750. var data = { object: this._name };
  751. data["count"] = Number.withThousandsSeparator(this._count);
  752. data["distance"] = this._distance;
  753. data["shallowSize"] = Number.withThousandsSeparator(this._shallowSize);
  754. data["retainedSize"] = Number.withThousandsSeparator(this._retainedSize);
  755. data["count-percent"] = this._toPercentString(this._countPercent);
  756. data["shallowSize-percent"] = this._toPercentString(this._shallowSizePercent);
  757. data["retainedSize-percent"] = this._toPercentString(this._retainedSizePercent);
  758. return data;
  759. },
  760. get _countPercent()
  761. {
  762. return this._count / this.dataGrid.snapshot.nodeCount * 100.0;
  763. },
  764. get _retainedSizePercent()
  765. {
  766. return this._retainedSize / this.dataGrid.snapshot.totalSize * 100.0;
  767. },
  768. get _shallowSizePercent()
  769. {
  770. return this._shallowSize / this.dataGrid.snapshot.totalSize * 100.0;
  771. },
  772. __proto__: WebInspector.HeapSnapshotGridNode.prototype
  773. }
  774. /**
  775. * @constructor
  776. * @extends {WebInspector.HeapSnapshotProviderProxy}
  777. * @param {WebInspector.HeapSnapshotProviderProxy} addedNodesProvider
  778. * @param {WebInspector.HeapSnapshotProviderProxy} deletedNodesProvider
  779. */
  780. WebInspector.HeapSnapshotDiffNodesProvider = function(addedNodesProvider, deletedNodesProvider, addedCount, removedCount)
  781. {
  782. this._addedNodesProvider = addedNodesProvider;
  783. this._deletedNodesProvider = deletedNodesProvider;
  784. this._addedCount = addedCount;
  785. this._removedCount = removedCount;
  786. }
  787. WebInspector.HeapSnapshotDiffNodesProvider.prototype = {
  788. dispose: function()
  789. {
  790. this._addedNodesProvider.dispose();
  791. this._deletedNodesProvider.dispose();
  792. },
  793. isEmpty: function(callback)
  794. {
  795. callback(false);
  796. },
  797. serializeItemsRange: function(beginPosition, endPosition, callback)
  798. {
  799. function didReceiveAllItems(items)
  800. {
  801. items.totalLength = this._addedCount + this._removedCount;
  802. callback(items);
  803. }
  804. function didReceiveDeletedItems(addedItems, items)
  805. {
  806. if (!addedItems.length)
  807. addedItems.startPosition = this._addedCount + items.startPosition;
  808. for (var i = 0; i < items.length; i++) {
  809. items[i].isAddedNotRemoved = false;
  810. addedItems.push(items[i]);
  811. }
  812. addedItems.endPosition = this._addedCount + items.endPosition;
  813. didReceiveAllItems.call(this, addedItems);
  814. }
  815. function didReceiveAddedItems(items)
  816. {
  817. for (var i = 0; i < items.length; i++)
  818. items[i].isAddedNotRemoved = true;
  819. if (items.endPosition < endPosition)
  820. return this._deletedNodesProvider.serializeItemsRange(0, endPosition - items.endPosition, didReceiveDeletedItems.bind(this, items));
  821. items.totalLength = this._addedCount + this._removedCount;
  822. didReceiveAllItems.call(this, items);
  823. }
  824. if (beginPosition < this._addedCount)
  825. this._addedNodesProvider.serializeItemsRange(beginPosition, endPosition, didReceiveAddedItems.bind(this));
  826. else
  827. this._deletedNodesProvider.serializeItemsRange(beginPosition - this._addedCount, endPosition - this._addedCount, didReceiveDeletedItems.bind(this, []));
  828. },
  829. sortAndRewind: function(comparator, callback)
  830. {
  831. function afterSort()
  832. {
  833. this._deletedNodesProvider.sortAndRewind(comparator, callback);
  834. }
  835. this._addedNodesProvider.sortAndRewind(comparator, afterSort.bind(this));
  836. }
  837. };
  838. /**
  839. * @constructor
  840. * @extends {WebInspector.HeapSnapshotGridNode}
  841. */
  842. WebInspector.HeapSnapshotDiffNode = function(tree, className, diffForClass)
  843. {
  844. WebInspector.HeapSnapshotGridNode.call(this, tree, true);
  845. this._name = className;
  846. this._addedCount = diffForClass.addedCount;
  847. this._removedCount = diffForClass.removedCount;
  848. this._countDelta = diffForClass.countDelta;
  849. this._addedSize = diffForClass.addedSize;
  850. this._removedSize = diffForClass.removedSize;
  851. this._sizeDelta = diffForClass.sizeDelta;
  852. this._deletedIndexes = diffForClass.deletedIndexes;
  853. }
  854. WebInspector.HeapSnapshotDiffNode.prototype = {
  855. /**
  856. * @override
  857. * @return {WebInspector.HeapSnapshotDiffNodesProvider}
  858. */
  859. createProvider: function()
  860. {
  861. var tree = this._dataGrid;
  862. return new WebInspector.HeapSnapshotDiffNodesProvider(
  863. tree.snapshot.createAddedNodesProvider(tree.baseSnapshot.uid, this._name),
  864. tree.baseSnapshot.createDeletedNodesProvider(this._deletedIndexes),
  865. this._addedCount,
  866. this._removedCount);
  867. },
  868. _createChildNode: function(item)
  869. {
  870. if (item.isAddedNotRemoved)
  871. return new WebInspector.HeapSnapshotInstanceNode(this._dataGrid, null, this._dataGrid.snapshot, item);
  872. else
  873. return new WebInspector.HeapSnapshotInstanceNode(this._dataGrid, this._dataGrid.baseSnapshot, null, item);
  874. },
  875. _childHashForEntity: function(node)
  876. {
  877. return node.id;
  878. },
  879. _childHashForNode: function(childNode)
  880. {
  881. return childNode.snapshotNodeId;
  882. },
  883. comparator: function()
  884. {
  885. var sortAscending = this._dataGrid.isSortOrderAscending();
  886. var sortColumnIdentifier = this._dataGrid.sortColumnIdentifier();
  887. var sortFields = {
  888. object: ["id", sortAscending, "selfSize", false],
  889. addedCount: ["selfSize", sortAscending, "id", true],
  890. removedCount: ["selfSize", sortAscending, "id", true],
  891. countDelta: ["selfSize", sortAscending, "id", true],
  892. addedSize: ["selfSize", sortAscending, "id", true],
  893. removedSize: ["selfSize", sortAscending, "id", true],
  894. sizeDelta: ["selfSize", sortAscending, "id", true]
  895. }[sortColumnIdentifier];
  896. return WebInspector.HeapSnapshotFilteredOrderedIterator.prototype.createComparator(sortFields);
  897. },
  898. _signForDelta: function(delta)
  899. {
  900. if (delta === 0)
  901. return "";
  902. if (delta > 0)
  903. return "+";
  904. else
  905. return "\u2212"; // Math minus sign, same width as plus.
  906. },
  907. get data()
  908. {
  909. var data = {object: this._name};
  910. data["addedCount"] = Number.withThousandsSeparator(this._addedCount);
  911. data["removedCount"] = Number.withThousandsSeparator(this._removedCount);
  912. data["countDelta"] = this._signForDelta(this._countDelta) + Number.withThousandsSeparator(Math.abs(this._countDelta));
  913. data["addedSize"] = Number.withThousandsSeparator(this._addedSize);
  914. data["removedSize"] = Number.withThousandsSeparator(this._removedSize);
  915. data["sizeDelta"] = this._signForDelta(this._sizeDelta) + Number.withThousandsSeparator(Math.abs(this._sizeDelta));
  916. return data;
  917. },
  918. __proto__: WebInspector.HeapSnapshotGridNode.prototype
  919. }
  920. /**
  921. * @constructor
  922. * @extends {WebInspector.HeapSnapshotGenericObjectNode}
  923. */
  924. WebInspector.HeapSnapshotDominatorObjectNode = function(tree, node)
  925. {
  926. WebInspector.HeapSnapshotGenericObjectNode.call(this, tree, node);
  927. this.updateHasChildren();
  928. };
  929. WebInspector.HeapSnapshotDominatorObjectNode.prototype = {
  930. /**
  931. * @override
  932. * @return {WebInspector.HeapSnapshotProviderProxy}
  933. */
  934. createProvider: function()
  935. {
  936. return this._dataGrid.snapshot.createNodesProviderForDominator(this.snapshotNodeIndex);
  937. },
  938. /**
  939. * @param {number} snapshotObjectId
  940. * @param {function(?WebInspector.HeapSnapshotDominatorObjectNode)} callback
  941. */
  942. retrieveChildBySnapshotObjectId: function(snapshotObjectId, callback)
  943. {
  944. function didExpand()
  945. {
  946. this._provider().nodePosition(snapshotObjectId, didGetNodePosition.bind(this));
  947. }
  948. function didGetNodePosition(nodePosition)
  949. {
  950. if (nodePosition === -1) {
  951. this.collapse();
  952. callback(null);
  953. } else
  954. this._populateChildren(nodePosition, null, didPopulateChildren.bind(this, nodePosition));
  955. }
  956. function didPopulateChildren(nodePosition)
  957. {
  958. var child = this.childForPosition(nodePosition);
  959. callback(child);
  960. }
  961. // Make sure hasChildren flag is updated before expanding this node as updateHasChildren response
  962. // may not have been received yet.
  963. this.hasChildren = true;
  964. this.expandWithoutPopulate(didExpand.bind(this));
  965. },
  966. _createChildNode: function(item)
  967. {
  968. return new WebInspector.HeapSnapshotDominatorObjectNode(this._dataGrid, item);
  969. },
  970. _childHashForEntity: function(node)
  971. {
  972. return node.id;
  973. },
  974. _childHashForNode: function(childNode)
  975. {
  976. return childNode.snapshotNodeId;
  977. },
  978. comparator: function()
  979. {
  980. var sortAscending = this._dataGrid.isSortOrderAscending();
  981. var sortColumnIdentifier = this._dataGrid.sortColumnIdentifier();
  982. var sortFields = {
  983. object: ["id", sortAscending, "retainedSize", false],
  984. shallowSize: ["selfSize", sortAscending, "id", true],
  985. retainedSize: ["retainedSize", sortAscending, "id", true]
  986. }[sortColumnIdentifier];
  987. return WebInspector.HeapSnapshotFilteredOrderedIterator.prototype.createComparator(sortFields);
  988. },
  989. _emptyData: function()
  990. {
  991. return {};
  992. },
  993. __proto__: WebInspector.HeapSnapshotGenericObjectNode.prototype
  994. }