Commit 190af788 authored by Camillo Bruni's avatar Camillo Bruni Committed by V8 LUCI CQ

[tools][system-analyzer] Speed up log parsing

Reduce the dispatching overhead in the hottest loop when parsing log-lines.

- Using a JSMap we can avoid internalizing strings
- Preprocess the dispatch table and only have varArgs or functions as
  parsers
- string[] seems to be slightly faster than string.charAt()

Bug: v8:10644
Change-Id: I03b13bdeecda1ad037191ff74e05142ceeb6533c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3571890Reviewed-by: 's avatarPatrick Thier <pthier@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79816}
parent ea96bd76
...@@ -42,7 +42,7 @@ export class CsvParser { ...@@ -42,7 +42,7 @@ export class CsvParser {
// Escape sequences of the form \x00 and \u0000; // Escape sequences of the form \x00 and \u0000;
let pos = 0; let pos = 0;
while (nextPos !== -1) { while (nextPos !== -1) {
const escapeIdentifier = string.charAt(nextPos + 1); const escapeIdentifier = string[nextPos + 1];
pos = nextPos + 2; pos = nextPos + 2;
if (escapeIdentifier === 'n') { if (escapeIdentifier === 'n') {
result += '\n'; result += '\n';
......
...@@ -12,13 +12,13 @@ export { ...@@ -12,13 +12,13 @@ export {
export class CppProcessor extends LogReader { export class CppProcessor extends LogReader {
constructor(cppEntriesProvider, timedRange, pairwiseTimedRange) { constructor(cppEntriesProvider, timedRange, pairwiseTimedRange) {
super({}, timedRange, pairwiseTimedRange); super(timedRange, pairwiseTimedRange);
this.dispatchTable_ = { this.setDispatchTable({
__proto__: null, __proto__: null,
'shared-library': { 'shared-library': {
parsers: [parseString, parseInt, parseInt, parseInt], parsers: [parseString, parseInt, parseInt, parseInt],
processor: this.processSharedLibrary } processor: this.processSharedLibrary }
}; });
this.cppEntriesProvider_ = cppEntriesProvider; this.cppEntriesProvider_ = cppEntriesProvider;
this.codeMap_ = new CodeMap(); this.codeMap_ = new CodeMap();
this.lastLogFileName_ = null; this.lastLogFileName_ = null;
......
...@@ -32,64 +32,65 @@ ...@@ -32,64 +32,65 @@
// Parses dummy variable for readability; // Parses dummy variable for readability;
export const parseString = 'parse-string'; export function parseString(field) { return field };
export const parseVarArgs = 'parse-var-args'; export const parseVarArgs = 'parse-var-args';
/** /**
* Base class for processing log files. * Base class for processing log files.
* *
* @param {Array.<Object>} dispatchTable A table used for parsing and processing
* log records.
* @param {boolean} timedRange Ignore ticks outside timed range. * @param {boolean} timedRange Ignore ticks outside timed range.
* @param {boolean} pairwiseTimedRange Ignore ticks outside pairs of timer * @param {boolean} pairwiseTimedRange Ignore ticks outside pairs of timer
* markers. * markers.
* @constructor * @constructor
*/ */
export class LogReader { export class LogReader {
constructor (dispatchTable, timedRange, pairwiseTimedRange) { constructor (timedRange, pairwiseTimedRange) {
/** this.dispatchTable_ = new Map();
* @type {Array.<Object>}
*/
this.dispatchTable_ = dispatchTable;
/**
* @type {boolean}
*/
this.timedRange_ = timedRange; this.timedRange_ = timedRange;
/**
* @type {boolean}
*/
this.pairwiseTimedRange_ = pairwiseTimedRange; this.pairwiseTimedRange_ = pairwiseTimedRange;
if (pairwiseTimedRange) { if (pairwiseTimedRange) this.timedRange_ = true;
this.timedRange_ = true;
}
/**
* Current line.
* @type {number}
*/
this.lineNum_ = 0; this.lineNum_ = 0;
/**
* CSV lines parser.
* @type {CsvParser}
*/
this.csvParser_ = new CsvParser(); this.csvParser_ = new CsvParser();
// Variables for tracking of 'current-time' log entries:
/**
* Keeps track of whether we've seen a "current-time" tick yet.
* @type {boolean}
*/
this.hasSeenTimerMarker_ = false; this.hasSeenTimerMarker_ = false;
/**
* List of log lines seen since last "current-time" tick.
* @type {Array.<String>}
*/
this.logLinesSinceLastTimerMarker_ = []; this.logLinesSinceLastTimerMarker_ = [];
} }
/**
* @param {Object} table A table used for parsing and processing
* log records.
* exampleDispatchTable = {
* "log-entry-XXX": {
* parser: [parseString, parseInt, ..., parseVarArgs],
* processor: this.processXXX.bind(this)
* },
* ...
* }
*/
setDispatchTable(table) {
if (Object.getPrototypeOf(table) !== null) {
throw new Error("Dispatch expected table.__proto__=null for speedup");
}
for (let name in table) {
const parser = table[name];
if (parser === undefined) continue;
if (!parser.isAsync) parser.isAsync = false;
if (!Array.isArray(parser.parsers)) {
throw new Error(`Invalid parsers: dispatchTable['${
name}'].parsers should be an Array.`);
}
let type = typeof parser.processor;
if (type !== 'function') {
throw new Error(`Invalid processor: typeof dispatchTable['${
name}'].processor is '${type}' instead of 'function'`);
}
if (!parser.processor.name.startsWith('bound ')) {
parser.processor = parser.processor.bind(this);
}
this.dispatchTable_.set(name, parser);
}
}
/** /**
* A thin wrapper around shell's 'read' function showing a file name on error. * A thin wrapper around shell's 'read' function showing a file name on error.
...@@ -118,7 +119,18 @@ export class LogReader { ...@@ -118,7 +119,18 @@ export class LogReader {
* @param {string} chunk A portion of log. * @param {string} chunk A portion of log.
*/ */
async processLogChunk(chunk) { async processLogChunk(chunk) {
await this.processLog_(chunk.split('\n')); let end = chunk.length;
let current = 0;
// Kept for debugging in case of parsing errors.
let lineNumber = 0;
while (current < end) {
const next = chunk.indexOf("\n", current);
if (next === -1) break;
lineNumber++;
const line = chunk.substring(current, next);
current = next + 1;
await this.processLogLine(line);
}
} }
/** /**
...@@ -162,18 +174,19 @@ export class LogReader { ...@@ -162,18 +174,19 @@ export class LogReader {
processStack(pc, func, stack) { processStack(pc, func, stack) {
const fullStack = func ? [pc, func] : [pc]; const fullStack = func ? [pc, func] : [pc];
let prevFrame = pc; let prevFrame = pc;
for (let i = 0, n = stack.length; i < n; ++i) { const length = stack.length;
for (let i = 0, n = length; i < n; ++i) {
const frame = stack[i]; const frame = stack[i];
const firstChar = frame.charAt(0); const firstChar = frame[0];
if (firstChar == '+' || firstChar == '-') { if (firstChar === '+' || firstChar === '-') {
// An offset from the previous frame. // An offset from the previous frame.
prevFrame += parseInt(frame, 16); prevFrame += parseInt(frame, 16);
fullStack.push(prevFrame); fullStack.push(prevFrame);
// Filter out possible 'overflow' string. // Filter out possible 'overflow' string.
} else if (firstChar != 'o') { } else if (firstChar !== 'o') {
fullStack.push(parseInt(frame, 16)); fullStack.push(parseInt(frame, 16));
} else { } else {
console.error(`dropping: ${frame}`); console.error(`Dropping unknown tick frame: ${frame}`);
} }
} }
return fullStack; return fullStack;
...@@ -188,29 +201,23 @@ export class LogReader { ...@@ -188,29 +201,23 @@ export class LogReader {
async dispatchLogRow_(fields) { async dispatchLogRow_(fields) {
// Obtain the dispatch. // Obtain the dispatch.
const command = fields[0]; const command = fields[0];
const dispatch = this.dispatchTable_[command]; const dispatch = this.dispatchTable_.get(command);
if (dispatch === undefined) return; if (dispatch === undefined) return;
const parsers = dispatch.parsers; const parsers = dispatch.parsers;
const length = parsers.length; const length = parsers.length;
// Parse fields. // Parse fields.
const parsedFields = []; const parsedFields = new Array(length);
for (let i = 0; i < length; ++i) { for (let i = 0; i < length; ++i) {
const parser = parsers[i]; const parser = parsers[i];
if (parser === parseString) { if (parser === parseVarArgs) {
parsedFields.push(fields[1 + i]); parsedFields[i] = fields.slice(1 + i);
} else if (typeof parser == 'function') {
parsedFields.push(parser(fields[1 + i]));
} else if (parser === parseVarArgs) {
// var-args
parsedFields.push(fields.slice(1 + i));
break; break;
} else { } else {
throw new Error(`Invalid log field parser: ${parser}`); parsedFields[i] = parser(fields[1 + i]);
} }
} }
// Run the processor. // Run the processor.
await dispatch.processor.apply(this, parsedFields); await dispatch.processor(...parsedFields);
} }
/** /**
......
...@@ -746,7 +746,7 @@ function startOf(timestamp, time) { ...@@ -746,7 +746,7 @@ function startOf(timestamp, time) {
export class ParseProcessor extends LogReader { export class ParseProcessor extends LogReader {
constructor() { constructor() {
super(); super();
this.dispatchTable_ = { this.setDispatchTable({
// Avoid accidental leaking of __proto__ properties and force this object // Avoid accidental leaking of __proto__ properties and force this object
// to be in dictionary-mode. // to be in dictionary-mode.
__proto__: null, __proto__: null,
...@@ -780,7 +780,7 @@ export class ParseProcessor extends LogReader { ...@@ -780,7 +780,7 @@ export class ParseProcessor extends LogReader {
parsers: [parseInt, parseString, parseString], parsers: [parseInt, parseString, parseString],
processor: this.processScriptSource processor: this.processScriptSource
}, },
}; });
this.functionEventDispatchTable_ = { this.functionEventDispatchTable_ = {
// Avoid accidental leaking of __proto__ properties and force this object // Avoid accidental leaking of __proto__ properties and force this object
// to be in dictionary-mode. // to be in dictionary-mode.
...@@ -820,20 +820,7 @@ export class ParseProcessor extends LogReader { ...@@ -820,20 +820,7 @@ export class ParseProcessor extends LogReader {
} }
processString(string) { processString(string) {
let end = string.length; this.processLogChunk(string);
let current = 0;
let next = 0;
let line;
let i = 0;
let entry;
while (current < end) {
next = string.indexOf("\n", current);
if (next === -1) break;
i++;
line = string.substring(current, next);
current = next + 1;
this.processLogLine(line);
}
this.postProcess(); this.postProcess();
} }
......
...@@ -71,18 +71,19 @@ export class Processor extends LogReader { ...@@ -71,18 +71,19 @@ export class Processor extends LogReader {
parseInt, parseInt, parseInt, parseInt, parseString, parseString, parseInt, parseInt, parseInt, parseInt, parseString, parseString,
parseString, parseString, parseString, parseString parseString, parseString, parseString, parseString
]; ];
this.dispatchTable_ = { this.setDispatchTable({
__proto__: null, __proto__: null,
'v8-version': { 'v8-version': {
parsers: [ parsers: [
parseInt, parseInt,
parseInt, parseInt,
], ],
processor: this.processV8Version processor: this.processV8Version,
}, },
'shared-library': { 'shared-library': {
parsers: [parseString, parseInt, parseInt, parseInt], parsers: [parseString, parseInt, parseInt, parseInt],
processor: this.processSharedLibrary processor: this.processSharedLibrary.bind(this),
isAsync: true,
}, },
'code-creation': { 'code-creation': {
parsers: [ parsers: [
...@@ -190,7 +191,7 @@ export class Processor extends LogReader { ...@@ -190,7 +191,7 @@ export class Processor extends LogReader {
parsers: [parseString, parseVarArgs], parsers: [parseString, parseVarArgs],
processor: this.processApiEvent processor: this.processApiEvent
}, },
}; });
// TODO(cbruni): Choose correct cpp entries provider // TODO(cbruni): Choose correct cpp entries provider
this._cppEntriesProvider = new RemoteLinuxCppEntriesProvider(); this._cppEntriesProvider = new RemoteLinuxCppEntriesProvider();
} }
...@@ -209,6 +210,7 @@ export class Processor extends LogReader { ...@@ -209,6 +210,7 @@ export class Processor extends LogReader {
let current = 0; let current = 0;
let next = 0; let next = 0;
let line; let line;
let lineNumber = 1;
try { try {
while (current < end) { while (current < end) {
next = chunk.indexOf('\n', current); next = chunk.indexOf('\n', current);
...@@ -222,10 +224,12 @@ export class Processor extends LogReader { ...@@ -222,10 +224,12 @@ export class Processor extends LogReader {
this._chunkRemainder = ''; this._chunkRemainder = '';
} }
current = next + 1; current = next + 1;
lineNumber++;
await this.processLogLine(line); await this.processLogLine(line);
} }
} catch (e) { } catch (e) {
console.error(`Error occurred during parsing, trying to continue: ${e}`); console.error(
`Could not parse log line ${lineNumber}, trying to continue: ${e}`);
} }
} }
......
...@@ -74,7 +74,6 @@ DOM.defineCustomElement( ...@@ -74,7 +74,6 @@ DOM.defineCustomElement(
const flameStack = []; const flameStack = [];
const ticks = this._timeline.values; const ticks = this._timeline.values;
let maxDepth = 0; let maxDepth = 0;
for (let tickIndex = 0; tickIndex < ticks.length; tickIndex++) { for (let tickIndex = 0; tickIndex < ticks.length; tickIndex++) {
const tick = ticks[tickIndex]; const tick = ticks[tickIndex];
const tickStack = tick.stack; const tickStack = tick.stack;
......
...@@ -510,10 +510,8 @@ export class TickProcessor extends LogReader { ...@@ -510,10 +510,8 @@ export class TickProcessor extends LogReader {
onlySummary, onlySummary,
runtimeTimerFilter, runtimeTimerFilter,
preprocessJson) { preprocessJson) {
super({}, super(timedRange, pairwiseTimedRange);
timedRange, this.setDispatchTable({
pairwiseTimedRange);
this.dispatchTable_ = {
__proto__: null, __proto__: null,
'shared-library': { 'shared-library': {
parsers: [parseString, parseInt, parseInt, parseInt], parsers: [parseString, parseInt, parseInt, parseInt],
...@@ -586,7 +584,7 @@ export class TickProcessor extends LogReader { ...@@ -586,7 +584,7 @@ export class TickProcessor extends LogReader {
'code-allocate': undefined, 'code-allocate': undefined,
'begin-code-region': undefined, 'begin-code-region': undefined,
'end-code-region': undefined 'end-code-region': undefined
}; });
this.preprocessJson = preprocessJson; this.preprocessJson = preprocessJson;
this.cppEntriesProvider_ = cppEntriesProvider; this.cppEntriesProvider_ = cppEntriesProvider;
......
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