HeapSnapshotProxy.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 copyrightdd
  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.Object}
  33. */
  34. WebInspector.HeapSnapshotWorkerWrapper = function()
  35. {
  36. }
  37. WebInspector.HeapSnapshotWorkerWrapper.prototype = {
  38. postMessage: function(message)
  39. {
  40. },
  41. terminate: function()
  42. {
  43. },
  44. __proto__: WebInspector.Object.prototype
  45. }
  46. /**
  47. * @constructor
  48. * @extends {WebInspector.HeapSnapshotWorkerWrapper}
  49. */
  50. WebInspector.HeapSnapshotRealWorker = function()
  51. {
  52. this._worker = new Worker("HeapSnapshotWorker.js");
  53. this._worker.addEventListener("message", this._messageReceived.bind(this), false);
  54. }
  55. WebInspector.HeapSnapshotRealWorker.prototype = {
  56. _messageReceived: function(event)
  57. {
  58. var message = event.data;
  59. if ("callId" in message)
  60. this.dispatchEventToListeners("message", message);
  61. else {
  62. if (message.object !== "console") {
  63. console.log(WebInspector.UIString("Worker asks to call a method '%s' on an unsupported object '%s'.", message.method, message.object));
  64. return;
  65. }
  66. if (message.method !== "log" && message.method !== "info" && message.method !== "error") {
  67. console.log(WebInspector.UIString("Worker asks to call an unsupported method '%s' on the console object.", message.method));
  68. return;
  69. }
  70. console[message.method].apply(window[message.object], message.arguments);
  71. }
  72. },
  73. postMessage: function(message)
  74. {
  75. this._worker.postMessage(message);
  76. },
  77. terminate: function()
  78. {
  79. this._worker.terminate();
  80. },
  81. __proto__: WebInspector.HeapSnapshotWorkerWrapper.prototype
  82. }
  83. /**
  84. * @constructor
  85. */
  86. WebInspector.AsyncTaskQueue = function()
  87. {
  88. this._queue = [];
  89. this._isTimerSheduled = false;
  90. }
  91. WebInspector.AsyncTaskQueue.prototype = {
  92. /**
  93. * @param {function()} task
  94. */
  95. addTask: function(task)
  96. {
  97. this._queue.push(task);
  98. this._scheduleTimer();
  99. },
  100. _onTimeout: function()
  101. {
  102. this._isTimerSheduled = false;
  103. var queue = this._queue;
  104. this._queue = [];
  105. for (var i = 0; i < queue.length; i++) {
  106. try {
  107. queue[i]();
  108. } catch (e) {
  109. console.error("Exception while running task: " + e.stack);
  110. }
  111. }
  112. this._scheduleTimer();
  113. },
  114. _scheduleTimer: function()
  115. {
  116. if (this._queue.length && !this._isTimerSheduled) {
  117. setTimeout(this._onTimeout.bind(this), 0);
  118. this._isTimerSheduled = true;
  119. }
  120. }
  121. }
  122. /**
  123. * @constructor
  124. * @extends {WebInspector.HeapSnapshotWorkerWrapper}
  125. */
  126. WebInspector.HeapSnapshotFakeWorker = function()
  127. {
  128. this._dispatcher = new WebInspector.HeapSnapshotWorkerDispatcher(window, this._postMessageFromWorker.bind(this));
  129. this._asyncTaskQueue = new WebInspector.AsyncTaskQueue();
  130. }
  131. WebInspector.HeapSnapshotFakeWorker.prototype = {
  132. postMessage: function(message)
  133. {
  134. function dispatch()
  135. {
  136. if (this._dispatcher)
  137. this._dispatcher.dispatchMessage({data: message});
  138. }
  139. this._asyncTaskQueue.addTask(dispatch.bind(this));
  140. },
  141. terminate: function()
  142. {
  143. this._dispatcher = null;
  144. },
  145. _postMessageFromWorker: function(message)
  146. {
  147. function send()
  148. {
  149. this.dispatchEventToListeners("message", message);
  150. }
  151. this._asyncTaskQueue.addTask(send.bind(this));
  152. },
  153. __proto__: WebInspector.HeapSnapshotWorkerWrapper.prototype
  154. }
  155. /**
  156. * @constructor
  157. * @extends {WebInspector.Object}
  158. */
  159. WebInspector.HeapSnapshotWorker = function()
  160. {
  161. this._nextObjectId = 1;
  162. this._nextCallId = 1;
  163. this._callbacks = [];
  164. this._previousCallbacks = [];
  165. // There is no support for workers in Chromium DRT.
  166. this._worker = typeof InspectorTest === "undefined" ? new WebInspector.HeapSnapshotRealWorker() : new WebInspector.HeapSnapshotFakeWorker();
  167. this._worker.addEventListener("message", this._messageReceived, this);
  168. }
  169. WebInspector.HeapSnapshotWorker.prototype = {
  170. createLoader: function(snapshotConstructorName, proxyConstructor)
  171. {
  172. var objectId = this._nextObjectId++;
  173. var proxy = new WebInspector.HeapSnapshotLoaderProxy(this, objectId, snapshotConstructorName, proxyConstructor);
  174. this._postMessage({callId: this._nextCallId++, disposition: "create", objectId: objectId, methodName: "WebInspector.HeapSnapshotLoader"});
  175. return proxy;
  176. },
  177. dispose: function()
  178. {
  179. this._worker.terminate();
  180. if (this._interval)
  181. clearInterval(this._interval);
  182. },
  183. disposeObject: function(objectId)
  184. {
  185. this._postMessage({callId: this._nextCallId++, disposition: "dispose", objectId: objectId});
  186. },
  187. callGetter: function(callback, objectId, getterName)
  188. {
  189. var callId = this._nextCallId++;
  190. this._callbacks[callId] = callback;
  191. this._postMessage({callId: callId, disposition: "getter", objectId: objectId, methodName: getterName});
  192. },
  193. callFactoryMethod: function(callback, objectId, methodName, proxyConstructor)
  194. {
  195. var callId = this._nextCallId++;
  196. var methodArguments = Array.prototype.slice.call(arguments, 4);
  197. var newObjectId = this._nextObjectId++;
  198. if (callback) {
  199. function wrapCallback(remoteResult)
  200. {
  201. callback(remoteResult ? new proxyConstructor(this, newObjectId) : null);
  202. }
  203. this._callbacks[callId] = wrapCallback.bind(this);
  204. this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
  205. return null;
  206. } else {
  207. this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
  208. return new proxyConstructor(this, newObjectId);
  209. }
  210. },
  211. callMethod: function(callback, objectId, methodName)
  212. {
  213. var callId = this._nextCallId++;
  214. var methodArguments = Array.prototype.slice.call(arguments, 3);
  215. if (callback)
  216. this._callbacks[callId] = callback;
  217. this._postMessage({callId: callId, disposition: "method", objectId: objectId, methodName: methodName, methodArguments: methodArguments});
  218. },
  219. startCheckingForLongRunningCalls: function()
  220. {
  221. if (this._interval)
  222. return;
  223. this._checkLongRunningCalls();
  224. this._interval = setInterval(this._checkLongRunningCalls.bind(this), 300);
  225. },
  226. _checkLongRunningCalls: function()
  227. {
  228. for (var callId in this._previousCallbacks)
  229. if (!(callId in this._callbacks))
  230. delete this._previousCallbacks[callId];
  231. var hasLongRunningCalls = false;
  232. for (callId in this._previousCallbacks) {
  233. hasLongRunningCalls = true;
  234. break;
  235. }
  236. this.dispatchEventToListeners("wait", hasLongRunningCalls);
  237. for (callId in this._callbacks)
  238. this._previousCallbacks[callId] = true;
  239. },
  240. _findFunction: function(name)
  241. {
  242. var path = name.split(".");
  243. var result = window;
  244. for (var i = 0; i < path.length; ++i)
  245. result = result[path[i]];
  246. return result;
  247. },
  248. _messageReceived: function(event)
  249. {
  250. var data = event.data;
  251. if (event.data.error) {
  252. if (event.data.errorMethodName)
  253. WebInspector.log(WebInspector.UIString("An error happened when a call for method '%s' was requested", event.data.errorMethodName));
  254. WebInspector.log(event.data.errorCallStack);
  255. delete this._callbacks[data.callId];
  256. return;
  257. }
  258. if (!this._callbacks[data.callId])
  259. return;
  260. var callback = this._callbacks[data.callId];
  261. delete this._callbacks[data.callId];
  262. callback(data.result);
  263. },
  264. _postMessage: function(message)
  265. {
  266. this._worker.postMessage(message);
  267. },
  268. __proto__: WebInspector.Object.prototype
  269. }
  270. /**
  271. * @constructor
  272. */
  273. WebInspector.HeapSnapshotProxyObject = function(worker, objectId)
  274. {
  275. this._worker = worker;
  276. this._objectId = objectId;
  277. }
  278. WebInspector.HeapSnapshotProxyObject.prototype = {
  279. _callWorker: function(workerMethodName, args)
  280. {
  281. args.splice(1, 0, this._objectId);
  282. return this._worker[workerMethodName].apply(this._worker, args);
  283. },
  284. dispose: function()
  285. {
  286. this._worker.disposeObject(this._objectId);
  287. },
  288. disposeWorker: function()
  289. {
  290. this._worker.dispose();
  291. },
  292. /**
  293. * @param {...*} var_args
  294. */
  295. callFactoryMethod: function(callback, methodName, proxyConstructor, var_args)
  296. {
  297. return this._callWorker("callFactoryMethod", Array.prototype.slice.call(arguments, 0));
  298. },
  299. callGetter: function(callback, getterName)
  300. {
  301. return this._callWorker("callGetter", Array.prototype.slice.call(arguments, 0));
  302. },
  303. /**
  304. * @param {...*} var_args
  305. */
  306. callMethod: function(callback, methodName, var_args)
  307. {
  308. return this._callWorker("callMethod", Array.prototype.slice.call(arguments, 0));
  309. },
  310. get worker() {
  311. return this._worker;
  312. }
  313. };
  314. /**
  315. * @constructor
  316. * @extends {WebInspector.HeapSnapshotProxyObject}
  317. * @implements {WebInspector.OutputStream}
  318. */
  319. WebInspector.HeapSnapshotLoaderProxy = function(worker, objectId, snapshotConstructorName, proxyConstructor)
  320. {
  321. WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
  322. this._snapshotConstructorName = snapshotConstructorName;
  323. this._proxyConstructor = proxyConstructor;
  324. this._pendingSnapshotConsumers = [];
  325. }
  326. WebInspector.HeapSnapshotLoaderProxy.prototype = {
  327. /**
  328. * @param {function(WebInspector.HeapSnapshotProxy)} callback
  329. */
  330. addConsumer: function(callback)
  331. {
  332. this._pendingSnapshotConsumers.push(callback);
  333. },
  334. /**
  335. * @param {string} chunk
  336. * @param {function(WebInspector.OutputStream)=} callback
  337. */
  338. write: function(chunk, callback)
  339. {
  340. this.callMethod(callback, "write", chunk);
  341. },
  342. close: function()
  343. {
  344. function buildSnapshot()
  345. {
  346. this.callFactoryMethod(updateStaticData.bind(this), "buildSnapshot", this._proxyConstructor, this._snapshotConstructorName);
  347. }
  348. function updateStaticData(snapshotProxy)
  349. {
  350. this.dispose();
  351. snapshotProxy.updateStaticData(notifyPendingConsumers.bind(this));
  352. }
  353. function notifyPendingConsumers(snapshotProxy)
  354. {
  355. for (var i = 0; i < this._pendingSnapshotConsumers.length; ++i)
  356. this._pendingSnapshotConsumers[i](snapshotProxy);
  357. this._pendingSnapshotConsumers = [];
  358. }
  359. this.callMethod(buildSnapshot.bind(this), "close");
  360. },
  361. __proto__: WebInspector.HeapSnapshotProxyObject.prototype
  362. }
  363. /**
  364. * @constructor
  365. * @extends {WebInspector.HeapSnapshotProxyObject}
  366. */
  367. WebInspector.HeapSnapshotProxy = function(worker, objectId)
  368. {
  369. WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
  370. }
  371. WebInspector.HeapSnapshotProxy.prototype = {
  372. aggregates: function(sortedIndexes, key, filter, callback)
  373. {
  374. this.callMethod(callback, "aggregates", sortedIndexes, key, filter);
  375. },
  376. aggregatesForDiff: function(callback)
  377. {
  378. this.callMethod(callback, "aggregatesForDiff");
  379. },
  380. calculateSnapshotDiff: function(baseSnapshotId, baseSnapshotAggregates, callback)
  381. {
  382. this.callMethod(callback, "calculateSnapshotDiff", baseSnapshotId, baseSnapshotAggregates);
  383. },
  384. nodeClassName: function(snapshotObjectId, callback)
  385. {
  386. this.callMethod(callback, "nodeClassName", snapshotObjectId);
  387. },
  388. dominatorIdsForNode: function(nodeIndex, callback)
  389. {
  390. this.callMethod(callback, "dominatorIdsForNode", nodeIndex);
  391. },
  392. createEdgesProvider: function(nodeIndex, showHiddenData)
  393. {
  394. return this.callFactoryMethod(null, "createEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex, showHiddenData);
  395. },
  396. createRetainingEdgesProvider: function(nodeIndex, showHiddenData)
  397. {
  398. return this.callFactoryMethod(null, "createRetainingEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex, showHiddenData);
  399. },
  400. createAddedNodesProvider: function(baseSnapshotId, className)
  401. {
  402. return this.callFactoryMethod(null, "createAddedNodesProvider", WebInspector.HeapSnapshotProviderProxy, baseSnapshotId, className);
  403. },
  404. createDeletedNodesProvider: function(nodeIndexes)
  405. {
  406. return this.callFactoryMethod(null, "createDeletedNodesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndexes);
  407. },
  408. createNodesProvider: function(filter)
  409. {
  410. return this.callFactoryMethod(null, "createNodesProvider", WebInspector.HeapSnapshotProviderProxy, filter);
  411. },
  412. createNodesProviderForClass: function(className, aggregatesKey)
  413. {
  414. return this.callFactoryMethod(null, "createNodesProviderForClass", WebInspector.HeapSnapshotProviderProxy, className, aggregatesKey);
  415. },
  416. createNodesProviderForDominator: function(nodeIndex)
  417. {
  418. return this.callFactoryMethod(null, "createNodesProviderForDominator", WebInspector.HeapSnapshotProviderProxy, nodeIndex);
  419. },
  420. dispose: function()
  421. {
  422. this.disposeWorker();
  423. },
  424. get nodeCount()
  425. {
  426. return this._staticData.nodeCount;
  427. },
  428. get rootNodeIndex()
  429. {
  430. return this._staticData.rootNodeIndex;
  431. },
  432. updateStaticData: function(callback)
  433. {
  434. function dataReceived(staticData)
  435. {
  436. this._staticData = staticData;
  437. callback(this);
  438. }
  439. this.callMethod(dataReceived.bind(this), "updateStaticData");
  440. },
  441. get totalSize()
  442. {
  443. return this._staticData.totalSize;
  444. },
  445. get uid()
  446. {
  447. return this._staticData.uid;
  448. },
  449. __proto__: WebInspector.HeapSnapshotProxyObject.prototype
  450. }
  451. /**
  452. * @constructor
  453. * @extends {WebInspector.HeapSnapshotProxy}
  454. */
  455. WebInspector.NativeHeapSnapshotProxy = function(worker, objectId)
  456. {
  457. WebInspector.HeapSnapshotProxy.call(this, worker, objectId);
  458. }
  459. WebInspector.NativeHeapSnapshotProxy.prototype = {
  460. images: function(callback)
  461. {
  462. this.callMethod(callback, "images");
  463. },
  464. __proto__: WebInspector.HeapSnapshotProxy.prototype
  465. }
  466. /**
  467. * @constructor
  468. * @extends {WebInspector.HeapSnapshotProxyObject}
  469. */
  470. WebInspector.HeapSnapshotProviderProxy = function(worker, objectId)
  471. {
  472. WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
  473. }
  474. WebInspector.HeapSnapshotProviderProxy.prototype = {
  475. nodePosition: function(snapshotObjectId, callback)
  476. {
  477. this.callMethod(callback, "nodePosition", snapshotObjectId);
  478. },
  479. isEmpty: function(callback)
  480. {
  481. this.callMethod(callback, "isEmpty");
  482. },
  483. serializeItemsRange: function(startPosition, endPosition, callback)
  484. {
  485. this.callMethod(callback, "serializeItemsRange", startPosition, endPosition);
  486. },
  487. sortAndRewind: function(comparator, callback)
  488. {
  489. this.callMethod(callback, "sortAndRewind", comparator);
  490. },
  491. __proto__: WebInspector.HeapSnapshotProxyObject.prototype
  492. }