Skip to content

Commit

Permalink
Merge pull request #2852 from OffchainLabs/stylus_metrics
Browse files Browse the repository at this point in the history
Add metrics for how many Stylus calls and gas used
  • Loading branch information
tsahee authored Jan 3, 2025
2 parents ab51cbf + d90cf0a commit 910cd85
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 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,17 +266,23 @@ 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)
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)))
if startingGas < evmCost {
contract.Gas = 0
// #nosec G115
metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/gas_used/%s", runModeToString(runmode)), nil).Inc(int64(startingGas))
return nil, vm.ErrOutOfGas
}
maxGasToReturn := startingGas - evmCost
contract.Gas = am.MinInt(contract.Gas, maxGasToReturn)
}
// #nosec G115
metrics.GetOrRegisterCounter(fmt.Sprintf("arb/arbos/stylus/gas_used/%s", runModeToString(runmode)), nil).Inc(int64(startingGas - contract.Gas))
return ret, err
}

Expand Down

0 comments on commit 910cd85

Please sign in to comment.