From affee2eba34813a5a4c447744e58d3d66a3fe31a Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Wed, 25 Sep 2024 04:14:21 +0800 Subject: [PATCH 1/4] refactor: make timeout configurable for jit execution --- validator/server_jit/jit_machine.go | 22 ++++++++++++---------- validator/server_jit/machine_loader.go | 16 +++++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/validator/server_jit/jit_machine.go b/validator/server_jit/jit_machine.go index 06c451bda1..77112f0635 100644 --- a/validator/server_jit/jit_machine.go +++ b/validator/server_jit/jit_machine.go @@ -26,13 +26,14 @@ import ( var jitWasmMemoryUsage = metrics.NewRegisteredHistogram("jit/wasm/memoryusage", nil, metrics.NewBoundedHistogramSample()) type JitMachine struct { - binary string - process *exec.Cmd - stdin io.WriteCloser - wasmMemoryUsageLimit int + binary string + process *exec.Cmd + stdin io.WriteCloser + wasmMemoryUsageLimit int + maxExecutionTimeInSeconds int } -func createJitMachine(jitBinary string, binaryPath string, cranelift bool, wasmMemoryUsageLimit int, moduleRoot common.Hash, fatalErrChan chan error) (*JitMachine, error) { +func createJitMachine(jitBinary string, binaryPath string, cranelift bool, wasmMemoryUsageLimit int, maxExecutionTimeInSeconds int, moduleRoot common.Hash, fatalErrChan chan error) (*JitMachine, error) { invocation := []string{"--binary", binaryPath, "--forks"} if cranelift { invocation = append(invocation, "--cranelift") @@ -51,10 +52,11 @@ func createJitMachine(jitBinary string, binaryPath string, cranelift bool, wasmM }() machine := &JitMachine{ - binary: binaryPath, - process: process, - stdin: stdin, - wasmMemoryUsageLimit: wasmMemoryUsageLimit, + binary: binaryPath, + process: process, + stdin: stdin, + wasmMemoryUsageLimit: wasmMemoryUsageLimit, + maxExecutionTimeInSeconds: maxExecutionTimeInSeconds, } return machine, nil } @@ -73,7 +75,7 @@ func (machine *JitMachine) prove( defer cancel() // ensure our cleanup functions run when we're done state := validator.GoGlobalState{} - timeout := time.Now().Add(60 * time.Second) + timeout := time.Now().Add(time.Duration(machine.maxExecutionTimeInSeconds) * time.Second) tcp, err := net.ListenTCP("tcp4", &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, }) diff --git a/validator/server_jit/machine_loader.go b/validator/server_jit/machine_loader.go index cfa475370c..342f5c37de 100644 --- a/validator/server_jit/machine_loader.go +++ b/validator/server_jit/machine_loader.go @@ -13,15 +13,17 @@ import ( ) type JitMachineConfig struct { - ProverBinPath string - JitCranelift bool - WasmMemoryUsageLimit int + ProverBinPath string + JitCranelift bool + WasmMemoryUsageLimit int + MaxExecutionTimeInSeconds int } var DefaultJitMachineConfig = JitMachineConfig{ - JitCranelift: true, - ProverBinPath: "replay.wasm", - WasmMemoryUsageLimit: 4294967296, + JitCranelift: true, + ProverBinPath: "replay.wasm", + WasmMemoryUsageLimit: 4294967296, + MaxExecutionTimeInSeconds: 60, } func getJitPath() (string, error) { @@ -59,7 +61,7 @@ func NewJitMachineLoader(config *JitMachineConfig, locator *server_common.Machin } createMachineThreadFunc := func(ctx context.Context, moduleRoot common.Hash) (*JitMachine, error) { binPath := filepath.Join(locator.GetMachinePath(moduleRoot), config.ProverBinPath) - return createJitMachine(jitPath, binPath, config.JitCranelift, config.WasmMemoryUsageLimit, moduleRoot, fatalErrChan) + return createJitMachine(jitPath, binPath, config.JitCranelift, config.WasmMemoryUsageLimit, config.MaxExecutionTimeInSeconds, moduleRoot, fatalErrChan) } return &JitMachineLoader{ MachineLoader: *server_common.NewMachineLoader[JitMachine](locator, createMachineThreadFunc), From c2d720cea420b03e77903ed0103e2e5cba45d86f Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Wed, 25 Sep 2024 04:51:11 +0800 Subject: [PATCH 2/4] refactor: move MaxExecutionTime to JitSpawner config --- validator/server_jit/jit_machine.go | 24 ++++++++++++------------ validator/server_jit/machine_loader.go | 19 +++++++++---------- validator/server_jit/spawner.go | 14 +++++++++----- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/validator/server_jit/jit_machine.go b/validator/server_jit/jit_machine.go index 77112f0635..2bea75fbe9 100644 --- a/validator/server_jit/jit_machine.go +++ b/validator/server_jit/jit_machine.go @@ -26,14 +26,14 @@ import ( var jitWasmMemoryUsage = metrics.NewRegisteredHistogram("jit/wasm/memoryusage", nil, metrics.NewBoundedHistogramSample()) type JitMachine struct { - binary string - process *exec.Cmd - stdin io.WriteCloser - wasmMemoryUsageLimit int - maxExecutionTimeInSeconds int + binary string + process *exec.Cmd + stdin io.WriteCloser + wasmMemoryUsageLimit int + maxExecutionTime time.Duration } -func createJitMachine(jitBinary string, binaryPath string, cranelift bool, wasmMemoryUsageLimit int, maxExecutionTimeInSeconds int, moduleRoot common.Hash, fatalErrChan chan error) (*JitMachine, error) { +func createJitMachine(jitBinary string, binaryPath string, cranelift bool, wasmMemoryUsageLimit int, maxExecutionTime time.Duration, moduleRoot common.Hash, fatalErrChan chan error) (*JitMachine, error) { invocation := []string{"--binary", binaryPath, "--forks"} if cranelift { invocation = append(invocation, "--cranelift") @@ -52,11 +52,11 @@ func createJitMachine(jitBinary string, binaryPath string, cranelift bool, wasmM }() machine := &JitMachine{ - binary: binaryPath, - process: process, - stdin: stdin, - wasmMemoryUsageLimit: wasmMemoryUsageLimit, - maxExecutionTimeInSeconds: maxExecutionTimeInSeconds, + binary: binaryPath, + process: process, + stdin: stdin, + wasmMemoryUsageLimit: wasmMemoryUsageLimit, + maxExecutionTime: maxExecutionTime, } return machine, nil } @@ -75,7 +75,7 @@ func (machine *JitMachine) prove( defer cancel() // ensure our cleanup functions run when we're done state := validator.GoGlobalState{} - timeout := time.Now().Add(time.Duration(machine.maxExecutionTimeInSeconds) * time.Second) + timeout := time.Now().Add(machine.maxExecutionTime) tcp, err := net.ListenTCP("tcp4", &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, }) diff --git a/validator/server_jit/machine_loader.go b/validator/server_jit/machine_loader.go index 342f5c37de..b2393120d5 100644 --- a/validator/server_jit/machine_loader.go +++ b/validator/server_jit/machine_loader.go @@ -7,23 +7,22 @@ import ( "path/filepath" "runtime" "strings" + "time" "github.com/ethereum/go-ethereum/common" "github.com/offchainlabs/nitro/validator/server_common" ) type JitMachineConfig struct { - ProverBinPath string - JitCranelift bool - WasmMemoryUsageLimit int - MaxExecutionTimeInSeconds int + ProverBinPath string + JitCranelift bool + WasmMemoryUsageLimit int } var DefaultJitMachineConfig = JitMachineConfig{ - JitCranelift: true, - ProverBinPath: "replay.wasm", - WasmMemoryUsageLimit: 4294967296, - MaxExecutionTimeInSeconds: 60, + JitCranelift: true, + ProverBinPath: "replay.wasm", + WasmMemoryUsageLimit: 4294967296, } func getJitPath() (string, error) { @@ -54,14 +53,14 @@ type JitMachineLoader struct { stopped bool } -func NewJitMachineLoader(config *JitMachineConfig, locator *server_common.MachineLocator, fatalErrChan chan error) (*JitMachineLoader, error) { +func NewJitMachineLoader(config *JitMachineConfig, locator *server_common.MachineLocator, MaxExecutionTime time.Duration, fatalErrChan chan error) (*JitMachineLoader, error) { jitPath, err := getJitPath() if err != nil { return nil, err } createMachineThreadFunc := func(ctx context.Context, moduleRoot common.Hash) (*JitMachine, error) { binPath := filepath.Join(locator.GetMachinePath(moduleRoot), config.ProverBinPath) - return createJitMachine(jitPath, binPath, config.JitCranelift, config.WasmMemoryUsageLimit, config.MaxExecutionTimeInSeconds, moduleRoot, fatalErrChan) + return createJitMachine(jitPath, binPath, config.JitCranelift, config.WasmMemoryUsageLimit, MaxExecutionTime, moduleRoot, fatalErrChan) } return &JitMachineLoader{ MachineLoader: *server_common.NewMachineLoader[JitMachine](locator, createMachineThreadFunc), diff --git a/validator/server_jit/spawner.go b/validator/server_jit/spawner.go index d77317d218..094943fd1d 100644 --- a/validator/server_jit/spawner.go +++ b/validator/server_jit/spawner.go @@ -3,10 +3,10 @@ package server_jit import ( "context" "fmt" + flag "github.com/spf13/pflag" "runtime" "sync/atomic" - - flag "github.com/spf13/pflag" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" @@ -18,8 +18,9 @@ import ( ) type JitSpawnerConfig struct { - Workers int `koanf:"workers" reload:"hot"` - Cranelift bool `koanf:"cranelift"` + Workers int `koanf:"workers" reload:"hot"` + Cranelift bool `koanf:"cranelift"` + MaxExecutionTime time.Duration `koanf:"max-execution-time" reload:"hot"` // TODO: change WasmMemoryUsageLimit to a string and use resourcemanager.ParseMemLimit WasmMemoryUsageLimit int `koanf:"wasm-memory-usage-limit"` @@ -30,6 +31,7 @@ type JitSpawnerConfigFecher func() *JitSpawnerConfig var DefaultJitSpawnerConfig = JitSpawnerConfig{ Workers: 0, Cranelift: true, + MaxExecutionTime: time.Minute * 10, WasmMemoryUsageLimit: 4294967296, // 2^32 WASM memeory limit } @@ -37,6 +39,7 @@ func JitSpawnerConfigAddOptions(prefix string, f *flag.FlagSet) { f.Int(prefix+".workers", DefaultJitSpawnerConfig.Workers, "number of concurrent validation threads") f.Bool(prefix+".cranelift", DefaultJitSpawnerConfig.Cranelift, "use Cranelift instead of LLVM when validating blocks using the jit-accelerated block validator") f.Int(prefix+".wasm-memory-usage-limit", DefaultJitSpawnerConfig.WasmMemoryUsageLimit, "if memory used by a jit wasm exceeds this limit, a warning is logged") + f.Duration(prefix+"max_execution_time", DefaultJitSpawnerConfig.MaxExecutionTime, "if execution time used by a jit wasm exceeds this limit, a rpc error is returned") } type JitSpawner struct { @@ -52,7 +55,8 @@ func NewJitSpawner(locator *server_common.MachineLocator, config JitSpawnerConfi machineConfig := DefaultJitMachineConfig machineConfig.JitCranelift = config().Cranelift machineConfig.WasmMemoryUsageLimit = config().WasmMemoryUsageLimit - loader, err := NewJitMachineLoader(&machineConfig, locator, fatalErrChan) + maxExecutionTime := config().MaxExecutionTime + loader, err := NewJitMachineLoader(&machineConfig, locator, maxExecutionTime, fatalErrChan) if err != nil { return nil, err } From 43645ce69c95fffc37decef9c4bccbcb46d431ef Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Wed, 25 Sep 2024 04:59:26 +0800 Subject: [PATCH 3/4] chore: rename some variable --- validator/server_jit/machine_loader.go | 4 ++-- validator/server_jit/spawner.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/validator/server_jit/machine_loader.go b/validator/server_jit/machine_loader.go index b2393120d5..3d8b01367f 100644 --- a/validator/server_jit/machine_loader.go +++ b/validator/server_jit/machine_loader.go @@ -53,14 +53,14 @@ type JitMachineLoader struct { stopped bool } -func NewJitMachineLoader(config *JitMachineConfig, locator *server_common.MachineLocator, MaxExecutionTime time.Duration, fatalErrChan chan error) (*JitMachineLoader, error) { +func NewJitMachineLoader(config *JitMachineConfig, locator *server_common.MachineLocator, maxExecutionTime time.Duration, fatalErrChan chan error) (*JitMachineLoader, error) { jitPath, err := getJitPath() if err != nil { return nil, err } createMachineThreadFunc := func(ctx context.Context, moduleRoot common.Hash) (*JitMachine, error) { binPath := filepath.Join(locator.GetMachinePath(moduleRoot), config.ProverBinPath) - return createJitMachine(jitPath, binPath, config.JitCranelift, config.WasmMemoryUsageLimit, MaxExecutionTime, moduleRoot, fatalErrChan) + return createJitMachine(jitPath, binPath, config.JitCranelift, config.WasmMemoryUsageLimit, maxExecutionTime, moduleRoot, fatalErrChan) } return &JitMachineLoader{ MachineLoader: *server_common.NewMachineLoader[JitMachine](locator, createMachineThreadFunc), diff --git a/validator/server_jit/spawner.go b/validator/server_jit/spawner.go index 094943fd1d..29acff246d 100644 --- a/validator/server_jit/spawner.go +++ b/validator/server_jit/spawner.go @@ -39,7 +39,7 @@ func JitSpawnerConfigAddOptions(prefix string, f *flag.FlagSet) { f.Int(prefix+".workers", DefaultJitSpawnerConfig.Workers, "number of concurrent validation threads") f.Bool(prefix+".cranelift", DefaultJitSpawnerConfig.Cranelift, "use Cranelift instead of LLVM when validating blocks using the jit-accelerated block validator") f.Int(prefix+".wasm-memory-usage-limit", DefaultJitSpawnerConfig.WasmMemoryUsageLimit, "if memory used by a jit wasm exceeds this limit, a warning is logged") - f.Duration(prefix+"max_execution_time", DefaultJitSpawnerConfig.MaxExecutionTime, "if execution time used by a jit wasm exceeds this limit, a rpc error is returned") + f.Duration(prefix+"max-execution-time", DefaultJitSpawnerConfig.MaxExecutionTime, "if execution time used by a jit wasm exceeds this limit, a rpc error is returned") } type JitSpawner struct { From 3ea5870c6127fa2db36a83ce5eccc4f7f0786219 Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Wed, 25 Sep 2024 05:03:52 +0800 Subject: [PATCH 4/4] fix: add . for max-execution-time --- validator/server_jit/spawner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/server_jit/spawner.go b/validator/server_jit/spawner.go index 29acff246d..f30b6e181a 100644 --- a/validator/server_jit/spawner.go +++ b/validator/server_jit/spawner.go @@ -39,7 +39,7 @@ func JitSpawnerConfigAddOptions(prefix string, f *flag.FlagSet) { f.Int(prefix+".workers", DefaultJitSpawnerConfig.Workers, "number of concurrent validation threads") f.Bool(prefix+".cranelift", DefaultJitSpawnerConfig.Cranelift, "use Cranelift instead of LLVM when validating blocks using the jit-accelerated block validator") f.Int(prefix+".wasm-memory-usage-limit", DefaultJitSpawnerConfig.WasmMemoryUsageLimit, "if memory used by a jit wasm exceeds this limit, a warning is logged") - f.Duration(prefix+"max-execution-time", DefaultJitSpawnerConfig.MaxExecutionTime, "if execution time used by a jit wasm exceeds this limit, a rpc error is returned") + f.Duration(prefix+".max-execution-time", DefaultJitSpawnerConfig.MaxExecutionTime, "if execution time used by a jit wasm exceeds this limit, a rpc error is returned") } type JitSpawner struct {