Commit d6f045cf authored by Alexei Filippov's avatar Alexei Filippov Committed by Commit Bot

DevTools: Fix hitCount calculation for CPU profiles.

BUG=890467

Change-Id: Ie0e7fc413c068a83c9e225c63bc3e3abdebf3120
Reviewed-on: https://chromium-review.googlesource.com/1252636
Commit-Queue: Alexei Filippov <alph@chromium.org>
Reviewed-by: default avatarAleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: default avatarPaul Irish <paulirish@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595536}
parent 787c7e6c
Tests profile without hitCounts is parsed correctly.
Profile tree:
(root) id:1 total:5 self:0 depth:-1
(garbage collector) id:2 total:1 self:1 depth:0
foo id:3 total:4 self:1 depth:0
bar id:4 total:3 self:3 depth:1
samples: 4, 4, 3, 4, 2
timestamps: 1.5, 1.75, 2.75, 3, 4, 4.625
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(async function() {
TestRunner.addResult(`Tests profile without hitCounts is parsed correctly.\n`);
await TestRunner.loadModule('cpu_profiler_test_runner');
var profile = {
startTime: 1000,
endTime: 6000,
nodes: [
{id: 1, callFrame: {functionName: '(root)'}, children: [2,3]},
{id: 2, callFrame: {functionName: '(garbage collector)'}},
{id: 3, callFrame: {functionName: 'foo'}, children: [4]},
{id: 4, callFrame: {functionName: 'bar'}}
],
timeDeltas: [500, 250, 1000, 250, 1000],
samples: [4, 4, 3, 4, 2]
};
var model = new SDK.CPUProfileDataModel(profile);
TestRunner.addResult('Profile tree:');
printTree(' ', model.profileHead);
function printTree(padding, node) {
TestRunner.addResult(
`${padding}${node.functionName} id:${node.id} total:${node.total} self:${node.self} depth:${node.depth}`);
node.children.sort((a, b) => a.id - b.id).forEach(printTree.bind(null, padding + ' '));
}
TestRunner.addResult('\nsamples: ' + model.samples.join(', '));
TestRunner.addResult('timestamps: ' + model.timestamps.join(', '));
TestRunner.completeTest();
})();
...@@ -114,6 +114,7 @@ SDK.CPUProfileDataModel = class extends SDK.ProfileTreeModel { ...@@ -114,6 +114,7 @@ SDK.CPUProfileDataModel = class extends SDK.ProfileTreeModel {
return !!node.callFrame.url && node.callFrame.url.startsWith('native '); return !!node.callFrame.url && node.callFrame.url.startsWith('native ');
return !!node['url'] && node['url'].startsWith('native '); return !!node['url'] && node['url'].startsWith('native ');
} }
/** /**
* @param {!Array<!Protocol.Profiler.ProfileNode>} nodes * @param {!Array<!Protocol.Profiler.ProfileNode>} nodes
*/ */
...@@ -130,12 +131,29 @@ SDK.CPUProfileDataModel = class extends SDK.ProfileTreeModel { ...@@ -130,12 +131,29 @@ SDK.CPUProfileDataModel = class extends SDK.ProfileTreeModel {
parentNode.children = [node.id]; parentNode.children = [node.id];
} }
} }
/**
* @param {!Array<!Protocol.Profiler.ProfileNode>} nodes
* @param {!Array<number>|undefined} samples
*/
function buildHitCountFromSamples(nodes, samples) {
if (typeof(nodes[0].hitCount) === 'number')
return;
console.assert(samples, 'Error: Neither hitCount nor samples are present in profile.');
for (let i = 0; i < nodes.length; ++i)
nodes[i].hitCount = 0;
for (let i = 0; i < samples.length; ++i)
++nodeByIdMap.get(samples[i]).hitCount;
}
/** @type {!Map<number, !Protocol.Profiler.ProfileNode>} */ /** @type {!Map<number, !Protocol.Profiler.ProfileNode>} */
const nodeByIdMap = new Map(); const nodeByIdMap = new Map();
for (let i = 0; i < nodes.length; ++i) { for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i]; const node = nodes[i];
nodeByIdMap.set(node.id, node); nodeByIdMap.set(node.id, node);
} }
buildHitCountFromSamples(nodes, this.samples);
buildChildrenFromParents(nodes); buildChildrenFromParents(nodes);
this.totalHitCount = nodes.reduce((acc, node) => acc + node.hitCount, 0); this.totalHitCount = nodes.reduce((acc, node) => acc + node.hitCount, 0);
const sampleTime = (this.profileEndTime - this.profileStartTime) / this.totalHitCount; const sampleTime = (this.profileEndTime - this.profileStartTime) / this.totalHitCount;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment