-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add EVM profiler #2988
Merged
Merged
Add EVM profiler #2988
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
567f7c8
evm/vm: add optional evm profiler
jochem-brouwer 7a47482
fix submodule delete
jochem-brouwer f81882e
Merge branch 'master' into evm-performance-logger-sm3
jochem-brouwer 4768496
fix syntax
jochem-brouwer 07f5dd9
Merge branch 'master' into evm-performance-logger
acolytec3 7a337c8
vm: update logger option names
jochem-brouwer de824c8
client: fix performance opts
jochem-brouwer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,9 @@ | |
|
||
import type { EVM } from './evm.js' | ||
import type { Journal } from './journal.js' | ||
import type { EVMPerformanceLogger, Timer } from './logger.js' | ||
import type { AsyncOpHandler, OpHandler, Opcode } from './opcodes/index.js' | ||
import type { Block, Blockchain, EVMResult, Log } from './types.js' | ||
import type { Block, Blockchain, EVMProfilerOpts, EVMResult, Log } from './types.js' | ||
import type { Common, EVMStateManagerInterface } from '@ethereumjs/common' | ||
import type { Address } from '@ethereumjs/util' | ||
const { debug: createDebugLogger } = debugDefault | ||
|
@@ -133,14 +134,19 @@ | |
// Opcode debuggers (e.g. { 'push': [debug Object], 'sstore': [debug Object], ...}) | ||
private opDebuggers: { [key: string]: (debug: string) => void } = {} | ||
|
||
private profilerOpts?: EVMProfilerOpts | ||
private performanceLogger: EVMPerformanceLogger | ||
|
||
// TODO remove gasLeft as constructor argument | ||
constructor( | ||
evm: EVM, | ||
stateManager: EVMStateManagerInterface, | ||
blockchain: Blockchain, | ||
env: Env, | ||
gasLeft: bigint, | ||
journal: Journal | ||
journal: Journal, | ||
performanceLogs: EVMPerformanceLogger, | ||
profilerOpts?: EVMProfilerOpts | ||
) { | ||
this._evm = evm | ||
this._stateManager = stateManager | ||
|
@@ -171,6 +177,7 @@ | |
returnValue: undefined, | ||
selfdestruct: new Set(), | ||
} | ||
;(this.profilerOpts = profilerOpts), (this.performanceLogger = performanceLogs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this syntax? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea 😂 I definitely did not write this, I think I made a weird syntax mistake and then the linter did this. Will fix |
||
} | ||
|
||
async run(code: Uint8Array, opts: InterpreterOpts = {}): Promise<InterpreterResult> { | ||
|
@@ -259,41 +266,54 @@ | |
async runStep(): Promise<void> { | ||
const opInfo = this.lookupOpInfo(this._runState.opCode) | ||
|
||
let gas = BigInt(opInfo.fee) | ||
// clone the gas limit; call opcodes can add stipend, | ||
// which makes it seem like the gas left increases | ||
const gasLimitClone = this.getGasLeft() | ||
|
||
if (opInfo.dynamicGas) { | ||
const dynamicGasHandler = (this._evm as any)._dynamicGasHandlers.get(this._runState.opCode)! | ||
// This function updates the gas in-place. | ||
// It needs the base fee, for correct gas limit calculation for the CALL opcodes | ||
gas = await dynamicGasHandler(this._runState, gas, this.common) | ||
} | ||
let timer: Timer | ||
|
||
if (this._evm.events.listenerCount('step') > 0 || this._evm.DEBUG) { | ||
// Only run this stepHook function if there is an event listener (e.g. test runner) | ||
// or if the vm is running in debug mode (to display opcode debug logs) | ||
await this._runStepHook(gas, gasLimitClone) | ||
if (this.profilerOpts?.enabled === true) { | ||
timer = this.performanceLogger.startTimer(opInfo.name) | ||
} | ||
|
||
// Check for invalid opcode | ||
if (opInfo.name === 'INVALID') { | ||
throw new EvmError(ERROR.INVALID_OPCODE) | ||
} | ||
let gas = BigInt(opInfo.fee) | ||
|
||
// Reduce opcode's base fee | ||
this.useGas(gas, `${opInfo.name} fee`) | ||
// Advance program counter | ||
this._runState.programCounter++ | ||
try { | ||
acolytec3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// clone the gas limit; call opcodes can add stipend, | ||
// which makes it seem like the gas left increases | ||
const gasLimitClone = this.getGasLeft() | ||
|
||
// Execute opcode handler | ||
const opFn = this.getOpHandler(opInfo) | ||
if (opInfo.dynamicGas) { | ||
const dynamicGasHandler = (this._evm as any)._dynamicGasHandlers.get(this._runState.opCode)! | ||
// This function updates the gas in-place. | ||
// It needs the base fee, for correct gas limit calculation for the CALL opcodes | ||
gas = await dynamicGasHandler(this._runState, gas, this.common) | ||
} | ||
|
||
if (opInfo.isAsync) { | ||
await (opFn as AsyncOpHandler).apply(null, [this._runState, this.common]) | ||
} else { | ||
opFn.apply(null, [this._runState, this.common]) | ||
if (this._evm.events.listenerCount('step') > 0 || this._evm.DEBUG) { | ||
// Only run this stepHook function if there is an event listener (e.g. test runner) | ||
// or if the vm is running in debug mode (to display opcode debug logs) | ||
await this._runStepHook(gas, gasLimitClone) | ||
} | ||
|
||
// Check for invalid opcode | ||
if (opInfo.name === 'INVALID') { | ||
throw new EvmError(ERROR.INVALID_OPCODE) | ||
acolytec3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Reduce opcode's base fee | ||
this.useGas(gas, `${opInfo.name} fee`) | ||
// Advance program counter | ||
this._runState.programCounter++ | ||
|
||
// Execute opcode handler | ||
const opFn = this.getOpHandler(opInfo) | ||
|
||
if (opInfo.isAsync) { | ||
await (opFn as AsyncOpHandler).apply(null, [this._runState, this.common]) | ||
} else { | ||
opFn.apply(null, [this._runState, this.common]) | ||
} | ||
} finally { | ||
if (this.profilerOpts?.enabled === true) { | ||
this.performanceLogger.stopTimer(timer!, Number(gas), 'opcodes') | ||
} | ||
} | ||
} | ||
|
||
|
@@ -872,7 +892,14 @@ | |
return BigInt(0) | ||
} | ||
|
||
let timer: Timer | ||
if (this.profilerOpts?.enabled === true) { | ||
timer = this.performanceLogger.pauseTimer() | ||
} | ||
const results = await this._evm.runCall({ message: msg }) | ||
if (this.profilerOpts?.enabled === true) { | ||
this.performanceLogger.unpauseTimer(timer!) | ||
} | ||
|
||
if (results.execResult.logs) { | ||
this._result.logs = this._result.logs.concat(results.execResult.logs) | ||
|
@@ -971,7 +998,14 @@ | |
message.createdAddresses = createdAddresses | ||
} | ||
|
||
let timer: Timer | ||
if (this.profilerOpts?.enabled === true) { | ||
timer = this.performanceLogger.pauseTimer() | ||
} | ||
const results = await this._evm.runCall({ message }) | ||
if (this.profilerOpts?.enabled === true) { | ||
this.performanceLogger.unpauseTimer(timer!) | ||
} | ||
|
||
if (results.execResult.logs) { | ||
this._result.logs = this._result.logs.concat(results.execResult.logs) | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is cool! 🤩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know that you had integrated so deeply into client already...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the only thing not to forget is; it does not work if you don't set the
DEBUG=ethjs,evm:profiler
env, so this is something we can improve (but not sure how to do this cleanly)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should just be able to set the debug variable programmatically with this
process.env.DEBUG = 'ethjs,evm:profiler'
. So just add a code block to therun
command in the client that checks for either of these args and then sets the debug accordingly. Or better, do something like this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, I haven't tested the above code but I do something like this in one of the
trie
demos to force it to output certain debug logs.