2020-09-28 13:50:56 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
import { CodeMap, CodeEntry } from "./codemap.mjs";
|
2020-09-28 13:50:56 +00:00
|
|
|
import { ConsArray } from "./consarray.mjs";
|
2021-06-22 13:32:53 +00:00
|
|
|
import { WebInspector } from "./sourcemap.mjs";
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-30 10:53:12 +00:00
|
|
|
// Used to associate log entries with source positions in scripts.
|
2020-09-28 13:50:56 +00:00
|
|
|
// TODO: move to separate modules
|
|
|
|
export class SourcePosition {
|
2021-06-22 13:32:53 +00:00
|
|
|
script = null;
|
|
|
|
line = -1;
|
|
|
|
column = -1;
|
|
|
|
entries = [];
|
|
|
|
isFunction = false;
|
|
|
|
originalPosition = undefined;
|
|
|
|
|
2020-09-28 13:50:56 +00:00
|
|
|
constructor(script, line, column) {
|
|
|
|
this.script = script;
|
|
|
|
this.line = line;
|
|
|
|
this.column = column;
|
|
|
|
}
|
2020-12-07 08:44:54 +00:00
|
|
|
|
2020-09-28 13:50:56 +00:00
|
|
|
addEntry(entry) {
|
|
|
|
this.entries.push(entry);
|
|
|
|
}
|
2020-12-07 08:44:54 +00:00
|
|
|
|
|
|
|
toString() {
|
2021-01-12 14:36:02 +00:00
|
|
|
return `${this.script.name}:${this.line}:${this.column}`;
|
2020-12-07 08:44:54 +00:00
|
|
|
}
|
2021-06-15 11:19:00 +00:00
|
|
|
|
|
|
|
get functionPosition() {
|
|
|
|
// TODO(cbruni)
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
get toolTipDict() {
|
|
|
|
return {
|
|
|
|
title: this.toString(),
|
|
|
|
__this__: this,
|
|
|
|
script: this.script,
|
2021-06-15 21:27:47 +00:00
|
|
|
entries: this.entries,
|
2021-06-15 11:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Script {
|
2021-06-07 12:57:16 +00:00
|
|
|
url;
|
2021-11-18 09:53:32 +00:00
|
|
|
source = "";
|
2021-06-07 12:57:16 +00:00
|
|
|
name;
|
2021-06-22 13:32:53 +00:00
|
|
|
sourcePosition = undefined;
|
2020-11-30 10:53:12 +00:00
|
|
|
// Map<line, Map<column, SourcePosition>>
|
|
|
|
lineToColumn = new Map();
|
2020-12-03 17:48:45 +00:00
|
|
|
_entries = [];
|
2021-06-22 13:32:53 +00:00
|
|
|
_sourceMapState = "unknown";
|
2020-11-30 10:53:12 +00:00
|
|
|
|
|
|
|
constructor(id) {
|
2020-09-28 13:50:56 +00:00
|
|
|
this.id = id;
|
2020-11-30 10:53:12 +00:00
|
|
|
this.sourcePositions = [];
|
|
|
|
}
|
|
|
|
|
2021-05-25 08:49:11 +00:00
|
|
|
update(url, source) {
|
|
|
|
this.url = url;
|
|
|
|
this.name = Script.getShortestUniqueName(url, this);
|
2020-09-28 13:50:56 +00:00
|
|
|
this.source = source;
|
|
|
|
}
|
|
|
|
|
2020-11-26 17:02:36 +00:00
|
|
|
get length() {
|
|
|
|
return this.source.length;
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:48:45 +00:00
|
|
|
get entries() {
|
|
|
|
return this._entries;
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:32:53 +00:00
|
|
|
get startLine() {
|
|
|
|
return this.sourcePosition?.line ?? 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get sourceMapState() {
|
|
|
|
return this._sourceMapState;
|
|
|
|
}
|
|
|
|
|
2021-06-15 11:19:00 +00:00
|
|
|
findFunctionSourcePosition(sourcePosition) {
|
2021-11-18 09:53:32 +00:00
|
|
|
// TODO(cbruni): implement
|
2021-06-15 11:19:00 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-09-28 13:50:56 +00:00
|
|
|
addSourcePosition(line, column, entry) {
|
|
|
|
let sourcePosition = this.lineToColumn.get(line)?.get(column);
|
|
|
|
if (sourcePosition === undefined) {
|
2021-01-12 14:36:02 +00:00
|
|
|
sourcePosition = new SourcePosition(this, line, column,)
|
2020-10-19 10:45:42 +00:00
|
|
|
this._addSourcePosition(line, column, sourcePosition);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2021-09-07 16:21:12 +00:00
|
|
|
if (this.sourcePosition === undefined && entry.entry?.type === "Script") {
|
|
|
|
// Mark the source position of scripts, for inline scripts which don't
|
|
|
|
// start at line 1.
|
2021-06-22 13:32:53 +00:00
|
|
|
this.sourcePosition = sourcePosition;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
sourcePosition.addEntry(entry);
|
2020-12-03 17:48:45 +00:00
|
|
|
this._entries.push(entry);
|
2020-09-28 13:50:56 +00:00
|
|
|
return sourcePosition;
|
|
|
|
}
|
|
|
|
|
2020-10-19 10:45:42 +00:00
|
|
|
_addSourcePosition(line, column, sourcePosition) {
|
2020-09-28 13:50:56 +00:00
|
|
|
let columnToSourcePosition;
|
|
|
|
if (this.lineToColumn.has(line)) {
|
|
|
|
columnToSourcePosition = this.lineToColumn.get(line);
|
|
|
|
} else {
|
|
|
|
columnToSourcePosition = new Map();
|
|
|
|
this.lineToColumn.set(line, columnToSourcePosition);
|
|
|
|
}
|
|
|
|
this.sourcePositions.push(sourcePosition);
|
|
|
|
columnToSourcePosition.set(column, sourcePosition);
|
|
|
|
}
|
2020-12-03 17:48:45 +00:00
|
|
|
|
2020-12-07 08:44:54 +00:00
|
|
|
toString() {
|
2020-12-09 08:01:26 +00:00
|
|
|
return `Script(${this.id}): ${this.name}`;
|
2020-12-07 08:44:54 +00:00
|
|
|
}
|
2020-12-03 17:48:45 +00:00
|
|
|
|
2021-05-25 08:49:11 +00:00
|
|
|
get toolTipDict() {
|
|
|
|
return {
|
|
|
|
title: this.toString(),
|
|
|
|
__this__: this,
|
|
|
|
id: this.id,
|
|
|
|
url: this.url,
|
|
|
|
source: this.source,
|
2021-06-15 21:27:47 +00:00
|
|
|
sourcePositions: this.sourcePositions
|
2021-05-25 08:49:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getShortestUniqueName(url, script) {
|
|
|
|
const parts = url.split('/');
|
|
|
|
const filename = parts[parts.length -1];
|
|
|
|
const dict = this._dict ?? (this._dict = new Map());
|
|
|
|
const matchingScripts = dict.get(filename);
|
|
|
|
if (matchingScripts == undefined) {
|
|
|
|
dict.set(filename, [script]);
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
// TODO: find shortest unique substring
|
|
|
|
// Update all matching scripts to have a unique filename again.
|
|
|
|
for (let matchingScript of matchingScripts) {
|
|
|
|
matchingScript.name = script.url
|
|
|
|
}
|
|
|
|
matchingScripts.push(script);
|
|
|
|
return url;
|
2020-12-07 08:44:54 +00:00
|
|
|
}
|
2021-06-22 13:32:53 +00:00
|
|
|
|
|
|
|
ensureSourceMapCalculated(sourceMapFetchPrefix=undefined) {
|
|
|
|
if (this._sourceMapState !== "unknown") return;
|
|
|
|
|
|
|
|
const sourceMapURLMatch =
|
|
|
|
this.source.match(/\/\/# sourceMappingURL=(.*)\n/);
|
|
|
|
if (!sourceMapURLMatch) {
|
|
|
|
this._sourceMapState = "none";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._sourceMapState = "loading";
|
|
|
|
let sourceMapURL = sourceMapURLMatch[1];
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
let sourceMapPayload;
|
2022-07-19 13:11:01 +00:00
|
|
|
const options = { timeout: 15 };
|
2021-06-22 13:32:53 +00:00
|
|
|
try {
|
2022-07-19 13:11:01 +00:00
|
|
|
sourceMapPayload = await fetch(sourceMapURL, options);
|
2021-06-22 13:32:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TypeError && sourceMapFetchPrefix) {
|
|
|
|
// Try again with fetch prefix.
|
|
|
|
// TODO(leszeks): Remove the retry once the prefix is
|
|
|
|
// configurable.
|
|
|
|
sourceMapPayload =
|
2022-07-19 13:11:01 +00:00
|
|
|
await fetch(sourceMapFetchPrefix + sourceMapURL, options);
|
2021-06-22 13:32:53 +00:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sourceMapPayload = await sourceMapPayload.text();
|
|
|
|
|
|
|
|
if (sourceMapPayload.startsWith(')]}')) {
|
|
|
|
sourceMapPayload =
|
|
|
|
sourceMapPayload.substring(sourceMapPayload.indexOf('\n'));
|
|
|
|
}
|
|
|
|
sourceMapPayload = JSON.parse(sourceMapPayload);
|
|
|
|
const sourceMap =
|
|
|
|
new WebInspector.SourceMap(sourceMapURL, sourceMapPayload);
|
|
|
|
|
|
|
|
const startLine = this.startLine;
|
|
|
|
for (const sourcePosition of this.sourcePositions) {
|
|
|
|
const line = sourcePosition.line - startLine;
|
|
|
|
const column = sourcePosition.column - 1;
|
|
|
|
const mapping = sourceMap.findEntry(line, column);
|
|
|
|
if (mapping) {
|
|
|
|
sourcePosition.originalPosition = {
|
|
|
|
source: new URL(mapping[2], sourceMapURL).href,
|
|
|
|
line: mapping[3] + 1,
|
|
|
|
column: mapping[4] + 1
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
sourcePosition.originalPosition = {source: null, line:0, column:0};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._sourceMapState = "loaded";
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this._sourceMapState = "failed";
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 10:53:12 +00:00
|
|
|
|
2022-04-08 18:54:33 +00:00
|
|
|
const kOffsetPairRegex = /C([0-9]+)O([0-9]+)/g;
|
|
|
|
class SourcePositionTable {
|
|
|
|
constructor(encodedTable) {
|
|
|
|
this._offsets = [];
|
|
|
|
while (true) {
|
|
|
|
const regexResult = kOffsetPairRegex.exec(encodedTable);
|
|
|
|
if (!regexResult) break;
|
|
|
|
const codeOffset = parseInt(regexResult[1]);
|
|
|
|
const scriptOffset = parseInt(regexResult[2]);
|
|
|
|
if (isNaN(codeOffset) || isNaN(scriptOffset)) continue;
|
|
|
|
this._offsets.push({code: codeOffset, script: scriptOffset});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptOffset(codeOffset) {
|
|
|
|
if (codeOffset < 0) {
|
|
|
|
throw new Exception(`Invalid codeOffset=${codeOffset}, should be >= 0`);
|
|
|
|
}
|
|
|
|
for (let i = this.offsetTable.length - 1; i >= 0; i--) {
|
|
|
|
const offset = this._offsets[i];
|
|
|
|
if (offset.code <= codeOffset) {
|
|
|
|
return offset.script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._offsets[0].script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-03 17:48:45 +00:00
|
|
|
class SourceInfo {
|
2021-01-12 14:36:02 +00:00
|
|
|
script;
|
|
|
|
start;
|
|
|
|
end;
|
|
|
|
positions;
|
|
|
|
inlined;
|
|
|
|
fns;
|
|
|
|
disassemble;
|
2020-12-01 19:37:17 +00:00
|
|
|
|
2022-04-08 18:54:33 +00:00
|
|
|
setSourcePositionInfo(
|
|
|
|
script, startPos, endPos, sourcePositionTableData, inliningPositions,
|
|
|
|
inlinedFunctions) {
|
2020-11-30 10:53:12 +00:00
|
|
|
this.script = script;
|
|
|
|
this.start = startPos;
|
|
|
|
this.end = endPos;
|
2022-04-08 18:54:33 +00:00
|
|
|
this.positions = sourcePositionTableData;
|
2020-11-30 10:53:12 +00:00
|
|
|
this.inlined = inliningPositions;
|
|
|
|
this.fns = inlinedFunctions;
|
2022-04-08 18:54:33 +00:00
|
|
|
this.sourcePositionTable = new SourcePositionTable(sourcePositionTableData);
|
2020-11-30 10:53:12 +00:00
|
|
|
}
|
2020-12-01 19:37:17 +00:00
|
|
|
|
|
|
|
setDisassemble(code) {
|
|
|
|
this.disassemble = code;
|
|
|
|
}
|
2020-12-03 17:48:45 +00:00
|
|
|
|
|
|
|
getSourceCode() {
|
|
|
|
return this.script.source?.substring(this.start, this.end);
|
|
|
|
}
|
2020-11-30 10:53:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 09:56:03 +00:00
|
|
|
const kProfileOperationMove = 0;
|
|
|
|
const kProfileOperationDelete = 1;
|
|
|
|
const kProfileOperationTick = 2;
|
|
|
|
|
2020-09-28 13:50:56 +00:00
|
|
|
/**
|
|
|
|
* Creates a profile object for processing profiling-related events
|
|
|
|
* and calculating function execution times.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
export class Profile {
|
2022-12-07 00:04:45 +00:00
|
|
|
codeMap_ = new CodeMap();
|
2020-11-03 09:17:12 +00:00
|
|
|
topDownTree_ = new CallTree();
|
|
|
|
bottomUpTree_ = new CallTree();
|
2022-03-02 09:56:03 +00:00
|
|
|
c_entries_ = {__proto__:null};
|
2020-11-03 09:17:12 +00:00
|
|
|
scripts_ = [];
|
|
|
|
urlToScript_ = new Map();
|
2022-03-02 09:56:03 +00:00
|
|
|
warnings = new Set();
|
2020-11-03 09:17:12 +00:00
|
|
|
|
2021-04-20 16:57:36 +00:00
|
|
|
serializeVMSymbols() {
|
|
|
|
let result = this.codeMap_.getAllStaticEntriesWithAddresses();
|
|
|
|
result.concat(this.codeMap_.getAllLibraryEntriesWithAddresses())
|
|
|
|
return result.map(([startAddress, codeEntry]) => {
|
|
|
|
return [codeEntry.getName(), startAddress, startAddress + codeEntry.size]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns whether a function with the specified name must be skipped.
|
2021-06-15 21:27:47 +00:00
|
|
|
* Should be overridden by subclasses.
|
2020-11-03 09:17:12 +00:00
|
|
|
*
|
|
|
|
* @param {string} name Function name.
|
|
|
|
*/
|
|
|
|
skipThisFunction(name) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Enum for profiler operations that involve looking up existing
|
|
|
|
* code entries.
|
|
|
|
*
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
static Operation = {
|
2022-03-02 09:56:03 +00:00
|
|
|
MOVE: kProfileOperationMove,
|
|
|
|
DELETE: kProfileOperationDelete,
|
|
|
|
TICK: kProfileOperationTick
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Enum for code state regarding its dynamic optimization.
|
|
|
|
*
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
static CodeState = {
|
|
|
|
COMPILED: 0,
|
2020-12-01 17:36:40 +00:00
|
|
|
IGNITION: 1,
|
2022-07-19 13:11:01 +00:00
|
|
|
SPARKPLUG: 2,
|
|
|
|
MAGLEV: 4,
|
2021-02-12 08:41:14 +00:00
|
|
|
TURBOFAN: 5,
|
2020-12-01 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 12:18:32 +00:00
|
|
|
static VMState = {
|
|
|
|
JS: 0,
|
|
|
|
GC: 1,
|
|
|
|
PARSER: 2,
|
|
|
|
BYTECODE_COMPILER: 3,
|
2022-07-19 13:11:01 +00:00
|
|
|
// TODO(cbruni): add SPARKPLUG_COMPILER
|
2021-05-31 12:18:32 +00:00
|
|
|
COMPILER: 4,
|
|
|
|
OTHER: 5,
|
|
|
|
EXTERNAL: 6,
|
|
|
|
IDLE: 7,
|
|
|
|
}
|
|
|
|
|
|
|
|
static CodeType = {
|
|
|
|
CPP: 0,
|
|
|
|
SHARED_LIB: 1
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:36:40 +00:00
|
|
|
/**
|
|
|
|
* Parser for dynamic code optimization state.
|
|
|
|
*/
|
|
|
|
static parseState(s) {
|
|
|
|
switch (s) {
|
|
|
|
case '':
|
|
|
|
return this.CodeState.COMPILED;
|
|
|
|
case '~':
|
|
|
|
return this.CodeState.IGNITION;
|
2021-02-12 08:41:14 +00:00
|
|
|
case '^':
|
2022-07-19 13:11:01 +00:00
|
|
|
return this.CodeState.SPARKPLUG;
|
|
|
|
case '+':
|
|
|
|
return this.CodeState.MAGLEV;
|
2020-12-01 17:36:40 +00:00
|
|
|
case '*':
|
|
|
|
return this.CodeState.TURBOFAN;
|
|
|
|
}
|
|
|
|
throw new Error(`unknown code state: ${s}`);
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-12-07 09:36:31 +00:00
|
|
|
static getKindFromState(state) {
|
|
|
|
if (state === this.CodeState.COMPILED) {
|
|
|
|
return "Builtin";
|
|
|
|
} else if (state === this.CodeState.IGNITION) {
|
|
|
|
return "Unopt";
|
2022-07-19 13:11:01 +00:00
|
|
|
} else if (state === this.CodeState.SPARKPLUG) {
|
|
|
|
return "Sparkplug";
|
|
|
|
} else if (state === this.CodeState.MAGLEV) {
|
|
|
|
return "Maglev";
|
2020-12-07 09:36:31 +00:00
|
|
|
} else if (state === this.CodeState.TURBOFAN) {
|
|
|
|
return "Opt";
|
|
|
|
}
|
|
|
|
throw new Error(`unknown code state: ${state}`);
|
|
|
|
}
|
|
|
|
|
2021-06-14 12:44:00 +00:00
|
|
|
static vmStateString(state) {
|
|
|
|
switch (state) {
|
|
|
|
case this.VMState.JS:
|
|
|
|
return 'JS';
|
|
|
|
case this.VMState.GC:
|
|
|
|
return 'GC';
|
|
|
|
case this.VMState.PARSER:
|
|
|
|
return 'Parse';
|
|
|
|
case this.VMState.BYTECODE_COMPILER:
|
|
|
|
return 'Compile Bytecode';
|
|
|
|
case this.VMState.COMPILER:
|
|
|
|
return 'Compile';
|
|
|
|
case this.VMState.OTHER:
|
|
|
|
return 'Other';
|
|
|
|
case this.VMState.EXTERNAL:
|
|
|
|
return 'External';
|
|
|
|
case this.VMState.IDLE:
|
|
|
|
return 'Idle';
|
|
|
|
}
|
|
|
|
return 'unknown';
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Called whenever the specified operation has failed finding a function
|
2022-07-19 13:11:01 +00:00
|
|
|
* containing the specified address. Should be overridden by subclasses.
|
2020-11-03 09:17:12 +00:00
|
|
|
* See the Profile.Operation enum for the list of
|
|
|
|
* possible operations.
|
|
|
|
*
|
|
|
|
* @param {number} operation Operation.
|
|
|
|
* @param {number} addr Address of the unknown code.
|
|
|
|
* @param {number} opt_stackPos If an unknown address is encountered
|
|
|
|
* during stack strace processing, specifies a position of the frame
|
|
|
|
* containing the address.
|
|
|
|
*/
|
2021-01-12 14:36:02 +00:00
|
|
|
handleUnknownCode(operation, addr, opt_stackPos) { }
|
2020-11-03 09:17:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a library.
|
|
|
|
*
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} startAddr Starting address.
|
|
|
|
* @param {number} endAddr Ending address.
|
|
|
|
*/
|
|
|
|
addLibrary(name, startAddr, endAddr) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = new CodeEntry(endAddr - startAddr, name, 'SHARED_LIB');
|
2020-11-03 09:17:12 +00:00
|
|
|
this.codeMap_.addLibrary(startAddr, entry);
|
|
|
|
return entry;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Registers statically compiled code entry.
|
|
|
|
*
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} startAddr Starting address.
|
|
|
|
* @param {number} endAddr Ending address.
|
|
|
|
*/
|
|
|
|
addStaticCode(name, startAddr, endAddr) {
|
2021-01-12 14:36:02 +00:00
|
|
|
const entry = new CodeEntry(endAddr - startAddr, name, 'CPP');
|
2020-11-03 09:17:12 +00:00
|
|
|
this.codeMap_.addStaticCode(startAddr, entry);
|
|
|
|
return entry;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Registers dynamic (JIT-compiled) code entry.
|
|
|
|
*
|
|
|
|
* @param {string} type Code entry type.
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} start Starting address.
|
|
|
|
* @param {number} size Code entry size.
|
|
|
|
*/
|
|
|
|
addCode(type, name, timestamp, start, size) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = new DynamicCodeEntry(size, type, name);
|
2020-11-03 09:17:12 +00:00
|
|
|
this.codeMap_.addCode(start, entry);
|
|
|
|
return entry;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2022-04-26 17:18:04 +00:00
|
|
|
/**
|
|
|
|
* Registers dynamic (JIT-compiled) code entry or entries that overlap with
|
|
|
|
* static entries (like builtins).
|
|
|
|
*
|
|
|
|
* @param {string} type Code entry type.
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} start Starting address.
|
|
|
|
* @param {number} size Code entry size.
|
|
|
|
*/
|
|
|
|
addAnyCode(type, name, timestamp, start, size) {
|
|
|
|
const entry = new DynamicCodeEntry(size, type, name);
|
|
|
|
this.codeMap_.addAnyCode(start, entry);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Registers dynamic (JIT-compiled) code entry.
|
|
|
|
*
|
|
|
|
* @param {string} type Code entry type.
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} start Starting address.
|
|
|
|
* @param {number} size Code entry size.
|
|
|
|
* @param {number} funcAddr Shared function object address.
|
|
|
|
* @param {Profile.CodeState} state Optimization state.
|
|
|
|
*/
|
|
|
|
addFuncCode(type, name, timestamp, start, size, funcAddr, state) {
|
|
|
|
// As code and functions are in the same address space,
|
|
|
|
// it is safe to put them in a single code map.
|
2020-11-10 11:47:40 +00:00
|
|
|
let func = this.codeMap_.findDynamicEntryByStartAddress(funcAddr);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (func === null) {
|
2022-12-07 00:04:45 +00:00
|
|
|
func = new FunctionEntry(name);
|
2020-11-03 09:17:12 +00:00
|
|
|
this.codeMap_.addCode(funcAddr, func);
|
|
|
|
} else if (func.name !== name) {
|
|
|
|
// Function object has been overwritten with a new one.
|
|
|
|
func.name = name;
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
let entry = this.codeMap_.findDynamicEntryByStartAddress(start);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (entry !== null) {
|
2020-11-03 09:17:12 +00:00
|
|
|
if (entry.size === size && entry.func === func) {
|
|
|
|
// Entry state has changed.
|
|
|
|
entry.state = state;
|
|
|
|
} else {
|
|
|
|
this.codeMap_.deleteCode(start);
|
|
|
|
entry = null;
|
|
|
|
}
|
|
|
|
}
|
2022-03-02 09:56:03 +00:00
|
|
|
if (entry === null) {
|
2020-11-03 09:17:12 +00:00
|
|
|
entry = new DynamicFuncCodeEntry(size, type, func, state);
|
|
|
|
this.codeMap_.addCode(start, entry);
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Reports about moving of a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} from Current code entry address.
|
|
|
|
* @param {number} to New code entry address.
|
|
|
|
*/
|
|
|
|
moveCode(from, to) {
|
|
|
|
try {
|
|
|
|
this.codeMap_.moveCode(from, to);
|
|
|
|
} catch (e) {
|
2022-03-02 09:56:03 +00:00
|
|
|
this.handleUnknownCode(kProfileOperationMove, from);
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-30 10:34:28 +00:00
|
|
|
deoptCode(timestamp, code, inliningId, scriptOffset, bailoutType,
|
2020-11-03 09:17:12 +00:00
|
|
|
sourcePositionText, deoptReasonText) {
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reports about deletion of a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} start Starting address.
|
|
|
|
*/
|
|
|
|
deleteCode(start) {
|
|
|
|
try {
|
2020-09-28 13:50:56 +00:00
|
|
|
this.codeMap_.deleteCode(start);
|
2020-11-03 09:17:12 +00:00
|
|
|
} catch (e) {
|
2022-03-02 09:56:03 +00:00
|
|
|
this.handleUnknownCode(kProfileOperationDelete, start);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Adds source positions for given code.
|
|
|
|
*/
|
2020-11-30 10:53:12 +00:00
|
|
|
addSourcePositions(start, scriptId, startPos, endPos, sourcePositionTable,
|
2020-11-03 09:17:12 +00:00
|
|
|
inliningPositions, inlinedFunctions) {
|
2020-11-30 10:53:12 +00:00
|
|
|
const script = this.getOrCreateScript(scriptId);
|
|
|
|
const entry = this.codeMap_.findDynamicEntryByStartAddress(start);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (entry === null) return;
|
2020-11-30 10:53:12 +00:00
|
|
|
// Resolve the inlined functions list.
|
|
|
|
if (inlinedFunctions.length > 0) {
|
|
|
|
inlinedFunctions = inlinedFunctions.substring(1).split("S");
|
|
|
|
for (let i = 0; i < inlinedFunctions.length; i++) {
|
|
|
|
const funcAddr = parseInt(inlinedFunctions[i]);
|
|
|
|
const func = this.codeMap_.findDynamicEntryByStartAddress(funcAddr);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (func === null || func.funcId === undefined) {
|
2020-11-30 10:53:12 +00:00
|
|
|
// TODO: fix
|
2022-03-02 09:56:03 +00:00
|
|
|
this.warnings.add(`Could not find function ${inlinedFunctions[i]}`);
|
2020-11-30 10:53:12 +00:00
|
|
|
inlinedFunctions[i] = null;
|
|
|
|
} else {
|
|
|
|
inlinedFunctions[i] = func.funcId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inlinedFunctions = [];
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:37:17 +00:00
|
|
|
this.getOrCreateSourceInfo(entry).setSourcePositionInfo(
|
2021-01-12 14:36:02 +00:00
|
|
|
script, startPos, endPos, sourcePositionTable, inliningPositions,
|
|
|
|
inlinedFunctions);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 19:37:17 +00:00
|
|
|
addDisassemble(start, kind, disassemble) {
|
|
|
|
const entry = this.codeMap_.findDynamicEntryByStartAddress(start);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (entry !== null) {
|
|
|
|
this.getOrCreateSourceInfo(entry).setDisassemble(disassemble);
|
|
|
|
}
|
2021-06-28 19:46:31 +00:00
|
|
|
return entry;
|
2020-12-01 19:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getOrCreateSourceInfo(entry) {
|
|
|
|
return entry.source ?? (entry.source = new SourceInfo());
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
addScriptSource(id, url, source) {
|
2020-11-30 10:53:12 +00:00
|
|
|
const script = this.getOrCreateScript(id);
|
|
|
|
script.update(url, source);
|
2020-11-03 09:17:12 +00:00
|
|
|
this.urlToScript_.set(url, script);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 10:53:12 +00:00
|
|
|
getOrCreateScript(id) {
|
|
|
|
let script = this.scripts_[id];
|
2022-03-02 09:56:03 +00:00
|
|
|
if (script === undefined) {
|
2020-11-30 10:53:12 +00:00
|
|
|
script = new Script(id);
|
|
|
|
this.scripts_[id] = script;
|
|
|
|
}
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
getScript(url) {
|
|
|
|
return this.urlToScript_.get(url);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Reports about moving of a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} from Current code entry address.
|
|
|
|
* @param {number} to New code entry address.
|
|
|
|
*/
|
|
|
|
moveFunc(from, to) {
|
|
|
|
if (this.codeMap_.findDynamicEntryByStartAddress(from)) {
|
|
|
|
this.codeMap_.moveCode(from, to);
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a code entry by an address.
|
|
|
|
*
|
|
|
|
* @param {number} addr Entry address.
|
|
|
|
*/
|
|
|
|
findEntry(addr) {
|
|
|
|
return this.codeMap_.findEntry(addr);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Records a tick event. Stack must contain a sequence of
|
|
|
|
* addresses starting with the program counter value.
|
|
|
|
*
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {number[]} stack Stack sample.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
|
|
|
recordTick(time_ns, vmState, stack) {
|
2021-05-31 12:18:32 +00:00
|
|
|
const {nameStack, entryStack} = this.resolveAndFilterFuncs_(stack);
|
|
|
|
this.bottomUpTree_.addPath(nameStack);
|
|
|
|
nameStack.reverse();
|
|
|
|
this.topDownTree_.addPath(nameStack);
|
2021-06-07 14:57:44 +00:00
|
|
|
return entryStack;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Translates addresses into function names and filters unneeded
|
|
|
|
* functions.
|
|
|
|
*
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {number[]} stack Stack sample.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
|
|
|
resolveAndFilterFuncs_(stack) {
|
2021-05-31 12:18:32 +00:00
|
|
|
const nameStack = [];
|
|
|
|
const entryStack = [];
|
2020-11-10 11:47:40 +00:00
|
|
|
let last_seen_c_function = '';
|
|
|
|
let look_for_first_c_function = false;
|
|
|
|
for (let i = 0; i < stack.length; ++i) {
|
2021-05-31 12:18:32 +00:00
|
|
|
const pc = stack[i];
|
|
|
|
const entry = this.codeMap_.findEntry(pc);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (entry !== null) {
|
2021-05-31 12:18:32 +00:00
|
|
|
entryStack.push(entry);
|
2020-11-10 11:47:40 +00:00
|
|
|
const name = entry.getName();
|
2020-11-03 09:17:12 +00:00
|
|
|
if (i === 0 && (entry.type === 'CPP' || entry.type === 'SHARED_LIB')) {
|
|
|
|
look_for_first_c_function = true;
|
|
|
|
}
|
|
|
|
if (look_for_first_c_function && entry.type === 'CPP') {
|
|
|
|
last_seen_c_function = name;
|
|
|
|
}
|
|
|
|
if (!this.skipThisFunction(name)) {
|
2021-05-31 12:18:32 +00:00
|
|
|
nameStack.push(name);
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-02 09:56:03 +00:00
|
|
|
this.handleUnknownCode(kProfileOperationTick, pc, i);
|
2021-05-31 12:18:32 +00:00
|
|
|
if (i === 0) nameStack.push("UNKNOWN");
|
|
|
|
entryStack.push(pc);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2021-05-31 12:18:32 +00:00
|
|
|
if (look_for_first_c_function && i > 0 &&
|
2022-03-02 09:56:03 +00:00
|
|
|
(entry === null || entry.type !== 'CPP')
|
|
|
|
&& last_seen_c_function !== '') {
|
2020-11-03 09:17:12 +00:00
|
|
|
if (this.c_entries_[last_seen_c_function] === undefined) {
|
|
|
|
this.c_entries_[last_seen_c_function] = 0;
|
|
|
|
}
|
|
|
|
this.c_entries_[last_seen_c_function]++;
|
|
|
|
look_for_first_c_function = false; // Found it, we're done.
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 12:18:32 +00:00
|
|
|
return {nameStack, entryStack};
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Performs a BF traversal of the top down call graph.
|
|
|
|
*
|
|
|
|
* @param {function(CallTreeNode)} f Visitor function.
|
|
|
|
*/
|
|
|
|
traverseTopDownTree(f) {
|
|
|
|
this.topDownTree_.traverse(f);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Performs a BF traversal of the bottom up call graph.
|
|
|
|
*
|
|
|
|
* @param {function(CallTreeNode)} f Visitor function.
|
|
|
|
*/
|
|
|
|
traverseBottomUpTree(f) {
|
|
|
|
this.bottomUpTree_.traverse(f);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Calculates a top down profile for a node with the specified label.
|
|
|
|
* If no name specified, returns the whole top down calls tree.
|
|
|
|
*
|
|
|
|
* @param {string} opt_label Node label.
|
|
|
|
*/
|
|
|
|
getTopDownProfile(opt_label) {
|
|
|
|
return this.getTreeProfile_(this.topDownTree_, opt_label);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Calculates a bottom up profile for a node with the specified label.
|
|
|
|
* If no name specified, returns the whole bottom up calls tree.
|
|
|
|
*
|
|
|
|
* @param {string} opt_label Node label.
|
|
|
|
*/
|
|
|
|
getBottomUpProfile(opt_label) {
|
|
|
|
return this.getTreeProfile_(this.bottomUpTree_, opt_label);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Helper function for calculating a tree profile.
|
|
|
|
*
|
|
|
|
* @param {Profile.CallTree} tree Call tree.
|
|
|
|
* @param {string} opt_label Node label.
|
|
|
|
*/
|
|
|
|
getTreeProfile_(tree, opt_label) {
|
|
|
|
if (!opt_label) {
|
|
|
|
tree.computeTotalWeights();
|
|
|
|
return tree;
|
|
|
|
} else {
|
2020-11-10 11:47:40 +00:00
|
|
|
const subTree = tree.cloneSubtree(opt_label);
|
2020-11-03 09:17:12 +00:00
|
|
|
subTree.computeTotalWeights();
|
|
|
|
return subTree;
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Calculates a flat profile of callees starting from a node with
|
|
|
|
* the specified label. If no name specified, starts from the root.
|
|
|
|
*
|
|
|
|
* @param {string} opt_label Starting node label.
|
|
|
|
*/
|
|
|
|
getFlatProfile(opt_label) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const counters = new CallTree();
|
|
|
|
const rootLabel = opt_label || CallTree.ROOT_NODE_LABEL;
|
2022-03-02 09:56:03 +00:00
|
|
|
const precs = {__proto__:null};
|
2020-11-03 09:17:12 +00:00
|
|
|
precs[rootLabel] = 0;
|
2020-11-10 11:47:40 +00:00
|
|
|
const root = counters.findOrAddChild(rootLabel);
|
2020-11-03 09:17:12 +00:00
|
|
|
|
|
|
|
this.topDownTree_.computeTotalWeights();
|
|
|
|
this.topDownTree_.traverseInDepth(
|
|
|
|
function onEnter(node) {
|
|
|
|
if (!(node.label in precs)) {
|
|
|
|
precs[node.label] = 0;
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
const nodeLabelIsRootLabel = node.label == rootLabel;
|
2020-11-03 09:17:12 +00:00
|
|
|
if (nodeLabelIsRootLabel || precs[rootLabel] > 0) {
|
|
|
|
if (precs[rootLabel] == 0) {
|
|
|
|
root.selfWeight += node.selfWeight;
|
|
|
|
root.totalWeight += node.totalWeight;
|
|
|
|
} else {
|
2020-11-10 11:47:40 +00:00
|
|
|
const rec = root.findOrAddChild(node.label);
|
2020-11-03 09:17:12 +00:00
|
|
|
rec.selfWeight += node.selfWeight;
|
|
|
|
if (nodeLabelIsRootLabel || precs[node.label] == 0) {
|
|
|
|
rec.totalWeight += node.totalWeight;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
precs[node.label]++;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
},
|
|
|
|
function onExit(node) {
|
|
|
|
if (node.label == rootLabel || precs[rootLabel] > 0) {
|
|
|
|
precs[node.label]--;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null);
|
|
|
|
|
|
|
|
if (!opt_label) {
|
|
|
|
// If we have created a flat profile for the whole program, we don't
|
|
|
|
// need an explicit root in it. Thus, replace the counters tree
|
|
|
|
// root with the node corresponding to the whole program.
|
|
|
|
counters.root_ = root;
|
|
|
|
} else {
|
|
|
|
// Propagate weights so percents can be calculated correctly.
|
|
|
|
counters.getRoot().selfWeight = root.selfWeight;
|
|
|
|
counters.getRoot().totalWeight = root.totalWeight;
|
|
|
|
}
|
|
|
|
return counters;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
getCEntryProfile() {
|
2020-11-10 11:47:40 +00:00
|
|
|
const result = [new CEntryNode("TOTAL", 0)];
|
|
|
|
let total_ticks = 0;
|
|
|
|
for (let f in this.c_entries_) {
|
|
|
|
const ticks = this.c_entries_[f];
|
2020-11-03 09:17:12 +00:00
|
|
|
total_ticks += ticks;
|
|
|
|
result.push(new CEntryNode(f, ticks));
|
|
|
|
}
|
|
|
|
result[0].ticks = total_ticks; // Sorting will keep this at index 0.
|
2020-11-10 11:47:40 +00:00
|
|
|
result.sort((n1, n2) => n2.ticks - n1.ticks || (n2.name < n1.name ? -1 : 1));
|
2020-11-03 09:17:12 +00:00
|
|
|
return result;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Cleans up function entries that are not referenced by code entries.
|
|
|
|
*/
|
|
|
|
cleanUpFuncEntries() {
|
2020-11-10 11:47:40 +00:00
|
|
|
const referencedFuncEntries = [];
|
|
|
|
const entries = this.codeMap_.getAllDynamicEntriesWithAddresses();
|
|
|
|
for (let i = 0, l = entries.length; i < l; ++i) {
|
2020-11-03 09:17:12 +00:00
|
|
|
if (entries[i][1].constructor === FunctionEntry) {
|
|
|
|
entries[i][1].used = false;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
for (let i = 0, l = entries.length; i < l; ++i) {
|
2020-11-03 09:17:12 +00:00
|
|
|
if ("func" in entries[i][1]) {
|
|
|
|
entries[i][1].func.used = true;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
for (let i = 0, l = entries.length; i < l; ++i) {
|
2020-11-03 09:17:12 +00:00
|
|
|
if (entries[i][1].constructor === FunctionEntry &&
|
|
|
|
!entries[i][1].used) {
|
|
|
|
this.codeMap_.deleteCode(entries[i][0]);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class CEntryNode {
|
|
|
|
constructor(name, ticks) {
|
|
|
|
this.name = name;
|
|
|
|
this.ticks = ticks;
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} size Code size.
|
|
|
|
* @param {string} type Code type.
|
|
|
|
* @param {string} name Function name.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
class DynamicCodeEntry extends CodeEntry {
|
|
|
|
constructor(size, type, name) {
|
|
|
|
super(size, name, type);
|
|
|
|
}
|
2021-01-12 14:36:02 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
getName() {
|
|
|
|
return this.type + ': ' + this.name;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns raw node name (without type decoration).
|
|
|
|
*/
|
|
|
|
getRawName() {
|
|
|
|
return this.name;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
isJSFunction() {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
toString() {
|
|
|
|
return this.getName() + ': ' + this.size.toString(16);
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} size Code size.
|
|
|
|
* @param {string} type Code type.
|
2020-11-03 09:17:12 +00:00
|
|
|
* @param {FunctionEntry} func Shared function entry.
|
2020-09-28 13:50:56 +00:00
|
|
|
* @param {Profile.CodeState} state Code optimization state.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
class DynamicFuncCodeEntry extends CodeEntry {
|
|
|
|
constructor(size, type, func, state) {
|
|
|
|
super(size, '', type);
|
|
|
|
this.func = func;
|
2020-12-03 17:48:45 +00:00
|
|
|
func.addDynamicCode(this);
|
2020-11-03 09:17:12 +00:00
|
|
|
this.state = state;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-12-09 08:01:26 +00:00
|
|
|
get functionName() {
|
|
|
|
return this.func.functionName;
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:48:45 +00:00
|
|
|
getSourceCode() {
|
|
|
|
return this.source?.getSourceCode();
|
|
|
|
}
|
|
|
|
|
2021-02-12 08:41:14 +00:00
|
|
|
static STATE_PREFIX = ["", "~", "^", "-", "+", "*"];
|
2020-11-03 09:17:12 +00:00
|
|
|
getState() {
|
|
|
|
return DynamicFuncCodeEntry.STATE_PREFIX[this.state];
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
getName() {
|
2020-11-10 11:47:40 +00:00
|
|
|
const name = this.func.getName();
|
2020-11-03 09:17:12 +00:00
|
|
|
return this.type + ': ' + this.getState() + name;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns raw node name (without type decoration).
|
|
|
|
*/
|
|
|
|
getRawName() {
|
|
|
|
return this.func.getName();
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
isJSFunction() {
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
toString() {
|
|
|
|
return this.getName() + ': ' + this.size.toString(16);
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a shared function object entry.
|
|
|
|
*
|
|
|
|
* @param {string} name Function name.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
class FunctionEntry extends CodeEntry {
|
2021-01-12 14:36:02 +00:00
|
|
|
|
2020-12-03 17:48:45 +00:00
|
|
|
// Contains the list of generated code for this function.
|
2022-04-26 17:18:04 +00:00
|
|
|
/** @type {Set<DynamicCodeEntry>} */
|
2020-12-03 17:48:45 +00:00
|
|
|
_codeEntries = new Set();
|
|
|
|
|
2022-12-07 00:04:45 +00:00
|
|
|
constructor(name) {
|
|
|
|
super(0, name);
|
2020-12-09 08:01:26 +00:00
|
|
|
const index = name.lastIndexOf(' ');
|
|
|
|
this.functionName = 1 <= index ? name.substring(0, index) : '<anonymous>';
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-12-03 17:48:45 +00:00
|
|
|
addDynamicCode(code) {
|
|
|
|
if (code.func != this) {
|
|
|
|
throw new Error("Adding dynamic code to wrong function");
|
|
|
|
}
|
|
|
|
this._codeEntries.add(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSourceCode() {
|
|
|
|
// All code entries should map to the same source positions.
|
|
|
|
return this._codeEntries.values().next().value.getSourceCode();
|
|
|
|
}
|
|
|
|
|
2021-06-15 21:27:47 +00:00
|
|
|
get codeEntries() {
|
|
|
|
return this._codeEntries;
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns node name.
|
|
|
|
*/
|
|
|
|
getName() {
|
2020-11-10 11:47:40 +00:00
|
|
|
let name = this.name;
|
2020-11-03 09:17:12 +00:00
|
|
|
if (name.length == 0) {
|
2021-01-12 14:36:02 +00:00
|
|
|
return '<anonymous>';
|
2020-11-03 09:17:12 +00:00
|
|
|
} else if (name.charAt(0) == ' ') {
|
|
|
|
// An anonymous function with location: " aaa.js:10".
|
2020-12-09 08:01:26 +00:00
|
|
|
return `<anonymous>${name}`;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a call graph.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
class CallTree {
|
|
|
|
root_ = new CallTreeNode(CallTree.ROOT_NODE_LABEL);
|
|
|
|
totalsComputed_ = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The label of the root node.
|
|
|
|
*/
|
|
|
|
static ROOT_NODE_LABEL = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the tree root.
|
|
|
|
*/
|
|
|
|
getRoot() {
|
|
|
|
return this.root_;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Adds the specified call path, constructing nodes as necessary.
|
|
|
|
*
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {string[]} path Call path.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
|
|
|
addPath(path) {
|
2022-03-02 09:56:03 +00:00
|
|
|
if (path.length == 0) return;
|
2020-11-10 11:47:40 +00:00
|
|
|
let curr = this.root_;
|
|
|
|
for (let i = 0; i < path.length; ++i) {
|
2020-11-03 09:17:12 +00:00
|
|
|
curr = curr.findOrAddChild(path[i]);
|
|
|
|
}
|
|
|
|
curr.selfWeight++;
|
|
|
|
this.totalsComputed_ = false;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Finds an immediate child of the specified parent with the specified
|
|
|
|
* label, creates a child node if necessary. If a parent node isn't
|
|
|
|
* specified, uses tree root.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
|
|
|
findOrAddChild(label) {
|
|
|
|
return this.root_.findOrAddChild(label);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Creates a subtree by cloning and merging all subtrees rooted at nodes
|
|
|
|
* with a given label. E.g. cloning the following call tree on label 'A'
|
|
|
|
* will give the following result:
|
|
|
|
*
|
|
|
|
* <A>--<B> <B>
|
|
|
|
* / /
|
|
|
|
* <root> == clone on 'A' ==> <root>--<A>
|
|
|
|
* \ \
|
|
|
|
* <C>--<A>--<D> <D>
|
|
|
|
*
|
|
|
|
* And <A>'s selfWeight will be the sum of selfWeights of <A>'s from the
|
|
|
|
* source call tree.
|
|
|
|
*
|
|
|
|
* @param {string} label The label of the new root node.
|
|
|
|
*/
|
|
|
|
cloneSubtree(label) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const subTree = new CallTree();
|
2020-11-03 09:17:12 +00:00
|
|
|
this.traverse((node, parent) => {
|
|
|
|
if (!parent && node.label != label) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
const child = (parent ? parent : subTree).findOrAddChild(node.label);
|
2020-11-03 09:17:12 +00:00
|
|
|
child.selfWeight += node.selfWeight;
|
|
|
|
return child;
|
2020-09-28 13:50:56 +00:00
|
|
|
});
|
2020-11-03 09:17:12 +00:00
|
|
|
return subTree;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Computes total weights in the call graph.
|
|
|
|
*/
|
|
|
|
computeTotalWeights() {
|
|
|
|
if (this.totalsComputed_) return;
|
|
|
|
this.root_.computeTotalWeight();
|
|
|
|
this.totalsComputed_ = true;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Traverses the call graph in preorder. This function can be used for
|
|
|
|
* building optionally modified tree clones. This is the boilerplate code
|
|
|
|
* for this scenario:
|
|
|
|
*
|
|
|
|
* callTree.traverse(function(node, parentClone) {
|
|
|
|
* var nodeClone = cloneNode(node);
|
|
|
|
* if (parentClone)
|
|
|
|
* parentClone.addChild(nodeClone);
|
|
|
|
* return nodeClone;
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param {function(CallTreeNode, *)} f Visitor function.
|
|
|
|
* The second parameter is the result of calling 'f' on the parent node.
|
|
|
|
*/
|
|
|
|
traverse(f) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const pairsToProcess = new ConsArray();
|
2020-11-03 09:17:12 +00:00
|
|
|
pairsToProcess.concat([{ node: this.root_, param: null }]);
|
|
|
|
while (!pairsToProcess.atEnd()) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const pair = pairsToProcess.next();
|
|
|
|
const node = pair.node;
|
|
|
|
const newParam = f(node, pair.param);
|
|
|
|
const morePairsToProcess = [];
|
2020-11-03 09:17:12 +00:00
|
|
|
node.forEachChild((child) => {
|
|
|
|
morePairsToProcess.push({ node: child, param: newParam });
|
|
|
|
});
|
|
|
|
pairsToProcess.concat(morePairsToProcess);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs an indepth call graph traversal.
|
|
|
|
*
|
|
|
|
* @param {function(CallTreeNode)} enter A function called
|
|
|
|
* prior to visiting node's children.
|
|
|
|
* @param {function(CallTreeNode)} exit A function called
|
|
|
|
* after visiting node's children.
|
|
|
|
*/
|
2021-01-12 14:36:02 +00:00
|
|
|
traverseInDepth(enter, exit) {
|
2020-11-03 09:17:12 +00:00
|
|
|
function traverse(node) {
|
|
|
|
enter(node);
|
|
|
|
node.forEachChild(traverse);
|
|
|
|
exit(node);
|
|
|
|
}
|
|
|
|
traverse(this.root_);
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a call graph node.
|
|
|
|
*
|
|
|
|
* @param {string} label Node label.
|
2020-11-03 09:17:12 +00:00
|
|
|
* @param {CallTreeNode} opt_parent Node parent.
|
2020-09-28 13:50:56 +00:00
|
|
|
*/
|
2021-01-12 14:36:02 +00:00
|
|
|
class CallTreeNode {
|
2020-11-03 09:17:12 +00:00
|
|
|
|
|
|
|
constructor(label, opt_parent) {
|
2022-03-02 09:56:03 +00:00
|
|
|
// Node self weight (how many times this node was the last node in
|
|
|
|
// a call path).
|
|
|
|
this.selfWeight = 0;
|
|
|
|
// Node total weight (includes weights of all children).
|
|
|
|
this.totalWeight = 0;
|
|
|
|
this. children = { __proto__:null };
|
2020-11-03 09:17:12 +00:00
|
|
|
this.label = label;
|
|
|
|
this.parent = opt_parent;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Adds a child node.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
|
|
|
addChild(label) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const child = new CallTreeNode(label, this);
|
2020-11-03 09:17:12 +00:00
|
|
|
this.children[label] = child;
|
|
|
|
return child;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Computes node's total weight.
|
|
|
|
*/
|
|
|
|
computeTotalWeight() {
|
2020-11-10 11:47:40 +00:00
|
|
|
let totalWeight = this.selfWeight;
|
2020-09-28 13:50:56 +00:00
|
|
|
this.forEachChild(function (child) {
|
|
|
|
totalWeight += child.computeTotalWeight();
|
|
|
|
});
|
|
|
|
return this.totalWeight = totalWeight;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns all node's children as an array.
|
|
|
|
*/
|
|
|
|
exportChildren() {
|
2020-11-10 11:47:40 +00:00
|
|
|
const result = [];
|
2020-11-03 09:17:12 +00:00
|
|
|
this.forEachChild(function (node) { result.push(node); });
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Finds an immediate child with the specified label.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
|
|
|
findChild(label) {
|
2022-03-02 09:56:03 +00:00
|
|
|
const found = this.children[label];
|
|
|
|
return found === undefined ? null : found;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Finds an immediate child with the specified label, creates a child
|
|
|
|
* node if necessary.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
|
|
|
findOrAddChild(label) {
|
2022-03-02 09:56:03 +00:00
|
|
|
const found = this.findChild(label)
|
|
|
|
if (found === null) return this.addChild(label);
|
|
|
|
return found;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Calls the specified function for every child.
|
|
|
|
*
|
|
|
|
* @param {function(CallTreeNode)} f Visitor function.
|
|
|
|
*/
|
|
|
|
forEachChild(f) {
|
2020-11-10 11:47:40 +00:00
|
|
|
for (let c in this.children) {
|
2020-11-03 09:17:12 +00:00
|
|
|
f(this.children[c]);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Walks up from the current node up to the call tree root.
|
|
|
|
*
|
|
|
|
* @param {function(CallTreeNode)} f Visitor function.
|
|
|
|
*/
|
|
|
|
walkUpToRoot(f) {
|
2022-03-02 09:56:03 +00:00
|
|
|
for (let curr = this; curr !== null; curr = curr.parent) {
|
2020-11-03 09:17:12 +00:00
|
|
|
f(curr);
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Tries to find a node with the specified path.
|
|
|
|
*
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {string[]} labels The path.
|
2020-11-03 09:17:12 +00:00
|
|
|
* @param {function(CallTreeNode)} opt_f Visitor function.
|
|
|
|
*/
|
|
|
|
descendToChild(labels, opt_f) {
|
2020-11-10 11:47:40 +00:00
|
|
|
let curr = this;
|
|
|
|
for (let pos = 0; pos < labels.length && curr != null; pos++) {
|
|
|
|
const child = curr.findChild(labels[pos]);
|
2020-11-03 09:17:12 +00:00
|
|
|
if (opt_f) {
|
|
|
|
opt_f(child, pos);
|
|
|
|
}
|
|
|
|
curr = child;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
return curr;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
export function JsonProfile() {
|
|
|
|
this.codeMap_ = new CodeMap();
|
|
|
|
this.codeEntries_ = [];
|
|
|
|
this.functionEntries_ = [];
|
|
|
|
this.ticks_ = [];
|
|
|
|
this.scripts_ = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonProfile.prototype.addLibrary = function (
|
|
|
|
name, startAddr, endAddr) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = new CodeEntry(
|
2020-09-28 13:50:56 +00:00
|
|
|
endAddr - startAddr, name, 'SHARED_LIB');
|
|
|
|
this.codeMap_.addLibrary(startAddr, entry);
|
|
|
|
|
|
|
|
entry.codeId = this.codeEntries_.length;
|
|
|
|
this.codeEntries_.push({ name: entry.name, type: entry.type });
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.addStaticCode = function (
|
|
|
|
name, startAddr, endAddr) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = new CodeEntry(
|
2020-09-28 13:50:56 +00:00
|
|
|
endAddr - startAddr, name, 'CPP');
|
|
|
|
this.codeMap_.addStaticCode(startAddr, entry);
|
|
|
|
|
|
|
|
entry.codeId = this.codeEntries_.length;
|
|
|
|
this.codeEntries_.push({ name: entry.name, type: entry.type });
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.addCode = function (
|
|
|
|
kind, name, timestamp, start, size) {
|
|
|
|
let codeId = this.codeEntries_.length;
|
|
|
|
// Find out if we have a static code entry for the code. If yes, we will
|
|
|
|
// make sure it is written to the JSON file just once.
|
|
|
|
let staticEntry = this.codeMap_.findAddress(start);
|
|
|
|
if (staticEntry && staticEntry.entry.type === 'CPP') {
|
|
|
|
codeId = staticEntry.entry.codeId;
|
|
|
|
}
|
|
|
|
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = new CodeEntry(size, name, 'CODE');
|
2020-09-28 13:50:56 +00:00
|
|
|
this.codeMap_.addCode(start, entry);
|
|
|
|
|
|
|
|
entry.codeId = codeId;
|
|
|
|
this.codeEntries_[codeId] = {
|
|
|
|
name: entry.name,
|
|
|
|
timestamp: timestamp,
|
|
|
|
type: entry.type,
|
2020-11-10 11:47:40 +00:00
|
|
|
kind: kind,
|
2020-09-28 13:50:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.addFuncCode = function (
|
|
|
|
kind, name, timestamp, start, size, funcAddr, state) {
|
|
|
|
// As code and functions are in the same address space,
|
|
|
|
// it is safe to put them in a single code map.
|
2020-11-10 11:47:40 +00:00
|
|
|
let func = this.codeMap_.findDynamicEntryByStartAddress(funcAddr);
|
2020-09-28 13:50:56 +00:00
|
|
|
if (!func) {
|
2020-11-10 11:47:40 +00:00
|
|
|
func = new CodeEntry(0, name, 'SFI');
|
2020-09-28 13:50:56 +00:00
|
|
|
this.codeMap_.addCode(funcAddr, func);
|
|
|
|
|
|
|
|
func.funcId = this.functionEntries_.length;
|
2020-11-10 11:47:40 +00:00
|
|
|
this.functionEntries_.push({ name, codes: [] });
|
2020-09-28 13:50:56 +00:00
|
|
|
} else if (func.name !== name) {
|
|
|
|
// Function object has been overwritten with a new one.
|
|
|
|
func.name = name;
|
|
|
|
|
|
|
|
func.funcId = this.functionEntries_.length;
|
2020-11-10 11:47:40 +00:00
|
|
|
this.functionEntries_.push({ name, codes: [] });
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
// TODO(jarin): Insert the code object into the SFI's code list.
|
2020-11-10 11:47:40 +00:00
|
|
|
let entry = this.codeMap_.findDynamicEntryByStartAddress(start);
|
2020-09-28 13:50:56 +00:00
|
|
|
if (entry) {
|
|
|
|
if (entry.size === size && entry.func === func) {
|
|
|
|
// Entry state has changed.
|
|
|
|
entry.state = state;
|
|
|
|
} else {
|
|
|
|
this.codeMap_.deleteCode(start);
|
|
|
|
entry = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!entry) {
|
2020-11-03 09:17:12 +00:00
|
|
|
entry = new CodeEntry(size, name, 'JS');
|
2020-09-28 13:50:56 +00:00
|
|
|
this.codeMap_.addCode(start, entry);
|
|
|
|
|
|
|
|
entry.codeId = this.codeEntries_.length;
|
|
|
|
|
|
|
|
this.functionEntries_[func.funcId].codes.push(entry.codeId);
|
|
|
|
|
2020-12-07 09:36:31 +00:00
|
|
|
kind = Profile.getKindFromState(state);
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
this.codeEntries_.push({
|
|
|
|
name: entry.name,
|
|
|
|
type: entry.type,
|
|
|
|
kind: kind,
|
|
|
|
func: func.funcId,
|
2020-11-10 11:47:40 +00:00
|
|
|
tm: timestamp,
|
2020-09-28 13:50:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.moveCode = function (from, to) {
|
|
|
|
try {
|
|
|
|
this.codeMap_.moveCode(from, to);
|
|
|
|
} catch (e) {
|
2020-11-10 11:47:40 +00:00
|
|
|
printErr(`Move: unknown source ${from}`);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.addSourcePositions = function (
|
|
|
|
start, script, startPos, endPos, sourcePositions, inliningPositions,
|
|
|
|
inlinedFunctions) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = this.codeMap_.findDynamicEntryByStartAddress(start);
|
2020-09-28 13:50:56 +00:00
|
|
|
if (!entry) return;
|
2020-11-10 11:47:40 +00:00
|
|
|
const codeId = entry.codeId;
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
// Resolve the inlined functions list.
|
|
|
|
if (inlinedFunctions.length > 0) {
|
|
|
|
inlinedFunctions = inlinedFunctions.substring(1).split("S");
|
2020-11-10 11:47:40 +00:00
|
|
|
for (let i = 0; i < inlinedFunctions.length; i++) {
|
|
|
|
const funcAddr = parseInt(inlinedFunctions[i]);
|
|
|
|
const func = this.codeMap_.findDynamicEntryByStartAddress(funcAddr);
|
2020-09-28 13:50:56 +00:00
|
|
|
if (!func || func.funcId === undefined) {
|
2020-11-10 11:47:40 +00:00
|
|
|
printErr(`Could not find function ${inlinedFunctions[i]}`);
|
2020-09-28 13:50:56 +00:00
|
|
|
inlinedFunctions[i] = null;
|
|
|
|
} else {
|
|
|
|
inlinedFunctions[i] = func.funcId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inlinedFunctions = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.codeEntries_[entry.codeId].source = {
|
|
|
|
script: script,
|
|
|
|
start: startPos,
|
|
|
|
end: endPos,
|
|
|
|
positions: sourcePositions,
|
|
|
|
inlined: inliningPositions,
|
|
|
|
fns: inlinedFunctions
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.addScriptSource = function (id, url, source) {
|
2021-06-07 12:57:16 +00:00
|
|
|
const script = new Script(id);
|
|
|
|
script.update(url, source);
|
|
|
|
this.scripts_[id] = script;
|
2020-09-28 13:50:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.deoptCode = function (
|
|
|
|
timestamp, code, inliningId, scriptOffset, bailoutType,
|
|
|
|
sourcePositionText, deoptReasonText) {
|
|
|
|
let entry = this.codeMap_.findDynamicEntryByStartAddress(code);
|
|
|
|
if (entry) {
|
|
|
|
let codeId = entry.codeId;
|
|
|
|
if (!this.codeEntries_[codeId].deopt) {
|
|
|
|
// Only add the deopt if there was no deopt before.
|
|
|
|
// The subsequent deoptimizations should be lazy deopts for
|
|
|
|
// other on-stack activations.
|
|
|
|
this.codeEntries_[codeId].deopt = {
|
|
|
|
tm: timestamp,
|
|
|
|
inliningId: inliningId,
|
|
|
|
scriptOffset: scriptOffset,
|
|
|
|
posText: sourcePositionText,
|
|
|
|
reason: deoptReasonText,
|
2020-11-10 11:47:40 +00:00
|
|
|
bailoutType: bailoutType,
|
2020-09-28 13:50:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.deleteCode = function (start) {
|
|
|
|
try {
|
|
|
|
this.codeMap_.deleteCode(start);
|
|
|
|
} catch (e) {
|
2020-11-10 11:47:40 +00:00
|
|
|
printErr(`Delete: unknown address ${start}`);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.moveFunc = function (from, to) {
|
|
|
|
if (this.codeMap_.findDynamicEntryByStartAddress(from)) {
|
|
|
|
this.codeMap_.moveCode(from, to);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.findEntry = function (addr) {
|
|
|
|
return this.codeMap_.findEntry(addr);
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonProfile.prototype.recordTick = function (time_ns, vmState, stack) {
|
|
|
|
// TODO(jarin) Resolve the frame-less case (when top of stack is
|
|
|
|
// known code).
|
2020-11-10 11:47:40 +00:00
|
|
|
const processedStack = [];
|
|
|
|
for (let i = 0; i < stack.length; i++) {
|
|
|
|
const resolved = this.codeMap_.findAddress(stack[i]);
|
2020-09-28 13:50:56 +00:00
|
|
|
if (resolved) {
|
|
|
|
processedStack.push(resolved.entry.codeId, resolved.offset);
|
|
|
|
} else {
|
|
|
|
processedStack.push(-1, stack[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.ticks_.push({ tm: time_ns, vm: vmState, s: processedStack });
|
|
|
|
};
|
|
|
|
|
|
|
|
function writeJson(s) {
|
|
|
|
write(JSON.stringify(s, null, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonProfile.prototype.writeJson = function () {
|
|
|
|
// Write out the JSON in a partially manual way to avoid creating too-large
|
|
|
|
// strings in one JSON.stringify call when there are a lot of ticks.
|
|
|
|
write('{\n')
|
|
|
|
|
|
|
|
write(' "code": ');
|
|
|
|
writeJson(this.codeEntries_);
|
|
|
|
write(',\n');
|
|
|
|
|
|
|
|
write(' "functions": ');
|
|
|
|
writeJson(this.functionEntries_);
|
|
|
|
write(',\n');
|
|
|
|
|
|
|
|
write(' "ticks": [\n');
|
2020-11-10 11:47:40 +00:00
|
|
|
for (let i = 0; i < this.ticks_.length; i++) {
|
2020-09-28 13:50:56 +00:00
|
|
|
write(' ');
|
|
|
|
writeJson(this.ticks_[i]);
|
|
|
|
if (i < this.ticks_.length - 1) {
|
|
|
|
write(',\n');
|
|
|
|
} else {
|
|
|
|
write('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write(' ],\n');
|
|
|
|
|
|
|
|
write(' "scripts": ');
|
|
|
|
writeJson(this.scripts_);
|
|
|
|
|
|
|
|
write('}\n');
|
|
|
|
};
|