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