Commit a8eea879 authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

[tools] Port more tools to ES6 classes

Convert Profile, CodeMap and their helpers to ES6 classes.
Code cleanup will happen in a separate step.

Bug: v8:10667
Change-Id: Icfb28f6d9ef7f00efba93b347fdf210a9af36a49
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2509591
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70969}
parent 73ed5430
......@@ -25,12 +25,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import { CodeMap } from "../../../tools/codemap.mjs";
function newCodeEntry(size, name) {
return new CodeMap.CodeEntry(size, name);
};
import { CodeMap, CodeEntry } from "../../../tools/codemap.mjs";
function assertEntry(codeMap, expected_name, addr) {
var entry = codeMap.findEntry(addr);
......@@ -46,9 +41,9 @@ function assertNoEntry(codeMap, addr) {
(function testLibrariesAndStaticCode() {
var codeMap = new CodeMap();
codeMap.addLibrary(0x1500, newCodeEntry(0x3000, 'lib1'));
codeMap.addLibrary(0x15500, newCodeEntry(0x5000, 'lib2'));
codeMap.addLibrary(0x155500, newCodeEntry(0x10000, 'lib3'));
codeMap.addLibrary(0x1500, new CodeEntry(0x3000, 'lib1'));
codeMap.addLibrary(0x15500, new CodeEntry(0x5000, 'lib2'));
codeMap.addLibrary(0x155500, new CodeEntry(0x10000, 'lib3'));
assertNoEntry(codeMap, 0);
assertNoEntry(codeMap, 0x1500 - 1);
assertEntry(codeMap, 'lib1', 0x1500);
......@@ -70,9 +65,9 @@ function assertNoEntry(codeMap, addr) {
assertNoEntry(codeMap, 0x155500 + 0x10000);
assertNoEntry(codeMap, 0xFFFFFFFF);
codeMap.addStaticCode(0x1510, newCodeEntry(0x30, 'lib1-f1'));
codeMap.addStaticCode(0x1600, newCodeEntry(0x50, 'lib1-f2'));
codeMap.addStaticCode(0x15520, newCodeEntry(0x100, 'lib2-f1'));
codeMap.addStaticCode(0x1510, new CodeEntry(0x30, 'lib1-f1'));
codeMap.addStaticCode(0x1600, new CodeEntry(0x50, 'lib1-f2'));
codeMap.addStaticCode(0x15520, new CodeEntry(0x100, 'lib2-f1'));
assertEntry(codeMap, 'lib1', 0x1500);
assertEntry(codeMap, 'lib1', 0x1510 - 1);
assertEntry(codeMap, 'lib1-f1', 0x1510);
......@@ -96,10 +91,10 @@ function assertNoEntry(codeMap, addr) {
(function testDynamicCode() {
var codeMap = new CodeMap();
codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
codeMap.addCode(0x1900, newCodeEntry(0x50, 'code3'));
codeMap.addCode(0x1950, newCodeEntry(0x10, 'code4'));
codeMap.addCode(0x1500, new CodeEntry(0x200, 'code1'));
codeMap.addCode(0x1700, new CodeEntry(0x100, 'code2'));
codeMap.addCode(0x1900, new CodeEntry(0x50, 'code3'));
codeMap.addCode(0x1950, new CodeEntry(0x10, 'code4'));
assertNoEntry(codeMap, 0);
assertNoEntry(codeMap, 0x1500 - 1);
assertEntry(codeMap, 'code1', 0x1500);
......@@ -122,8 +117,8 @@ function assertNoEntry(codeMap, addr) {
(function testCodeMovesAndDeletions() {
var codeMap = new CodeMap();
codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
codeMap.addCode(0x1500, new CodeEntry(0x200, 'code1'));
codeMap.addCode(0x1700, new CodeEntry(0x100, 'code2'));
assertEntry(codeMap, 'code1', 0x1500);
assertEntry(codeMap, 'code2', 0x1700);
codeMap.moveCode(0x1500, 0x1800);
......@@ -139,8 +134,8 @@ function assertNoEntry(codeMap, addr) {
(function testDynamicNamesDuplicates() {
var codeMap = new CodeMap();
// Code entries with same names but different addresses.
codeMap.addCode(0x1500, newCodeEntry(0x200, 'code'));
codeMap.addCode(0x1700, newCodeEntry(0x100, 'code'));
codeMap.addCode(0x1500, new CodeEntry(0x200, 'code'));
codeMap.addCode(0x1700, new CodeEntry(0x100, 'code'));
assertEntry(codeMap, 'code', 0x1500);
assertEntry(codeMap, 'code {1}', 0x1700);
// Test name stability.
......@@ -151,9 +146,9 @@ function assertNoEntry(codeMap, addr) {
(function testStaticEntriesExport() {
var codeMap = new CodeMap();
codeMap.addStaticCode(0x1500, newCodeEntry(0x3000, 'lib1'));
codeMap.addStaticCode(0x15500, newCodeEntry(0x5000, 'lib2'));
codeMap.addStaticCode(0x155500, newCodeEntry(0x10000, 'lib3'));
codeMap.addStaticCode(0x1500, new CodeEntry(0x3000, 'lib1'));
codeMap.addStaticCode(0x15500, new CodeEntry(0x5000, 'lib2'));
codeMap.addStaticCode(0x155500, new CodeEntry(0x10000, 'lib3'));
var allStatics = codeMap.getAllStaticEntries();
allStatics = allStatics.map(String);
allStatics.sort();
......@@ -163,9 +158,9 @@ function assertNoEntry(codeMap, addr) {
(function testDynamicEntriesExport() {
var codeMap = new CodeMap();
codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
codeMap.addCode(0x1900, newCodeEntry(0x50, 'code3'));
codeMap.addCode(0x1500, new CodeEntry(0x200, 'code1'));
codeMap.addCode(0x1700, new CodeEntry(0x100, 'code2'));
codeMap.addCode(0x1900, new CodeEntry(0x50, 'code3'));
var allDynamics = codeMap.getAllDynamicEntries();
allDynamics = allDynamics.map(String);
allDynamics.sort();
......
......@@ -32,124 +32,114 @@ import { SplayTree } from "./splaytree.mjs";
*
* @constructor
*/
export function CodeMap() {
export class CodeMap {
/**
* Dynamic code entries. Used for JIT compiled code.
*/
this.dynamics_ = new SplayTree();
dynamics_ = new SplayTree();
/**
* Name generator for entries having duplicate names.
*/
this.dynamicsNameGen_ = new CodeMap.NameGenerator();
dynamicsNameGen_ = new NameGenerator();
/**
* Static code entries. Used for statically compiled code.
*/
this.statics_ = new SplayTree();
statics_ = new SplayTree();
/**
* Libraries entries. Used for the whole static code libraries.
*/
this.libraries_ = new SplayTree();
libraries_ = new SplayTree();
/**
* Map of memory pages occupied with static code.
*/
this.pages_ = [];
};
pages_ = [];
/**
/**
* The number of alignment bits in a page address.
*/
CodeMap.PAGE_ALIGNMENT = 12;
static PAGE_ALIGNMENT = 12;
/**
/**
* Page size in bytes.
*/
CodeMap.PAGE_SIZE =
1 << CodeMap.PAGE_ALIGNMENT;
static PAGE_SIZE = 1 << CodeMap.PAGE_ALIGNMENT;
/**
/**
* Adds a dynamic (i.e. moveable and discardable) code entry.
*
* @param {number} start The starting address.
* @param {CodeMap.CodeEntry} codeEntry Code entry object.
*/
CodeMap.prototype.addCode = function(start, codeEntry) {
addCode(start, codeEntry) {
this.deleteAllCoveredNodes_(this.dynamics_, start, start + codeEntry.size);
this.dynamics_.insert(start, codeEntry);
};
}
/**
/**
* Moves a dynamic code entry. Throws an exception if there is no dynamic
* code entry with the specified starting address.
*
* @param {number} from The starting address of the entry being moved.
* @param {number} to The destination address.
*/
CodeMap.prototype.moveCode = function(from, to) {
moveCode(from, to) {
var removedNode = this.dynamics_.remove(from);
this.deleteAllCoveredNodes_(this.dynamics_, to, to + removedNode.value.size);
this.dynamics_.insert(to, removedNode.value);
};
}
/**
/**
* Discards a dynamic code entry. Throws an exception if there is no dynamic
* code entry with the specified starting address.
*
* @param {number} start The starting address of the entry being deleted.
*/
CodeMap.prototype.deleteCode = function(start) {
deleteCode(start) {
var removedNode = this.dynamics_.remove(start);
};
}
/**
/**
* Adds a library entry.
*
* @param {number} start The starting address.
* @param {CodeMap.CodeEntry} codeEntry Code entry object.
*/
CodeMap.prototype.addLibrary = function(
start, codeEntry) {
addLibrary(start, codeEntry) {
this.markPages_(start, start + codeEntry.size);
this.libraries_.insert(start, codeEntry);
};
}
/**
/**
* Adds a static code entry.
*
* @param {number} start The starting address.
* @param {CodeMap.CodeEntry} codeEntry Code entry object.
*/
CodeMap.prototype.addStaticCode = function(
start, codeEntry) {
addStaticCode(start, codeEntry) {
this.statics_.insert(start, codeEntry);
};
}
/**
/**
* @private
*/
CodeMap.prototype.markPages_ = function(start, end) {
markPages_(start, end) {
for (var addr = start; addr <= end;
addr += CodeMap.PAGE_SIZE) {
this.pages_[(addr / CodeMap.PAGE_SIZE)|0] = 1;
}
};
}
/**
/**
* @private
*/
CodeMap.prototype.deleteAllCoveredNodes_ = function(tree, start, end) {
deleteAllCoveredNodes_(tree, start, end) {
var to_delete = [];
var addr = end - 1;
while (addr >= start) {
......@@ -160,34 +150,31 @@ CodeMap.prototype.deleteAllCoveredNodes_ = function(tree, start, end) {
addr = start2 - 1;
}
for (var i = 0, l = to_delete.length; i < l; ++i) tree.remove(to_delete[i]);
};
}
/**
/**
* @private
*/
CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) {
isAddressBelongsTo_(addr, node) {
return addr >= node.key && addr < (node.key + node.value.size);
};
}
/**
/**
* @private
*/
CodeMap.prototype.findInTree_ = function(tree, addr) {
findInTree_(tree, addr) {
var node = tree.findGreatestLessThan(addr);
return node && this.isAddressBelongsTo_(addr, node) ? node : null;
};
}
/**
/**
* Finds a code entry that contains the specified address. Both static and
* dynamic code entries are considered. Returns the code entry and the offset
* within the entry.
*
* @param {number} addr Address.
*/
CodeMap.prototype.findAddress = function(addr) {
findAddress(addr) {
var pageAddr = (addr / CodeMap.PAGE_SIZE)|0;
if (pageAddr in this.pages_) {
// Static code entries can contain "holes" of unnamed code.
......@@ -213,71 +200,64 @@ CodeMap.prototype.findAddress = function(addr) {
return { entry : entry, offset : addr - dynaEntry.key };
}
return null;
};
}
/**
/**
* Finds a code entry that contains the specified address. Both static and
* dynamic code entries are considered.
*
* @param {number} addr Address.
*/
CodeMap.prototype.findEntry = function(addr) {
findEntry(addr) {
var result = this.findAddress(addr);
return result ? result.entry : null;
};
}
/**
/**
* Returns a dynamic code entry using its starting address.
*
* @param {number} addr Address.
*/
CodeMap.prototype.findDynamicEntryByStartAddress =
function(addr) {
findDynamicEntryByStartAddress(addr) {
var node = this.dynamics_.find(addr);
return node ? node.value : null;
};
}
/**
/**
* Returns an array of all dynamic code entries.
*/
CodeMap.prototype.getAllDynamicEntries = function() {
getAllDynamicEntries() {
return this.dynamics_.exportValues();
};
}
/**
/**
* Returns an array of pairs of all dynamic code entries and their addresses.
*/
CodeMap.prototype.getAllDynamicEntriesWithAddresses = function() {
getAllDynamicEntriesWithAddresses() {
return this.dynamics_.exportKeysAndValues();
};
}
/**
/**
* Returns an array of all static code entries.
*/
CodeMap.prototype.getAllStaticEntries = function() {
getAllStaticEntries() {
return this.statics_.exportValues();
};
}
/**
/**
* Returns an array of pairs of all static code entries and their addresses.
*/
CodeMap.prototype.getAllStaticEntriesWithAddresses = function() {
getAllStaticEntriesWithAddresses() {
return this.statics_.exportKeysAndValues();
};
}
/**
/**
* Returns an array of all libraries entries.
*/
CodeMap.prototype.getAllLibrariesEntries = function() {
getAllLibrariesEntries() {
return this.libraries_.exportValues();
};
}
}
/**
......@@ -288,34 +268,31 @@ CodeMap.prototype.getAllLibrariesEntries = function() {
* @param {string} opt_type Code entry type, e.g. SHARED_LIB, CPP.
* @constructor
*/
CodeMap.CodeEntry = function(size, opt_name, opt_type) {
export class CodeEntry {
constructor(size, opt_name, opt_type) {
this.size = size;
this.name = opt_name || '';
this.type = opt_type || '';
this.nameUpdated_ = false;
};
}
CodeMap.CodeEntry.prototype.getName = function() {
getName() {
return this.name;
};
}
CodeMap.CodeEntry.prototype.toString = function() {
toString() {
return this.name + ': ' + this.size.toString(16);
};
CodeMap.NameGenerator = function() {
this.knownNames_ = {};
};
}
}
CodeMap.NameGenerator.prototype.getName = function(name) {
class NameGenerator {
knownNames_ = { __proto__:null }
getName(name) {
if (!(name in this.knownNames_)) {
this.knownNames_[name] = 0;
return name;
}
var count = ++this.knownNames_[name];
return name + ' {' + count + '}';
};
};
}
......@@ -3,7 +3,7 @@
// found in the LICENSE file.
import { LogReader, parseString } from "./logreader.mjs";
import { CodeMap } from "./codemap.mjs";
import { CodeMap, CodeEntry } from "./codemap.mjs";
export {
ArgumentsProcessor, UnixCppEntriesProvider,
WindowsCppEntriesProvider, MacCppEntriesProvider,
......@@ -11,51 +11,51 @@ export {
import { inherits } from "./tickprocessor.mjs";
export function CppProcessor(cppEntriesProvider, timedRange, pairwiseTimedRange) {
LogReader.call(this, {
'shared-library': { parsers: [parseString, parseInt, parseInt, parseInt],
export class CppProcessor extends LogReader {
constructor(cppEntriesProvider, timedRange, pairwiseTimedRange) {
super({}, timedRange, pairwiseTimedRange);
this.dispatchTable_ = {
'shared-library': {
parsers: [parseString, parseInt, parseInt, parseInt],
processor: this.processSharedLibrary }
}, timedRange, pairwiseTimedRange);
};
this.cppEntriesProvider_ = cppEntriesProvider;
this.codeMap_ = new CodeMap();
this.lastLogFileName_ = null;
}
inherits(CppProcessor, LogReader);
}
/**
/**
* @override
*/
CppProcessor.prototype.printError = function(str) {
printError(str) {
print(str);
};
};
CppProcessor.prototype.processLogFile = function(fileName) {
processLogFile(fileName) {
this.lastLogFileName_ = fileName;
var line;
while (line = readline()) {
this.processLogLine(line);
}
};
};
CppProcessor.prototype.processLogFileInTest = function(fileName) {
processLogFileInTest(fileName) {
// Hack file name to avoid dealing with platform specifics.
this.lastLogFileName_ = 'v8.log';
var contents = readFile(fileName);
this.processLogChunk(contents);
};
};
CppProcessor.prototype.processSharedLibrary = function(
name, startAddr, endAddr, aslrSlide) {
processSharedLibrary(name, startAddr, endAddr, aslrSlide) {
var self = this;
var libFuncs = this.cppEntriesProvider_.parseVmSymbols(
name, startAddr, endAddr, aslrSlide, function(fName, fStart, fEnd) {
var entry = new CodeMap.CodeEntry(fEnd - fStart, fName, 'CPP');
var entry = new CodeEntry(fEnd - fStart, fName, 'CPP');
self.codeMap_.addStaticCode(fStart, entry);
});
};
};
CppProcessor.prototype.dumpCppSymbols = function() {
dumpCppSymbols() {
var staticEntries = this.codeMap_.getAllStaticEntriesWithAddresses();
var total = staticEntries.length;
for (var i = 0; i < total; ++i) {
......@@ -64,4 +64,5 @@ CppProcessor.prototype.dumpCppSymbols = function() {
'"' + entry[1].name + '"'];
print(printValues.join(','));
}
};
}
}
......@@ -34,4 +34,4 @@ fi
# nm spits out 'no symbols found' messages to stderr.
cat $log_file | $d8_exec --enable-os-system \
--module $tools_path/tickprocessor-driver.mjs -- $@ 2>/dev/null
--module $tools_path/tickprocessor-driver.mjs -- $@
......@@ -25,7 +25,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import { CodeMap } from "./codemap.mjs";
import { CodeMap, CodeEntry } from "./codemap.mjs";
import { ConsArray } from "./consarray.mjs";
// TODO: move to separate modules
......@@ -42,7 +42,6 @@ export class SourcePosition {
}
export class Script {
constructor(id, name, source) {
this.id = id;
this.name = name;
......@@ -81,54 +80,49 @@ export class Script {
*
* @constructor
*/
export function Profile() {
this.codeMap_ = new CodeMap();
this.topDownTree_ = new CallTree();
this.bottomUpTree_ = new CallTree();
this.c_entries_ = {};
this.ticks_ = [];
this.scripts_ = [];
this.urlToScript_ = new Map();
};
/**
export class Profile {
codeMap_ = new CodeMap();
topDownTree_ = new CallTree();
bottomUpTree_ = new CallTree();
c_entries_ = {};
ticks_ = [];
scripts_ = [];
urlToScript_ = new Map();
/**
* Returns whether a function with the specified name must be skipped.
* Should be overriden by subclasses.
*
* @param {string} name Function name.
*/
Profile.prototype.skipThisFunction = function (name) {
skipThisFunction(name) {
return false;
};
}
/**
/**
* Enum for profiler operations that involve looking up existing
* code entries.
*
* @enum {number}
*/
Profile.Operation = {
static Operation = {
MOVE: 0,
DELETE: 1,
TICK: 2
};
}
/**
/**
* Enum for code state regarding its dynamic optimization.
*
* @enum {number}
*/
Profile.CodeState = {
static CodeState = {
COMPILED: 0,
OPTIMIZABLE: 1,
OPTIMIZED: 2
};
}
/**
/**
* Called whenever the specified operation has failed finding a function
* containing the specified address. Should be overriden by subclasses.
* See the Profile.Operation enum for the list of
......@@ -140,44 +134,35 @@ Profile.CodeState = {
* during stack strace processing, specifies a position of the frame
* containing the address.
*/
Profile.prototype.handleUnknownCode = function (
operation, addr, opt_stackPos) {
};
handleUnknownCode(operation, addr, opt_stackPos) {}
/**
/**
* Registers a library.
*
* @param {string} name Code entry name.
* @param {number} startAddr Starting address.
* @param {number} endAddr Ending address.
*/
Profile.prototype.addLibrary = function (
name, startAddr, endAddr) {
var entry = new CodeMap.CodeEntry(
endAddr - startAddr, name, 'SHARED_LIB');
addLibrary(name, startAddr, endAddr) {
var entry = new CodeEntry(endAddr - startAddr, name, 'SHARED_LIB');
this.codeMap_.addLibrary(startAddr, entry);
return entry;
};
}
/**
/**
* Registers statically compiled code entry.
*
* @param {string} name Code entry name.
* @param {number} startAddr Starting address.
* @param {number} endAddr Ending address.
*/
Profile.prototype.addStaticCode = function (
name, startAddr, endAddr) {
var entry = new CodeMap.CodeEntry(
endAddr - startAddr, name, 'CPP');
addStaticCode(name, startAddr, endAddr) {
var entry = new CodeEntry(endAddr - startAddr, name, 'CPP');
this.codeMap_.addStaticCode(startAddr, entry);
return entry;
};
}
/**
/**
* Registers dynamic (JIT-compiled) code entry.
*
* @param {string} type Code entry type.
......@@ -185,15 +170,13 @@ Profile.prototype.addStaticCode = function (
* @param {number} start Starting address.
* @param {number} size Code entry size.
*/
Profile.prototype.addCode = function (
type, name, timestamp, start, size) {
var entry = new Profile.DynamicCodeEntry(size, type, name);
addCode(type, name, timestamp, start, size) {
var entry = new DynamicCodeEntry(size, type, name);
this.codeMap_.addCode(start, entry);
return entry;
};
}
/**
/**
* Registers dynamic (JIT-compiled) code entry.
*
* @param {string} type Code entry type.
......@@ -203,13 +186,12 @@ Profile.prototype.addCode = function (
* @param {number} funcAddr Shared function object address.
* @param {Profile.CodeState} state Optimization state.
*/
Profile.prototype.addFuncCode = function (
type, name, timestamp, start, size, funcAddr, 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.
var func = this.codeMap_.findDynamicEntryByStartAddress(funcAddr);
if (!func) {
func = new Profile.FunctionEntry(name);
func = new FunctionEntry(name);
this.codeMap_.addCode(funcAddr, func);
} else if (func.name !== name) {
// Function object has been overwritten with a new one.
......@@ -226,115 +208,108 @@ Profile.prototype.addFuncCode = function (
}
}
if (!entry) {
entry = new Profile.DynamicFuncCodeEntry(size, type, func, state);
entry = new DynamicFuncCodeEntry(size, type, func, state);
this.codeMap_.addCode(start, entry);
}
return entry;
};
}
/**
/**
* Reports about moving of a dynamic code entry.
*
* @param {number} from Current code entry address.
* @param {number} to New code entry address.
*/
Profile.prototype.moveCode = function (from, to) {
moveCode(from, to) {
try {
this.codeMap_.moveCode(from, to);
} catch (e) {
this.handleUnknownCode(Profile.Operation.MOVE, from);
}
};
}
Profile.prototype.deoptCode = function (
timestamp, code, inliningId, scriptOffset, bailoutType,
deoptCode( timestamp, code, inliningId, scriptOffset, bailoutType,
sourcePositionText, deoptReasonText) {
};
}
/**
/**
* Reports about deletion of a dynamic code entry.
*
* @param {number} start Starting address.
*/
Profile.prototype.deleteCode = function (start) {
deleteCode(start) {
try {
this.codeMap_.deleteCode(start);
} catch (e) {
this.handleUnknownCode(Profile.Operation.DELETE, start);
}
};
}
/**
/**
* Adds source positions for given code.
*/
Profile.prototype.addSourcePositions = function (
start, script, startPos, endPos, sourcePositions, inliningPositions,
inlinedFunctions) {
addSourcePositions(start, script, startPos, endPos, sourcePositions,
inliningPositions, inlinedFunctions) {
// CLI does not need source code => ignore.
};
}
/**
/**
* Adds script source code.
*/
Profile.prototype.addScriptSource = function (id, url, source) {
addScriptSource(id, url, source) {
const script = new Script(id, url, source);
this.scripts_[id] = script;
this.urlToScript_.set(url, script);
};
}
/**
/**
* Adds script source code.
*/
Profile.prototype.getScript = function (url) {
getScript(url) {
return this.urlToScript_.get(url);
};
}
/**
/**
* Reports about moving of a dynamic code entry.
*
* @param {number} from Current code entry address.
* @param {number} to New code entry address.
*/
Profile.prototype.moveFunc = function (from, to) {
moveFunc(from, to) {
if (this.codeMap_.findDynamicEntryByStartAddress(from)) {
this.codeMap_.moveCode(from, to);
}
};
}
/**
/**
* Retrieves a code entry by an address.
*
* @param {number} addr Entry address.
*/
Profile.prototype.findEntry = function (addr) {
findEntry(addr) {
return this.codeMap_.findEntry(addr);
};
}
/**
/**
* Records a tick event. Stack must contain a sequence of
* addresses starting with the program counter value.
*
* @param {Array<number>} stack Stack sample.
*/
Profile.prototype.recordTick = function (time_ns, vmState, stack) {
recordTick(time_ns, vmState, stack) {
var processedStack = this.resolveAndFilterFuncs_(stack);
this.bottomUpTree_.addPath(processedStack);
processedStack.reverse();
this.topDownTree_.addPath(processedStack);
};
}
/**
/**
* Translates addresses into function names and filters unneeded
* functions.
*
* @param {Array<number>} stack Stack sample.
*/
Profile.prototype.resolveAndFilterFuncs_ = function (stack) {
resolveAndFilterFuncs_(stack) {
var result = [];
var last_seen_c_function = '';
var look_for_first_c_function = false;
......@@ -367,58 +342,53 @@ Profile.prototype.resolveAndFilterFuncs_ = function (stack) {
}
}
return result;
};
}
/**
/**
* Performs a BF traversal of the top down call graph.
*
* @param {function(CallTree.Node)} f Visitor function.
* @param {function(CallTreeNode)} f Visitor function.
*/
Profile.prototype.traverseTopDownTree = function (f) {
traverseTopDownTree(f) {
this.topDownTree_.traverse(f);
};
}
/**
/**
* Performs a BF traversal of the bottom up call graph.
*
* @param {function(CallTree.Node)} f Visitor function.
* @param {function(CallTreeNode)} f Visitor function.
*/
Profile.prototype.traverseBottomUpTree = function (f) {
traverseBottomUpTree(f) {
this.bottomUpTree_.traverse(f);
};
}
/**
/**
* 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.
*/
Profile.prototype.getTopDownProfile = function (opt_label) {
getTopDownProfile(opt_label) {
return this.getTreeProfile_(this.topDownTree_, opt_label);
};
}
/**
/**
* 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.
*/
Profile.prototype.getBottomUpProfile = function (opt_label) {
getBottomUpProfile(opt_label) {
return this.getTreeProfile_(this.bottomUpTree_, opt_label);
};
}
/**
/**
* Helper function for calculating a tree profile.
*
* @param {Profile.CallTree} tree Call tree.
* @param {string} opt_label Node label.
*/
Profile.prototype.getTreeProfile_ = function (tree, opt_label) {
getTreeProfile_(tree, opt_label) {
if (!opt_label) {
tree.computeTotalWeights();
return tree;
......@@ -427,16 +397,15 @@ Profile.prototype.getTreeProfile_ = function (tree, opt_label) {
subTree.computeTotalWeights();
return subTree;
}
};
}
/**
/**
* 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.
*/
Profile.prototype.getFlatProfile = function (opt_label) {
getFlatProfile(opt_label) {
var counters = new CallTree();
var rootLabel = opt_label || CallTree.ROOT_NODE_LABEL;
var precs = {};
......@@ -482,39 +451,32 @@ Profile.prototype.getFlatProfile = function (opt_label) {
counters.getRoot().totalWeight = root.totalWeight;
}
return counters;
};
Profile.CEntryNode = function (name, ticks) {
this.name = name;
this.ticks = ticks;
}
}
Profile.prototype.getCEntryProfile = function () {
var result = [new Profile.CEntryNode("TOTAL", 0)];
getCEntryProfile() {
var result = [new CEntryNode("TOTAL", 0)];
var total_ticks = 0;
for (var f in this.c_entries_) {
var ticks = this.c_entries_[f];
total_ticks += ticks;
result.push(new Profile.CEntryNode(f, ticks));
result.push(new CEntryNode(f, ticks));
}
result[0].ticks = total_ticks; // Sorting will keep this at index 0.
result.sort(function (n1, n2) {
return n2.ticks - n1.ticks || (n2.name < n1.name ? -1 : 1)
});
return result;
}
}
/**
/**
* Cleans up function entries that are not referenced by code entries.
*/
Profile.prototype.cleanUpFuncEntries = function () {
cleanUpFuncEntries() {
var referencedFuncEntries = [];
var entries = this.codeMap_.getAllDynamicEntriesWithAddresses();
for (var i = 0, l = entries.length; i < l; ++i) {
if (entries[i][1].constructor === Profile.FunctionEntry) {
if (entries[i][1].constructor === FunctionEntry) {
entries[i][1].used = false;
}
}
......@@ -524,12 +486,20 @@ Profile.prototype.cleanUpFuncEntries = function () {
}
}
for (var i = 0, l = entries.length; i < l; ++i) {
if (entries[i][1].constructor === Profile.FunctionEntry &&
if (entries[i][1].constructor === FunctionEntry &&
!entries[i][1].used) {
this.codeMap_.deleteCode(entries[i][0]);
}
}
};
}
}
class CEntryNode {
constructor(name, ticks) {
this.name = name;
this.ticks = ticks;
}
}
/**
......@@ -540,35 +510,30 @@ Profile.prototype.cleanUpFuncEntries = function () {
* @param {string} name Function name.
* @constructor
*/
Profile.DynamicCodeEntry = function (size, type, name) {
CodeMap.CodeEntry.call(this, size, name, type);
};
class DynamicCodeEntry extends CodeEntry {
constructor(size, type, name) {
super(size, name, type);
}
/**
* Returns node name.
*/
Profile.DynamicCodeEntry.prototype.getName = function () {
getName() {
return this.type + ': ' + this.name;
};
}
/**
/**
* Returns raw node name (without type decoration).
*/
Profile.DynamicCodeEntry.prototype.getRawName = function () {
getRawName() {
return this.name;
};
}
Profile.DynamicCodeEntry.prototype.isJSFunction = function () {
isJSFunction() {
return false;
};
}
Profile.DynamicCodeEntry.prototype.toString = function () {
toString() {
return this.getName() + ': ' + this.size.toString(16);
};
}
}
/**
......@@ -576,51 +541,42 @@ Profile.DynamicCodeEntry.prototype.toString = function () {
*
* @param {number} size Code size.
* @param {string} type Code type.
* @param {Profile.FunctionEntry} func Shared function entry.
* @param {FunctionEntry} func Shared function entry.
* @param {Profile.CodeState} state Code optimization state.
* @constructor
*/
Profile.DynamicFuncCodeEntry = function (size, type, func, state) {
CodeMap.CodeEntry.call(this, size, '', type);
class DynamicFuncCodeEntry extends CodeEntry {
constructor(size, type, func, state) {
super(size, '', type);
this.func = func;
this.state = state;
};
Profile.DynamicFuncCodeEntry.STATE_PREFIX = ["", "~", "*"];
}
/**
* Returns state.
*/
Profile.DynamicFuncCodeEntry.prototype.getState = function () {
return Profile.DynamicFuncCodeEntry.STATE_PREFIX[this.state];
};
static STATE_PREFIX = ["", "~", "*"];
getState() {
return DynamicFuncCodeEntry.STATE_PREFIX[this.state];
}
/**
* Returns node name.
*/
Profile.DynamicFuncCodeEntry.prototype.getName = function () {
getName() {
var name = this.func.getName();
return this.type + ': ' + this.getState() + name;
};
}
/**
/**
* Returns raw node name (without type decoration).
*/
Profile.DynamicFuncCodeEntry.prototype.getRawName = function () {
getRawName() {
return this.func.getName();
};
}
Profile.DynamicFuncCodeEntry.prototype.isJSFunction = function () {
isJSFunction() {
return true;
};
}
Profile.DynamicFuncCodeEntry.prototype.toString = function () {
toString() {
return this.getName() + ': ' + this.size.toString(16);
};
}
}
/**
* Creates a shared function object entry.
......@@ -628,15 +584,15 @@ Profile.DynamicFuncCodeEntry.prototype.toString = function () {
* @param {string} name Function name.
* @constructor
*/
Profile.FunctionEntry = function (name) {
CodeMap.CodeEntry.call(this, 0, name);
};
class FunctionEntry extends CodeEntry {
constructor(name) {
super(0, name);
}
/**
/**
* Returns node name.
*/
Profile.FunctionEntry.prototype.getName = function () {
getName() {
var name = this.name;
if (name.length == 0) {
name = '<anonymous>';
......@@ -645,47 +601,36 @@ Profile.FunctionEntry.prototype.getName = function () {
name = '<anonymous>' + name;
}
return name;
};
Profile.FunctionEntry.prototype.toString = CodeMap.CodeEntry.prototype.toString;
}
}
/**
* Constructs a call graph.
*
* @constructor
*/
function CallTree() {
this.root_ = new CallTree.Node(
CallTree.ROOT_NODE_LABEL);
};
class CallTree {
root_ = new CallTreeNode(CallTree.ROOT_NODE_LABEL);
totalsComputed_ = false;
/**
/**
* The label of the root node.
*/
CallTree.ROOT_NODE_LABEL = '';
static ROOT_NODE_LABEL = '';
/**
* @private
*/
CallTree.prototype.totalsComputed_ = false;
/**
/**
* Returns the tree root.
*/
CallTree.prototype.getRoot = function () {
getRoot() {
return this.root_;
};
}
/**
/**
* Adds the specified call path, constructing nodes as necessary.
*
* @param {Array<string>} path Call path.
*/
CallTree.prototype.addPath = function (path) {
addPath(path) {
if (path.length == 0) {
return;
}
......@@ -695,22 +640,20 @@ CallTree.prototype.addPath = function (path) {
}
curr.selfWeight++;
this.totalsComputed_ = false;
};
}
/**
/**
* 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.
*/
CallTree.prototype.findOrAddChild = function (label) {
findOrAddChild(label) {
return this.root_.findOrAddChild(label);
};
}
/**
/**
* 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:
......@@ -726,9 +669,9 @@ CallTree.prototype.findOrAddChild = function (label) {
*
* @param {string} label The label of the new root node.
*/
CallTree.prototype.cloneSubtree = function (label) {
cloneSubtree(label) {
var subTree = new CallTree();
this.traverse(function (node, parent) {
this.traverse((node, parent) => {
if (!parent && node.label != label) {
return null;
}
......@@ -737,22 +680,18 @@ CallTree.prototype.cloneSubtree = function (label) {
return child;
});
return subTree;
};
}
/**
/**
* Computes total weights in the call graph.
*/
CallTree.prototype.computeTotalWeights = function () {
if (this.totalsComputed_) {
return;
}
computeTotalWeights() {
if (this.totalsComputed_) return;
this.root_.computeTotalWeight();
this.totalsComputed_ = true;
};
}
/**
/**
* 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:
......@@ -764,10 +703,10 @@ CallTree.prototype.computeTotalWeights = function () {
* return nodeClone;
* });
*
* @param {function(CallTree.Node, *)} f Visitor function.
* @param {function(CallTreeNode, *)} f Visitor function.
* The second parameter is the result of calling 'f' on the parent node.
*/
CallTree.prototype.traverse = function (f) {
traverse(f) {
var pairsToProcess = new ConsArray();
pairsToProcess.concat([{ node: this.root_, param: null }]);
while (!pairsToProcess.atEnd()) {
......@@ -775,148 +714,138 @@ CallTree.prototype.traverse = function (f) {
var node = pair.node;
var newParam = f(node, pair.param);
var morePairsToProcess = [];
node.forEachChild(function (child) {
node.forEachChild((child) => {
morePairsToProcess.push({ node: child, param: newParam });
});
pairsToProcess.concat(morePairsToProcess);
}
};
}
/**
/**
* Performs an indepth call graph traversal.
*
* @param {function(CallTree.Node)} enter A function called
* @param {function(CallTreeNode)} enter A function called
* prior to visiting node's children.
* @param {function(CallTree.Node)} exit A function called
* @param {function(CallTreeNode)} exit A function called
* after visiting node's children.
*/
CallTree.prototype.traverseInDepth = function (enter, exit) {
traverseInDepth(enter, exit) {
function traverse(node) {
enter(node);
node.forEachChild(traverse);
exit(node);
}
traverse(this.root_);
};
}
}
/**
* Constructs a call graph node.
*
* @param {string} label Node label.
* @param {CallTree.Node} opt_parent Node parent.
* @param {CallTreeNode} opt_parent Node parent.
*/
CallTree.Node = function (label, opt_parent) {
this.label = label;
this.parent = opt_parent;
this.children = {};
};
/**
class CallTreeNode {
/**
* Node self weight (how many times this node was the last node in
* a call path).
* @type {number}
*/
CallTree.Node.prototype.selfWeight = 0;
selfWeight = 0;
/**
/**
* Node total weight (includes weights of all children).
* @type {number}
*/
CallTree.Node.prototype.totalWeight = 0;
totalWeight = 0;
children = {};
constructor(label, opt_parent) {
this.label = label;
this.parent = opt_parent;
}
/**
/**
* Adds a child node.
*
* @param {string} label Child node label.
*/
CallTree.Node.prototype.addChild = function (label) {
var child = new CallTree.Node(label, this);
addChild(label) {
var child = new CallTreeNode(label, this);
this.children[label] = child;
return child;
};
}
/**
/**
* Computes node's total weight.
*/
CallTree.Node.prototype.computeTotalWeight =
function () {
computeTotalWeight() {
var totalWeight = this.selfWeight;
this.forEachChild(function (child) {
totalWeight += child.computeTotalWeight();
});
return this.totalWeight = totalWeight;
};
}
/**
/**
* Returns all node's children as an array.
*/
CallTree.Node.prototype.exportChildren = function () {
exportChildren() {
var result = [];
this.forEachChild(function (node) { result.push(node); });
return result;
};
}
/**
/**
* Finds an immediate child with the specified label.
*
* @param {string} label Child node label.
*/
CallTree.Node.prototype.findChild = function (label) {
findChild(label) {
return this.children[label] || null;
};
}
/**
/**
* Finds an immediate child with the specified label, creates a child
* node if necessary.
*
* @param {string} label Child node label.
*/
CallTree.Node.prototype.findOrAddChild = function (label) {
findOrAddChild(label) {
return this.findChild(label) || this.addChild(label);
};
}
/**
/**
* Calls the specified function for every child.
*
* @param {function(CallTree.Node)} f Visitor function.
* @param {function(CallTreeNode)} f Visitor function.
*/
CallTree.Node.prototype.forEachChild = function (f) {
forEachChild(f) {
for (var c in this.children) {
f(this.children[c]);
}
};
}
/**
/**
* Walks up from the current node up to the call tree root.
*
* @param {function(CallTree.Node)} f Visitor function.
* @param {function(CallTreeNode)} f Visitor function.
*/
CallTree.Node.prototype.walkUpToRoot = function (f) {
walkUpToRoot(f) {
for (var curr = this; curr != null; curr = curr.parent) {
f(curr);
}
};
}
/**
/**
* Tries to find a node with the specified path.
*
* @param {Array<string>} labels The path.
* @param {function(CallTree.Node)} opt_f Visitor function.
* @param {function(CallTreeNode)} opt_f Visitor function.
*/
CallTree.Node.prototype.descendToChild = function (
labels, opt_f) {
descendToChild(labels, opt_f) {
for (var pos = 0, curr = this; pos < labels.length && curr != null; pos++) {
var child = curr.findChild(labels[pos]);
if (opt_f) {
......@@ -925,7 +854,8 @@ CallTree.Node.prototype.descendToChild = function (
curr = child;
}
return curr;
};
}
}
export function JsonProfile() {
this.codeMap_ = new CodeMap();
......@@ -937,7 +867,7 @@ export function JsonProfile() {
JsonProfile.prototype.addLibrary = function (
name, startAddr, endAddr) {
var entry = new CodeMap.CodeEntry(
var entry = new CodeEntry(
endAddr - startAddr, name, 'SHARED_LIB');
this.codeMap_.addLibrary(startAddr, entry);
......@@ -948,7 +878,7 @@ JsonProfile.prototype.addLibrary = function (
JsonProfile.prototype.addStaticCode = function (
name, startAddr, endAddr) {
var entry = new CodeMap.CodeEntry(
var entry = new CodeEntry(
endAddr - startAddr, name, 'CPP');
this.codeMap_.addStaticCode(startAddr, entry);
......@@ -967,7 +897,7 @@ JsonProfile.prototype.addCode = function (
codeId = staticEntry.entry.codeId;
}
var entry = new CodeMap.CodeEntry(size, name, 'CODE');
var entry = new CodeEntry(size, name, 'CODE');
this.codeMap_.addCode(start, entry);
entry.codeId = codeId;
......@@ -987,7 +917,7 @@ JsonProfile.prototype.addFuncCode = function (
// it is safe to put them in a single code map.
var func = this.codeMap_.findDynamicEntryByStartAddress(funcAddr);
if (!func) {
var func = new CodeMap.CodeEntry(0, name, 'SFI');
var func = new CodeEntry(0, name, 'SFI');
this.codeMap_.addCode(funcAddr, func);
func.funcId = this.functionEntries_.length;
......@@ -1011,7 +941,7 @@ JsonProfile.prototype.addFuncCode = function (
}
}
if (!entry) {
entry = new CodeMap.CodeEntry(size, name, 'JS');
entry = new CodeEntry(size, name, 'JS');
this.codeMap_.addCode(start, entry);
entry.codeId = this.codeEntries_.length;
......@@ -1083,7 +1013,6 @@ JsonProfile.prototype.addScriptSource = function (id, url, source) {
this.scripts_[id] = new Script(id, url, source);
};
JsonProfile.prototype.deoptCode = function (
timestamp, code, inliningId, scriptOffset, bailoutType,
sourcePositionText, deoptReasonText) {
......
......@@ -36,9 +36,15 @@ export function inherits(childCtor, parentCtor) {
};
function V8Profile(separateIc, separateBytecodes, separateBuiltins,
separateStubs) {
Profile.call(this);
class V8Profile extends Profile {
static IC_RE =
/^(LoadGlobalIC: )|(Handler: )|(?:CallIC|LoadIC|StoreIC)|(?:Builtin: (?:Keyed)?(?:Load|Store)IC_)/;
static BYTECODES_RE = /^(BytecodeHandler: )/;
static BUILTINS_RE = /^(Builtin: )/;
static STUBS_RE = /^(Stub: )/;
constructor(separateIc, separateBytecodes, separateBuiltins, separateStubs) {
super();
var regexps = [];
if (!separateIc) regexps.push(V8Profile.IC_RE);
if (!separateBytecodes) regexps.push(V8Profile.BYTECODES_RE);
......@@ -52,15 +58,8 @@ function V8Profile(separateIc, separateBytecodes, separateBuiltins,
return false;
};
}
};
inherits(V8Profile, Profile);
V8Profile.IC_RE =
/^(LoadGlobalIC: )|(Handler: )|(?:CallIC|LoadIC|StoreIC)|(?:Builtin: (?:Keyed)?(?:Load|Store)IC_)/;
V8Profile.BYTECODES_RE = /^(BytecodeHandler: )/
V8Profile.BUILTINS_RE = /^(Builtin: )/
V8Profile.STUBS_RE = /^(Stub: )/
}
}
/**
......
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