HeapSnapshotProxy.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. this.dispatchEventToListeners("message", message);
  60. },
  61. postMessage: function(message)
  62. {
  63. this._worker.postMessage(message);
  64. },
  65. terminate: function()
  66. {
  67. this._worker.terminate();
  68. },
  69. __proto__: WebInspector.HeapSnapshotWorkerWrapper.prototype
  70. }
  71. /**
  72. * @constructor
  73. */
  74. WebInspector.AsyncTaskQueue = function()
  75. {
  76. this._queue = [];
  77. this._isTimerSheduled = false;
  78. }
  79. WebInspector.AsyncTaskQueue.prototype = {
  80. /**
  81. * @param {function()} task
  82. */
  83. addTask: function(task)
  84. {
  85. this._queue.push(task);
  86. this._scheduleTimer();
  87. },
  88. _onTimeout: function()
  89. {
  90. this._isTimerSheduled = false;
  91. var queue = this._queue;
  92. this._queue = [];
  93. for (var i = 0; i < queue.length; i++) {
  94. try {
  95. queue[i]();
  96. } catch (e) {
  97. console.error("Exception while running task: " + e.stack);
  98. }
  99. }
  100. this._scheduleTimer();
  101. },
  102. _scheduleTimer: function()
  103. {
  104. if (this._queue.length && !this._isTimerSheduled) {
  105. setTimeout(this._onTimeout.bind(this), 0);
  106. this._isTimerSheduled = true;
  107. }
  108. }
  109. }
  110. /**
  111. * @constructor
  112. * @extends {WebInspector.HeapSnapshotWorkerWrapper}
  113. */
  114. WebInspector.HeapSnapshotFakeWorker = function()
  115. {
  116. this._dispatcher = new WebInspector.HeapSnapshotWorkerDispatcher(window, this._postMessageFromWorker.bind(this));
  117. this._asyncTaskQueue = new WebInspector.AsyncTaskQueue();
  118. }
  119. WebInspector.HeapSnapshotFakeWorker.prototype = {
  120. postMessage: function(message)
  121. {
  122. function dispatch()
  123. {
  124. if (this._dispatcher)
  125. this._dispatcher.dispatchMessage({data: message});
  126. }
  127. this._asyncTaskQueue.addTask(dispatch.bind(this));
  128. },
  129. terminate: function()
  130. {
  131. this._dispatcher = null;
  132. },
  133. _postMessageFromWorker: function(message)
  134. {
  135. function send()
  136. {
  137. this.dispatchEventToListeners("message", message);
  138. }
  139. this._asyncTaskQueue.addTask(send.bind(this));
  140. },
  141. __proto__: WebInspector.HeapSnapshotWorkerWrapper.prototype
  142. }
  143. /**
  144. * @constructor
  145. * @param {function(string, *)} eventHandler
  146. * @extends {WebInspector.Object}
  147. */
  148. WebInspector.HeapSnapshotWorkerProxy = function(eventHandler)
  149. {
  150. this._eventHandler = eventHandler;
  151. this._nextObjectId = 1;
  152. this._nextCallId = 1;
  153. this._callbacks = [];
  154. this._previousCallbacks = [];
  155. // There is no support for workers in Chromium DRT.
  156. this._worker = typeof InspectorTest === "undefined" ? new WebInspector.HeapSnapshotRealWorker() : new WebInspector.HeapSnapshotFakeWorker();
  157. this._worker.addEventListener("message", this._messageReceived, this);
  158. }
  159. WebInspector.HeapSnapshotWorkerProxy.prototype = {
  160. createLoader: function(snapshotConstructorName, proxyConstructor)
  161. {
  162. var objectId = this._nextObjectId++;
  163. var proxy = new WebInspector.HeapSnapshotLoaderProxy(this, objectId, snapshotConstructorName, proxyConstructor);
  164. this._postMessage({callId: this._nextCallId++, disposition: "create", objectId: objectId, methodName: "WebInspector.HeapSnapshotLoader"});
  165. return proxy;
  166. },
  167. dispose: function()
  168. {
  169. this._worker.terminate();
  170. if (this._interval)
  171. clearInterval(this._interval);
  172. },
  173. disposeObject: function(objectId)
  174. {
  175. this._postMessage({callId: this._nextCallId++, disposition: "dispose", objectId: objectId});
  176. },
  177. callGetter: function(callback, objectId, getterName)
  178. {
  179. var callId = this._nextCallId++;
  180. this._callbacks[callId] = callback;
  181. this._postMessage({callId: callId, disposition: "getter", objectId: objectId, methodName: getterName});
  182. },
  183. callFactoryMethod: function(callback, objectId, methodName, proxyConstructor)
  184. {
  185. var callId = this._nextCallId++;
  186. var methodArguments = Array.prototype.slice.call(arguments, 4);
  187. var newObjectId = this._nextObjectId++;
  188. if (callback) {
  189. function wrapCallback(remoteResult)
  190. {
  191. callback(remoteResult ? new proxyConstructor(this, newObjectId) : null);
  192. }
  193. this._callbacks[callId] = wrapCallback.bind(this);
  194. this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
  195. return null;
  196. } else {
  197. this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
  198. return new proxyConstructor(this, newObjectId);
  199. }
  200. },
  201. callMethod: function(callback, objectId, methodName)
  202. {
  203. var callId = this._nextCallId++;
  204. var methodArguments = Array.prototype.slice.call(arguments, 3);
  205. if (callback)
  206. this._callbacks[callId] = callback;
  207. this._postMessage({callId: callId, disposition: "method", objectId: objectId, methodName: methodName, methodArguments: methodArguments});
  208. },
  209. startCheckingForLongRunningCalls: function()
  210. {
  211. if (this._interval)
  212. return;
  213. this._checkLongRunningCalls();
  214. this._interval = setInterval(this._checkLongRunningCalls.bind(this), 300);
  215. },
  216. _checkLongRunningCalls: function()
  217. {
  218. for (var callId in this._previousCallbacks)
  219. if (!(callId in this._callbacks))
  220. delete this._previousCallbacks[callId];
  221. var hasLongRunningCalls = false;
  222. for (callId in this._previousCallbacks) {
  223. hasLongRunningCalls = true;
  224. break;
  225. }
  226. this.dispatchEventToListeners("wait", hasLongRunningCalls);
  227. for (callId in this._callbacks)
  228. this._previousCallbacks[callId] = true;
  229. },
  230. _findFunction: function(name)
  231. {
  232. var path = name.split(".");
  233. var result = window;
  234. for (var i = 0; i < path.length; ++i)
  235. result = result[path[i]];
  236. return result;
  237. },
  238. _messageReceived: function(event)
  239. {
  240. var data = event.data;
  241. if (data.eventName) {
  242. if (this._eventHandler)
  243. this._eventHandler(data.eventName, data.data);
  244. return;
  245. }
  246. if (data.error) {
  247. if (data.errorMethodName)
  248. WebInspector.log(WebInspector.UIString("An error happened when a call for method '%s' was requested", data.errorMethodName));
  249. WebInspector.log(data.errorCallStack);
  250. delete this._callbacks[data.callId];
  251. return;
  252. }
  253. if (!this._callbacks[data.callId])
  254. return;
  255. var callback = this._callbacks[data.callId];
  256. delete this._callbacks[data.callId];
  257. callback(data.result);
  258. },
  259. _postMessage: function(message)
  260. {
  261. this._worker.postMessage(message);
  262. },
  263. __proto__: WebInspector.Object.prototype
  264. }
  265. /**
  266. * @constructor
  267. */
  268. WebInspector.HeapSnapshotProxyObject = function(worker, objectId)
  269. {
  270. this._worker = worker;
  271. this._objectId = objectId;
  272. }
  273. WebInspector.HeapSnapshotProxyObject.prototype = {
  274. _callWorker: function(workerMethodName, args)
  275. {
  276. args.splice(1, 0, this._objectId);
  277. return this._worker[workerMethodName].apply(this._worker, args);
  278. },
  279. dispose: function()
  280. {
  281. this._worker.disposeObject(this._objectId);
  282. },
  283. disposeWorker: function()
  284. {
  285. this._worker.dispose();
  286. },
  287. /**
  288. * @param {...*} var_args
  289. */
  290. callFactoryMethod: function(callback, methodName, proxyConstructor, var_args)
  291. {
  292. return this._callWorker("callFactoryMethod", Array.prototype.slice.call(arguments, 0));
  293. },
  294. callGetter: function(callback, getterName)
  295. {
  296. return this._callWorker("callGetter", Array.prototype.slice.call(arguments, 0));
  297. },
  298. /**
  299. * @param {...*} var_args
  300. */
  301. callMethod: function(callback, methodName, var_args)
  302. {
  303. return this._callWorker("callMethod", Array.prototype.slice.call(arguments, 0));
  304. },
  305. get worker() {
  306. return this._worker;
  307. }
  308. };
  309. /**
  310. * @constructor
  311. * @extends {WebInspector.HeapSnapshotProxyObject}
  312. * @implements {WebInspector.OutputStream}
  313. */
  314. WebInspector.HeapSnapshotLoaderProxy = function(worker, objectId, snapshotConstructorName, proxyConstructor)
  315. {
  316. WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
  317. this._snapshotConstructorName = snapshotConstructorName;
  318. this._proxyConstructor = proxyConstructor;
  319. this._pendingSnapshotConsumers = [];
  320. }
  321. WebInspector.HeapSnapshotLoaderProxy.prototype = {
  322. /**
  323. * @param {function(WebInspector.HeapSnapshotProxy)} callback
  324. */
  325. addConsumer: function(callback)
  326. {
  327. this._pendingSnapshotConsumers.push(callback);
  328. },
  329. /**
  330. * @param {string} chunk
  331. * @param {function(WebInspector.OutputStream)=} callback
  332. */
  333. write: function(chunk, callback)
  334. {
  335. this.callMethod(callback, "write", chunk);
  336. },
  337. /**
  338. * @param {function()=} callback
  339. */
  340. close: function(callback)
  341. {
  342. function buildSnapshot()
  343. {
  344. if (callback)
  345. callback();
  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.HeapSnapshotProxyObject}
  454. */
  455. WebInspector.HeapSnapshotProviderProxy = function(worker, objectId)
  456. {
  457. WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
  458. }
  459. WebInspector.HeapSnapshotProviderProxy.prototype = {
  460. nodePosition: function(snapshotObjectId, callback)
  461. {
  462. this.callMethod(callback, "nodePosition", snapshotObjectId);
  463. },
  464. isEmpty: function(callback)
  465. {
  466. this.callMethod(callback, "isEmpty");
  467. },
  468. serializeItemsRange: function(startPosition, endPosition, callback)
  469. {
  470. this.callMethod(callback, "serializeItemsRange", startPosition, endPosition);
  471. },
  472. sortAndRewind: function(comparator, callback)
  473. {
  474. this.callMethod(callback, "sortAndRewind", comparator);
  475. },
  476. __proto__: WebInspector.HeapSnapshotProxyObject.prototype
  477. }