From 7cd1330acd9802ee5709b324ba584f3882d8d2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Wed, 7 Oct 2020 15:52:03 +0100 Subject: [PATCH] make vm.EnableGasTracing public. --- chain/vm/runtime.go | 9 +++++---- chain/vm/runtime_test.go | 20 ++++++++++++++++++++ chain/vm/vm.go | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/chain/vm/runtime.go b/chain/vm/runtime.go index 72dd413ed..c612e374f 100644 --- a/chain/vm/runtime.go +++ b/chain/vm/runtime.go @@ -28,6 +28,9 @@ import ( "github.com/filecoin-project/lotus/chain/types" ) +// EnableGasTracing, if true, outputs gas tracing in execution traces. +var EnableGasTracing = false + type Runtime struct { types.Message rt0.Syscalls @@ -459,7 +462,7 @@ func (rt *Runtime) stateCommit(oldh, newh cid.Cid) aerrors.ActorError { } func (rt *Runtime) finilizeGasTracing() { - if enableTracing { + if EnableGasTracing { if rt.lastGasCharge != nil { rt.lastGasCharge.TimeTaken = time.Since(rt.lastGasChargeTime) } @@ -491,11 +494,9 @@ func (rt *Runtime) chargeGasFunc(skip int) func(GasCharge) { } -var enableTracing = false - func (rt *Runtime) chargeGasInternal(gas GasCharge, skip int) aerrors.ActorError { toUse := gas.Total() - if enableTracing { + if EnableGasTracing { var callers [10]uintptr cout := 0 //gruntime.Callers(2+skip, callers[:]) diff --git a/chain/vm/runtime_test.go b/chain/vm/runtime_test.go index c22a8b615..9fc87f7c5 100644 --- a/chain/vm/runtime_test.go +++ b/chain/vm/runtime_test.go @@ -45,3 +45,23 @@ func TestRuntimePutErrors(t *testing.T) { rt.StorePut(&NotAVeryGoodMarshaler{}) t.Error("expected panic") } + +func BenchmarkRuntime_CreateRuntimeChargeGas_TracingDisabled(b *testing.B) { + var ( + cst = cbor.NewCborStore(nil) + gch = newGasCharge("foo", 1000, 1000) + ) + + b.ResetTimer() + + EnableGasTracing = false + noop := func() bool { return EnableGasTracing } + for n := 0; n < b.N; n++ { + // flip the value and access it to make sure + // the compiler doesn't optimize away + EnableGasTracing = true + _ = noop() + EnableGasTracing = false + _ = (&Runtime{cst: cst}).chargeGasInternal(gch, 0) + } +} diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 44979454f..e2ccd33c7 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -227,7 +227,7 @@ func (vm *VM) send(ctx context.Context, msg *types.Message, parent *Runtime, } rt := vm.makeRuntime(ctx, msg, origin, on, gasUsed, nac) - if enableTracing { + if EnableGasTracing { rt.lastGasChargeTime = start if parent != nil { rt.lastGasChargeTime = parent.lastGasChargeTime