From 9dd6aba19211b89071fb0c4dba447752a48fe1fb Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Thu, 3 Mar 2022 14:07:35 +0900 Subject: [PATCH] add refresh_thread_num cache option --- go.mod | 2 +- go.sum | 4 ++-- x/wasm/config/config.go | 13 +++++++++++++ x/wasm/keeper/keeper.go | 6 ++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7a8ae4fb1..d35d14da7 100644 --- a/go.mod +++ b/go.mod @@ -121,7 +121,7 @@ require ( replace ( github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76 - github.com/CosmWasm/wasmvm => github.com/terra-money/wasmvm v0.16.4-0.20220223064700-00b867119589 + github.com/CosmWasm/wasmvm => github.com/terra-money/wasmvm v0.16.4-0.20220303060445-1ebe5807aa66 github.com/cosmos/cosmos-sdk => github.com/terra-money/cosmos-sdk v0.44.5-terra.2 github.com/cosmos/ledger-cosmos-go => github.com/terra-money/ledger-terra-go v0.11.2 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 diff --git a/go.sum b/go.sum index 166433516..77feaa0c9 100644 --- a/go.sum +++ b/go.sum @@ -803,8 +803,8 @@ github.com/terra-money/ledger-terra-go v0.11.2 h1:BVXZl+OhJOri6vFNjjVaTabRLApw9M github.com/terra-money/ledger-terra-go v0.11.2/go.mod h1:ClJ2XMj1ptcnONzKH+GhVPi7Y8pXIT+UzJ0TNt0tfZE= github.com/terra-money/tendermint v0.34.14-terra.2 h1:UYDDCI001ZNhs+aX09HjKVldUcz084q3RwLHUOSSZQU= github.com/terra-money/tendermint v0.34.14-terra.2/go.mod h1:FrwVm3TvsVicI9Z7FlucHV6Znfd5KBc/Lpp69cCwtk0= -github.com/terra-money/wasmvm v0.16.4-0.20220223064700-00b867119589 h1:0sA+UJS6NH0KTGuKlh3EU6zgIiAom7VIVhex2ymoITw= -github.com/terra-money/wasmvm v0.16.4-0.20220223064700-00b867119589/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A= +github.com/terra-money/wasmvm v0.16.4-0.20220303060445-1ebe5807aa66 h1:d1kSz5gbRraD1UU3X51xT1kIeYTJnrd6waqv9501QXc= +github.com/terra-money/wasmvm v0.16.4-0.20220303060445-1ebe5807aa66/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A= github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= diff --git a/x/wasm/config/config.go b/x/wasm/config/config.go index 4dae6095c..e6b7701a9 100644 --- a/x/wasm/config/config.go +++ b/x/wasm/config/config.go @@ -11,6 +11,7 @@ const ( DefaultContractQueryGasLimit = uint64(3000000) DefaultContractDebugMode = false DefaultContractMemoryCacheSize = uint32(100) + DefaultRefreshThreadNum = uint32(16) ) // DBDir used to store wasm data to @@ -28,6 +29,11 @@ type Config struct { // The WASM VM memory cache size in MiB not bytes ContractMemoryCacheSize uint32 `mapstructure:"contract-memory-cache-size"` + + // The number of background thread to refresh wasm cache. + // This background thread is to prevent memory leak which + // comes from reusing wasm module. + RefreshThreadNum uint32 `mapstructure:"refresh-thread-num"` } // DefaultConfig returns the default settings for WasmConfig @@ -36,6 +42,7 @@ func DefaultConfig() *Config { ContractQueryGasLimit: DefaultContractQueryGasLimit, ContractDebugMode: DefaultContractDebugMode, ContractMemoryCacheSize: DefaultContractMemoryCacheSize, + RefreshThreadNum: DefaultRefreshThreadNum, } } @@ -45,6 +52,7 @@ func GetConfig(appOpts servertypes.AppOptions) *Config { ContractQueryGasLimit: cast.ToUint64(appOpts.Get("wasm.contract-query-gas-limit")), ContractDebugMode: cast.ToBool(appOpts.Get("wasm.contract-debug-mode")), ContractMemoryCacheSize: cast.ToUint32(appOpts.Get("wasm.contract-memory-cache-size")), + RefreshThreadNum: cast.ToUint32(appOpts.Get("wasm.refresh-thread-num")), } } @@ -61,4 +69,9 @@ contract-debug-mode = "{{ .WASMConfig.ContractDebugMode }}" # The WASM VM memory cache size in MiB not bytes contract-memory-cache-size = "{{ .WASMConfig.ContractMemoryCacheSize }}" + +# The number of background thread to refresh wasm cache. +# This background thread is to prevent memory leak which +# comes from reusing wasm module. +refresh-thread-num = "{{ .WASMConfig.RefreshThreadNum }}" ` diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 0d59ffdc1..868497c96 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -65,12 +65,18 @@ func NewKeeper( wasmConfig.ContractMemoryCacheSize = config.DefaultContractMemoryCacheSize } + // prevent zero refresh thread + if wasmConfig.RefreshThreadNum == 0 { + wasmConfig.RefreshThreadNum = config.DefaultRefreshThreadNum + } + vm, err := wasmvm.NewVM( filepath.Join(homePath, config.DBDir), supportedFeatures, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, wasmConfig.ContractMemoryCacheSize, + wasmConfig.RefreshThreadNum, ) if err != nil {