Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp/wazero: add memory debugging and correct Halt() implementation #28

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions interp/wazero/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"io"
"runtime"

"github.com/hybridgroup/mechanoid"
"github.com/hybridgroup/mechanoid/engine"
"github.com/orsinium-labs/wypes"
"github.com/tetratelabs/wazero"
Expand All @@ -23,6 +25,12 @@ func (i *Interpreter) Name() string {
}

func (i *Interpreter) Init() error {
return i.init()
}

func (i *Interpreter) init() error {
mechanoid.DebugMemory("Interpreter Init")

ctx := context.Background()
conf := wazero.NewRuntimeConfigInterpreter()
conf = conf.WithDebugInfoEnabled(false)
Expand All @@ -32,6 +40,8 @@ func (i *Interpreter) Init() error {
}

func (i *Interpreter) SetModules(modules wypes.Modules) error {
mechanoid.Log("Registering host modules...")

if i.modules == nil {
i.modules = modules
return nil
Expand All @@ -50,6 +60,14 @@ func (i *Interpreter) SetModules(modules wypes.Modules) error {
}

func (i *Interpreter) Load(code engine.Reader) error {
if i.runtime == nil {
if err := i.init(); err != nil {
return fmt.Errorf("init wazero runtime: %v", err)
}
}

mechanoid.DebugMemory("Interpreter Load")

err := i.defineModules()
if err != nil {
return fmt.Errorf("register wazero host modules: %v", err)
Expand Down Expand Up @@ -115,6 +133,8 @@ func wazeroAdaptHostFunc(hf wypes.HostFunc, refs wypes.Refs) api.GoModuleFunctio
}

func (i *Interpreter) Run() (engine.Instance, error) {
mechanoid.DebugMemory("Interpreter Run")

var err error
ctx := context.Background()
init := i.module.ExportedFunction("_initialize")
Expand All @@ -128,9 +148,17 @@ func (i *Interpreter) Run() (engine.Instance, error) {
}

func (i *Interpreter) Halt() error {
mechanoid.DebugMemory("Interpreter Halt")

ctx := context.Background()
err := i.runtime.Close(ctx)
i.runtime = nil
i.module = nil

// force a garbage collection to free memory
runtime.GC()
mechanoid.DebugMemory("Interpreter Halt after GC")

return err
}

Expand Down