Skip to content

Commit

Permalink
add refresh_thread_num cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed Mar 3, 2022
1 parent b225a2e commit 9dd6aba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -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=
Expand Down
13 changes: 13 additions & 0 deletions x/wasm/config/config.go
Expand Up @@ -11,6 +11,7 @@ const (
DefaultContractQueryGasLimit = uint64(3000000)
DefaultContractDebugMode = false
DefaultContractMemoryCacheSize = uint32(100)
DefaultRefreshThreadNum = uint32(16)
)

// DBDir used to store wasm data to
Expand All @@ -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
Expand All @@ -36,6 +42,7 @@ func DefaultConfig() *Config {
ContractQueryGasLimit: DefaultContractQueryGasLimit,
ContractDebugMode: DefaultContractDebugMode,
ContractMemoryCacheSize: DefaultContractMemoryCacheSize,
RefreshThreadNum: DefaultRefreshThreadNum,
}
}

Expand All @@ -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")),
}
}

Expand All @@ -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 }}"
`
6 changes: 6 additions & 0 deletions x/wasm/keeper/keeper.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 9dd6aba

Please sign in to comment.