diff --git a/services/substrate/runtime/memory.go b/services/substrate/runtime/memory.go new file mode 100644 index 00000000..e14e9d18 --- /dev/null +++ b/services/substrate/runtime/memory.go @@ -0,0 +1,18 @@ +//go:build linux || darwin +// +build linux darwin + +package runtime + +import ( + "github.com/mackerelio/go-osstat/memory" +) + +func getTotalAndUsedMemory() (totalWithSwap uint64, usedWithSwap uint64, total uint64, used uint64, err error) { + mem, err := memory.Get() + if err != nil { + return 0, 0, 0, 0, err + } + totalWithSwap = mem.Total + mem.SwapTotal + usedWithSwap = mem.Used + mem.SwapUsed + return totalWithSwap, usedWithSwap, mem.Total, mem.Used, nil +} diff --git a/services/substrate/runtime/memory_windows.go b/services/substrate/runtime/memory_windows.go new file mode 100644 index 00000000..816f7989 --- /dev/null +++ b/services/substrate/runtime/memory_windows.go @@ -0,0 +1,20 @@ +//go:build windows +// +build windows + +package runtime + +import ( + "github.com/mackerelio/go-osstat/memory" +) + +func getTotalAndUsedMemory() (totalWithSwap uint64, usedWithSwap uint64, total uint64, used uint64, err error) { + mem, err := memory.Get() + if err != nil { + return 0, 0, 0, 0, err + } + totalWithSwap = mem.VirtualTotal + usedWithSwap = mem.VirtualTotal - mem.VirtualFree + total = mem.Total + used = mem.Used + return totalWithSwap, usedWithSwap, total, used, nil +} diff --git a/services/substrate/runtime/shadow.go b/services/substrate/runtime/shadow.go index 106dcfa8..7144d38b 100644 --- a/services/substrate/runtime/shadow.go +++ b/services/substrate/runtime/shadow.go @@ -6,8 +6,6 @@ import ( "sync" "sync/atomic" "time" - - "github.com/mackerelio/go-osstat/memory" ) var globalInstanceCount int64 @@ -61,14 +59,12 @@ func (f *Function) initShadow() { } } - mem, err := memory.Get() + totalMemAndSwap, usedMemAndSwap, memTotal, memUsed, err := getTotalAndUsedMemory() if err != nil { logger.Errorf("failed to get memory stats: %s", err.Error()) continue } - totalMemAndSwap := mem.Total + mem.SwapTotal - usedMemAndSwap := mem.Used + mem.SwapUsed usedMemoryPercentage := (usedMemAndSwap * 100) / totalMemAndSwap if usedMemoryPercentage < MemoryThreshold { maxMemory := f.config.Memory @@ -76,7 +72,7 @@ func (f *Function) initShadow() { maxMemory = DefaultWasmMemory } - if mem.Used+maxMemory <= mem.Total { + if memUsed+maxMemory <= memTotal { shadow, err := f.shadows.newInstance() if err != nil { logger.Errorf("creating new shadow instance failed with: %s", err.Error()) diff --git a/services/substrate/runtime/utils_test.go b/services/substrate/runtime/utils_for_test.go similarity index 100% rename from services/substrate/runtime/utils_test.go rename to services/substrate/runtime/utils_for_test.go