Skip to content

Commit

Permalink
fixed shadow does not compile for windows #298
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Jan 14, 2025
1 parent faa96b8 commit 6883cb8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
18 changes: 18 additions & 0 deletions services/substrate/runtime/memory.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions services/substrate/runtime/memory_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 2 additions & 6 deletions services/substrate/runtime/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/mackerelio/go-osstat/memory"
)

var globalInstanceCount int64
Expand Down Expand Up @@ -61,22 +59,20 @@ 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
if maxMemory == 0 {
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())
Expand Down
File renamed without changes.

0 comments on commit 6883cb8

Please # to comment.