Skip to content

Commit

Permalink
Add metrics for how many Stylus calls and gas used
Browse files Browse the repository at this point in the history
  • Loading branch information
amsanghi committed Dec 23, 2024
1 parent d74229c commit 562747c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions arbos/programs/programs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
gethParams "github.com/ethereum/go-ethereum/params"

"github.com/offchainlabs/nitro/arbcompress"
Expand Down Expand Up @@ -163,6 +164,21 @@ func (p Programs) ActivateProgram(evm *vm.EVM, address common.Address, arbosVers
return stylusVersion, codeHash, info.moduleHash, dataFee, false, p.setProgram(codeHash, programData)
}

func runModeToString(runmode core.MessageRunMode) string {
switch runmode {
case core.MessageCommitMode:
return "commit_runmode"
case core.MessageGasEstimationMode:
return "gas_estimation_runmode"
case core.MessageEthcallMode:
return "eth_call_runmode"
case core.MessageReplayMode:
return "replay_runmode"
default:
return "unknown_runmode"
}
}

func (p Programs) CallProgram(
scope *vm.ScopeContext,
statedb vm.StateDB,
Expand Down Expand Up @@ -250,7 +266,10 @@ func (p Programs) CallProgram(
if runmode == core.MessageCommitMode {
arbos_tag = statedb.Database().WasmCacheTag()
}

metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/program_calls/%s", runModeToString(runmode)), nil).Inc(1)
ret, err := callProgram(address, moduleHash, localAsm, scope, interpreter, tracingInfo, calldata, evmData, goParams, model, arbos_tag)
gasUsed := callCost
if len(ret) > 0 && arbosVersion >= gethParams.ArbosVersion_StylusFixes {
// Ensure that return data costs as least as much as it would in the EVM.
evmCost := evmMemoryCost(uint64(len(ret)))
Expand All @@ -260,7 +279,12 @@ func (p Programs) CallProgram(
}
maxGasToReturn := startingGas - evmCost
contract.Gas = am.MinInt(contract.Gas, maxGasToReturn)
if evmCost > gasUsed {
gasUsed = evmCost
}
}
// #nosec G115
metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/gas_used/%s", runModeToString(runmode)), nil).Inc(int64(gasUsed))
return ret, err
}

Expand Down

0 comments on commit 562747c

Please sign in to comment.