TimelinePanel.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. * Copyright (C) 2012 Intel Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. importScript("MemoryStatistics.js");
  32. importScript("DOMCountersGraph.js");
  33. importScript("NativeMemoryGraph.js");
  34. importScript("TimelineModel.js");
  35. importScript("TimelineOverviewPane.js");
  36. importScript("TimelinePresentationModel.js");
  37. importScript("TimelineFrameController.js");
  38. /**
  39. * @constructor
  40. * @extends {WebInspector.Panel}
  41. */
  42. WebInspector.TimelinePanel = function()
  43. {
  44. WebInspector.Panel.call(this, "timeline");
  45. this.registerRequiredCSS("timelinePanel.css");
  46. this._model = new WebInspector.TimelineModel();
  47. this._presentationModel = new WebInspector.TimelinePresentationModel();
  48. this._overviewModeSetting = WebInspector.settings.createSetting("timelineOverviewMode", WebInspector.TimelineOverviewPane.Mode.Events);
  49. this._glueRecordsSetting = WebInspector.settings.createSetting("timelineGlueRecords", false);
  50. this._overviewPane = new WebInspector.TimelineOverviewPane(this._model);
  51. this._overviewPane.addEventListener(WebInspector.TimelineOverviewPane.Events.WindowChanged, this._invalidateAndScheduleRefresh.bind(this, false, true));
  52. this._overviewPane.addEventListener(WebInspector.TimelineOverviewPane.Events.ModeChanged, this._overviewModeChanged, this);
  53. this._overviewPane.show(this.element);
  54. this.element.addEventListener("contextmenu", this._contextMenu.bind(this), false);
  55. this.element.tabIndex = 0;
  56. this.element.addStyleClass("split-view-vertical");
  57. this._sidebarBackgroundElement = document.createElement("div");
  58. this._sidebarBackgroundElement.className = "sidebar split-view-sidebar split-view-contents-first timeline-sidebar-background";
  59. this.element.appendChild(this._sidebarBackgroundElement);
  60. this.createSidebarViewWithTree();
  61. this.element.appendChild(this.splitView.resizerElement());
  62. this._containerElement = this.splitView.element;
  63. this._containerElement.id = "timeline-container";
  64. this._containerElement.addEventListener("scroll", this._onScroll.bind(this), false);
  65. this._timelineMemorySplitter = this.element.createChild("div");
  66. this._timelineMemorySplitter.id = "timeline-memory-splitter";
  67. WebInspector.installDragHandle(this._timelineMemorySplitter, this._startSplitterDragging.bind(this), this._splitterDragging.bind(this), this._endSplitterDragging.bind(this), "ns-resize");
  68. this._timelineMemorySplitter.addStyleClass("hidden");
  69. this._includeDomCounters = false;
  70. this._includeNativeMemoryStatistics = false;
  71. if (WebInspector.experimentsSettings.nativeMemoryTimeline.isEnabled()) {
  72. this._memoryStatistics = new WebInspector.NativeMemoryGraph(this, this._model, this.splitView.sidebarWidth());
  73. this._includeNativeMemoryStatistics = true;
  74. } else {
  75. this._memoryStatistics = new WebInspector.DOMCountersGraph(this, this._model, this.splitView.sidebarWidth());
  76. this._includeDomCounters = true;
  77. }
  78. WebInspector.settings.memoryCounterGraphsHeight = WebInspector.settings.createSetting("memoryCounterGraphsHeight", 150);
  79. var itemsTreeElement = new WebInspector.SidebarSectionTreeElement(WebInspector.UIString("RECORDS"), {}, true);
  80. this.sidebarTree.appendChild(itemsTreeElement);
  81. this._sidebarListElement = document.createElement("div");
  82. this.sidebarElement.appendChild(this._sidebarListElement);
  83. this._containerContentElement = this.splitView.mainElement;
  84. this._containerContentElement.id = "resources-container-content";
  85. this._timelineGrid = new WebInspector.TimelineGrid();
  86. this._itemsGraphsElement = this._timelineGrid.itemsGraphsElement;
  87. this._itemsGraphsElement.id = "timeline-graphs";
  88. this._containerContentElement.appendChild(this._timelineGrid.element);
  89. this._timelineGrid.gridHeaderElement.id = "timeline-grid-header";
  90. this._memoryStatistics.setMainTimelineGrid(this._timelineGrid);
  91. this.element.appendChild(this._timelineGrid.gridHeaderElement);
  92. this._topGapElement = document.createElement("div");
  93. this._topGapElement.className = "timeline-gap";
  94. this._itemsGraphsElement.appendChild(this._topGapElement);
  95. this._graphRowsElement = document.createElement("div");
  96. this._itemsGraphsElement.appendChild(this._graphRowsElement);
  97. this._bottomGapElement = document.createElement("div");
  98. this._bottomGapElement.className = "timeline-gap";
  99. this._itemsGraphsElement.appendChild(this._bottomGapElement);
  100. this._expandElements = document.createElement("div");
  101. this._expandElements.id = "orphan-expand-elements";
  102. this._itemsGraphsElement.appendChild(this._expandElements);
  103. this._calculator = new WebInspector.TimelineCalculator(this._model);
  104. this._createStatusBarItems();
  105. this._frameMode = false;
  106. this._boundariesAreValid = true;
  107. this._scrollTop = 0;
  108. this._popoverHelper = new WebInspector.PopoverHelper(this.element, this._getPopoverAnchor.bind(this), this._showPopover.bind(this));
  109. this.element.addEventListener("mousemove", this._mouseMove.bind(this), false);
  110. this.element.addEventListener("mouseout", this._mouseOut.bind(this), false);
  111. // Short events filter is disabled by default.
  112. this._durationFilter = new WebInspector.TimelineIsLongFilter();
  113. this._expandOffset = 15;
  114. this._headerLineCount = 1;
  115. this._adjustHeaderHeight();
  116. this._mainThreadTasks = /** @type {!Array.<{startTime: number, endTime: number}>} */ ([]);
  117. this._cpuBarsElement = this._timelineGrid.gridHeaderElement.createChild("div", "timeline-cpu-bars");
  118. this._mainThreadMonitoringEnabled = Capabilities.timelineCanMonitorMainThread && WebInspector.settings.showCpuOnTimelineRuler.get();
  119. WebInspector.settings.showCpuOnTimelineRuler.addChangeListener(this._showCpuOnTimelineRulerChanged, this);
  120. this._createFileSelector();
  121. this._model.addEventListener(WebInspector.TimelineModel.Events.RecordAdded, this._onTimelineEventRecorded, this);
  122. this._model.addEventListener(WebInspector.TimelineModel.Events.RecordsCleared, this._onRecordsCleared, this);
  123. this._registerShortcuts();
  124. this._allRecordsCount = 0;
  125. this._presentationModel.addFilter(new WebInspector.TimelineWindowFilter(this._overviewPane));
  126. this._presentationModel.addFilter(new WebInspector.TimelineCategoryFilter());
  127. this._presentationModel.addFilter(this._durationFilter);
  128. }
  129. // Define row height, should be in sync with styles for timeline graphs.
  130. WebInspector.TimelinePanel.rowHeight = 18;
  131. WebInspector.TimelinePanel.durationFilterPresetsMs = [0, 1, 15];
  132. WebInspector.TimelinePanel.prototype = {
  133. _showCpuOnTimelineRulerChanged: function()
  134. {
  135. var mainThreadMonitoringEnabled = WebInspector.settings.showCpuOnTimelineRuler.get();
  136. if (this._mainThreadMonitoringEnabled !== mainThreadMonitoringEnabled) {
  137. this._mainThreadMonitoringEnabled = mainThreadMonitoringEnabled;
  138. this._refreshMainThreadBars();
  139. }
  140. },
  141. /**
  142. * @param {Event} event
  143. * @return {boolean}
  144. */
  145. _startSplitterDragging: function(event)
  146. {
  147. this._dragOffset = this._timelineMemorySplitter.offsetTop + 2 - event.pageY;
  148. return true;
  149. },
  150. /**
  151. * @param {Event} event
  152. */
  153. _splitterDragging: function(event)
  154. {
  155. var top = event.pageY + this._dragOffset
  156. this._setSplitterPosition(top);
  157. event.preventDefault();
  158. },
  159. /**
  160. * @param {Event} event
  161. */
  162. _endSplitterDragging: function(event)
  163. {
  164. delete this._dragOffset;
  165. this._memoryStatistics.show();
  166. WebInspector.settings.memoryCounterGraphsHeight.set(this.splitView.element.offsetHeight);
  167. },
  168. _setSplitterPosition: function(top)
  169. {
  170. const overviewHeight = 90;
  171. const sectionMinHeight = 100;
  172. top = Number.constrain(top, overviewHeight + sectionMinHeight, this.element.offsetHeight - sectionMinHeight);
  173. this.splitView.element.style.height = (top - overviewHeight) + "px";
  174. this._timelineMemorySplitter.style.top = (top - 2) + "px";
  175. this._memoryStatistics.setTopPosition(top);
  176. this._containerElementHeight = this._containerElement.clientHeight;
  177. this.onResize();
  178. },
  179. get calculator()
  180. {
  181. return this._calculator;
  182. },
  183. statusBarItems: function()
  184. {
  185. return this._statusBarItems.select("element").concat([
  186. this._miscStatusBarItems
  187. ]);
  188. },
  189. defaultFocusedElement: function()
  190. {
  191. return this.element;
  192. },
  193. _createStatusBarItems: function()
  194. {
  195. this._statusBarItems = /** @type {!Array.<!WebInspector.StatusBarItem>} */ ([]);
  196. this.toggleTimelineButton = new WebInspector.StatusBarButton(WebInspector.UIString("Record"), "record-profile-status-bar-item");
  197. this.toggleTimelineButton.addEventListener("click", this._toggleTimelineButtonClicked, this);
  198. this._statusBarItems.push(this.toggleTimelineButton);
  199. this.clearButton = new WebInspector.StatusBarButton(WebInspector.UIString("Clear"), "clear-status-bar-item");
  200. this.clearButton.addEventListener("click", this._clearPanel, this);
  201. this._statusBarItems.push(this.clearButton);
  202. this.garbageCollectButton = new WebInspector.StatusBarButton(WebInspector.UIString("Collect Garbage"), "garbage-collect-status-bar-item");
  203. this.garbageCollectButton.addEventListener("click", this._garbageCollectButtonClicked, this);
  204. this._statusBarItems.push(this.garbageCollectButton);
  205. this._glueParentButton = new WebInspector.StatusBarButton(WebInspector.UIString("Glue asynchronous events to causes"), "glue-async-status-bar-item");
  206. this._glueParentButton.toggled = this._glueRecordsSetting.get();
  207. this._presentationModel.setGlueRecords(this._glueParentButton.toggled);
  208. this._glueParentButton.addEventListener("click", this._glueParentButtonClicked, this);
  209. this._statusBarItems.push(this._glueParentButton);
  210. this._durationFilterSelector = new WebInspector.StatusBarComboBox(this._durationFilterChanged.bind(this));
  211. for (var presetIndex = 0; presetIndex < WebInspector.TimelinePanel.durationFilterPresetsMs.length; ++presetIndex) {
  212. var durationMs = WebInspector.TimelinePanel.durationFilterPresetsMs[presetIndex];
  213. var option = document.createElement("option");
  214. if (!durationMs) {
  215. option.text = WebInspector.UIString("All");
  216. option.title = WebInspector.UIString("Show all records");
  217. } else {
  218. option.text = WebInspector.UIString("\u2265 %dms", durationMs);
  219. option.title = WebInspector.UIString("Hide records shorter than %dms", durationMs);
  220. }
  221. option._durationMs = durationMs;
  222. this._durationFilterSelector.addOption(option);
  223. this._durationFilterSelector.element.title = this._durationFilterSelector.selectedOption().title;
  224. }
  225. this._statusBarItems.push(this._durationFilterSelector);
  226. this._miscStatusBarItems = document.createElement("div");
  227. this._miscStatusBarItems.className = "status-bar-items timeline-misc-status-bar-items";
  228. this._statusBarFilters = this._miscStatusBarItems.createChild("div", "timeline-misc-status-bar-filters");
  229. var categories = WebInspector.TimelinePresentationModel.categories();
  230. for (var categoryName in categories) {
  231. var category = categories[categoryName];
  232. if (category.overviewStripGroupIndex < 0)
  233. continue;
  234. this._statusBarFilters.appendChild(this._createTimelineCategoryStatusBarCheckbox(category, this._onCategoryCheckboxClicked.bind(this, category)));
  235. }
  236. var statsContainer = this._statusBarFilters.createChild("div");
  237. statsContainer.className = "timeline-records-stats-container";
  238. this.recordsCounter = statsContainer.createChild("div");
  239. this.recordsCounter.className = "timeline-records-stats";
  240. this.frameStatistics = statsContainer.createChild("div");
  241. this.frameStatistics.className = "timeline-records-stats hidden";
  242. function getAnchor()
  243. {
  244. return this.frameStatistics;
  245. }
  246. this._frameStatisticsPopoverHelper = new WebInspector.PopoverHelper(this.frameStatistics, getAnchor.bind(this), this._showFrameStatistics.bind(this));
  247. },
  248. _createTimelineCategoryStatusBarCheckbox: function(category, onCheckboxClicked)
  249. {
  250. var labelContainer = document.createElement("div");
  251. labelContainer.addStyleClass("timeline-category-statusbar-item");
  252. labelContainer.addStyleClass("timeline-category-" + category.name);
  253. labelContainer.addStyleClass("status-bar-item");
  254. var label = document.createElement("label");
  255. var checkElement = document.createElement("input");
  256. checkElement.type = "checkbox";
  257. checkElement.className = "timeline-category-checkbox";
  258. checkElement.checked = true;
  259. checkElement.addEventListener("click", onCheckboxClicked, false);
  260. label.appendChild(checkElement);
  261. var typeElement = document.createElement("span");
  262. typeElement.className = "type";
  263. typeElement.textContent = category.title;
  264. label.appendChild(typeElement);
  265. labelContainer.appendChild(label);
  266. return labelContainer;
  267. },
  268. _onCategoryCheckboxClicked: function(category, event)
  269. {
  270. category.hidden = !event.target.checked;
  271. this._invalidateAndScheduleRefresh(true, true);
  272. },
  273. /**
  274. * @param {?WebInspector.ProgressIndicator} indicator
  275. */
  276. _setOperationInProgress: function(indicator)
  277. {
  278. this._operationInProgress = !!indicator;
  279. for (var i = 0; i < this._statusBarItems.length; ++i)
  280. this._statusBarItems[i].setEnabled(!this._operationInProgress);
  281. this._glueParentButton.setEnabled(!this._operationInProgress && !this._frameController);
  282. this._miscStatusBarItems.removeChildren();
  283. this._miscStatusBarItems.appendChild(indicator ? indicator.element : this._statusBarFilters);
  284. },
  285. _registerShortcuts: function()
  286. {
  287. this.registerShortcuts(WebInspector.TimelinePanelDescriptor.ShortcutKeys.StartStopRecording, this._toggleTimelineButtonClicked.bind(this));
  288. if (InspectorFrontendHost.canSave())
  289. this.registerShortcuts(WebInspector.TimelinePanelDescriptor.ShortcutKeys.SaveToFile, this._saveToFile.bind(this));
  290. this.registerShortcuts(WebInspector.TimelinePanelDescriptor.ShortcutKeys.LoadFromFile, this._selectFileToLoad.bind(this));
  291. },
  292. _createFileSelector: function()
  293. {
  294. if (this._fileSelectorElement)
  295. this.element.removeChild(this._fileSelectorElement);
  296. this._fileSelectorElement = WebInspector.createFileSelectorElement(this._loadFromFile.bind(this));
  297. this.element.appendChild(this._fileSelectorElement);
  298. },
  299. _contextMenu: function(event)
  300. {
  301. var contextMenu = new WebInspector.ContextMenu(event);
  302. if (InspectorFrontendHost.canSave())
  303. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save Timeline data\u2026" : "Save Timeline Data\u2026"), this._saveToFile.bind(this), this._operationInProgress);
  304. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Load Timeline data\u2026" : "Load Timeline Data\u2026"), this._selectFileToLoad.bind(this), this._operationInProgress);
  305. contextMenu.show();
  306. },
  307. /**
  308. * @param {Event=} event
  309. * @return {boolean}
  310. */
  311. _saveToFile: function(event)
  312. {
  313. if (this._operationInProgress)
  314. return true;
  315. this._model.saveToFile();
  316. return true;
  317. },
  318. /**
  319. * @param {Event=} event
  320. * @return {boolean}
  321. */
  322. _selectFileToLoad: function(event) {
  323. this._fileSelectorElement.click();
  324. return true;
  325. },
  326. /**
  327. * @param {!File} file
  328. */
  329. _loadFromFile: function(file)
  330. {
  331. var progressIndicator = this._prepareToLoadTimeline();
  332. if (!progressIndicator)
  333. return;
  334. this._model.loadFromFile(file, progressIndicator);
  335. this._createFileSelector();
  336. },
  337. /**
  338. * @param {string} url
  339. */
  340. loadFromURL: function(url)
  341. {
  342. var progressIndicator = this._prepareToLoadTimeline();
  343. if (!progressIndicator)
  344. return;
  345. this._model.loadFromURL(url, progressIndicator);
  346. },
  347. /**
  348. * @return {?WebInspector.ProgressIndicator}
  349. */
  350. _prepareToLoadTimeline: function()
  351. {
  352. if (this._operationInProgress)
  353. return null;
  354. if (this.toggleTimelineButton.toggled) {
  355. this.toggleTimelineButton.toggled = false;
  356. this._model.stopRecord();
  357. }
  358. var progressIndicator = new WebInspector.ProgressIndicator();
  359. progressIndicator.addEventListener(WebInspector.ProgressIndicator.Events.Done, this._setOperationInProgress.bind(this, null));
  360. this._setOperationInProgress(progressIndicator);
  361. return progressIndicator;
  362. },
  363. _rootRecord: function()
  364. {
  365. return this._presentationModel.rootRecord();
  366. },
  367. _updateRecordsCounter: function(recordsInWindowCount)
  368. {
  369. this.recordsCounter.textContent = WebInspector.UIString("%d of %d records shown", recordsInWindowCount, this._allRecordsCount);
  370. },
  371. _updateFrameStatistics: function(frames)
  372. {
  373. if (frames.length) {
  374. this._lastFrameStatistics = new WebInspector.FrameStatistics(frames);
  375. var details = WebInspector.UIString("avg: %s, \u03c3: %s",
  376. Number.secondsToString(this._lastFrameStatistics.average, true), Number.secondsToString(this._lastFrameStatistics.stddev, true));
  377. } else
  378. this._lastFrameStatistics = null;
  379. this.frameStatistics.textContent = WebInspector.UIString("%d of %d frames shown", frames.length, this._presentationModel.frames().length);
  380. if (details) {
  381. this.frameStatistics.appendChild(document.createTextNode(" ("));
  382. this.frameStatistics.createChild("span", "timeline-frames-stats").textContent = details;
  383. this.frameStatistics.appendChild(document.createTextNode(")"));
  384. }
  385. },
  386. /**
  387. * @param {Element} anchor
  388. * @param {WebInspector.Popover} popover
  389. */
  390. _showFrameStatistics: function(anchor, popover)
  391. {
  392. popover.show(WebInspector.TimelinePresentationModel.generatePopupContentForFrameStatistics(this._lastFrameStatistics), anchor);
  393. },
  394. _updateEventDividers: function()
  395. {
  396. this._timelineGrid.removeEventDividers();
  397. var clientWidth = this._graphRowsElementWidth;
  398. var dividers = [];
  399. var eventDividerRecords = this._presentationModel.eventDividerRecords();
  400. for (var i = 0; i < eventDividerRecords.length; ++i) {
  401. var record = eventDividerRecords[i];
  402. var positions = this._calculator.computeBarGraphWindowPosition(record);
  403. var dividerPosition = Math.round(positions.left);
  404. if (dividerPosition < 0 || dividerPosition >= clientWidth || dividers[dividerPosition])
  405. continue;
  406. var divider = WebInspector.TimelinePresentationModel.createEventDivider(record.type, record.title);
  407. divider.style.left = dividerPosition + "px";
  408. dividers[dividerPosition] = divider;
  409. }
  410. this._timelineGrid.addEventDividers(dividers);
  411. },
  412. _updateFrameBars: function(frames)
  413. {
  414. var clientWidth = this._graphRowsElementWidth;
  415. if (this._frameContainer)
  416. this._frameContainer.removeChildren();
  417. else {
  418. const frameContainerBorderWidth = 1;
  419. this._frameContainer = document.createElement("div");
  420. this._frameContainer.addStyleClass("fill");
  421. this._frameContainer.addStyleClass("timeline-frame-container");
  422. this._frameContainer.style.height = this._headerLineCount * WebInspector.TimelinePanel.rowHeight + frameContainerBorderWidth + "px";
  423. this._frameContainer.addEventListener("dblclick", this._onFrameDoubleClicked.bind(this), false);
  424. }
  425. var dividers = [ this._frameContainer ];
  426. for (var i = 0; i < frames.length; ++i) {
  427. var frame = frames[i];
  428. var frameStart = this._calculator.computePosition(frame.startTime);
  429. var frameEnd = this._calculator.computePosition(frame.endTime);
  430. var frameStrip = document.createElement("div");
  431. frameStrip.className = "timeline-frame-strip";
  432. var actualStart = Math.max(frameStart, 0);
  433. var width = frameEnd - actualStart;
  434. frameStrip.style.left = actualStart + "px";
  435. frameStrip.style.width = width + "px";
  436. frameStrip._frame = frame;
  437. const minWidthForFrameInfo = 60;
  438. if (width > minWidthForFrameInfo)
  439. frameStrip.textContent = Number.secondsToString(frame.endTime - frame.startTime, true);
  440. this._frameContainer.appendChild(frameStrip);
  441. if (actualStart > 0) {
  442. var frameMarker = WebInspector.TimelinePresentationModel.createEventDivider(WebInspector.TimelineModel.RecordType.BeginFrame);
  443. frameMarker.style.left = frameStart + "px";
  444. dividers.push(frameMarker);
  445. }
  446. }
  447. this._timelineGrid.addEventDividers(dividers);
  448. },
  449. _onFrameDoubleClicked: function(event)
  450. {
  451. var frameBar = event.target.enclosingNodeOrSelfWithClass("timeline-frame-strip");
  452. if (!frameBar)
  453. return;
  454. this._overviewPane.zoomToFrame(frameBar._frame);
  455. },
  456. _overviewModeChanged: function(event)
  457. {
  458. var mode = event.data;
  459. var shouldShowMemory = mode === WebInspector.TimelineOverviewPane.Mode.Memory;
  460. var frameMode = mode === WebInspector.TimelineOverviewPane.Mode.Frames;
  461. this._overviewModeSetting.set(mode);
  462. if (frameMode !== this._frameMode) {
  463. this._frameMode = frameMode;
  464. this._glueParentButton.setEnabled(!frameMode);
  465. this._presentationModel.setGlueRecords(this._glueParentButton.toggled && !frameMode);
  466. this._repopulateRecords();
  467. if (frameMode) {
  468. this.element.addStyleClass("timeline-frame-overview");
  469. this.recordsCounter.addStyleClass("hidden");
  470. this.frameStatistics.removeStyleClass("hidden");
  471. this._frameController = new WebInspector.TimelineFrameController(this._model, this._overviewPane, this._presentationModel);
  472. } else {
  473. this._frameController.dispose();
  474. this._frameController = null;
  475. this.element.removeStyleClass("timeline-frame-overview");
  476. this.recordsCounter.removeStyleClass("hidden");
  477. this.frameStatistics.addStyleClass("hidden");
  478. }
  479. }
  480. if (shouldShowMemory === this._memoryStatistics.visible())
  481. return;
  482. if (!shouldShowMemory) {
  483. this._timelineMemorySplitter.addStyleClass("hidden");
  484. this._memoryStatistics.hide();
  485. this.splitView.element.style.height = "auto";
  486. this.splitView.element.style.bottom = "0";
  487. this.onResize();
  488. } else {
  489. this._timelineMemorySplitter.removeStyleClass("hidden");
  490. this._memoryStatistics.show();
  491. this.splitView.element.style.bottom = "auto";
  492. this._setSplitterPosition(WebInspector.settings.memoryCounterGraphsHeight.get());
  493. }
  494. },
  495. /**
  496. * @return {boolean}
  497. */
  498. _toggleTimelineButtonClicked: function()
  499. {
  500. if (this._operationInProgress)
  501. return true;
  502. if (this.toggleTimelineButton.toggled) {
  503. this._model.stopRecord();
  504. this.toggleTimelineButton.title = WebInspector.UIString("Record");
  505. } else {
  506. this._model.startRecord(this._includeDomCounters, this._includeNativeMemoryStatistics);
  507. this.toggleTimelineButton.title = WebInspector.UIString("Stop");
  508. WebInspector.userMetrics.TimelineStarted.record();
  509. }
  510. this.toggleTimelineButton.toggled = !this.toggleTimelineButton.toggled;
  511. return true;
  512. },
  513. _durationFilterChanged: function()
  514. {
  515. var option = this._durationFilterSelector.selectedOption();
  516. var minimumRecordDuration = +option._durationMs / 1000.0;
  517. this._durationFilter.setMinimumRecordDuration(minimumRecordDuration);
  518. this._durationFilterSelector.element.title = option.title;
  519. this._invalidateAndScheduleRefresh(true, true);
  520. },
  521. _garbageCollectButtonClicked: function()
  522. {
  523. HeapProfilerAgent.collectGarbage();
  524. },
  525. _glueParentButtonClicked: function()
  526. {
  527. var newValue = !this._glueParentButton.toggled;
  528. this._glueParentButton.toggled = newValue;
  529. this._presentationModel.setGlueRecords(newValue);
  530. this._glueRecordsSetting.set(newValue);
  531. this._repopulateRecords();
  532. },
  533. _repopulateRecords: function()
  534. {
  535. this._resetPanel();
  536. this._automaticallySizeWindow = false;
  537. var records = this._model.records;
  538. for (var i = 0; i < records.length; ++i)
  539. this._innerAddRecordToTimeline(records[i]);
  540. this._invalidateAndScheduleRefresh(false, true);
  541. },
  542. _onTimelineEventRecorded: function(event)
  543. {
  544. if (this._innerAddRecordToTimeline(event.data))
  545. this._invalidateAndScheduleRefresh(false, false);
  546. },
  547. _innerAddRecordToTimeline: function(record)
  548. {
  549. if (record.type === WebInspector.TimelineModel.RecordType.Program) {
  550. this._mainThreadTasks.push({
  551. startTime: WebInspector.TimelineModel.startTimeInSeconds(record),
  552. endTime: WebInspector.TimelineModel.endTimeInSeconds(record)
  553. });
  554. }
  555. var records = this._presentationModel.addRecord(record);
  556. this._allRecordsCount += records.length;
  557. var hasVisibleRecords = false;
  558. var presentationModel = this._presentationModel;
  559. function checkVisible(record)
  560. {
  561. hasVisibleRecords |= presentationModel.isVisible(record);
  562. }
  563. WebInspector.TimelinePresentationModel.forAllRecords(records, checkVisible);
  564. function isAdoptedRecord(record)
  565. {
  566. return record.parent !== presentationModel.rootRecord;
  567. }
  568. // Tell caller update is necessary either if we added a visible record or if we re-parented a record.
  569. return hasVisibleRecords || records.some(isAdoptedRecord);
  570. },
  571. sidebarResized: function(event)
  572. {
  573. var width = event.data;
  574. this._resize(width);
  575. this._sidebarBackgroundElement.style.width = width + "px";
  576. this._overviewPane.sidebarResized(width);
  577. this._memoryStatistics.setSidebarWidth(width);
  578. this._timelineGrid.gridHeaderElement.style.left = width + "px";
  579. },
  580. onResize: function()
  581. {
  582. this._resize(this.splitView.sidebarWidth());
  583. },
  584. /**
  585. * @param {number} sidebarWidth
  586. */
  587. _resize: function(sidebarWidth)
  588. {
  589. this._closeRecordDetails();
  590. this._scheduleRefresh(false, true);
  591. this._graphRowsElementWidth = this._graphRowsElement.offsetWidth;
  592. this._containerElementHeight = this._containerElement.clientHeight;
  593. var lastItemElement = this._statusBarItems[this._statusBarItems.length - 1].element;
  594. var minFloatingStatusBarItemsOffset = lastItemElement.totalOffsetLeft() + lastItemElement.offsetWidth;
  595. this._timelineGrid.gridHeaderElement.style.width = this._itemsGraphsElement.offsetWidth + "px";
  596. this._miscStatusBarItems.style.left = Math.max(minFloatingStatusBarItemsOffset, sidebarWidth) + "px";
  597. },
  598. _clearPanel: function()
  599. {
  600. this._model.reset();
  601. },
  602. _onRecordsCleared: function()
  603. {
  604. this._resetPanel();
  605. this._invalidateAndScheduleRefresh(true, true);
  606. },
  607. _resetPanel: function()
  608. {
  609. this._presentationModel.reset();
  610. this._boundariesAreValid = false;
  611. this._adjustScrollPosition(0);
  612. this._closeRecordDetails();
  613. this._allRecordsCount = 0;
  614. this._automaticallySizeWindow = true;
  615. this._mainThreadTasks = [];
  616. },
  617. elementsToRestoreScrollPositionsFor: function()
  618. {
  619. return [this._containerElement];
  620. },
  621. wasShown: function()
  622. {
  623. WebInspector.Panel.prototype.wasShown.call(this);
  624. if (!WebInspector.TimelinePanel._categoryStylesInitialized) {
  625. WebInspector.TimelinePanel._categoryStylesInitialized = true;
  626. this._injectCategoryStyles();
  627. }
  628. this._overviewPane.setMode(this._overviewModeSetting.get());
  629. this._refresh();
  630. },
  631. willHide: function()
  632. {
  633. this._closeRecordDetails();
  634. WebInspector.Panel.prototype.willHide.call(this);
  635. },
  636. _onScroll: function(event)
  637. {
  638. this._closeRecordDetails();
  639. this._scrollTop = this._containerElement.scrollTop;
  640. var dividersTop = Math.max(0, this._scrollTop);
  641. this._timelineGrid.setScrollAndDividerTop(this._scrollTop, dividersTop);
  642. this._scheduleRefresh(true, true);
  643. },
  644. /**
  645. * @param {boolean} preserveBoundaries
  646. * @param {boolean} userGesture
  647. */
  648. _invalidateAndScheduleRefresh: function(preserveBoundaries, userGesture)
  649. {
  650. this._presentationModel.invalidateFilteredRecords();
  651. delete this._searchResults;
  652. this._scheduleRefresh(preserveBoundaries, userGesture);
  653. },
  654. /**
  655. * @param {boolean} preserveBoundaries
  656. * @param {boolean} userGesture
  657. */
  658. _scheduleRefresh: function(preserveBoundaries, userGesture)
  659. {
  660. this._closeRecordDetails();
  661. this._boundariesAreValid &= preserveBoundaries;
  662. if (!this.isShowing())
  663. return;
  664. if (preserveBoundaries || userGesture)
  665. this._refresh();
  666. else {
  667. if (!this._refreshTimeout)
  668. this._refreshTimeout = setTimeout(this._refresh.bind(this), 300);
  669. }
  670. },
  671. _refresh: function()
  672. {
  673. if (this._refreshTimeout) {
  674. clearTimeout(this._refreshTimeout);
  675. delete this._refreshTimeout;
  676. }
  677. this._timelinePaddingLeft = this._expandOffset;
  678. this._calculator.setWindow(this._overviewPane.windowStartTime(), this._overviewPane.windowEndTime());
  679. this._calculator.setDisplayWindow(this._timelinePaddingLeft, this._graphRowsElementWidth);
  680. var recordsInWindowCount = this._refreshRecords();
  681. this._updateRecordsCounter(recordsInWindowCount);
  682. if (!this._boundariesAreValid) {
  683. this._updateEventDividers();
  684. var frames = this._frameController && this._presentationModel.filteredFrames(this._overviewPane.windowStartTime(), this._overviewPane.windowEndTime());
  685. if (frames) {
  686. this._updateFrameStatistics(frames);
  687. const maxFramesForFrameBars = 30;
  688. if (frames.length && frames.length < maxFramesForFrameBars) {
  689. this._timelineGrid.removeDividers();
  690. this._updateFrameBars(frames);
  691. } else
  692. this._timelineGrid.updateDividers(this._calculator);
  693. } else
  694. this._timelineGrid.updateDividers(this._calculator);
  695. if (this._mainThreadMonitoringEnabled)
  696. this._refreshMainThreadBars();
  697. }
  698. if (this._memoryStatistics.visible())
  699. this._memoryStatistics.refresh();
  700. this._boundariesAreValid = true;
  701. },
  702. revealRecordAt: function(time)
  703. {
  704. var recordToReveal;
  705. function findRecordToReveal(record)
  706. {
  707. if (record.containsTime(time)) {
  708. recordToReveal = record;
  709. return true;
  710. }
  711. // If there is no record containing the time than use the latest one before that time.
  712. if (!recordToReveal || record.endTime < time && recordToReveal.endTime < record.endTime)
  713. recordToReveal = record;
  714. return false;
  715. }
  716. WebInspector.TimelinePresentationModel.forAllRecords(this._presentationModel.rootRecord().children, null, findRecordToReveal);
  717. // The record ends before the window left bound so scroll to the top.
  718. if (!recordToReveal) {
  719. this._containerElement.scrollTop = 0;
  720. return;
  721. }
  722. this._revealRecord(recordToReveal);
  723. },
  724. _revealRecord: function(recordToReveal)
  725. {
  726. // Expand all ancestors.
  727. this._recordToHighlight = recordToReveal;
  728. var treeUpdated = false;
  729. for (var parent = recordToReveal.parent; parent !== this._rootRecord(); parent = parent.parent) {
  730. treeUpdated = treeUpdated || parent.collapsed;
  731. parent.collapsed = false;
  732. }
  733. if (treeUpdated)
  734. this._invalidateAndScheduleRefresh(true, true);
  735. var recordsInWindow = this._presentationModel.filteredRecords();
  736. var index = recordsInWindow.indexOf(recordToReveal);
  737. this._containerElement.scrollTop = index * WebInspector.TimelinePanel.rowHeight;
  738. },
  739. _refreshRecords: function()
  740. {
  741. var recordsInWindow = this._presentationModel.filteredRecords();
  742. // Calculate the visible area.
  743. var visibleTop = this._scrollTop;
  744. var visibleBottom = visibleTop + this._containerElementHeight;
  745. const rowHeight = WebInspector.TimelinePanel.rowHeight;
  746. // Convert visible area to visible indexes. Always include top-level record for a visible nested record.
  747. var startIndex = Math.max(0, Math.min(Math.floor(visibleTop / rowHeight) - this._headerLineCount, recordsInWindow.length - 1));
  748. var endIndex = Math.min(recordsInWindow.length, Math.ceil(visibleBottom / rowHeight));
  749. var lastVisibleLine = Math.max(0, Math.floor(visibleBottom / rowHeight) - this._headerLineCount);
  750. if (this._automaticallySizeWindow && recordsInWindow.length > lastVisibleLine) {
  751. this._automaticallySizeWindow = false;
  752. // If we're at the top, always use real timeline start as a left window bound so that expansion arrow padding logic works.
  753. var windowStartTime = startIndex ? recordsInWindow[startIndex].startTime : this._model.minimumRecordTime();
  754. this._overviewPane.setWindowTimes(windowStartTime, recordsInWindow[Math.max(0, lastVisibleLine - 1)].endTime);
  755. recordsInWindow = this._presentationModel.filteredRecords();
  756. endIndex = Math.min(recordsInWindow.length, lastVisibleLine);
  757. }
  758. // Resize gaps first.
  759. const top = (startIndex * rowHeight) + "px";
  760. this._topGapElement.style.height = top;
  761. this.sidebarElement.style.top = top;
  762. this._bottomGapElement.style.height = (recordsInWindow.length - endIndex) * rowHeight + "px";
  763. // Update visible rows.
  764. var listRowElement = this._sidebarListElement.firstChild;
  765. var width = this._graphRowsElementWidth;
  766. this._itemsGraphsElement.removeChild(this._graphRowsElement);
  767. var graphRowElement = this._graphRowsElement.firstChild;
  768. var scheduleRefreshCallback = this._invalidateAndScheduleRefresh.bind(this, true, true);
  769. this._itemsGraphsElement.removeChild(this._expandElements);
  770. this._expandElements.removeChildren();
  771. this._clearRecordHighlight();
  772. var highlightedRecord = this._recordToHighlight;
  773. delete this._recordToHighlight;
  774. for (var i = 0; i < endIndex; ++i) {
  775. var record = recordsInWindow[i];
  776. var isEven = !(i % 2);
  777. if (i < startIndex) {
  778. var lastChildIndex = i + record.visibleChildrenCount;
  779. if (lastChildIndex >= startIndex && lastChildIndex < endIndex) {
  780. var expandElement = new WebInspector.TimelineExpandableElement(this._expandElements);
  781. var positions = this._calculator.computeBarGraphWindowPosition(record);
  782. expandElement._update(record, i, positions.left - this._expandOffset, positions.width);
  783. }
  784. } else {
  785. if (!listRowElement) {
  786. listRowElement = new WebInspector.TimelineRecordListRow().element;
  787. this._sidebarListElement.appendChild(listRowElement);
  788. }
  789. if (!graphRowElement) {
  790. graphRowElement = new WebInspector.TimelineRecordGraphRow(this._itemsGraphsElement, scheduleRefreshCallback).element;
  791. this._graphRowsElement.appendChild(graphRowElement);
  792. }
  793. if (highlightedRecord === record) {
  794. this._highlightedListRowElement = listRowElement;
  795. this._highlightedGraphRowElement = graphRowElement;
  796. }
  797. listRowElement.row.update(record, isEven, visibleTop);
  798. graphRowElement.row.update(record, isEven, this._calculator, this._expandOffset, i);
  799. listRowElement = listRowElement.nextSibling;
  800. graphRowElement = graphRowElement.nextSibling;
  801. }
  802. }
  803. // Remove extra rows.
  804. while (listRowElement) {
  805. var nextElement = listRowElement.nextSibling;
  806. listRowElement.row.dispose();
  807. listRowElement = nextElement;
  808. }
  809. while (graphRowElement) {
  810. var nextElement = graphRowElement.nextSibling;
  811. graphRowElement.row.dispose();
  812. graphRowElement = nextElement;
  813. }
  814. this._itemsGraphsElement.insertBefore(this._graphRowsElement, this._bottomGapElement);
  815. this._itemsGraphsElement.appendChild(this._expandElements);
  816. this._adjustScrollPosition((recordsInWindow.length + this._headerLineCount) * rowHeight);
  817. this._updateSearchHighlight(false);
  818. if (this._highlightedListRowElement) {
  819. this._highlightedListRowElement.addStyleClass("highlighted-timeline-record");
  820. this._highlightedGraphRowElement.addStyleClass("highlighted-timeline-record");
  821. }
  822. return recordsInWindow.length;
  823. },
  824. _clearRecordHighlight: function()
  825. {
  826. if (!this._highlightedListRowElement)
  827. return;
  828. this._highlightedListRowElement.removeStyleClass("highlighted-timeline-record");
  829. delete this._highlightedListRowElement;
  830. this._highlightedGraphRowElement.removeStyleClass("highlighted-timeline-record");
  831. delete this._highlightedGraphRowElement;
  832. },
  833. _refreshMainThreadBars: function()
  834. {
  835. const barOffset = 3;
  836. const minGap = 3;
  837. var minWidth = WebInspector.TimelineCalculator._minWidth;
  838. var widthAdjustment = minWidth / 2;
  839. var width = this._graphRowsElementWidth;
  840. var boundarySpan = this._overviewPane.windowEndTime() - this._overviewPane.windowStartTime();
  841. var scale = boundarySpan / (width - minWidth - this._timelinePaddingLeft);
  842. var startTime = this._overviewPane.windowStartTime() - this._timelinePaddingLeft * scale;
  843. var endTime = startTime + width * scale;
  844. var tasks = this._mainThreadMonitoringEnabled ? this._mainThreadTasks : [];
  845. function compareEndTime(value, task)
  846. {
  847. return value < task.endTime ? -1 : 1;
  848. }
  849. var taskIndex = insertionIndexForObjectInListSortedByFunction(startTime, tasks, compareEndTime);
  850. var container = this._cpuBarsElement;
  851. var element = container.firstChild;
  852. var lastElement;
  853. var lastLeft;
  854. var lastRight;
  855. for (; taskIndex < tasks.length; ++taskIndex) {
  856. var task = tasks[taskIndex];
  857. if (task.startTime > endTime)
  858. break;
  859. var left = Math.max(0, this._calculator.computePosition(task.startTime) + barOffset - widthAdjustment);
  860. var right = Math.min(width, this._calculator.computePosition(task.endTime) + barOffset + widthAdjustment);
  861. if (lastElement) {
  862. var gap = Math.floor(left) - Math.ceil(lastRight);
  863. if (gap < minGap) {
  864. lastRight = right;
  865. lastElement._tasksInfo.lastTaskIndex = taskIndex;
  866. continue;
  867. }
  868. lastElement.style.width = (lastRight - lastLeft) + "px";
  869. }
  870. if (!element)
  871. element = container.createChild("div", "timeline-graph-bar");
  872. element.style.left = left + "px";
  873. element._tasksInfo = {tasks: tasks, firstTaskIndex: taskIndex, lastTaskIndex: taskIndex};
  874. lastLeft = left;
  875. lastRight = right;
  876. lastElement = element;
  877. element = element.nextSibling;
  878. }
  879. if (lastElement)
  880. lastElement.style.width = (lastRight - lastLeft) + "px";
  881. while (element) {
  882. var nextElement = element.nextSibling;
  883. element._tasksInfo = null;
  884. container.removeChild(element);
  885. element = nextElement;
  886. }
  887. },
  888. _adjustHeaderHeight: function()
  889. {
  890. const headerBorderWidth = 1;
  891. const headerMargin = 2;
  892. var headerHeight = this._headerLineCount * WebInspector.TimelinePanel.rowHeight;
  893. this.sidebarElement.firstChild.style.height = headerHeight + "px";
  894. this._timelineGrid.dividersLabelBarElement.style.height = headerHeight + headerMargin + "px";
  895. this._itemsGraphsElement.style.top = headerHeight + headerBorderWidth + "px";
  896. },
  897. _adjustScrollPosition: function(totalHeight)
  898. {
  899. // Prevent the container from being scrolled off the end.
  900. if ((this._scrollTop + this._containerElementHeight) > totalHeight + 1)
  901. this._containerElement.scrollTop = (totalHeight - this._containerElement.offsetHeight);
  902. },
  903. _getPopoverAnchor: function(element)
  904. {
  905. return element.enclosingNodeOrSelfWithClass("timeline-graph-bar") ||
  906. element.enclosingNodeOrSelfWithClass("timeline-tree-item") ||
  907. element.enclosingNodeOrSelfWithClass("timeline-frame-strip");
  908. },
  909. _mouseOut: function(e)
  910. {
  911. this._hideQuadHighlight();
  912. },
  913. /**
  914. * @param {Event} e
  915. */
  916. _mouseMove: function(e)
  917. {
  918. var anchor = this._getPopoverAnchor(e.target);
  919. if (anchor && anchor.row && anchor.row._record.highlightQuad)
  920. this._highlightQuad(anchor.row._record.highlightQuad);
  921. else
  922. this._hideQuadHighlight();
  923. if (anchor && anchor._tasksInfo) {
  924. var offset = anchor.offsetLeft;
  925. this._timelineGrid.showCurtains(offset >= 0 ? offset : 0, anchor.offsetWidth);
  926. } else
  927. this._timelineGrid.hideCurtains();
  928. },
  929. /**
  930. * @param {Array.<number>} quad
  931. */
  932. _highlightQuad: function(quad)
  933. {
  934. if (this._highlightedQuad === quad)
  935. return;
  936. this._highlightedQuad = quad;
  937. DOMAgent.highlightQuad(quad, WebInspector.Color.PageHighlight.Content.toProtocolRGBA(), WebInspector.Color.PageHighlight.ContentOutline.toProtocolRGBA());
  938. },
  939. _hideQuadHighlight: function()
  940. {
  941. if (this._highlightedQuad) {
  942. delete this._highlightedQuad;
  943. DOMAgent.hideHighlight();
  944. }
  945. },
  946. /**
  947. * @param {Element} anchor
  948. * @param {WebInspector.Popover} popover
  949. */
  950. _showPopover: function(anchor, popover)
  951. {
  952. if (anchor.hasStyleClass("timeline-frame-strip")) {
  953. var frame = anchor._frame;
  954. popover.show(WebInspector.TimelinePresentationModel.generatePopupContentForFrame(frame), anchor);
  955. } else {
  956. if (anchor.row && anchor.row._record)
  957. anchor.row._record.generatePopupContent(showCallback);
  958. else if (anchor._tasksInfo)
  959. popover.show(this._presentationModel.generateMainThreadBarPopupContent(anchor._tasksInfo), anchor, null, null, WebInspector.Popover.Orientation.Bottom);
  960. }
  961. function showCallback(popupContent)
  962. {
  963. popover.show(popupContent, anchor);
  964. }
  965. },
  966. _closeRecordDetails: function()
  967. {
  968. this._popoverHelper.hidePopover();
  969. },
  970. _injectCategoryStyles: function()
  971. {
  972. var style = document.createElement("style");
  973. var categories = WebInspector.TimelinePresentationModel.categories();
  974. style.textContent = Object.values(categories).map(WebInspector.TimelinePresentationModel.createStyleRuleForCategory).join("\n");
  975. document.head.appendChild(style);
  976. },
  977. jumpToNextSearchResult: function()
  978. {
  979. this._jumpToAdjacentRecord(1);
  980. },
  981. jumpToPreviousSearchResult: function()
  982. {
  983. this._jumpToAdjacentRecord(-1);
  984. },
  985. _jumpToAdjacentRecord: function(offset)
  986. {
  987. if (!this._searchResults || !this._searchResults.length || !this._selectedSearchResult)
  988. return;
  989. var index = this._searchResults.indexOf(this._selectedSearchResult);
  990. index = (index + offset + this._searchResults.length) % this._searchResults.length;
  991. this._selectSearchResult(index);
  992. this._highlightSelectedSearchResult(true);
  993. },
  994. _selectSearchResult: function(index)
  995. {
  996. this._selectedSearchResult = this._searchResults[index];
  997. WebInspector.searchController.updateCurrentMatchIndex(index, this);
  998. },
  999. _highlightSelectedSearchResult: function(revealRecord)
  1000. {
  1001. this._clearHighlight();
  1002. if (this._searchFilter)
  1003. return;
  1004. var record = this._selectedSearchResult;
  1005. if (!record)
  1006. return;
  1007. for (var element = this._sidebarListElement.firstChild; element; element = element.nextSibling) {
  1008. if (element.row._record === record) {
  1009. element.row.highlight(this._searchRegExp, this._highlightDomChanges);
  1010. return;
  1011. }
  1012. }
  1013. if (revealRecord)
  1014. this._revealRecord(record);
  1015. },
  1016. _clearHighlight: function()
  1017. {
  1018. if (this._highlightDomChanges)
  1019. WebInspector.revertDomChanges(this._highlightDomChanges);
  1020. this._highlightDomChanges = [];
  1021. },
  1022. /**
  1023. * @param {boolean} revealRecord
  1024. */
  1025. _updateSearchHighlight: function(revealRecord)
  1026. {
  1027. if (this._searchFilter || !this._searchRegExp) {
  1028. this._clearHighlight();
  1029. return;
  1030. }
  1031. if (!this._searchResults)
  1032. this._updateSearchResults();
  1033. this._highlightSelectedSearchResult(revealRecord);
  1034. },
  1035. _updateSearchResults: function()
  1036. {
  1037. var searchRegExp = this._searchRegExp;
  1038. if (!searchRegExp)
  1039. return;
  1040. var matches = [];
  1041. var presentationModel = this._presentationModel;
  1042. function processRecord(record)
  1043. {
  1044. if (presentationModel.isVisible(record) && WebInspector.TimelineRecordListRow.testContentMatching(record, searchRegExp))
  1045. matches.push(record);
  1046. return false;
  1047. }
  1048. WebInspector.TimelinePresentationModel.forAllRecords(presentationModel.rootRecord().children, processRecord);
  1049. var matchesCount = matches.length;
  1050. if (matchesCount) {
  1051. this._searchResults = matches;
  1052. WebInspector.searchController.updateSearchMatchesCount(matchesCount, this);
  1053. var selectedIndex = matches.indexOf(this._selectedSearchResult);
  1054. if (selectedIndex === -1)
  1055. selectedIndex = 0;
  1056. this._selectSearchResult(selectedIndex);
  1057. } else {
  1058. WebInspector.searchController.updateSearchMatchesCount(0, this);
  1059. delete this._selectedSearchResult;
  1060. }
  1061. },
  1062. searchCanceled: function()
  1063. {
  1064. this._clearHighlight();
  1065. delete this._searchResults;
  1066. delete this._selectedSearchResult;
  1067. delete this._searchRegExp;
  1068. },
  1069. /**
  1070. * @return {boolean}
  1071. */
  1072. canFilter: function()
  1073. {
  1074. return true;
  1075. },
  1076. performFilter: function(searchQuery)
  1077. {
  1078. this._presentationModel.removeFilter(this._searchFilter);
  1079. delete this._searchFilter;
  1080. this.searchCanceled();
  1081. if (searchQuery) {
  1082. this._searchFilter = new WebInspector.TimelineSearchFilter(createPlainTextSearchRegex(searchQuery, "i"));
  1083. this._presentationModel.addFilter(this._searchFilter);
  1084. }
  1085. this._invalidateAndScheduleRefresh(true, true);
  1086. },
  1087. performSearch: function(searchQuery)
  1088. {
  1089. this._searchRegExp = createPlainTextSearchRegex(searchQuery, "i");
  1090. delete this._searchResults;
  1091. this._updateSearchHighlight(true);
  1092. },
  1093. __proto__: WebInspector.Panel.prototype
  1094. }
  1095. /**
  1096. * @constructor
  1097. * @param {WebInspector.TimelineModel} model
  1098. * @implements {WebInspector.TimelineGrid.Calculator}
  1099. */
  1100. WebInspector.TimelineCalculator = function(model)
  1101. {
  1102. this._model = model;
  1103. }
  1104. WebInspector.TimelineCalculator._minWidth = 5;
  1105. WebInspector.TimelineCalculator.prototype = {
  1106. /**
  1107. * @param {number} time
  1108. */
  1109. computePosition: function(time)
  1110. {
  1111. return (time - this._minimumBoundary) / this.boundarySpan() * this._workingArea + this._paddingLeft;
  1112. },
  1113. computeBarGraphPercentages: function(record)
  1114. {
  1115. var start = (record.startTime - this._minimumBoundary) / this.boundarySpan() * 100;
  1116. var end = (record.startTime + record.selfTime - this._minimumBoundary) / this.boundarySpan() * 100;
  1117. var endWithChildren = (record.lastChildEndTime - this._minimumBoundary) / this.boundarySpan() * 100;
  1118. var cpuWidth = record.coalesced ? endWithChildren - start : record.cpuTime / this.boundarySpan() * 100;
  1119. return {start: start, end: end, endWithChildren: endWithChildren, cpuWidth: cpuWidth};
  1120. },
  1121. computeBarGraphWindowPosition: function(record)
  1122. {
  1123. var percentages = this.computeBarGraphPercentages(record);
  1124. var widthAdjustment = 0;
  1125. var left = this.computePosition(record.startTime);
  1126. var width = (percentages.end - percentages.start) / 100 * this._workingArea;
  1127. if (width < WebInspector.TimelineCalculator._minWidth) {
  1128. widthAdjustment = WebInspector.TimelineCalculator._minWidth - width;
  1129. left -= widthAdjustment / 2;
  1130. width += widthAdjustment;
  1131. }
  1132. var widthWithChildren = (percentages.endWithChildren - percentages.start) / 100 * this._workingArea + widthAdjustment;
  1133. var cpuWidth = percentages.cpuWidth / 100 * this._workingArea + widthAdjustment;
  1134. if (percentages.endWithChildren > percentages.end)
  1135. widthWithChildren += widthAdjustment;
  1136. return {left: left, width: width, widthWithChildren: widthWithChildren, cpuWidth: cpuWidth};
  1137. },
  1138. setWindow: function(minimumBoundary, maximumBoundary)
  1139. {
  1140. this._minimumBoundary = minimumBoundary;
  1141. this._maximumBoundary = maximumBoundary;
  1142. },
  1143. /**
  1144. * @param {number} paddingLeft
  1145. * @param {number} clientWidth
  1146. */
  1147. setDisplayWindow: function(paddingLeft, clientWidth)
  1148. {
  1149. this._workingArea = clientWidth - WebInspector.TimelineCalculator._minWidth - paddingLeft;
  1150. this._paddingLeft = paddingLeft;
  1151. },
  1152. formatTime: function(value)
  1153. {
  1154. return Number.secondsToString(value + this._minimumBoundary - this._model.minimumRecordTime());
  1155. },
  1156. maximumBoundary: function()
  1157. {
  1158. return this._maximumBoundary;
  1159. },
  1160. minimumBoundary: function()
  1161. {
  1162. return this._minimumBoundary;
  1163. },
  1164. zeroTime: function()
  1165. {
  1166. return this._model.minimumRecordTime();
  1167. },
  1168. boundarySpan: function()
  1169. {
  1170. return this._maximumBoundary - this._minimumBoundary;
  1171. }
  1172. }
  1173. /**
  1174. * @constructor
  1175. */
  1176. WebInspector.TimelineRecordListRow = function()
  1177. {
  1178. this.element = document.createElement("div");
  1179. this.element.row = this;
  1180. this.element.style.cursor = "pointer";
  1181. var iconElement = document.createElement("span");
  1182. iconElement.className = "timeline-tree-icon";
  1183. this.element.appendChild(iconElement);
  1184. this._typeElement = document.createElement("span");
  1185. this._typeElement.className = "type";
  1186. this.element.appendChild(this._typeElement);
  1187. var separatorElement = document.createElement("span");
  1188. separatorElement.className = "separator";
  1189. separatorElement.textContent = " ";
  1190. this._dataElement = document.createElement("span");
  1191. this._dataElement.className = "data dimmed";
  1192. this.element.appendChild(separatorElement);
  1193. this.element.appendChild(this._dataElement);
  1194. }
  1195. WebInspector.TimelineRecordListRow.prototype = {
  1196. update: function(record, isEven, offset)
  1197. {
  1198. this._record = record;
  1199. this._offset = offset;
  1200. this.element.className = "timeline-tree-item timeline-category-" + record.category.name;
  1201. if (isEven)
  1202. this.element.addStyleClass("even");
  1203. if (record.hasWarning)
  1204. this.element.addStyleClass("warning");
  1205. else if (record.childHasWarning)
  1206. this.element.addStyleClass("child-warning");
  1207. if (record.isBackground)
  1208. this.element.addStyleClass("background");
  1209. this._typeElement.textContent = record.title;
  1210. if (this._dataElement.firstChild)
  1211. this._dataElement.removeChildren();
  1212. if (record.detailsNode())
  1213. this._dataElement.appendChild(record.detailsNode());
  1214. },
  1215. highlight: function(regExp, domChanges)
  1216. {
  1217. var matchInfo = this.element.textContent.match(regExp);
  1218. if (matchInfo)
  1219. WebInspector.highlightSearchResult(this.element, matchInfo.index, matchInfo[0].length, domChanges);
  1220. },
  1221. dispose: function()
  1222. {
  1223. this.element.parentElement.removeChild(this.element);
  1224. }
  1225. }
  1226. /**
  1227. * @param {!WebInspector.TimelinePresentationModel.Record} record
  1228. * @param {!RegExp} regExp
  1229. */
  1230. WebInspector.TimelineRecordListRow.testContentMatching = function(record, regExp)
  1231. {
  1232. var toSearchText = record.title;
  1233. if (record.detailsNode())
  1234. toSearchText += " " + record.detailsNode().textContent;
  1235. return regExp.test(toSearchText);
  1236. }
  1237. /**
  1238. * @constructor
  1239. */
  1240. WebInspector.TimelineRecordGraphRow = function(graphContainer, scheduleRefresh)
  1241. {
  1242. this.element = document.createElement("div");
  1243. this.element.row = this;
  1244. this._barAreaElement = document.createElement("div");
  1245. this._barAreaElement.className = "timeline-graph-bar-area";
  1246. this.element.appendChild(this._barAreaElement);
  1247. this._barWithChildrenElement = document.createElement("div");
  1248. this._barWithChildrenElement.className = "timeline-graph-bar with-children";
  1249. this._barWithChildrenElement.row = this;
  1250. this._barAreaElement.appendChild(this._barWithChildrenElement);
  1251. this._barCpuElement = document.createElement("div");
  1252. this._barCpuElement.className = "timeline-graph-bar cpu"
  1253. this._barCpuElement.row = this;
  1254. this._barAreaElement.appendChild(this._barCpuElement);
  1255. this._barElement = document.createElement("div");
  1256. this._barElement.className = "timeline-graph-bar";
  1257. this._barElement.row = this;
  1258. this._barAreaElement.appendChild(this._barElement);
  1259. this._expandElement = new WebInspector.TimelineExpandableElement(graphContainer);
  1260. this._expandElement._element.addEventListener("click", this._onClick.bind(this));
  1261. this._scheduleRefresh = scheduleRefresh;
  1262. }
  1263. WebInspector.TimelineRecordGraphRow.prototype = {
  1264. update: function(record, isEven, calculator, expandOffset, index)
  1265. {
  1266. this._record = record;
  1267. this.element.className = "timeline-graph-side timeline-category-" + record.category.name;
  1268. if (isEven)
  1269. this.element.addStyleClass("even");
  1270. if (record.isBackground)
  1271. this.element.addStyleClass("background");
  1272. var barPosition = calculator.computeBarGraphWindowPosition(record);
  1273. this._barWithChildrenElement.style.left = barPosition.left + "px";
  1274. this._barWithChildrenElement.style.width = barPosition.widthWithChildren + "px";
  1275. this._barElement.style.left = barPosition.left + "px";
  1276. this._barElement.style.width = barPosition.width + "px";
  1277. this._barCpuElement.style.left = barPosition.left + "px";
  1278. this._barCpuElement.style.width = barPosition.cpuWidth + "px";
  1279. this._expandElement._update(record, index, barPosition.left - expandOffset, barPosition.width);
  1280. },
  1281. _onClick: function(event)
  1282. {
  1283. this._record.collapsed = !this._record.collapsed;
  1284. this._scheduleRefresh(false, true);
  1285. },
  1286. dispose: function()
  1287. {
  1288. this.element.parentElement.removeChild(this.element);
  1289. this._expandElement._dispose();
  1290. }
  1291. }
  1292. /**
  1293. * @constructor
  1294. */
  1295. WebInspector.TimelineExpandableElement = function(container)
  1296. {
  1297. this._element = document.createElement("div");
  1298. this._element.className = "timeline-expandable";
  1299. var leftBorder = document.createElement("div");
  1300. leftBorder.className = "timeline-expandable-left";
  1301. this._element.appendChild(leftBorder);
  1302. container.appendChild(this._element);
  1303. }
  1304. WebInspector.TimelineExpandableElement.prototype = {
  1305. _update: function(record, index, left, width)
  1306. {
  1307. const rowHeight = WebInspector.TimelinePanel.rowHeight;
  1308. if (record.visibleChildrenCount || record.invisibleChildrenCount) {
  1309. this._element.style.top = index * rowHeight + "px";
  1310. this._element.style.left = left + "px";
  1311. this._element.style.width = Math.max(12, width + 25) + "px";
  1312. if (!record.collapsed) {
  1313. this._element.style.height = (record.visibleChildrenCount + 1) * rowHeight + "px";
  1314. this._element.addStyleClass("timeline-expandable-expanded");
  1315. this._element.removeStyleClass("timeline-expandable-collapsed");
  1316. } else {
  1317. this._element.style.height = rowHeight + "px";
  1318. this._element.addStyleClass("timeline-expandable-collapsed");
  1319. this._element.removeStyleClass("timeline-expandable-expanded");
  1320. }
  1321. this._element.removeStyleClass("hidden");
  1322. } else
  1323. this._element.addStyleClass("hidden");
  1324. },
  1325. _dispose: function()
  1326. {
  1327. this._element.parentElement.removeChild(this._element);
  1328. }
  1329. }
  1330. /**
  1331. * @constructor
  1332. * @implements {WebInspector.TimelinePresentationModel.Filter}
  1333. */
  1334. WebInspector.TimelineCategoryFilter = function()
  1335. {
  1336. }
  1337. WebInspector.TimelineCategoryFilter.prototype = {
  1338. /**
  1339. * @param {!WebInspector.TimelinePresentationModel.Record} record
  1340. * @return {boolean}
  1341. */
  1342. accept: function(record)
  1343. {
  1344. return !record.category.hidden && record.type !== WebInspector.TimelineModel.RecordType.BeginFrame;
  1345. }
  1346. }
  1347. /**
  1348. * @constructor
  1349. * @implements {WebInspector.TimelinePresentationModel.Filter}
  1350. */
  1351. WebInspector.TimelineIsLongFilter = function()
  1352. {
  1353. this._minimumRecordDuration = 0;
  1354. }
  1355. WebInspector.TimelineIsLongFilter.prototype = {
  1356. /**
  1357. * @param {number} value
  1358. */
  1359. setMinimumRecordDuration: function(value)
  1360. {
  1361. this._minimumRecordDuration = value;
  1362. },
  1363. /**
  1364. * @param {!WebInspector.TimelinePresentationModel.Record} record
  1365. * @return {boolean}
  1366. */
  1367. accept: function(record)
  1368. {
  1369. return this._minimumRecordDuration ? ((record.lastChildEndTime - record.startTime) >= this._minimumRecordDuration) : true;
  1370. }
  1371. }
  1372. /**
  1373. * @param {!RegExp} regExp
  1374. * @constructor
  1375. * @implements {WebInspector.TimelinePresentationModel.Filter}
  1376. */
  1377. WebInspector.TimelineSearchFilter = function(regExp)
  1378. {
  1379. this._regExp = regExp;
  1380. }
  1381. WebInspector.TimelineSearchFilter.prototype = {
  1382. /**
  1383. * @param {!WebInspector.TimelinePresentationModel.Record} record
  1384. * @return {boolean}
  1385. */
  1386. accept: function(record)
  1387. {
  1388. return WebInspector.TimelineRecordListRow.testContentMatching(record, this._regExp);
  1389. }
  1390. }