| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 WebInspector.CPUProfileNode = class extends WebInspector.ProfileNode { | 7 SDK.CPUProfileNode = class extends SDK.ProfileNode { |
| 8 /** | 8 /** |
| 9 * @param {!Protocol.Profiler.ProfileNode} node | 9 * @param {!Protocol.Profiler.ProfileNode} node |
| 10 * @param {number} sampleTime | 10 * @param {number} sampleTime |
| 11 */ | 11 */ |
| 12 constructor(node, sampleTime) { | 12 constructor(node, sampleTime) { |
| 13 var callFrame = node.callFrame || /** @type {!Protocol.Runtime.CallFrame} */
({ | 13 var callFrame = node.callFrame || /** @type {!Protocol.Runtime.CallFrame} */
({ |
| 14 // Backward compatibility for old SamplingHeapProfileNode
format. | 14 // Backward compatibility for old SamplingHeapProfileNode
format. |
| 15 functionName: node['functionName'], | 15 functionName: node['functionName'], |
| 16 scriptId: node['scriptId'], | 16 scriptId: node['scriptId'], |
| 17 url: node['url'], | 17 url: node['url'], |
| 18 lineNumber: node['lineNumber'] - 1, | 18 lineNumber: node['lineNumber'] - 1, |
| 19 columnNumber: node['columnNumber'] - 1 | 19 columnNumber: node['columnNumber'] - 1 |
| 20 }); | 20 }); |
| 21 super(callFrame); | 21 super(callFrame); |
| 22 this.id = node.id; | 22 this.id = node.id; |
| 23 this.self = node.hitCount * sampleTime; | 23 this.self = node.hitCount * sampleTime; |
| 24 this.positionTicks = node.positionTicks; | 24 this.positionTicks = node.positionTicks; |
| 25 // Compatibility: legacy backends could provide "no reason" for optimized fu
nctions. | 25 // Compatibility: legacy backends could provide "no reason" for optimized fu
nctions. |
| 26 this.deoptReason = node.deoptReason && node.deoptReason !== 'no reason' ? no
de.deoptReason : null; | 26 this.deoptReason = node.deoptReason && node.deoptReason !== 'no reason' ? no
de.deoptReason : null; |
| 27 } | 27 } |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * @unrestricted | 31 * @unrestricted |
| 32 */ | 32 */ |
| 33 WebInspector.CPUProfileDataModel = class extends WebInspector.ProfileTreeModel { | 33 SDK.CPUProfileDataModel = class extends SDK.ProfileTreeModel { |
| 34 /** | 34 /** |
| 35 * @param {!Protocol.Profiler.Profile} profile | 35 * @param {!Protocol.Profiler.Profile} profile |
| 36 */ | 36 */ |
| 37 constructor(profile) { | 37 constructor(profile) { |
| 38 super(); | 38 super(); |
| 39 var isLegacyFormat = !!profile['head']; | 39 var isLegacyFormat = !!profile['head']; |
| 40 if (isLegacyFormat) { | 40 if (isLegacyFormat) { |
| 41 // Legacy format contains raw timestamps and start/stop times are in secon
ds. | 41 // Legacy format contains raw timestamps and start/stop times are in secon
ds. |
| 42 this.profileStartTime = profile.startTime * 1000; | 42 this.profileStartTime = profile.startTime * 1000; |
| 43 this.profileEndTime = profile.endTime * 1000; | 43 this.profileEndTime = profile.endTime * 1000; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 var timestamps = new Array(profile.timeDeltas.length); | 94 var timestamps = new Array(profile.timeDeltas.length); |
| 95 for (var i = 0; i < timestamps.length; ++i) { | 95 for (var i = 0; i < timestamps.length; ++i) { |
| 96 lastTimeUsec += profile.timeDeltas[i]; | 96 lastTimeUsec += profile.timeDeltas[i]; |
| 97 timestamps[i] = lastTimeUsec; | 97 timestamps[i] = lastTimeUsec; |
| 98 } | 98 } |
| 99 return timestamps; | 99 return timestamps; |
| 100 } | 100 } |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * @param {!Array<!Protocol.Profiler.ProfileNode>} nodes | 103 * @param {!Array<!Protocol.Profiler.ProfileNode>} nodes |
| 104 * @return {!WebInspector.CPUProfileNode} | 104 * @return {!SDK.CPUProfileNode} |
| 105 */ | 105 */ |
| 106 _translateProfileTree(nodes) { | 106 _translateProfileTree(nodes) { |
| 107 /** | 107 /** |
| 108 * @param {!Protocol.Profiler.ProfileNode} node | 108 * @param {!Protocol.Profiler.ProfileNode} node |
| 109 * @return {boolean} | 109 * @return {boolean} |
| 110 */ | 110 */ |
| 111 function isNativeNode(node) { | 111 function isNativeNode(node) { |
| 112 if (node.callFrame) | 112 if (node.callFrame) |
| 113 return !!node.callFrame.url && node.callFrame.url.startsWith('native '); | 113 return !!node.callFrame.url && node.callFrame.url.startsWith('native '); |
| 114 return !!node['url'] && node['url'].startsWith('native '); | 114 return !!node['url'] && node['url'].startsWith('native '); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 131 } | 131 } |
| 132 /** @type {!Map<number, !Protocol.Profiler.ProfileNode>} */ | 132 /** @type {!Map<number, !Protocol.Profiler.ProfileNode>} */ |
| 133 var nodeByIdMap = new Map(); | 133 var nodeByIdMap = new Map(); |
| 134 for (var i = 0; i < nodes.length; ++i) { | 134 for (var i = 0; i < nodes.length; ++i) { |
| 135 var node = nodes[i]; | 135 var node = nodes[i]; |
| 136 nodeByIdMap.set(node.id, node); | 136 nodeByIdMap.set(node.id, node); |
| 137 } | 137 } |
| 138 buildChildrenFromParents(nodes); | 138 buildChildrenFromParents(nodes); |
| 139 this.totalHitCount = nodes.reduce((acc, node) => acc + node.hitCount, 0); | 139 this.totalHitCount = nodes.reduce((acc, node) => acc + node.hitCount, 0); |
| 140 var sampleTime = (this.profileEndTime - this.profileStartTime) / this.totalH
itCount; | 140 var sampleTime = (this.profileEndTime - this.profileStartTime) / this.totalH
itCount; |
| 141 var keepNatives = !!WebInspector.moduleSetting('showNativeFunctionsInJSProfi
le').get(); | 141 var keepNatives = !!Common.moduleSetting('showNativeFunctionsInJSProfile').g
et(); |
| 142 var root = nodes[0]; | 142 var root = nodes[0]; |
| 143 /** @type {!Map<number, number>} */ | 143 /** @type {!Map<number, number>} */ |
| 144 var idMap = new Map([[root.id, root.id]]); | 144 var idMap = new Map([[root.id, root.id]]); |
| 145 var resultRoot = new WebInspector.CPUProfileNode(root, sampleTime); | 145 var resultRoot = new SDK.CPUProfileNode(root, sampleTime); |
| 146 var parentNodeStack = root.children.map(() => resultRoot); | 146 var parentNodeStack = root.children.map(() => resultRoot); |
| 147 var sourceNodeStack = root.children.map(id => nodeByIdMap.get(id)); | 147 var sourceNodeStack = root.children.map(id => nodeByIdMap.get(id)); |
| 148 while (sourceNodeStack.length) { | 148 while (sourceNodeStack.length) { |
| 149 var parentNode = parentNodeStack.pop(); | 149 var parentNode = parentNodeStack.pop(); |
| 150 var sourceNode = sourceNodeStack.pop(); | 150 var sourceNode = sourceNodeStack.pop(); |
| 151 if (!sourceNode.children) | 151 if (!sourceNode.children) |
| 152 sourceNode.children = []; | 152 sourceNode.children = []; |
| 153 var targetNode = new WebInspector.CPUProfileNode(sourceNode, sampleTime); | 153 var targetNode = new SDK.CPUProfileNode(sourceNode, sampleTime); |
| 154 if (keepNatives || !isNativeNode(sourceNode)) { | 154 if (keepNatives || !isNativeNode(sourceNode)) { |
| 155 parentNode.children.push(targetNode); | 155 parentNode.children.push(targetNode); |
| 156 parentNode = targetNode; | 156 parentNode = targetNode; |
| 157 } else { | 157 } else { |
| 158 parentNode.self += targetNode.self; | 158 parentNode.self += targetNode.self; |
| 159 } | 159 } |
| 160 idMap.set(sourceNode.id, parentNode.id); | 160 idMap.set(sourceNode.id, parentNode.id); |
| 161 parentNodeStack.push.apply(parentNodeStack, sourceNode.children.map(() =>
parentNode)); | 161 parentNodeStack.push.apply(parentNodeStack, sourceNode.children.map(() =>
parentNode)); |
| 162 sourceNodeStack.push.apply(sourceNodeStack, sourceNode.children.map(id =>
nodeByIdMap.get(id))); | 162 sourceNodeStack.push.apply(sourceNodeStack, sourceNode.children.map(id =>
nodeByIdMap.get(id))); |
| 163 } | 163 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 for (var i = 0; i < timestamps.length; ++i) | 211 for (var i = 0; i < timestamps.length; ++i) |
| 212 timestamps[i] /= 1000; | 212 timestamps[i] /= 1000; |
| 213 var averageSample = (timestamps.peekLast() - timestamps[0]) / (timestamps.le
ngth - 1); | 213 var averageSample = (timestamps.peekLast() - timestamps[0]) / (timestamps.le
ngth - 1); |
| 214 // Add an extra timestamp used to calculate the last sample duration. | 214 // Add an extra timestamp used to calculate the last sample duration. |
| 215 this.timestamps.push(timestamps.peekLast() + averageSample); | 215 this.timestamps.push(timestamps.peekLast() + averageSample); |
| 216 this.profileStartTime = timestamps[0]; | 216 this.profileStartTime = timestamps[0]; |
| 217 this.profileEndTime = timestamps.peekLast(); | 217 this.profileEndTime = timestamps.peekLast(); |
| 218 } | 218 } |
| 219 | 219 |
| 220 _buildIdToNodeMap() { | 220 _buildIdToNodeMap() { |
| 221 /** @type {!Map<number, !WebInspector.CPUProfileNode>} */ | 221 /** @type {!Map<number, !SDK.CPUProfileNode>} */ |
| 222 this._idToNode = new Map(); | 222 this._idToNode = new Map(); |
| 223 var idToNode = this._idToNode; | 223 var idToNode = this._idToNode; |
| 224 var stack = [this.profileHead]; | 224 var stack = [this.profileHead]; |
| 225 while (stack.length) { | 225 while (stack.length) { |
| 226 var node = stack.pop(); | 226 var node = stack.pop(); |
| 227 idToNode.set(node.id, node); | 227 idToNode.set(node.id, node); |
| 228 stack.push.apply(stack, node.children); | 228 stack.push.apply(stack, node.children); |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 | 231 |
| 232 _extractMetaNodes() { | 232 _extractMetaNodes() { |
| 233 var topLevelNodes = this.profileHead.children; | 233 var topLevelNodes = this.profileHead.children; |
| 234 for (var i = 0; i < topLevelNodes.length && !(this.gcNode && this.programNod
e && this.idleNode); i++) { | 234 for (var i = 0; i < topLevelNodes.length && !(this.gcNode && this.programNod
e && this.idleNode); i++) { |
| 235 var node = topLevelNodes[i]; | 235 var node = topLevelNodes[i]; |
| 236 if (node.functionName === '(garbage collector)') | 236 if (node.functionName === '(garbage collector)') |
| 237 this.gcNode = node; | 237 this.gcNode = node; |
| 238 else if (node.functionName === '(program)') | 238 else if (node.functionName === '(program)') |
| 239 this.programNode = node; | 239 this.programNode = node; |
| 240 else if (node.functionName === '(idle)') | 240 else if (node.functionName === '(idle)') |
| 241 this.idleNode = node; | 241 this.idleNode = node; |
| 242 } | 242 } |
| 243 } | 243 } |
| 244 | 244 |
| 245 /** | 245 /** |
| 246 * @param {function(number, !WebInspector.CPUProfileNode, number)} openFrameCa
llback | 246 * @param {function(number, !SDK.CPUProfileNode, number)} openFrameCallback |
| 247 * @param {function(number, !WebInspector.CPUProfileNode, number, number, numb
er)} closeFrameCallback | 247 * @param {function(number, !SDK.CPUProfileNode, number, number, number)} clos
eFrameCallback |
| 248 * @param {number=} startTime | 248 * @param {number=} startTime |
| 249 * @param {number=} stopTime | 249 * @param {number=} stopTime |
| 250 */ | 250 */ |
| 251 forEachFrame(openFrameCallback, closeFrameCallback, startTime, stopTime) { | 251 forEachFrame(openFrameCallback, closeFrameCallback, startTime, stopTime) { |
| 252 if (!this.profileHead || !this.samples) | 252 if (!this.profileHead || !this.samples) |
| 253 return; | 253 return; |
| 254 | 254 |
| 255 startTime = startTime || 0; | 255 startTime = startTime || 0; |
| 256 stopTime = stopTime || Infinity; | 256 stopTime = stopTime || Infinity; |
| 257 var samples = this.samples; | 257 var samples = this.samples; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 stackNodes.push(node); | 308 stackNodes.push(node); |
| 309 node = node.parent; | 309 node = node.parent; |
| 310 } | 310 } |
| 311 | 311 |
| 312 // Go down to the LCA and close current intervals. | 312 // Go down to the LCA and close current intervals. |
| 313 while (prevNode !== node) { | 313 while (prevNode !== node) { |
| 314 var start = stackStartTimes[stackTop]; | 314 var start = stackStartTimes[stackTop]; |
| 315 var duration = sampleTime - start; | 315 var duration = sampleTime - start; |
| 316 stackChildrenDuration[stackTop - 1] += duration; | 316 stackChildrenDuration[stackTop - 1] += duration; |
| 317 closeFrameCallback( | 317 closeFrameCallback( |
| 318 prevNode.depth, /** @type {!WebInspector.CPUProfileNode} */ (prevNod
e), start, duration, | 318 prevNode.depth, /** @type {!SDK.CPUProfileNode} */ (prevNode), start
, duration, |
| 319 duration - stackChildrenDuration[stackTop]); | 319 duration - stackChildrenDuration[stackTop]); |
| 320 --stackTop; | 320 --stackTop; |
| 321 if (node.depth === prevNode.depth) { | 321 if (node.depth === prevNode.depth) { |
| 322 stackNodes.push(node); | 322 stackNodes.push(node); |
| 323 node = node.parent; | 323 node = node.parent; |
| 324 } | 324 } |
| 325 prevNode = prevNode.parent; | 325 prevNode = prevNode.parent; |
| 326 } | 326 } |
| 327 | 327 |
| 328 // Go up the nodes stack and open new intervals. | 328 // Go up the nodes stack and open new intervals. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 342 stackChildrenDuration[stackTop - 1] += duration; | 342 stackChildrenDuration[stackTop - 1] += duration; |
| 343 closeFrameCallback(gcParentNode.depth + 1, node, start, duration, duration
- stackChildrenDuration[stackTop]); | 343 closeFrameCallback(gcParentNode.depth + 1, node, start, duration, duration
- stackChildrenDuration[stackTop]); |
| 344 --stackTop; | 344 --stackTop; |
| 345 } | 345 } |
| 346 | 346 |
| 347 for (var node = idToNode.get(prevId); node.parent; node = node.parent) { | 347 for (var node = idToNode.get(prevId); node.parent; node = node.parent) { |
| 348 var start = stackStartTimes[stackTop]; | 348 var start = stackStartTimes[stackTop]; |
| 349 var duration = sampleTime - start; | 349 var duration = sampleTime - start; |
| 350 stackChildrenDuration[stackTop - 1] += duration; | 350 stackChildrenDuration[stackTop - 1] += duration; |
| 351 closeFrameCallback( | 351 closeFrameCallback( |
| 352 node.depth, /** @type {!WebInspector.CPUProfileNode} */ (node), start,
duration, | 352 node.depth, /** @type {!SDK.CPUProfileNode} */ (node), start, duration
, |
| 353 duration - stackChildrenDuration[stackTop]); | 353 duration - stackChildrenDuration[stackTop]); |
| 354 --stackTop; | 354 --stackTop; |
| 355 } | 355 } |
| 356 } | 356 } |
| 357 | 357 |
| 358 /** | 358 /** |
| 359 * @param {number} index | 359 * @param {number} index |
| 360 * @return {?WebInspector.CPUProfileNode} | 360 * @return {?SDK.CPUProfileNode} |
| 361 */ | 361 */ |
| 362 nodeByIndex(index) { | 362 nodeByIndex(index) { |
| 363 return this._idToNode.get(this.samples[index]) || null; | 363 return this._idToNode.get(this.samples[index]) || null; |
| 364 } | 364 } |
| 365 }; | 365 }; |
| OLD | NEW |