2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // FIXME: Rename the file.
28 WebInspector.CPUProfileView = function(profile)
30 WebInspector.View.call(this);
32 this.element.addStyleClass("profile-view");
34 this.showSelfTimeAsPercent = WebInspector.settings.createSetting("cpuProfilerShowSelfTimeAsPercent", true);
35 this.showTotalTimeAsPercent = WebInspector.settings.createSetting("cpuProfilerShowTotalTimeAsPercent", true);
36 this.showAverageTimeAsPercent = WebInspector.settings.createSetting("cpuProfilerShowAverageTimeAsPercent", true);
37 this._viewType = WebInspector.settings.createSetting("cpuProfilerView", WebInspector.CPUProfileView._TypeHeavy);
39 var columns = { "self": { title: WebInspector.UIString("Self"), width: "72px", sort: "descending", sortable: true },
40 "total": { title: WebInspector.UIString("Total"), width: "72px", sortable: true },
41 "average": { title: WebInspector.UIString("Average"), width: "72px", sortable: true },
42 "calls": { title: WebInspector.UIString("Calls"), width: "54px", sortable: true },
43 "function": { title: WebInspector.UIString("Function"), disclosure: true, sortable: true } };
45 if (Capabilities.samplingCPUProfiler) {
46 delete columns.average;
50 this.dataGrid = new WebInspector.DataGrid(columns);
51 this.dataGrid.addEventListener("sorting changed", this._sortData, this);
52 this.dataGrid.element.addEventListener("mousedown", this._mouseDownInDataGrid.bind(this), true);
53 this.dataGrid.show(this.element);
55 this.viewSelectElement = document.createElement("select");
56 this.viewSelectElement.className = "status-bar-item";
57 this.viewSelectElement.addEventListener("change", this._changeView.bind(this), false);
59 var heavyViewOption = document.createElement("option");
60 heavyViewOption.label = WebInspector.UIString("Heavy (Bottom Up)");
61 var treeViewOption = document.createElement("option");
62 treeViewOption.label = WebInspector.UIString("Tree (Top Down)");
63 this.viewSelectElement.appendChild(heavyViewOption);
64 this.viewSelectElement.appendChild(treeViewOption);
65 this.viewSelectElement.selectedIndex = this._viewType.get() === WebInspector.CPUProfileView._TypeHeavy ? 0 : 1;
67 this.percentButton = new WebInspector.StatusBarButton("", "percent-time-status-bar-item");
68 this.percentButton.addEventListener("click", this._percentClicked.bind(this), false);
70 this.focusButton = new WebInspector.StatusBarButton(WebInspector.UIString("Focus selected function."), "focus-profile-node-status-bar-item");
71 this.focusButton.disabled = true;
72 this.focusButton.addEventListener("click", this._focusClicked.bind(this), false);
74 this.excludeButton = new WebInspector.StatusBarButton(WebInspector.UIString("Exclude selected function."), "exclude-profile-node-status-bar-item");
75 this.excludeButton.disabled = true;
76 this.excludeButton.addEventListener("click", this._excludeClicked.bind(this), false);
78 this.resetButton = new WebInspector.StatusBarButton(WebInspector.UIString("Restore all functions."), "reset-profile-status-bar-item");
79 this.resetButton.visible = false;
80 this.resetButton.addEventListener("click", this._resetClicked.bind(this), false);
82 this.profile = profile;
84 function profileCallback(error, profile)
88 this.profile.head = profile.head;
89 this._assignParentsInProfile();
91 this._updatePercentButton();
94 this._linkifier = WebInspector.debuggerPresentationModel.createLinkifier(new WebInspector.DebuggerPresentationModel.DefaultLinkifierFormatter(30));
96 ProfilerAgent.getProfile(this.profile.typeId, this.profile.uid, profileCallback.bind(this));
99 WebInspector.CPUProfileView._TypeTree = "Tree";
100 WebInspector.CPUProfileView._TypeHeavy = "Heavy";
102 WebInspector.CPUProfileView.prototype = {
105 return [this.viewSelectElement, this.percentButton.element, this.focusButton.element, this.excludeButton.element, this.resetButton.element];
110 return this._profile;
115 this._profile = profile;
118 get bottomUpProfileDataGridTree()
120 if (!this._bottomUpProfileDataGridTree) {
121 if (this.profile.bottomUpHead)
122 this._bottomUpProfileDataGridTree = new WebInspector.TopDownProfileDataGridTree(this, this.profile.bottomUpHead);
124 this._bottomUpProfileDataGridTree = new WebInspector.BottomUpProfileDataGridTree(this, this.profile.head);
126 return this._bottomUpProfileDataGridTree;
129 get topDownProfileDataGridTree()
131 if (!this._topDownProfileDataGridTree)
132 this._topDownProfileDataGridTree = new WebInspector.TopDownProfileDataGridTree(this, this.profile.head);
133 return this._topDownProfileDataGridTree;
138 return this._currentTree;
141 set currentTree(tree)
143 this._currentTree = tree;
149 if (!this._topDownTree) {
150 this._topDownTree = WebInspector.TopDownTreeFactory.create(this.profile.head);
151 this._sortProfile(this._topDownTree);
154 return this._topDownTree;
159 if (!this._bottomUpTree) {
160 this._bottomUpTree = WebInspector.BottomUpTreeFactory.create(this.profile.head);
161 this._sortProfile(this._bottomUpTree);
164 return this._bottomUpTree;
169 this._currentSearchResultIndex = -1;
174 var selectedProfileNode = this.dataGrid.selectedNode ? this.dataGrid.selectedNode.profileNode : null;
176 this.dataGrid.removeChildren();
178 var children = this.profileDataGridTree.children;
179 var count = children.length;
181 for (var index = 0; index < count; ++index)
182 this.dataGrid.appendChild(children[index]);
184 if (selectedProfileNode)
185 selectedProfileNode.selected = true;
188 refreshVisibleData: function()
190 var child = this.dataGrid.children[0];
193 child = child.traverseNextNode(false, null, true);
197 refreshShowAsPercents: function()
199 this._updatePercentButton();
200 this.refreshVisibleData();
203 searchCanceled: function()
205 if (this._searchResults) {
206 for (var i = 0; i < this._searchResults.length; ++i) {
207 var profileNode = this._searchResults[i].profileNode;
209 delete profileNode._searchMatchedSelfColumn;
210 delete profileNode._searchMatchedTotalColumn;
211 delete profileNode._searchMatchedCallsColumn;
212 delete profileNode._searchMatchedFunctionColumn;
214 profileNode.refresh();
218 delete this._searchFinishedCallback;
219 this._currentSearchResultIndex = -1;
220 this._searchResults = [];
223 performSearch: function(query, finishedCallback)
225 // Call searchCanceled since it will reset everything we need before doing a new search.
226 this.searchCanceled();
228 query = query.trim();
233 this._searchFinishedCallback = finishedCallback;
235 var greaterThan = (query.indexOf(">") === 0);
236 var lessThan = (query.indexOf("<") === 0);
237 var equalTo = (query.indexOf("=") === 0 || ((greaterThan || lessThan) && query.indexOf("=") === 1));
238 var percentUnits = (query.lastIndexOf("%") === (query.length - 1));
239 var millisecondsUnits = (query.length > 2 && query.lastIndexOf("ms") === (query.length - 2));
240 var secondsUnits = (!millisecondsUnits && query.lastIndexOf("s") === (query.length - 1));
242 var queryNumber = parseFloat(query);
243 if (greaterThan || lessThan || equalTo) {
244 if (equalTo && (greaterThan || lessThan))
245 queryNumber = parseFloat(query.substring(2));
247 queryNumber = parseFloat(query.substring(1));
250 var queryNumberMilliseconds = (secondsUnits ? (queryNumber * 1000) : queryNumber);
252 // Make equalTo implicitly true if it wasn't specified there is no other operator.
253 if (!isNaN(queryNumber) && !(greaterThan || lessThan))
256 function matchesQuery(/*ProfileDataGridNode*/ profileDataGridNode)
258 delete profileDataGridNode._searchMatchedSelfColumn;
259 delete profileDataGridNode._searchMatchedTotalColumn;
260 delete profileDataGridNode._searchMatchedAverageColumn;
261 delete profileDataGridNode._searchMatchedCallsColumn;
262 delete profileDataGridNode._searchMatchedFunctionColumn;
266 if (profileDataGridNode.selfPercent < queryNumber)
267 profileDataGridNode._searchMatchedSelfColumn = true;
268 if (profileDataGridNode.totalPercent < queryNumber)
269 profileDataGridNode._searchMatchedTotalColumn = true;
270 if (profileDataGridNode.averagePercent < queryNumberMilliseconds)
271 profileDataGridNode._searchMatchedAverageColumn = true;
272 } else if (greaterThan) {
273 if (profileDataGridNode.selfPercent > queryNumber)
274 profileDataGridNode._searchMatchedSelfColumn = true;
275 if (profileDataGridNode.totalPercent > queryNumber)
276 profileDataGridNode._searchMatchedTotalColumn = true;
277 if (profileDataGridNode.averagePercent < queryNumberMilliseconds)
278 profileDataGridNode._searchMatchedAverageColumn = true;
282 if (profileDataGridNode.selfPercent == queryNumber)
283 profileDataGridNode._searchMatchedSelfColumn = true;
284 if (profileDataGridNode.totalPercent == queryNumber)
285 profileDataGridNode._searchMatchedTotalColumn = true;
286 if (profileDataGridNode.averagePercent < queryNumberMilliseconds)
287 profileDataGridNode._searchMatchedAverageColumn = true;
289 } else if (millisecondsUnits || secondsUnits) {
291 if (profileDataGridNode.selfTime < queryNumberMilliseconds)
292 profileDataGridNode._searchMatchedSelfColumn = true;
293 if (profileDataGridNode.totalTime < queryNumberMilliseconds)
294 profileDataGridNode._searchMatchedTotalColumn = true;
295 if (profileDataGridNode.averageTime < queryNumberMilliseconds)
296 profileDataGridNode._searchMatchedAverageColumn = true;
297 } else if (greaterThan) {
298 if (profileDataGridNode.selfTime > queryNumberMilliseconds)
299 profileDataGridNode._searchMatchedSelfColumn = true;
300 if (profileDataGridNode.totalTime > queryNumberMilliseconds)
301 profileDataGridNode._searchMatchedTotalColumn = true;
302 if (profileDataGridNode.averageTime > queryNumberMilliseconds)
303 profileDataGridNode._searchMatchedAverageColumn = true;
307 if (profileDataGridNode.selfTime == queryNumberMilliseconds)
308 profileDataGridNode._searchMatchedSelfColumn = true;
309 if (profileDataGridNode.totalTime == queryNumberMilliseconds)
310 profileDataGridNode._searchMatchedTotalColumn = true;
311 if (profileDataGridNode.averageTime == queryNumberMilliseconds)
312 profileDataGridNode._searchMatchedAverageColumn = true;
315 if (equalTo && profileDataGridNode.numberOfCalls == queryNumber)
316 profileDataGridNode._searchMatchedCallsColumn = true;
317 if (greaterThan && profileDataGridNode.numberOfCalls > queryNumber)
318 profileDataGridNode._searchMatchedCallsColumn = true;
319 if (lessThan && profileDataGridNode.numberOfCalls < queryNumber)
320 profileDataGridNode._searchMatchedCallsColumn = true;
323 if (profileDataGridNode.functionName.hasSubstring(query, true) || profileDataGridNode.url.hasSubstring(query, true))
324 profileDataGridNode._searchMatchedFunctionColumn = true;
326 if (profileDataGridNode._searchMatchedSelfColumn ||
327 profileDataGridNode._searchMatchedTotalColumn ||
328 profileDataGridNode._searchMatchedAverageColumn ||
329 profileDataGridNode._searchMatchedCallsColumn ||
330 profileDataGridNode._searchMatchedFunctionColumn)
332 profileDataGridNode.refresh();
339 var current = this.profileDataGridTree.children[0];
342 if (matchesQuery(current)) {
343 this._searchResults.push({ profileNode: current });
346 current = current.traverseNextNode(false, null, false);
349 finishedCallback(this, this._searchResults.length);
352 jumpToFirstSearchResult: function()
354 if (!this._searchResults || !this._searchResults.length)
356 this._currentSearchResultIndex = 0;
357 this._jumpToSearchResult(this._currentSearchResultIndex);
360 jumpToLastSearchResult: function()
362 if (!this._searchResults || !this._searchResults.length)
364 this._currentSearchResultIndex = (this._searchResults.length - 1);
365 this._jumpToSearchResult(this._currentSearchResultIndex);
368 jumpToNextSearchResult: function()
370 if (!this._searchResults || !this._searchResults.length)
372 if (++this._currentSearchResultIndex >= this._searchResults.length)
373 this._currentSearchResultIndex = 0;
374 this._jumpToSearchResult(this._currentSearchResultIndex);
377 jumpToPreviousSearchResult: function()
379 if (!this._searchResults || !this._searchResults.length)
381 if (--this._currentSearchResultIndex < 0)
382 this._currentSearchResultIndex = (this._searchResults.length - 1);
383 this._jumpToSearchResult(this._currentSearchResultIndex);
386 showingFirstSearchResult: function()
388 return (this._currentSearchResultIndex === 0);
391 showingLastSearchResult: function()
393 return (this._searchResults && this._currentSearchResultIndex === (this._searchResults.length - 1));
396 _jumpToSearchResult: function(index)
398 var searchResult = this._searchResults[index];
402 var profileNode = searchResult.profileNode;
403 profileNode.revealAndSelect();
406 _changeView: function()
411 if (this.viewSelectElement.selectedIndex == 1) {
412 this.profileDataGridTree = this.topDownProfileDataGridTree;
414 this._viewType.set(WebInspector.CPUProfileView._TypeTree);
415 } else if (this.viewSelectElement.selectedIndex == 0) {
416 this.profileDataGridTree = this.bottomUpProfileDataGridTree;
418 this._viewType.set(WebInspector.CPUProfileView._TypeHeavy);
421 if (!this.currentQuery || !this._searchFinishedCallback || !this._searchResults)
424 // The current search needs to be performed again. First negate out previous match
425 // count by calling the search finished callback with a negative number of matches.
426 // Then perform the search again the with same query and callback.
427 this._searchFinishedCallback(this, -this._searchResults.length);
428 this.performSearch(this.currentQuery, this._searchFinishedCallback);
431 _percentClicked: function(event)
433 var currentState = this.showSelfTimeAsPercent.get() && this.showTotalTimeAsPercent.get() && this.showAverageTimeAsPercent.get();
434 this.showSelfTimeAsPercent.set(!currentState);
435 this.showTotalTimeAsPercent.set(!currentState);
436 this.showAverageTimeAsPercent.set(!currentState);
437 this.refreshShowAsPercents();
440 _updatePercentButton: function()
442 if (this.showSelfTimeAsPercent.get() && this.showTotalTimeAsPercent.get() && this.showAverageTimeAsPercent.get()) {
443 this.percentButton.title = WebInspector.UIString("Show absolute total and self times.");
444 this.percentButton.toggled = true;
446 this.percentButton.title = WebInspector.UIString("Show total and self times as percentages.");
447 this.percentButton.toggled = false;
451 _focusClicked: function(event)
453 if (!this.dataGrid.selectedNode)
456 this.resetButton.visible = true;
457 this.profileDataGridTree.focus(this.dataGrid.selectedNode);
459 this.refreshVisibleData();
462 _excludeClicked: function(event)
464 var selectedNode = this.dataGrid.selectedNode
469 selectedNode.deselect();
471 this.resetButton.visible = true;
472 this.profileDataGridTree.exclude(selectedNode);
474 this.refreshVisibleData();
477 _resetClicked: function(event)
479 this.resetButton.visible = false;
480 this.profileDataGridTree.restore();
481 this._linkifier.reset();
483 this.refreshVisibleData();
486 _dataGridNodeSelected: function(node)
488 this.focusButton.disabled = false;
489 this.excludeButton.disabled = false;
492 _dataGridNodeDeselected: function(node)
494 this.focusButton.disabled = true;
495 this.excludeButton.disabled = true;
498 _sortData: function(event)
500 this._sortProfile(this.profile);
503 _sortProfile: function()
505 var sortAscending = this.dataGrid.sortOrder === "ascending";
506 var sortColumnIdentifier = this.dataGrid.sortColumnIdentifier;
508 "average": "averageTime",
510 "total": "totalTime",
511 "calls": "numberOfCalls",
512 "function": "functionName"
513 }[sortColumnIdentifier];
515 this.profileDataGridTree.sort(WebInspector.ProfileDataGridTree.propertyComparator(sortProperty, sortAscending));
520 _mouseDownInDataGrid: function(event)
522 if (event.detail < 2)
525 var cell = event.target.enclosingNodeOrSelfWithNodeName("td");
526 if (!cell || (!cell.hasStyleClass("total-column") && !cell.hasStyleClass("self-column") && !cell.hasStyleClass("average-column")))
529 if (cell.hasStyleClass("total-column"))
530 this.showTotalTimeAsPercent.set(!this.showTotalTimeAsPercent.get());
531 else if (cell.hasStyleClass("self-column"))
532 this.showSelfTimeAsPercent.set(!this.showSelfTimeAsPercent.get());
533 else if (cell.hasStyleClass("average-column"))
534 this.showAverageTimeAsPercent.set(!this.showAverageTimeAsPercent.get());
536 this.refreshShowAsPercents();
538 event.preventDefault();
539 event.stopPropagation();
542 _assignParentsInProfile: function()
544 var head = this.profile.head;
547 var nodesToTraverse = [ { parent: head, children: head.children } ];
548 while (nodesToTraverse.length > 0) {
549 var pair = nodesToTraverse.shift();
550 var parent = pair.parent;
551 var children = pair.children;
552 var length = children.length;
553 for (var i = 0; i < length; ++i) {
554 children[i].head = head;
555 children[i].parent = parent;
556 if (children[i].children.length > 0)
557 nodesToTraverse.push({ parent: children[i], children: children[i].children });
563 WebInspector.CPUProfileView.prototype.__proto__ = WebInspector.View.prototype;
565 WebInspector.CPUProfileType = function()
567 WebInspector.ProfileType.call(this, WebInspector.CPUProfileType.TypeId, WebInspector.UIString("CPU PROFILES"));
568 this._recording = false;
569 WebInspector.CPUProfileType.instance = this;
572 WebInspector.CPUProfileType.TypeId = "CPU";
574 WebInspector.CPUProfileType.prototype = {
577 return this._recording ? WebInspector.UIString("Stop profiling.") : WebInspector.UIString("Start profiling.");
582 return this._recording ? "record-profile-status-bar-item status-bar-item toggled-on" : "record-profile-status-bar-item status-bar-item";
585 buttonClicked: function()
587 this._recording = !this._recording;
590 ProfilerAgent.start();
592 ProfilerAgent.stop();
597 return WebInspector.UIString("Control CPU profiling by pressing the %s button on the status bar.");
600 isRecordingProfile: function()
602 return this._recording;
605 startRecordingProfile: function()
607 this._recording = true;
608 ProfilerAgent.start();
611 stopRecordingProfile: function()
613 this._recording = false;
614 ProfilerAgent.stop();
617 setRecordingProfile: function(isProfiling)
619 this._recording = isProfiling;
622 createSidebarTreeElementForProfile: function(profile)
624 return new WebInspector.ProfileSidebarTreeElement(profile, WebInspector.UIString("Profile %d"), "profile-sidebar-tree-item");
627 createView: function(profile)
629 return new WebInspector.CPUProfileView(profile);
633 WebInspector.CPUProfileType.prototype.__proto__ = WebInspector.ProfileType.prototype;